mpt

package
v1.7.1 Latest Latest
Warning

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

Go to latest
Published: May 26, 2023 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	EmptyNodeRaw     = []byte{}
	EmptyNodeHash, _ = hex.DecodeString("56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421")
)

Functions

func Hash

func Hash(n Node) []byte

func IsNibble

func IsNibble(nibble byte) bool

func NibblesToBytes

func NibblesToBytes(ns []Nibble) []byte

func PrefixMatchedLen

func PrefixMatchedLen(n []Nibble, b []Nibble) (matched int)

func Serialize

func Serialize(n Node) []byte
Example
key := []byte{0xa7, 0x11, 0x35, 0x51}
value := []byte("45")
key2 := []byte{0xa7, 0x7d, 0x33, 0x71}
value2 := []byte("1")
key3 := []byte{0xa7, 0xf9, 0x36, 0x51}
value3 := []byte("2")
key4 := []byte{0xa7, 0x7d, 0x39, 0x71}
value4 := []byte("3")
trie := NewTrie()
_ = trie.Put(key, value)
_ = trie.Put(key2, value2)
_ = trie.Put(key3, value3)
_ = trie.Put(key4, value4)
i := Serialize(trie.Root)
var e []interface{}
err := rlp.DecodeBytes(i, &e)
if err != nil {
	log.Panic("Failed to DecodeBytes:", err)
}
trie2 := NewTrieWithDecodeData(e)
v, _ := trie2.Get(key)
fmt.Println(v)
Output:

[52 53]

Types

type BranchNode

type BranchNode struct {
	Branches [16]Node
	Value    []byte
}

func NewBranchNode

func NewBranchNode() *BranchNode

func NewBranchNodeWithDecodeData

func NewBranchNodeWithDecodeData(is interface{}) *BranchNode

func (*BranchNode) HasValue

func (b *BranchNode) HasValue() bool

func (*BranchNode) Hash

func (b *BranchNode) Hash() []byte

func (*BranchNode) Raw

func (b *BranchNode) Raw() []interface{}

func (*BranchNode) Serialize

func (b *BranchNode) Serialize() []byte

func (*BranchNode) SetBranch

func (b *BranchNode) SetBranch(nb Nibble, n Node)

func (*BranchNode) SetValue

func (b *BranchNode) SetValue(v []byte)

type ExtensionNode

type ExtensionNode struct {
	Shared []Nibble
	Next   Node
}

func NewExtensionNode

func NewExtensionNode(nibbles []Nibble, next Node) *ExtensionNode

func NewExtensionNodeWithDecodeData

func NewExtensionNodeWithDecodeData(ns []Nibble, is interface{}) *ExtensionNode

func (*ExtensionNode) Hash

func (e *ExtensionNode) Hash() []byte

func (*ExtensionNode) Raw

func (e *ExtensionNode) Raw() []interface{}

func (*ExtensionNode) Serialize

func (e *ExtensionNode) Serialize() []byte

type LeafNode

type LeafNode struct {
	Suffix []Nibble
	Value  []byte
}

func NewLeafNode

func NewLeafNode(nibbles []Nibble, value []byte) *LeafNode

func NewLeafNodeWithDecodeData

func NewLeafNodeWithDecodeData(ns []Nibble, is interface{}) *LeafNode

func (*LeafNode) Hash

func (l *LeafNode) Hash() []byte

func (*LeafNode) Raw

func (l *LeafNode) Raw() []interface{}

func (*LeafNode) Serialize

func (l *LeafNode) Serialize() []byte

type Nibble

type Nibble byte

func AddPrefixedByIsLeafNode

func AddPrefixedByIsLeafNode(ns []Nibble, isLeafNode bool) []Nibble

func ByteToNibbles

func ByteToNibbles(b byte) []Nibble

func BytesToNibbles

func BytesToNibbles(bs []byte) []Nibble

type Node

type Node interface {
	Hash() []byte
	Raw() []interface{}
}

func IsToNode

func IsToNode(is []interface{}) Node

type Trie

type Trie struct {
	Root Node
}

func NewTrie

func NewTrie() *Trie

func NewTrieWithDecodeData

func NewTrieWithDecodeData(is []interface{}) *Trie

func (*Trie) Get

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

func (*Trie) Hash

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

func (*Trie) Put

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

Put inserts a key -> value in the merkle tree EmptyNode -> replace with a leaf node with the path LeafNode -> transform into a Extension Node add a new branch node and a new leaf node ExtensionNode -> convert to a Extension Node with a shorter path, create a branch node that points to a new Extension Node

func (*Trie) Update

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

Jump to

Keyboard shortcuts

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