mmr

package
v0.3.8 Latest Latest
Warning

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

Go to latest
Published: Jul 6, 2021 License: ISC Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func LeafIndex

func LeafIndex(value uint64) *leafIndex

func MemoryDb

func MemoryDb() (res *blockDb)

func NodeIndex

func NodeIndex(value uint64) (res *nodeIndex)

Types

type BlockData

type BlockData struct {
	Weight *big.Int
	Hash   Hash
}

func (*BlockData) Digest

func (d *BlockData) Digest(hasher Hasher) (result Hash)

Digest calculates Block Digest

type Hash

type Hash [32]byte

func (*Hash) String

func (h *Hash) String() string

type Hasher

type Hasher func() hash.Hash

type IBlockIndex

type IBlockIndex interface {
	// GetLeftBranch returns left hand branch from current
	GetLeftBranch() IBlockIndex

	// GetTop returns ...
	GetTop() IBlockIndex

	// RightUp returns upper block navigating by right mountain side
	// If current block is left side - return nli
	RightUp() IBlockIndex

	// IsRight checks if current block is right side sibling
	IsRight() bool

	// GetSibling returns sibling
	// If current brunch is left - return right sibling
	// If current brunch is right - return left sibling
	GetSibling() IBlockIndex

	// GetHeight returns height of this block
	GetHeight() uint64

	// Index returns index of the block
	// if it's a leaf - returns leaf index
	// if it's a node - returns node index
	Index() uint64

	// Value returns Block data
	Value(mmr *ShardsMergedMiningTree) (*BlockData, bool)

	// SetValue Set block value
	SetValue(mmr *ShardsMergedMiningTree, data *BlockData) error
}

IBlockIndex

MMR Blocks navigation abstraction
Might be two types of MMR block
 - Leafs. It's a bottom layer, representing block data
 - Nodes. It's a Mountain nodes data, representing aggregation of bottom layers

type IShardsMergedMiningTree

type IShardsMergedMiningTree interface {
	Index() int
	Root() (root Hash, err error)
	Set(index uint64, weight *big.Int, hash []byte) (root Hash, err error)
	Append(weight *big.Int, hash []byte) (root Hash, err error)
	Copy(db IStore) (IShardsMergedMiningTree, error)
}

IShardsMergedMiningTree ...

type IStore

type IStore interface {
	GetNode(index uint64) (res *BlockData, err error)
	SetNode(index uint64, data *BlockData) error
	GetBlock(index uint64) (res *BlockData, err error)
	SetBlock(index uint64, data *BlockData) error
	Nodes() (res []uint64, err error)
	Blocks() (res []uint64, err error)
	Debug()
}

func Storage

func Storage(db database.DB) IStore

type MmrProof

type MmrProof struct {
	Nodes map[uint64]*BlockData
}

type ShardsMergedMiningTree

type ShardsMergedMiningTree struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

func MergedMiningTree

func MergedMiningTree(db IStore, genesis []byte) *ShardsMergedMiningTree

func (*ShardsMergedMiningTree) Append

func (mmTree *ShardsMergedMiningTree) Append(weight *big.Int, hash []byte) (root Hash, err error)

func (*ShardsMergedMiningTree) Copy

func (*ShardsMergedMiningTree) GetProofs

func (mmTree *ShardsMergedMiningTree) GetProofs(length uint64, indexes ...uint64) (result *MmrProof, err error)

func (*ShardsMergedMiningTree) GetRoot

func (mmTree *ShardsMergedMiningTree) GetRoot(length uint64) (root Hash)

GetRoot returns MMR Root hash Algorithm:

  1. Take peaks
  2. Aggregate starting from end to start

func (*ShardsMergedMiningTree) Index

func (mmTree *ShardsMergedMiningTree) Index() int

func (*ShardsMergedMiningTree) MmrFromProofs

func (mmTree *ShardsMergedMiningTree) MmrFromProofs(db IStore, proof MmrProof) (*ShardsMergedMiningTree, error)

func (*ShardsMergedMiningTree) Proof

func (mmTree *ShardsMergedMiningTree) Proof(index uint64, length uint64) (result *ShardsMergedMiningTree, err error)

Proof build an MMR Proof by MMR length and index Algorithm:

  1. Get current block.
  2. If left - go to Right. Take it
  3. Go up. If block exists - take it
  4. Go to Step 2

func (*ShardsMergedMiningTree) Proofs

func (mmTree *ShardsMergedMiningTree) Proofs(length uint64, indexes ...uint64) (result *ShardsMergedMiningTree, err error)

Proofs build an MMR Proof by MMR length and indexes

func (*ShardsMergedMiningTree) Root

func (mmTree *ShardsMergedMiningTree) Root() (root Hash, err error)

func (*ShardsMergedMiningTree) Set

func (mmTree *ShardsMergedMiningTree) Set(index uint64, weight *big.Int, hash []byte) (Hash, error)

Set appends ShardsMergedMiningTree with the block data by index

func (*ShardsMergedMiningTree) ValidateProof

func (mmTree *ShardsMergedMiningTree) ValidateProof(index, length uint64, fullMmr *ShardsMergedMiningTree) (ok bool)

ValidateProof validates MMR proof

Jump to

Keyboard shortcuts

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