trie

package
v0.0.0-...-a173fca Latest Latest
Warning

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

Go to latest
Published: Mar 1, 2021 License: LGPL-3.0 Imports: 19 Imported by: 0

Documentation

Overview

Package trie implements Merkle Patricia Tries.

Index

Constants

This section is empty.

Variables

View Source
var (
	MODULENAME = "trie"

	// emptyRoot is the known root hash of an empty trie.
	EmptyRoot = crypto.Bytes2Hash(sha3.Keccak256([]byte(stateRoot)))
)

Functions

This section is empty.

Types

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.

Note, the trie Database is **not** thread safe in its mutations, but it **is** thread safe in providing individual, independent node access. The rationale behind this split design is to provide read access to RPC handlers and sync servers even while the trie is executing expensive garbage collection.

func NewDatabase

func NewDatabase(diskdb dbinterface.KeyValueStore) *Database

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 NewDatabaseWithCache

func NewDatabaseWithCache(diskdb dbinterface.KeyValueStore, cache int) *Database

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

Note, this method is a non-synchronized mutator. It is unsafe to call this concurrently with other mutators.

func (*Database) Commit

func (db *Database) Commit(node crypto.Hash, report bool) error

func (*Database) Dereference

func (db *Database) Dereference(root crypto.Hash)

Dereference removes an existing reference from a root node.

func (*Database) DiskDB

func (db *Database) DiskDB() dbinterface.KeyValueReader

DiskDB retrieves the persistent storage backing the trie database.

func (*Database) InsertBlob

func (db *Database) InsertBlob(hash crypto.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 (*Database) Node

func (db *Database) Node(hash crypto.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 (*Database) Nodes

func (db *Database) Nodes() []crypto.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 (*Database) Reference

func (db *Database) Reference(child crypto.Hash, parent crypto.Hash)

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

func (*Database) Size

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

func (*Database) TrieDb

func (db *Database) TrieDb(node crypto.Hash, report bool) error

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

Note, this method is a non-synchronized mutator. It is unsafe to call this concurrently with other mutators.

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) 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 crypto.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 crypto.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 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() crypto.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() crypto.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
}

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 SecureTrie

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

SecureTrie wraps a trie with key hashing. In a secure 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 SecureTrie can only be created with New and must have an attached database. The database also stores the preimage of each key.

SecureTrie is not safe for concurrent use.

func NewSecure

func NewSecure(root crypto.Hash, db *Database) (*SecureTrie, error)

NewSecure creates a trie with an existing root node from a backing database and optional intermediate in-memory node pool.

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.

Accessing the trie loads nodes from the database or node pool on demand. Loaded nodes are kept around until their 'cache generation' expires. A new cache generation is created by each call to TrieDb. cachelimit sets the number of past cache generations to keep.

func NewSecureNewWithRWDB

func NewSecureNewWithRWDB(root crypto.Hash, readDB *Database, writeDB *Database) (*SecureTrie, error)

读数据从oldDB中,数据写入,写到newDB中

func (*SecureTrie) Commit

func (t *SecureTrie) Commit(onleaf LeafCallback) (root crypto.Hash, err error)

TrieDb writes all nodes and the secure hash pre-images to the trie's database. Nodes are stored with their sha3 hash as the key.

Committing flushes nodes from memory. Subsequent Get calls will load nodes from the database.

func (*SecureTrie) Copy

func (t *SecureTrie) Copy() *SecureTrie

Copy returns a copy of SecureTrie.

func (*SecureTrie) Delete

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

Delete removes any existing value for key from the trie.

func (*SecureTrie) Get

func (t *SecureTrie) 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 (*SecureTrie) GetKey

func (t *SecureTrie) GetKey(shaKey []byte) []byte

GetKey returns the sha3 preimage of a hashed key that was previously used to store a value.

func (*SecureTrie) Hash

func (t *SecureTrie) Hash() crypto.Hash

Hash returns the root hash of SecureTrie. It does not write to the database and can be used even if the trie doesn't have one.

func (*SecureTrie) NodeIterator

func (t *SecureTrie) 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 (*SecureTrie) TryDelete

func (t *SecureTrie) 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 (*SecureTrie) TryGet

func (t *SecureTrie) 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 (*SecureTrie) TryUpdate

func (t *SecureTrie) 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 (*SecureTrie) Update

func (t *SecureTrie) 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.

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 New to create a trie that sits on top of a database.

Trie is not safe for concurrent use.

func New

func New(root crypto.Hash, db *Database) (*Trie, error)

New 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, New 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 NewWithRWDB

func NewWithRWDB(root crypto.Hash, readDB *Database, writeDB *Database) (*Trie, error)

数据恢复从oldDB中读取数据,数据存储使用writeDB

func (*Trie) Commit

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

TrieDb 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() crypto.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) 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