iterator_test.go 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046
  1. // Copyright 2019 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 snapshot
  17. import (
  18. "bytes"
  19. "encoding/binary"
  20. "fmt"
  21. "math/rand"
  22. "testing"
  23. "github.com/VictoriaMetrics/fastcache"
  24. "github.com/ethereum/go-ethereum/common"
  25. "github.com/ethereum/go-ethereum/core/rawdb"
  26. )
  27. // TestAccountIteratorBasics tests some simple single-layer(diff and disk) iteration
  28. func TestAccountIteratorBasics(t *testing.T) {
  29. var (
  30. destructs = make(map[common.Hash]struct{})
  31. accounts = make(map[common.Hash][]byte)
  32. storage = make(map[common.Hash]map[common.Hash][]byte)
  33. )
  34. // Fill up a parent
  35. for i := 0; i < 100; i++ {
  36. h := randomHash()
  37. data := randomAccount()
  38. accounts[h] = data
  39. if rand.Intn(4) == 0 {
  40. destructs[h] = struct{}{}
  41. }
  42. if rand.Intn(2) == 0 {
  43. accStorage := make(map[common.Hash][]byte)
  44. value := make([]byte, 32)
  45. rand.Read(value)
  46. accStorage[randomHash()] = value
  47. storage[h] = accStorage
  48. }
  49. }
  50. // Add some (identical) layers on top
  51. diffLayer := newDiffLayer(emptyLayer(), common.Hash{}, copyDestructs(destructs), copyAccounts(accounts), copyStorage(storage))
  52. it := diffLayer.AccountIterator(common.Hash{})
  53. verifyIterator(t, 100, it, verifyNothing) // Nil is allowed for single layer iterator
  54. diskLayer := diffToDisk(diffLayer)
  55. it = diskLayer.AccountIterator(common.Hash{})
  56. verifyIterator(t, 100, it, verifyNothing) // Nil is allowed for single layer iterator
  57. }
  58. // TestStorageIteratorBasics tests some simple single-layer(diff and disk) iteration for storage
  59. func TestStorageIteratorBasics(t *testing.T) {
  60. var (
  61. nilStorage = make(map[common.Hash]int)
  62. accounts = make(map[common.Hash][]byte)
  63. storage = make(map[common.Hash]map[common.Hash][]byte)
  64. )
  65. // Fill some random data
  66. for i := 0; i < 10; i++ {
  67. h := randomHash()
  68. accounts[h] = randomAccount()
  69. accStorage := make(map[common.Hash][]byte)
  70. value := make([]byte, 32)
  71. var nilstorage int
  72. for i := 0; i < 100; i++ {
  73. rand.Read(value)
  74. if rand.Intn(2) == 0 {
  75. accStorage[randomHash()] = common.CopyBytes(value)
  76. } else {
  77. accStorage[randomHash()] = nil // delete slot
  78. nilstorage += 1
  79. }
  80. }
  81. storage[h] = accStorage
  82. nilStorage[h] = nilstorage
  83. }
  84. // Add some (identical) layers on top
  85. diffLayer := newDiffLayer(emptyLayer(), common.Hash{}, nil, copyAccounts(accounts), copyStorage(storage))
  86. for account := range accounts {
  87. it, _ := diffLayer.StorageIterator(account, common.Hash{})
  88. verifyIterator(t, 100, it, verifyNothing) // Nil is allowed for single layer iterator
  89. }
  90. diskLayer := diffToDisk(diffLayer)
  91. for account := range accounts {
  92. it, _ := diskLayer.StorageIterator(account, common.Hash{})
  93. verifyIterator(t, 100-nilStorage[account], it, verifyNothing) // Nil is allowed for single layer iterator
  94. }
  95. }
  96. type testIterator struct {
  97. values []byte
  98. }
  99. func newTestIterator(values ...byte) *testIterator {
  100. return &testIterator{values}
  101. }
  102. func (ti *testIterator) Seek(common.Hash) {
  103. panic("implement me")
  104. }
  105. func (ti *testIterator) Next() bool {
  106. ti.values = ti.values[1:]
  107. return len(ti.values) > 0
  108. }
  109. func (ti *testIterator) Error() error {
  110. return nil
  111. }
  112. func (ti *testIterator) Hash() common.Hash {
  113. return common.BytesToHash([]byte{ti.values[0]})
  114. }
  115. func (ti *testIterator) Account() []byte {
  116. return nil
  117. }
  118. func (ti *testIterator) Slot() []byte {
  119. return nil
  120. }
  121. func (ti *testIterator) Release() {}
  122. func TestFastIteratorBasics(t *testing.T) {
  123. type testCase struct {
  124. lists [][]byte
  125. expKeys []byte
  126. }
  127. for i, tc := range []testCase{
  128. {lists: [][]byte{{0, 1, 8}, {1, 2, 8}, {2, 9}, {4},
  129. {7, 14, 15}, {9, 13, 15, 16}},
  130. expKeys: []byte{0, 1, 2, 4, 7, 8, 9, 13, 14, 15, 16}},
  131. {lists: [][]byte{{0, 8}, {1, 2, 8}, {7, 14, 15}, {8, 9},
  132. {9, 10}, {10, 13, 15, 16}},
  133. expKeys: []byte{0, 1, 2, 7, 8, 9, 10, 13, 14, 15, 16}},
  134. } {
  135. var iterators []*weightedIterator
  136. for i, data := range tc.lists {
  137. it := newTestIterator(data...)
  138. iterators = append(iterators, &weightedIterator{it, i})
  139. }
  140. fi := &fastIterator{
  141. iterators: iterators,
  142. initiated: false,
  143. }
  144. count := 0
  145. for fi.Next() {
  146. if got, exp := fi.Hash()[31], tc.expKeys[count]; exp != got {
  147. t.Errorf("tc %d, [%d]: got %d exp %d", i, count, got, exp)
  148. }
  149. count++
  150. }
  151. }
  152. }
  153. type verifyContent int
  154. const (
  155. verifyNothing verifyContent = iota
  156. verifyAccount
  157. verifyStorage
  158. )
  159. func verifyIterator(t *testing.T, expCount int, it Iterator, verify verifyContent) {
  160. t.Helper()
  161. var (
  162. count = 0
  163. last = common.Hash{}
  164. )
  165. for it.Next() {
  166. hash := it.Hash()
  167. if bytes.Compare(last[:], hash[:]) >= 0 {
  168. t.Errorf("wrong order: %x >= %x", last, hash)
  169. }
  170. count++
  171. if verify == verifyAccount && len(it.(AccountIterator).Account()) == 0 {
  172. t.Errorf("iterator returned nil-value for hash %x", hash)
  173. } else if verify == verifyStorage && len(it.(StorageIterator).Slot()) == 0 {
  174. t.Errorf("iterator returned nil-value for hash %x", hash)
  175. }
  176. last = hash
  177. }
  178. if count != expCount {
  179. t.Errorf("iterator count mismatch: have %d, want %d", count, expCount)
  180. }
  181. if err := it.Error(); err != nil {
  182. t.Errorf("iterator failed: %v", err)
  183. }
  184. }
  185. // TestAccountIteratorTraversal tests some simple multi-layer iteration.
  186. func TestAccountIteratorTraversal(t *testing.T) {
  187. // Create an empty base layer and a snapshot tree out of it
  188. base := &diskLayer{
  189. diskdb: rawdb.NewMemoryDatabase(),
  190. root: common.HexToHash("0x01"),
  191. cache: fastcache.New(1024 * 500),
  192. }
  193. snaps := &Tree{
  194. layers: map[common.Hash]snapshot{
  195. base.root: base,
  196. },
  197. }
  198. // Stack three diff layers on top with various overlaps
  199. snaps.Update(common.HexToHash("0x02"), common.HexToHash("0x01"), nil,
  200. randomAccountSet("0xaa", "0xee", "0xff", "0xf0"), nil)
  201. snaps.Update(common.HexToHash("0x03"), common.HexToHash("0x02"), nil,
  202. randomAccountSet("0xbb", "0xdd", "0xf0"), nil)
  203. snaps.Update(common.HexToHash("0x04"), common.HexToHash("0x03"), nil,
  204. randomAccountSet("0xcc", "0xf0", "0xff"), nil)
  205. // Verify the single and multi-layer iterators
  206. head := snaps.Snapshot(common.HexToHash("0x04"))
  207. verifyIterator(t, 3, head.(snapshot).AccountIterator(common.Hash{}), verifyNothing)
  208. verifyIterator(t, 7, head.(*diffLayer).newBinaryAccountIterator(), verifyAccount)
  209. it, _ := snaps.AccountIterator(common.HexToHash("0x04"), common.Hash{})
  210. verifyIterator(t, 7, it, verifyAccount)
  211. it.Release()
  212. // Test after persist some bottom-most layers into the disk,
  213. // the functionalities still work.
  214. limit := aggregatorMemoryLimit
  215. defer func() {
  216. aggregatorMemoryLimit = limit
  217. }()
  218. aggregatorMemoryLimit = 0 // Force pushing the bottom-most layer into disk
  219. snaps.Cap(common.HexToHash("0x04"), 2)
  220. verifyIterator(t, 7, head.(*diffLayer).newBinaryAccountIterator(), verifyAccount)
  221. it, _ = snaps.AccountIterator(common.HexToHash("0x04"), common.Hash{})
  222. verifyIterator(t, 7, it, verifyAccount)
  223. it.Release()
  224. }
  225. func TestStorageIteratorTraversal(t *testing.T) {
  226. // Create an empty base layer and a snapshot tree out of it
  227. base := &diskLayer{
  228. diskdb: rawdb.NewMemoryDatabase(),
  229. root: common.HexToHash("0x01"),
  230. cache: fastcache.New(1024 * 500),
  231. }
  232. snaps := &Tree{
  233. layers: map[common.Hash]snapshot{
  234. base.root: base,
  235. },
  236. }
  237. // Stack three diff layers on top with various overlaps
  238. snaps.Update(common.HexToHash("0x02"), common.HexToHash("0x01"), nil,
  239. randomAccountSet("0xaa"), randomStorageSet([]string{"0xaa"}, [][]string{{"0x01", "0x02", "0x03"}}, nil))
  240. snaps.Update(common.HexToHash("0x03"), common.HexToHash("0x02"), nil,
  241. randomAccountSet("0xaa"), randomStorageSet([]string{"0xaa"}, [][]string{{"0x04", "0x05", "0x06"}}, nil))
  242. snaps.Update(common.HexToHash("0x04"), common.HexToHash("0x03"), nil,
  243. randomAccountSet("0xaa"), randomStorageSet([]string{"0xaa"}, [][]string{{"0x01", "0x02", "0x03"}}, nil))
  244. // Verify the single and multi-layer iterators
  245. head := snaps.Snapshot(common.HexToHash("0x04"))
  246. diffIter, _ := head.(snapshot).StorageIterator(common.HexToHash("0xaa"), common.Hash{})
  247. verifyIterator(t, 3, diffIter, verifyNothing)
  248. verifyIterator(t, 6, head.(*diffLayer).newBinaryStorageIterator(common.HexToHash("0xaa")), verifyStorage)
  249. it, _ := snaps.StorageIterator(common.HexToHash("0x04"), common.HexToHash("0xaa"), common.Hash{})
  250. verifyIterator(t, 6, it, verifyStorage)
  251. it.Release()
  252. // Test after persist some bottom-most layers into the disk,
  253. // the functionalities still work.
  254. limit := aggregatorMemoryLimit
  255. defer func() {
  256. aggregatorMemoryLimit = limit
  257. }()
  258. aggregatorMemoryLimit = 0 // Force pushing the bottom-most layer into disk
  259. snaps.Cap(common.HexToHash("0x04"), 2)
  260. verifyIterator(t, 6, head.(*diffLayer).newBinaryStorageIterator(common.HexToHash("0xaa")), verifyStorage)
  261. it, _ = snaps.StorageIterator(common.HexToHash("0x04"), common.HexToHash("0xaa"), common.Hash{})
  262. verifyIterator(t, 6, it, verifyStorage)
  263. it.Release()
  264. }
  265. // TestAccountIteratorTraversalValues tests some multi-layer iteration, where we
  266. // also expect the correct values to show up.
  267. func TestAccountIteratorTraversalValues(t *testing.T) {
  268. // Create an empty base layer and a snapshot tree out of it
  269. base := &diskLayer{
  270. diskdb: rawdb.NewMemoryDatabase(),
  271. root: common.HexToHash("0x01"),
  272. cache: fastcache.New(1024 * 500),
  273. }
  274. snaps := &Tree{
  275. layers: map[common.Hash]snapshot{
  276. base.root: base,
  277. },
  278. }
  279. // Create a batch of account sets to seed subsequent layers with
  280. var (
  281. a = make(map[common.Hash][]byte)
  282. b = make(map[common.Hash][]byte)
  283. c = make(map[common.Hash][]byte)
  284. d = make(map[common.Hash][]byte)
  285. e = make(map[common.Hash][]byte)
  286. f = make(map[common.Hash][]byte)
  287. g = make(map[common.Hash][]byte)
  288. h = make(map[common.Hash][]byte)
  289. )
  290. for i := byte(2); i < 0xff; i++ {
  291. a[common.Hash{i}] = []byte(fmt.Sprintf("layer-%d, key %d", 0, i))
  292. if i > 20 && i%2 == 0 {
  293. b[common.Hash{i}] = []byte(fmt.Sprintf("layer-%d, key %d", 1, i))
  294. }
  295. if i%4 == 0 {
  296. c[common.Hash{i}] = []byte(fmt.Sprintf("layer-%d, key %d", 2, i))
  297. }
  298. if i%7 == 0 {
  299. d[common.Hash{i}] = []byte(fmt.Sprintf("layer-%d, key %d", 3, i))
  300. }
  301. if i%8 == 0 {
  302. e[common.Hash{i}] = []byte(fmt.Sprintf("layer-%d, key %d", 4, i))
  303. }
  304. if i > 50 || i < 85 {
  305. f[common.Hash{i}] = []byte(fmt.Sprintf("layer-%d, key %d", 5, i))
  306. }
  307. if i%64 == 0 {
  308. g[common.Hash{i}] = []byte(fmt.Sprintf("layer-%d, key %d", 6, i))
  309. }
  310. if i%128 == 0 {
  311. h[common.Hash{i}] = []byte(fmt.Sprintf("layer-%d, key %d", 7, i))
  312. }
  313. }
  314. // Assemble a stack of snapshots from the account layers
  315. snaps.Update(common.HexToHash("0x02"), common.HexToHash("0x01"), nil, a, nil)
  316. snaps.Update(common.HexToHash("0x03"), common.HexToHash("0x02"), nil, b, nil)
  317. snaps.Update(common.HexToHash("0x04"), common.HexToHash("0x03"), nil, c, nil)
  318. snaps.Update(common.HexToHash("0x05"), common.HexToHash("0x04"), nil, d, nil)
  319. snaps.Update(common.HexToHash("0x06"), common.HexToHash("0x05"), nil, e, nil)
  320. snaps.Update(common.HexToHash("0x07"), common.HexToHash("0x06"), nil, f, nil)
  321. snaps.Update(common.HexToHash("0x08"), common.HexToHash("0x07"), nil, g, nil)
  322. snaps.Update(common.HexToHash("0x09"), common.HexToHash("0x08"), nil, h, nil)
  323. it, _ := snaps.AccountIterator(common.HexToHash("0x09"), common.Hash{})
  324. head := snaps.Snapshot(common.HexToHash("0x09"))
  325. for it.Next() {
  326. hash := it.Hash()
  327. want, err := head.AccountRLP(hash)
  328. if err != nil {
  329. t.Fatalf("failed to retrieve expected account: %v", err)
  330. }
  331. if have := it.Account(); !bytes.Equal(want, have) {
  332. t.Fatalf("hash %x: account mismatch: have %x, want %x", hash, have, want)
  333. }
  334. }
  335. it.Release()
  336. // Test after persist some bottom-most layers into the disk,
  337. // the functionalities still work.
  338. limit := aggregatorMemoryLimit
  339. defer func() {
  340. aggregatorMemoryLimit = limit
  341. }()
  342. aggregatorMemoryLimit = 0 // Force pushing the bottom-most layer into disk
  343. snaps.Cap(common.HexToHash("0x09"), 2)
  344. it, _ = snaps.AccountIterator(common.HexToHash("0x09"), common.Hash{})
  345. for it.Next() {
  346. hash := it.Hash()
  347. want, err := head.AccountRLP(hash)
  348. if err != nil {
  349. t.Fatalf("failed to retrieve expected account: %v", err)
  350. }
  351. if have := it.Account(); !bytes.Equal(want, have) {
  352. t.Fatalf("hash %x: account mismatch: have %x, want %x", hash, have, want)
  353. }
  354. }
  355. it.Release()
  356. }
  357. func TestStorageIteratorTraversalValues(t *testing.T) {
  358. // Create an empty base layer and a snapshot tree out of it
  359. base := &diskLayer{
  360. diskdb: rawdb.NewMemoryDatabase(),
  361. root: common.HexToHash("0x01"),
  362. cache: fastcache.New(1024 * 500),
  363. }
  364. snaps := &Tree{
  365. layers: map[common.Hash]snapshot{
  366. base.root: base,
  367. },
  368. }
  369. wrapStorage := func(storage map[common.Hash][]byte) map[common.Hash]map[common.Hash][]byte {
  370. return map[common.Hash]map[common.Hash][]byte{
  371. common.HexToHash("0xaa"): storage,
  372. }
  373. }
  374. // Create a batch of storage sets to seed subsequent layers with
  375. var (
  376. a = make(map[common.Hash][]byte)
  377. b = make(map[common.Hash][]byte)
  378. c = make(map[common.Hash][]byte)
  379. d = make(map[common.Hash][]byte)
  380. e = make(map[common.Hash][]byte)
  381. f = make(map[common.Hash][]byte)
  382. g = make(map[common.Hash][]byte)
  383. h = make(map[common.Hash][]byte)
  384. )
  385. for i := byte(2); i < 0xff; i++ {
  386. a[common.Hash{i}] = []byte(fmt.Sprintf("layer-%d, key %d", 0, i))
  387. if i > 20 && i%2 == 0 {
  388. b[common.Hash{i}] = []byte(fmt.Sprintf("layer-%d, key %d", 1, i))
  389. }
  390. if i%4 == 0 {
  391. c[common.Hash{i}] = []byte(fmt.Sprintf("layer-%d, key %d", 2, i))
  392. }
  393. if i%7 == 0 {
  394. d[common.Hash{i}] = []byte(fmt.Sprintf("layer-%d, key %d", 3, i))
  395. }
  396. if i%8 == 0 {
  397. e[common.Hash{i}] = []byte(fmt.Sprintf("layer-%d, key %d", 4, i))
  398. }
  399. if i > 50 || i < 85 {
  400. f[common.Hash{i}] = []byte(fmt.Sprintf("layer-%d, key %d", 5, i))
  401. }
  402. if i%64 == 0 {
  403. g[common.Hash{i}] = []byte(fmt.Sprintf("layer-%d, key %d", 6, i))
  404. }
  405. if i%128 == 0 {
  406. h[common.Hash{i}] = []byte(fmt.Sprintf("layer-%d, key %d", 7, i))
  407. }
  408. }
  409. // Assemble a stack of snapshots from the account layers
  410. snaps.Update(common.HexToHash("0x02"), common.HexToHash("0x01"), nil, randomAccountSet("0xaa"), wrapStorage(a))
  411. snaps.Update(common.HexToHash("0x03"), common.HexToHash("0x02"), nil, randomAccountSet("0xaa"), wrapStorage(b))
  412. snaps.Update(common.HexToHash("0x04"), common.HexToHash("0x03"), nil, randomAccountSet("0xaa"), wrapStorage(c))
  413. snaps.Update(common.HexToHash("0x05"), common.HexToHash("0x04"), nil, randomAccountSet("0xaa"), wrapStorage(d))
  414. snaps.Update(common.HexToHash("0x06"), common.HexToHash("0x05"), nil, randomAccountSet("0xaa"), wrapStorage(e))
  415. snaps.Update(common.HexToHash("0x07"), common.HexToHash("0x06"), nil, randomAccountSet("0xaa"), wrapStorage(e))
  416. snaps.Update(common.HexToHash("0x08"), common.HexToHash("0x07"), nil, randomAccountSet("0xaa"), wrapStorage(g))
  417. snaps.Update(common.HexToHash("0x09"), common.HexToHash("0x08"), nil, randomAccountSet("0xaa"), wrapStorage(h))
  418. it, _ := snaps.StorageIterator(common.HexToHash("0x09"), common.HexToHash("0xaa"), common.Hash{})
  419. head := snaps.Snapshot(common.HexToHash("0x09"))
  420. for it.Next() {
  421. hash := it.Hash()
  422. want, err := head.Storage(common.HexToHash("0xaa"), hash)
  423. if err != nil {
  424. t.Fatalf("failed to retrieve expected storage slot: %v", err)
  425. }
  426. if have := it.Slot(); !bytes.Equal(want, have) {
  427. t.Fatalf("hash %x: slot mismatch: have %x, want %x", hash, have, want)
  428. }
  429. }
  430. it.Release()
  431. // Test after persist some bottom-most layers into the disk,
  432. // the functionalities still work.
  433. limit := aggregatorMemoryLimit
  434. defer func() {
  435. aggregatorMemoryLimit = limit
  436. }()
  437. aggregatorMemoryLimit = 0 // Force pushing the bottom-most layer into disk
  438. snaps.Cap(common.HexToHash("0x09"), 2)
  439. it, _ = snaps.StorageIterator(common.HexToHash("0x09"), common.HexToHash("0xaa"), common.Hash{})
  440. for it.Next() {
  441. hash := it.Hash()
  442. want, err := head.Storage(common.HexToHash("0xaa"), hash)
  443. if err != nil {
  444. t.Fatalf("failed to retrieve expected slot: %v", err)
  445. }
  446. if have := it.Slot(); !bytes.Equal(want, have) {
  447. t.Fatalf("hash %x: slot mismatch: have %x, want %x", hash, have, want)
  448. }
  449. }
  450. it.Release()
  451. }
  452. // This testcase is notorious, all layers contain the exact same 200 accounts.
  453. func TestAccountIteratorLargeTraversal(t *testing.T) {
  454. // Create a custom account factory to recreate the same addresses
  455. makeAccounts := func(num int) map[common.Hash][]byte {
  456. accounts := make(map[common.Hash][]byte)
  457. for i := 0; i < num; i++ {
  458. h := common.Hash{}
  459. binary.BigEndian.PutUint64(h[:], uint64(i+1))
  460. accounts[h] = randomAccount()
  461. }
  462. return accounts
  463. }
  464. // Build up a large stack of snapshots
  465. base := &diskLayer{
  466. diskdb: rawdb.NewMemoryDatabase(),
  467. root: common.HexToHash("0x01"),
  468. cache: fastcache.New(1024 * 500),
  469. }
  470. snaps := &Tree{
  471. layers: map[common.Hash]snapshot{
  472. base.root: base,
  473. },
  474. }
  475. for i := 1; i < 128; i++ {
  476. snaps.Update(common.HexToHash(fmt.Sprintf("0x%02x", i+1)), common.HexToHash(fmt.Sprintf("0x%02x", i)), nil, makeAccounts(200), nil)
  477. }
  478. // Iterate the entire stack and ensure everything is hit only once
  479. head := snaps.Snapshot(common.HexToHash("0x80"))
  480. verifyIterator(t, 200, head.(snapshot).AccountIterator(common.Hash{}), verifyNothing)
  481. verifyIterator(t, 200, head.(*diffLayer).newBinaryAccountIterator(), verifyAccount)
  482. it, _ := snaps.AccountIterator(common.HexToHash("0x80"), common.Hash{})
  483. verifyIterator(t, 200, it, verifyAccount)
  484. it.Release()
  485. // Test after persist some bottom-most layers into the disk,
  486. // the functionalities still work.
  487. limit := aggregatorMemoryLimit
  488. defer func() {
  489. aggregatorMemoryLimit = limit
  490. }()
  491. aggregatorMemoryLimit = 0 // Force pushing the bottom-most layer into disk
  492. snaps.Cap(common.HexToHash("0x80"), 2)
  493. verifyIterator(t, 200, head.(*diffLayer).newBinaryAccountIterator(), verifyAccount)
  494. it, _ = snaps.AccountIterator(common.HexToHash("0x80"), common.Hash{})
  495. verifyIterator(t, 200, it, verifyAccount)
  496. it.Release()
  497. }
  498. // TestAccountIteratorFlattening tests what happens when we
  499. // - have a live iterator on child C (parent C1 -> C2 .. CN)
  500. // - flattens C2 all the way into CN
  501. // - continues iterating
  502. func TestAccountIteratorFlattening(t *testing.T) {
  503. // Create an empty base layer and a snapshot tree out of it
  504. base := &diskLayer{
  505. diskdb: rawdb.NewMemoryDatabase(),
  506. root: common.HexToHash("0x01"),
  507. cache: fastcache.New(1024 * 500),
  508. }
  509. snaps := &Tree{
  510. layers: map[common.Hash]snapshot{
  511. base.root: base,
  512. },
  513. }
  514. // Create a stack of diffs on top
  515. snaps.Update(common.HexToHash("0x02"), common.HexToHash("0x01"), nil,
  516. randomAccountSet("0xaa", "0xee", "0xff", "0xf0"), nil)
  517. snaps.Update(common.HexToHash("0x03"), common.HexToHash("0x02"), nil,
  518. randomAccountSet("0xbb", "0xdd", "0xf0"), nil)
  519. snaps.Update(common.HexToHash("0x04"), common.HexToHash("0x03"), nil,
  520. randomAccountSet("0xcc", "0xf0", "0xff"), nil)
  521. // Create an iterator and flatten the data from underneath it
  522. it, _ := snaps.AccountIterator(common.HexToHash("0x04"), common.Hash{})
  523. defer it.Release()
  524. if err := snaps.Cap(common.HexToHash("0x04"), 1); err != nil {
  525. t.Fatalf("failed to flatten snapshot stack: %v", err)
  526. }
  527. //verifyIterator(t, 7, it)
  528. }
  529. func TestAccountIteratorSeek(t *testing.T) {
  530. // Create a snapshot stack with some initial data
  531. base := &diskLayer{
  532. diskdb: rawdb.NewMemoryDatabase(),
  533. root: common.HexToHash("0x01"),
  534. cache: fastcache.New(1024 * 500),
  535. }
  536. snaps := &Tree{
  537. layers: map[common.Hash]snapshot{
  538. base.root: base,
  539. },
  540. }
  541. snaps.Update(common.HexToHash("0x02"), common.HexToHash("0x01"), nil,
  542. randomAccountSet("0xaa", "0xee", "0xff", "0xf0"), nil)
  543. snaps.Update(common.HexToHash("0x03"), common.HexToHash("0x02"), nil,
  544. randomAccountSet("0xbb", "0xdd", "0xf0"), nil)
  545. snaps.Update(common.HexToHash("0x04"), common.HexToHash("0x03"), nil,
  546. randomAccountSet("0xcc", "0xf0", "0xff"), nil)
  547. // Account set is now
  548. // 02: aa, ee, f0, ff
  549. // 03: aa, bb, dd, ee, f0 (, f0), ff
  550. // 04: aa, bb, cc, dd, ee, f0 (, f0), ff (, ff)
  551. // Construct various iterators and ensure their traversal is correct
  552. it, _ := snaps.AccountIterator(common.HexToHash("0x02"), common.HexToHash("0xdd"))
  553. defer it.Release()
  554. verifyIterator(t, 3, it, verifyAccount) // expected: ee, f0, ff
  555. it, _ = snaps.AccountIterator(common.HexToHash("0x02"), common.HexToHash("0xaa"))
  556. defer it.Release()
  557. verifyIterator(t, 4, it, verifyAccount) // expected: aa, ee, f0, ff
  558. it, _ = snaps.AccountIterator(common.HexToHash("0x02"), common.HexToHash("0xff"))
  559. defer it.Release()
  560. verifyIterator(t, 1, it, verifyAccount) // expected: ff
  561. it, _ = snaps.AccountIterator(common.HexToHash("0x02"), common.HexToHash("0xff1"))
  562. defer it.Release()
  563. verifyIterator(t, 0, it, verifyAccount) // expected: nothing
  564. it, _ = snaps.AccountIterator(common.HexToHash("0x04"), common.HexToHash("0xbb"))
  565. defer it.Release()
  566. verifyIterator(t, 6, it, verifyAccount) // expected: bb, cc, dd, ee, f0, ff
  567. it, _ = snaps.AccountIterator(common.HexToHash("0x04"), common.HexToHash("0xef"))
  568. defer it.Release()
  569. verifyIterator(t, 2, it, verifyAccount) // expected: f0, ff
  570. it, _ = snaps.AccountIterator(common.HexToHash("0x04"), common.HexToHash("0xf0"))
  571. defer it.Release()
  572. verifyIterator(t, 2, it, verifyAccount) // expected: f0, ff
  573. it, _ = snaps.AccountIterator(common.HexToHash("0x04"), common.HexToHash("0xff"))
  574. defer it.Release()
  575. verifyIterator(t, 1, it, verifyAccount) // expected: ff
  576. it, _ = snaps.AccountIterator(common.HexToHash("0x04"), common.HexToHash("0xff1"))
  577. defer it.Release()
  578. verifyIterator(t, 0, it, verifyAccount) // expected: nothing
  579. }
  580. func TestStorageIteratorSeek(t *testing.T) {
  581. // Create a snapshot stack with some initial data
  582. base := &diskLayer{
  583. diskdb: rawdb.NewMemoryDatabase(),
  584. root: common.HexToHash("0x01"),
  585. cache: fastcache.New(1024 * 500),
  586. }
  587. snaps := &Tree{
  588. layers: map[common.Hash]snapshot{
  589. base.root: base,
  590. },
  591. }
  592. // Stack three diff layers on top with various overlaps
  593. snaps.Update(common.HexToHash("0x02"), common.HexToHash("0x01"), nil,
  594. randomAccountSet("0xaa"), randomStorageSet([]string{"0xaa"}, [][]string{{"0x01", "0x03", "0x05"}}, nil))
  595. snaps.Update(common.HexToHash("0x03"), common.HexToHash("0x02"), nil,
  596. randomAccountSet("0xaa"), randomStorageSet([]string{"0xaa"}, [][]string{{"0x02", "0x05", "0x06"}}, nil))
  597. snaps.Update(common.HexToHash("0x04"), common.HexToHash("0x03"), nil,
  598. randomAccountSet("0xaa"), randomStorageSet([]string{"0xaa"}, [][]string{{"0x01", "0x05", "0x08"}}, nil))
  599. // Account set is now
  600. // 02: 01, 03, 05
  601. // 03: 01, 02, 03, 05 (, 05), 06
  602. // 04: 01(, 01), 02, 03, 05(, 05, 05), 06, 08
  603. // Construct various iterators and ensure their traversal is correct
  604. it, _ := snaps.StorageIterator(common.HexToHash("0x02"), common.HexToHash("0xaa"), common.HexToHash("0x01"))
  605. defer it.Release()
  606. verifyIterator(t, 3, it, verifyStorage) // expected: 01, 03, 05
  607. it, _ = snaps.StorageIterator(common.HexToHash("0x02"), common.HexToHash("0xaa"), common.HexToHash("0x02"))
  608. defer it.Release()
  609. verifyIterator(t, 2, it, verifyStorage) // expected: 03, 05
  610. it, _ = snaps.StorageIterator(common.HexToHash("0x02"), common.HexToHash("0xaa"), common.HexToHash("0x5"))
  611. defer it.Release()
  612. verifyIterator(t, 1, it, verifyStorage) // expected: 05
  613. it, _ = snaps.StorageIterator(common.HexToHash("0x02"), common.HexToHash("0xaa"), common.HexToHash("0x6"))
  614. defer it.Release()
  615. verifyIterator(t, 0, it, verifyStorage) // expected: nothing
  616. it, _ = snaps.StorageIterator(common.HexToHash("0x04"), common.HexToHash("0xaa"), common.HexToHash("0x01"))
  617. defer it.Release()
  618. verifyIterator(t, 6, it, verifyStorage) // expected: 01, 02, 03, 05, 06, 08
  619. it, _ = snaps.StorageIterator(common.HexToHash("0x04"), common.HexToHash("0xaa"), common.HexToHash("0x05"))
  620. defer it.Release()
  621. verifyIterator(t, 3, it, verifyStorage) // expected: 05, 06, 08
  622. it, _ = snaps.StorageIterator(common.HexToHash("0x04"), common.HexToHash("0xaa"), common.HexToHash("0x08"))
  623. defer it.Release()
  624. verifyIterator(t, 1, it, verifyStorage) // expected: 08
  625. it, _ = snaps.StorageIterator(common.HexToHash("0x04"), common.HexToHash("0xaa"), common.HexToHash("0x09"))
  626. defer it.Release()
  627. verifyIterator(t, 0, it, verifyStorage) // expected: nothing
  628. }
  629. // TestAccountIteratorDeletions tests that the iterator behaves correct when there are
  630. // deleted accounts (where the Account() value is nil). The iterator
  631. // should not output any accounts or nil-values for those cases.
  632. func TestAccountIteratorDeletions(t *testing.T) {
  633. // Create an empty base layer and a snapshot tree out of it
  634. base := &diskLayer{
  635. diskdb: rawdb.NewMemoryDatabase(),
  636. root: common.HexToHash("0x01"),
  637. cache: fastcache.New(1024 * 500),
  638. }
  639. snaps := &Tree{
  640. layers: map[common.Hash]snapshot{
  641. base.root: base,
  642. },
  643. }
  644. // Stack three diff layers on top with various overlaps
  645. snaps.Update(common.HexToHash("0x02"), common.HexToHash("0x01"),
  646. nil, randomAccountSet("0x11", "0x22", "0x33"), nil)
  647. deleted := common.HexToHash("0x22")
  648. destructed := map[common.Hash]struct{}{
  649. deleted: {},
  650. }
  651. snaps.Update(common.HexToHash("0x03"), common.HexToHash("0x02"),
  652. destructed, randomAccountSet("0x11", "0x33"), nil)
  653. snaps.Update(common.HexToHash("0x04"), common.HexToHash("0x03"),
  654. nil, randomAccountSet("0x33", "0x44", "0x55"), nil)
  655. // The output should be 11,33,44,55
  656. it, _ := snaps.AccountIterator(common.HexToHash("0x04"), common.Hash{})
  657. // Do a quick check
  658. verifyIterator(t, 4, it, verifyAccount)
  659. it.Release()
  660. // And a more detailed verification that we indeed do not see '0x22'
  661. it, _ = snaps.AccountIterator(common.HexToHash("0x04"), common.Hash{})
  662. defer it.Release()
  663. for it.Next() {
  664. hash := it.Hash()
  665. if it.Account() == nil {
  666. t.Errorf("iterator returned nil-value for hash %x", hash)
  667. }
  668. if hash == deleted {
  669. t.Errorf("expected deleted elem %x to not be returned by iterator", deleted)
  670. }
  671. }
  672. }
  673. func TestStorageIteratorDeletions(t *testing.T) {
  674. // Create an empty base layer and a snapshot tree out of it
  675. base := &diskLayer{
  676. diskdb: rawdb.NewMemoryDatabase(),
  677. root: common.HexToHash("0x01"),
  678. cache: fastcache.New(1024 * 500),
  679. }
  680. snaps := &Tree{
  681. layers: map[common.Hash]snapshot{
  682. base.root: base,
  683. },
  684. }
  685. // Stack three diff layers on top with various overlaps
  686. snaps.Update(common.HexToHash("0x02"), common.HexToHash("0x01"), nil,
  687. randomAccountSet("0xaa"), randomStorageSet([]string{"0xaa"}, [][]string{{"0x01", "0x03", "0x05"}}, nil))
  688. snaps.Update(common.HexToHash("0x03"), common.HexToHash("0x02"), nil,
  689. randomAccountSet("0xaa"), randomStorageSet([]string{"0xaa"}, [][]string{{"0x02", "0x04", "0x06"}}, [][]string{{"0x01", "0x03"}}))
  690. // The output should be 02,04,05,06
  691. it, _ := snaps.StorageIterator(common.HexToHash("0x03"), common.HexToHash("0xaa"), common.Hash{})
  692. verifyIterator(t, 4, it, verifyStorage)
  693. it.Release()
  694. // The output should be 04,05,06
  695. it, _ = snaps.StorageIterator(common.HexToHash("0x03"), common.HexToHash("0xaa"), common.HexToHash("0x03"))
  696. verifyIterator(t, 3, it, verifyStorage)
  697. it.Release()
  698. // Destruct the whole storage
  699. destructed := map[common.Hash]struct{}{
  700. common.HexToHash("0xaa"): {},
  701. }
  702. snaps.Update(common.HexToHash("0x04"), common.HexToHash("0x03"), destructed, nil, nil)
  703. it, _ = snaps.StorageIterator(common.HexToHash("0x04"), common.HexToHash("0xaa"), common.Hash{})
  704. verifyIterator(t, 0, it, verifyStorage)
  705. it.Release()
  706. // Re-insert the slots of the same account
  707. snaps.Update(common.HexToHash("0x05"), common.HexToHash("0x04"), nil,
  708. randomAccountSet("0xaa"), randomStorageSet([]string{"0xaa"}, [][]string{{"0x07", "0x08", "0x09"}}, nil))
  709. // The output should be 07,08,09
  710. it, _ = snaps.StorageIterator(common.HexToHash("0x05"), common.HexToHash("0xaa"), common.Hash{})
  711. verifyIterator(t, 3, it, verifyStorage)
  712. it.Release()
  713. // Destruct the whole storage but re-create the account in the same layer
  714. snaps.Update(common.HexToHash("0x06"), common.HexToHash("0x05"), destructed, randomAccountSet("0xaa"), randomStorageSet([]string{"0xaa"}, [][]string{{"0x11", "0x12"}}, nil))
  715. it, _ = snaps.StorageIterator(common.HexToHash("0x06"), common.HexToHash("0xaa"), common.Hash{})
  716. verifyIterator(t, 2, it, verifyStorage) // The output should be 11,12
  717. it.Release()
  718. verifyIterator(t, 2, snaps.Snapshot(common.HexToHash("0x06")).(*diffLayer).newBinaryStorageIterator(common.HexToHash("0xaa")), verifyStorage)
  719. }
  720. // BenchmarkAccountIteratorTraversal is a bit a bit notorious -- all layers contain the
  721. // exact same 200 accounts. That means that we need to process 2000 items, but
  722. // only spit out 200 values eventually.
  723. //
  724. // The value-fetching benchmark is easy on the binary iterator, since it never has to reach
  725. // down at any depth for retrieving the values -- all are on the toppmost layer
  726. //
  727. // BenchmarkAccountIteratorTraversal/binary_iterator_keys-6 2239 483674 ns/op
  728. // BenchmarkAccountIteratorTraversal/binary_iterator_values-6 2403 501810 ns/op
  729. // BenchmarkAccountIteratorTraversal/fast_iterator_keys-6 1923 677966 ns/op
  730. // BenchmarkAccountIteratorTraversal/fast_iterator_values-6 1741 649967 ns/op
  731. func BenchmarkAccountIteratorTraversal(b *testing.B) {
  732. // Create a custom account factory to recreate the same addresses
  733. makeAccounts := func(num int) map[common.Hash][]byte {
  734. accounts := make(map[common.Hash][]byte)
  735. for i := 0; i < num; i++ {
  736. h := common.Hash{}
  737. binary.BigEndian.PutUint64(h[:], uint64(i+1))
  738. accounts[h] = randomAccount()
  739. }
  740. return accounts
  741. }
  742. // Build up a large stack of snapshots
  743. base := &diskLayer{
  744. diskdb: rawdb.NewMemoryDatabase(),
  745. root: common.HexToHash("0x01"),
  746. cache: fastcache.New(1024 * 500),
  747. }
  748. snaps := &Tree{
  749. layers: map[common.Hash]snapshot{
  750. base.root: base,
  751. },
  752. }
  753. for i := 1; i <= 100; i++ {
  754. snaps.Update(common.HexToHash(fmt.Sprintf("0x%02x", i+1)), common.HexToHash(fmt.Sprintf("0x%02x", i)), nil, makeAccounts(200), nil)
  755. }
  756. // We call this once before the benchmark, so the creation of
  757. // sorted accountlists are not included in the results.
  758. head := snaps.Snapshot(common.HexToHash("0x65"))
  759. head.(*diffLayer).newBinaryAccountIterator()
  760. b.Run("binary iterator keys", func(b *testing.B) {
  761. for i := 0; i < b.N; i++ {
  762. got := 0
  763. it := head.(*diffLayer).newBinaryAccountIterator()
  764. for it.Next() {
  765. got++
  766. }
  767. if exp := 200; got != exp {
  768. b.Errorf("iterator len wrong, expected %d, got %d", exp, got)
  769. }
  770. }
  771. })
  772. b.Run("binary iterator values", func(b *testing.B) {
  773. for i := 0; i < b.N; i++ {
  774. got := 0
  775. it := head.(*diffLayer).newBinaryAccountIterator()
  776. for it.Next() {
  777. got++
  778. head.(*diffLayer).accountRLP(it.Hash(), 0)
  779. }
  780. if exp := 200; got != exp {
  781. b.Errorf("iterator len wrong, expected %d, got %d", exp, got)
  782. }
  783. }
  784. })
  785. b.Run("fast iterator keys", func(b *testing.B) {
  786. for i := 0; i < b.N; i++ {
  787. it, _ := snaps.AccountIterator(common.HexToHash("0x65"), common.Hash{})
  788. defer it.Release()
  789. got := 0
  790. for it.Next() {
  791. got++
  792. }
  793. if exp := 200; got != exp {
  794. b.Errorf("iterator len wrong, expected %d, got %d", exp, got)
  795. }
  796. }
  797. })
  798. b.Run("fast iterator values", func(b *testing.B) {
  799. for i := 0; i < b.N; i++ {
  800. it, _ := snaps.AccountIterator(common.HexToHash("0x65"), common.Hash{})
  801. defer it.Release()
  802. got := 0
  803. for it.Next() {
  804. got++
  805. it.Account()
  806. }
  807. if exp := 200; got != exp {
  808. b.Errorf("iterator len wrong, expected %d, got %d", exp, got)
  809. }
  810. }
  811. })
  812. }
  813. // BenchmarkAccountIteratorLargeBaselayer is a pretty realistic benchmark, where
  814. // the baselayer is a lot larger than the upper layer.
  815. //
  816. // This is heavy on the binary iterator, which in most cases will have to
  817. // call recursively 100 times for the majority of the values
  818. //
  819. // BenchmarkAccountIteratorLargeBaselayer/binary_iterator_(keys)-6 514 1971999 ns/op
  820. // BenchmarkAccountIteratorLargeBaselayer/binary_iterator_(values)-6 61 18997492 ns/op
  821. // BenchmarkAccountIteratorLargeBaselayer/fast_iterator_(keys)-6 10000 114385 ns/op
  822. // BenchmarkAccountIteratorLargeBaselayer/fast_iterator_(values)-6 4047 296823 ns/op
  823. func BenchmarkAccountIteratorLargeBaselayer(b *testing.B) {
  824. // Create a custom account factory to recreate the same addresses
  825. makeAccounts := func(num int) map[common.Hash][]byte {
  826. accounts := make(map[common.Hash][]byte)
  827. for i := 0; i < num; i++ {
  828. h := common.Hash{}
  829. binary.BigEndian.PutUint64(h[:], uint64(i+1))
  830. accounts[h] = randomAccount()
  831. }
  832. return accounts
  833. }
  834. // Build up a large stack of snapshots
  835. base := &diskLayer{
  836. diskdb: rawdb.NewMemoryDatabase(),
  837. root: common.HexToHash("0x01"),
  838. cache: fastcache.New(1024 * 500),
  839. }
  840. snaps := &Tree{
  841. layers: map[common.Hash]snapshot{
  842. base.root: base,
  843. },
  844. }
  845. snaps.Update(common.HexToHash("0x02"), common.HexToHash("0x01"), nil, makeAccounts(2000), nil)
  846. for i := 2; i <= 100; i++ {
  847. snaps.Update(common.HexToHash(fmt.Sprintf("0x%02x", i+1)), common.HexToHash(fmt.Sprintf("0x%02x", i)), nil, makeAccounts(20), nil)
  848. }
  849. // We call this once before the benchmark, so the creation of
  850. // sorted accountlists are not included in the results.
  851. head := snaps.Snapshot(common.HexToHash("0x65"))
  852. head.(*diffLayer).newBinaryAccountIterator()
  853. b.Run("binary iterator (keys)", func(b *testing.B) {
  854. for i := 0; i < b.N; i++ {
  855. got := 0
  856. it := head.(*diffLayer).newBinaryAccountIterator()
  857. for it.Next() {
  858. got++
  859. }
  860. if exp := 2000; got != exp {
  861. b.Errorf("iterator len wrong, expected %d, got %d", exp, got)
  862. }
  863. }
  864. })
  865. b.Run("binary iterator (values)", func(b *testing.B) {
  866. for i := 0; i < b.N; i++ {
  867. got := 0
  868. it := head.(*diffLayer).newBinaryAccountIterator()
  869. for it.Next() {
  870. got++
  871. v := it.Hash()
  872. head.(*diffLayer).accountRLP(v, 0)
  873. }
  874. if exp := 2000; got != exp {
  875. b.Errorf("iterator len wrong, expected %d, got %d", exp, got)
  876. }
  877. }
  878. })
  879. b.Run("fast iterator (keys)", func(b *testing.B) {
  880. for i := 0; i < b.N; i++ {
  881. it, _ := snaps.AccountIterator(common.HexToHash("0x65"), common.Hash{})
  882. defer it.Release()
  883. got := 0
  884. for it.Next() {
  885. got++
  886. }
  887. if exp := 2000; got != exp {
  888. b.Errorf("iterator len wrong, expected %d, got %d", exp, got)
  889. }
  890. }
  891. })
  892. b.Run("fast iterator (values)", func(b *testing.B) {
  893. for i := 0; i < b.N; i++ {
  894. it, _ := snaps.AccountIterator(common.HexToHash("0x65"), common.Hash{})
  895. defer it.Release()
  896. got := 0
  897. for it.Next() {
  898. it.Account()
  899. got++
  900. }
  901. if exp := 2000; got != exp {
  902. b.Errorf("iterator len wrong, expected %d, got %d", exp, got)
  903. }
  904. }
  905. })
  906. }
  907. /*
  908. func BenchmarkBinaryAccountIteration(b *testing.B) {
  909. benchmarkAccountIteration(b, func(snap snapshot) AccountIterator {
  910. return snap.(*diffLayer).newBinaryAccountIterator()
  911. })
  912. }
  913. func BenchmarkFastAccountIteration(b *testing.B) {
  914. benchmarkAccountIteration(b, newFastAccountIterator)
  915. }
  916. func benchmarkAccountIteration(b *testing.B, iterator func(snap snapshot) AccountIterator) {
  917. // Create a diff stack and randomize the accounts across them
  918. layers := make([]map[common.Hash][]byte, 128)
  919. for i := 0; i < len(layers); i++ {
  920. layers[i] = make(map[common.Hash][]byte)
  921. }
  922. for i := 0; i < b.N; i++ {
  923. depth := rand.Intn(len(layers))
  924. layers[depth][randomHash()] = randomAccount()
  925. }
  926. stack := snapshot(emptyLayer())
  927. for _, layer := range layers {
  928. stack = stack.Update(common.Hash{}, layer, nil, nil)
  929. }
  930. // Reset the timers and report all the stats
  931. it := iterator(stack)
  932. b.ResetTimer()
  933. b.ReportAllocs()
  934. for it.Next() {
  935. }
  936. }
  937. */