sync_test.go 11 KB

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