sync_test.go 4.7 KB

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