Documentation ¶
Overview ¶
Package trie implements Merkle Patricia Tries.
Index ¶
- Constants
- Variables
- func VerifyProof(rootHash common.Hash, key []byte, proofDb ethdb.KeyValueReader) (value []byte, err error)
- func VerifyRangeProof(rootHash common.Hash, firstKey []byte, keys [][]byte, values [][]byte, ...) (bool, error)
- type ChunkedCode
- type Config
- type Database
- func (db *Database) Cap(limit common.StorageSize) error
- func (db *Database) Close() error
- func (db *Database) Commit(root common.Hash, report bool) error
- func (db *Database) Dereference(root common.Hash) error
- func (db *Database) Disable() error
- func (db *Database) Enable(root common.Hash) error
- func (db *Database) Initialized(genesisRoot common.Hash) bool
- func (db *Database) IsVerkle() bool
- func (db *Database) Journal(root common.Hash) error
- func (db *Database) Preimage(hash common.Hash) []byte
- func (db *Database) Reader(blockRoot common.Hash) (Reader, error)
- func (db *Database) Recover(target common.Hash) error
- func (db *Database) Recoverable(root common.Hash) (bool, error)
- func (db *Database) Reference(root common.Hash, parent common.Hash) error
- func (db *Database) Scheme() string
- func (db *Database) SetBufferSize(size int) error
- func (db *Database) Size() (common.StorageSize, common.StorageSize, common.StorageSize)
- func (db *Database) Update(root common.Hash, parent common.Hash, block uint64, ...) error
- func (db *Database) UpdateAndReferenceRoot(root common.Hash, parent common.Hash, block uint64, ...) error
- func (db *Database) WritePreimages()
- type ID
- type Iterator
- type MissingNodeError
- type NodeIterator
- type NodeResolver
- type Reader
- type SecureTrie
- type StackTrie
- type StackTrieOptions
- func (o *StackTrieOptions) WithCleaner(cleaner func(path []byte)) *StackTrieOptions
- func (o *StackTrieOptions) WithSkipBoundary(skipLeft, skipRight bool, gauge metrics.Gauge) *StackTrieOptions
- func (o *StackTrieOptions) WithWriter(writer func(path []byte, hash common.Hash, blob []byte)) *StackTrieOptions
- type StateTrie
- func (t *StateTrie) Commit(collectLeaf bool) (common.Hash, *trienode.NodeSet, error)
- func (t *StateTrie) Copy() *StateTrie
- func (t *StateTrie) DeleteAccount(address common.Address) error
- func (t *StateTrie) DeleteStorage(_ common.Address, key []byte) error
- func (t *StateTrie) GetAccount(address common.Address) (*types.StateAccount, error)
- func (t *StateTrie) GetAccountByHash(addrHash common.Hash) (*types.StateAccount, error)
- func (t *StateTrie) GetKey(shaKey []byte) []byte
- func (t *StateTrie) GetNode(path []byte) ([]byte, int, error)
- func (t *StateTrie) GetStorage(_ common.Address, key []byte) ([]byte, error)
- func (t *StateTrie) Hash() common.Hash
- func (t *StateTrie) MustDelete(key []byte)
- func (t *StateTrie) MustGet(key []byte) []byte
- func (t *StateTrie) MustNodeIterator(start []byte) NodeIterator
- func (t *StateTrie) MustUpdate(key, value []byte)
- func (t *StateTrie) NodeIterator(start []byte) (NodeIterator, error)
- func (t *StateTrie) Prove(key []byte, proofDb ethdb.KeyValueWriter) error
- func (t *StateTrie) UpdateAccount(address common.Address, acc *types.StateAccount) error
- func (t *StateTrie) UpdateContractCode(_ common.Address, _ common.Hash, _ []byte) error
- func (t *StateTrie) UpdateStorage(_ common.Address, key, value []byte) error
- type Trie
- func (t *Trie) Commit(collectLeaf bool) (common.Hash, *trienode.NodeSet, error)
- func (t *Trie) Copy() *Trie
- func (t *Trie) Delete(key []byte) error
- func (t *Trie) Get(key []byte) ([]byte, error)
- func (t *Trie) GetNode(path []byte) ([]byte, int, error)
- func (t *Trie) Hash() common.Hash
- func (t *Trie) MustDelete(key []byte)
- func (t *Trie) MustGet(key []byte) []byte
- func (t *Trie) MustGetNode(path []byte) ([]byte, int)
- func (t *Trie) MustNodeIterator(start []byte) NodeIterator
- func (t *Trie) MustUpdate(key, value []byte)
- func (t *Trie) NodeIterator(start []byte) (NodeIterator, error)
- func (t *Trie) Prove(key []byte, proofDb ethdb.KeyValueWriter) error
- func (t *Trie) Reset()
- func (t *Trie) Update(key, value []byte) error
- type VerkleTrie
- func (t *VerkleTrie) Commit(_ bool) (common.Hash, *trienode.NodeSet, error)
- func (t *VerkleTrie) Copy() *VerkleTrie
- func (t *VerkleTrie) DeleteAccount(addr common.Address) error
- func (t *VerkleTrie) DeleteStorage(addr common.Address, key []byte) error
- func (t *VerkleTrie) GetAccount(addr common.Address) (*types.StateAccount, error)
- func (t *VerkleTrie) GetKey(key []byte) []byte
- func (t *VerkleTrie) GetStorage(addr common.Address, key []byte) ([]byte, error)
- func (t *VerkleTrie) Hash() common.Hash
- func (t *VerkleTrie) IsVerkle() bool
- func (t *VerkleTrie) NodeIterator(startKey []byte) (NodeIterator, error)
- func (t *VerkleTrie) Prove(key []byte, proofDb ethdb.KeyValueWriter) error
- func (t *VerkleTrie) ToDot() string
- func (t *VerkleTrie) UpdateAccount(addr common.Address, acc *types.StateAccount) error
- func (t *VerkleTrie) UpdateContractCode(addr common.Address, codeHash common.Hash, code []byte) error
- func (t *VerkleTrie) UpdateStorage(address common.Address, key, value []byte) error
Constants ¶
const ( PUSH1 = byte(0x60) PUSH32 = byte(0x7f) )
Copy the values here so as to avoid an import cycle
Variables ¶
var ErrCommitted = errors.New("trie is already committed")
ErrCommitted is returned when a already committed trie is requested for usage. The potential usages can be `Get`, `Update`, `Delete`, `NodeIterator`, `Prove` and so on.
var HashDefaults = &Config{ Preimages: false, HashDB: hashdb.Defaults, }
HashDefaults represents a config for using hash-based scheme with default settings.
Functions ¶
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, 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 ChunkedCode ¶ added in v0.6.5
type ChunkedCode []byte
ChunkedCode represents a sequence of 32-bytes chunks of code (31 bytes of which are actual code, and 1 byte is the pushdata offset).
func ChunkifyCode ¶ added in v0.6.5
func ChunkifyCode(code []byte) ChunkedCode
ChunkifyCode generates the chunked version of an array representing EVM bytecode
type Config ¶
type Config struct { Preimages bool // Flag whether the preimage of node key is recorded IsVerkle bool // Flag whether the db is holding a verkle tree HashDB *hashdb.Config // Configs for hash-based scheme PathDB *pathdb.Config // Configs for experimental path-based scheme }
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 ¶
NewDatabase initializes the trie database with default settings, note the legacy hash-based scheme is used by default.
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 v0.5.9
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 ¶
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 ¶
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) Disable ¶ added in v0.6.4
Disable deactivates the database and invalidates all available state layers as stale to prevent access to the persistent state, which is in the syncing stage.
It's only supported by path-based database and will return an error for others.
func (*Database) Enable ¶ added in v0.6.4
Enable activates database and resets the state tree with the provided persistent state root once the state sync is finished.
func (*Database) Initialized ¶ added in v0.5.9
Initialized returns an indicator if the state data is already initialized according to the state scheme.
func (*Database) IsVerkle ¶ added in v0.6.5
IsVerkle returns the indicator if the database is holding a verkle tree.
func (*Database) Journal ¶ added in v0.6.4
Journal commits an entire diff hierarchy to disk into a single journal entry. This is meant to be used during shutdown to persist the snapshot without flattening everything down (bad for reorgs). It's only supported by path-based database and will return an error for others.
func (*Database) Preimage ¶ added in v0.2.4
Preimage retrieves a cached trie node pre-image from memory. If it cannot be found cached, the method queries the persistent database for the content.
func (*Database) Reader ¶ added in v0.5.9
Reader returns a reader for accessing all trie nodes with provided state root. An error will be returned if the requested state is not available.
func (*Database) Recover ¶ added in v0.6.4
Recover rollbacks the database to a specified historical point. The state is supported as the rollback destination only if it's canonical state and the corresponding trie histories are existent. It's only supported by path-based database and will return an error for others.
func (*Database) Recoverable ¶ added in v0.6.4
Recoverable returns the indicator if the specified state is enabled to be recovered. It's only supported by path-based database and will return an error for others.
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.
It's only supported by hash-based database and will return an error for others.
func (*Database) SetBufferSize ¶ added in v0.6.4
SetBufferSize sets the node buffer size to the provided value(in bytes). It's only supported by path-based database and will return an error for others.
func (*Database) Size ¶
func (db *Database) Size() (common.StorageSize, common.StorageSize, common.StorageSize)
Size returns the storage size of diff layer nodes above the persistent disk layer, the dirty nodes buffered within the disk layer, and the size of cached preimages.
func (*Database) Update ¶ added in v0.4.1
func (db *Database) Update(root common.Hash, parent common.Hash, block uint64, nodes *trienode.MergedNodeSet, states *triestate.Set) 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.
The passed in maps(nodes, states) will be retained to avoid copying everything. Therefore, these maps must not be changed afterwards.
func (*Database) UpdateAndReferenceRoot ¶ added in v0.4.1
func (*Database) WritePreimages ¶ added in v0.2.4
func (db *Database) WritePreimages()
WritePreimages flushes all accumulated preimages to disk forcibly.
type ID ¶ added in v0.5.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.5.3
StateTrieID constructs an identifier for state trie with the provided state root.
func StorageTrieID ¶ added in v0.5.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 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 v0.4.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 NodeResolver ¶ added in v0.5.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 Reader ¶ added in v0.5.3
type Reader interface { // Node retrieves the 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. // // When looking up nodes in the account trie, 'owner' is the zero hash. For contract // storage trie nodes, 'owner' is the hash of the account address that containing the // storage. // // TODO(rjl493456442): remove the 'hash' parameter, it's redundant in PBSS. 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.
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 NewStackTrie ¶
func NewStackTrie(options *StackTrieOptions) *StackTrie
NewStackTrie allocates and initializes an empty trie.
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 have been committed already. The main purpose here is to commit the nodes on right boundary.
For stack trie, Hash and Commit are functionally identical.
func (*StackTrie) Hash ¶
Hash 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 have been committed already. The main purpose here is to commit the nodes on right boundary.
For stack trie, Hash and Commit are functionally identical.
func (*StackTrie) MustUpdate ¶ added in v0.5.9
MustUpdate is a wrapper of Update and will omit any encountered error but just print out an error message.
type StackTrieOptions ¶ added in v0.6.5
type StackTrieOptions struct { Writer func(path []byte, hash common.Hash, blob []byte) // The function to commit the dirty nodes Cleaner func(path []byte) // The function to clean up dangling nodes SkipLeftBoundary bool // Flag whether the nodes on the left boundary are skipped for committing SkipRightBoundary bool // Flag whether the nodes on the right boundary are skipped for committing // contains filtered or unexported fields }
StackTrieOptions contains the configured options for manipulating the stackTrie.
func NewStackTrieOptions ¶ added in v0.6.5
func NewStackTrieOptions() *StackTrieOptions
NewStackTrieOptions initializes an empty options for stackTrie.
func (*StackTrieOptions) WithCleaner ¶ added in v0.6.5
func (o *StackTrieOptions) WithCleaner(cleaner func(path []byte)) *StackTrieOptions
WithCleaner configures the cleaner in the option for removing dangling nodes.
func (*StackTrieOptions) WithSkipBoundary ¶ added in v0.6.5
func (o *StackTrieOptions) WithSkipBoundary(skipLeft, skipRight bool, gauge metrics.Gauge) *StackTrieOptions
WithSkipBoundary configures whether the left and right boundary nodes are filtered for committing, along with a gauge metrics to track how many boundary nodes are met.
func (*StackTrieOptions) WithWriter ¶ added in v0.6.5
func (o *StackTrieOptions) WithWriter(writer func(path []byte, hash common.Hash, blob []byte)) *StackTrieOptions
WithWriter configures trie node writer within the options.
type StateTrie ¶ added in v0.4.1
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.4.1
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.4.1
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) DeleteAccount ¶ added in v0.5.9
DeleteAccount abstracts an account deletion from the trie.
func (*StateTrie) DeleteStorage ¶ added in v0.5.9
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 v0.5.9
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 v0.5.9
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 v0.4.1
GetKey returns the sha3 preimage of a hashed key that was previously used to store a value.
func (*StateTrie) GetNode ¶ added in v0.5.9
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 v0.5.9
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 v0.4.1
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 v0.5.9
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 v0.5.9
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) MustNodeIterator ¶ added in v0.6.3
func (t *StateTrie) MustNodeIterator(start []byte) NodeIterator
MustNodeIterator is a wrapper of NodeIterator and will omit any encountered error but just print out an error message.
func (*StateTrie) MustUpdate ¶ added in v0.5.9
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 v0.4.1
func (t *StateTrie) NodeIterator(start []byte) (NodeIterator, error)
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.4.1
func (t *StateTrie) Prove(key []byte, 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 v0.5.9
UpdateAccount will abstract the write of an account to the secure trie.
func (*StateTrie) UpdateContractCode ¶ added in v0.6.3
func (*StateTrie) UpdateStorage ¶ added in v0.5.9
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 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 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.4.0
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) Delete ¶
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 ¶
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 v0.5.9
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 ¶
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 v0.5.9
MustDelete is a wrapper of Delete and will omit any encountered error but just print out an error message.
func (*Trie) MustGet ¶ added in v0.5.9
MustGet is a wrapper of Get and will omit any encountered error but just print out an error message.
func (*Trie) MustGetNode ¶ added in v0.5.9
MustGetNode is a wrapper of GetNode and will omit any encountered error but just print out an error message.
func (*Trie) MustNodeIterator ¶ added in v0.6.3
func (t *Trie) MustNodeIterator(start []byte) NodeIterator
MustNodeIterator is a wrapper of NodeIterator and will omit any encountered error but just print out an error message.
func (*Trie) MustUpdate ¶ added in v0.5.9
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, error)
NodeIterator returns an iterator that returns nodes of the trie. Iteration starts at the key after the given start key.
func (*Trie) Prove ¶
func (t *Trie) Prove(key []byte, 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) 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.
If the requested node is not present in trie, no error will be returned. If the trie is corrupted, a MissingNodeError is returned.
type VerkleTrie ¶ added in v0.6.5
type VerkleTrie struct {
// contains filtered or unexported fields
}
VerkleTrie is a wrapper around VerkleNode that implements the trie.Trie interface so that Verkle trees can be reused verbatim.
func NewVerkleTrie ¶ added in v0.6.5
func NewVerkleTrie(root common.Hash, db *Database, cache *utils.PointCache) (*VerkleTrie, error)
NewVerkleTrie constructs a verkle tree based on the specified root hash.
func (*VerkleTrie) Copy ¶ added in v0.6.5
func (t *VerkleTrie) Copy() *VerkleTrie
Copy returns a deep-copied verkle tree.
func (*VerkleTrie) DeleteAccount ¶ added in v0.6.5
func (t *VerkleTrie) DeleteAccount(addr common.Address) error
DeleteAccount implements state.Trie, deleting the specified account from the trie. If the account was not existent in the trie, no error will be returned. If the trie is corrupted, an error will be returned.
func (*VerkleTrie) DeleteStorage ¶ added in v0.6.5
func (t *VerkleTrie) DeleteStorage(addr common.Address, key []byte) error
DeleteStorage implements state.Trie, deleting the specified storage slot from the trie. If the storage slot was not existent in the trie, no error will be returned. If the trie is corrupted, an error will be returned.
func (*VerkleTrie) GetAccount ¶ added in v0.6.5
func (t *VerkleTrie) GetAccount(addr common.Address) (*types.StateAccount, error)
GetAccount implements state.Trie, retrieving the account with the specified account address. If the specified account is not in the verkle tree, nil will be returned. If the tree is corrupted, an error will be returned.
func (*VerkleTrie) GetKey ¶ added in v0.6.5
func (t *VerkleTrie) GetKey(key []byte) []byte
GetKey returns the sha3 preimage of a hashed key that was previously used to store a value.
func (*VerkleTrie) GetStorage ¶ added in v0.6.5
GetStorage implements state.Trie, retrieving the storage slot with the specified account address and storage key. If the specified slot is not in the verkle tree, nil will be returned. If the tree is corrupted, an error will be returned.
func (*VerkleTrie) Hash ¶ added in v0.6.5
func (t *VerkleTrie) Hash() common.Hash
Hash returns the root hash of the tree. It does not write to the database and can be used even if the tree doesn't have one.
func (*VerkleTrie) IsVerkle ¶ added in v0.6.5
func (t *VerkleTrie) IsVerkle() bool
IsVerkle indicates if the trie is a Verkle trie.
func (*VerkleTrie) NodeIterator ¶ added in v0.6.5
func (t *VerkleTrie) NodeIterator(startKey []byte) (NodeIterator, error)
NodeIterator implements state.Trie, returning an iterator that returns nodes of the trie. Iteration starts at the key after the given start key.
TODO(gballet, rjl493456442) implement it.
func (*VerkleTrie) Prove ¶ added in v0.6.5
func (t *VerkleTrie) Prove(key []byte, proofDb ethdb.KeyValueWriter) error
Prove implements state.Trie, constructing 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), ending with the node that proves the absence of the key.
TODO(gballet, rjl493456442) implement it.
func (*VerkleTrie) ToDot ¶ added in v0.6.5
func (t *VerkleTrie) ToDot() string
func (*VerkleTrie) UpdateAccount ¶ added in v0.6.5
func (t *VerkleTrie) UpdateAccount(addr common.Address, acc *types.StateAccount) error
UpdateAccount implements state.Trie, writing the provided account into the tree. If the tree is corrupted, an error will be returned.
func (*VerkleTrie) UpdateContractCode ¶ added in v0.6.5
func (t *VerkleTrie) UpdateContractCode(addr common.Address, codeHash common.Hash, code []byte) error
UpdateContractCode implements state.Trie, writing the provided contract code into the trie.
func (*VerkleTrie) UpdateStorage ¶ added in v0.6.5
func (t *VerkleTrie) UpdateStorage(address common.Address, key, value []byte) error
UpdateStorage implements state.Trie, writing the provided storage slot into the tree. If the tree is corrupted, an error will be returned.