arnaucube
arnaucube/go-merkletree-oldPublic Self-hosted repository
go-merkletree-old/print.go
print.goRaw
 1package merkletree
 2
 3import (
 4	"fmt"
 5
 6	"github.com/fatih/color"
 7)
 8
 9func (mt *MerkleTree) printLevel(parent Hash, iLevel int, maxLevel int) {
10	for i := 0; i < iLevel; i++ {
11		fmt.Print("	")
12	}
13	fmt.Print("level ")
14	fmt.Print(iLevel)
15	fmt.Print(" - ")
16	fmt.Print("'" + parent.Hex() + "' = ")
17	nodeType, _, nodeBytes, err := mt.Get(parent)
18	if err != nil {
19		color.Red(err.Error())
20	}
21	var node treeNode
22	if nodeType == byte(normalNodeType) {
23		node = parseNodeBytes(nodeBytes)
24		color.Blue("'" + node.ChildL.Hex() + "' - '" + node.ChildR.Hex() + "'")
25	} else if nodeType == byte(valueNodeType) {
26		color.Green("value")
27	} else if nodeType == byte(finalNodeType) { //typ==FINAL_NODE
28		fmt.Print("[FinalTree]:")
29		color.Cyan("final tree node: " + HashBytes(nodeBytes).Hex())
30		_, _, leafNodeBytes, err := mt.Get(HashBytes(nodeBytes))
31		if err != nil {
32			color.Red(err.Error())
33		}
34		for i := 0; i < iLevel; i++ {
35			fmt.Print("	")
36		}
37		// color.Cyan("					leaf value: 0x" + hex.EncodeToString(leafNodeBytes))
38		color.Cyan("					leaf value: " + string(leafNodeBytes))
39	} else {
40		//EMPTY_NODE
41		fmt.Print("[EmptyBranch]:")
42		fmt.Println(EmptyNodeValue.Bytes())
43	}
44	iLevel++
45	if len(node.ChildR) > 0 && iLevel < maxLevel && nodeType != byte(EmptyNodeType) && nodeType != byte(finalNodeType) {
46		mt.printLevel(node.ChildL, iLevel, maxLevel)
47		mt.printLevel(node.ChildR, iLevel, maxLevel)
48	}
49}
50
51// PrintFullMT prints the tree in the terminal, all the levels with all the nodes
52func (mt *MerkleTree) PrintFullMT() {
53	mt.printLevel(mt.root, 0, mt.numLevels-1)
54	fmt.Print("root: ")
55	color.Yellow(mt.Root().Hex())
56}
57
58// PrintLevelsMT prints the tree in the terminal until a specified depth
59func (mt *MerkleTree) PrintLevelsMT(maxLevel int) {
60	mt.printLevel(mt.root, 0, mt.numLevels-1-maxLevel)
61	fmt.Print("root: ")
62	color.Yellow(mt.Root().Hex())
63}