github.com/chain5j/chain5j-pkg@v1.0.7/collection/trees/tree/tree.go (about)

     1  // Package tree
     2  //
     3  // @author: xwc1125
     4  package tree
     5  
     6  import (
     7  	"github.com/chain5j/chain5j-pkg/database/kvstore"
     8  	"github.com/chain5j/chain5j-pkg/types"
     9  )
    10  
    11  type Tree interface {
    12  	// TryGet returns the value for key stored in the trie. The value bytes must
    13  	// not be modified by the caller. If a node was not found in the database, a
    14  	// trie.MissingNodeError is returned.
    15  	TryGet(key []byte) ([]byte, error)
    16  
    17  	// TryUpdate associates key with value in the trie. If value has length zero, any
    18  	// existing value is deleted from the trie. The value bytes must not be modified
    19  	// by the caller while they are stored in the trie. If a node was not found in the
    20  	// database, a trie.MissingNodeError is returned.
    21  	TryUpdate(key, value []byte) error
    22  
    23  	// TryDelete removes any existing value for key from the trie. If a node was not
    24  	// found in the database, a trie.MissingNodeError is returned.
    25  	TryDelete(key []byte) error
    26  
    27  	// Hash returns the root hash of the trie. It does not write to the database and
    28  	// can be used even if the trie doesn't have one.
    29  	Hash() types.Hash
    30  
    31  	// Commit writes all nodes to the trie's memory database, tracking the internal
    32  	// and external (for account tries) references.
    33  	Commit(onleaf LeafCallback) (types.Hash, error)
    34  
    35  	// NodeIterator returns an iterator that returns nodes of the trie. Iteration
    36  	// starts at the key after the given start key.
    37  	NodeIterator(startKey []byte) NodeIterator
    38  
    39  	// Prove constructs a Merkle proof for key. The result contains all encoded nodes
    40  	// on the path to the value at key. The value itself is also included in the last
    41  	// node and can be retrieved by verifying the proof.
    42  	//
    43  	// If the trie does not contain a value for key, the returned proof contains all
    44  	// nodes of the longest existing prefix of the key (at least the root), ending
    45  	// with the node that proves the absence of the key.
    46  	Prove(key []byte, fromLevel uint, proofDb kvstore.KeyValueWriter) error
    47  }