Documentation ¶
Overview ¶
Package trie implements Merkle Patricia Tries.
Index ¶
- Variables
- func AssertTrieConsistency(t testing.TB, root common.Hash, a, b *Database, ...)
- func CorruptTrie(t *testing.T, trieDB *Database, root common.Hash, n int)
- func FillAccounts(t *testing.T, trieDB *Database, root common.Hash, numAccounts int, ...) (common.Hash, map[*keystore.Key]*types.StateAccount)
- func FillTrie(t *testing.T, numKeys int, keySize int, testTrie *Trie) ([][]byte, [][]byte)
- func GenerateTrie(t *testing.T, trieDB *Database, numKeys int, keySize int) (common.Hash, [][]byte, [][]byte)
- 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 Config
- type Database
- func (db *Database) Cap(limit common.StorageSize) error
- func (db *Database) Commit(node common.Hash, report bool) error
- func (db *Database) CommitPreimages() error
- func (db *Database) Dereference(root common.Hash)
- func (db *Database) EncodedNode(h common.Hash) node
- func (db *Database) GetReader(root common.Hash) Reader
- func (db *Database) Nodes() []common.Hash
- func (db *Database) RawNode(h common.Hash) ([]byte, error)
- func (db *Database) Reference(child common.Hash, parent common.Hash)
- func (db *Database) SaveCachePeriodically(dir string, interval time.Duration, stopCh <-chan struct{})
- func (db *Database) Scheme() string
- func (db *Database) Size() (common.StorageSize, common.StorageSize)
- func (db *Database) Update(nodes *MergedNodeSet) error
- func (db *Database) UpdateAndReferenceRoot(nodes *MergedNodeSet, root common.Hash) error
- type ID
- type Iterator
- type MergedNodeSet
- type MissingNodeError
- type NodeIterator
- type NodeReader
- type NodeResolver
- type NodeSet
- type NodeWriteFunc
- type Reader
- 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)
- 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(address common.Address) error
- func (t *StateTrie) TryGet(key []byte) ([]byte, error)
- func (t *StateTrie) TryGetAccount(address common.Address) (*types.StateAccount, error)
- func (t *StateTrie) TryGetAccountByHash(addrHash common.Hash) (*types.StateAccount, error)
- func (t *StateTrie) TryGetNode(path []byte) ([]byte, int, error)
- func (t *StateTrie) TryUpdate(key, value []byte) error
- func (t *StateTrie) TryUpdateAccount(address common.Address, acc *types.StateAccount) error
- func (t *StateTrie) Update(key, value []byte)
- type Trie
- func (t *Trie) Commit(collectLeaf bool) (common.Hash, *NodeSet)
- 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 ErrCommitDisabled = errors.New("no database for committing")
Functions ¶
func AssertTrieConsistency ¶ added in v0.8.10
func AssertTrieConsistency(t testing.TB, root common.Hash, a, b *Database, onLeaf func(key, val []byte) error)
AssertTrieConsistency ensures given trieDB [a] and [b] both have the same non-empty trie at [root]. (all key/value pairs must be equal)
func CorruptTrie ¶ added in v0.8.10
CorruptTrie deletes every [n]th trie node from the trie given by [root] from the trieDB. Assumes that the trie given by root can be iterated without issue.
func FillAccounts ¶ added in v0.8.11
func FillAccounts( t *testing.T, trieDB *Database, root common.Hash, numAccounts int, onAccount func(*testing.T, int, types.StateAccount) types.StateAccount, ) (common.Hash, map[*keystore.Key]*types.StateAccount)
FillAccounts adds [numAccounts] randomly generated accounts to the secure trie at [root] and commits it to [trieDB]. [onAccount] is called if non-nil (so the caller can modify the account before it is stored in the secure trie). returns the new trie root and a map of funded keys to StateAccount structs.
func FillTrie ¶ added in v0.8.10
FillTrie fills a given trie with [numKeys] number of keys, each of size [keySize] returns inserted keys and values FillTrie reads from rand and the caller should call rand.Seed(n) for deterministic results
func GenerateTrie ¶ added in v0.8.10
func GenerateTrie(t *testing.T, trieDB *Database, numKeys int, keySize int) (common.Hash, [][]byte, [][]byte)
GenerateTrie creates a trie with [numKeys] key-value pairs inside of [trieDB]. Returns the root of the generated trie, the slice of keys inserted into the trie in lexicographical order, and the slice of corresponding values. GenerateTrie reads from rand and the caller should call rand.Seed(n) for deterministic results
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 Config ¶
type Config struct { Cache int // Memory allowance (MB) to use for caching trie nodes in memory Preimages bool // Flag whether the preimage of trie key is recorded Journal string // File location to load trie clean cache from StatsPrefix string // Prefix for cache stats (disabled if empty) }
Config defines all necessary options for database.
type Database ¶
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.
The trie Database is thread-safe in its mutations and is thread-safe in providing individual, independent node access.
func NewDatabase ¶
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 ¶
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 ¶
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.
func (*Database) Commit ¶
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.
func (*Database) CommitPreimages ¶ added in v0.9.0
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 ¶
Dereference removes an existing reference from a root node.
func (*Database) EncodedNode ¶ added in v0.8.11
EncodedNode returns a formatted [node] when given a node hash. If no node exists, nil is returned. This function will return the metaroot.
func (*Database) GetReader ¶ added in v0.12.3
GetReader retrieves a node reader belonging to the given state root.
func (*Database) Nodes ¶
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) RawNode ¶ added in v0.8.11
RawNode retrieves an encoded cached trie node from memory. If it cannot be found cached, the method queries the persistent database for the content. This function will not return the metaroot.
func (*Database) Reference ¶
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) 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) Size ¶
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 v0.9.0
func (db *Database) Update(nodes *MergedNodeSet) error
Update inserts the dirty nodes in provided nodeset into database and links the account trie with multiple storage tries if necessary.
func (*Database) UpdateAndReferenceRoot ¶ added in v0.9.0
func (db *Database) UpdateAndReferenceRoot(nodes *MergedNodeSet, root common.Hash) error
UpdateAndReferenceRoot inserts the dirty nodes in provided nodeset into database and links the account trie with multiple storage tries if necessary, then adds a reference [from] root to the metaroot while holding the db's lock.
type ID ¶ added in v0.12.3
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 v0.12.3
StateTrieID constructs an identifier for state trie with the provided state root.
func StorageTrieID ¶ added in v0.12.3
StorageTrieID constructs an identifier for storage trie which belongs to a certain state and contract specified by the stateRoot and owner.
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 MergedNodeSet ¶ added in v0.9.0
type MergedNodeSet struct {
// contains filtered or unexported fields
}
MergedNodeSet represents a merged dirty node set for a group of tries.
func NewMergedNodeSet ¶ added in v0.9.0
func NewMergedNodeSet() *MergedNodeSet
NewMergedNodeSet initializes an empty merged set.
func NewWithNodeSet ¶ added in v0.9.0
func NewWithNodeSet(set *NodeSet) *MergedNodeSet
NewWithNodeSet constructs a merged nodeset with the provided single set.
func (*MergedNodeSet) Merge ¶ added in v0.9.0
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 ¶
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 ¶
func (err *MissingNodeError) Error() string
func (*MissingNodeError) Unwrap ¶ added in v0.8.14
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 v0.12.3
type NodeReader interface { // GetReader returns a reader for accessing all trie nodes with provided // state root. Nil is returned in case the state is not available. GetReader(root common.Hash) Reader }
NodeReader wraps all the necessary functions for accessing trie node.
type NodeResolver ¶ added in v0.12.3
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 NodeSet ¶ added in v0.9.0
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 v0.9.0
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.
func (*NodeSet) Hashes ¶ added in v0.12.3
Hashes returns the hashes of all updated nodes. TODO(rjl493456442) how can we get rid of it?
type NodeWriteFunc ¶ added in v0.12.3
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 v0.12.3
type Reader interface { // Node retrieves the trie node with the provided trie identifier, hexary // 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) (node, error) // NodeBlob retrieves the RLP-encoded trie node blob with the provided trie // identifier, hexary node path and the corresponding node hash. // No error will be returned if the node is not found. NodeBlob(owner common.Hash, path []byte, hash common.Hash) ([]byte, error) }
Reader wraps the Node and NodeBlob method of a backing trie store.
type SecureTrie ¶
type SecureTrie = StateTrie
SecureTrie is the old name of StateTrie. Deprecated: use StateTrie.
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 v0.8.14
func NewStackTrieWithOwner(writeFn NodeWriteFunc, owner common.Hash) *StackTrie
NewStackTrieWithOwner allocates and initializes an empty trie, but with the additional owner field.
func (*StackTrie) Commit ¶
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) MarshalBinary ¶
MarshalBinary implements encoding.BinaryMarshaler
func (*StackTrie) UnmarshalBinary ¶
UnmarshalBinary implements encoding.BinaryUnmarshaler
type StateTrie ¶ added in v0.9.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 v0.9.0
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 v0.9.0
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) Get ¶ added in v0.9.0
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 v0.9.0
GetKey returns the sha3 preimage of a hashed key that was previously used to store a value.
func (*StateTrie) Hash ¶ added in v0.9.0
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 v0.9.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 v0.9.0
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 v0.9.0
TryDelete removes any existing value for key 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) TryDeleteAccount ¶ added in v0.9.0
TryDeleteAccount abstracts an account deletion from the trie.
func (*StateTrie) TryGet ¶ added in v0.9.0
TryGet returns the value for key stored in the trie. The value bytes must not be modified by the caller. If the specified 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) TryGetAccount ¶ added in v0.9.0
TryGetAccount 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) TryGetAccountByHash ¶ added in v0.12.3
TryGetAccountByHash does the same thing as TryGetAccount, 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) TryGetNode ¶ added in v0.9.0
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. 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) TryUpdate ¶ added in v0.9.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 is not found in the database, a MissingNodeError is returned.
func (*StateTrie) TryUpdateAccount ¶ added in v0.9.0
TryUpdateAccount account will abstract the write of an account to the secure trie.
func (*StateTrie) Update ¶ added in v0.9.0
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 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 v0.8.14
NewEmpty is a shortcut to create empty tree. It's mostly used in tests.
func (*Trie) Commit ¶
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) Get ¶
Get returns the value for key stored in the trie. The value bytes must not be modified by the caller.
func (*Trie) 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) 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) Prove ¶
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) TryDelete ¶
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 ¶
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 ¶
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 ¶
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.