merkle

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Aug 2, 2021 License: LGPL-3.0 Imports: 5 Imported by: 0

Documentation

Overview

Example (Complete)
items := [][]byte{[]byte("alpha"), []byte("beta"), []byte("gamma"), []byte("delta"), []byte("epsilon")}

treeOptions := TreeOptions{
	EnableHashSorting: false,
	DisableHashLeaves: false,
}

tree := NewTree(md5.New, &treeOptions)
err := tree.Generate(items)
if err != nil {
	fmt.Println(err)
	return
}

fmt.Printf("Height: %d\n", tree.Height())
fmt.Printf("Root: %v\n", tree.Root())
fmt.Printf("N Leaves: %v\n", len(tree.Leaves()))
fmt.Printf("Height 2: %v\n", tree.GetNodesAtHeight(2))
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func CalculateHeightAndNodeCount

func CalculateHeightAndNodeCount(leaves uint64) (height, nodeCount uint64)

CalculateHeightAndNodeCount returns the height and number of nodes in an unbalanced binary tree given number of leaves

Types

type HashFunc

type HashFunc func() hash.Hash

HashFunc is a func that is used to set the hash for tree

type Node

type Node struct {
	Hash  []byte
	Left  *Node
	Right *Node
}

Node in the merkle tree

func (Node) String

func (n Node) String() string

Node implements the stringer interface for printing. It prints its hash and any nodes on the left or right if they exist.

type Tree

type Tree struct {
	// All nodes, linear
	Nodes []Node
	// Points to each level in the node. The first level contains the root node
	Levels [][]Node
	// Any particular behavior changing option
	Options TreeOptions

	Hash HashFunc
}

Tree contains all nodes

func NewTree

func NewTree(hashStrat HashFunc, options *TreeOptions) *Tree

NewTree creates a Tree. hashStrat must be given and is used when creating nodes. options object is optional and can be given as nil in order to use default options.

func (*Tree) Generate

func (tree *Tree) Generate(blocks [][]byte) error

Generate generates the tree nodes

func (*Tree) GetNodesAtHeight

func (tree *Tree) GetNodesAtHeight(h uint64) []Node

GetNodesAtHeight returns all nodes at a given height, where height 1 returns a 1-element slice containing the root node, and a height of tree.Height() returns the leaves

func (*Tree) GetProof

func (tree *Tree) GetProof(data []byte) ([][]byte, error)

GetProof produces proof for the given data.

func (*Tree) Height

func (tree *Tree) Height() uint64

Height returns the height of this tree

func (*Tree) Leaves

func (tree *Tree) Leaves() []Node

Leaves returns a slice of the leaf nodes in the tree, if available, else nil

func (*Tree) Root

func (tree *Tree) Root() *Node

Root returns the root node of the tree, if available, else nil

type TreeOptions

type TreeOptions struct {
	// EnableHashSorting modifies the tree's hash behavior to sort the hashes before concatenating them
	// to calculate the parent hash. This removes the capability of proving the position in the tree but
	// simplifies the proof format by removing the need to specify left/right.
	EnableHashSorting bool

	// DisableHashLeaves determines whether leaf nodes should be hashed or not. By doing disabling this behavior,
	// you can use a different hash function for leaves or generate a tree that contains already hashed
	// values. If this is disabled, a length of 32 bytes is enforced for all leaves.
	DisableHashLeaves bool

	// DoubleOddNodes repeats trailing nodes so they become their own
	// left/right pair and are hashed together. The default behavior is to
	// use a null hash as the missing pair.
	DoubleOddNodes bool
}

TreeOptions configures tree behavior

Jump to

Keyboard shortcuts

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