sync_test.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. // Copyright 2015 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 state
  17. import (
  18. "bytes"
  19. "math/big"
  20. "testing"
  21. "github.com/ethereum/go-ethereum/common"
  22. "github.com/ethereum/go-ethereum/core/rawdb"
  23. "github.com/ethereum/go-ethereum/crypto"
  24. "github.com/ethereum/go-ethereum/ethdb"
  25. "github.com/ethereum/go-ethereum/ethdb/memorydb"
  26. "github.com/ethereum/go-ethereum/rlp"
  27. "github.com/ethereum/go-ethereum/trie"
  28. )
  29. // testAccount is the data associated with an account used by the state tests.
  30. type testAccount struct {
  31. address common.Address
  32. balance *big.Int
  33. nonce uint64
  34. code []byte
  35. }
  36. // makeTestState create a sample test state to test node-wise reconstruction.
  37. func makeTestState() (Database, common.Hash, []*testAccount) {
  38. // Create an empty state
  39. db := NewDatabase(rawdb.NewMemoryDatabase())
  40. state, _ := New(common.Hash{}, db, nil)
  41. // Fill it with some arbitrary data
  42. var accounts []*testAccount
  43. for i := byte(0); i < 96; i++ {
  44. obj := state.GetOrNewStateObject(common.BytesToAddress([]byte{i}))
  45. acc := &testAccount{address: common.BytesToAddress([]byte{i})}
  46. obj.AddBalance(big.NewInt(int64(11 * i)))
  47. acc.balance = big.NewInt(int64(11 * i))
  48. obj.SetNonce(uint64(42 * i))
  49. acc.nonce = uint64(42 * i)
  50. if i%3 == 0 {
  51. obj.SetCode(crypto.Keccak256Hash([]byte{i, i, i, i, i}), []byte{i, i, i, i, i})
  52. acc.code = []byte{i, i, i, i, i}
  53. }
  54. if i%5 == 0 {
  55. for j := byte(0); j < 5; j++ {
  56. hash := crypto.Keccak256Hash([]byte{i, i, i, i, i, j, j})
  57. obj.SetState(db, hash, hash)
  58. }
  59. }
  60. state.updateStateObject(obj)
  61. accounts = append(accounts, acc)
  62. }
  63. root, _ := state.Commit(false)
  64. // Return the generated state
  65. return db, root, accounts
  66. }
  67. // checkStateAccounts cross references a reconstructed state with an expected
  68. // account array.
  69. func checkStateAccounts(t *testing.T, db ethdb.Database, root common.Hash, accounts []*testAccount) {
  70. // Check root availability and state contents
  71. state, err := New(root, NewDatabase(db), nil)
  72. if err != nil {
  73. t.Fatalf("failed to create state trie at %x: %v", root, err)
  74. }
  75. if err := checkStateConsistency(db, root); err != nil {
  76. t.Fatalf("inconsistent state trie at %x: %v", root, err)
  77. }
  78. for i, acc := range accounts {
  79. if balance := state.GetBalance(acc.address); balance.Cmp(acc.balance) != 0 {
  80. t.Errorf("account %d: balance mismatch: have %v, want %v", i, balance, acc.balance)
  81. }
  82. if nonce := state.GetNonce(acc.address); nonce != acc.nonce {
  83. t.Errorf("account %d: nonce mismatch: have %v, want %v", i, nonce, acc.nonce)
  84. }
  85. if code := state.GetCode(acc.address); !bytes.Equal(code, acc.code) {
  86. t.Errorf("account %d: code mismatch: have %x, want %x", i, code, acc.code)
  87. }
  88. }
  89. }
  90. // checkTrieConsistency checks that all nodes in a (sub-)trie are indeed present.
  91. func checkTrieConsistency(db ethdb.Database, root common.Hash) error {
  92. if v, _ := db.Get(root[:]); v == nil {
  93. return nil // Consider a non existent state consistent.
  94. }
  95. trie, err := trie.New(root, trie.NewDatabase(db))
  96. if err != nil {
  97. return err
  98. }
  99. it := trie.NodeIterator(nil)
  100. for it.Next(true) {
  101. }
  102. return it.Error()
  103. }
  104. // checkStateConsistency checks that all data of a state root is present.
  105. func checkStateConsistency(db ethdb.Database, root common.Hash) error {
  106. // Create and iterate a state trie rooted in a sub-node
  107. if _, err := db.Get(root.Bytes()); err != nil {
  108. return nil // Consider a non existent state consistent.
  109. }
  110. state, err := New(root, NewDatabase(db), nil)
  111. if err != nil {
  112. return err
  113. }
  114. it := NewNodeIterator(state)
  115. for it.Next() {
  116. }
  117. return it.Error
  118. }
  119. // Tests that an empty state is not scheduled for syncing.
  120. func TestEmptyStateSync(t *testing.T) {
  121. empty := common.HexToHash("56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421")
  122. sync := NewStateSync(empty, rawdb.NewMemoryDatabase(), trie.NewSyncBloom(1, memorydb.New()), nil)
  123. if nodes, paths, codes := sync.Missing(1); len(nodes) != 0 || len(paths) != 0 || len(codes) != 0 {
  124. t.Errorf(" content requested for empty state: %v, %v, %v", nodes, paths, codes)
  125. }
  126. }
  127. // Tests that given a root hash, a state can sync iteratively on a single thread,
  128. // requesting retrieval tasks and returning all of them in one go.
  129. func TestIterativeStateSyncIndividual(t *testing.T) {
  130. testIterativeStateSync(t, 1, false, false)
  131. }
  132. func TestIterativeStateSyncBatched(t *testing.T) {
  133. testIterativeStateSync(t, 100, false, false)
  134. }
  135. func TestIterativeStateSyncIndividualFromDisk(t *testing.T) {
  136. testIterativeStateSync(t, 1, true, false)
  137. }
  138. func TestIterativeStateSyncBatchedFromDisk(t *testing.T) {
  139. testIterativeStateSync(t, 100, true, false)
  140. }
  141. func TestIterativeStateSyncIndividualByPath(t *testing.T) {
  142. testIterativeStateSync(t, 1, false, true)
  143. }
  144. func TestIterativeStateSyncBatchedByPath(t *testing.T) {
  145. testIterativeStateSync(t, 100, false, true)
  146. }
  147. func testIterativeStateSync(t *testing.T, count int, commit bool, bypath bool) {
  148. // Create a random state to copy
  149. srcDb, srcRoot, srcAccounts := makeTestState()
  150. if commit {
  151. srcDb.TrieDB().Commit(srcRoot, false, nil)
  152. }
  153. srcTrie, _ := trie.New(srcRoot, srcDb.TrieDB())
  154. // Create a destination state and sync with the scheduler
  155. dstDb := rawdb.NewMemoryDatabase()
  156. sched := NewStateSync(srcRoot, dstDb, trie.NewSyncBloom(1, dstDb), nil)
  157. nodes, paths, codes := sched.Missing(count)
  158. var (
  159. hashQueue []common.Hash
  160. pathQueue []trie.SyncPath
  161. )
  162. if !bypath {
  163. hashQueue = append(append(hashQueue[:0], nodes...), codes...)
  164. } else {
  165. hashQueue = append(hashQueue[:0], codes...)
  166. pathQueue = append(pathQueue[:0], paths...)
  167. }
  168. for len(hashQueue)+len(pathQueue) > 0 {
  169. results := make([]trie.SyncResult, len(hashQueue)+len(pathQueue))
  170. for i, hash := range hashQueue {
  171. data, err := srcDb.TrieDB().Node(hash)
  172. if err != nil {
  173. data, err = srcDb.ContractCode(common.Hash{}, hash)
  174. }
  175. if err != nil {
  176. t.Fatalf("failed to retrieve node data for hash %x", hash)
  177. }
  178. results[i] = trie.SyncResult{Hash: hash, Data: data}
  179. }
  180. for i, path := range pathQueue {
  181. if len(path) == 1 {
  182. data, _, err := srcTrie.TryGetNode(path[0])
  183. if err != nil {
  184. t.Fatalf("failed to retrieve node data for path %x: %v", path, err)
  185. }
  186. results[len(hashQueue)+i] = trie.SyncResult{Hash: crypto.Keccak256Hash(data), Data: data}
  187. } else {
  188. var acc Account
  189. if err := rlp.DecodeBytes(srcTrie.Get(path[0]), &acc); err != nil {
  190. t.Fatalf("failed to decode account on path %x: %v", path, err)
  191. }
  192. stTrie, err := trie.New(acc.Root, srcDb.TrieDB())
  193. if err != nil {
  194. t.Fatalf("failed to retriev storage trie for path %x: %v", path, err)
  195. }
  196. data, _, err := stTrie.TryGetNode(path[1])
  197. if err != nil {
  198. t.Fatalf("failed to retrieve node data for path %x: %v", path, err)
  199. }
  200. results[len(hashQueue)+i] = trie.SyncResult{Hash: crypto.Keccak256Hash(data), Data: data}
  201. }
  202. }
  203. for _, result := range results {
  204. if err := sched.Process(result); err != nil {
  205. t.Errorf("failed to process result %v", err)
  206. }
  207. }
  208. batch := dstDb.NewBatch()
  209. if err := sched.Commit(batch); err != nil {
  210. t.Fatalf("failed to commit data: %v", err)
  211. }
  212. batch.Write()
  213. nodes, paths, codes = sched.Missing(count)
  214. if !bypath {
  215. hashQueue = append(append(hashQueue[:0], nodes...), codes...)
  216. } else {
  217. hashQueue = append(hashQueue[:0], codes...)
  218. pathQueue = append(pathQueue[:0], paths...)
  219. }
  220. }
  221. // Cross check that the two states are in sync
  222. checkStateAccounts(t, dstDb, srcRoot, srcAccounts)
  223. }
  224. // Tests that the trie scheduler can correctly reconstruct the state even if only
  225. // partial results are returned, and the others sent only later.
  226. func TestIterativeDelayedStateSync(t *testing.T) {
  227. // Create a random state to copy
  228. srcDb, srcRoot, srcAccounts := makeTestState()
  229. // Create a destination state and sync with the scheduler
  230. dstDb := rawdb.NewMemoryDatabase()
  231. sched := NewStateSync(srcRoot, dstDb, trie.NewSyncBloom(1, dstDb), nil)
  232. nodes, _, codes := sched.Missing(0)
  233. queue := append(append([]common.Hash{}, nodes...), codes...)
  234. for len(queue) > 0 {
  235. // Sync only half of the scheduled nodes
  236. results := make([]trie.SyncResult, len(queue)/2+1)
  237. for i, hash := range queue[:len(results)] {
  238. data, err := srcDb.TrieDB().Node(hash)
  239. if err != nil {
  240. data, err = srcDb.ContractCode(common.Hash{}, hash)
  241. }
  242. if err != nil {
  243. t.Fatalf("failed to retrieve node data for %x", hash)
  244. }
  245. results[i] = trie.SyncResult{Hash: hash, Data: data}
  246. }
  247. for _, result := range results {
  248. if err := sched.Process(result); err != nil {
  249. t.Fatalf("failed to process result %v", err)
  250. }
  251. }
  252. batch := dstDb.NewBatch()
  253. if err := sched.Commit(batch); err != nil {
  254. t.Fatalf("failed to commit data: %v", err)
  255. }
  256. batch.Write()
  257. nodes, _, codes = sched.Missing(0)
  258. queue = append(append(queue[len(results):], nodes...), codes...)
  259. }
  260. // Cross check that the two states are in sync
  261. checkStateAccounts(t, dstDb, srcRoot, srcAccounts)
  262. }
  263. // Tests that given a root hash, a trie can sync iteratively on a single thread,
  264. // requesting retrieval tasks and returning all of them in one go, however in a
  265. // random order.
  266. func TestIterativeRandomStateSyncIndividual(t *testing.T) { testIterativeRandomStateSync(t, 1) }
  267. func TestIterativeRandomStateSyncBatched(t *testing.T) { testIterativeRandomStateSync(t, 100) }
  268. func testIterativeRandomStateSync(t *testing.T, count int) {
  269. // Create a random state to copy
  270. srcDb, srcRoot, srcAccounts := makeTestState()
  271. // Create a destination state and sync with the scheduler
  272. dstDb := rawdb.NewMemoryDatabase()
  273. sched := NewStateSync(srcRoot, dstDb, trie.NewSyncBloom(1, dstDb), nil)
  274. queue := make(map[common.Hash]struct{})
  275. nodes, _, codes := sched.Missing(count)
  276. for _, hash := range append(nodes, codes...) {
  277. queue[hash] = struct{}{}
  278. }
  279. for len(queue) > 0 {
  280. // Fetch all the queued nodes in a random order
  281. results := make([]trie.SyncResult, 0, len(queue))
  282. for hash := range queue {
  283. data, err := srcDb.TrieDB().Node(hash)
  284. if err != nil {
  285. data, err = srcDb.ContractCode(common.Hash{}, hash)
  286. }
  287. if err != nil {
  288. t.Fatalf("failed to retrieve node data for %x", hash)
  289. }
  290. results = append(results, trie.SyncResult{Hash: hash, Data: data})
  291. }
  292. // Feed the retrieved results back and queue new tasks
  293. for _, result := range results {
  294. if err := sched.Process(result); err != nil {
  295. t.Fatalf("failed to process result %v", err)
  296. }
  297. }
  298. batch := dstDb.NewBatch()
  299. if err := sched.Commit(batch); err != nil {
  300. t.Fatalf("failed to commit data: %v", err)
  301. }
  302. batch.Write()
  303. queue = make(map[common.Hash]struct{})
  304. nodes, _, codes = sched.Missing(count)
  305. for _, hash := range append(nodes, codes...) {
  306. queue[hash] = struct{}{}
  307. }
  308. }
  309. // Cross check that the two states are in sync
  310. checkStateAccounts(t, dstDb, srcRoot, srcAccounts)
  311. }
  312. // Tests that the trie scheduler can correctly reconstruct the state even if only
  313. // partial results are returned (Even those randomly), others sent only later.
  314. func TestIterativeRandomDelayedStateSync(t *testing.T) {
  315. // Create a random state to copy
  316. srcDb, srcRoot, srcAccounts := makeTestState()
  317. // Create a destination state and sync with the scheduler
  318. dstDb := rawdb.NewMemoryDatabase()
  319. sched := NewStateSync(srcRoot, dstDb, trie.NewSyncBloom(1, dstDb), nil)
  320. queue := make(map[common.Hash]struct{})
  321. nodes, _, codes := sched.Missing(0)
  322. for _, hash := range append(nodes, codes...) {
  323. queue[hash] = struct{}{}
  324. }
  325. for len(queue) > 0 {
  326. // Sync only half of the scheduled nodes, even those in random order
  327. results := make([]trie.SyncResult, 0, len(queue)/2+1)
  328. for hash := range queue {
  329. delete(queue, hash)
  330. data, err := srcDb.TrieDB().Node(hash)
  331. if err != nil {
  332. data, err = srcDb.ContractCode(common.Hash{}, hash)
  333. }
  334. if err != nil {
  335. t.Fatalf("failed to retrieve node data for %x", hash)
  336. }
  337. results = append(results, trie.SyncResult{Hash: hash, Data: data})
  338. if len(results) >= cap(results) {
  339. break
  340. }
  341. }
  342. // Feed the retrieved results back and queue new tasks
  343. for _, result := range results {
  344. if err := sched.Process(result); err != nil {
  345. t.Fatalf("failed to process result %v", err)
  346. }
  347. }
  348. batch := dstDb.NewBatch()
  349. if err := sched.Commit(batch); err != nil {
  350. t.Fatalf("failed to commit data: %v", err)
  351. }
  352. batch.Write()
  353. for _, result := range results {
  354. delete(queue, result.Hash)
  355. }
  356. nodes, _, codes = sched.Missing(0)
  357. for _, hash := range append(nodes, codes...) {
  358. queue[hash] = struct{}{}
  359. }
  360. }
  361. // Cross check that the two states are in sync
  362. checkStateAccounts(t, dstDb, srcRoot, srcAccounts)
  363. }
  364. // Tests that at any point in time during a sync, only complete sub-tries are in
  365. // the database.
  366. func TestIncompleteStateSync(t *testing.T) {
  367. // Create a random state to copy
  368. srcDb, srcRoot, srcAccounts := makeTestState()
  369. // isCodeLookup to save some hashing
  370. var isCode = make(map[common.Hash]struct{})
  371. for _, acc := range srcAccounts {
  372. if len(acc.code) > 0 {
  373. isCode[crypto.Keccak256Hash(acc.code)] = struct{}{}
  374. }
  375. }
  376. isCode[common.BytesToHash(emptyCodeHash)] = struct{}{}
  377. checkTrieConsistency(srcDb.TrieDB().DiskDB().(ethdb.Database), srcRoot)
  378. // Create a destination state and sync with the scheduler
  379. dstDb := rawdb.NewMemoryDatabase()
  380. sched := NewStateSync(srcRoot, dstDb, trie.NewSyncBloom(1, dstDb), nil)
  381. var added []common.Hash
  382. nodes, _, codes := sched.Missing(1)
  383. queue := append(append([]common.Hash{}, nodes...), codes...)
  384. for len(queue) > 0 {
  385. // Fetch a batch of state nodes
  386. results := make([]trie.SyncResult, len(queue))
  387. for i, hash := range queue {
  388. data, err := srcDb.TrieDB().Node(hash)
  389. if err != nil {
  390. data, err = srcDb.ContractCode(common.Hash{}, hash)
  391. }
  392. if err != nil {
  393. t.Fatalf("failed to retrieve node data for %x", hash)
  394. }
  395. results[i] = trie.SyncResult{Hash: hash, Data: data}
  396. }
  397. // Process each of the state nodes
  398. for _, result := range results {
  399. if err := sched.Process(result); err != nil {
  400. t.Fatalf("failed to process result %v", err)
  401. }
  402. }
  403. batch := dstDb.NewBatch()
  404. if err := sched.Commit(batch); err != nil {
  405. t.Fatalf("failed to commit data: %v", err)
  406. }
  407. batch.Write()
  408. for _, result := range results {
  409. added = append(added, result.Hash)
  410. // Check that all known sub-tries added so far are complete or missing entirely.
  411. if _, ok := isCode[result.Hash]; ok {
  412. continue
  413. }
  414. // Can't use checkStateConsistency here because subtrie keys may have odd
  415. // length and crash in LeafKey.
  416. if err := checkTrieConsistency(dstDb, result.Hash); err != nil {
  417. t.Fatalf("state inconsistent: %v", err)
  418. }
  419. }
  420. // Fetch the next batch to retrieve
  421. nodes, _, codes = sched.Missing(1)
  422. queue = append(append(queue[:0], nodes...), codes...)
  423. }
  424. // Sanity check that removing any node from the database is detected
  425. for _, node := range added[1:] {
  426. var (
  427. key = node.Bytes()
  428. _, code = isCode[node]
  429. val []byte
  430. )
  431. if code {
  432. val = rawdb.ReadCode(dstDb, node)
  433. rawdb.DeleteCode(dstDb, node)
  434. } else {
  435. val = rawdb.ReadTrieNode(dstDb, node)
  436. rawdb.DeleteTrieNode(dstDb, node)
  437. }
  438. if err := checkStateConsistency(dstDb, added[0]); err == nil {
  439. t.Fatalf("trie inconsistency not caught, missing: %x", key)
  440. }
  441. if code {
  442. rawdb.WriteCode(dstDb, node, val)
  443. } else {
  444. rawdb.WriteTrieNode(dstDb, node, val)
  445. }
  446. }
  447. }