trie.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. package ethutil
  2. import (
  3. "fmt"
  4. "reflect"
  5. )
  6. type Node struct {
  7. Key []byte
  8. Value *Value
  9. Dirty bool
  10. }
  11. func NewNode(key []byte, val *Value, dirty bool) *Node {
  12. return &Node{Key: key, Value: val, Dirty: dirty}
  13. }
  14. func (n *Node) Copy() *Node {
  15. return NewNode(n.Key, n.Value, n.Dirty)
  16. }
  17. type Cache struct {
  18. nodes map[string]*Node
  19. db Database
  20. }
  21. func NewCache(db Database) *Cache {
  22. return &Cache{db: db, nodes: make(map[string]*Node)}
  23. }
  24. func (cache *Cache) Put(v interface{}) interface{} {
  25. value := NewValue(v)
  26. enc := value.Encode()
  27. if len(enc) >= 32 {
  28. sha := Sha3Bin(enc)
  29. cache.nodes[string(sha)] = NewNode(sha, value, true)
  30. return sha
  31. }
  32. return v
  33. }
  34. func (cache *Cache) Get(key []byte) *Value {
  35. // First check if the key is the cache
  36. if cache.nodes[string(key)] != nil {
  37. return cache.nodes[string(key)].Value
  38. }
  39. // Get the key of the database instead and cache it
  40. data, _ := cache.db.Get(key)
  41. // Create the cached value
  42. value := NewValueFromBytes(data)
  43. // Create caching node
  44. cache.nodes[string(key)] = NewNode(key, value, false)
  45. return value
  46. }
  47. func (cache *Cache) Commit() {
  48. for key, node := range cache.nodes {
  49. if node.Dirty {
  50. cache.db.Put([]byte(key), node.Value.Encode())
  51. node.Dirty = false
  52. }
  53. }
  54. // If the nodes grows beyond the 200 entries we simple empty it
  55. // FIXME come up with something better
  56. if len(cache.nodes) > 200 {
  57. cache.nodes = make(map[string]*Node)
  58. }
  59. }
  60. func (cache *Cache) Undo() {
  61. for key, node := range cache.nodes {
  62. if node.Dirty {
  63. delete(cache.nodes, key)
  64. }
  65. }
  66. }
  67. // A (modified) Radix Trie implementation. The Trie implements
  68. // a caching mechanism and will used cached values if they are
  69. // present. If a node is not present in the cache it will try to
  70. // fetch it from the database and store the cached value.
  71. // Please note that the data isn't persisted unless `Sync` is
  72. // explicitly called.
  73. type Trie struct {
  74. Root interface{}
  75. //db Database
  76. cache *Cache
  77. }
  78. func NewTrie(db Database, Root interface{}) *Trie {
  79. return &Trie{cache: NewCache(db), Root: Root}
  80. }
  81. // Save the cached value to the database.
  82. func (t *Trie) Sync() {
  83. t.cache.Commit()
  84. }
  85. /*
  86. * Public (query) interface functions
  87. */
  88. func (t *Trie) Update(key string, value string) {
  89. k := CompactHexDecode(key)
  90. t.Root = t.UpdateState(t.Root, k, value)
  91. }
  92. func (t *Trie) Get(key string) string {
  93. k := CompactHexDecode(key)
  94. c := NewValue(t.GetState(t.Root, k))
  95. return c.Str()
  96. }
  97. func (t *Trie) GetState(node interface{}, key []int) interface{} {
  98. n := NewValue(node)
  99. // Return the node if key is empty (= found)
  100. if len(key) == 0 || n.IsNil() || n.Len() == 0 {
  101. return node
  102. }
  103. currentNode := t.GetNode(node)
  104. length := currentNode.Len()
  105. if length == 0 {
  106. return ""
  107. } else if length == 2 {
  108. // Decode the key
  109. k := CompactDecode(currentNode.Get(0).Str())
  110. v := currentNode.Get(1).Raw()
  111. if len(key) >= len(k) && CompareIntSlice(k, key[:len(k)]) {
  112. return t.GetState(v, key[len(k):])
  113. } else {
  114. return ""
  115. }
  116. } else if length == 17 {
  117. return t.GetState(currentNode.Get(key[0]).Raw(), key[1:])
  118. }
  119. // It shouldn't come this far
  120. fmt.Println("GetState unexpected return")
  121. return ""
  122. }
  123. func (t *Trie) GetNode(node interface{}) *Value {
  124. n := NewValue(node)
  125. if !n.Get(0).IsNil() {
  126. return n
  127. }
  128. str := n.Str()
  129. if len(str) == 0 {
  130. return n
  131. } else if len(str) < 32 {
  132. return NewValueFromBytes([]byte(str))
  133. }
  134. return t.cache.Get(n.Bytes())
  135. }
  136. func (t *Trie) UpdateState(node interface{}, key []int, value string) interface{} {
  137. if value != "" {
  138. return t.InsertState(node, key, value)
  139. } else {
  140. // delete it
  141. }
  142. return ""
  143. }
  144. func (t *Trie) Put(node interface{}) interface{} {
  145. /*
  146. enc := Encode(node)
  147. if len(enc) >= 32 {
  148. var sha []byte
  149. sha = Sha3Bin(enc)
  150. //t.db.Put([]byte(sha), enc)
  151. return sha
  152. }
  153. return node
  154. */
  155. /*
  156. TODO?
  157. c := Conv(t.Root)
  158. fmt.Println(c.Type(), c.Length())
  159. if c.Type() == reflect.String && c.AsString() == "" {
  160. return enc
  161. }
  162. */
  163. return t.cache.Put(node)
  164. }
  165. func EmptyStringSlice(l int) []interface{} {
  166. slice := make([]interface{}, l)
  167. for i := 0; i < l; i++ {
  168. slice[i] = ""
  169. }
  170. return slice
  171. }
  172. func (t *Trie) InsertState(node interface{}, key []int, value interface{}) interface{} {
  173. if len(key) == 0 {
  174. return value
  175. }
  176. // New node
  177. n := NewValue(node)
  178. if node == nil || (n.Type() == reflect.String && (n.Str() == "" || n.Get(0).IsNil())) || n.Len() == 0 {
  179. newNode := []interface{}{CompactEncode(key), value}
  180. return t.Put(newNode)
  181. }
  182. currentNode := t.GetNode(node)
  183. // Check for "special" 2 slice type node
  184. if currentNode.Len() == 2 {
  185. // Decode the key
  186. k := CompactDecode(currentNode.Get(0).Str())
  187. v := currentNode.Get(1).Raw()
  188. // Matching key pair (ie. there's already an object with this key)
  189. if CompareIntSlice(k, key) {
  190. newNode := []interface{}{CompactEncode(key), value}
  191. return t.Put(newNode)
  192. }
  193. var newHash interface{}
  194. matchingLength := MatchingNibbleLength(key, k)
  195. if matchingLength == len(k) {
  196. // Insert the hash, creating a new node
  197. newHash = t.InsertState(v, key[matchingLength:], value)
  198. } else {
  199. // Expand the 2 length slice to a 17 length slice
  200. oldNode := t.InsertState("", k[matchingLength+1:], v)
  201. newNode := t.InsertState("", key[matchingLength+1:], value)
  202. // Create an expanded slice
  203. scaledSlice := EmptyStringSlice(17)
  204. // Set the copied and new node
  205. scaledSlice[k[matchingLength]] = oldNode
  206. scaledSlice[key[matchingLength]] = newNode
  207. newHash = t.Put(scaledSlice)
  208. }
  209. if matchingLength == 0 {
  210. // End of the chain, return
  211. return newHash
  212. } else {
  213. newNode := []interface{}{CompactEncode(key[:matchingLength]), newHash}
  214. return t.Put(newNode)
  215. }
  216. } else {
  217. // Copy the current node over to the new node and replace the first nibble in the key
  218. newNode := EmptyStringSlice(17)
  219. for i := 0; i < 17; i++ {
  220. cpy := currentNode.Get(i).Raw()
  221. if cpy != nil {
  222. newNode[i] = cpy
  223. }
  224. }
  225. newNode[key[0]] = t.InsertState(currentNode.Get(key[0]).Raw(), key[1:], value)
  226. return t.Put(newNode)
  227. }
  228. return ""
  229. }
  230. // Simple compare function which creates a rlp value out of the evaluated objects
  231. func (t *Trie) Cmp(trie *Trie) bool {
  232. return NewValue(t.Root).Cmp(NewValue(trie.Root))
  233. }
  234. // Returns a copy of this trie
  235. func (t *Trie) Copy() *Trie {
  236. trie := NewTrie(t.cache.db, t.Root)
  237. for key, node := range t.cache.nodes {
  238. trie.cache.nodes[key] = node.Copy()
  239. }
  240. return trie
  241. }