Class MerkleTrie
An ITrie implementation implemented Merkle Patricia Trie.
Inheritance
Implements
Namespace: Libplanet.Store.Trie
Assembly: Libplanet.Store.dll
Syntax
public class MerkleTrie : object, ITrie
Constructors
| Improve this Doc View SourceMerkleTrie(IKeyValueStore, HashDigest<SHA256>, HashNodeCache)
An ITrie implementation.
Declaration
public MerkleTrie(IKeyValueStore keyValueStore, HashDigest<SHA256> rootHash, HashNodeCache cache = null)
Parameters
Type | Name | Description |
---|---|---|
IKeyValueStore | keyValueStore | The IKeyValueStore storage to store nodes. |
HashDigest<SHA256> | rootHash | The root Hash of MerkleTrie. |
HashNodeCache | cache | The HashNodeCache to use as cache. |
MerkleTrie(IKeyValueStore, INode, HashNodeCache)
An ITrie implementation.
Declaration
public MerkleTrie(IKeyValueStore keyValueStore, INode root = null, HashNodeCache cache = null)
Parameters
Type | Name | Description |
---|---|---|
IKeyValueStore | keyValueStore | The IKeyValueStore storage to store nodes. |
INode | root | The root node of MerkleTrie. If it is null, it will be treated like empty trie. |
HashNodeCache | cache | The HashNodeCache to use as cache. |
Fields
| Improve this Doc View SourceEmptyRootHash
An ITrie implementation implemented Merkle Patricia Trie.
Declaration
public static readonly HashDigest<SHA256> EmptyRootHash
Field Value
Type | Description |
---|---|
HashDigest<SHA256> |
Properties
| Improve this Doc View SourceHash
The state root hash of the trie.
Declaration
public HashDigest<SHA256> Hash { get; }
Property Value
Type | Description |
---|---|
HashDigest<SHA256> |
Remarks
If Root is null, this still gives a unique HashDigest<T> value corresponding to null that is never recorded.
See Also
| Improve this Doc View SourceRecorded
Whether Root is recorded in the store.
Declaration
public bool Recorded { get; }
Property Value
Type | Description |
---|---|
System.Boolean |
Remarks
A Root that is null is always considered as recorded.
Root
The root of the ITrie. This is null if and only if the ITrie is empty. That is, this is never a "hashed node" of a null root.
Declaration
public INode Root { get; }
Property Value
Type | Description |
---|---|
INode |
See Also
Methods
| Improve this Doc View SourceDiff(ITrie)
Lists every non-null other
given any KeyBytes path.
Declaration
public IEnumerable<(KeyBytes Path, IValue? TargetValue, IValue SourceValue)> Diff(ITrie other)
Parameters
Type | Name | Description |
---|---|---|
ITrie | other | The other MerkleTrie to compare to. |
Returns
Type | Description |
---|---|
IEnumerable<System.ValueTuple<KeyBytes, System.Nullable<IValue>, IValue>> | A list of tuples where each tuple consists of the path where
the difference occurred, the "old" value from |
Remarks
This operation has the following properties:
-
This operation is non-symmetric. That is, in general,
trieA.Diff(trieB)
andtrieB.Diff(trieA)
are not the same. -
Values existing in
other
but not in the source instance, considered as null in the source, are not included in the result.
Exceptions
Type | Condition |
---|---|
InvalidTrieNodeException | Thrown when the method fails to traverse the ITrie. |
GenerateProof(KeyBytes, IValue)
Generates a proof of existence for value
at key
.
Declaration
public IReadOnlyList<INode> GenerateProof(KeyBytes key, IValue value)
Parameters
Type | Name | Description |
---|---|---|
KeyBytes | key | The path in KeyBytes to search. |
IValue | value | The |
Returns
Type | Description |
---|---|
IReadOnlyList<INode> | An |
Remarks
In order to generate a valid proof, both a valid key
and
a valid value
must be known beforehand.
Get(IReadOnlyList<KeyBytes>)
Gets the values stored with keys
in Set(in KeyBytes, IValue).
Declaration
public IReadOnlyList<IValue?> Get(IReadOnlyList<KeyBytes> keys)
Parameters
Type | Name | Description |
---|---|---|
IReadOnlyList<KeyBytes> | keys | The keys used in Set(in KeyBytes, IValue) to store a value. |
Returns
Type | Description |
---|---|
IReadOnlyList<System.Nullable<IValue>> | The values associated to the specified |
Get(KeyBytes)
Gets the values stored with key
in Set(in KeyBytes, IValue).
Declaration
public IValue? Get(KeyBytes key)
Parameters
Type | Name | Description |
---|---|---|
KeyBytes | key | The key used in Set(in KeyBytes, IValue) to store a value. |
Returns
Type | Description |
---|---|
System.Nullable<IValue> | The value associated to the specified |
GetNode(Nibbles)
Gets the first node encountered at nibbles
when traversing down
from Root.
Declaration
public INode GetNode(Nibbles nibbles)
Parameters
Type | Name | Description |
---|---|---|
Nibbles | nibbles | The path to check. |
Returns
Type | Description |
---|---|
INode | A node at |
Remarks
There may be more than one INode at nibbles
.
For instance, a FullNode, a ValueNode as the value of the
aforementioned FullNode, and up to two additional HashNodes
is possible.
As such, for two equivalent ITries, Libplanet.Store.Tries that would have the same committed Hashes, this may return different types of INode depending on the actual underlying "structure". However, returned INodes for such ITries are equivalent as sub-ITries.
Exceptions
Type | Condition |
---|---|
InvalidTrieNodeException | Thrown when an unknown type of INode is encountered while traversing to the given path. |
IterateNodes()
Declaration
public IEnumerable<(Nibbles Path, INode Node)> IterateNodes()
Returns
Type | Description |
---|---|
IEnumerable<System.ValueTuple<Nibbles, INode>> | An |
Remarks
This is a very heavy operation.
IterateSubTrieNodes(KeyBytes)
Declaration
public IEnumerable<(Nibbles Path, INode Node)> IterateSubTrieNodes(KeyBytes rootPath)
Parameters
Type | Name | Description |
---|---|---|
KeyBytes | rootPath | The KeyBytes path of an INode to use as the root of traversal. |
Returns
Type | Description |
---|---|
IEnumerable<System.ValueTuple<Nibbles, INode>> |
Remarks
This requires an INode to exist at rootPath
.
As such, this does not necessarily return all INode with paths starting
with rootPath
. In particular, if there doesn't exist
an INode at rootPath
, this returns nothing.
IterateSubTrieValues(KeyBytes)
Declaration
public IEnumerable<(KeyBytes Path, IValue Value)> IterateSubTrieValues(KeyBytes rootPath)
Parameters
Type | Name | Description |
---|---|---|
KeyBytes | rootPath | The KeyBytes path of an INode to use as the root of traversal. |
Returns
Type | Description |
---|---|
IEnumerable<System.ValueTuple<KeyBytes, IValue>> | All |
Remarks
This requires an INode to exist at rootPath
.
As such, this does not necessarily return all INode with paths starting
with rootPath
. In particular, if there doesn't exist
an INode at rootPath
, this returns nothing.
IterateValues()
Iterates and every stored
Declaration
public IEnumerable<(KeyBytes Path, IValue Value)> IterateValues()
Returns
Type | Description |
---|---|
IEnumerable<System.ValueTuple<KeyBytes, IValue>> | An |
Remarks
This is a very heavy operation.
Remove(in KeyBytes)
Removes the value at the path corresponding to given key
in memory. If there is no key
,
this does nothing.
Declaration
public ITrie Remove(in KeyBytes key)
Parameters
Type | Name | Description |
---|---|---|
KeyBytes | key | The unique key to associate with the |
Returns
Type | Description |
---|---|
ITrie | Returns new updated ITrie. |
Remarks
This should not actually remove anything from storage.
The removal of the value at the marked path given by key
is actually
recorded to storage when Commit(ITrie) is called.
Regardless, there is actually no removal of any value from storage even when
Commit(ITrie) is called.
See Also
| Improve this Doc View SourceSet(in KeyBytes, IValue)
Stores the value
at the path corresponding to
given key
in memory.
Declaration
public ITrie Set(in KeyBytes key, IValue value)
Parameters
Type | Name | Description |
---|---|---|
KeyBytes | key | The unique key to associate with the |
IValue | value | The value to store. |
Returns
Type | Description |
---|---|
ITrie | Returns new updated ITrie. |
Remarks
This should not actually write anything to storage.
Stored value
is actually written to storage when
Commit(ITrie) is called.
See Also
| Improve this Doc View SourceValidateProof(HashDigest<SHA256>, IReadOnlyList<INode>, KeyBytes, IValue)
Checks whether given proof
is a valid proof
for given key
and value
against
given stateRootHash
.
Declaration
public static bool ValidateProof(HashDigest<SHA256> stateRootHash, IReadOnlyList<INode> proof, KeyBytes key, IValue value)
Parameters
Type | Name | Description |
---|---|---|
HashDigest<SHA256> | stateRootHash | |
IReadOnlyList<INode> | proof | An |
KeyBytes | key | The path in KeyBytes to validate. |
IValue | value | The |
Returns
Type | Description |
---|---|
System.Boolean | true if |