trie

package
v1.9.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 1, 2023 License: GPL-3.0 Imports: 24 Imported by: 1

Documentation

Overview

Package trie implements Merkle Patricia Tries.

Index

Constants

This section is empty.

Variables

View Source
var ErrAlreadyProcessed = errors.New("already processed")

ErrAlreadyProcessed is returned by the trie sync when it's requested to process a node it already processed previously.

View Source
var ErrCommitDisabled = errors.New("no database for committing")
View Source
var ErrNotRequested = errors.New("not requested")

ErrNotRequested is returned by the trie sync when it's requested to process a node it did not request.

Functions

func GenerateRanges added in v1.9.0

func GenerateRanges(length int, numThreads int) []int

func ParallelWorker added in v1.9.0

func ParallelWorker(total, nThds int, worker func(start, end, idx int, args ...interface{}), args ...interface{})

func ResolvePath added in v1.8.0

func ResolvePath(path []byte) (common.Hash, []byte)

ResolvePath resolves the provided composite node path by separating the path in account trie if it's existent.

func VerifyProof

func VerifyProof(rootHash common.Hash, key []byte, proofDb ethdb.KeyValueReader) (value []byte, err error)

VerifyProof checks merkle proofs. The given proof must contain the value for key in a trie with the given root hash. VerifyProof returns an error if the proof contains invalid trie nodes or the wrong value.

func VerifyRangeProof

func VerifyRangeProof(rootHash common.Hash, firstKey []byte, lastKey []byte, keys [][]byte, values [][]byte, proof ethdb.KeyValueReader) (bool, error)

VerifyRangeProof checks whether the given leaf nodes and edge proof can prove the given trie leaves range is matched with the specific root. Besides, the range should be consecutive (no gap inside) and monotonic increasing.

Note the given proof actually contains two edge proofs. Both of them can be non-existent proofs. For example the first proof is for a non-existent key 0x03, the last proof is for a non-existent key 0x10. The given batch leaves are [0x04, 0x05, .. 0x09]. It's still feasible to prove the given batch is valid.

The firstKey is paired with firstProof, not necessarily the same as keys[0] (unless firstProof is an existent proof). Similarly, lastKey and lastProof are paired.

Expect the normal case, this function can also be used to verify the following range proofs:

  • All elements proof. In this case the proof can be nil, but the range should be all the leaves in the trie.

  • One element proof. In this case no matter the edge proof is a non-existent proof or not, we can always verify the correctness of the proof.

  • Zero element proof. In this case a single non-existent proof is enough to prove. Besides, if there are still some other leaves available on the right side, then an error will be returned.

Except returning the error to indicate the proof is valid or not, the function will also return a flag to indicate whether there exists more accounts/slots in the trie.

Note: This method does not verify that the proof is of minimal form. If the input proofs are 'bloated' with neighbour leaves or random data, aside from the 'useful' data, then the proof will still be accepted.

Types

type AccessListCache added in v1.9.0

type AccessListCache struct {
	// contains filtered or unexported fields
}

func NewAccessListCaches added in v1.9.0

func NewAccessListCaches(num int) []*AccessListCache

func (*AccessListCache) Add added in v1.9.0

func (this *AccessListCache) Add(key []byte, val []byte)

func (*AccessListCache) Merge added in v1.9.0

func (this *AccessListCache) Merge(accesses ...*AccessListCache)

func (*AccessListCache) ToMap added in v1.9.0

func (this *AccessListCache) ToMap() map[string][]byte

func (*AccessListCache) Unique added in v1.9.0

func (this *AccessListCache) Unique() ([]string, [][]byte)

type CodeSyncResult added in v1.8.0

type CodeSyncResult struct {
	Hash common.Hash // Hash the originally unknown bytecode
	Data []byte      // Data content of the retrieved bytecode
}

CodeSyncResult is a response with requested bytecode along with its hash.

type Config

type Config struct {
	Cache     int    // Memory allowance (MB) to use for caching trie nodes in memory
	Journal   string // Journal of clean cache to survive node restarts
	Preimages bool   // Flag whether the preimage of trie key is recorded
}

