sync_test.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // Copyright 2018 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/core"
  23. "github.com/ethereum/go-ethereum/crypto"
  24. "github.com/ethereum/go-ethereum/light"
  25. "github.com/ethereum/go-ethereum/params"
  26. )
  27. // Test light syncing which will download all headers from genesis.
  28. func TestLightSyncingLes2(t *testing.T) { testCheckpointSyncing(t, 2, 0) }
  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 TestLegacyCheckpointSyncingLes2(t *testing.T) { testCheckpointSyncing(t, 2, 1) }
  33. func TestLegacyCheckpointSyncingLes3(t *testing.T) { testCheckpointSyncing(t, 3, 1) }
  34. // Test checkpoint syncing which will download tail headers based
  35. // on a verified checkpoint.
  36. func TestCheckpointSyncingLes2(t *testing.T) { testCheckpointSyncing(t, 2, 2) }
  37. func TestCheckpointSyncingLes3(t *testing.T) { testCheckpointSyncing(t, 3, 2) }
  38. func testCheckpointSyncing(t *testing.T, protocol int, syncMode int) {
  39. config := light.TestServerIndexerConfig
  40. waitIndexers := func(cIndexer, bIndexer, btIndexer *core.ChainIndexer) {
  41. for {
  42. cs, _, _ := cIndexer.Sections()
  43. bts, _, _ := btIndexer.Sections()
  44. if cs >= 1 && bts >= 1 {
  45. break
  46. }
  47. time.Sleep(10 * time.Millisecond)
  48. }
  49. }
  50. // Generate 512+4 blocks (totally 1 CHT sections)
  51. server, client, tearDown := newClientServerEnv(t, int(config.ChtSize+config.ChtConfirms), protocol, waitIndexers, false)
  52. defer tearDown()
  53. expected := config.ChtSize + config.ChtConfirms
  54. // Checkpoint syncing or legacy checkpoint syncing.
  55. if syncMode == 1 || syncMode == 2 {
  56. // Assemble checkpoint 0
  57. s, _, head := server.chtIndexer.Sections()
  58. cp := &params.TrustedCheckpoint{
  59. SectionIndex: 0,
  60. SectionHead: head,
  61. CHTRoot: light.GetChtRoot(server.db, s-1, head),
  62. BloomRoot: light.GetBloomTrieRoot(server.db, s-1, head),
  63. }
  64. if syncMode == 1 {
  65. // Register the assembled checkpoint as hardcoded one.
  66. client.pm.checkpoint = cp
  67. client.pm.blockchain.(*light.LightChain).AddTrustedCheckpoint(cp)
  68. } else {
  69. // Register the assembled checkpoint into oracle.
  70. header := server.backend.Blockchain().CurrentHeader()
  71. data := append([]byte{0x19, 0x00}, append(registrarAddr.Bytes(), append([]byte{0, 0, 0, 0, 0, 0, 0, 0}, cp.Hash().Bytes()...)...)...)
  72. sig, _ := crypto.Sign(crypto.Keccak256(data), signerKey)
  73. sig[64] += 27 // Transform V from 0/1 to 27/28 according to the yellow paper
  74. if _, err := server.pm.reg.contract.RegisterCheckpoint(signerKey, cp.SectionIndex, cp.Hash().Bytes(), new(big.Int).Sub(header.Number, big.NewInt(1)), header.ParentHash, [][]byte{sig}); err != nil {
  75. t.Error("register checkpoint failed", err)
  76. }
  77. server.backend.Commit()
  78. // Wait for the checkpoint registration
  79. for {
  80. _, hash, _, err := server.pm.reg.contract.Contract().GetLatestCheckpoint(nil)
  81. if err != nil || hash == [32]byte{} {
  82. time.Sleep(100 * time.Millisecond)
  83. continue
  84. }
  85. break
  86. }
  87. expected += 1
  88. }
  89. }
  90. done := make(chan error)
  91. client.pm.reg.syncDoneHook = func() {
  92. header := client.pm.blockchain.CurrentHeader()
  93. if header.Number.Uint64() == expected {
  94. done <- nil
  95. } else {
  96. done <- fmt.Errorf("blockchain length mismatch, want %d, got %d", expected, header.Number)
  97. }
  98. }
  99. // Create connected peer pair.
  100. peer, err1, lPeer, err2 := newTestPeerPair("peer", protocol, server.pm, client.pm)
  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. server.rPeer, client.rPeer = peer, lPeer
  109. select {
  110. case err := <-done:
  111. if err != nil {
  112. t.Error("sync failed", err)
  113. }
  114. return
  115. case <-time.NewTimer(10 * time.Second).C:
  116. t.Error("checkpoint syncing timeout")
  117. }
  118. }