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 ¶
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 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) 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. 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.