generate_test.go 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833
  1. // Copyright 2020 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. "fmt"
  19. "math/big"
  20. "os"
  21. "testing"
  22. "time"
  23. "github.com/ethereum/go-ethereum/common"
  24. "github.com/ethereum/go-ethereum/core/rawdb"
  25. "github.com/ethereum/go-ethereum/ethdb"
  26. "github.com/ethereum/go-ethereum/ethdb/memorydb"
  27. "github.com/ethereum/go-ethereum/log"
  28. "github.com/ethereum/go-ethereum/rlp"
  29. "github.com/ethereum/go-ethereum/trie"
  30. "golang.org/x/crypto/sha3"
  31. )
  32. // Tests that snapshot generation from an empty database.
  33. func TestGeneration(t *testing.T) {
  34. // We can't use statedb to make a test trie (circular dependency), so make
  35. // a fake one manually. We're going with a small account trie of 3 accounts,
  36. // two of which also has the same 3-slot storage trie attached.
  37. var (
  38. diskdb = memorydb.New()
  39. triedb = trie.NewDatabase(diskdb)
  40. )
  41. stTrie, _ := trie.NewSecure(common.Hash{}, triedb)
  42. stTrie.Update([]byte("key-1"), []byte("val-1")) // 0x1314700b81afc49f94db3623ef1df38f3ed18b73a1b7ea2f6c095118cf6118a0
  43. stTrie.Update([]byte("key-2"), []byte("val-2")) // 0x18a0f4d79cff4459642dd7604f303886ad9d77c30cf3d7d7cedb3a693ab6d371
  44. stTrie.Update([]byte("key-3"), []byte("val-3")) // 0x51c71a47af0695957647fb68766d0becee77e953df17c29b3c2f25436f055c78
  45. stTrie.Commit(nil) // Root: 0xddefcd9376dd029653ef384bd2f0a126bb755fe84fdcc9e7cf421ba454f2bc67
  46. accTrie, _ := trie.NewSecure(common.Hash{}, triedb)
  47. acc := &Account{Balance: big.NewInt(1), Root: stTrie.Hash().Bytes(), CodeHash: emptyCode.Bytes()}
  48. val, _ := rlp.EncodeToBytes(acc)
  49. accTrie.Update([]byte("acc-1"), val) // 0x9250573b9c18c664139f3b6a7a8081b7d8f8916a8fcc5d94feec6c29f5fd4e9e
  50. acc = &Account{Balance: big.NewInt(2), Root: emptyRoot.Bytes(), CodeHash: emptyCode.Bytes()}
  51. val, _ = rlp.EncodeToBytes(acc)
  52. accTrie.Update([]byte("acc-2"), val) // 0x65145f923027566669a1ae5ccac66f945b55ff6eaeb17d2ea8e048b7d381f2d7
  53. acc = &Account{Balance: big.NewInt(3), Root: stTrie.Hash().Bytes(), CodeHash: emptyCode.Bytes()}
  54. val, _ = rlp.EncodeToBytes(acc)
  55. accTrie.Update([]byte("acc-3"), val) // 0x50815097425d000edfc8b3a4a13e175fc2bdcfee8bdfbf2d1ff61041d3c235b2
  56. root, _ := accTrie.Commit(nil) // Root: 0xe3712f1a226f3782caca78ca770ccc19ee000552813a9f59d479f8611db9b1fd
  57. triedb.Commit(root, false, nil)
  58. if have, want := root, common.HexToHash("0xe3712f1a226f3782caca78ca770ccc19ee000552813a9f59d479f8611db9b1fd"); have != want {
  59. t.Fatalf("have %#x want %#x", have, want)
  60. }
  61. snap := generateSnapshot(diskdb, triedb, 16, root)
  62. select {
  63. case <-snap.genPending:
  64. // Snapshot generation succeeded
  65. case <-time.After(250 * time.Millisecond):
  66. t.Errorf("Snapshot generation failed")
  67. }
  68. checkSnapRoot(t, snap, root)
  69. // Signal abortion to the generator and wait for it to tear down
  70. stop := make(chan *generatorStats)
  71. snap.genAbort <- stop
  72. <-stop
  73. }
  74. func hashData(input []byte) common.Hash {
  75. var hasher = sha3.NewLegacyKeccak256()
  76. var hash common.Hash
  77. hasher.Reset()
  78. hasher.Write(input)
  79. hasher.Sum(hash[:0])
  80. return hash
  81. }
  82. // Tests that snapshot generation with existent flat state.
  83. func TestGenerateExistentState(t *testing.T) {
  84. // We can't use statedb to make a test trie (circular dependency), so make
  85. // a fake one manually. We're going with a small account trie of 3 accounts,
  86. // two of which also has the same 3-slot storage trie attached.
  87. var (
  88. diskdb = memorydb.New()
  89. triedb = trie.NewDatabase(diskdb)
  90. )
  91. stTrie, _ := trie.NewSecure(common.Hash{}, triedb)
  92. stTrie.Update([]byte("key-1"), []byte("val-1")) // 0x1314700b81afc49f94db3623ef1df38f3ed18b73a1b7ea2f6c095118cf6118a0
  93. stTrie.Update([]byte("key-2"), []byte("val-2")) // 0x18a0f4d79cff4459642dd7604f303886ad9d77c30cf3d7d7cedb3a693ab6d371
  94. stTrie.Update([]byte("key-3"), []byte("val-3")) // 0x51c71a47af0695957647fb68766d0becee77e953df17c29b3c2f25436f055c78
  95. stTrie.Commit(nil) // Root: 0xddefcd9376dd029653ef384bd2f0a126bb755fe84fdcc9e7cf421ba454f2bc67
  96. accTrie, _ := trie.NewSecure(common.Hash{}, triedb)
  97. acc := &Account{Balance: big.NewInt(1), Root: stTrie.Hash().Bytes(), CodeHash: emptyCode.Bytes()}
  98. val, _ := rlp.EncodeToBytes(acc)
  99. accTrie.Update([]byte("acc-1"), val) // 0x9250573b9c18c664139f3b6a7a8081b7d8f8916a8fcc5d94feec6c29f5fd4e9e
  100. rawdb.WriteAccountSnapshot(diskdb, hashData([]byte("acc-1")), val)
  101. rawdb.WriteStorageSnapshot(diskdb, hashData([]byte("acc-1")), hashData([]byte("key-1")), []byte("val-1"))
  102. rawdb.WriteStorageSnapshot(diskdb, hashData([]byte("acc-1")), hashData([]byte("key-2")), []byte("val-2"))
  103. rawdb.WriteStorageSnapshot(diskdb, hashData([]byte("acc-1")), hashData([]byte("key-3")), []byte("val-3"))
  104. acc = &Account{Balance: big.NewInt(2), Root: emptyRoot.Bytes(), CodeHash: emptyCode.Bytes()}
  105. val, _ = rlp.EncodeToBytes(acc)
  106. accTrie.Update([]byte("acc-2"), val) // 0x65145f923027566669a1ae5ccac66f945b55ff6eaeb17d2ea8e048b7d381f2d7
  107. diskdb.Put(hashData([]byte("acc-2")).Bytes(), val)
  108. rawdb.WriteAccountSnapshot(diskdb, hashData([]byte("acc-2")), val)
  109. acc = &Account{Balance: big.NewInt(3), Root: stTrie.Hash().Bytes(), CodeHash: emptyCode.Bytes()}
  110. val, _ = rlp.EncodeToBytes(acc)
  111. accTrie.Update([]byte("acc-3"), val) // 0x50815097425d000edfc8b3a4a13e175fc2bdcfee8bdfbf2d1ff61041d3c235b2
  112. rawdb.WriteAccountSnapshot(diskdb, hashData([]byte("acc-3")), val)
  113. rawdb.WriteStorageSnapshot(diskdb, hashData([]byte("acc-3")), hashData([]byte("key-1")), []byte("val-1"))
  114. rawdb.WriteStorageSnapshot(diskdb, hashData([]byte("acc-3")), hashData([]byte("key-2")), []byte("val-2"))
  115. rawdb.WriteStorageSnapshot(diskdb, hashData([]byte("acc-3")), hashData([]byte("key-3")), []byte("val-3"))
  116. root, _ := accTrie.Commit(nil) // Root: 0xe3712f1a226f3782caca78ca770ccc19ee000552813a9f59d479f8611db9b1fd
  117. triedb.Commit(root, false, nil)
  118. snap := generateSnapshot(diskdb, triedb, 16, root)
  119. select {
  120. case <-snap.genPending:
  121. // Snapshot generation succeeded
  122. case <-time.After(250 * time.Millisecond):
  123. t.Errorf("Snapshot generation failed")
  124. }
  125. checkSnapRoot(t, snap, root)
  126. // Signal abortion to the generator and wait for it to tear down
  127. stop := make(chan *generatorStats)
  128. snap.genAbort <- stop
  129. <-stop
  130. }
  131. func checkSnapRoot(t *testing.T, snap *diskLayer, trieRoot common.Hash) {
  132. t.Helper()
  133. accIt := snap.AccountIterator(common.Hash{})
  134. defer accIt.Release()
  135. snapRoot, err := generateTrieRoot(nil, accIt, common.Hash{}, stackTrieGenerate,
  136. func(db ethdb.KeyValueWriter, accountHash, codeHash common.Hash, stat *generateStats) (common.Hash, error) {
  137. storageIt, _ := snap.StorageIterator(accountHash, common.Hash{})
  138. defer storageIt.Release()
  139. hash, err := generateTrieRoot(nil, storageIt, accountHash, stackTrieGenerate, nil, stat, false)
  140. if err != nil {
  141. return common.Hash{}, err
  142. }
  143. return hash, nil
  144. }, newGenerateStats(), true)
  145. if err != nil {
  146. t.Fatal(err)
  147. }
  148. if snapRoot != trieRoot {
  149. t.Fatalf("snaproot: %#x != trieroot #%x", snapRoot, trieRoot)
  150. }
  151. }
  152. type testHelper struct {
  153. diskdb *memorydb.Database
  154. triedb *trie.Database
  155. accTrie *trie.SecureTrie
  156. }
  157. func newHelper() *testHelper {
  158. diskdb := memorydb.New()
  159. triedb := trie.NewDatabase(diskdb)
  160. accTrie, _ := trie.NewSecure(common.Hash{}, triedb)
  161. return &testHelper{
  162. diskdb: diskdb,
  163. triedb: triedb,
  164. accTrie: accTrie,
  165. }
  166. }
  167. func (t *testHelper) addTrieAccount(acckey string, acc *Account) {
  168. val, _ := rlp.EncodeToBytes(acc)
  169. t.accTrie.Update([]byte(acckey), val)
  170. }
  171. func (t *testHelper) addSnapAccount(acckey string, acc *Account) {
  172. val, _ := rlp.EncodeToBytes(acc)
  173. key := hashData([]byte(acckey))
  174. rawdb.WriteAccountSnapshot(t.diskdb, key, val)
  175. }
  176. func (t *testHelper) addAccount(acckey string, acc *Account) {
  177. t.addTrieAccount(acckey, acc)
  178. t.addSnapAccount(acckey, acc)
  179. }
  180. func (t *testHelper) addSnapStorage(accKey string, keys []string, vals []string) {
  181. accHash := hashData([]byte(accKey))
  182. for i, key := range keys {
  183. rawdb.WriteStorageSnapshot(t.diskdb, accHash, hashData([]byte(key)), []byte(vals[i]))
  184. }
  185. }
  186. func (t *testHelper) makeStorageTrie(keys []string, vals []string) []byte {
  187. stTrie, _ := trie.NewSecure(common.Hash{}, t.triedb)
  188. for i, k := range keys {
  189. stTrie.Update([]byte(k), []byte(vals[i]))
  190. }
  191. root, _ := stTrie.Commit(nil)
  192. return root.Bytes()
  193. }
  194. func (t *testHelper) Generate() (common.Hash, *diskLayer) {
  195. root, _ := t.accTrie.Commit(nil)
  196. t.triedb.Commit(root, false, nil)
  197. snap := generateSnapshot(t.diskdb, t.triedb, 16, root)
  198. return root, snap
  199. }
  200. // Tests that snapshot generation with existent flat state, where the flat state
  201. // contains some errors:
  202. // - the contract with empty storage root but has storage entries in the disk
  203. // - the contract with non empty storage root but empty storage slots
  204. // - the contract(non-empty storage) misses some storage slots
  205. // - miss in the beginning
  206. // - miss in the middle
  207. // - miss in the end
  208. // - the contract(non-empty storage) has wrong storage slots
  209. // - wrong slots in the beginning
  210. // - wrong slots in the middle
  211. // - wrong slots in the end
  212. // - the contract(non-empty storage) has extra storage slots
  213. // - extra slots in the beginning
  214. // - extra slots in the middle
  215. // - extra slots in the end
  216. func TestGenerateExistentStateWithWrongStorage(t *testing.T) {
  217. helper := newHelper()
  218. stRoot := helper.makeStorageTrie([]string{"key-1", "key-2", "key-3"}, []string{"val-1", "val-2", "val-3"})
  219. // Account one, empty root but non-empty database
  220. helper.addAccount("acc-1", &Account{Balance: big.NewInt(1), Root: emptyRoot.Bytes(), CodeHash: emptyCode.Bytes()})
  221. helper.addSnapStorage("acc-1", []string{"key-1", "key-2", "key-3"}, []string{"val-1", "val-2", "val-3"})
  222. // Account two, non empty root but empty database
  223. helper.addAccount("acc-2", &Account{Balance: big.NewInt(1), Root: stRoot, CodeHash: emptyCode.Bytes()})
  224. // Miss slots
  225. {
  226. // Account three, non empty root but misses slots in the beginning
  227. helper.addAccount("acc-3", &Account{Balance: big.NewInt(1), Root: stRoot, CodeHash: emptyCode.Bytes()})
  228. helper.addSnapStorage("acc-3", []string{"key-2", "key-3"}, []string{"val-2", "val-3"})
  229. // Account four, non empty root but misses slots in the middle
  230. helper.addAccount("acc-4", &Account{Balance: big.NewInt(1), Root: stRoot, CodeHash: emptyCode.Bytes()})
  231. helper.addSnapStorage("acc-4", []string{"key-1", "key-3"}, []string{"val-1", "val-3"})
  232. // Account five, non empty root but misses slots in the end
  233. helper.addAccount("acc-5", &Account{Balance: big.NewInt(1), Root: stRoot, CodeHash: emptyCode.Bytes()})
  234. helper.addSnapStorage("acc-5", []string{"key-1", "key-2"}, []string{"val-1", "val-2"})
  235. }
  236. // Wrong storage slots
  237. {
  238. // Account six, non empty root but wrong slots in the beginning
  239. helper.addAccount("acc-6", &Account{Balance: big.NewInt(1), Root: stRoot, CodeHash: emptyCode.Bytes()})
  240. helper.addSnapStorage("acc-6", []string{"key-1", "key-2", "key-3"}, []string{"badval-1", "val-2", "val-3"})
  241. // Account seven, non empty root but wrong slots in the middle
  242. helper.addAccount("acc-7", &Account{Balance: big.NewInt(1), Root: stRoot, CodeHash: emptyCode.Bytes()})
  243. helper.addSnapStorage("acc-7", []string{"key-1", "key-2", "key-3"}, []string{"val-1", "badval-2", "val-3"})
  244. // Account eight, non empty root but wrong slots in the end
  245. helper.addAccount("acc-8", &Account{Balance: big.NewInt(1), Root: stRoot, CodeHash: emptyCode.Bytes()})
  246. helper.addSnapStorage("acc-8", []string{"key-1", "key-2", "key-3"}, []string{"val-1", "val-2", "badval-3"})
  247. // Account 9, non empty root but rotated slots
  248. helper.addAccount("acc-9", &Account{Balance: big.NewInt(1), Root: stRoot, CodeHash: emptyCode.Bytes()})
  249. helper.addSnapStorage("acc-9", []string{"key-1", "key-2", "key-3"}, []string{"val-1", "val-3", "val-2"})
  250. }
  251. // Extra storage slots
  252. {
  253. // Account 10, non empty root but extra slots in the beginning
  254. helper.addAccount("acc-10", &Account{Balance: big.NewInt(1), Root: stRoot, CodeHash: emptyCode.Bytes()})
  255. helper.addSnapStorage("acc-10", []string{"key-0", "key-1", "key-2", "key-3"}, []string{"val-0", "val-1", "val-2", "val-3"})
  256. // Account 11, non empty root but extra slots in the middle
  257. helper.addAccount("acc-11", &Account{Balance: big.NewInt(1), Root: stRoot, CodeHash: emptyCode.Bytes()})
  258. helper.addSnapStorage("acc-11", []string{"key-1", "key-2", "key-2-1", "key-3"}, []string{"val-1", "val-2", "val-2-1", "val-3"})
  259. // Account 12, non empty root but extra slots in the end
  260. helper.addAccount("acc-12", &Account{Balance: big.NewInt(1), Root: stRoot, CodeHash: emptyCode.Bytes()})
  261. helper.addSnapStorage("acc-12", []string{"key-1", "key-2", "key-3", "key-4"}, []string{"val-1", "val-2", "val-3", "val-4"})
  262. }
  263. root, snap := helper.Generate()
  264. t.Logf("Root: %#x\n", root) // Root = 0x8746cce9fd9c658b2cfd639878ed6584b7a2b3e73bb40f607fcfa156002429a0
  265. select {
  266. case <-snap.genPending:
  267. // Snapshot generation succeeded
  268. case <-time.After(250 * time.Millisecond):
  269. t.Errorf("Snapshot generation failed")
  270. }
  271. checkSnapRoot(t, snap, root)
  272. // Signal abortion to the generator and wait for it to tear down
  273. stop := make(chan *generatorStats)
  274. snap.genAbort <- stop
  275. <-stop
  276. }
  277. // Tests that snapshot generation with existent flat state, where the flat state
  278. // contains some errors:
  279. // - miss accounts
  280. // - wrong accounts
  281. // - extra accounts
  282. func TestGenerateExistentStateWithWrongAccounts(t *testing.T) {
  283. helper := newHelper()
  284. stRoot := helper.makeStorageTrie([]string{"key-1", "key-2", "key-3"}, []string{"val-1", "val-2", "val-3"})
  285. // Trie accounts [acc-1, acc-2, acc-3, acc-4, acc-6]
  286. // Extra accounts [acc-0, acc-5, acc-7]
  287. // Missing accounts, only in the trie
  288. {
  289. helper.addTrieAccount("acc-1", &Account{Balance: big.NewInt(1), Root: stRoot, CodeHash: emptyCode.Bytes()}) // Beginning
  290. helper.addTrieAccount("acc-4", &Account{Balance: big.NewInt(1), Root: stRoot, CodeHash: emptyCode.Bytes()}) // Middle
  291. helper.addTrieAccount("acc-6", &Account{Balance: big.NewInt(1), Root: stRoot, CodeHash: emptyCode.Bytes()}) // End
  292. }
  293. // Wrong accounts
  294. {
  295. helper.addTrieAccount("acc-2", &Account{Balance: big.NewInt(1), Root: stRoot, CodeHash: emptyCode.Bytes()})
  296. helper.addSnapAccount("acc-2", &Account{Balance: big.NewInt(1), Root: stRoot, CodeHash: common.Hex2Bytes("0x1234")})
  297. helper.addTrieAccount("acc-3", &Account{Balance: big.NewInt(1), Root: stRoot, CodeHash: emptyCode.Bytes()})
  298. helper.addSnapAccount("acc-3", &Account{Balance: big.NewInt(1), Root: emptyRoot.Bytes(), CodeHash: emptyCode.Bytes()})
  299. }
  300. // Extra accounts, only in the snap
  301. {
  302. helper.addSnapAccount("acc-0", &Account{Balance: big.NewInt(1), Root: stRoot, CodeHash: emptyRoot.Bytes()}) // before the beginning
  303. helper.addSnapAccount("acc-5", &Account{Balance: big.NewInt(1), Root: emptyRoot.Bytes(), CodeHash: common.Hex2Bytes("0x1234")}) // Middle
  304. helper.addSnapAccount("acc-7", &Account{Balance: big.NewInt(1), Root: emptyRoot.Bytes(), CodeHash: emptyRoot.Bytes()}) // after the end
  305. }
  306. root, snap := helper.Generate()
  307. t.Logf("Root: %#x\n", root) // Root = 0x825891472281463511e7ebcc7f109e4f9200c20fa384754e11fd605cd98464e8
  308. select {
  309. case <-snap.genPending:
  310. // Snapshot generation succeeded
  311. case <-time.After(250 * time.Millisecond):
  312. t.Errorf("Snapshot generation failed")
  313. }
  314. checkSnapRoot(t, snap, root)
  315. // Signal abortion to the generator and wait for it to tear down
  316. stop := make(chan *generatorStats)
  317. snap.genAbort <- stop
  318. <-stop
  319. }
  320. // Tests that snapshot generation errors out correctly in case of a missing trie
  321. // node in the account trie.
  322. func TestGenerateCorruptAccountTrie(t *testing.T) {
  323. // We can't use statedb to make a test trie (circular dependency), so make
  324. // a fake one manually. We're going with a small account trie of 3 accounts,
  325. // without any storage slots to keep the test smaller.
  326. var (
  327. diskdb = memorydb.New()
  328. triedb = trie.NewDatabase(diskdb)
  329. )
  330. tr, _ := trie.NewSecure(common.Hash{}, triedb)
  331. acc := &Account{Balance: big.NewInt(1), Root: emptyRoot.Bytes(), CodeHash: emptyCode.Bytes()}
  332. val, _ := rlp.EncodeToBytes(acc)
  333. tr.Update([]byte("acc-1"), val) // 0xc7a30f39aff471c95d8a837497ad0e49b65be475cc0953540f80cfcdbdcd9074
  334. acc = &Account{Balance: big.NewInt(2), Root: emptyRoot.Bytes(), CodeHash: emptyCode.Bytes()}
  335. val, _ = rlp.EncodeToBytes(acc)
  336. tr.Update([]byte("acc-2"), val) // 0x65145f923027566669a1ae5ccac66f945b55ff6eaeb17d2ea8e048b7d381f2d7
  337. acc = &Account{Balance: big.NewInt(3), Root: emptyRoot.Bytes(), CodeHash: emptyCode.Bytes()}
  338. val, _ = rlp.EncodeToBytes(acc)
  339. tr.Update([]byte("acc-3"), val) // 0x19ead688e907b0fab07176120dceec244a72aff2f0aa51e8b827584e378772f4
  340. tr.Commit(nil) // Root: 0xa04693ea110a31037fb5ee814308a6f1d76bdab0b11676bdf4541d2de55ba978
  341. // Delete an account trie leaf and ensure the generator chokes
  342. triedb.Commit(common.HexToHash("0xa04693ea110a31037fb5ee814308a6f1d76bdab0b11676bdf4541d2de55ba978"), false, nil)
  343. diskdb.Delete(common.HexToHash("0x65145f923027566669a1ae5ccac66f945b55ff6eaeb17d2ea8e048b7d381f2d7").Bytes())
  344. snap := generateSnapshot(diskdb, triedb, 16, common.HexToHash("0xa04693ea110a31037fb5ee814308a6f1d76bdab0b11676bdf4541d2de55ba978"))
  345. select {
  346. case <-snap.genPending:
  347. // Snapshot generation succeeded
  348. t.Errorf("Snapshot generated against corrupt account trie")
  349. case <-time.After(250 * time.Millisecond):
  350. // Not generated fast enough, hopefully blocked inside on missing trie node fail
  351. }
  352. // Signal abortion to the generator and wait for it to tear down
  353. stop := make(chan *generatorStats)
  354. snap.genAbort <- stop
  355. <-stop
  356. }
  357. // Tests that snapshot generation errors out correctly in case of a missing root
  358. // trie node for a storage trie. It's similar to internal corruption but it is
  359. // handled differently inside the generator.
  360. func TestGenerateMissingStorageTrie(t *testing.T) {
  361. // We can't use statedb to make a test trie (circular dependency), so make
  362. // a fake one manually. We're going with a small account trie of 3 accounts,
  363. // two of which also has the same 3-slot storage trie attached.
  364. var (
  365. diskdb = memorydb.New()
  366. triedb = trie.NewDatabase(diskdb)
  367. )
  368. stTrie, _ := trie.NewSecure(common.Hash{}, triedb)
  369. stTrie.Update([]byte("key-1"), []byte("val-1")) // 0x1314700b81afc49f94db3623ef1df38f3ed18b73a1b7ea2f6c095118cf6118a0
  370. stTrie.Update([]byte("key-2"), []byte("val-2")) // 0x18a0f4d79cff4459642dd7604f303886ad9d77c30cf3d7d7cedb3a693ab6d371
  371. stTrie.Update([]byte("key-3"), []byte("val-3")) // 0x51c71a47af0695957647fb68766d0becee77e953df17c29b3c2f25436f055c78
  372. stTrie.Commit(nil) // Root: 0xddefcd9376dd029653ef384bd2f0a126bb755fe84fdcc9e7cf421ba454f2bc67
  373. accTrie, _ := trie.NewSecure(common.Hash{}, triedb)
  374. acc := &Account{Balance: big.NewInt(1), Root: stTrie.Hash().Bytes(), CodeHash: emptyCode.Bytes()}
  375. val, _ := rlp.EncodeToBytes(acc)
  376. accTrie.Update([]byte("acc-1"), val) // 0x9250573b9c18c664139f3b6a7a8081b7d8f8916a8fcc5d94feec6c29f5fd4e9e
  377. acc = &Account{Balance: big.NewInt(2), Root: emptyRoot.Bytes(), CodeHash: emptyCode.Bytes()}
  378. val, _ = rlp.EncodeToBytes(acc)
  379. accTrie.Update([]byte("acc-2"), val) // 0x65145f923027566669a1ae5ccac66f945b55ff6eaeb17d2ea8e048b7d381f2d7
  380. acc = &Account{Balance: big.NewInt(3), Root: stTrie.Hash().Bytes(), CodeHash: emptyCode.Bytes()}
  381. val, _ = rlp.EncodeToBytes(acc)
  382. accTrie.Update([]byte("acc-3"), val) // 0x50815097425d000edfc8b3a4a13e175fc2bdcfee8bdfbf2d1ff61041d3c235b2
  383. accTrie.Commit(nil) // Root: 0xe3712f1a226f3782caca78ca770ccc19ee000552813a9f59d479f8611db9b1fd
  384. // We can only corrupt the disk database, so flush the tries out
  385. triedb.Reference(
  386. common.HexToHash("0xddefcd9376dd029653ef384bd2f0a126bb755fe84fdcc9e7cf421ba454f2bc67"),
  387. common.HexToHash("0x9250573b9c18c664139f3b6a7a8081b7d8f8916a8fcc5d94feec6c29f5fd4e9e"),
  388. )
  389. triedb.Reference(
  390. common.HexToHash("0xddefcd9376dd029653ef384bd2f0a126bb755fe84fdcc9e7cf421ba454f2bc67"),
  391. common.HexToHash("0x50815097425d000edfc8b3a4a13e175fc2bdcfee8bdfbf2d1ff61041d3c235b2"),
  392. )
  393. triedb.Commit(common.HexToHash("0xe3712f1a226f3782caca78ca770ccc19ee000552813a9f59d479f8611db9b1fd"), false, nil)
  394. // Delete a storage trie root and ensure the generator chokes
  395. diskdb.Delete(common.HexToHash("0xddefcd9376dd029653ef384bd2f0a126bb755fe84fdcc9e7cf421ba454f2bc67").Bytes())
  396. snap := generateSnapshot(diskdb, triedb, 16, common.HexToHash("0xe3712f1a226f3782caca78ca770ccc19ee000552813a9f59d479f8611db9b1fd"))
  397. select {
  398. case <-snap.genPending:
  399. // Snapshot generation succeeded
  400. t.Errorf("Snapshot generated against corrupt storage trie")
  401. case <-time.After(250 * time.Millisecond):
  402. // Not generated fast enough, hopefully blocked inside on missing trie node fail
  403. }
  404. // Signal abortion to the generator and wait for it to tear down
  405. stop := make(chan *generatorStats)
  406. snap.genAbort <- stop
  407. <-stop
  408. }
  409. // Tests that snapshot generation errors out correctly in case of a missing trie
  410. // node in a storage trie.
  411. func TestGenerateCorruptStorageTrie(t *testing.T) {
  412. // We can't use statedb to make a test trie (circular dependency), so make
  413. // a fake one manually. We're going with a small account trie of 3 accounts,
  414. // two of which also has the same 3-slot storage trie attached.
  415. var (
  416. diskdb = memorydb.New()
  417. triedb = trie.NewDatabase(diskdb)
  418. )
  419. stTrie, _ := trie.NewSecure(common.Hash{}, triedb)
  420. stTrie.Update([]byte("key-1"), []byte("val-1")) // 0x1314700b81afc49f94db3623ef1df38f3ed18b73a1b7ea2f6c095118cf6118a0
  421. stTrie.Update([]byte("key-2"), []byte("val-2")) // 0x18a0f4d79cff4459642dd7604f303886ad9d77c30cf3d7d7cedb3a693ab6d371
  422. stTrie.Update([]byte("key-3"), []byte("val-3")) // 0x51c71a47af0695957647fb68766d0becee77e953df17c29b3c2f25436f055c78
  423. stTrie.Commit(nil) // Root: 0xddefcd9376dd029653ef384bd2f0a126bb755fe84fdcc9e7cf421ba454f2bc67
  424. accTrie, _ := trie.NewSecure(common.Hash{}, triedb)
  425. acc := &Account{Balance: big.NewInt(1), Root: stTrie.Hash().Bytes(), CodeHash: emptyCode.Bytes()}
  426. val, _ := rlp.EncodeToBytes(acc)
  427. accTrie.Update([]byte("acc-1"), val) // 0x9250573b9c18c664139f3b6a7a8081b7d8f8916a8fcc5d94feec6c29f5fd4e9e
  428. acc = &Account{Balance: big.NewInt(2), Root: emptyRoot.Bytes(), CodeHash: emptyCode.Bytes()}
  429. val, _ = rlp.EncodeToBytes(acc)
  430. accTrie.Update([]byte("acc-2"), val) // 0x65145f923027566669a1ae5ccac66f945b55ff6eaeb17d2ea8e048b7d381f2d7
  431. acc = &Account{Balance: big.NewInt(3), Root: stTrie.Hash().Bytes(), CodeHash: emptyCode.Bytes()}
  432. val, _ = rlp.EncodeToBytes(acc)
  433. accTrie.Update([]byte("acc-3"), val) // 0x50815097425d000edfc8b3a4a13e175fc2bdcfee8bdfbf2d1ff61041d3c235b2
  434. accTrie.Commit(nil) // Root: 0xe3712f1a226f3782caca78ca770ccc19ee000552813a9f59d479f8611db9b1fd
  435. // We can only corrupt the disk database, so flush the tries out
  436. triedb.Reference(
  437. common.HexToHash("0xddefcd9376dd029653ef384bd2f0a126bb755fe84fdcc9e7cf421ba454f2bc67"),
  438. common.HexToHash("0x9250573b9c18c664139f3b6a7a8081b7d8f8916a8fcc5d94feec6c29f5fd4e9e"),
  439. )
  440. triedb.Reference(
  441. common.HexToHash("0xddefcd9376dd029653ef384bd2f0a126bb755fe84fdcc9e7cf421ba454f2bc67"),
  442. common.HexToHash("0x50815097425d000edfc8b3a4a13e175fc2bdcfee8bdfbf2d1ff61041d3c235b2"),
  443. )
  444. triedb.Commit(common.HexToHash("0xe3712f1a226f3782caca78ca770ccc19ee000552813a9f59d479f8611db9b1fd"), false, nil)
  445. // Delete a storage trie leaf and ensure the generator chokes
  446. diskdb.Delete(common.HexToHash("0x18a0f4d79cff4459642dd7604f303886ad9d77c30cf3d7d7cedb3a693ab6d371").Bytes())
  447. snap := generateSnapshot(diskdb, triedb, 16, common.HexToHash("0xe3712f1a226f3782caca78ca770ccc19ee000552813a9f59d479f8611db9b1fd"))
  448. select {
  449. case <-snap.genPending:
  450. // Snapshot generation succeeded
  451. t.Errorf("Snapshot generated against corrupt storage trie")
  452. case <-time.After(250 * time.Millisecond):
  453. // Not generated fast enough, hopefully blocked inside on missing trie node fail
  454. }
  455. // Signal abortion to the generator and wait for it to tear down
  456. stop := make(chan *generatorStats)
  457. snap.genAbort <- stop
  458. <-stop
  459. }
  460. func getStorageTrie(n int, triedb *trie.Database) *trie.SecureTrie {
  461. stTrie, _ := trie.NewSecure(common.Hash{}, triedb)
  462. for i := 0; i < n; i++ {
  463. k := fmt.Sprintf("key-%d", i)
  464. v := fmt.Sprintf("val-%d", i)
  465. stTrie.Update([]byte(k), []byte(v))
  466. }
  467. stTrie.Commit(nil)
  468. return stTrie
  469. }
  470. // Tests that snapshot generation when an extra account with storage exists in the snap state.
  471. func TestGenerateWithExtraAccounts(t *testing.T) {
  472. var (
  473. diskdb = memorydb.New()
  474. triedb = trie.NewDatabase(diskdb)
  475. stTrie = getStorageTrie(5, triedb)
  476. )
  477. accTrie, _ := trie.NewSecure(common.Hash{}, triedb)
  478. { // Account one in the trie
  479. acc := &Account{Balance: big.NewInt(1), Root: stTrie.Hash().Bytes(), CodeHash: emptyCode.Bytes()}
  480. val, _ := rlp.EncodeToBytes(acc)
  481. accTrie.Update([]byte("acc-1"), val) // 0x9250573b9c18c664139f3b6a7a8081b7d8f8916a8fcc5d94feec6c29f5fd4e9e
  482. // Identical in the snap
  483. key := hashData([]byte("acc-1"))
  484. rawdb.WriteAccountSnapshot(diskdb, key, val)
  485. rawdb.WriteStorageSnapshot(diskdb, key, hashData([]byte("key-1")), []byte("val-1"))
  486. rawdb.WriteStorageSnapshot(diskdb, key, hashData([]byte("key-2")), []byte("val-2"))
  487. rawdb.WriteStorageSnapshot(diskdb, key, hashData([]byte("key-3")), []byte("val-3"))
  488. rawdb.WriteStorageSnapshot(diskdb, key, hashData([]byte("key-4")), []byte("val-4"))
  489. rawdb.WriteStorageSnapshot(diskdb, key, hashData([]byte("key-5")), []byte("val-5"))
  490. }
  491. { // Account two exists only in the snapshot
  492. acc := &Account{Balance: big.NewInt(1), Root: stTrie.Hash().Bytes(), CodeHash: emptyCode.Bytes()}
  493. val, _ := rlp.EncodeToBytes(acc)
  494. key := hashData([]byte("acc-2"))
  495. rawdb.WriteAccountSnapshot(diskdb, key, val)
  496. rawdb.WriteStorageSnapshot(diskdb, key, hashData([]byte("b-key-1")), []byte("b-val-1"))
  497. rawdb.WriteStorageSnapshot(diskdb, key, hashData([]byte("b-key-2")), []byte("b-val-2"))
  498. rawdb.WriteStorageSnapshot(diskdb, key, hashData([]byte("b-key-3")), []byte("b-val-3"))
  499. }
  500. root, _ := accTrie.Commit(nil)
  501. t.Logf("root: %x", root)
  502. triedb.Commit(root, false, nil)
  503. // To verify the test: If we now inspect the snap db, there should exist extraneous storage items
  504. if data := rawdb.ReadStorageSnapshot(diskdb, hashData([]byte("acc-2")), hashData([]byte("b-key-1"))); data == nil {
  505. t.Fatalf("expected snap storage to exist")
  506. }
  507. snap := generateSnapshot(diskdb, triedb, 16, root)
  508. select {
  509. case <-snap.genPending:
  510. // Snapshot generation succeeded
  511. case <-time.After(250 * time.Millisecond):
  512. t.Errorf("Snapshot generation failed")
  513. }
  514. checkSnapRoot(t, snap, root)
  515. // Signal abortion to the generator and wait for it to tear down
  516. stop := make(chan *generatorStats)
  517. snap.genAbort <- stop
  518. <-stop
  519. // If we now inspect the snap db, there should exist no extraneous storage items
  520. if data := rawdb.ReadStorageSnapshot(diskdb, hashData([]byte("acc-2")), hashData([]byte("b-key-1"))); data != nil {
  521. t.Fatalf("expected slot to be removed, got %v", string(data))
  522. }
  523. }
  524. func enableLogging() {
  525. log.Root().SetHandler(log.LvlFilterHandler(log.LvlTrace, log.StreamHandler(os.Stderr, log.TerminalFormat(true))))
  526. }
  527. // Tests that snapshot generation when an extra account with storage exists in the snap state.
  528. func TestGenerateWithManyExtraAccounts(t *testing.T) {
  529. if false {
  530. enableLogging()
  531. }
  532. var (
  533. diskdb = memorydb.New()
  534. triedb = trie.NewDatabase(diskdb)
  535. stTrie = getStorageTrie(3, triedb)
  536. )
  537. accTrie, _ := trie.NewSecure(common.Hash{}, triedb)
  538. { // Account one in the trie
  539. acc := &Account{Balance: big.NewInt(1), Root: stTrie.Hash().Bytes(), CodeHash: emptyCode.Bytes()}
  540. val, _ := rlp.EncodeToBytes(acc)
  541. accTrie.Update([]byte("acc-1"), val) // 0x9250573b9c18c664139f3b6a7a8081b7d8f8916a8fcc5d94feec6c29f5fd4e9e
  542. // Identical in the snap
  543. key := hashData([]byte("acc-1"))
  544. rawdb.WriteAccountSnapshot(diskdb, key, val)
  545. rawdb.WriteStorageSnapshot(diskdb, key, hashData([]byte("key-1")), []byte("val-1"))
  546. rawdb.WriteStorageSnapshot(diskdb, key, hashData([]byte("key-2")), []byte("val-2"))
  547. rawdb.WriteStorageSnapshot(diskdb, key, hashData([]byte("key-3")), []byte("val-3"))
  548. }
  549. { // 100 accounts exist only in snapshot
  550. for i := 0; i < 1000; i++ {
  551. //acc := &Account{Balance: big.NewInt(int64(i)), Root: stTrie.Hash().Bytes(), CodeHash: emptyCode.Bytes()}
  552. acc := &Account{Balance: big.NewInt(int64(i)), Root: emptyRoot.Bytes(), CodeHash: emptyCode.Bytes()}
  553. val, _ := rlp.EncodeToBytes(acc)
  554. key := hashData([]byte(fmt.Sprintf("acc-%d", i)))
  555. rawdb.WriteAccountSnapshot(diskdb, key, val)
  556. }
  557. }
  558. root, _ := accTrie.Commit(nil)
  559. t.Logf("root: %x", root)
  560. triedb.Commit(root, false, nil)
  561. snap := generateSnapshot(diskdb, triedb, 16, root)
  562. select {
  563. case <-snap.genPending:
  564. // Snapshot generation succeeded
  565. case <-time.After(250 * time.Millisecond):
  566. t.Errorf("Snapshot generation failed")
  567. }
  568. checkSnapRoot(t, snap, root)
  569. // Signal abortion to the generator and wait for it to tear down
  570. stop := make(chan *generatorStats)
  571. snap.genAbort <- stop
  572. <-stop
  573. }
  574. // Tests this case
  575. // maxAccountRange 3
  576. // snapshot-accounts: 01, 02, 03, 04, 05, 06, 07
  577. // trie-accounts: 03, 07
  578. //
  579. // We iterate three snapshot storage slots (max = 3) from the database. They are 0x01, 0x02, 0x03.
  580. // The trie has a lot of deletions.
  581. // So in trie, we iterate 2 entries 0x03, 0x07. We create the 0x07 in the database and abort the procedure, because the trie is exhausted.
  582. // But in the database, we still have the stale storage slots 0x04, 0x05. They are not iterated yet, but the procedure is finished.
  583. func TestGenerateWithExtraBeforeAndAfter(t *testing.T) {
  584. accountCheckRange = 3
  585. if false {
  586. enableLogging()
  587. }
  588. var (
  589. diskdb = memorydb.New()
  590. triedb = trie.NewDatabase(diskdb)
  591. )
  592. accTrie, _ := trie.New(common.Hash{}, triedb)
  593. {
  594. acc := &Account{Balance: big.NewInt(1), Root: emptyRoot.Bytes(), CodeHash: emptyCode.Bytes()}
  595. val, _ := rlp.EncodeToBytes(acc)
  596. accTrie.Update(common.HexToHash("0x03").Bytes(), val)
  597. accTrie.Update(common.HexToHash("0x07").Bytes(), val)
  598. rawdb.WriteAccountSnapshot(diskdb, common.HexToHash("0x01"), val)
  599. rawdb.WriteAccountSnapshot(diskdb, common.HexToHash("0x02"), val)
  600. rawdb.WriteAccountSnapshot(diskdb, common.HexToHash("0x03"), val)
  601. rawdb.WriteAccountSnapshot(diskdb, common.HexToHash("0x04"), val)
  602. rawdb.WriteAccountSnapshot(diskdb, common.HexToHash("0x05"), val)
  603. rawdb.WriteAccountSnapshot(diskdb, common.HexToHash("0x06"), val)
  604. rawdb.WriteAccountSnapshot(diskdb, common.HexToHash("0x07"), val)
  605. }
  606. root, _ := accTrie.Commit(nil)
  607. t.Logf("root: %x", root)
  608. triedb.Commit(root, false, nil)
  609. snap := generateSnapshot(diskdb, triedb, 16, root)
  610. select {
  611. case <-snap.genPending:
  612. // Snapshot generation succeeded
  613. case <-time.After(250 * time.Millisecond):
  614. t.Errorf("Snapshot generation failed")
  615. }
  616. checkSnapRoot(t, snap, root)
  617. // Signal abortion to the generator and wait for it to tear down
  618. stop := make(chan *generatorStats)
  619. snap.genAbort <- stop
  620. <-stop
  621. }
  622. // TestGenerateWithMalformedSnapdata tests what happes if we have some junk
  623. // in the snapshot database, which cannot be parsed back to an account
  624. func TestGenerateWithMalformedSnapdata(t *testing.T) {
  625. accountCheckRange = 3
  626. if false {
  627. enableLogging()
  628. }
  629. var (
  630. diskdb = memorydb.New()
  631. triedb = trie.NewDatabase(diskdb)
  632. )
  633. accTrie, _ := trie.New(common.Hash{}, triedb)
  634. {
  635. acc := &Account{Balance: big.NewInt(1), Root: emptyRoot.Bytes(), CodeHash: emptyCode.Bytes()}
  636. val, _ := rlp.EncodeToBytes(acc)
  637. accTrie.Update(common.HexToHash("0x03").Bytes(), val)
  638. junk := make([]byte, 100)
  639. copy(junk, []byte{0xde, 0xad})
  640. rawdb.WriteAccountSnapshot(diskdb, common.HexToHash("0x02"), junk)
  641. rawdb.WriteAccountSnapshot(diskdb, common.HexToHash("0x03"), junk)
  642. rawdb.WriteAccountSnapshot(diskdb, common.HexToHash("0x04"), junk)
  643. rawdb.WriteAccountSnapshot(diskdb, common.HexToHash("0x05"), junk)
  644. }
  645. root, _ := accTrie.Commit(nil)
  646. t.Logf("root: %x", root)
  647. triedb.Commit(root, false, nil)
  648. snap := generateSnapshot(diskdb, triedb, 16, root)
  649. select {
  650. case <-snap.genPending:
  651. // Snapshot generation succeeded
  652. case <-time.After(250 * time.Millisecond):
  653. t.Errorf("Snapshot generation failed")
  654. }
  655. checkSnapRoot(t, snap, root)
  656. // Signal abortion to the generator and wait for it to tear down
  657. stop := make(chan *generatorStats)
  658. snap.genAbort <- stop
  659. <-stop
  660. // If we now inspect the snap db, there should exist no extraneous storage items
  661. if data := rawdb.ReadStorageSnapshot(diskdb, hashData([]byte("acc-2")), hashData([]byte("b-key-1"))); data != nil {
  662. t.Fatalf("expected slot to be removed, got %v", string(data))
  663. }
  664. }
  665. func TestGenerateFromEmptySnap(t *testing.T) {
  666. //enableLogging()
  667. accountCheckRange = 10
  668. storageCheckRange = 20
  669. helper := newHelper()
  670. stRoot := helper.makeStorageTrie([]string{"key-1", "key-2", "key-3"}, []string{"val-1", "val-2", "val-3"})
  671. // Add 1K accounts to the trie
  672. for i := 0; i < 400; i++ {
  673. helper.addTrieAccount(fmt.Sprintf("acc-%d", i),
  674. &Account{Balance: big.NewInt(1), Root: stRoot, CodeHash: emptyCode.Bytes()})
  675. }
  676. root, snap := helper.Generate()
  677. t.Logf("Root: %#x\n", root) // Root: 0x6f7af6d2e1a1bf2b84a3beb3f8b64388465fbc1e274ca5d5d3fc787ca78f59e4
  678. select {
  679. case <-snap.genPending:
  680. // Snapshot generation succeeded
  681. case <-time.After(1 * time.Second):
  682. t.Errorf("Snapshot generation failed")
  683. }
  684. checkSnapRoot(t, snap, root)
  685. // Signal abortion to the generator and wait for it to tear down
  686. stop := make(chan *generatorStats)
  687. snap.genAbort <- stop
  688. <-stop
  689. }
  690. // Tests that snapshot generation with existent flat state, where the flat state
  691. // storage is correct, but incomplete.
  692. // The incomplete part is on the second range
  693. // snap: [ 0x01, 0x02, 0x03, 0x04] , [ 0x05, 0x06, 0x07, {missing}] (with storageCheck = 4)
  694. // trie: 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08
  695. // This hits a case where the snap verification passes, but there are more elements in the trie
  696. // which we must also add.
  697. func TestGenerateWithIncompleteStorage(t *testing.T) {
  698. storageCheckRange = 4
  699. helper := newHelper()
  700. stKeys := []string{"1", "2", "3", "4", "5", "6", "7", "8"}
  701. stVals := []string{"v1", "v2", "v3", "v4", "v5", "v6", "v7", "v8"}
  702. stRoot := helper.makeStorageTrie(stKeys, stVals)
  703. // We add 8 accounts, each one is missing exactly one of the storage slots. This means
  704. // we don't have to order the keys and figure out exactly which hash-key winds up
  705. // on the sensitive spots at the boundaries
  706. for i := 0; i < 8; i++ {
  707. accKey := fmt.Sprintf("acc-%d", i)
  708. helper.addAccount(accKey, &Account{Balance: big.NewInt(int64(i)), Root: stRoot, CodeHash: emptyCode.Bytes()})
  709. var moddedKeys []string
  710. var moddedVals []string
  711. for ii := 0; ii < 8; ii++ {
  712. if ii != i {
  713. moddedKeys = append(moddedKeys, stKeys[ii])
  714. moddedVals = append(moddedVals, stVals[ii])
  715. }
  716. }
  717. helper.addSnapStorage(accKey, moddedKeys, moddedVals)
  718. }
  719. root, snap := helper.Generate()
  720. t.Logf("Root: %#x\n", root) // Root: 0xca73f6f05ba4ca3024ef340ef3dfca8fdabc1b677ff13f5a9571fd49c16e67ff
  721. select {
  722. case <-snap.genPending:
  723. // Snapshot generation succeeded
  724. case <-time.After(250 * time.Millisecond):
  725. t.Errorf("Snapshot generation failed")
  726. }
  727. checkSnapRoot(t, snap, root)
  728. // Signal abortion to the generator and wait for it to tear down
  729. stop := make(chan *generatorStats)
  730. snap.genAbort <- stop
  731. <-stop
  732. }