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.

460 lines
12 KiB

10 years ago
11 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
11 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
11 years ago
10 years ago
11 years ago
10 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
10 years ago
10 years ago
11 years ago
11 years ago
10 years ago
10 years ago
10 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
10 years ago
10 years ago
11 years ago
10 years ago
11 years ago
11 years ago
10 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
10 years ago
10 years ago
10 years ago
11 years ago
11 years ago
10 years ago
10 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
10 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  1. package merkle
  2. import (
  3. "bytes"
  4. "crypto/sha256"
  5. "io"
  6. "github.com/tendermint/tendermint/binary"
  7. )
  8. // Node
  9. type IAVLNode struct {
  10. key interface{}
  11. value interface{}
  12. height int8
  13. size int
  14. hash []byte
  15. leftHash []byte
  16. leftNode *IAVLNode
  17. rightHash []byte
  18. rightNode *IAVLNode
  19. persisted bool
  20. }
  21. func NewIAVLNode(key interface{}, value interface{}) *IAVLNode {
  22. return &IAVLNode{
  23. key: key,
  24. value: value,
  25. height: 0,
  26. size: 1,
  27. }
  28. }
  29. // NOTE: The hash is not saved or set. The caller should set the hash afterwards.
  30. // (Presumably the caller already has the hash)
  31. func ReadIAVLNode(t *IAVLTree, r io.Reader, n *int64, err *error) *IAVLNode {
  32. node := &IAVLNode{}
  33. // node header
  34. node.height = binary.ReadInt8(r, n, err)
  35. node.size = binary.ReadVarint(r, n, err)
  36. node.key = decodeByteSlice(t.keyCodec, r, n, err)
  37. if node.height == 0 {
  38. // value
  39. node.value = decodeByteSlice(t.valueCodec, r, n, err)
  40. } else {
  41. // children
  42. node.leftHash = binary.ReadByteSlice(r, n, err)
  43. node.rightHash = binary.ReadByteSlice(r, n, err)
  44. }
  45. if *err != nil {
  46. panic(*err)
  47. }
  48. return node
  49. }
  50. func (node *IAVLNode) _copy() *IAVLNode {
  51. if node.height == 0 {
  52. panic("Why are you copying a value node?")
  53. }
  54. return &IAVLNode{
  55. key: node.key,
  56. height: node.height,
  57. size: node.size,
  58. hash: nil, // Going to be mutated anyways.
  59. leftHash: node.leftHash,
  60. leftNode: node.leftNode,
  61. rightHash: node.rightHash,
  62. rightNode: node.rightNode,
  63. persisted: false, // Going to be mutated, so it can't already be persisted.
  64. }
  65. }
  66. func (node *IAVLNode) has(t *IAVLTree, key interface{}) (has bool) {
  67. if t.keyCodec.Compare(node.key, key) == 0 {
  68. return true
  69. }
  70. if node.height == 0 {
  71. return false
  72. } else {
  73. if t.keyCodec.Compare(key, node.key) < 0 {
  74. return node.getLeftNode(t).has(t, key)
  75. } else {
  76. return node.getRightNode(t).has(t, key)
  77. }
  78. }
  79. }
  80. func (node *IAVLNode) get(t *IAVLTree, key interface{}) (index int, value interface{}) {
  81. if node.height == 0 {
  82. if t.keyCodec.Compare(node.key, key) == 0 {
  83. return 0, node.value
  84. } else {
  85. return 0, nil
  86. }
  87. } else {
  88. if t.keyCodec.Compare(key, node.key) < 0 {
  89. return node.getLeftNode(t).get(t, key)
  90. } else {
  91. rightNode := node.getRightNode(t)
  92. index, value = rightNode.get(t, key)
  93. index += node.size - rightNode.size
  94. return index, value
  95. }
  96. }
  97. }
  98. func (node *IAVLNode) getByIndex(t *IAVLTree, index int) (key interface{}, value interface{}) {
  99. if node.height == 0 {
  100. if index == 0 {
  101. return node.key, node.value
  102. } else {
  103. panic("getByIndex asked for invalid index")
  104. }
  105. } else {
  106. // TODO: could improve this by storing the
  107. // sizes as well as left/right hash.
  108. leftNode := node.getLeftNode(t)
  109. if index < leftNode.size {
  110. return leftNode.getByIndex(t, index)
  111. } else {
  112. return node.getRightNode(t).getByIndex(t, index-leftNode.size)
  113. }
  114. }
  115. }
  116. // NOTE: sets hashes recursively
  117. func (node *IAVLNode) hashWithCount(t *IAVLTree) ([]byte, int) {
  118. if node.hash != nil {
  119. return node.hash, 0
  120. }
  121. hasher := sha256.New()
  122. buf := new(bytes.Buffer)
  123. _, hashCount, err := node.writeHashBytes(t, buf)
  124. if err != nil {
  125. panic(err)
  126. }
  127. // fmt.Printf("Wrote IAVL hash bytes: %X\n", buf.Bytes())
  128. hasher.Write(buf.Bytes())
  129. node.hash = hasher.Sum(nil)
  130. // fmt.Printf("Write IAVL hash: %X\n", node.hash)
  131. return node.hash, hashCount + 1
  132. }
  133. // NOTE: sets hashes recursively
  134. func (node *IAVLNode) writeHashBytes(t *IAVLTree, w io.Writer) (n int64, hashCount int, err error) {
  135. // height & size
  136. binary.WriteInt8(node.height, w, &n, &err)
  137. binary.WriteVarint(node.size, w, &n, &err)
  138. // key is not written for inner nodes, unlike writePersistBytes
  139. if node.height == 0 {
  140. // key & value
  141. encodeByteSlice(node.key, t.keyCodec, w, &n, &err)
  142. encodeByteSlice(node.value, t.valueCodec, w, &n, &err)
  143. } else {
  144. // left
  145. if node.leftNode != nil {
  146. leftHash, leftCount := node.leftNode.hashWithCount(t)
  147. node.leftHash = leftHash
  148. hashCount += leftCount
  149. }
  150. if node.leftHash == nil {
  151. panic("node.leftHash was nil in writeHashBytes")
  152. }
  153. binary.WriteByteSlice(node.leftHash, w, &n, &err)
  154. // right
  155. if node.rightNode != nil {
  156. rightHash, rightCount := node.rightNode.hashWithCount(t)
  157. node.rightHash = rightHash
  158. hashCount += rightCount
  159. }
  160. if node.rightHash == nil {
  161. panic("node.rightHash was nil in writeHashBytes")
  162. }
  163. binary.WriteByteSlice(node.rightHash, w, &n, &err)
  164. }
  165. return
  166. }
  167. // NOTE: sets hashes recursively
  168. // NOTE: clears leftNode/rightNode recursively
  169. func (node *IAVLNode) save(t *IAVLTree) []byte {
  170. if node.hash == nil {
  171. node.hash, _ = node.hashWithCount(t)
  172. }
  173. if node.persisted {
  174. return node.hash
  175. }
  176. // save children
  177. if node.leftNode != nil {
  178. node.leftHash = node.leftNode.save(t)
  179. node.leftNode = nil
  180. }
  181. if node.rightNode != nil {
  182. node.rightHash = node.rightNode.save(t)
  183. node.rightNode = nil
  184. }
  185. // save node
  186. t.ndb.SaveNode(t, node)
  187. return node.hash
  188. }
  189. // NOTE: sets hashes recursively
  190. func (node *IAVLNode) writePersistBytes(t *IAVLTree, w io.Writer) (n int64, err error) {
  191. // node header
  192. binary.WriteInt8(node.height, w, &n, &err)
  193. binary.WriteVarint(node.size, w, &n, &err)
  194. // key (unlike writeHashBytes, key is written for inner nodes)
  195. encodeByteSlice(node.key, t.keyCodec, w, &n, &err)
  196. if node.height == 0 {
  197. // value
  198. encodeByteSlice(node.value, t.valueCodec, w, &n, &err)
  199. } else {
  200. // left
  201. if node.leftHash == nil {
  202. panic("node.leftHash was nil in writePersistBytes")
  203. }
  204. binary.WriteByteSlice(node.leftHash, w, &n, &err)
  205. // right
  206. if node.rightHash == nil {
  207. panic("node.rightHash was nil in writePersistBytes")
  208. }
  209. binary.WriteByteSlice(node.rightHash, w, &n, &err)
  210. }
  211. return
  212. }
  213. func (node *IAVLNode) set(t *IAVLTree, key interface{}, value interface{}) (newSelf *IAVLNode, updated bool) {
  214. if node.height == 0 {
  215. cmp := t.keyCodec.Compare(key, node.key)
  216. if cmp < 0 {
  217. return &IAVLNode{
  218. key: node.key,
  219. height: 1,
  220. size: 2,
  221. leftNode: NewIAVLNode(key, value),
  222. rightNode: node,
  223. }, false
  224. } else if cmp == 0 {
  225. return NewIAVLNode(key, value), true
  226. } else {
  227. return &IAVLNode{
  228. key: key,
  229. height: 1,
  230. size: 2,
  231. leftNode: node,
  232. rightNode: NewIAVLNode(key, value),
  233. }, false
  234. }
  235. } else {
  236. node = node._copy()
  237. if t.keyCodec.Compare(key, node.key) < 0 {
  238. node.leftNode, updated = node.getLeftNode(t).set(t, key, value)
  239. node.leftHash = nil
  240. } else {
  241. node.rightNode, updated = node.getRightNode(t).set(t, key, value)
  242. node.rightHash = nil
  243. }
  244. if updated {
  245. return node, updated
  246. } else {
  247. node.calcHeightAndSize(t)
  248. return node.balance(t), updated
  249. }
  250. }
  251. }
  252. // newHash/newNode: The new hash or node to replace node after remove.
  253. // newKey: new leftmost leaf key for tree after successfully removing 'key' if changed.
  254. // value: removed value.
  255. func (node *IAVLNode) remove(t *IAVLTree, key interface{}) (
  256. newHash []byte, newNode *IAVLNode, newKey interface{}, value interface{}, removed bool) {
  257. if node.height == 0 {
  258. if t.keyCodec.Compare(key, node.key) == 0 {
  259. return nil, nil, nil, node.value, true
  260. } else {
  261. return nil, node, nil, nil, false
  262. }
  263. } else {
  264. if t.keyCodec.Compare(key, node.key) < 0 {
  265. var newLeftHash []byte
  266. var newLeftNode *IAVLNode
  267. newLeftHash, newLeftNode, newKey, value, removed = node.getLeftNode(t).remove(t, key)
  268. if !removed {
  269. return nil, node, nil, value, false
  270. } else if newLeftHash == nil && newLeftNode == nil { // left node held value, was removed
  271. return node.rightHash, node.rightNode, node.key, value, true
  272. }
  273. node = node._copy()
  274. node.leftHash, node.leftNode = newLeftHash, newLeftNode
  275. node.calcHeightAndSize(t)
  276. return nil, node.balance(t), newKey, value, true
  277. } else {
  278. var newRightHash []byte
  279. var newRightNode *IAVLNode
  280. newRightHash, newRightNode, newKey, value, removed = node.getRightNode(t).remove(t, key)
  281. if !removed {
  282. return nil, node, nil, value, false
  283. } else if newRightHash == nil && newRightNode == nil { // right node held value, was removed
  284. return node.leftHash, node.leftNode, nil, value, true
  285. }
  286. node = node._copy()
  287. node.rightHash, node.rightNode = newRightHash, newRightNode
  288. if newKey != nil {
  289. node.key = newKey
  290. newKey = nil
  291. }
  292. node.calcHeightAndSize(t)
  293. return nil, node.balance(t), newKey, value, true
  294. }
  295. }
  296. }
  297. func (node *IAVLNode) getLeftNode(t *IAVLTree) *IAVLNode {
  298. if node.leftNode != nil {
  299. return node.leftNode
  300. } else {
  301. return t.ndb.GetNode(t, node.leftHash)
  302. }
  303. }
  304. func (node *IAVLNode) getRightNode(t *IAVLTree) *IAVLNode {
  305. if node.rightNode != nil {
  306. return node.rightNode
  307. } else {
  308. return t.ndb.GetNode(t, node.rightHash)
  309. }
  310. }
  311. func (node *IAVLNode) rotateRight(t *IAVLTree) *IAVLNode {
  312. node = node._copy()
  313. sl := node.getLeftNode(t)._copy()
  314. slrHash, slrCached := sl.rightHash, sl.rightNode
  315. sl.rightHash, sl.rightNode = nil, node
  316. node.leftHash, node.leftNode = slrHash, slrCached
  317. node.calcHeightAndSize(t)
  318. sl.calcHeightAndSize(t)
  319. return sl
  320. }
  321. func (node *IAVLNode) rotateLeft(t *IAVLTree) *IAVLNode {
  322. node = node._copy()
  323. sr := node.getRightNode(t)._copy()
  324. srlHash, srlCached := sr.leftHash, sr.leftNode
  325. sr.leftHash, sr.leftNode = nil, node
  326. node.rightHash, node.rightNode = srlHash, srlCached
  327. node.calcHeightAndSize(t)
  328. sr.calcHeightAndSize(t)
  329. return sr
  330. }
  331. // NOTE: mutates height and size
  332. func (node *IAVLNode) calcHeightAndSize(t *IAVLTree) {
  333. node.height = maxInt8(node.getLeftNode(t).height, node.getRightNode(t).height) + 1
  334. node.size = node.getLeftNode(t).size + node.getRightNode(t).size
  335. }
  336. func (node *IAVLNode) calcBalance(t *IAVLTree) int {
  337. return int(node.getLeftNode(t).height) - int(node.getRightNode(t).height)
  338. }
  339. func (node *IAVLNode) balance(t *IAVLTree) (newSelf *IAVLNode) {
  340. balance := node.calcBalance(t)
  341. if balance > 1 {
  342. if node.getLeftNode(t).calcBalance(t) >= 0 {
  343. // Left Left Case
  344. return node.rotateRight(t)
  345. } else {
  346. // Left Right Case
  347. node = node._copy()
  348. node.leftHash, node.leftNode = nil, node.getLeftNode(t).rotateLeft(t)
  349. //node.calcHeightAndSize()
  350. return node.rotateRight(t)
  351. }
  352. }
  353. if balance < -1 {
  354. if node.getRightNode(t).calcBalance(t) <= 0 {
  355. // Right Right Case
  356. return node.rotateLeft(t)
  357. } else {
  358. // Right Left Case
  359. node = node._copy()
  360. node.rightHash, node.rightNode = nil, node.getRightNode(t).rotateRight(t)
  361. //node.calcHeightAndSize()
  362. return node.rotateLeft(t)
  363. }
  364. }
  365. // Nothing changed
  366. return node
  367. }
  368. func (node *IAVLNode) traverse(t *IAVLTree, cb func(*IAVLNode) bool) bool {
  369. stop := cb(node)
  370. if stop {
  371. return stop
  372. }
  373. if node.height > 0 {
  374. stop = node.getLeftNode(t).traverse(t, cb)
  375. if stop {
  376. return stop
  377. }
  378. stop = node.getRightNode(t).traverse(t, cb)
  379. if stop {
  380. return stop
  381. }
  382. }
  383. return false
  384. }
  385. // Only used in testing...
  386. func (node *IAVLNode) lmd(t *IAVLTree) *IAVLNode {
  387. if node.height == 0 {
  388. return node
  389. }
  390. return node.getLeftNode(t).lmd(t)
  391. }
  392. // Only used in testing...
  393. func (node *IAVLNode) rmd(t *IAVLTree) *IAVLNode {
  394. if node.height == 0 {
  395. return node
  396. }
  397. return node.getRightNode(t).rmd(t)
  398. }
  399. //--------------------------------------------------------------------------------
  400. // Read a (length prefixed) byteslice then decode the object using the codec
  401. func decodeByteSlice(codec binary.Codec, r io.Reader, n *int64, err *error) interface{} {
  402. bytez := binary.ReadByteSlice(r, n, err)
  403. if *err != nil {
  404. return nil
  405. }
  406. n_ := new(int64)
  407. return codec.Decode(bytes.NewBuffer(bytez), n_, err)
  408. }
  409. // Encode object using codec, then write a (length prefixed) byteslice.
  410. func encodeByteSlice(o interface{}, codec binary.Codec, w io.Writer, n *int64, err *error) {
  411. buf, n_ := new(bytes.Buffer), new(int64)
  412. codec.Encode(o, buf, n_, err)
  413. if *err != nil {
  414. return
  415. }
  416. binary.WriteByteSlice(buf.Bytes(), w, n, err)
  417. }