sync_test.go 16 KB

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