merkle

package
v0.0.0-...-169a2f2 Latest Latest
Warning

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

Go to latest
Published: Jul 10, 2021 License: GPL-3.0 Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	Hash            crypto.Hash
	BranchFactor    uint8
	ConcurrentLimit int
}

type Group

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

Group is a Group of child nodes under the same parent node

func NewGroup

func NewGroup(h crypto.Hash, tc *TreeCalc, store Store, pPos *Position) *Group

NewGroup creates a new Group

func (*Group) IsEmpty

func (b *Group) IsEmpty() bool

IsEmpty checks whether all the child nodes are nil

func (*Group) Load

func (b *Group) Load(rowSize *big.Int) *Group

Load loads the child nodes from the store

func (*Group) MakeParent

func (b *Group) MakeParent() *Node

MakeParent compute the sum of the child nodes and returns the parent node

func (*Group) SetNode

func (b *Group) SetNode(n *Node) *Group

SetNode sets the node at the corresponding index in the block

func (*Group) Sum

func (b *Group) Sum() []byte

Sum sums the child nodes

type MapStore

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

MapStore is simple Store implementation

func NewMapStore

func NewMapStore() *MapStore

NewMapStore create a new MapStore

func (*MapStore) CommitUpdate

func (ms *MapStore) CommitUpdate(res *UpdateResult)

CommitUpdate commits tree node updates

func (*MapStore) GetHeight

func (ms *MapStore) GetHeight() uint8

GetHeight implement Store

func (*MapStore) GetLeafCount

func (ms *MapStore) GetLeafCount() *big.Int

GetLeafCount implement Store

func (*MapStore) GetNode

func (ms *MapStore) GetNode(p *Position) []byte

GetNode implement Store

type Node

type Node struct {
	Position *Position
	Data     []byte
}

Node type

type Position

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

Position of a node in the tree

func NewPosition

func NewPosition(level uint8, index *big.Int) *Position

NewPosition create a new position

func UnmarshalPosition

func UnmarshalPosition(b []byte) *Position

UnmarshalPosition unmarshals position from raw bytes

func (*Position) Bytes

func (p *Position) Bytes() []byte

Bytes returns the serialized bytes of position

func (*Position) Index

func (p *Position) Index() *big.Int

Index gives the index of position NOTE: the value of index must not be changed

func (*Position) Level

func (p *Position) Level() uint8

Level gives the level of position

func (*Position) String

func (p *Position) String() string

type Positions

type Positions []*Position

Positions slice

func (Positions) UniqueMap

func (ps Positions) UniqueMap() map[string]*Position

UniqueMap merges the same positions

type Store

type Store interface {
	GetLeafCount() *big.Int
	GetHeight() uint8
	GetNode(p *Position) []byte
}

Store is merkle tree store

type Tree

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

Tree implements a merkle tree engine

func NewTree

func NewTree(store Store, config Config) *Tree

NewTree creates a new Merkle Tree

func (*Tree) Root

func (tree *Tree) Root() *Node

Root returns the root node of the tree

func (*Tree) Update

func (tree *Tree) Update(leaves []*Node, newLeafCount *big.Int) *UpdateResult

Update accepts new/modified tree leaves, recompute the corresponding nodes until root node.

func (*Tree) Verify

func (tree *Tree) Verify(leaves []*Node) bool

Verify verifies leaves with the current root-node.

type TreeCalc

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

TreeCalc calculates merkle tree properties

func NewTreeCalc

func NewTreeCalc(bfactor uint8) *TreeCalc

NewTreeCalc creates a new NewTreeCalc

func (*TreeCalc) BranchFactor

func (tc *TreeCalc) BranchFactor() uint8

BranchFactor returns uint8 branch factor

func (TreeCalc) FirstNodeOfGroup

func (tc TreeCalc) FirstNodeOfGroup(blkIdx *big.Int) *big.Int

FirstNodeOfGroup gives the index of first node of a group

e.g branch factor 5 [0 1 2 3 4] [5 6 7 _ _] // first node of block 1 is 0 and of block 2 is 5

func (*TreeCalc) GroupCount

func (tc *TreeCalc) GroupCount(nodeCount *big.Int) *big.Int

GroupCount gives the number of groups for a tree level with given node count

e.g branch factor 5

[0           1] 		// 2 parent nodes

[0 1 2 3 4] [5 6 7 _ _] // 8 nodes becomes 2 blocks

func (*TreeCalc) GroupOfNode

func (tc *TreeCalc) GroupOfNode(nodeIdx *big.Int) *big.Int

GroupOfNode gives the group index in which the node exist

e.g branch factor 5 [0 1 2 3 4] [5 6 7 _ _] // group of node 1 is 0 and of node 7 is 2

func (*TreeCalc) Height

func (tc *TreeCalc) Height(nleaf *big.Int) uint8

Height gives the height of the tree based on number of leaves

func (*TreeCalc) NodeIndexInGroup

func (tc *TreeCalc) NodeIndexInGroup(nodeIdx *big.Int) int

NodeIndexInGroup gives the index of node in the corresponding group

e.g branch factor 5 [0 1 2 3 4] [5 6 7 _ _] // indexInGroup of node 2 is 2 and of node 6 is 1

type UpdateResult

type UpdateResult struct {
	LeafCount *big.Int
	Height    uint8
	Leaves    []*Node
	Branches  []*Node
	Root      *Node
}

UpdateResult type

Jump to

Keyboard shortcuts

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