pmt

package
v0.0.24 Latest Latest
Warning

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

Go to latest
Published: Sep 11, 2021 License: MIT Imports: 6 Imported by: 1

Documentation

Index

Constants

View Source
const (
	TNil       = iota + 1 // When persisting, this is the type for nils
	TNode                 // Type for Nodes
	TValue                // Type for values
	TNotLoaded            // When transisioning into a new Byte Block, the NotLoaded indicates a need to load from disk
)

Variables

This section is empty.

Functions

func GetBBKey

func GetBBKey(BIdx byte, key [32]byte) (BBKey [32]byte)

func GetHash

func GetHash(e Entry) []byte

GetHash Makes the code just a bit more simple. Checks for nils

Types

type BPT

type BPT struct {
	Root      *Node            // The root of the Patricia Tree, holding the summary hash for the Patricia Tree
	DirtyMap  map[uint64]*Node // Map of dirty nodes.
	MaxHeight int              // Highest height of any node in the BPT
	MaxNodeID uint64           // Maximum node id assigned to any node
	// contains filtered or unexported fields
}

BPT Binary Patricia Tree. Two types of Entry in the Tree:

Node - a node in a binary tree that ends in Values (Left and Right)
Value - a key / value pair where the key is a ChainID and the value
        is the hash of the state of the chain

The BPT can be updated many times, then updated in batch (which reduces the hashes that have to be performed to update the summary hash)

func NewBPT

func NewBPT() *BPT

New BPT Allocate a new BPT and set up the structures required to get to work with Binary Patricia Trees.

func (*BPT) Clean

func (b *BPT) Clean(node *Node)

Clean Take a node out of the dirty tracking. Don't care if it is or isn't dirty

func (*BPT) Dirty

func (b *BPT) Dirty(node *Node)

Dirty Add a node to the dirty tracking

func (*BPT) Equal

func (b *BPT) Equal(b2 *BPT) (equal bool)

Equal Used to do some testing

func (*BPT) GetDirtyList

func (b *BPT) GetDirtyList() (list []*Node)

