mpt

package
v0.0.0-...-0abe6a2 Latest Latest
Warning

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

Go to latest
Published: Sep 19, 2019 License: BSD-3-Clause Imports: 16 Imported by: 1

Documentation

Overview

Package mpt implements Merkle Patricia Tries.

Index

Constants

View Source
const (
	// TyFullNode 全节点类型
	TyFullNode = 1
	// TyShortNode 扩展节点类型
	TyShortNode = 2
	// TyHashNode hash节点类型
	TyHashNode = 3
	// TyValueNode value节点类型
	TyValueNode = 4
)
View Source
const (
	// HashLength hash长度32字节
	HashLength = 32
)

Variables

This section is empty.

Functions

func DelKVPair

func DelKVPair(db dbm.DB, storeDel *types.StoreGet) ([]byte, [][]byte, error)

DelKVPair 剔除key对应的节点在本次tree中,返回新的roothash和key对应的value

func GetKVPair

func GetKVPair(db dbm.DB, storeGet *types.StoreGet) ([][]byte, error)

GetKVPair get values by keys, return values and error

func GetKVPairProof

func GetKVPairProof(db dbm.DB, roothash []byte, key []byte) []byte

GetKVPairProof 获取指定k:v pair的proof证明

func IterateRangeByStateHash

func IterateRangeByStateHash(db dbm.DB, statehash, start, end []byte, ascending bool, fn func([]byte, []byte) bool)

IterateRangeByStateHash 迭代实现功能; statehash:当前状态hash, start:开始查找的key, end: 结束的key, ascending:升序,降序, fn 迭代回调函数

func SetKVPair

func SetKVPair(db dbm.DB, storeSet *types.StoreSet, sync bool) ([]byte, error)

SetKVPair set key value 的对外接口

func VerifyKVPairProof

func VerifyKVPairProof(db dbm.DB, roothash []byte, keyvalue types.KeyValue, proof []byte) bool

VerifyKVPairProof 验证KVPair 的证明

func VerifyProof

func VerifyProof(rootHash common.Hash, key []byte, proofDb dbm.DB) (value []byte, nodes int, err error)

VerifyProof checks merkle proofs. The given proof must contain the value for key in a trie with the given root hash. VerifyProof returns an error if the proof contains invalid trie nodes or the wrong value.

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.

func NewDatabase

func NewDatabase(db dbm.DB) *Database

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

func (*Database) Commit

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

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

Dereference removes an existing reference from a root node.

func (*Database) InsertBlob

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

func (*Database) Nodes

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

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

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

func (*Database) Size

func (db *Database) Size() (float64, float64)

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

type FullNode

type FullNode struct {
	Nodes                []*Node  `protobuf:"bytes,1,rep,name=nodes,proto3" json:"nodes,omitempty"`
	XXX_NoUnkeyedLiteral struct{} `json:"-"`
	XXX_unrecognized     []byte   `json:"-"`
	XXX_sizecache        int32    `json:"-"`
}

FullNode 分支节点

func (*FullNode) Descriptor

func (*FullNode) Descriptor() ([]byte, []int)

func (*FullNode) GetNodes

func (m *FullNode) GetNodes() []*Node

func (*FullNode) ProtoMessage

func (*FullNode) ProtoMessage()

func (*FullNode) Reset

func (m *FullNode) Reset()

func (*FullNode) String

func (m *FullNode) String() string

func (*FullNode) XXX_DiscardUnknown

func (m *FullNode) XXX_DiscardUnknown()

func (*FullNode) XXX_Marshal

func (m *FullNode) XXX_Marshal(b []byte, deterministic bool) ([]byte, error)

func (*FullNode) XXX_Merge

func (dst *FullNode) XXX_Merge(src proto.Message)

func (*FullNode) XXX_Size

func (m *FullNode) XXX_Size() int

func (*FullNode) XXX_Unmarshal

func (m *FullNode) XXX_Unmarshal(b []byte) error

type HashNode

type HashNode struct {
	Hash                 []byte   `protobuf:"bytes,1,opt,name=hash,proto3" json:"hash,omitempty"`
	XXX_NoUnkeyedLiteral struct{} `json:"-"`
	XXX_unrecognized     []byte   `json:"-"`
	XXX_sizecache        int32    `json:"-"`
}

HashNode hash节点

func (*HashNode) Descriptor

func (*HashNode) Descriptor() ([]byte, []int)

func (*HashNode) GetHash

