Documentation ¶
Overview ¶
Package trie implements Merkle Patricia Tries.
Index ¶
- Variables
- func VerifyProof(rootHash common.Hash, key []byte, proofDb ethdb.KeyValueReader) (value []byte, err error)
- func VerifyRangeProof(rootHash common.Hash, firstKey []byte, lastKey []byte, keys [][]byte, ...) (bool, error)
- type CodeSyncResult
- type Config
- type Database
- func (db *Database) Cap(limit common.StorageSize) error
- func (db *Database) Commit(node common.Hash, report bool, callback func(common.Hash)) error
- func (db *Database) CommitPreimages() error
- func (db *Database) Dereference(root common.Hash)
- func (db *Database) DiskDB() ethdb.KeyValueStore
- func (db *Database) Node(hash common.Hash) ([]byte, error)
- func (db *Database) Nodes() []common.Hash
- func (db *Database) Reference(child common.Hash, parent common.Hash)
- func (db *Database) SaveCache(dir string) error
- func (db *Database) SaveCachePeriodically(dir string, interval time.Duration, stopCh <-chan struct{})
- func (db *Database) Size() (common.StorageSize, common.StorageSize)
- func (db *Database) Update(nodes *MergedNodeSet) error
- type Iterator
- type LeafCallback
- type MergedNodeSet
- type MissingNodeError
- type NodeIterator
- type NodeSet
- type NodeSyncResult
- type SecureTrie
- type StackTrie
- func (st *StackTrie) Commit() (h common.Hash, err error)
- func (st *StackTrie) Hash() (h common.Hash)
- func (st *StackTrie) MarshalBinary() (data []byte, err error)
- func (st *StackTrie) Reset()
- func (st *StackTrie) TryUpdate(key, value []byte) error
- func (st *StackTrie) UnmarshalBinary(data []byte) error
- func (st *StackTrie) Update(key, value []byte)
- type StateTrie
- func (t *StateTrie) Commit(collectLeaf bool) (common.Hash, *NodeSet, error)
- func (t *StateTrie) Copy() *StateTrie
- func (t *StateTrie) Delete(key []byte)
- func (t *StateTrie) Get(key []byte) []byte
- func (t *StateTrie) GetKey(shaKey []byte) []byte
- func (t *StateTrie) Hash() common.Hash
- func (t *StateTrie) NodeIterator(start []byte) NodeIterator
- func (t *StateTrie) Prove(key []byte, fromLevel uint, proofDb ethdb.KeyValueWriter) error
- func (t *StateTrie) TryDelete(key []byte) error
- func (t *StateTrie) TryDeleteAccount(key []byte) error
- func (t *StateTrie) TryGet(key []byte) ([]byte, error)
- func (t *StateTrie) TryGetAccount(key []byte) (*types.StateAccount, error)
- func (t *StateTrie) TryGetAccountWithPreHashedKey(key []byte) (*types.StateAccount, error)
- func (t *StateTrie) TryGetNode(path []byte) ([]byte, int, error)
- func (t *StateTrie) TryUpdate(key, value []byte) error
- func (t *StateTrie) TryUpdateAccount(key []byte, acc *types.StateAccount) error
- func (t *StateTrie) Update(key, value []byte)
- type Sync
- func (s *Sync) AddCodeEntry(hash common.Hash, path []byte, parent common.Hash, parentPath []byte)
- func (s *Sync) AddSubTrie(root common.Hash, path []byte, parent common.Hash, parentPath []byte, ...)
- func (s *Sync) Commit(dbw ethdb.Batch) error
- func (s *Sync) Missing(max int) ([]string, []common.Hash, []common.Hash)
- func (s *Sync) Pending() int
- func (s *Sync) ProcessCode(result CodeSyncResult) error
- func (s *Sync) ProcessNode(result NodeSyncResult) error
- type SyncPath
- type Trie
- func (t *Trie) Commit(collectLeaf bool) (common.Hash, *NodeSet, error)
- func (t *Trie) Copy() *Trie
- func (t *Trie) Delete(key []byte)
- func (t *Trie) Get(key []byte) []byte
- func (t *Trie) Hash() common.Hash
- func (t *Trie) NodeIterator(start []byte) NodeIterator
- func (t *Trie) Prove(key []byte, fromLevel uint, proofDb ethdb.KeyValueWriter) error
- func (t *Trie) Reset()
- func (t *Trie) TryDelete(key []byte) error
- func (t *Trie) TryGet(key []byte) ([]byte, error)
- func (t *Trie) TryGetNode(path []byte) ([]byte, int, error)
- func (t *Trie) TryUpdate(key, value []byte) error
- func (t *Trie) Update(key, value []byte)
Constants ¶
This section is empty.
Variables ¶
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.
var ErrCommitDisabled = errors.New("no database for committing")
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 VerifyProof ¶ added in v1.3.1
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 ¶ added in v1.9.14
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 CodeSyncResult ¶ added in v1.10.21
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 ¶ added in v1.9.25
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 ¶ added in v1.3.1
type Database struct {
// contains filtered or unexported fields
}
Database is an intermediate write layer between the trie data structures and the disk database. The aim is to accumulate trie writes in-memory and only periodically flush a couple tries to disk, garbage collecting the remainder.
Note, the trie Database is **not** thread safe in its mutations, but it **is** thread safe in providing individual, independent node access. The rationale behind this split design is to provide read access to RPC handlers and sync servers even while the trie is executing expensive garbage collection.
func NewDatabase ¶ added in v1.8.0
func NewDatabase(diskdb ethdb.KeyValueStore) *Database
NewDatabase creates a new trie database to store ephemeral trie content before its written out to disk or garbage collected. No read cache is created, so all data retrievals will hit the underlying disk database.
func NewDatabaseWithConfig ¶ added in v1.9.25
func NewDatabaseWithConfig(diskdb ethdb.KeyValueStore, config *Config) *Database
NewDatabaseWithConfig creates a new trie database to store ephemeral trie content before its written out to disk or garbage collected. It also acts as a read cache for nodes loaded from disk.
func (*Database) Cap ¶ added in v1.8.11
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.
Note, this method is a non-synchronized mutator. It is unsafe to call this concurrently with other mutators.
func (*Database) Commit ¶ added in v1.8.0
Commit iterates over all the children of a particular node, writes them out to disk, forcefully tearing down all references in both directions. As a side effect, all pre-images accumulated up to this point are also written.
Note, this method is a non-synchronized mutator. It is unsafe to call this concurrently with other mutators.
func (*Database) CommitPreimages ¶ added in v1.10.22
CommitPreimages flushes the dangling preimages to disk. It is meant to be called when closing the blockchain object, so that preimages are persisted to the database.
func (*Database) Dereference ¶ added in v1.8.0
Dereference removes an existing reference from a root node.
func (*Database) DiskDB ¶ added in v1.8.0
func (db *Database) DiskDB() ethdb.KeyValueStore
DiskDB retrieves the persistent storage backing the trie database.
func (*Database) Node ¶ added in v1.8.0
Node retrieves an encoded cached trie node from memory. If it cannot be found cached, the method queries the persistent database for the content.
func (*Database) Nodes ¶ added in v1.8.0
Nodes retrieves the hashes of all the nodes cached within the memory database. This method is extremely expensive and should only be used to validate internal states in test code.
func (*Database) Reference ¶ added in v1.8.0
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.
func (*Database) SaveCache ¶ added in v1.9.19
SaveCache atomically saves fast cache data to the given dir using all available CPU cores.
func (*Database) SaveCachePeriodically ¶ added in v1.9.19
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) Size ¶ added in v1.8.0
func (db *Database) Size() (common.StorageSize, common.StorageSize)
Size returns the current storage size of the memory cache in front of the persistent database layer.
func (*Database) Update ¶ added in v1.10.22
func (db *Database) Update(nodes *MergedNodeSet) error
Update inserts the dirty nodes in provided nodeset into database and link the account trie with multiple storage tries if necessary.
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.
type LeafCallback ¶ added in v1.8.0
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 MergedNodeSet ¶ added in v1.10.22
type MergedNodeSet struct {
// contains filtered or unexported fields
}
MergedNodeSet represents a merged dirty node set for a group of tries.
func NewMergedNodeSet ¶ added in v1.10.22
func NewMergedNodeSet() *MergedNodeSet
NewMergedNodeSet initializes an empty merged set.
func NewWithNodeSet ¶ added in v1.10.22
func NewWithNodeSet(set *NodeSet) *MergedNodeSet
NewWithNodeSet constructs a merged nodeset with the provided single set.
func (*MergedNodeSet) Merge ¶ added in v1.10.22
func (set *MergedNodeSet) Merge(other *NodeSet) error
Merge merges the provided dirty nodes of a trie into the set. The assumption is held that no duplicated set belonging to the same trie will be merged twice.
type MissingNodeError ¶ added in v1.4.0
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 (TryGet, TryUpdate, TryDelete) 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 ¶ added in v1.4.0
func (err *MissingNodeError) Error() string
func (*MissingNodeError) Unwrap ¶ added in v1.10.19
func (err *MissingNodeError) Unwrap() error
Unwrap returns the concrete error for missing trie node which allows us for further analysis outside.
type NodeIterator ¶ added in v1.4.0
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 an intermediate database 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(ethdb.KeyValueReader) }
NodeIterator is an iterator to traverse the trie pre-order.
func NewDifferenceIterator ¶ added in v1.6.0
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 ¶ added in v1.6.0
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 NodeSet ¶ added in v1.10.22
type NodeSet struct {
// contains filtered or unexported fields
}
NodeSet contains all dirty nodes collected during the commit operation. Each node is keyed by path. It's not thread-safe to use.
func NewNodeSet ¶ added in v1.10.22
NewNodeSet initializes an empty node set to be used for tracking dirty nodes from a specific account or storage trie. The owner is zero for the account trie and the owning account address hash for storage tries.
type NodeSyncResult ¶ added in v1.10.21
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 SecureTrie ¶ added in v0.9.17
type SecureTrie = StateTrie
SecureTrie is the old name of StateTrie. Deprecated: use StateTrie.
type StackTrie ¶ added in v1.9.23
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 ¶ added in v1.10.3
func NewFromBinary(data []byte, db ethdb.KeyValueWriter) (*StackTrie, error)
NewFromBinary initialises a serialized stacktrie with the given db.
func NewStackTrie ¶ added in v1.9.23
func NewStackTrie(db ethdb.KeyValueWriter) *StackTrie
NewStackTrie allocates and initializes an empty trie.
func NewStackTrieWithOwner ¶ added in v1.10.19
func NewStackTrieWithOwner(db ethdb.KeyValueWriter, owner common.Hash) *StackTrie
NewStackTrieWithOwner allocates and initializes an empty trie, but with the additional owner field.
func (*StackTrie) Commit ¶ added in v1.9.23
Commit will firstly hash the entrie 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) MarshalBinary ¶ added in v1.10.3
MarshalBinary implements encoding.BinaryMarshaler
func (*StackTrie) TryUpdate ¶ added in v1.9.23
TryUpdate inserts a (key, value) pair into the stack trie
func (*StackTrie) UnmarshalBinary ¶ added in v1.10.3
UnmarshalBinary implements encoding.BinaryUnmarshaler
type StateTrie ¶ added in v1.10.22
type StateTrie struct {
// contains filtered or unexported fields
}
StateTrie wraps a trie with key hashing. In a secure 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.
StateTrie is not safe for concurrent use.
func NewStateTrie ¶ added in v1.10.22
NewStateTrie creates a trie with an existing root node from a backing database and optional intermediate in-memory node pool.
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.
Accessing the trie loads nodes from the database or node pool on demand. Loaded nodes are kept around until their 'cache generation' expires. A new cache generation is created by each call to Commit. cachelimit sets the number of past cache generations to keep.
func (*StateTrie) Commit ¶ added in v1.10.22
Commit collects all dirty nodes in the trie and replace 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) Delete ¶ added in v1.10.22
Delete removes any existing value for key from the trie.
func (*StateTrie) Get ¶ added in v1.10.22
Get returns the value for key stored in the trie. The value bytes must not be modified by the caller.
func (*StateTrie) GetKey ¶ added in v1.10.22
GetKey returns the sha3 preimage of a hashed key that was previously used to store a value.
func (*StateTrie) Hash ¶ added in v1.10.22
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) NodeIterator ¶ added in v1.10.22
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.10.22
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) TryDelete ¶ added in v1.10.22
TryDelete removes any existing value for key from the trie. If a node was not found in the database, a MissingNodeError is returned.
func (*StateTrie) TryDeleteAccount ¶ added in v1.10.22
TryDeleteACcount abstracts an account deletion from the trie.
func (*StateTrie) TryGet ¶ added in v1.10.22
TryGet returns the value for key stored in the trie. The value bytes must not be modified by the caller. If a node was not found in the database, a MissingNodeError is returned.
func (*StateTrie) TryGetAccount ¶ added in v1.10.22
func (t *StateTrie) TryGetAccount(key []byte) (*types.StateAccount, error)
func (*StateTrie) TryGetAccountWithPreHashedKey ¶ added in v1.10.22
func (t *StateTrie) TryGetAccountWithPreHashedKey(key []byte) (*types.StateAccount, error)
TryGetAccountWithPreHashedKey does the same thing as TryGetAccount, however it expects a key that is already hashed. This constitutes an abstraction leak, since the client code needs to know the key format.
func (*StateTrie) TryGetNode ¶ added in v1.10.22
TryGetNode 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.
func (*StateTrie) TryUpdate ¶ added in v1.10.22
TryUpdate 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 was not found in the database, a MissingNodeError is returned.
func (*StateTrie) TryUpdateAccount ¶ added in v1.10.22
func (t *StateTrie) TryUpdateAccount(key []byte, acc *types.StateAccount) error
TryUpdateAccount account will abstract the write of an account to the secure trie.
func (*StateTrie) Update ¶ added in v1.10.22
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.
type Sync ¶ added in v1.8.10
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 ¶ added in v1.8.10
func NewSync(root common.Hash, database ethdb.KeyValueReader, callback LeafCallback) *Sync
NewSync creates a new trie data download scheduler.
func (*Sync) AddCodeEntry ¶ added in v1.9.20
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 ¶ added in v1.8.10
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 ¶ added in v1.8.10
Commit flushes the data stored in the internal membatch out to persistent storage, returning any occurred error.
func (*Sync) Missing ¶ added in v1.8.10
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 ¶ added in v1.8.10
Pending returns the number of state entries currently pending for download.
func (*Sync) ProcessCode ¶ added in v1.10.21
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.10.21
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 ¶ added in v1.9.21
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.10.18
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 ¶
New creates a trie with an existing root node from db and an assigned owner for storage proximity.
If root is the zero hash or the sha3 hash of an empty string, the trie is initially empty and does not require a database. Otherwise, New will panic if db is nil and returns a MissingNodeError if root does not exist in the database. Accessing the trie loads nodes from db on demand.
func NewEmpty ¶ added in v1.10.19
NewEmpty is a shortcut to create empty tree. It's mostly used in tests.
func (*Trie) Commit ¶ added in v0.8.4
Commit collects all dirty nodes in the trie and replace 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) Get ¶
Get returns the value for key stored in the trie. The value bytes must not be modified by the caller.
func (*Trie) Hash ¶ added in v0.8.4
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) NodeIterator ¶ added in v1.6.1
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) Prove ¶ added in v1.3.1
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 ¶ added in v0.8.4
func (t *Trie) Reset()
Reset drops the referenced root node and cleans all internal state.
func (*Trie) TryDelete ¶ added in v1.4.0
TryDelete removes any existing value for key from the trie. If a node was not found in the database, a MissingNodeError is returned.
func (*Trie) TryGet ¶ added in v1.4.0
TryGet returns the value for key stored in the trie. The value bytes must not be modified by the caller. If a node was not found in the database, a MissingNodeError is returned.
func (*Trie) TryGetNode ¶ added in v1.9.21
TryGetNode 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.
func (*Trie) TryUpdate ¶ added in v1.4.0
TryUpdate 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 was not found in the database, a MissingNodeError is returned.
func (*Trie) Update ¶
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.