node.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. package trie
  2. import "fmt"
  3. var indices = []string{"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f", "[17]"}
  4. type Node interface {
  5. Value() Node
  6. Copy(*Trie) Node // All nodes, for now, return them self
  7. Dirty() bool
  8. fstring(string) string
  9. Hash() interface{}
  10. RlpData() interface{}
  11. setDirty(dirty bool)
  12. }
  13. // Value node
  14. func (self *ValueNode) String() string { return self.fstring("") }
  15. func (self *FullNode) String() string { return self.fstring("") }
  16. func (self *ShortNode) String() string { return self.fstring("") }
  17. func (self *ValueNode) fstring(ind string) string { return fmt.Sprintf("%x ", self.data) }
  18. //func (self *HashNode) fstring(ind string) string { return fmt.Sprintf("< %x > ", self.key) }
  19. func (self *HashNode) fstring(ind string) string {
  20. return fmt.Sprintf("%v", self.trie.trans(self))
  21. }
  22. // Full node
  23. func (self *FullNode) fstring(ind string) string {
  24. resp := fmt.Sprintf("[\n%s ", ind)
  25. for i, node := range self.nodes {
  26. if node == nil {
  27. resp += fmt.Sprintf("%s: <nil> ", indices[i])
  28. } else {
  29. resp += fmt.Sprintf("%s: %v", indices[i], node.fstring(ind+" "))
  30. }
  31. }
  32. return resp + fmt.Sprintf("\n%s] ", ind)
  33. }
  34. // Short node
  35. func (self *ShortNode) fstring(ind string) string {
  36. return fmt.Sprintf("[ %x: %v ] ", self.key, self.value.fstring(ind+" "))
  37. }