sync_test.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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() (*Database, *Trie, map[string][]byte) {
  25. // Create an empty trie
  26. diskdb, _ := ethdb.NewMemDatabase()
  27. triedb := NewDatabase(diskdb)
  28. trie, _ := New(common.Hash{}, triedb)
  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 the 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(nil)
  47. // Return the generated trie
  48. return triedb, trie, content
  49. }
  50. // checkTrieContents cross references a reconstructed trie with an expected data
  51. // content map.
  52. func checkTrieContents(t *testing.T, db *Database, root []byte, content map[string][]byte) {
  53. // Check root availability and trie contents
  54. trie, err := New(common.BytesToHash(root), db)
  55. if err != nil {
  56. t.Fatalf("failed to create trie at %x: %v", root, err)
  57. }
  58. if err := checkTrieConsistency(db, common.BytesToHash(root)); err != nil {
  59. t.Fatalf("inconsistent trie at %x: %v", root, err)
  60. }
  61. for key, val := range content {
  62. if have := trie.Get([]byte(key)); !bytes.Equal(have, val) {
  63. t.Errorf("entry %x: content mismatch: have %x, want %x", key, have, val)
  64. }
  65. }
  66. }
  67. // checkTrieConsistency checks that all nodes in a trie are indeed present.
  68. func checkTrieConsistency(db *Database, root common.Hash) error {
  69. // Create and iterate a trie rooted in a subnode
  70. trie, err := New(root, db)
  71. if err != nil {
  72. return nil // Consider a non existent state consistent
  73. }
  74. it := trie.NodeIterator(nil)
  75. for it.Next(true) {
  76. }
  77. return it.Error()
  78. }
  79. // Tests that an empty trie is not scheduled for syncing.
  80. func TestEmptyTrieSync(t *testing.T) {
  81. diskdbA, _ := ethdb.NewMemDatabase()
  82. triedbA := NewDatabase(diskdbA)
  83. diskdbB, _ := ethdb.NewMemDatabase()
  84. triedbB := NewDatabase(diskdbB)
  85. emptyA, _ := New(common.Hash{}, triedbA)
  86. emptyB, _ := New(emptyRoot, triedbB)
  87. for i, trie := range []*Trie{emptyA, emptyB} {
  88. diskdb, _ := ethdb.NewMemDatabase()
  89. if req := NewTrieSync(trie.Hash(), diskdb, nil).Missing(1); len(req) != 0 {
  90. t.Errorf("test %d: content requested for empty trie: %v", i, req)
  91. }
  92. }
  93. }
  94. // Tests that given a root hash, a trie can sync iteratively on a single thread,
  95. // requesting retrieval tasks and returning all of them in one go.
  96. func TestIterativeTrieSyncIndividual(t *testing.T) { testIterativeTrieSync(t, 1) }
  97. func TestIterativeTrieSyncBatched(t *testing.T) { testIterativeTrieSync(t, 100) }
  98. func testIterativeTrieSync(t *testing.T, batch int) {
  99. // Create a random trie to copy
  100. srcDb, srcTrie, srcData := makeTestTrie()
  101. // Create a destination trie and sync with the scheduler
  102. diskdb, _ := ethdb.NewMemDatabase()
  103. triedb := NewDatabase(diskdb)
  104. sched := NewTrieSync(srcTrie.Hash(), diskdb, 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.Node(hash)
  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. if index, err := sched.Commit(diskdb); err != nil {
  119. t.Fatalf("failed to commit data #%d: %v", index, err)
  120. }
  121. queue = append(queue[:0], sched.Missing(batch)...)
  122. }
  123. // Cross check that the two tries are in sync
  124. checkTrieContents(t, triedb, srcTrie.Root(), srcData)
  125. }
  126. // Tests that the trie scheduler can correctly reconstruct the state even if only
  127. // partial results are returned, and the others sent only later.
  128. func TestIterativeDelayedTrieSync(t *testing.T) {
  129. // Create a random trie to copy
  130. srcDb, srcTrie, srcData := makeTestTrie()
  131. // Create a destination trie and sync with the scheduler
  132. diskdb, _ := ethdb.NewMemDatabase()
  133. triedb := NewDatabase(diskdb)
  134. sched := NewTrieSync(srcTrie.Hash(), diskdb, nil)
  135. queue := append([]common.Hash{}, sched.Missing(10000)...)
  136. for len(queue) > 0 {
  137. // Sync only half of the scheduled nodes
  138. results := make([]SyncResult, len(queue)/2+1)
  139. for i, hash := range queue[:len(results)] {
  140. data, err := srcDb.Node(hash)
  141. if err != nil {
  142. t.Fatalf("failed to retrieve node data for %x: %v", hash, err)
  143. }
  144. results[i] = SyncResult{hash, data}
  145. }
  146. if _, index, err := sched.Process(results); err != nil {
  147. t.Fatalf("failed to process result #%d: %v", index, err)
  148. }
  149. if index, err := sched.Commit(diskdb); err != nil {
  150. t.Fatalf("failed to commit data #%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, triedb, 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. diskdb, _ := ethdb.NewMemDatabase()
  167. triedb := NewDatabase(diskdb)
  168. sched := NewTrieSync(srcTrie.Hash(), diskdb, nil)
  169. queue := make(map[common.Hash]struct{})
  170. for _, hash := range sched.Missing(batch) {
  171. queue[hash] = struct{}{}
  172. }
  173. for len(queue) > 0 {
  174. // Fetch all the queued nodes in a random order
  175. results := make([]SyncResult, 0, len(queue))
  176. for hash := range queue {
  177. data, err := srcDb.Node(hash)
  178. if err != nil {
  179. t.Fatalf("failed to retrieve node data for %x: %v", hash, err)
  180. }
  181. results = append(results, SyncResult{hash, data})
  182. }
  183. // Feed the retrieved results back and queue new tasks
  184. if _, index, err := sched.Process(results); err != nil {
  185. t.Fatalf("failed to process result #%d: %v", index, err)
  186. }
  187. if index, err := sched.Commit(diskdb); err != nil {
  188. t.Fatalf("failed to commit data #%d: %v", index, err)
  189. }
  190. queue = make(map[common.Hash]struct{})
  191. for _, hash := range sched.Missing(batch) {
  192. queue[hash] = struct{}{}
  193. }
  194. }
  195. // Cross check that the two tries are in sync
  196. checkTrieContents(t, triedb, srcTrie.Root(), srcData)
  197. }
  198. // Tests that the trie scheduler can correctly reconstruct the state even if only
  199. // partial results are returned (Even those randomly), others sent only later.
  200. func TestIterativeRandomDelayedTrieSync(t *testing.T) {
  201. // Create a random trie to copy
  202. srcDb, srcTrie, srcData := makeTestTrie()
  203. // Create a destination trie and sync with the scheduler
  204. diskdb, _ := ethdb.NewMemDatabase()
  205. triedb := NewDatabase(diskdb)
  206. sched := NewTrieSync(srcTrie.Hash(), diskdb, nil)
  207. queue := make(map[common.Hash]struct{})
  208. for _, hash := range sched.Missing(10000) {
  209. queue[hash] = struct{}{}
  210. }
  211. for len(queue) > 0 {
  212. // Sync only half of the scheduled nodes, even those in random order
  213. results := make([]SyncResult, 0, len(queue)/2+1)
  214. for hash := range queue {
  215. data, err := srcDb.Node(hash)
  216. if err != nil {
  217. t.Fatalf("failed to retrieve node data for %x: %v", hash, err)
  218. }
  219. results = append(results, SyncResult{hash, data})
  220. if len(results) >= cap(results) {
  221. break
  222. }
  223. }
  224. // Feed the retrieved results back and queue new tasks
  225. if _, index, err := sched.Process(results); err != nil {
  226. t.Fatalf("failed to process result #%d: %v", index, err)
  227. }
  228. if index, err := sched.Commit(diskdb); err != nil {
  229. t.Fatalf("failed to commit data #%d: %v", index, err)
  230. }
  231. for _, result := range results {
  232. delete(queue, result.Hash)
  233. }
  234. for _, hash := range sched.Missing(10000) {
  235. queue[hash] = struct{}{}
  236. }
  237. }
  238. // Cross check that the two tries are in sync
  239. checkTrieContents(t, triedb, srcTrie.Root(), srcData)
  240. }
  241. // Tests that a trie sync will not request nodes multiple times, even if they
  242. // have such references.
  243. func TestDuplicateAvoidanceTrieSync(t *testing.T) {
  244. // Create a random trie to copy
  245. srcDb, srcTrie, srcData := makeTestTrie()
  246. // Create a destination trie and sync with the scheduler
  247. diskdb, _ := ethdb.NewMemDatabase()
  248. triedb := NewDatabase(diskdb)
  249. sched := NewTrieSync(srcTrie.Hash(), diskdb, nil)
  250. queue := append([]common.Hash{}, sched.Missing(0)...)
  251. requested := make(map[common.Hash]struct{})
  252. for len(queue) > 0 {
  253. results := make([]SyncResult, len(queue))
  254. for i, hash := range queue {
  255. data, err := srcDb.Node(hash)
  256. if err != nil {
  257. t.Fatalf("failed to retrieve node data for %x: %v", hash, err)
  258. }
  259. if _, ok := requested[hash]; ok {
  260. t.Errorf("hash %x already requested once", hash)
  261. }
  262. requested[hash] = struct{}{}
  263. results[i] = SyncResult{hash, data}
  264. }
  265. if _, index, err := sched.Process(results); err != nil {
  266. t.Fatalf("failed to process result #%d: %v", index, err)
  267. }
  268. if index, err := sched.Commit(diskdb); err != nil {
  269. t.Fatalf("failed to commit data #%d: %v", index, err)
  270. }
  271. queue = append(queue[:0], sched.Missing(0)...)
  272. }
  273. // Cross check that the two tries are in sync
  274. checkTrieContents(t, triedb, srcTrie.Root(), srcData)
  275. }
  276. // Tests that at any point in time during a sync, only complete sub-tries are in
  277. // the database.
  278. func TestIncompleteTrieSync(t *testing.T) {
  279. // Create a random trie to copy
  280. srcDb, srcTrie, _ := makeTestTrie()
  281. // Create a destination trie and sync with the scheduler
  282. diskdb, _ := ethdb.NewMemDatabase()
  283. triedb := NewDatabase(diskdb)
  284. sched := NewTrieSync(srcTrie.Hash(), diskdb, nil)
  285. added := []common.Hash{}
  286. queue := append([]common.Hash{}, sched.Missing(1)...)
  287. for len(queue) > 0 {
  288. // Fetch a batch of trie nodes
  289. results := make([]SyncResult, len(queue))
  290. for i, hash := range queue {
  291. data, err := srcDb.Node(hash)
  292. if err != nil {
  293. t.Fatalf("failed to retrieve node data for %x: %v", hash, err)
  294. }
  295. results[i] = SyncResult{hash, data}
  296. }
  297. // Process each of the trie nodes
  298. if _, index, err := sched.Process(results); err != nil {
  299. t.Fatalf("failed to process result #%d: %v", index, err)
  300. }
  301. if index, err := sched.Commit(diskdb); err != nil {
  302. t.Fatalf("failed to commit data #%d: %v", index, err)
  303. }
  304. for _, result := range results {
  305. added = append(added, result.Hash)
  306. }
  307. // Check that all known sub-tries in the synced trie are complete
  308. for _, root := range added {
  309. if err := checkTrieConsistency(triedb, root); err != nil {
  310. t.Fatalf("trie inconsistent: %v", err)
  311. }
  312. }
  313. // Fetch the next batch to retrieve
  314. queue = append(queue[:0], sched.Missing(1)...)
  315. }
  316. // Sanity check that removing any node from the database is detected
  317. for _, node := range added[1:] {
  318. key := node.Bytes()
  319. value, _ := diskdb.Get(key)
  320. diskdb.Delete(key)
  321. if err := checkTrieConsistency(triedb, added[0]); err == nil {
  322. t.Fatalf("trie inconsistency not caught, missing: %x", key)
  323. }
  324. diskdb.Put(key, value)
  325. }
  326. }