Documentation ¶
Overview ¶
Package node defines the `Node` structure with methods to be used in the modified Merkle-Patricia Radix-16 trie.
Index ¶
- Constants
- Variables
- func MerkleValue(encoding []byte, writer io.Writer) (err error)
- func MerkleValueRoot(rootEncoding []byte, writer io.Writer) (err error)
- type Buffer
- type CopySettings
- type Kind
- type Node
- func (n *Node) CalculateMerkleValue() (merkleValue []byte, err error)
- func (n *Node) CalculateRootMerkleValue() (merkleValue []byte, err error)
- func (n *Node) ChildrenBitmap() (bitmap uint16)
- func (n *Node) Copy(settings CopySettings) *Node
- func (n *Node) Encode(buffer Buffer) (err error)
- func (n *Node) EncodeAndHash() (encoding, merkleValue []byte, err error)
- func (n *Node) EncodeAndHashRoot() (encoding, merkleValue []byte, err error)
- func (n *Node) HasChild() (has bool)
- func (n *Node) Kind() Kind
- func (n *Node) NumChildren() (count int)
- func (n *Node) SetClean()
- func (n *Node) SetDirty()
- func (n *Node) StorageValueEqual(stoageValue []byte) (equal bool)
- func (n *Node) String() string
- func (n *Node) StringNode() (stringNode *gotree.Node)
Constants ¶
const (
// ChildrenCapacity is the maximum number of children in a branch node.
ChildrenCapacity = 16
)
Variables ¶
var ( // DefaultCopySettings contains the following copy settings: // - children are NOT deep copied recursively // - the Merkle value field is left empty on the copy // - the partial key field is deep copied // - the storage value field is deep copied DefaultCopySettings = CopySettings{ CopyPartialKey: true, CopyStorageValue: true, } // DeepCopySettings returns the following copy settings: // - children are deep copied recursively // - the Merkle value field is deep copied // - the partial key field is deep copied // - the storage value field is deep copied DeepCopySettings = CopySettings{ CopyChildren: true, CopyMerkleValue: true, CopyPartialKey: true, CopyStorageValue: true, } )
var ( // ErrDecodeStorageValue is defined since no sentinel error is defined // in the scale package. ErrDecodeStorageValue = errors.New("cannot decode storage value") ErrDecodeHashedStorageValue = errors.New("cannot decode hashed storage value") ErrDecodeHashedValueTooShort = errors.New("hashed storage value too short") ErrReadChildrenBitmap = errors.New("cannot read children bitmap") // ErrDecodeChildHash is defined since no sentinel error is defined // in the scale package. ErrDecodeChildHash = errors.New("cannot decode child hash") )
var (
ErrPartialKeyTooBig = errors.New("partial key length cannot be larger than 2^16")
)
var ErrReaderMismatchCount = errors.New("read unexpected number of bytes from reader")
var ErrVariantUnknown = errors.New("node variant is unknown")
Functions ¶
func MerkleValue ¶
MerkleValue writes the Merkle value from the encoding of a non-root node to the writer given. If the encoding is less or equal to 32 bytes, the Merkle value is the encoding. Otherwise, the Merkle value is the Blake2b hash digest of the encoding.
Types ¶
type CopySettings ¶
type CopySettings struct { // CopyChildren can be set to true to recursively deep copy the eventual // children of the node. This is false by default and should only be used // in tests since it is quite inefficient. CopyChildren bool // CopyMerkleValue can be set to true to deep copy the Merkle value // field on the current node copied. // This is false by default because in production, a node is copied // when it is about to be mutated, hence making its cached Merkle value // field no longer valid. CopyMerkleValue bool // CopyPartialKey can be set to true to deep copy the partial key field of // the node. This is useful when false if the partial key is about to // be assigned after the Copy operation, to save a memory operation. CopyPartialKey bool // CopyStorageValue can be set to true to deep copy the storage value field of // the node. This is useful when false if the storage value is about to // be assigned after the Copy operation, to save a memory operation. CopyStorageValue bool }
CopySettings contains settings to configure the deep copy of a node.
type Node ¶
type Node struct { // PartialKey is the partial key bytes in nibbles (0 to f in hexadecimal) PartialKey []byte StorageValue []byte MustBeHashed bool // IsHashedValue is true when the StorageValue is a blake2b hash IsHashedValue bool // Generation is incremented on every trie Snapshot() call. // Each node also contain a certain Generation number, // which is updated to match the trie Generation once they are // inserted, moved or iterated over. Generation uint64 // Children is a slice of length 16 for branches. // It is left to nil for leaves to reduce memory usage. Children []*Node // Dirty is true when the branch differs // from the node stored in the database. Dirty bool // MerkleValue is the cached Merkle value of the node. MerkleValue []byte // Descendants is the number of descendant nodes for // this particular node. Descendants uint32 }
Node is a node in the trie and can be a leaf or a branch.
func Decode ¶
Decode decodes a node from a reader. The encoding format is documented in the README.md of this package, and specified in the Polkadot spec at https://spec.polkadot.network/#sect-state-storage For branch decoding, see the comments on decodeBranch. For leaf decoding, see the comments on decodeLeaf.
func (*Node) CalculateMerkleValue ¶
CalculateMerkleValue returns the Merkle value of the non-root node.
func (*Node) CalculateRootMerkleValue ¶
CalculateRootMerkleValue returns the Merkle value of the root node.
func (*Node) ChildrenBitmap ¶
ChildrenBitmap returns the 16 bit bitmap of the children in the branch node.
func (*Node) Copy ¶
func (n *Node) Copy(settings CopySettings) *Node
Copy deep copies the node. Setting copyChildren to true will deep copy children as well.
func (*Node) Encode ¶
Encode encodes the node to the buffer given. The encoding format is documented in the README.md of this package, and specified in the Polkadot spec at https://spec.polkadot.network/#sect-state-storage
func (*Node) EncodeAndHash ¶
EncodeAndHash returns the encoding of the node and the Merkle value of the node. See the `MerkleValue` method for more details on the value of the Merkle value. TODO change this function to write to an encoding writer and a merkle value writer, such that buffer sync pools can be used by the caller.
func (*Node) EncodeAndHashRoot ¶
EncodeAndHashRoot returns the encoding of the node and the Merkle value of the node. See the `MerkleValueRoot` method for more details on the value of the Merkle value. TODO change this function to write to an encoding writer and a merkle value writer, such that buffer sync pools can be used by the caller.
func (*Node) NumChildren ¶
NumChildren returns the total number of children in the branch node.
func (*Node) SetClean ¶
func (n *Node) SetClean()
SetClean sets the dirty status to false for the node.
func (*Node) SetDirty ¶
func (n *Node) SetDirty()
SetDirty sets the dirty status to true for the node.
func (*Node) StorageValueEqual ¶
StorageValueEqual returns true if the node storage value is equal to the storage value given as argument. In particular, it returns false if one storage value is nil and the other storage value is the empty slice.
func (*Node) StringNode ¶
StringNode returns a gotree compatible node for String methods.