func (m *HashNode) GetHash() []byte

func (*HashNode) ProtoMessage

func (*HashNode) ProtoMessage()

func (*HashNode) Reset

func (m *HashNode) Reset()

func (*HashNode) String

func (m *HashNode) String() string

func (*HashNode) XXX_DiscardUnknown

func (m *HashNode) XXX_DiscardUnknown()

func (*HashNode) XXX_Marshal

func (m *HashNode) XXX_Marshal(b []byte, deterministic bool) ([]byte, error)

func (*HashNode) XXX_Merge

func (dst *HashNode) XXX_Merge(src proto.Message)

func (*HashNode) XXX_Size

func (m *HashNode) XXX_Size() int

func (*HashNode) XXX_Unmarshal

func (m *HashNode) XXX_Unmarshal(b []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) 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 Node

type Node struct {
	// Types that are valid to be assigned to Value:
	//	*Node_Full
	//	*Node_Short
	//	*Node_Hash
	//	*Node_Val
	Value                isNode_Value `protobuf_oneof:"value"`
	Ty                   int32        `protobuf:"varint,1,opt,name=Ty,proto3" json:"Ty,omitempty"`
	Index                int32        `protobuf:"varint,6,opt,name=index,proto3" json:"index,omitempty"`
	XXX_NoUnkeyedLiteral struct{}     `json:"-"`
	XXX_unrecognized     []byte       `json:"-"`
	XXX_sizecache        int32        `json:"-"`
}

Node mpt涉及到的node类型由四种基本类型

func (*Node) Descriptor

func (*Node) Descriptor() ([]byte, []int)

func (*Node) GetFull

func (m *Node) GetFull() *FullNode

func (*Node) GetHash

func (m *Node) GetHash() *HashNode

func (*Node) GetIndex

func (m *Node) GetIndex() int32

func (*Node) GetShort

func (m *Node) GetShort() *ShortNode

func (*Node) GetTy

func (m *Node) GetTy() int32

func (*Node) GetVal

func (m *Node) GetVal() *ValueNode

func (*Node) GetValue

func (m *Node) GetValue() isNode_Value

func (*Node) ProtoMessage

func (*Node) ProtoMessage()

func (*Node) Reset

func (m *Node) Reset()

func (*Node) String

func (m *Node) String() string

func (*Node) XXX_DiscardUnknown

func (m *Node) XXX_DiscardUnknown()

func (*Node) XXX_Marshal

func (m *Node) XXX_Marshal(b []byte, deterministic bool) ([]byte, error)

func (*Node) XXX_Merge

func (dst *Node) XXX_Merge(src proto.Message)

func (*Node) XXX_OneofFuncs

func (*Node) XXX_OneofFuncs() (func(msg proto.Message, b *proto.Buffer) error, func(msg proto.Message, tag, wire int, b *proto.Buffer) (bool, error), func(msg proto.Message) (n int), []interface{})

XXX_OneofFuncs is for the internal use of the proto package.

func (*Node) XXX_Size

func (m *Node) XXX_Size() int

func (*Node) XXX_Unmarshal

func (m *Node) XXX_Unmarshal(b []byte) error

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
}

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 Node_Full

type Node_Full struct {
	Full *FullNode `protobuf:"bytes,2,opt,name=full,proto3,oneof"`
}

type Node_Hash

type Node_Hash struct {
	Hash *HashNode `protobuf:"bytes,4,opt,name=hash,proto3,oneof"`
}

type Node_Short

type Node_Short struct {
	Short *ShortNode `protobuf:"bytes,3,opt,name=short,proto3,oneof"`
}

type Node_Val

type Node_Val struct {
	Val *ValueNode `protobuf:"bytes,5,opt,name=val,proto3,oneof"`
}

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 common.Hash, db *Database, cachelimit uint16) (*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 Commit. cachelimit sets the number of past cache generations to keep.

func (*SecureTrie) Commit

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

Commit 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() common.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) Prove

func (t *SecureTrie) Prove(key []byte, fromLevel uint, proofDb dbm.DB) error

Prove constructs a merkle proof for key. The result contains all encoded nodes on the path to the value at key. The value itself is also included in the last node and can be retrieved by verifying the proof.

If the trie does not contain a value for key, the returned proof contains all nodes of the longest existing prefix of the key (at least the root node), ending with the node that proves the absence of the key.

func (*SecureTrie) Root

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

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

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 ShortNode

