node.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. // Copyright 2014 The go-ethereum Authors
  2. // This file is part of go-ethereum.
  3. //
  4. // go-ethereum is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // go-ethereum is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
  16. package trie
  17. import "fmt"
  18. var indices = []string{"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f", "[17]"}
  19. type Node interface {
  20. Value() Node
  21. Copy(*Trie) Node // All nodes, for now, return them self
  22. Dirty() bool
  23. fstring(string) string
  24. Hash() interface{}
  25. RlpData() interface{}
  26. setDirty(dirty bool)
  27. }
  28. // Value node
  29. func (self *ValueNode) String() string { return self.fstring("") }
  30. func (self *FullNode) String() string { return self.fstring("") }
  31. func (self *ShortNode) String() string { return self.fstring("") }
  32. func (self *ValueNode) fstring(ind string) string { return fmt.Sprintf("%x ", self.data) }
  33. //func (self *HashNode) fstring(ind string) string { return fmt.Sprintf("< %x > ", self.key) }
  34. func (self *HashNode) fstring(ind string) string {
  35. return fmt.Sprintf("%v", self.trie.trans(self))
  36. }
  37. // Full node
  38. func (self *FullNode) fstring(ind string) string {
  39. resp := fmt.Sprintf("[\n%s ", ind)
  40. for i, node := range self.nodes {
  41. if node == nil {
  42. resp += fmt.Sprintf("%s: <nil> ", indices[i])
  43. } else {
  44. resp += fmt.Sprintf("%s: %v", indices[i], node.fstring(ind+" "))
  45. }
  46. }
  47. return resp + fmt.Sprintf("\n%s] ", ind)
  48. }
  49. // Short node
  50. func (self *ShortNode) fstring(ind string) string {
  51. return fmt.Sprintf("[ %x: %v ] ", self.key, self.value.fstring(ind+" "))
  52. }