sync_test.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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 trie
  17. import (
  18. "bytes"
  19. "testing"
  20. "github.com/ethereum/go-ethereum/common"
  21. "github.com/ethereum/go-ethereum/ethdb"
  22. )
  23. // makeTestTrie create a sample test trie to test node-wise reconstruction.
  24. func makeTestTrie() (ethdb.Database, *Trie, map[string][]byte) {
  25. // Create an empty trie
  26. db, _ := ethdb.NewMemDatabase()
  27. trie, _ := New(common.Hash{}, db)
  28. // Fill it with some arbitrary data
  29. content := make(map[string][]byte)
  30. for i := byte(0); i < 255; i++ {
  31. // Map the same data under multiple keys
  32. key, val := common.LeftPadBytes([]byte{1, i}, 32), []byte{i}
  33. content[string(key)] = val
  34. trie.Update(key, val)
  35. key, val = common.LeftPadBytes([]byte{2, i}, 32), []byte{i}
  36. content[string(key)] = val
  37. trie.Update(key, val)
  38. // Add some other data to inflate th trie
  39. for j := byte(3); j < 13; j++ {
  40. key, val = common.LeftPadBytes([]byte{j, i}, 32), []byte{j, i}
  41. content[string(key)] = val
  42. trie.Update(key, val)
  43. }
  44. }
  45. trie.Commit()
  46. // Remove any potentially cached data from the test trie creation
  47. globalCache.Clear()
  48. // Return the generated trie
  49. return db, trie, content
  50. }
  51. // checkTrieContents cross references a reconstructed trie with an expected data
  52. // content map.
  53. func checkTrieContents(t *testing.T, db Database, root []byte, content map[string][]byte) {
  54. // Remove any potentially cached data from the trie synchronisation
  55. globalCache.Clear()
  56. // Check root availability and trie contents
  57. trie, err := New(common.BytesToHash(root), db)
  58. if err != nil {
  59. t.Fatalf("failed to create trie at %x: %v", root, err)
  60. }
  61. if err := checkTrieConsistency(db, common.BytesToHash(root)); err != nil {
  62. t.Fatalf("inconsistent trie at %x: %v", root, err)
  63. }
  64. for key, val := range content {
  65. if have := trie.Get([]byte(key)); bytes.Compare(have, val) != 0 {
  66. t.Errorf("entry %x: content mismatch: have %x, want %x", key, have, val)
  67. }
  68. }
  69. }
  70. // checkTrieConsistency checks that all nodes in a trie are indeed present.
  71. func checkTrieConsistency(db Database, root common.Hash) error {
  72. // Remove any potentially cached data from the test trie creation or previous checks
  73. globalCache.Clear()
  74. // Create and iterate a trie rooted in a subnode
  75. trie, err := New(root, db)
  76. if err != nil {
  77. return nil // // Consider a non existent state consistent
  78. }
  79. it := NewNodeIterator(trie)
  80. for it.Next() {
  81. }
  82. return it.Error
  83. }
  84. // Tests that an empty trie is not scheduled for syncing.
  85. func TestEmptyTrieSync(t *testing.T) {
  86. emptyA, _ := New(common.Hash{}, nil)
  87. emptyB, _ := New(emptyRoot, nil)
  88. for i, trie := range []*Trie{emptyA, emptyB} {
  89. db, _ := ethdb.NewMemDatabase()
  90. if req := NewTrieSync(common.BytesToHash(trie.Root()), db, nil).Missing(1); len(req) != 0 {
  91. t.Errorf("test %d: content requested for empty trie: %v", i, req)
  92. }
  93. }
  94. }
  95. // Tests that given a root hash, a trie can sync iteratively on a single thread,
  96. // requesting retrieval tasks and returning all of them in one go.
  97. func TestIterativeTrieSyncIndividual(t *testing.T) { testIterativeTrieSync(t, 1) }
  98. func TestIterativeTrieSyncBatched(t *testing.T) { testIterativeTrieSync(t, 100) }
  99. func testIterativeTrieSync(t *testing.T, batch int) {
  100. // Create a random trie to copy
  101. srcDb, srcTrie, srcData := makeTestTrie()
  102. // Create a destination trie and sync with the scheduler
  103. dstDb, _ := ethdb.NewMemDatabase()
  104. sched := NewTrieSync(common.BytesToHash(srcTrie.Root()), dstDb, nil)
  105. queue := append([]common.Hash{}, sched.Missing(batch)...)
  106. for len(queue) > 0 {
  107. results := make([]SyncResult, len(queue))
  108. for i, hash := range queue {
  109. data, err := srcDb.Get(hash.Bytes())
  110. if err != nil {
  111. t.Fatalf("failed to retrieve node data for %x: %v", hash, err)
  112. }
  113. results[i] = SyncResult{hash, data}
  114. }
  115. if index, err := sched.Process(results); err != nil {
  116. t.Fatalf("failed to process result #%d: %v", index, err)
  117. }
  118. queue = append(queue[:0], sched.Missing(batch)...)
  119. }
  120. // Cross check that the two tries are in sync
  121. checkTrieContents(t, dstDb, srcTrie.Root(), srcData)
  122. }
  123. // Tests that the trie scheduler can correctly reconstruct the state even if only
  124. // partial results are returned, and the others sent only later.
  125. func TestIterativeDelayedTrieSync(t *testing.T) {
  126. // Create a random trie to copy
  127. srcDb, srcTrie, srcData := makeTestTrie()
  128. // Create a destination trie and sync with the scheduler
  129. dstDb, _ := ethdb.NewMemDatabase()
  130. sched := NewTrieSync(common.BytesToHash(srcTrie.Root()), dstDb, nil)
  131. queue := append([]common.Hash{}, sched.Missing(10000)...)
  132. for len(queue) > 0 {
  133. // Sync only half of the scheduled nodes
  134. results := make([]SyncResult, len(queue)/2+1)
  135. for i, hash := range queue[:len(results)] {
  136. data, err := srcDb.Get(hash.Bytes())
  137. if err != nil {
  138. t.Fatalf("failed to retrieve node data for %x: %v", hash, err)
  139. }
  140. results[i] = SyncResult{hash, data}
  141. }
  142. if index, err := sched.Process(results); err != nil {
  143. t.Fatalf("failed to process result #%d: %v", index, err)
  144. }
  145. queue = append(queue[len(results):], sched.Missing(10000)...)
  146. }
  147. // Cross check that the two tries are in sync
  148. checkTrieContents(t, dstDb, srcTrie.Root(), srcData)
  149. }
  150. // Tests that given a root hash, a trie can sync iteratively on a single thread,
  151. // requesting retrieval tasks and returning all of them in one go, however in a
  152. // random order.
  153. func TestIterativeRandomTrieSyncIndividual(t *testing.T) { testIterativeRandomTrieSync(t, 1) }
  154. func TestIterativeRandomTrieSyncBatched(t *testing.T) { testIterativeRandomTrieSync(t, 100) }
  155. func testIterativeRandomTrieSync(t *testing.T, batch int) {
  156. // Create a random trie to copy
  157. srcDb, srcTrie, srcData := makeTestTrie()
  158. // Create a destination trie and sync with the scheduler
  159. dstDb, _ := ethdb.NewMemDatabase()
  160. sched := NewTrieSync(common.BytesToHash(srcTrie.Root()), dstDb, nil)
  161. queue := make(map[common.Hash]struct{})
  162. for _, hash := range sched.Missing(batch) {
  163. queue[hash] = struct{}{}
  164. }
  165. for len(queue) > 0 {
  166. // Fetch all the queued nodes in a random order
  167. results := make([]SyncResult, 0, len(queue))
  168. for hash, _ := range queue {
  169. data, err := srcDb.Get(hash.Bytes())
  170. if err != nil {
  171. t.Fatalf("failed to retrieve node data for %x: %v", hash, err)
  172. }
  173. results = append(results, SyncResult{hash, data})
  174. }
  175. // Feed the retrieved results back and queue new tasks
  176. if index, err := sched.Process(results); err != nil {
  177. t.Fatalf("failed to process result #%d: %v", index, err)
  178. }
  179. queue = make(map[common.Hash]struct{})
  180. for _, hash := range sched.Missing(batch) {
  181. queue[hash] = struct{}{}
  182. }
  183. }
  184. // Cross check that the two tries are in sync
  185. checkTrieContents(t, dstDb, srcTrie.Root(), srcData)
  186. }
  187. // Tests that the trie scheduler can correctly reconstruct the state even if only
  188. // partial results are returned (Even those randomly), others sent only later.
  189. func TestIterativeRandomDelayedTrieSync(t *testing.T) {
  190. // Create a random trie to copy
  191. srcDb, srcTrie, srcData := makeTestTrie()
  192. // Create a destination trie and sync with the scheduler
  193. dstDb, _ := ethdb.NewMemDatabase()
  194. sched := NewTrieSync(common.BytesToHash(srcTrie.Root()), dstDb, nil)
  195. queue := make(map[common.Hash]struct{})
  196. for _, hash := range sched.Missing(10000) {
  197. queue[hash] = struct{}{}
  198. }
  199. for len(queue) > 0 {
  200. // Sync only half of the scheduled nodes, even those in random order
  201. results := make([]SyncResult, 0, len(queue)/2+1)
  202. for hash, _ := range queue {
  203. data, err := srcDb.Get(hash.Bytes())
  204. if err != nil {
  205. t.Fatalf("failed to retrieve node data for %x: %v", hash, err)
  206. }
  207. results = append(results, SyncResult{hash, data})
  208. if len(results) >= cap(results) {
  209. break
  210. }
  211. }
  212. // Feed the retrieved results back and queue new tasks
  213. if index, err := sched.Process(results); err != nil {
  214. t.Fatalf("failed to process result #%d: %v", index, err)
  215. }
  216. for _, result := range results {
  217. delete(queue, result.Hash)
  218. }
  219. for _, hash := range sched.Missing(10000) {
  220. queue[hash] = struct{}{}
  221. }
  222. }
  223. // Cross check that the two tries are in sync
  224. checkTrieContents(t, dstDb, srcTrie.Root(), srcData)
  225. }
  226. // Tests that a trie sync will not request nodes multiple times, even if they
  227. // have such references.
  228. func TestDuplicateAvoidanceTrieSync(t *testing.T) {
  229. // Create a random trie to copy
  230. srcDb, srcTrie, srcData := makeTestTrie()
  231. // Create a destination trie and sync with the scheduler
  232. dstDb, _ := ethdb.NewMemDatabase()
  233. sched := NewTrieSync(common.BytesToHash(srcTrie.Root()), dstDb, nil)
  234. queue := append([]common.Hash{}, sched.Missing(0)...)
  235. requested := make(map[common.Hash]struct{})
  236. for len(queue) > 0 {
  237. results := make([]SyncResult, len(queue))
  238. for i, hash := range queue {
  239. data, err := srcDb.Get(hash.Bytes())
  240. if err != nil {
  241. t.Fatalf("failed to retrieve node data for %x: %v", hash, err)
  242. }
  243. if _, ok := requested[hash]; ok {
  244. t.Errorf("hash %x already requested once", hash)
  245. }
  246. requested[hash] = struct{}{}
  247. results[i] = SyncResult{hash, data}
  248. }
  249. if index, err := sched.Process(results); err != nil {
  250. t.Fatalf("failed to process result #%d: %v", index, err)
  251. }
  252. queue = append(queue[:0], sched.Missing(0)...)
  253. }
  254. // Cross check that the two tries are in sync
  255. checkTrieContents(t, dstDb, srcTrie.Root(), srcData)
  256. }
  257. // Tests that at any point in time during a sync, only complete sub-tries are in
  258. // the database.
  259. func TestIncompleteTrieSync(t *testing.T) {
  260. // Create a random trie to copy
  261. srcDb, srcTrie, _ := makeTestTrie()
  262. // Create a destination trie and sync with the scheduler
  263. dstDb, _ := ethdb.NewMemDatabase()
  264. sched := NewTrieSync(common.BytesToHash(srcTrie.Root()), dstDb, nil)
  265. added := []common.Hash{}
  266. queue := append([]common.Hash{}, sched.Missing(1)...)
  267. for len(queue) > 0 {
  268. // Fetch a batch of trie nodes
  269. results := make([]SyncResult, len(queue))
  270. for i, hash := range queue {
  271. data, err := srcDb.Get(hash.Bytes())
  272. if err != nil {
  273. t.Fatalf("failed to retrieve node data for %x: %v", hash, err)
  274. }
  275. results[i] = SyncResult{hash, data}
  276. }
  277. // Process each of the trie nodes
  278. if index, err := sched.Process(results); err != nil {
  279. t.Fatalf("failed to process result #%d: %v", index, err)
  280. }
  281. for _, result := range results {
  282. added = append(added, result.Hash)
  283. }
  284. // Check that all known sub-tries in the synced trie is complete
  285. for _, root := range added {
  286. if err := checkTrieConsistency(dstDb, root); err != nil {
  287. t.Fatalf("trie inconsistent: %v", err)
  288. }
  289. }
  290. // Fetch the next batch to retrieve
  291. queue = append(queue[:0], sched.Missing(1)...)
  292. }
  293. // Sanity check that removing any node from the database is detected
  294. for _, node := range added[1:] {
  295. key := node.Bytes()
  296. value, _ := dstDb.Get(key)
  297. dstDb.Delete(key)
  298. if err := checkTrieConsistency(dstDb, added[0]); err == nil {
  299. t.Fatalf("trie inconsistency not caught, missing: %x", key)
  300. }
  301. dstDb.Put(key, value)
  302. }
  303. }