node.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. // Copyright 2014 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library 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. // The go-ethereum library 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 the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package trie
  17. import (
  18. "fmt"
  19. "io"
  20. "strings"
  21. "github.com/ethereum/go-ethereum/common"
  22. "github.com/ethereum/go-ethereum/rlp"
  23. )
  24. var indices = []string{"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f", "[17]"}
  25. type node interface {
  26. fstring(string) string
  27. }
  28. type (
  29. fullNode [17]node
  30. shortNode struct {
  31. Key []byte
  32. Val node
  33. }
  34. hashNode []byte
  35. valueNode []byte
  36. )
  37. // Pretty printing.
  38. func (n fullNode) String() string { return n.fstring("") }
  39. func (n shortNode) String() string { return n.fstring("") }
  40. func (n hashNode) String() string { return n.fstring("") }
  41. func (n valueNode) String() string { return n.fstring("") }
  42. func (n fullNode) fstring(ind string) string {
  43. resp := fmt.Sprintf("[\n%s ", ind)
  44. for i, node := range n {
  45. if node == nil {
  46. resp += fmt.Sprintf("%s: <nil> ", indices[i])
  47. } else {
  48. resp += fmt.Sprintf("%s: %v", indices[i], node.fstring(ind+" "))
  49. }
  50. }
  51. return resp + fmt.Sprintf("\n%s] ", ind)
  52. }
  53. func (n shortNode) fstring(ind string) string {
  54. return fmt.Sprintf("{%x: %v} ", n.Key, n.Val.fstring(ind+" "))
  55. }
  56. func (n hashNode) fstring(ind string) string {
  57. return fmt.Sprintf("<%x> ", []byte(n))
  58. }
  59. func (n valueNode) fstring(ind string) string {
  60. return fmt.Sprintf("%x ", []byte(n))
  61. }
  62. func mustDecodeNode(dbkey, buf []byte) node {
  63. n, err := decodeNode(buf)
  64. if err != nil {
  65. panic(fmt.Sprintf("node %x: %v", dbkey, err))
  66. }
  67. return n
  68. }
  69. // decodeNode parses the RLP encoding of a trie node.
  70. func decodeNode(buf []byte) (node, error) {
  71. if len(buf) == 0 {
  72. return nil, io.ErrUnexpectedEOF
  73. }
  74. elems, _, err := rlp.SplitList(buf)
  75. if err != nil {
  76. return nil, fmt.Errorf("decode error: %v", err)
  77. }
  78. switch c, _ := rlp.CountValues(elems); c {
  79. case 2:
  80. n, err := decodeShort(elems)
  81. return n, wrapError(err, "short")
  82. case 17:
  83. n, err := decodeFull(elems)
  84. return n, wrapError(err, "full")
  85. default:
  86. return nil, fmt.Errorf("invalid number of list elements: %v", c)
  87. }
  88. }
  89. func decodeShort(buf []byte) (node, error) {
  90. kbuf, rest, err := rlp.SplitString(buf)
  91. if err != nil {
  92. return nil, err
  93. }
  94. key := compactDecode(kbuf)
  95. if key[len(key)-1] == 16 {
  96. // value node
  97. val, _, err := rlp.SplitString(rest)
  98. if err != nil {
  99. return nil, fmt.Errorf("invalid value node: %v", err)
  100. }
  101. return shortNode{key, valueNode(val)}, nil
  102. }
  103. r, _, err := decodeRef(rest)
  104. if err != nil {
  105. return nil, wrapError(err, "val")
  106. }
  107. return shortNode{key, r}, nil
  108. }
  109. func decodeFull(buf []byte) (fullNode, error) {
  110. var n fullNode
  111. for i := 0; i < 16; i++ {
  112. cld, rest, err := decodeRef(buf)
  113. if err != nil {
  114. return n, wrapError(err, fmt.Sprintf("[%d]", i))
  115. }
  116. n[i], buf = cld, rest
  117. }
  118. val, _, err := rlp.SplitString(buf)
  119. if err != nil {
  120. return n, err
  121. }
  122. if len(val) > 0 {
  123. n[16] = valueNode(val)
  124. }
  125. return n, nil
  126. }
  127. const hashLen = len(common.Hash{})
  128. func decodeRef(buf []byte) (node, []byte, error) {
  129. kind, val, rest, err := rlp.Split(buf)
  130. if err != nil {
  131. return nil, buf, err
  132. }
  133. switch {
  134. case kind == rlp.List:
  135. // 'embedded' node reference. The encoding must be smaller
  136. // than a hash in order to be valid.
  137. if size := len(buf) - len(rest); size > hashLen {
  138. err := fmt.Errorf("oversized embedded node (size is %d bytes, want size < %d)", size, hashLen)
  139. return nil, buf, err
  140. }
  141. n, err := decodeNode(buf)
  142. return n, rest, err
  143. case kind == rlp.String && len(val) == 0:
  144. // empty node
  145. return nil, rest, nil
  146. case kind == rlp.String && len(val) == 32:
  147. return hashNode(val), rest, nil
  148. default:
  149. return nil, nil, fmt.Errorf("invalid RLP string size %d (want 0 or 32)", len(val))
  150. }
  151. }
  152. // wraps a decoding error with information about the path to the
  153. // invalid child node (for debugging encoding issues).
  154. type decodeError struct {
  155. what error
  156. stack []string
  157. }
  158. func wrapError(err error, ctx string) error {
  159. if err == nil {
  160. return nil
  161. }
  162. if decErr, ok := err.(*decodeError); ok {
  163. decErr.stack = append(decErr.stack, ctx)
  164. return decErr
  165. }
  166. return &decodeError{err, []string{ctx}}
  167. }
  168. func (err *decodeError) Error() string {
  169. return fmt.Sprintf("%v (decode path: %s)", err.what, strings.Join(err.stack, "<-"))
  170. }