sync_test.go 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. // Copyright 2019 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 les
  17. import (
  18. "fmt"
  19. "math/big"
  20. "testing"
  21. "time"
  22. "github.com/ethereum/go-ethereum/accounts/abi/bind"
  23. "github.com/ethereum/go-ethereum/core"
  24. "github.com/ethereum/go-ethereum/crypto"
  25. "github.com/ethereum/go-ethereum/light"
  26. "github.com/ethereum/go-ethereum/params"
  27. )
  28. // Test light syncing which will download all headers from genesis.
  29. func TestLightSyncingLes3(t *testing.T) { testCheckpointSyncing(t, 3, 0) }
  30. // Test legacy checkpoint syncing which will download tail headers
  31. // based on a hardcoded checkpoint.
  32. func TestLegacyCheckpointSyncingLes3(t *testing.T) { testCheckpointSyncing(t, 3, 1) }
  33. // Test checkpoint syncing which will download tail headers based
  34. // on a verified checkpoint.
  35. func TestCheckpointSyncingLes3(t *testing.T) { testCheckpointSyncing(t, 3, 2) }
  36. func testCheckpointSyncing(t *testing.T, protocol int, syncMode int) {
  37. config := light.TestServerIndexerConfig
  38. waitIndexers := func(cIndexer, bIndexer, btIndexer *core.ChainIndexer) {
  39. for {
  40. cs, _, _ := cIndexer.Sections()
  41. bts, _, _ := btIndexer.Sections()
  42. if cs >= 1 && bts >= 1 {
  43. break
  44. }
  45. time.Sleep(10 * time.Millisecond)
  46. }
  47. }
  48. // Generate 512+4 blocks (totally 1 CHT sections)
  49. server, client, tearDown := newClientServerEnv(t, int(config.ChtSize+config.ChtConfirms), protocol, waitIndexers, nil, 0, false, false)
  50. defer tearDown()
  51. expected := config.ChtSize + config.ChtConfirms
  52. // Checkpoint syncing or legacy checkpoint syncing.
  53. if syncMode == 1 || syncMode == 2 {
  54. // Assemble checkpoint 0
  55. s, _, head := server.chtIndexer.Sections()
  56. cp := &params.TrustedCheckpoint{
  57. SectionIndex: 0,
  58. SectionHead: head,
  59. CHTRoot: light.GetChtRoot(server.db, s-1, head),
  60. BloomRoot: light.GetBloomTrieRoot(server.db, s-1, head),
  61. }
  62. if syncMode == 1 {
  63. // Register the assembled checkpoint as hardcoded one.
  64. client.handler.checkpoint = cp
  65. client.handler.backend.blockchain.AddTrustedCheckpoint(cp)
  66. } else {
  67. // Register the assembled checkpoint into oracle.
  68. header := server.backend.Blockchain().CurrentHeader()
  69. data := append([]byte{0x19, 0x00}, append(registrarAddr.Bytes(), append([]byte{0, 0, 0, 0, 0, 0, 0, 0}, cp.Hash().Bytes()...)...)...)
  70. sig, _ := crypto.Sign(crypto.Keccak256(data), signerKey)
  71. sig[64] += 27 // Transform V from 0/1 to 27/28 according to the yellow paper
  72. if _, err := server.handler.server.oracle.Contract().RegisterCheckpoint(bind.NewKeyedTransactor(signerKey), cp.SectionIndex, cp.Hash().Bytes(), new(big.Int).Sub(header.Number, big.NewInt(1)), header.ParentHash, [][]byte{sig}); err != nil {
  73. t.Error("register checkpoint failed", err)
  74. }
  75. server.backend.Commit()
  76. // Wait for the checkpoint registration
  77. for {
  78. _, hash, _, err := server.handler.server.oracle.Contract().Contract().GetLatestCheckpoint(nil)
  79. if err != nil || hash == [32]byte{} {
  80. time.Sleep(10 * time.Millisecond)
  81. continue
  82. }
  83. break
  84. }
  85. expected += 1
  86. }
  87. }
  88. done := make(chan error)
  89. client.handler.syncDone = func() {
  90. header := client.handler.backend.blockchain.CurrentHeader()
  91. if header.Number.Uint64() == expected {
  92. done <- nil
  93. } else {
  94. done <- fmt.Errorf("blockchain length mismatch, want %d, got %d", expected, header.Number)
  95. }
  96. }
  97. // Create connected peer pair.
  98. peer1, err1, peer2, err2 := newTestPeerPair("peer", protocol, server.handler, client.handler)
  99. defer peer1.close()
  100. defer peer2.close()
  101. select {
  102. case <-time.After(time.Millisecond * 100):
  103. case err := <-err1:
  104. t.Fatalf("peer 1 handshake error: %v", err)
  105. case err := <-err2:
  106. t.Fatalf("peer 2 handshake error: %v", err)
  107. }
  108. select {
  109. case err := <-done:
  110. if err != nil {
  111. t.Error("sync failed", err)
  112. }
  113. return
  114. case <-time.NewTimer(10 * time.Second).C:
  115. t.Error("checkpoint syncing timeout")
  116. }
  117. }
  118. func TestMissOracleBackend(t *testing.T) { testMissOracleBackend(t, true) }
  119. func TestMissOracleBackendNoCheckpoint(t *testing.T) { testMissOracleBackend(t, false) }
  120. func testMissOracleBackend(t *testing.T, hasCheckpoint bool) {
  121. config := light.TestServerIndexerConfig
  122. waitIndexers := func(cIndexer, bIndexer, btIndexer *core.ChainIndexer) {
  123. for {
  124. cs, _, _ := cIndexer.Sections()
  125. bts, _, _ := btIndexer.Sections()
  126. if cs >= 1 && bts >= 1 {
  127. break
  128. }
  129. time.Sleep(10 * time.Millisecond)
  130. }
  131. }
  132. // Generate 512+4 blocks (totally 1 CHT sections)
  133. server, client, tearDown := newClientServerEnv(t, int(config.ChtSize+config.ChtConfirms), 3, waitIndexers, nil, 0, false, false)
  134. defer tearDown()
  135. expected := config.ChtSize + config.ChtConfirms
  136. s, _, head := server.chtIndexer.Sections()
  137. cp := &params.TrustedCheckpoint{
  138. SectionIndex: 0,
  139. SectionHead: head,
  140. CHTRoot: light.GetChtRoot(server.db, s-1, head),
  141. BloomRoot: light.GetBloomTrieRoot(server.db, s-1, head),
  142. }
  143. // Register the assembled checkpoint into oracle.
  144. header := server.backend.Blockchain().CurrentHeader()
  145. data := append([]byte{0x19, 0x00}, append(registrarAddr.Bytes(), append([]byte{0, 0, 0, 0, 0, 0, 0, 0}, cp.Hash().Bytes()...)...)...)
  146. sig, _ := crypto.Sign(crypto.Keccak256(data), signerKey)
  147. sig[64] += 27 // Transform V from 0/1 to 27/28 according to the yellow paper
  148. if _, err := server.handler.server.oracle.Contract().RegisterCheckpoint(bind.NewKeyedTransactor(signerKey), cp.SectionIndex, cp.Hash().Bytes(), new(big.Int).Sub(header.Number, big.NewInt(1)), header.ParentHash, [][]byte{sig}); err != nil {
  149. t.Error("register checkpoint failed", err)
  150. }
  151. server.backend.Commit()
  152. // Wait for the checkpoint registration
  153. for {
  154. _, hash, _, err := server.handler.server.oracle.Contract().Contract().GetLatestCheckpoint(nil)
  155. if err != nil || hash == [32]byte{} {
  156. time.Sleep(100 * time.Millisecond)
  157. continue
  158. }
  159. break
  160. }
  161. expected += 1
  162. // Explicitly set the oracle as nil. In normal use case it can happen
  163. // that user wants to unlock something which blocks the oracle backend
  164. // initialisation. But at the same time syncing starts.
  165. //
  166. // See https://github.com/ethereum/go-ethereum/issues/20097 for more detail.
  167. //
  168. // In this case, client should run light sync or legacy checkpoint sync
  169. // if hardcoded checkpoint is configured.
  170. client.handler.backend.oracle = nil
  171. // For some private networks it can happen checkpoint syncing is enabled
  172. // but there is no hardcoded checkpoint configured.
  173. if hasCheckpoint {
  174. client.handler.checkpoint = cp
  175. client.handler.backend.blockchain.AddTrustedCheckpoint(cp)
  176. }
  177. done := make(chan error)
  178. client.handler.syncDone = func() {
  179. header := client.handler.backend.blockchain.CurrentHeader()
  180. if header.Number.Uint64() == expected {
  181. done <- nil
  182. } else {
  183. done <- fmt.Errorf("blockchain length mismatch, want %d, got %d", expected, header.Number)
  184. }
  185. }
  186. // Create connected peer pair.
  187. _, err1, _, err2 := newTestPeerPair("peer", 2, server.handler, client.handler)
  188. select {
  189. case <-time.After(time.Millisecond * 100):
  190. case err := <-err1:
  191. t.Fatalf("peer 1 handshake error: %v", err)
  192. case err := <-err2:
  193. t.Fatalf("peer 2 handshake error: %v", err)
  194. }
  195. select {
  196. case err := <-done:
  197. if err != nil {
  198. t.Error("sync failed", err)
  199. }
  200. return
  201. case <-time.NewTimer(10 * time.Second).C:
  202. t.Error("checkpoint syncing timeout")
  203. }
  204. }