type ShortNode struct {
	Key                  []byte   `protobuf:"bytes,1,opt,name=key,proto3" json:"key,omitempty"`
	Val                  *Node    `protobuf:"bytes,2,opt,name=val,proto3" json:"val,omitempty"`
	XXX_NoUnkeyedLiteral struct{} `json:"-"`
	XXX_unrecognized     []byte   `json:"-"`
	XXX_sizecache        int32    `json:"-"`
}

ShortNode 扩展/叶子节点

func (*ShortNode) Descriptor

func (*ShortNode) Descriptor() ([]byte, []int)

func (*ShortNode) GetKey

func (m *ShortNode) GetKey() []byte

func (*ShortNode) GetVal

func (m *ShortNode) GetVal() *Node

func (*ShortNode) ProtoMessage

func (*ShortNode) ProtoMessage()

func (*ShortNode) Reset

func (m *ShortNode) Reset()

func (*ShortNode) String

func (m *ShortNode) String() string

func (*ShortNode) XXX_DiscardUnknown

func (m *ShortNode) XXX_DiscardUnknown()

func (*ShortNode) XXX_Marshal

func (m *ShortNode) XXX_Marshal(b []byte, deterministic bool) ([]byte, error)

func (*ShortNode) XXX_Merge

func (dst *ShortNode) XXX_Merge(src proto.Message)

func (*ShortNode) XXX_Size

func (m *ShortNode) XXX_Size() int

func (*ShortNode) XXX_Unmarshal

func (m *ShortNode) XXX_Unmarshal(b []byte) error

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

func (t *Trie) Commit2Db(node common.Hash, report bool) error

Commit2Db 保存tire数据到db

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

func (t *Trie) Prove(key []byte, fromLevel uint, proofDb dbm.DB) error

Prove constructs a merkle proof for key. The result contains all encoded nodes on the path to the value at key. The value itself is also included in the last node and can be retrieved by verifying the proof.

If the trie does not contain a value for key, the returned proof contains all nodes of the longest existing prefix of the key (at least the root node), ending with the node that proves the absence of the 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) 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.

type TrieEx

type TrieEx struct {
	*Trie
}

TrieEx Trie扩展,可以相应enableSecure

func NewEx

func NewEx(root common.Hash, db *Database) (*TrieEx, error)

NewEx creates a trie

func (*TrieEx) Commit

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

Commit writes all nodes to the trie's memory database

func (*TrieEx) Commit2Db

func (t *TrieEx) Commit2Db(node common.Hash, report bool) error

Commit2Db writes all nodes to the trie's database

func (*TrieEx) Delete

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

Delete removes any existing value for key from the trie.

func (*TrieEx) Get

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

Get returns the value for key stored in the trie

func (*TrieEx) TryDelete

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

TryDelete removes any existing value for key from the trie.

func (*TrieEx) TryGet

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

TryGet returns the value for key stored in the trie.

func (*TrieEx) TryUpdate

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

TryUpdate set key with value in the trie

func (*TrieEx) Update

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

Update set key with value in the trie

type ValueNode

type ValueNode struct {
	Value                []byte   `protobuf:"bytes,1,opt,name=value,proto3" json:"value,omitempty"`
	XXX_NoUnkeyedLiteral struct{} `json:"-"`
	XXX_unrecognized     []byte   `json:"-"`
	XXX_sizecache        int32    `json:"-"`
}

ValueNode value节点

func (*ValueNode) Descriptor

func (*ValueNode) Descriptor() ([]byte, []int)

func (*ValueNode) GetValue

func (m *ValueNode) GetValue() []byte

func (*ValueNode) ProtoMessage

func (*ValueNode) ProtoMessage()

func (*ValueNode) Reset

func (m *ValueNode) Reset()

func (*ValueNode) String

func (m *ValueNode) String() string

func (*ValueNode) XXX_DiscardUnknown

func (m *ValueNode) XXX_DiscardUnknown()

func (*ValueNode) XXX_Marshal

func (m *ValueNode) XXX_Marshal(b []byte, deterministic bool) ([]byte, error)

func (*ValueNode) XXX_Merge

func (dst *ValueNode) XXX_Merge(src proto.Message)

func (*ValueNode) XXX_Size

func (m *ValueNode) XXX_Size() int

func (*ValueNode) XXX_Unmarshal

func (m *ValueNode) XXX_Unmarshal(b []byte) error

Jump to

Keyboard shortcuts

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