Config defines all necessary options for database.

type Database

type Database struct {
	// contains filtered or unexported fields
}

Database is the wrapper of the underlying backend which is shared by different types of node backend as an entrypoint. It's responsible for all interactions relevant with trie nodes and node preimages.

func NewDatabase

func NewDatabase(diskdb ethdb.Database) *Database

NewDatabase initializes the trie database with default settings, namely the legacy hash-based scheme is used by default.

func NewDatabaseWithConfig

func NewDatabaseWithConfig(diskdb ethdb.Database, config *Config) *Database

NewDatabaseWithConfig initializes the trie database with provided configs. The path-based scheme is not activated yet, always initialized with legacy hash-based scheme by default.

func NewParallelDatabase added in v1.9.0

func NewParallelDatabase(diskdbs [16]ethdb.Database, config *Config) *Database

func (*Database) Cap

func (db *Database) Cap(limit common.StorageSize) error

Cap iteratively flushes old but still referenced trie nodes until the total memory usage goes below the given threshold. The held pre-images accumulated up to this point will be flushed in case the size exceeds the threshold.

It's only supported by hash-based database and will return an error for others.

func (*Database) Close added in v1.8.0

func (db *Database) Close() error

Close flushes the dangling preimages to disk and closes the trie database. It is meant to be called when closing the blockchain object, so that all resources held can be released correctly.

func (*Database) Commit

func (db *Database) Commit(root common.Hash, report bool) error

Commit iterates over all the children of a particular node, writes them out to disk. As a side effect, all pre-images accumulated up to this point are also written.

func (*Database) Dereference

func (db *Database) Dereference(root common.Hash) error

Dereference removes an existing reference from a root node. It's only supported by hash-based database and will return an error for others.

func (*Database) Initialized added in v1.8.0

func (db *Database) Initialized(genesisRoot common.Hash) bool

Initialized returns an indicator if the state data is already initialized according to the state scheme.

func (*Database) Node

func (db *Database) Node(hash common.Hash) ([]byte, error)

Node retrieves the rlp-encoded node blob with provided node hash. It's only supported by hash-based database and will return an error for others. Note, this function should be deprecated once ETH66 is deprecated.

func (*Database) Reader added in v1.8.0

func (db *Database) Reader(blockRoot common.Hash) Reader

Reader returns a reader for accessing all trie nodes with provided state root. Nil is returned in case the state is not available.

func (*Database) Reference

func (db *Database) Reference(root common.Hash, parent common.Hash) error

Reference adds a new reference from a parent node to a child node. This function is used to add reference between internal trie node and external node(e.g. storage trie root), all internal trie nodes are referenced together by database itself.

It's only supported by hash-based database and will return an error for others.

func (*Database) SaveCache

func (db *Database) SaveCache(dir string) error

SaveCache atomically saves fast cache data to the given dir using all available CPU cores.

func (*Database) SaveCachePeriodically

func (db *Database) SaveCachePeriodically(dir string, interval time.Duration, stopCh <-chan struct{})

SaveCachePeriodically atomically saves fast cache data to the given dir with the specified interval. All dump operation will only use a single CPU core.

func (*Database) Scheme added in v1.8.0

func (db *Database) Scheme() string

Scheme returns the node scheme used in the database.

func (*Database) Size

Size returns the storage size of dirty trie nodes in front of the persistent database and the size of cached preimages.

func (*Database) Update added in v1.8.0

func (db *Database) Update(root common.Hash, parent common.Hash, nodes *trienode.MergedNodeSet) error

Update performs a state transition by committing dirty nodes contained in the given set in order to update state from the specified parent to the specified root. The held pre-images accumulated up to this point will be flushed in case the size exceeds the threshold.

type ID added in v1.8.0

type ID struct {
	StateRoot common.Hash // The root of the corresponding state(block.root)
	Owner     common.Hash // The contract address hash which the trie belongs to
	Root      common.Hash // The root hash of trie
}

ID is the identifier for uniquely identifying a trie.

