csmt

package
v0.1.0-alpha.8 Latest Latest
Warning

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

Go to latest
Published: Nov 26, 2020 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// FlagSingle designates that this node is a single node.
	FlagSingle = iota

	// FlagLeft designates that this node has a left branch.
	FlagLeft

	// FlagRight designates that this node has a right branch.
	FlagRight

	// FlagBoth designates that this node has both a left and right branch.
	FlagBoth
)

Variables

This section is empty.

Functions

func ApplyWitness

func ApplyWitness(uw primitives.UpdateWitness, oldStateRoot chainhash.Hash) (*chainhash.Hash, error)

ApplyWitness applies a witness to an old state root to generate a new state root.

func CalculateRoot

func CalculateRoot(key chainhash.Hash, value chainhash.Hash, witnessBitfield chainhash.Hash, witnesses []chainhash.Hash, lastLevel uint8) (*chainhash.Hash, error)

CalculateRoot calculates the root of the tree with the given witness information.

func CalculateSubtreeHashWithOneLeaf

func CalculateSubtreeHashWithOneLeaf(key *chainhash.Hash, value *chainhash.Hash, atLevel uint8) chainhash.Hash

CalculateSubtreeHashWithOneLeaf calculates the hash of a subtree with only a single leaf at a certain height. atLevel is the height to calculate at.

func CheckWitness

func CheckWitness(vw *primitives.VerificationWitness, oldStateRoot chainhash.Hash) bool

CheckWitness ensures the state root matches.

func GenerateUpdateWitness

func GenerateUpdateWitness(tree TreeDatabaseTransaction, key chainhash.Hash, value chainhash.Hash) (*primitives.UpdateWitness, error)

GenerateUpdateWitness generates a witness that allows calculation of a new state root.

func GenerateVerificationWitness

func GenerateVerificationWitness(tree TreeDatabaseTransaction, key chainhash.Hash) (*primitives.VerificationWitness, error)

GenerateVerificationWitness generates a witness that allows verification of a key in the tree.

Types

type InMemoryTreeDB

type InMemoryTreeDB struct {
	// contains filtered or unexported fields
}

InMemoryTreeDB is a transaction based in memory.

func NewInMemoryTreeDB

func NewInMemoryTreeDB() *InMemoryTreeDB

NewInMemoryTreeDB creates a new in-memory tree database.

func (*InMemoryTreeDB) Hash

func (t *InMemoryTreeDB) Hash() (*chainhash.Hash, error)

Hash gets the root hash of the tree.

func (*InMemoryTreeDB) Nodes

func (t *InMemoryTreeDB) Nodes() map[chainhash.Hash]Node

func (*InMemoryTreeDB) Update

func (t *InMemoryTreeDB) Update(cb func(TreeDatabaseTransaction) error) error

Update runs cb with an update transaction.

func (*InMemoryTreeDB) View

View runs cb with a view only transaction.

type InMemoryTreeTX

type InMemoryTreeTX struct {
	// contains filtered or unexported fields
}

InMemoryTreeTX is a tree stored in memory.

func (*InMemoryTreeTX) DeleteNode

func (i *InMemoryTreeTX) DeleteNode(h chainhash.Hash) error

DeleteNode deletes a node if it exists.

func (*InMemoryTreeTX) Get

Get gets a value from the key-value store.

func (*InMemoryTreeTX) GetNode

func (i *InMemoryTreeTX) GetNode(nodeHash chainhash.Hash) (*Node, error)

GetNode gets a node from the tree database.

func (*InMemoryTreeTX) Hash

func (i *InMemoryTreeTX) Hash() (*chainhash.Hash, error)

Hash gets the hash of the tree transaction.

func (*InMemoryTreeTX) NewNode

func (i *InMemoryTreeTX) NewNode(left *Node, right *Node, subtreeHash chainhash.Hash) (*Node, error)

NewNode creates a new empty node.

func (*InMemoryTreeTX) NewSingleNode

func (i *InMemoryTreeTX) NewSingleNode(key chainhash.Hash, value chainhash.Hash, subtreeHash chainhash.Hash) (*Node, error)

NewSingleNode creates a new node with only one key-value pair.

