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))
Index ¶
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
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
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. Otherwise the default
// behavior is to simply forward odd nodes to the next layer.
DoubleOddNodes bool
}
TreeOptions configures tree behavior
Click to show internal directories.
Click to hide internal directories.