func StateTrieID added in v1.8.0

func StateTrieID(root common.Hash) *ID

StateTrieID constructs an identifier for state trie with the provided state root.

func StorageTrieID added in v1.8.0

func StorageTrieID(stateRoot common.Hash, owner common.Hash, root common.Hash) *ID

StorageTrieID constructs an identifier for storage trie which belongs to a certain state and contract specified by the stateRoot and owner.

func TrieID added in v1.8.0

func TrieID(root common.Hash) *ID

TrieID constructs an identifier for a standard trie(not a second-layer trie) with provided root. It's mostly used in tests and some other tries like CHT trie.

type Iterator

type Iterator struct {
	Key   []byte // Current data key on which the iterator is positioned on
	Value []byte // Current data value on which the iterator is positioned on
	Err   error
	// contains filtered or unexported fields
}

Iterator is a key-value trie iterator that traverses a Trie.

func NewIterator

func NewIterator(it NodeIterator) *Iterator

NewIterator creates a new key-value iterator from a node iterator. Note that the value returned by the iterator is raw. If the content is encoded (e.g. storage value is RLP-encoded), it's caller's duty to decode it.

func (*Iterator) Next

func (it *Iterator) Next() bool

Next moves the iterator forward one key-value entry.

func (*Iterator) Prove

func (it *Iterator) Prove() [][]byte

Prove generates the Merkle proof for the leaf node the iterator is currently positioned on.

type LeafCallback

type LeafCallback func(keys [][]byte, path []byte, leaf []byte, parent common.Hash, parentPath []byte) error

LeafCallback is a callback type invoked when a trie operation reaches a leaf node.

The keys is a path tuple identifying a particular trie node either in a single trie (account) or a layered trie (account -> storage). Each key in the tuple is in the raw format(32 bytes).

The path is a composite hexary path identifying the trie node. All the key bytes are converted to the hexary nibbles and composited with the parent path if the trie node is in a layered trie.

It's used by state sync and commit to allow handling external references between account and storage tries. And also it's used in the state healing for extracting the raw states(leaf nodes) with corresponding paths.

type MissingNodeError

type MissingNodeError struct {
	Owner    common.Hash // owner of the trie if it's 2-layered trie
	NodeHash common.Hash // hash of the missing node
	Path     []byte      // hex-encoded path to the missing node
	// contains filtered or unexported fields
}

MissingNodeError is returned by the trie functions (Get, Update, Delete) in the case where a trie node is not present in the local database. It contains information necessary for retrieving the missing node.

func (*MissingNodeError) Error

func (err *MissingNodeError) Error() string

func (*MissingNodeError) Unwrap added in v1.8.0

func (err *MissingNodeError) Unwrap() error

Unwrap returns the concrete error for missing trie node which allows us for further analysis outside.

type NodeIterator

type NodeIterator interface {
	// Next moves the iterator to the next node. If the parameter is false, any child
	// nodes will be skipped.
	Next(bool) bool

	// Error returns the error status of the iterator.
	Error() error

	// Hash returns the hash of the current node.
	Hash() common.Hash

	// Parent returns the hash of the parent of the current node. The hash may be the one
	// grandparent if the immediate parent is an internal node with no hash.
	Parent() common.Hash

	// Path returns the hex-encoded path to the current node.
	// Callers must not retain references to the return value after calling Next.
	// For leaf nodes, the last element of the path is the 'terminator symbol' 0x10.
	Path() []byte

	// NodeBlob returns the rlp-encoded value of the current iterated node.
	// If the node is an embedded node in its parent, nil is returned then.
	NodeBlob() []byte

	// Leaf returns true iff the current node is a leaf node.
	Leaf() bool

	// LeafKey returns the key of the leaf. The method panics if the iterator is not
	// positioned at a leaf. Callers must not retain references to the value after
	// calling Next.
	LeafKey() []byte

	// LeafBlob returns the content of the leaf. The method panics if the iterator
	// is not positioned at a leaf. Callers must not retain references to the value
	// after calling Next.
	LeafBlob() []byte

	// LeafProof returns the Merkle proof of the leaf. The method panics if the
	// iterator is not positioned at a leaf. Callers must not retain references
	// to the value after calling Next.
	LeafProof() [][]byte

	// AddResolver sets a node resolver to use for looking up trie nodes before
	// reaching into the real persistent layer.
	//
	// This is not required for normal operation, rather is an optimization for
	// cases where trie nodes can be recovered from some external mechanism without
	// reading from disk. In those cases, this resolver allows short circuiting
	// accesses and returning them from memory.
	//
	// Before adding a similar mechanism to any other place in Geth, consider
	// making trie.Database an interface and wrapping at that level. It's a huge
	// refactor, but it could be worth it if another occurrence arises.
	AddResolver(NodeResolver)
}

