sync_test.go 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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, true)
  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, peer2, err := newTestPeerPair("peer", protocol, server.handler, client.handler)
  99. if err != nil {
  100. t.Fatalf("Failed to connect testing peers %v", err)
  101. }
  102. defer peer1.close()
  103. defer peer2.close()
  104. select {
  105. case err := <-done:
  106. if err != nil {
  107. t.Error("sync failed", err)
  108. }
  109. return
  110. case <-time.NewTimer(10 * time.Second).C:
  111. t.Error("checkpoint syncing timeout")
  112. }
  113. }
  114. func TestMissOracleBackend(t *testing.T) { testMissOracleBackend(t, true) }
  115. func TestMissOracleBackendNoCheckpoint(t *testing.T) { testMissOracleBackend(t, false) }
  116. func testMissOracleBackend(t *testing.T, hasCheckpoint bool) {
  117. config := light.TestServerIndexerConfig
  118. waitIndexers := func(cIndexer, bIndexer, btIndexer *core.ChainIndexer) {
  119. for {
  120. cs, _, _ := cIndexer.Sections()
  121. bts, _, _ := btIndexer.Sections()
  122. if cs >= 1 && bts >= 1 {
  123. break
  124. }
  125. time.Sleep(10 * time.Millisecond)
  126. }
  127. }
  128. // Generate 512+4 blocks (totally 1 CHT sections)
  129. server, client, tearDown := newClientServerEnv(t, int(config.ChtSize+config.ChtConfirms), 3, waitIndexers, nil, 0, false, false, true)
  130. defer tearDown()
  131. expected := config.ChtSize + config.ChtConfirms
  132. s, _, head := server.chtIndexer.Sections()
  133. cp := &params.TrustedCheckpoint{
  134. SectionIndex: 0,
  135. SectionHead: head,
  136. CHTRoot: light.GetChtRoot(server.db, s-1, head),
  137. BloomRoot: light.GetBloomTrieRoot(server.db, s-1, head),
  138. }
  139. // Register the assembled checkpoint into oracle.
  140. header := server.backend.Blockchain().CurrentHeader()
  141. data := append([]byte{0x19, 0x00}, append(registrarAddr.Bytes(), append([]byte{0, 0, 0, 0, 0, 0, 0, 0}, cp.Hash().Bytes()...)...)...)
  142. sig, _ := crypto.Sign(crypto.Keccak256(data), signerKey)
  143. sig[64] += 27 // Transform V from 0/1 to 27/28 according to the yellow paper
  144. 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 {
  145. t.Error("register checkpoint failed", err)
  146. }
  147. server.backend.Commit()
  148. // Wait for the checkpoint registration
  149. for {
  150. _, hash, _, err := server.handler.server.oracle.Contract().Contract().GetLatestCheckpoint(nil)
  151. if err != nil || hash == [32]byte{} {
  152. time.Sleep(100 * time.Millisecond)
  153. continue
  154. }
  155. break
  156. }
  157. expected += 1
  158. // Explicitly set the oracle as nil. In normal use case it can happen
  159. // that user wants to unlock something which blocks the oracle backend
  160. // initialisation. But at the same time syncing starts.
  161. //
  162. // See https://github.com/ethereum/go-ethereum/issues/20097 for more detail.
  163. //
  164. // In this case, client should run light sync or legacy checkpoint sync
  165. // if hardcoded checkpoint is configured.
  166. client.handler.backend.oracle = nil
  167. // For some private networks it can happen checkpoint syncing is enabled
  168. // but there is no hardcoded checkpoint configured.
  169. if hasCheckpoint {
  170. client.handler.checkpoint = cp
  171. client.handler.backend.blockchain.AddTrustedCheckpoint(cp)
  172. }
  173. done := make(chan error)
  174. client.handler.syncDone = func() {
  175. header := client.handler.backend.blockchain.CurrentHeader()
  176. if header.Number.Uint64() == expected {
  177. done <- nil
  178. } else {
  179. done <- fmt.Errorf("blockchain length mismatch, want %d, got %d", expected, header.Number)
  180. }
  181. }
  182. // Create connected peer pair.
  183. if _, _, err := newTestPeerPair("peer", 2, server.handler, client.handler); err != nil {
  184. t.Fatalf("Failed to connect testing peers %v", err)
  185. }
  186. select {
  187. case err := <-done:
  188. if err != nil {
  189. t.Error("sync failed", err)
  190. }
  191. return
  192. case <-time.NewTimer(10 * time.Second).C:
  193. t.Error("checkpoint syncing timeout")
  194. }
  195. }