Documentation ¶
Overview ¶
Package merkle is a fixed merkle tree implementation
Example (Complete) ¶
items := [][]byte{[]byte("alpha"), []byte("beta"), []byte("gamma"), []byte("delta"), []byte("epsilon")} treeOptions := TreeOptions{ EnableHashSorting: false, DisableHashLeaves: false, } tree := NewTreeWithOpts(treeOptions) err := tree.Generate(items, md5.New()) 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 ¶
- func CalculateHeightAndNodeCount(leaves uint64) (height, nodeCount uint64)
- type Node
- type Tree
- func (tree *Tree) Generate(blocks [][]byte, hashf hash.Hash) error
- func (tree *Tree) GetNodesAtHeight(h uint64) []*Node
- func (tree *Tree) GetPathToRootFromNode(node *Node) ([][]byte, error)
- func (tree *Tree) GetProofForLeafHash(leafHash []byte) ([][]byte, error)
- func (tree *Tree) Height() uint64
- func (tree *Tree) Leaves() []*Node
- func (tree *Tree) Root() *Node
- type TreeOptions
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CalculateHeightAndNodeCount ¶
CalculateHeightAndNodeCount returns the height and number of nodes in an unbalanced binary tree given number of leaves
Types ¶
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 }
Tree contains all nodes
func NewTreeWithOpts ¶
func NewTreeWithOpts(options TreeOptions) Tree
NewTreeWithOpts creates a Tree with custom options
func (*Tree) GetNodesAtHeight ¶
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) GetPathToRootFromNode ¶
func (*Tree) GetProofForLeafHash ¶
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
Click to show internal directories.
Click to hide internal directories.