NodeIterator is an iterator to traverse the trie pre-order.

func NewDifferenceIterator

func NewDifferenceIterator(a, b NodeIterator) (NodeIterator, *int)

NewDifferenceIterator constructs a NodeIterator that iterates over elements in b that are not in a. Returns the iterator, and a pointer to an integer recording the number of nodes seen.

func NewUnionIterator

func NewUnionIterator(iters []NodeIterator) (NodeIterator, *int)

NewUnionIterator constructs a NodeIterator that iterates over elements in the union of the provided NodeIterators. Returns the iterator, and a pointer to an integer recording the number of nodes visited.

type NodeReader added in v1.8.0

type NodeReader interface {
	// Reader returns a reader for accessing all trie nodes with provided
	// state root. Nil is returned in case the state is not available.
	Reader(root common.Hash) Reader
}

NodeReader wraps all the necessary functions for accessing trie node.

type NodeResolver added in v1.8.0

type NodeResolver func(owner common.Hash, path []byte, hash common.Hash) []byte

NodeResolver is used for looking up trie nodes before reaching into the real persistent layer. This is not mandatory, rather is an optimization for cases where trie nodes can be recovered from some external mechanism without reading from disk. In those cases, this resolver allows short circuiting accesses and returning them from memory.

type NodeSyncResult added in v1.8.0

type NodeSyncResult struct {
	Path string // Path of the originally unknown trie node
	Data []byte // Data content of the retrieved trie node
}

NodeSyncResult is a response with requested trie node along with its node path.

type NodeWriteFunc added in v1.8.0

type NodeWriteFunc = func(owner common.Hash, path []byte, hash common.Hash, blob []byte)

NodeWriteFunc is used to provide all information of a dirty node for committing so that callers can flush nodes into database with desired scheme.

type Reader added in v1.8.0

type Reader interface {
	// Node retrieves the RLP-encoded trie node blob with the provided trie
	// identifier, node path and the corresponding node hash. No error will
	// be returned if the node is not found.
	Node(owner common.Hash, path []byte, hash common.Hash) ([]byte, error)
}

Reader wraps the Node method of a backing trie store.

type SecureTrie

type SecureTrie = StateTrie

SecureTrie is the old name of StateTrie. Deprecated: use StateTrie.

func NewSecure

func NewSecure(stateRoot common.Hash, owner common.Hash, root common.Hash, db *Database) (*SecureTrie, error)

NewSecure creates a new StateTrie. Deprecated: use NewStateTrie.

type StackTrie

type StackTrie struct {
	// contains filtered or unexported fields
}

StackTrie is a trie implementation that expects keys to be inserted in order. Once it determines that a subtree will no longer be inserted into, it will hash it and free up the memory it uses.

func NewFromBinary

func NewFromBinary(data []byte, writeFn NodeWriteFunc) (*StackTrie, error)

NewFromBinary initialises a serialized stacktrie with the given db.

func NewStackTrie

func NewStackTrie(writeFn NodeWriteFunc) *StackTrie

NewStackTrie allocates and initializes an empty trie.

func NewStackTrieWithOwner added in v1.8.0

func NewStackTrieWithOwner(writeFn NodeWriteFunc, owner common.Hash) *StackTrie

NewStackTrieWithOwner allocates and initializes an empty trie, but with the additional owner field.

