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.

36 lines
835 B

6 years ago
  1. package merkletree
  2. import "bytes"
  3. // treeNode is the data structure of an intermediate node of the Merkle Tree
  4. type treeNode struct {
  5. ChildL Hash // hash of the left child
  6. ChildR Hash // hash of the right child
  7. }
  8. // Bytes returns an array of bytes with the Node data
  9. func (n *treeNode) Bytes() (b []byte) {
  10. b = append(b, n.ChildL[:]...)
  11. b = append(b, n.ChildR[:]...)
  12. return b
  13. }
  14. // Ht returns the hash of the full node
  15. func (n *treeNode) Ht() Hash {
  16. h := HashBytes(n.Bytes())
  17. return h
  18. }
  19. // ParseNodeBytes returns a Node struct from an array of bytes
  20. func parseNodeBytes(b []byte) treeNode {
  21. if bytes.Equal(b, EmptyNodeValue[:]) {
  22. var node treeNode
  23. node.ChildL = EmptyNodeValue
  24. node.ChildR = EmptyNodeValue
  25. return node
  26. }
  27. var node treeNode
  28. copy(node.ChildL[:], b[:32])
  29. copy(node.ChildR[:], b[32:])
  30. return node
  31. }