trie

package
v0.0.9 Latest Latest
Warning

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

Go to latest
Published: Aug 6, 2021 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidArgument = errors.New("invalid argument")
	// ErrInvalidProtoToNode = errors.New("Pb Message cannot be converted into Trie Node")
	ErrUnknownChildTypeOfFull  = errors.New("unknown child type of fullnode")
	ErrUnknownChildTypeOfShort = errors.New("unknown child type of shortnode")
	ErrUnknownChildrenType     = errors.New("unknown children type")
)
View Source
var ErrCommitDisabled = errors.New("no database for committing")
View Source
var (
	ErrNotFound = errors.New("key not found")
)

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 massdb.KeyValueStore) *Database

func (*Database) Node

func (db *Database) 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.

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.

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

	// AddResolver sets an intermediate database 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(massdb.KeyValueStore)
}

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

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 NewFromBinary

func NewFromBinary(data []byte, db massdb.KeyValueWriter) (*StackTrie, error)

NewFromBinary initialises a serialized stacktrie with the given db.

func NewStackTrie

func NewStackTrie(db massdb.KeyValueWriter) *StackTrie

NewStackTrie allocates and initializes an empty trie.

func (*StackTrie) Commit

func (st *StackTrie) Commit() (common.Hash, error)

Commit will firstly hash the entrie trie if it's still not hashed and then commit all nodes to the associated database. Actually most of the trie nodes MAY have been committed already. The main purpose here is to commit the root node.

The associated database is expected, otherwise the whole commit functionality should be disabled.

func (*StackTrie) Hash

func (st *StackTrie) Hash() (h common.Hash)

Hash returns the hash of the current node

func (*StackTrie) MarshalBinary

func (st *StackTrie) MarshalBinary() (data []byte, err error)

MarshalBinary implements encoding.BinaryMarshaler

func (*StackTrie) Reset

func (st *StackTrie) Reset()

func (*StackTrie) TryUpdate

func (st *StackTrie) TryUpdate(key, value []byte) error

TryUpdate inserts a (key, value) pair into the stack trie

func (*StackTrie) UnmarshalBinary

func (st *StackTrie) UnmarshalBinary(data []byte) error

UnmarshalBinary implements encoding.BinaryUnmarshaler

func (*StackTrie) Update

func (st *StackTrie) Update(key, value []byte)

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 common.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 (*Trie) Clone

func (t *Trie) Clone() (*Trie, error)

Clone the trie to create a new trie sharing the same storage

func (*Trie) Commit

func (t *Trie) Commit() (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) Copy

func (t *Trie) Copy() *Trie

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

func (t *Trie) Reset()

Reset drops the referenced root node and cleans all internal state.

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.

Directories

Path Synopsis
hexutil
Package hexutil implements hex encoding with 0x prefix.
Package hexutil implements hex encoding with 0x prefix.
leveldb
Package leveldb implements the key-value database layer based on LevelDB.
Package leveldb implements the key-value database layer based on LevelDB.
memorydb
Package memorydb implements the key-value database layer based on memory maps.
Package memorydb implements the key-value database layer based on memory maps.
Package triepb is a generated protocol buffer package.
Package triepb is a generated protocol buffer package.

Jump to

Keyboard shortcuts

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