func (*StackTrie) Commit

func (st *StackTrie) Commit() (h common.Hash, err error)

Commit will firstly hash the entire trie if it's still not hashed and then commit all nodes to the associated database. Actually most of the trie nodes MAY have been committed already. The main purpose here is to commit the root node.

The associated database is expected, otherwise the whole commit functionality should be disabled.

func (*StackTrie) Hash

func (st *StackTrie) Hash() (h common.Hash)

Hash returns the hash of the current node.

func (*StackTrie) MarshalBinary

func (st *StackTrie) MarshalBinary() (data []byte, err error)

MarshalBinary implements encoding.BinaryMarshaler

func (*StackTrie) MustUpdate added in v1.8.0

func (st *StackTrie) MustUpdate(key, value []byte)

MustUpdate is a wrapper of Update and will omit any encountered error but just print out an error message.

func (*StackTrie) Reset

func (st *StackTrie) Reset()

func (*StackTrie) UnmarshalBinary

func (st *StackTrie) UnmarshalBinary(data []byte) error

UnmarshalBinary implements encoding.BinaryUnmarshaler

func (*StackTrie) Update

func (st *StackTrie) Update(key, value []byte) error

Update inserts a (key, value) pair into the stack trie.

type StateTrie added in v1.8.0

type StateTrie struct {
	// contains filtered or unexported fields
}

StateTrie wraps a trie with key hashing. In a stateTrie trie, all access operations hash the key using keccak256. This prevents calling code from creating long chains of nodes that increase the access time.

Contrary to a regular trie, a StateTrie can only be created with New and must have an attached database. The database also stores the preimage of each key if preimage recording is enabled.

StateTrie is not safe for concurrent use.

func NewStateTrie added in v1.8.0

func NewStateTrie(id *ID, db *Database) (*StateTrie, error)

NewStateTrie creates a trie with an existing root node from a backing database.

If root is the zero hash or the sha3 hash of an empty string, the trie is initially empty. Otherwise, New will panic if db is nil and returns MissingNodeError if the root node cannot be found.

func (*StateTrie) Commit added in v1.8.0

func (t *StateTrie) Commit(collectLeaf bool) (common.Hash, *trienode.NodeSet)

Commit collects all dirty nodes in the trie and replaces them with the corresponding node hash. All collected nodes (including dirty leaves if collectLeaf is true) will be encapsulated into a nodeset for return. The returned nodeset can be nil if the trie is clean (nothing to commit). All cached preimages will be also flushed if preimages recording is enabled. Once the trie is committed, it's not usable anymore. A new trie must be created with new root and updated trie database for following usage

func (*StateTrie) Copy added in v1.8.0

func (t *StateTrie) Copy() *StateTrie

Copy returns a copy of StateTrie.

func (*StateTrie) DeleteAccount added in v1.8.0

func (t *StateTrie) DeleteAccount(address common.Address) error

DeleteAccount abstracts an account deletion from the trie.

func (*StateTrie) DeleteStorage added in v1.8.0

func (t *StateTrie) DeleteStorage(_ common.Address, key []byte) error

DeleteStorage removes any existing storage slot from the trie. If the specified trie node is not in the trie, nothing will be changed. If a node is not found in the database, a MissingNodeError is returned.

func (*StateTrie) GetAccount added in v1.8.0

func (t *StateTrie) GetAccount(address common.Address) (*types.StateAccount, error)

GetAccount attempts to retrieve an account with provided account address. If the specified account is not in the trie, nil will be returned. If a trie node is not found in the database, a MissingNodeError is returned.

func (*StateTrie) GetAccountByHash added in v1.8.0

func (t *StateTrie) GetAccountByHash(addrHash common.Hash) (*types.StateAccount, error)

GetAccountByHash does the same thing as GetAccount, however it expects an account hash that is the hash of address. This constitutes an abstraction leak, since the client code needs to know the key format.

func (*StateTrie) GetKey added in v1.8.0

func (t *StateTrie) GetKey(shaKey []byte) []byte

