trie

package
v1.0.11 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 10, 2020 License: GPL-3.0 Imports: 21 Imported by: 0

Documentation

Overview

Package trie implements Merkle Patricia Tries.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CreateNodeCache

func CreateNodeCache(size int, db tasdb.Database)

Types

type DatabaseReader

type DatabaseReader interface {
	// Get retrieves the value associated with key form the database.
	Get(key []byte) (value []byte, err error)

	// Has retrieves whether a key is present in the database.
	Has(key []byte) (bool, error)
}

DatabaseReader wraps the Get and Has method of a backing store for the trie.

type ExtLeafCallback

type ExtLeafCallback func(key []byte, value []byte) error

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

func (*Iterator) EnableNodeCache

func (it *Iterator) EnableNodeCache()

EnableNodeCache enables node cache

func (*Iterator) Next

func (it *Iterator) Next() bool

Next moves the iterator forward one key-value entry.

func (*Iterator) Prove

func (it *Iterator) Prove() [][]byte

Prove generates the Merkle proof for the leaf node the iterator is currently positioned on.

type LeafCallback

type LeafCallback func(leaf []byte, parent common.Hash) error

LeafCallback is a callback type invoked when a trie operation reaches a leaf node. It's used by state sync and commit to allow handling external references between account and storage tries.

type MissingNodeError

type MissingNodeError struct {
	NodeHash common.Hash // hash of the missing node
	Path     []byte      // hex-encoded path to the missing node
}

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

type NodeDatabase

type NodeDatabase struct {
	// contains filtered or unexported fields
}

NodeDatabase 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.

func NewDatabase

func NewDatabase(diskdb tasdb.Database, cacheSize int, cacheDir string, pruneEnable bool) *NodeDatabase

NewDatabase creates a new trie database to store ephemeral trie content before its written out to disk or garbage collected.

func (*NodeDatabase) CanPersistent

func (db *NodeDatabase) CanPersistent(persistentCount int) bool

CanPersistent check can persistent if prune block count more than persistentCount

func (*NodeDatabase) Cap

func (db *NodeDatabase) Cap(height uint64, limit common.StorageSize) error

Cap iteratively flushes old but still referenced trie nodes until the total memory usage goes below the given threshold.

func (*NodeDatabase) Commit

func (db *NodeDatabase) Commit(height uint64, node common.Hash, report bool) error

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 (*NodeDatabase) CommitStateDataToBigDb

func (db *NodeDatabase) CommitStateDataToBigDb(blobs []*storeBlob, repeatKey map[common.Hash]struct{}) error

CommitStateDataToBigDb is for cold start Commit the state data to big db

func (*NodeDatabase) DecodeStoreBlob

func (db *NodeDatabase) DecodeStoreBlob(data []byte) (err error, blobs []*storeBlob)

func (*NodeDatabase) Dereference

func (db *NodeDatabase) Dereference(height uint64, root common.Hash)

Dereference removes an existing reference from a root node.

func (*NodeDatabase) DiskDB

func (db *NodeDatabase) DiskDB() DatabaseReader

DiskDB retrieves the persistent storage backing the trie database.

func (*NodeDatabase) InsertBlob

func (db *NodeDatabase) InsertBlob(hash common.Hash, blob []byte)

InsertBlob writes a new reference tracked blob to the memory database if it's yet unknown. This method should only be used for non-trie nodes that require reference counting, since trie nodes are garbage collected directly through their embedded children.

func (*NodeDatabase) InsertStateDataToSmallDb

func (db *NodeDatabase) InsertStateDataToSmallDb(height uint64, root common.Hash, smallDbWriter SmallDbWriter) error

InsertStateDatasToSmallDb insert nodes to small db,generate one record(use rlp)

func (*NodeDatabase) LastPruneHeight

func (db *NodeDatabase) LastPruneHeight() uint64

func (*NodeDatabase) Node

func (db *NodeDatabase) Node(hash common.Hash) ([]byte, error)

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 (*NodeDatabase) Nodes

func (db *NodeDatabase) Nodes() []common.Hash

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 (*NodeDatabase) Reference

func (db *NodeDatabase) Reference(child common.Hash, parent common.Hash)

Reference adds a new reference from a parent node to a child node.

func (*NodeDatabase) ResetNodeCache

func (db *NodeDatabase) ResetNodeCache()

ResetNodeCache make new slice before commit.

func (*NodeDatabase) ResetPruneCount

func (db *NodeDatabase) ResetPruneCount()

func (*NodeDatabase) SaveCache

func (db *NodeDatabase) SaveCache() error

func (*NodeDatabase) Size

Size returns the current storage size of the memory cache in front of the persistent database layer.

func (*NodeDatabase) StorePruneData

func (db *NodeDatabase) StorePruneData(height, currentHeight, cpHeight, pruneCount uint64)

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

	// 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

	// EnableNodeCache enables the node cache when iterates the trie, which may
	// accelerate most the visits of those tries that does not change often and
	// accelerate little with those tries than change often
	EnableNodeCache()
}

NodeIterator is an iterator to traverse the trie pre-order.

type ResolveNodeCallback

type ResolveNodeCallback func(hash common.Hash, data []byte)

type SmallDbWriter

type SmallDbWriter interface {
	StoreDataToSmallDb(height uint64, nb []byte) error
}

SmallDbWriter wraps the Get and Has method of a backing store for the current block state's modify datas.

type Trie

type Trie struct {
	// contains filtered or unexported fields
}

Trie is a Merkle Patricia Trie. The zero value is an empty trie with no database. Use NewTrie to create a trie that sits on top of a database.

Trie is not safe for concurrent use.

func NewTrie

func NewTrie(root common.Hash, db *NodeDatabase) (*Trie, error)

NewTrie creates a trie with an existing root node from db.

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, NewTrie 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 (*Trie) Commit

func (t *Trie) Commit(onleaf LeafCallback) (root common.Hash, err error)

Commit writes all nodes to the trie's memory database, tracking the internal and external (for account tries) references.

func (*Trie) Delete

func (t *Trie) Delete(key []byte)

Delete removes any existing value for key from the trie.

func (*Trie) Get

func (t *Trie) Get(key []byte) []byte

Get returns the value for key stored in the trie. The value bytes must not be modified by the caller.

func (*Trie) Hash

func (t *Trie) Hash() common.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) Root

func (t *Trie) Root() []byte

Root returns the root hash of the trie. Deprecated: use Hash instead.

func (*Trie) SetCacheLimit

func (t *Trie) SetCacheLimit(l uint16)

SetCacheLimit sets the number of 'cache generations' to keep. A cache generation is created by a call to Commit.

func (*Trie) Traverse

func (t *Trie) Traverse(onleaf ExtLeafCallback, resolve ResolveNodeCallback, checkHash bool) (bool, error)

Traverse is a debug method to iterate over the entire trie stored in the disk and check whether every node is reachable from the meta root. The goal is to find any errors that might cause trie nodes missing during prune

This method is extremely CPU and disk intensive, and time consuming, only use when must.

func (*Trie) TraverseKey

func (t *Trie) TraverseKey(key []byte, onleaf ExtLeafCallback, resolve ResolveNodeCallback, checkHash bool) (ok bool, err error)

TraverseKey traverses the path of the given key in the trie tree, An error is returned when the specified path cannot be found or some node is missing

func (*Trie) TryDelete

func (t *Trie) TryDelete(key []byte) error

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

func (t *Trie) TryGet(key []byte) ([]byte, error)

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) TryUpdate

func (t *Trie) TryUpdate(key, value []byte) error

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

func (t *Trie) Update(key, value []byte)

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL