sync_test.go 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 eth
  17. import (
  18. "sync/atomic"
  19. "testing"
  20. "time"
  21. "github.com/ethereum/go-ethereum/eth/downloader"
  22. "github.com/ethereum/go-ethereum/eth/protocols/eth"
  23. "github.com/ethereum/go-ethereum/eth/protocols/snap"
  24. "github.com/ethereum/go-ethereum/p2p"
  25. "github.com/ethereum/go-ethereum/p2p/enode"
  26. )
  27. // Tests that snap sync is disabled after a successful sync cycle.
  28. func TestSnapSyncDisabling66(t *testing.T) { testSnapSyncDisabling(t, eth.ETH66, snap.SNAP1) }
  29. func TestSnapSyncDisabling67(t *testing.T) { testSnapSyncDisabling(t, eth.ETH67, snap.SNAP1) }
  30. // Tests that snap sync gets disabled as soon as a real block is successfully
  31. // imported into the blockchain.
  32. func testSnapSyncDisabling(t *testing.T, ethVer uint, snapVer uint) {
  33. t.Parallel()
  34. // Create an empty handler and ensure it's in snap sync mode
  35. empty := newTestHandler()
  36. if atomic.LoadUint32(&empty.handler.snapSync) == 0 {
  37. t.Fatalf("snap sync disabled on pristine blockchain")
  38. }
  39. defer empty.close()
  40. // Create a full handler and ensure snap sync ends up disabled
  41. full := newTestHandlerWithBlocks(1024)
  42. if atomic.LoadUint32(&full.handler.snapSync) == 1 {
  43. t.Fatalf("snap sync not disabled on non-empty blockchain")
  44. }
  45. defer full.close()
  46. // Sync up the two handlers via both `eth` and `snap`
  47. caps := []p2p.Cap{{Name: "eth", Version: ethVer}, {Name: "snap", Version: snapVer}}
  48. emptyPipeEth, fullPipeEth := p2p.MsgPipe()
  49. defer emptyPipeEth.Close()
  50. defer fullPipeEth.Close()
  51. emptyPeerEth := eth.NewPeer(ethVer, p2p.NewPeer(enode.ID{1}, "", caps), emptyPipeEth, empty.txpool)
  52. fullPeerEth := eth.NewPeer(ethVer, p2p.NewPeer(enode.ID{2}, "", caps), fullPipeEth, full.txpool)
  53. defer emptyPeerEth.Close()
  54. defer fullPeerEth.Close()
  55. go empty.handler.runEthPeer(emptyPeerEth, func(peer *eth.Peer) error {
  56. return eth.Handle((*ethHandler)(empty.handler), peer)
  57. })
  58. go full.handler.runEthPeer(fullPeerEth, func(peer *eth.Peer) error {
  59. return eth.Handle((*ethHandler)(full.handler), peer)
  60. })
  61. emptyPipeSnap, fullPipeSnap := p2p.MsgPipe()
  62. defer emptyPipeSnap.Close()
  63. defer fullPipeSnap.Close()
  64. emptyPeerSnap := snap.NewPeer(snapVer, p2p.NewPeer(enode.ID{1}, "", caps), emptyPipeSnap)
  65. fullPeerSnap := snap.NewPeer(snapVer, p2p.NewPeer(enode.ID{2}, "", caps), fullPipeSnap)
  66. go empty.handler.runSnapExtension(emptyPeerSnap, func(peer *snap.Peer) error {
  67. return snap.Handle((*snapHandler)(empty.handler), peer)
  68. })
  69. go full.handler.runSnapExtension(fullPeerSnap, func(peer *snap.Peer) error {
  70. return snap.Handle((*snapHandler)(full.handler), peer)
  71. })
  72. // Wait a bit for the above handlers to start
  73. time.Sleep(250 * time.Millisecond)
  74. // Check that snap sync was disabled
  75. op := peerToSyncOp(downloader.SnapSync, empty.handler.peers.peerWithHighestTD())
  76. if err := empty.handler.doSync(op); err != nil {
  77. t.Fatal("sync failed:", err)
  78. }
  79. if atomic.LoadUint32(&empty.handler.snapSync) == 1 {
  80. t.Fatalf("snap sync not disabled after successful synchronisation")
  81. }
  82. }