GetKey returns the sha3 preimage of a hashed key that was previously used to store a value.

func (*StateTrie) GetNode added in v1.8.0

func (t *StateTrie) GetNode(path []byte) ([]byte, int, error)

GetNode attempts to retrieve a trie node by compact-encoded path. It is not possible to use keybyte-encoding as the path might contain odd nibbles. If the specified trie node is not in the trie, nil will be returned. If a trie node is not found in the database, a MissingNodeError is returned.

func (*StateTrie) GetStorage added in v1.8.0

func (t *StateTrie) GetStorage(_ common.Address, key []byte) ([]byte, error)

GetStorage attempts to retrieve a storage slot with provided account address and slot key. The value bytes must not be modified by the caller. If the specified storage slot is not in the trie, nil will be returned. If a trie node is not found in the database, a MissingNodeError is returned.

func (*StateTrie) Hash added in v1.8.0

func (t *StateTrie) Hash() common.Hash

Hash returns the root hash of StateTrie. It does not write to the database and can be used even if the trie doesn't have one.

func (*StateTrie) MustDelete added in v1.8.0

func (t *StateTrie) MustDelete(key []byte)

MustDelete removes any existing value for key from the trie. This function will omit any encountered error but just print out an error message.

func (*StateTrie) MustGet added in v1.8.0

func (t *StateTrie) MustGet(key []byte) []byte

MustGet returns the value for key stored in the trie. The value bytes must not be modified by the caller.

This function will omit any encountered error but just print out an error message.

func (*StateTrie) MustUpdate added in v1.8.0

func (t *StateTrie) MustUpdate(key, value []byte)

MustUpdate associates key with value in the trie. Subsequent calls to Get will return value. If value has length zero, any existing value is deleted from the trie and calls to Get will return nil.

The value bytes must not be modified by the caller while they are stored in the trie.

This function will omit any encountered error but just print out an error message.

func (*StateTrie) NodeIterator added in v1.8.0

func (t *StateTrie) NodeIterator(start []byte) NodeIterator

NodeIterator returns an iterator that returns nodes of the underlying trie. Iteration starts at the key after the given start key.

func (*StateTrie) Prove added in v1.8.0

func (t *StateTrie) Prove(key []byte, fromLevel uint, proofDb ethdb.KeyValueWriter) error

Prove constructs a merkle proof for key. The result contains all encoded nodes on the path to the value at key. The value itself is also included in the last node and can be retrieved by verifying the proof.

If the trie does not contain a value for key, the returned proof contains all nodes of the longest existing prefix of the key (at least the root node), ending with the node that proves the absence of the key.

func (*StateTrie) UpdateAccount added in v1.8.0

func (t *StateTrie) UpdateAccount(address common.Address, acc *types.StateAccount) error

UpdateAccount will abstract the write of an account to the secure trie.

func (*StateTrie) UpdateStorage added in v1.8.0

func (t *StateTrie) UpdateStorage(_ common.Address, key, value []byte) error

UpdateStorage associates key with value in the trie. Subsequent calls to Get will return value. If value has length zero, any existing value is deleted from the trie and calls to Get will return nil.

The value bytes must not be modified by the caller while they are stored in the trie.

If a node is not found in the database, a MissingNodeError is returned.

type Sync

type Sync struct {
	// contains filtered or unexported fields
}

Sync is the main state trie synchronisation scheduler, which provides yet unknown trie hashes to retrieve, accepts node data associated with said hashes and reconstructs the trie step by step until all is done.

func NewSync

func NewSync(root common.Hash, database ethdb.KeyValueReader, callback LeafCallback, scheme string) *Sync

NewSync creates a new trie data download scheduler.

func (*Sync) AddCodeEntry

func (s *Sync) AddCodeEntry(hash common.Hash, path []byte, parent common.Hash, parentPath []byte)

AddCodeEntry schedules the direct retrieval of a contract code that should not be interpreted as a trie node, but rather accepted and stored into the database as is.

func (*Sync) AddSubTrie