func (*InMemoryTreeTX) Root

func (i *InMemoryTreeTX) Root() (*Node, error)

Root gets the root of the tree.

func (*InMemoryTreeTX) Set

Set sets a value in the key-value store.

func (*InMemoryTreeTX) SetNode

func (i *InMemoryTreeTX) SetNode(n *Node) error

SetNode sets a node in the database. The node passed MUST be an Node

func (*InMemoryTreeTX) SetRoot

func (i *InMemoryTreeTX) SetRoot(n *Node) error

SetRoot sets the root of the tree.

type Node

type Node struct {
	// contains filtered or unexported fields
}

Node is a node of the in-memory tree database.

func NewNode

func NewNode(value chainhash.Hash, oneKey, oneValue, left, right *chainhash.Hash, one bool) Node

func (*Node) Empty

func (i *Node) Empty() bool

Empty checks if the node is empty.

func (*Node) GetHash

func (i *Node) GetHash() chainhash.Hash

GetHash gets the current hash from memory.

func (*Node) GetSingleKey

func (i *Node) GetSingleKey() chainhash.Hash

GetSingleKey gets the only key in the subtree.

func (*Node) GetSingleValue

func (i *Node) GetSingleValue() chainhash.Hash

GetSingleValue gets the only value in the subtree.

func (*Node) IsSingle

func (i *Node) IsSingle() bool

IsSingle checks if there is only a single key in the subtree.

func (*Node) Left

func (i *Node) Left() *chainhash.Hash

Left gets the left node in memory.

func (*Node) Marshal

func (i *Node) Marshal() []byte

Marshal gets the node as a byte representation.

func (*Node) Right

func (i *Node) Right() *chainhash.Hash

Right gets the right node in memory.

func (*Node) Unmarshal

func (i *Node) Unmarshal(b []byte) error

Unmarshal deserializes a node from disk.

type Tree

type Tree struct {
	// contains filtered or unexported fields
}

Tree represents a state tree.

func NewTree

func NewTree(d TreeDatabase) Tree

NewTree creates a MemoryTree

func (*Tree) Hash

func (t *Tree) Hash() (chainhash.Hash, error)

Hash gets the hash of the tree.

func (*Tree) Update

func (t *Tree) Update(cb func(access TreeTransactionAccess) error) error

Update creates a transaction for the tree.

func (*Tree) View

func (t *Tree) View(cb func(access TreeTransactionAccess) error) error

View creates a view-only transaction for the tree.

type TreeDatabase

type TreeDatabase interface {
	Update(func(TreeDatabaseTransaction) error) error

	View(func(TreeDatabaseTransaction) error) error

	Hash() (*chainhash.Hash, error)
}

TreeDatabase provides functions to update or view parts of the tree.

type TreeDatabaseTransaction

type TreeDatabaseTransaction interface {
	// Root gets the root node.
	Root() (*Node, error)

	// Hash gets the hash of the root node.
	Hash() (*chainhash.Hash, error)

	// SetRoot sets the root node.
	SetRoot(*Node) error

	// NewNode creates a new node, adds it to the tree database, and returns it.
	NewNode(left *Node, right *Node, subtreeHash chainhash.Hash) (*Node, error)

	// NewSingleNode creates a new node that represents a subtree with only a single key.
	NewSingleNode(key chainhash.Hash, value chainhash.Hash, subtreeHash chainhash.Hash) (*Node, error)

	// GetNode gets a node from the database.
	GetNode(chainhash.Hash) (*Node, error)

	// SetNode sets a node in the database.
	SetNode(*Node) error

	// DeleteNode deletes a node from the database.
	DeleteNode(chainhash.Hash) error

	// Get a value from the kv store.
	Get(chainhash.Hash) (*chainhash.Hash, error)

	// Set a value in the kv store.
	Set(chainhash.Hash, chainhash.Hash) error
}

TreeDatabaseTransaction is a transaction interface to access or update the tree.

type TreeMemoryCache

type TreeMemoryCache struct {
	// contains filtered or unexported fields
}

TreeMemoryCache is a cache that allows

func NewTreeMemoryCache

