You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

63 lines
1.8 KiB

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