func (s *Sync) AddSubTrie(root common.Hash, path []byte, parent common.Hash, parentPath []byte, callback LeafCallback)

AddSubTrie registers a new trie to the sync code, rooted at the designated parent for completion tracking. The given path is a unique node path in hex format and contain all the parent path if it's layered trie node.

func (*Sync) Commit

func (s *Sync) Commit(dbw ethdb.Batch) error

Commit flushes the data stored in the internal membatch out to persistent storage, returning any occurred error.

func (*Sync) MemSize added in v1.8.0

func (s *Sync) MemSize() uint64

MemSize returns an estimated size (in bytes) of the data held in the membatch.

func (*Sync) Missing

func (s *Sync) Missing(max int) ([]string, []common.Hash, []common.Hash)

Missing retrieves the known missing nodes from the trie for retrieval. To aid both eth/6x style fast sync and snap/1x style state sync, the paths of trie nodes are returned too, as well as separate hash list for codes.

func (*Sync) Pending

func (s *Sync) Pending() int

Pending returns the number of state entries currently pending for download.

func (*Sync) ProcessCode added in v1.8.0

func (s *Sync) ProcessCode(result CodeSyncResult) error

ProcessCode injects the received data for requested item. Note it can happpen that the single response commits two pending requests(e.g. there are two requests one for code and one for node but the hash is same). In this case the second response for the same hash will be treated as "non-requested" item or "already-processed" item but there is no downside.

func (*Sync) ProcessNode added in v1.8.0

func (s *Sync) ProcessNode(result NodeSyncResult) error

ProcessNode injects the received data for requested item. Note it can happen that the single response commits two pending requests(e.g. there are two requests one for code and one for node but the hash is same). In this case the second response for the same hash will be treated as "non-requested" item or "already-processed" item but there is no downside.

type SyncPath

type SyncPath [][]byte

SyncPath is a path tuple identifying a particular trie node either in a single trie (account) or a layered trie (account -> storage).

Content wise the tuple either has 1 element if it addresses a node in a single trie or 2 elements if it addresses a node in a stacked trie.

To support aiming arbitrary trie nodes, the path needs to support odd nibble lengths. To avoid transferring expanded hex form over the network, the last part of the tuple (which needs to index into the middle of a trie) is compact encoded. In case of a 2-tuple, the first item is always 32 bytes so that is simple binary encoded.

Examples:

  • Path 0x9 -> {0x19}
  • Path 0x99 -> {0x0099}
  • Path 0x01234567890123456789012345678901012345678901234567890123456789019 -> {0x0123456789012345678901234567890101234567890123456789012345678901, 0x19}
  • Path 0x012345678901234567890123456789010123456789012345678901234567890199 -> {0x0123456789012345678901234567890101234567890123456789012345678901, 0x0099}

func NewSyncPath added in v1.8.0

func NewSyncPath(path []byte) SyncPath

NewSyncPath converts an expanded trie path from nibble form into a compact version that can be sent over the network.

type Trie

type Trie struct {
	// contains filtered or unexported fields
}

Trie is a Merkle Patricia Trie. Use New to create a trie that sits on top of a database. Whenever trie performs a commit operation, the generated nodes will be gathered and returned in a set. Once the trie is committed, it's not usable anymore. Callers have to re-create the trie with new root based on the updated trie database.

Trie is not safe for concurrent use.

func New

func New(id *ID, db NodeReader) (*Trie, error)

New creates the trie instance with provided trie id and the read-only database. The state specified by trie id must be available, otherwise an error will be returned. The trie root specified by trie id can be zero hash or the sha3 hash of an empty string, then trie is initially empty, otherwise, the root node must be present in database or returns a MissingNodeError if not.

func NewEmpty added in v1.8.0

func NewEmpty(db *Database) *Trie

NewEmpty is a shortcut to create empty tree. It's mostly used in tests.

func NewEmptyParallel added in v1.9.0

func NewEmptyParallel(paraDB *Database) *Trie

func NewParallel added in v1.9.0

func NewParallel(id *ID, db NodeReader) (*Trie, error)