func NewTreeMemoryCache(underlyingDatabase TreeDatabase) (*TreeMemoryCache, error)

NewTreeMemoryCache constructs a tree memory cache that can be committed based on an underlying tree (probably on disk).

func (*TreeMemoryCache) Flush

func (t *TreeMemoryCache) Flush() error

Flush flushes the transaction to the underlying tree.

func (*TreeMemoryCache) GetUnderlying

func (t *TreeMemoryCache) GetUnderlying() TreeDatabase

GetUnderlying gets the underlying store.

func (*TreeMemoryCache) Hash

func (t *TreeMemoryCache) Hash() (*chainhash.Hash, error)

Hash gets the hash of the current tree.

func (*TreeMemoryCache) Update

func (t *TreeMemoryCache) Update(cb func(TreeDatabaseTransaction) error) error

Update creates an update transaction for the cache.

func (*TreeMemoryCache) UpdateUnderlying

func (t *TreeMemoryCache) UpdateUnderlying(cache TreeDatabase) error

UpdateUnderlying updates the underlying store.

func (*TreeMemoryCache) View

View creates a view transaction for the cache.

type TreeMemoryCacheTransaction

type TreeMemoryCacheTransaction struct {
	// contains filtered or unexported fields
}

TreeMemoryCacheTransaction is a transaction wrapper on a CSMT that allows reading and writing from an underlying data store.

func (*TreeMemoryCacheTransaction) DeleteNode

DeleteNode deletes a node from the transaction.

func (*TreeMemoryCacheTransaction) Get

Get gets a value from the transaction.

func (*TreeMemoryCacheTransaction) GetNode

GetNode gets a node based on the subtree hash.

func (*TreeMemoryCacheTransaction) Hash

Hash gets the root hash of the tree transaction cache.

func (*TreeMemoryCacheTransaction) NewNode

func (t *TreeMemoryCacheTransaction) NewNode(left *Node, right *Node, subtreeHash chainhash.Hash) (*Node, error)

NewNode creates a new node and adds it to the cache.

func (*TreeMemoryCacheTransaction) NewSingleNode

func (t *TreeMemoryCacheTransaction) NewSingleNode(key chainhash.Hash, value chainhash.Hash, subtreeHash chainhash.Hash) (*Node, error)

NewSingleNode creates a new node with only a single KV-pair in the subtree.

func (*TreeMemoryCacheTransaction) Root

func (t *TreeMemoryCacheTransaction) Root() (*Node, error)

Root gets the current root of the transaction.

func (*TreeMemoryCacheTransaction) Set

Set sets a value in the transaction.

func (*TreeMemoryCacheTransaction) SetNode

func (t *TreeMemoryCacheTransaction) SetNode(n *Node) error

SetNode sets a node in the transaction.

func (*TreeMemoryCacheTransaction) SetRoot

func (t *TreeMemoryCacheTransaction) SetRoot(n *Node) error

SetRoot sets the root for the current transaction. If it does not exist in the cache or the underlying data store, add it to the cache.

type TreeTransaction

type TreeTransaction struct {
	// contains filtered or unexported fields
}

TreeTransaction is a wr

func (*TreeTransaction) Get

Get gets a value from the tree.

func (*TreeTransaction) Hash

func (t *TreeTransaction) Hash() (*chainhash.Hash, error)

Hash get the root hash

func (*TreeTransaction) Prove

Prove proves a key in the tree.

func (*TreeTransaction) Set

func (t *TreeTransaction) Set(key chainhash.Hash, value chainhash.Hash) error

Set inserts/updates a value

func (*TreeTransaction) SetWithWitness

func (t *TreeTransaction) SetWithWitness(key chainhash.Hash, value chainhash.Hash) (*primitives.UpdateWitness, error)

SetWithWitness returns an update witness and sets the value in the tree.

type TreeTransactionAccess

type TreeTransactionAccess interface {
	Hash() (*chainhash.Hash, error)
	Set(key chainhash.Hash, value chainhash.Hash) error
	Get(key chainhash.Hash) (*chainhash.Hash, error)
}

TreeTransactionAccess represents basic access to the state tree.

Jump to

Keyboard shortcuts

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