mmr

package
v0.0.0-...-e976260 Latest Latest
Warning

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

Go to latest
Published: May 5, 2022 License: MIT Imports: 8 Imported by: 5

Documentation

Overview

Package mmr is responsible for calculating the merkle mountain range root, proof and the verification

Index

Constants

This section is empty.

Variables

View Source
var ErrCorruptedProof = errors.New("corrupted proof: proof items is not enough to build a tree")

ErrCorruptedProof is of the type error. It is returned when proof is considered corrupt

View Source
var ErrGenProofForInvalidLeaves = errors.New("leaves is an empty list, or beyond the mmr range")

ErrGenProofForInvalidLeaves is of the type error. It is returned when the list of leaves is empty or beyond mmr range

View Source
var ErrGetRootOnEmpty = errors.New("get root on an empty MMR")

ErrGetRootOnEmpty is of the type error. It is returned when the MMR is empty

View Source
var ErrInconsistentStore = errors.New("inconsistent store")

ErrInconsistentStore is of the type error. It is returned when the store is considered inconsistent

Functions

func GetPeaks

func GetPeaks(mmrSize uint64) (positions []uint64)

GetPeaks returns the positions of the peaks of the MMR using the MMR size.

  1. It starts by finding the leftmost peak.
  2. It then finds the next peak (right peak) by moving to the right sibling. If that node isn't in the MMR (which it won't), it take its left child. If that child is not in the MMR either, it keeps taking its left child until it finds a node that exists in the MMR.
  3. The process is repeated until it is at the last node.

func LeafIndexToMMRSize

func LeafIndexToMMRSize(index uint64) uint64

LeafIndexToMMRSize returns the mmr size of an mmr tree provided the leaf index passed as an argument is the last leaf in the tree.

func LeafIndexToPos

func LeafIndexToPos(index uint64) uint64

LeafIndexToPos returns the position of a leaf from its index.

func PosHeightInTree

func PosHeightInTree(pos uint64) uint32

PosHeightInTree calculates and returns the height of a node in the tree using its position.

func UseLogger

func UseLogger(l logger)

UseLogger takes any type that satisfies the logger interface as a argument. It the sets the global log variable to the the value passed as an argument.

Types

type Batch

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

Batch contains the a slice of Batch elements and a Store

func NewBatch

func NewBatch(store Store) *Batch

NewBatch returns an object of the Batch type

func (*Batch) GetElem

func (b *Batch) GetElem(pos uint64) []byte

GetElem returns an element in a store implementation using its position.

type BatchElem

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

BatchElem holds the fields of data for a Batch Element

type Iterator

type Iterator struct {
	Items [][]byte
	// contains filtered or unexported fields
}

Iterator is a wrapper for a slice of bytes. It exposes helper methods for accessing about the slice and storing data to it

func NewIterator

func NewIterator() *Iterator

NewIterator creates a new object of the Iterator

func (*Iterator) Next

func (i *Iterator) Next() []byte

Next returns the next item from the slice of items and increases the index. It returns nil when the last item in the slice has already been returned.

type MMR

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

MMR contains fields for computing the MMR tree.

func NewMMR

func NewMMR(mmrSize uint64, s Store, leaves []types.Leaf, m types.Hasher) *MMR

NewMMR returns a new MMR type. It takes four arguments. It takes the mmrSize, Store, leaves and Hasher interfaces. It accepts any type that satisfies both the Store and Hasher interfaces. The parameter 'leaves' are leaves whose proof of inclusion would be verified.

func (*MMR) Commit

func (m *MMR) Commit()

Commit calls the commit method on the batch property. It adds a batch element to the store

func (*MMR) GenProof

func (m *MMR) GenProof(posList []uint64) (*Proof, error)

GenProof generates merkle proof for positions. It sorts positions, pushes merkle proof to proof by peak from left to right. It then pushes bagged right hand side root

func (*MMR) IsEmpty

func (m *MMR) IsEmpty() bool

IsEmpty returns true if the MMR is empty and false if it is not.

func (*MMR) MMRSize

func (m *MMR) MMRSize() uint64

MMRSize returns the size of the mmr tree

func (*MMR) Push

func (m *MMR) Push(elem []byte) (uint64, error)

Push adds an element to the store and returns its position

func (*MMR) Root

func (m *MMR) Root() ([]byte, error)

Root returns the root of the MMR tree

func (*MMR) RootHex

func (m *MMR) RootHex() (string, error)

RootHex returns a hex encoded string instead of

type MemStore

type MemStore map[uint64][]byte

MemStore is a map of bytes with uint64 values as its key

func NewMemStore

func NewMemStore() MemStore

NewMemStore creates an returns a map of the MemStore type

func (MemStore) GetElem

func (m MemStore) GetElem(pos uint64) []byte

type Proof

type Proof struct {
	Hasher types.Hasher
	Leaves []types.Leaf
	// contains filtered or unexported fields
}

Proof is the mmr proof. It is constructed to verify an MMR leaf.

func NewProof

func NewProof(mmrSize uint64, proofItems [][]byte, mmrLeaves []types.Leaf, m types.Hasher) *Proof

NewProof creates and returns new Proof. It takes the mmrSize, proof which is of type *Iterator and any type that satisfies the Merge interface.

func (*Proof) CalculateRoot

func (m *Proof) CalculateRoot() ([]byte, error)

CalculateRoot calculates and returns the root of the MMR tree using the leaves, mmrSize and proofs. It sorts the leaves by position, calculates the root of each peak and bags the peaks

func (*Proof) CalculateRootWithNewLeaf

func (m *Proof) CalculateRootWithNewLeaf(leaves []types.Leaf, newIndex uint64, newElem []byte, newMMRSize uint64) ([]byte, error)

CalculateRootWithNewLeaf calculates and returns a new root provided a new leaf element, new position and new MMRsize. from merkle proof of leaf n to calculate merkle root of n + 1 leaves. By observing the MMR construction graph we know it is possible. https://github.com/jjyr/merkle-mountain-range#construct this is kinda tricky, but it works, and useful

func (*Proof) LeavesToVerify

func (m *Proof) LeavesToVerify(leaves []types.Leaf)

LeavesToVerify sets leaves to the leaves property in Proof

func (*Proof) MMRSize

func (m *Proof) MMRSize() uint64

MMRSize returns the mmr size

func (*Proof) ProofItems

func (m *Proof) ProofItems() [][]byte

ProofItems returns all the proof items from the Iterator.

func (*Proof) Verify

func (m *Proof) Verify(root []byte) bool

Verify takes a root and leaves as arguments. It calculates a root from the leaves using the CalculateRoot method and compares it with the supplied root. It returns tree if the roots are equal and false if they are not.

type Store

type Store interface {
	GetElem(pos uint64) []byte
	// contains filtered or unexported methods
}

Store defines the required method on any store passed to the Batch struct

Jump to

Keyboard shortcuts

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