func (*Trie) Commit

func (t *Trie) Commit(collectLeaf bool) (common.Hash, *trienode.NodeSet)

Commit collects all dirty nodes in the trie and replaces them with the corresponding node hash. All collected nodes (including dirty leaves if collectLeaf is true) will be encapsulated into a nodeset for return. The returned nodeset can be nil if the trie is clean (nothing to commit). Once the trie is committed, it's not usable anymore. A new trie must be created with new root and updated trie database for following usage

func (*Trie) Copy added in v1.8.0

func (t *Trie) Copy() *Trie

Copy returns a copy of Trie.

func (*Trie) Delete

func (t *Trie) Delete(key []byte) error

Delete removes any existing value for key from the trie.

If the requested node is not present in trie, no error will be returned. If the trie is corrupted, a MissingNodeError is returned.

func (*Trie) Get

func (t *Trie) Get(key []byte) ([]byte, error)

Get returns the value for key stored in the trie. The value bytes must not be modified by the caller.

If the requested node is not present in trie, no error will be returned. If the trie is corrupted, a MissingNodeError is returned.

func (*Trie) GetNode added in v1.8.0

func (t *Trie) GetNode(path []byte) ([]byte, int, error)

GetNode retrieves a trie node by compact-encoded path. It is not possible to use keybyte-encoding as the path might contain odd nibbles.

If the requested node is not present in trie, no error will be returned. If the trie is corrupted, a MissingNodeError is returned.

func (*Trie) Hash

func (t *Trie) Hash() common.Hash

Hash returns the root hash of the trie. It does not write to the database and can be used even if the trie doesn't have one.

func (*Trie) MustDelete added in v1.8.0

func (t *Trie) MustDelete(key []byte)

MustDelete is a wrapper of Delete and will omit any encountered error but just print out an error message.

func (*Trie) MustGet added in v1.8.0

func (t *Trie) MustGet(key []byte) []byte

MustGet is a wrapper of Get and will omit any encountered error but just print out an error message.

func (*Trie) MustGetNode added in v1.8.0

func (t *Trie) MustGetNode(path []byte) ([]byte, int)

MustGetNode is a wrapper of GetNode and will omit any encountered error but just print out an error message.

func (*Trie) MustUpdate added in v1.8.0

func (t *Trie) MustUpdate(key, value []byte)

MustUpdate is a wrapper of Update and will omit any encountered error but just print out an error message.

func (*Trie) NodeIterator

func (t *Trie) NodeIterator(start []byte) NodeIterator

NodeIterator returns an iterator that returns nodes of the trie. Iteration starts at the key after the given start key.

func (*Trie) ParallelGet added in v1.9.0

func (trie *Trie) ParallelGet(keys [][]byte) ([][]byte, error)

func (*Trie) ParallelUpdate added in v1.9.0

func (trie *Trie) ParallelUpdate(keys [][]byte, values [][]byte)

func (*Trie) Prove

func (t *Trie) Prove(key []byte, fromLevel uint, proofDb ethdb.KeyValueWriter) error

Prove constructs a merkle proof for key. The result contains all encoded nodes on the path to the value at key. The value itself is also included in the last node and can be retrieved by verifying the proof.

If the trie does not contain a value for key, the returned proof contains all nodes of the longest existing prefix of the key (at least the root node), ending with the node that proves the absence of the key.

func (*Trie) Reset

func (t *Trie) Reset()

Reset drops the referenced root node and cleans all internal state.

func (*Trie) ThreadSafeGet added in v1.9.0

func (t *Trie) ThreadSafeGet(key []byte, accesses *AccessListCache) ([]byte, error)

func (*Trie) Update

func (t *Trie) Update(key, value []byte) error

Update associates key with value in the trie. Subsequent calls to Get will return value. If value has length zero, any existing value is deleted from the trie and calls to Get will return nil.

The value bytes must not be modified by the caller while they are stored in the trie.

If the requested node is not present in trie, no error will be returned. If the trie is corrupted, a MissingNodeError is returned.

Directories

Path Synopsis
triedb

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL