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.

784 lines
19 KiB

  1. package arbo
  2. import (
  3. "bytes"
  4. "fmt"
  5. "math"
  6. "runtime"
  7. "sort"
  8. "sync"
  9. "github.com/iden3/go-merkletree/db"
  10. )
  11. /*
  12. AddBatch design
  13. ===============
  14. CASE A: Empty Tree --> if tree is empty (root==0)
  15. =================================================
  16. - Build the full tree from bottom to top (from all the leaf to the root)
  17. CASE B: ALMOST CASE A, Almost empty Tree --> if Tree has numLeafs < minLeafsThreshold
  18. ==============================================================================
  19. - Get the Leafs (key & value) (iterate the tree from the current root getting
  20. the leafs)
  21. - Create a new empty Tree
  22. - Do CASE A for the new Tree, giving the already existing key&values (leafs)
  23. from the original Tree + the new key&values to be added from the AddBatch call
  24. R R
  25. / \ / \
  26. A * / \
  27. / \ / \
  28. B C * *
  29. / | / \
  30. / | / \
  31. / | / \
  32. L: A B G D
  33. / \
  34. / \
  35. / \
  36. C *
  37. / \
  38. / \
  39. / \
  40. ... ... (nLeafs < minLeafsThreshold)
  41. CASE C: ALMOST CASE B --> if Tree has few Leafs (but numLeafs>=minLeafsThreshold)
  42. ==============================================================================
  43. - Use A, B, G, F as Roots of subtrees
  44. - Do CASE B for each subtree
  45. - Then go from L to the Root
  46. R
  47. / \
  48. / \
  49. / \
  50. * *
  51. / | / \
  52. / | / \
  53. / | / \
  54. L: A B G D
  55. / \
  56. / \
  57. / \
  58. C *
  59. / \
  60. / \
  61. / \
  62. ... ... (nLeafs >= minLeafsThreshold)
  63. CASE D: Already populated Tree
  64. ==============================
  65. - Use A, B, C, D as subtree
  66. - Sort the Keys in Buckets that share the initial part of the path
  67. - For each subtree add there the new leafs
  68. R
  69. / \
  70. / \
  71. / \
  72. * *
  73. / | / \
  74. / | / \
  75. / | / \
  76. L: A B C D
  77. /\ /\ / \ / \
  78. ... ... ... ... ... ...
  79. CASE E: Already populated Tree Unbalanced
  80. =========================================
  81. - Need to fill M1 and M2, and then will be able to use CASE D
  82. - Search for M1 & M2 in the inputed Keys
  83. - Add M1 & M2 to the Tree
  84. - From here can use CASE D
  85. R
  86. / \
  87. / \
  88. / \
  89. * *
  90. | \
  91. | \
  92. | \
  93. L: M1 * M2 * (where M1 and M2 are empty)
  94. / | /
  95. / | /
  96. / | /
  97. A * *
  98. / \ | \
  99. / \ | \
  100. / \ | \
  101. B * * C
  102. / \ |\
  103. ... ... | \
  104. | \
  105. D E
  106. Algorithm decision
  107. ==================
  108. - if nLeafs==0 (root==0): CASE A
  109. - if nLeafs<minLeafsThreshold: CASE B
  110. - if nLeafs>=minLeafsThreshold && (nLeafs/nBuckets) < minLeafsThreshold: CASE C
  111. - else: CASE D & CASE E
  112. - Multiple tree.Add calls: O(n log n)
  113. - Used in: cases A, B, C
  114. - Tree from bottom to top: O(log n)
  115. - Used in: cases D, E
  116. */
  117. const (
  118. minLeafsThreshold = 100 // nolint:gomnd // TMP WIP this will be autocalculated
  119. )
  120. // AddBatch adds a batch of key-values to the Tree. Returns an array containing
  121. // the indexes of the keys failed to add.
  122. func (t *Tree) AddBatch(keys, values [][]byte) ([]int, error) {
  123. // TODO: support vaules=nil
  124. t.updateAccessTime()
  125. t.Lock()
  126. defer t.Unlock()
  127. kvs, err := t.keysValuesToKvs(keys, values)
  128. if err != nil {
  129. return nil, err
  130. }
  131. t.tx, err = t.db.NewTx()
  132. if err != nil {
  133. return nil, err
  134. }
  135. // if nCPU is not a power of two, cut at the highest power of two under
  136. // nCPU
  137. nCPU := flp2(runtime.NumCPU())
  138. l := int(math.Log2(float64(nCPU)))
  139. var invalids []int
  140. // CASE A: if nLeafs==0 (root==0)
  141. if bytes.Equal(t.root, t.emptyHash) {
  142. invalids, err = t.caseA(nCPU, kvs)
  143. if err != nil {
  144. return nil, err
  145. }
  146. return t.finalizeAddBatch(len(keys), invalids)
  147. }
  148. // CASE B: if nLeafs<nBuckets
  149. nLeafs, err := t.GetNLeafs()
  150. if err != nil {
  151. return nil, err
  152. }
  153. if nLeafs < minLeafsThreshold { // CASE B
  154. invalids, err = t.caseB(nCPU, 0, kvs)
  155. if err != nil {
  156. return nil, err
  157. }
  158. return t.finalizeAddBatch(len(keys), invalids)
  159. }
  160. keysAtL, err := t.getKeysAtLevel(l + 1)
  161. if err != nil {
  162. return nil, err
  163. }
  164. // CASE C: if nLeafs>=minLeafsThreshold && (nLeafs/nBuckets) < minLeafsThreshold
  165. // available parallelization, will need to be a power of 2 (2**n)
  166. if nLeafs >= minLeafsThreshold &&
  167. (nLeafs/nCPU) < minLeafsThreshold &&
  168. len(keysAtL) == nCPU {
  169. invalids, err = t.caseC(nCPU, l, keysAtL, kvs)
  170. if err != nil {
  171. return nil, err
  172. }
  173. return t.finalizeAddBatch(len(keys), invalids)
  174. }
  175. // CASE E
  176. if len(keysAtL) != nCPU {
  177. // CASE E: add one key at each bucket, and then do CASE D
  178. buckets := splitInBuckets(kvs, nCPU)
  179. kvs = []kv{}
  180. for i := 0; i < len(buckets); i++ {
  181. // add one leaf of the bucket, if there is an error when
  182. // adding the k-v, try to add the next one of the bucket
  183. // (until one is added)
  184. var inserted int
  185. for j := 0; j < len(buckets[i]); j++ {
  186. if err := t.add(0, buckets[i][j].k, buckets[i][j].v); err == nil {
  187. inserted = j
  188. break
  189. }
  190. }
  191. // put the buckets elements except the inserted one
  192. kvs = append(kvs, buckets[i][:inserted]...)
  193. kvs = append(kvs, buckets[i][inserted+1:]...)
  194. }
  195. keysAtL, err = t.getKeysAtLevel(l + 1)
  196. if err != nil {
  197. return nil, err
  198. }
  199. }
  200. // CASE D
  201. if len(keysAtL) == nCPU { // enter in CASE D if len(keysAtL)=nCPU, if not, CASE E
  202. invalidsCaseD, err := t.caseD(nCPU, l, keysAtL, kvs)
  203. if err != nil {
  204. return nil, err
  205. }
  206. invalids = append(invalids, invalidsCaseD...)
  207. return t.finalizeAddBatch(len(keys), invalids)
  208. }
  209. return nil, fmt.Errorf("UNIMPLEMENTED")
  210. }
  211. func (t *Tree) finalizeAddBatch(nKeys int, invalids []int) ([]int, error) {
  212. // store root to db
  213. if err := t.dbPut(dbKeyRoot, t.root); err != nil {
  214. return nil, err
  215. }
  216. // update nLeafs
  217. if err := t.incNLeafs(nKeys - len(invalids)); err != nil {
  218. return nil, err
  219. }
  220. // commit db tx
  221. if err := t.tx.Commit(); err != nil {
  222. return nil, err
  223. }
  224. return invalids, nil
  225. }
  226. func (t *Tree) caseA(nCPU int, kvs []kv) ([]int, error) {
  227. invalids, err := t.buildTreeFromLeafs(nCPU, kvs)
  228. if err != nil {
  229. return nil, err
  230. }
  231. return invalids, nil
  232. }
  233. func (t *Tree) caseB(nCPU, l int, kvs []kv) ([]int, error) {
  234. // get already existing keys
  235. aKs, aVs, err := t.getLeafs(t.root)
  236. if err != nil {
  237. return nil, err
  238. }
  239. aKvs, err := t.keysValuesToKvs(aKs, aVs)
  240. if err != nil {
  241. return nil, err
  242. }
  243. // add already existing key-values to the inputted key-values
  244. // kvs = append(kvs, aKvs...)
  245. kvs, invalids := combineInKVSet(aKvs, kvs)
  246. // proceed with CASE A
  247. sortKvs(kvs)
  248. var invalids2 []int
  249. if nCPU > 1 {
  250. invalids2, err = t.buildTreeFromLeafs(nCPU, kvs)
  251. if err != nil {
  252. return nil, err
  253. }
  254. } else {
  255. invalids2, err = t.buildTreeFromLeafsSingleThread(l, kvs)
  256. if err != nil {
  257. return nil, err
  258. }
  259. }
  260. invalids = append(invalids, invalids2...)
  261. return invalids, nil
  262. }
  263. func (t *Tree) caseC(nCPU, l int, keysAtL [][]byte, kvs []kv) ([]int, error) {
  264. // 1. go down until level L (L=log2(nBuckets)): keysAtL
  265. var excedents []kv
  266. buckets := splitInBuckets(kvs, nCPU)
  267. // 2. use keys at level L as roots of the subtrees under each one
  268. subRoots := make([][]byte, nCPU)
  269. dbgStatsPerBucket := make([]*dbgStats, nCPU)
  270. txs := make([]db.Tx, nCPU)
  271. var wg sync.WaitGroup
  272. wg.Add(nCPU)
  273. for i := 0; i < nCPU; i++ {
  274. go func(cpu int) {
  275. var err error
  276. txs[cpu], err = t.db.NewTx()
  277. if err != nil {
  278. panic(err) // TODO WIP
  279. }
  280. if err := txs[cpu].Add(t.tx); err != nil {
  281. panic(err) // TODO
  282. }
  283. bucketTree := Tree{tx: txs[cpu], db: t.db, maxLevels: t.maxLevels,
  284. hashFunction: t.hashFunction, root: keysAtL[cpu],
  285. emptyHash: t.emptyHash, dbg: newDbgStats()}
  286. // 3. do CASE B (with 1 cpu) for each key at level L
  287. _, err = bucketTree.caseB(1, l, buckets[cpu]) // TODO handle invalids
  288. if err != nil {
  289. panic(err) // TODO WIP
  290. // return nil, err
  291. }
  292. subRoots[cpu] = bucketTree.root
  293. dbgStatsPerBucket[cpu] = bucketTree.dbg
  294. wg.Done()
  295. }(i)
  296. }
  297. wg.Wait()
  298. // merge buckets txs into Tree.tx
  299. for i := 0; i < len(txs); i++ {
  300. if err := t.tx.Add(txs[i]); err != nil {
  301. return nil, err
  302. }
  303. }
  304. // 4. go upFromKeys from the new roots of the subtrees
  305. newRoot, err := t.upFromKeys(subRoots)
  306. if err != nil {
  307. return nil, err
  308. }
  309. t.root = newRoot
  310. // add the key-values that have not been used yet
  311. var invalids []int
  312. for i := 0; i < len(excedents); i++ {
  313. if err = t.add(0, excedents[i].k, excedents[i].v); err != nil {
  314. invalids = append(invalids, excedents[i].pos)
  315. }
  316. }
  317. for i := 0; i < len(dbgStatsPerBucket); i++ {
  318. t.dbg.add(dbgStatsPerBucket[i])
  319. }
  320. return invalids, nil
  321. }
  322. func (t *Tree) caseD(nCPU, l int, keysAtL [][]byte, kvs []kv) ([]int, error) {
  323. if nCPU == 1 { // CASE D, but with 1 cpu
  324. var invalids []int
  325. for i := 0; i < len(kvs); i++ {
  326. if err := t.add(0, kvs[i].k, kvs[i].v); err != nil {
  327. invalids = append(invalids, kvs[i].pos)
  328. }
  329. }
  330. return invalids, nil
  331. }
  332. buckets := splitInBuckets(kvs, nCPU)
  333. subRoots := make([][]byte, nCPU)
  334. invalidsInBucket := make([][]int, nCPU)
  335. dbgStatsPerBucket := make([]*dbgStats, nCPU)
  336. txs := make([]db.Tx, nCPU)
  337. var wg sync.WaitGroup
  338. wg.Add(nCPU)
  339. for i := 0; i < nCPU; i++ {
  340. go func(cpu int) {
  341. var err error
  342. txs[cpu], err = t.db.NewTx()
  343. if err != nil {
  344. panic(err) // TODO WIP
  345. }
  346. // put already existing tx into txs[cpu], as txs[cpu]
  347. // needs the pending key-values that are not in tree.db,
  348. // but are in tree.tx
  349. if err := txs[cpu].Add(t.tx); err != nil {
  350. panic(err) // TODO WIP
  351. }
  352. bucketTree := Tree{tx: txs[cpu], db: t.db, maxLevels: t.maxLevels - l,
  353. hashFunction: t.hashFunction, root: keysAtL[cpu],
  354. emptyHash: t.emptyHash, dbg: newDbgStats()}
  355. for j := 0; j < len(buckets[cpu]); j++ {
  356. if err = bucketTree.add(l, buckets[cpu][j].k, buckets[cpu][j].v); err != nil {
  357. invalidsInBucket[cpu] = append(invalidsInBucket[cpu], buckets[cpu][j].pos)
  358. }
  359. }
  360. subRoots[cpu] = bucketTree.root
  361. dbgStatsPerBucket[cpu] = bucketTree.dbg
  362. wg.Done()
  363. }(i)
  364. }
  365. wg.Wait()
  366. // merge buckets txs into Tree.tx
  367. for i := 0; i < len(txs); i++ {
  368. if err := t.tx.Add(txs[i]); err != nil {
  369. return nil, err
  370. }
  371. }
  372. newRoot, err := t.upFromKeys(subRoots)
  373. if err != nil {
  374. return nil, err
  375. }
  376. t.root = newRoot
  377. var invalids []int
  378. for i := 0; i < len(invalidsInBucket); i++ {
  379. invalids = append(invalids, invalidsInBucket[i]...)
  380. }
  381. for i := 0; i < len(dbgStatsPerBucket); i++ {
  382. t.dbg.add(dbgStatsPerBucket[i])
  383. }
  384. return invalids, nil
  385. }
  386. func splitInBuckets(kvs []kv, nBuckets int) [][]kv {
  387. buckets := make([][]kv, nBuckets)
  388. // 1. classify the keyvalues into buckets
  389. for i := 0; i < len(kvs); i++ {
  390. pair := kvs[i]
  391. // bucketnum := keyToBucket(pair.k, nBuckets)
  392. bucketnum := keyToBucket(pair.keyPath, nBuckets)
  393. buckets[bucketnum] = append(buckets[bucketnum], pair)
  394. }
  395. return buckets
  396. }
  397. // TODO rename in a more 'real' name (calculate bucket from/for key)
  398. func keyToBucket(k []byte, nBuckets int) int {
  399. nLevels := int(math.Log2(float64(nBuckets)))
  400. b := make([]int, nBuckets)
  401. for i := 0; i < nBuckets; i++ {
  402. b[i] = i
  403. }
  404. r := b
  405. mid := len(r) / 2 //nolint:gomnd
  406. for i := 0; i < nLevels; i++ {
  407. if int(k[i/8]&(1<<(i%8))) != 0 {
  408. r = r[mid:]
  409. mid = len(r) / 2 //nolint:gomnd
  410. } else {
  411. r = r[:mid]
  412. mid = len(r) / 2 //nolint:gomnd
  413. }
  414. }
  415. return r[0]
  416. }
  417. type kv struct {
  418. pos int // original position in the array
  419. keyPath []byte
  420. k []byte
  421. v []byte
  422. }
  423. // compareBytes compares byte slices where the bytes are compared from left to
  424. // right and each byte is compared by bit from right to left
  425. func compareBytes(a, b []byte) bool {
  426. // WIP
  427. for i := 0; i < len(a); i++ {
  428. for j := 0; j < 8; j++ {
  429. aBit := a[i] & (1 << j)
  430. bBit := b[i] & (1 << j)
  431. if aBit > bBit {
  432. return false
  433. } else if aBit < bBit {
  434. return true
  435. }
  436. }
  437. }
  438. return false
  439. }
  440. // sortKvs sorts the kv by path
  441. func sortKvs(kvs []kv) {
  442. sort.Slice(kvs, func(i, j int) bool {
  443. return compareBytes(kvs[i].keyPath, kvs[j].keyPath)
  444. })
  445. }
  446. func (t *Tree) keysValuesToKvs(ks, vs [][]byte) ([]kv, error) {
  447. if len(ks) != len(vs) {
  448. return nil, fmt.Errorf("len(keys)!=len(values) (%d!=%d)",
  449. len(ks), len(vs))
  450. }
  451. kvs := make([]kv, len(ks))
  452. for i := 0; i < len(ks); i++ {
  453. keyPath := make([]byte, t.hashFunction.Len())
  454. copy(keyPath[:], ks[i])
  455. kvs[i].pos = i
  456. kvs[i].keyPath = keyPath
  457. kvs[i].k = ks[i]
  458. kvs[i].v = vs[i]
  459. }
  460. return kvs, nil
  461. }
  462. /*
  463. func (t *Tree) kvsToKeysValues(kvs []kv) ([][]byte, [][]byte) {
  464. ks := make([][]byte, len(kvs))
  465. vs := make([][]byte, len(kvs))
  466. for i := 0; i < len(kvs); i++ {
  467. ks[i] = kvs[i].k
  468. vs[i] = kvs[i].v
  469. }
  470. return ks, vs
  471. }
  472. */
  473. // buildTreeFromLeafs splits the key-values into n Buckets (where n is the number
  474. // of CPUs), in parallel builds a subtree for each bucket, once all the subtrees
  475. // are built, uses the subtrees roots as keys for a new tree, which as result
  476. // will have the complete Tree build from bottom to up, where until the
  477. // log2(nCPU) level it has been computed in parallel.
  478. func (t *Tree) buildTreeFromLeafs(nCPU int, kvs []kv) ([]int, error) {
  479. l := int(math.Log2(float64(nCPU)))
  480. buckets := splitInBuckets(kvs, nCPU)
  481. subRoots := make([][]byte, nCPU)
  482. invalidsInBucket := make([][]int, nCPU)
  483. dbgStatsPerBucket := make([]*dbgStats, nCPU)
  484. txs := make([]db.Tx, nCPU)
  485. var wg sync.WaitGroup
  486. wg.Add(nCPU)
  487. for i := 0; i < nCPU; i++ {
  488. go func(cpu int) {
  489. sortKvs(buckets[cpu])
  490. var err error
  491. txs[cpu], err = t.db.NewTx()
  492. if err != nil {
  493. panic(err) // TODO
  494. }
  495. if err := txs[cpu].Add(t.tx); err != nil {
  496. panic(err) // TODO
  497. }
  498. bucketTree := Tree{tx: txs[cpu], db: t.db, maxLevels: t.maxLevels,
  499. hashFunction: t.hashFunction, root: t.emptyHash,
  500. emptyHash: t.emptyHash, dbg: newDbgStats()}
  501. currInvalids, err := bucketTree.buildTreeFromLeafsSingleThread(l, buckets[cpu])
  502. if err != nil {
  503. panic(err) // TODO
  504. }
  505. invalidsInBucket[cpu] = currInvalids
  506. subRoots[cpu] = bucketTree.root
  507. dbgStatsPerBucket[cpu] = bucketTree.dbg
  508. wg.Done()
  509. }(i)
  510. }
  511. wg.Wait()
  512. // merge buckets txs into Tree.tx
  513. for i := 0; i < len(txs); i++ {
  514. if err := t.tx.Add(txs[i]); err != nil {
  515. return nil, err
  516. }
  517. }
  518. newRoot, err := t.upFromKeys(subRoots)
  519. if err != nil {
  520. return nil, err
  521. }
  522. t.root = newRoot
  523. var invalids []int
  524. for i := 0; i < len(invalidsInBucket); i++ {
  525. invalids = append(invalids, invalidsInBucket[i]...)
  526. }
  527. for i := 0; i < len(dbgStatsPerBucket); i++ {
  528. t.dbg.add(dbgStatsPerBucket[i])
  529. }
  530. return invalids, err
  531. }
  532. // buildTreeFromLeafsSingleThread builds the tree with the given []kv from bottom
  533. // to the root
  534. func (t *Tree) buildTreeFromLeafsSingleThread(l int, kvsRaw []kv) ([]int, error) {
  535. // TODO check that log2(len(leafs)) < t.maxLevels, if not, maxLevels
  536. // would be reached and should return error
  537. if len(kvsRaw) == 0 {
  538. return nil, nil
  539. }
  540. vt := newVT(t.maxLevels, t.hashFunction)
  541. if t.dbg != nil {
  542. vt.params.dbg = newDbgStats()
  543. }
  544. for i := 0; i < len(kvsRaw); i++ {
  545. if err := vt.add(l, kvsRaw[i].k, kvsRaw[i].v); err != nil {
  546. return nil, err
  547. }
  548. }
  549. pairs, err := vt.computeHashes()
  550. if err != nil {
  551. return nil, err
  552. }
  553. // store pairs in db
  554. for i := 0; i < len(pairs); i++ {
  555. if err := t.dbPut(pairs[i][0], pairs[i][1]); err != nil {
  556. return nil, err
  557. }
  558. }
  559. t.dbg.add(vt.params.dbg)
  560. // set tree.root from the virtual tree root
  561. t.root = vt.root.h
  562. return nil, nil // TODO invalids
  563. }
  564. // keys & values must be sorted by path, and the array ks must be length
  565. // multiple of 2
  566. func (t *Tree) upFromKeys(ks [][]byte) ([]byte, error) {
  567. if len(ks) == 1 {
  568. return ks[0], nil
  569. }
  570. var rKs [][]byte
  571. for i := 0; i < len(ks); i += 2 {
  572. if bytes.Equal(ks[i], t.emptyHash) && bytes.Equal(ks[i+1], t.emptyHash) {
  573. // when both sub keys are empty, the key is also empty
  574. rKs = append(rKs, t.emptyHash)
  575. continue
  576. }
  577. k, v, err := newIntermediate(t.hashFunction, ks[i], ks[i+1])
  578. if err != nil {
  579. return nil, err
  580. }
  581. // store k-v to db
  582. if err = t.dbPut(k, v); err != nil {
  583. return nil, err
  584. }
  585. rKs = append(rKs, k)
  586. }
  587. return t.upFromKeys(rKs)
  588. }
  589. func (t *Tree) getLeafs(root []byte) ([][]byte, [][]byte, error) {
  590. var ks, vs [][]byte
  591. err := t.iter(root, func(k, v []byte) {
  592. if v[0] != PrefixValueLeaf {
  593. return
  594. }
  595. leafK, leafV := ReadLeafValue(v)
  596. ks = append(ks, leafK)
  597. vs = append(vs, leafV)
  598. })
  599. return ks, vs, err
  600. }
  601. func (t *Tree) getKeysAtLevel(l int) ([][]byte, error) {
  602. var keys [][]byte
  603. err := t.iterWithStop(t.root, 0, func(currLvl int, k, v []byte) bool {
  604. if currLvl == l && !bytes.Equal(k, t.emptyHash) {
  605. keys = append(keys, k)
  606. }
  607. if currLvl >= l {
  608. return true // to stop the iter from going down
  609. }
  610. return false
  611. })
  612. return keys, err
  613. }
  614. // flp2 computes the floor power of 2, the highest power of 2 under the given
  615. // value.
  616. func flp2(n int) int {
  617. res := 0
  618. for i := n; i >= 1; i-- {
  619. if (i & (i - 1)) == 0 {
  620. res = i
  621. break
  622. }
  623. }
  624. return res
  625. }
  626. // combineInKVSet combines two kv array in one single array without repeated
  627. // keys.
  628. func combineInKVSet(base, toAdd []kv) ([]kv, []int) {
  629. // TODO this is a naive version, this will be implemented in a more
  630. // efficient way or through maps, or through sorted binary search
  631. r := base
  632. var invalids []int
  633. for i := 0; i < len(toAdd); i++ {
  634. e := false
  635. // check if toAdd[i] exists in the base set
  636. for j := 0; j < len(base); j++ {
  637. if bytes.Equal(toAdd[i].k, base[j].k) {
  638. e = true
  639. }
  640. }
  641. if !e {
  642. r = append(r, toAdd[i])
  643. } else {
  644. invalids = append(invalids, toAdd[i].pos)
  645. }
  646. }
  647. return r, invalids
  648. }
  649. // loadVT loads a new virtual tree (vt) from the current Tree, which contains
  650. // the same leafs.
  651. // func (t *Tree) loadVT() (vt, error) {
  652. // vt := newVT(t.maxLevels, t.hashFunction)
  653. // vt.params.dbg = t.dbg
  654. // err := t.Iterate(func(k, v []byte) {
  655. // switch v[0] {
  656. // case PrefixValueEmpty:
  657. // case PrefixValueLeaf:
  658. // leafK, leafV := ReadLeafValue(v)
  659. // if err := vt.add(0, leafK, leafV); err != nil {
  660. // panic(err)
  661. // }
  662. // case PrefixValueIntermediate:
  663. // default:
  664. // }
  665. // })
  666. //
  667. // return vt, err
  668. // }
  669. // func computeSimpleAddCost(nLeafs int) int {
  670. // // nLvls 2^nLvls
  671. // nLvls := int(math.Log2(float64(nLeafs)))
  672. // return nLvls * int(math.Pow(2, float64(nLvls)))
  673. // }
  674. //
  675. // func computeFromLeafsAddCost(nLeafs int) int {
  676. // // 2^nLvls * 2 - 1
  677. // nLvls := int(math.Log2(float64(nLeafs)))
  678. // return (int(math.Pow(2, float64(nLvls))) * 2) - 1
  679. // }