GetDirtyList Convert the map to a list (must work down from the highest heights to the root (to keep from stomping on hashing orders; all hashes at the same height are independent of each other, but must be computed before we handle the next lowest height, and so forth.

func (*BPT) Insert

func (b *BPT) Insert(key, hash [32]byte)

Insert Starts the search of the BPT for the location of the key in the BPT

func (*BPT) IsDirty

func (b *BPT) IsDirty(node *Node) bool

IsDirty Sort if a node is in the dirty tracking. Allows batching updates for greater efficiency.

func (*BPT) Marshal

func (b *BPT) Marshal() (data []byte)

Marshal Must have the MaxNodeID at the very least to be able to add nodes to the BPT

func (*BPT) MarshalByteBlock

func (b *BPT) MarshalByteBlock(borderNode *Node) (data []byte)

MarshalByteBlock Given the node leading into a byte block, marshal all the nodes within the block. A borderNode is a node that completes a byte boundry. So consider a theoretical key 03e706b93d2e515c6eff056ee481eb92f9e790277db91eb748b3cc5b46dfe8ca The first byte is 03, second is a7, third is 06 etc.

The node in block 03 that completes e7 is the board node. The Left path would begin the path to the theoretical key (a bit zero).

func (*BPT) MarshalEntry

func (b *BPT) MarshalEntry(entry Entry, data []byte) []byte

MarshalEntry Recursive routine that marshals a byte block starting from an entry on the Left or on the Right. Calling MarshalEntry from the Left only marshals half the node space of a byte block. Have to call MarshalEntry from the Right to complete coverage.

func (*BPT) NewNode

func (b *BPT) NewNode(parent *Node) (node *Node)

NewNode Allocate a new Node for use with this BPT. Note that various bookkeeping tasks are performed for the caller.

func (*BPT) NewValue

func (b *BPT) NewValue(key, hash [32]byte) (value *Value)

NewValue Allocate a new Value struct and do some bookkeeping for the user

func (*BPT) UnMarshal

func (b *BPT) UnMarshal(data []byte) (newData []byte)

UnMarshal Load the BPT in support of initialization from disk. Note that an existing BPT will be over written completely.

func (*BPT) UnMarshalByteBlock

func (b *BPT) UnMarshalByteBlock(borderNode *Node, data []byte) []byte

UnMarshalByteBlock

func (*BPT) UnMarshalEntry

func (b *BPT) UnMarshalEntry(parent *Node, data []byte) (Entry, []byte)

func (*BPT) Update

func (b *BPT) Update()

Update the Patricia Tree hashes with the values from the updates since the last update, and return the root hash

type Entry

type Entry interface {
	T() int                       // Returns the type of entry
	GetHash() []byte              // Returns the Hash for the entry
	Marshal() []byte              // Serialize the state of the Node or Value
	UnMarshal(data []byte) []byte // Unmarshal the state into the Node or Value
	Equal(entry Entry) bool       // Return Entry == entry
}

Entry We only have two node types, a Node that builds the Patricia Tree, and a Value that holds the values at the leaves.

type Manager

type Manager struct {
	DBManager *database.Manager
	Dirty     []*Node
	Bpt       *BPT
	LoadedBB  map[[32]byte]*Node
}

func NewBPTManager

func NewBPTManager(dbManager *database.Manager) *Manager

NewBPTManager Get a new BPTManager which keeps the BPT on disk. If the BPT is on disk, then it can be reloaded as needed.

func (*Manager) FlushNode

func (m *Manager) FlushNode(node *Node)

FlushNode Flushes the Byte Block to disk

func (*Manager) InsertKV

func (m *Manager) InsertKV(key, value [32]byte)

InsertKV Insert Key Value into Bpt

func (*Manager) LoadNode

func (m *Manager) LoadNode(node *Node) *Node

LoadNode Loads the nodes under the given node into the BPT

type Node

type Node struct {
	ID     uint64   // Node Count
	Height int      // Root is 0. above root is 1. Above above root is 2, etc.
	BBKey  [32]byte // Byte Block Key.
	Hash   [32]byte // This is the summary hash for the tree
	Left   Entry    // The hash of the child Left and up the tree, bit is zero
	Right  Entry    // the hash to the child Right and up the tree, bit is one
	Parent *Node    // the Parent node "below" this node
}

func (*Node) Equal

func (n *Node) Equal(entry Entry) (equal bool)

func (*Node) GetHash

func (n *Node) GetHash() []byte

GetHash Returns the Hash value for computing the summary hash of the BPT. By being in the interface, it does eliminate some book keeping where a node easily has L and R nodes, but doesn't know if those are Node or Value instances.

func (*Node) GetID

func (n *Node) GetID() uint64

GetID Returns the ID for this node. This is used to compare nodes and serve as a key in the BPT.DirtyMap.

func (*Node) Marshal

func (n *Node) Marshal() (data []byte)

Marshal Serialize the fields of the Node. Note this doesn't do too much towards fitting the node into the actual BPT, but it helps a bit.

See (p *BPT)MarshalByteBlock

func (*Node) T

func (n *Node) T() int

T Returns true if this node is really a Node, and false if the node is really a Value. We possibly want a way for Node to compress the distance to child nodes that have a long single path to leaves just because a key matches a number of bits before differentiating.

func (*Node) UnMarshal

func (n *Node) UnMarshal(data []byte) []byte

UnMarshal Deserialize the fields of the Node. See (p *BPT)UnMarshalByteBlock

type NotLoaded

type NotLoaded struct {
}

Node A node in our binary patricia/merkle tree

func (*NotLoaded) Equal

func (n *NotLoaded) Equal(entry Entry) bool

func (*NotLoaded) GetHash

func (n *NotLoaded) GetHash() []byte

func (*NotLoaded) Marshal

func (n *NotLoaded) Marshal() []byte

func (*NotLoaded) T

func (n *NotLoaded) T() int

func (*NotLoaded) UnMarshal

func (n *NotLoaded) UnMarshal(data []byte) []byte

type Value

type Value struct {
	Key  [32]byte // The key for the Patricia Tree value
	Hash [32]byte // The current value for the key
}

Value holds the key / hash mapping for the BPT. With Accumulate, the key represents a ChainID and a state Hash for a chain in the protocol

func (*Value) Equal

func (v *Value) Equal(entry Entry) (equal bool)

Equal Return true if a given Entry is a Value instance, and has the same Key and Hash as this Value

func (*Value) GetHash

func (v *Value) GetHash() []byte

GetHash Returns the combination hash of the Key and the Hash. This is the state that really must be proven to users

func (*Value) Marshal

func (v *Value) Marshal() []byte

Marshal Return the concatenation of the Key and Hash of the value

func (*Value) T

func (v *Value) T() int

Node Returns true if this is a Node, otherwise it is a value

func (*Value) UnMarshal

func (v *Value) UnMarshal(data []byte) []byte

UnMarshal Load the Value with the state marshalled to the given data slice

Jump to

Keyboard shortcuts

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