eth: request id dispatcher and direct req/reply APIs (#23576)
* eth: request ID based message dispatcher * eth: fix dispatcher cancellation, rework fetchers idleness tracker * eth/downloader: drop peers who refuse to serve advertised chains
This commit is contained in:
@@ -23,57 +23,74 @@ import (
|
||||
|
||||
"github.com/ethereum/go-ethereum/eth/downloader"
|
||||
"github.com/ethereum/go-ethereum/eth/protocols/eth"
|
||||
"github.com/ethereum/go-ethereum/eth/protocols/snap"
|
||||
"github.com/ethereum/go-ethereum/p2p"
|
||||
"github.com/ethereum/go-ethereum/p2p/enode"
|
||||
)
|
||||
|
||||
// Tests that fast sync is disabled after a successful sync cycle.
|
||||
func TestFastSyncDisabling66(t *testing.T) { testFastSyncDisabling(t, eth.ETH66) }
|
||||
// Tests that snap sync is disabled after a successful sync cycle.
|
||||
func TestSnapSyncDisabling66(t *testing.T) { testSnapSyncDisabling(t, eth.ETH66, snap.SNAP1) }
|
||||
|
||||
// Tests that fast sync gets disabled as soon as a real block is successfully
|
||||
// Tests that snap sync gets disabled as soon as a real block is successfully
|
||||
// imported into the blockchain.
|
||||
func testFastSyncDisabling(t *testing.T, protocol uint) {
|
||||
func testSnapSyncDisabling(t *testing.T, ethVer uint, snapVer uint) {
|
||||
t.Parallel()
|
||||
|
||||
// Create an empty handler and ensure it's in fast sync mode
|
||||
// Create an empty handler and ensure it's in snap sync mode
|
||||
empty := newTestHandler()
|
||||
if atomic.LoadUint32(&empty.handler.fastSync) == 0 {
|
||||
t.Fatalf("fast sync disabled on pristine blockchain")
|
||||
if atomic.LoadUint32(&empty.handler.snapSync) == 0 {
|
||||
t.Fatalf("snap sync disabled on pristine blockchain")
|
||||
}
|
||||
defer empty.close()
|
||||
|
||||
// Create a full handler and ensure fast sync ends up disabled
|
||||
// Create a full handler and ensure snap sync ends up disabled
|
||||
full := newTestHandlerWithBlocks(1024)
|
||||
if atomic.LoadUint32(&full.handler.fastSync) == 1 {
|
||||
t.Fatalf("fast sync not disabled on non-empty blockchain")
|
||||
if atomic.LoadUint32(&full.handler.snapSync) == 1 {
|
||||
t.Fatalf("snap sync not disabled on non-empty blockchain")
|
||||
}
|
||||
defer full.close()
|
||||
|
||||
// Sync up the two handlers
|
||||
emptyPipe, fullPipe := p2p.MsgPipe()
|
||||
defer emptyPipe.Close()
|
||||
defer fullPipe.Close()
|
||||
// Sync up the two handlers via both `eth` and `snap`
|
||||
caps := []p2p.Cap{{Name: "eth", Version: ethVer}, {Name: "snap", Version: snapVer}}
|
||||
|
||||
emptyPeer := eth.NewPeer(protocol, p2p.NewPeer(enode.ID{1}, "", nil), emptyPipe, empty.txpool)
|
||||
fullPeer := eth.NewPeer(protocol, p2p.NewPeer(enode.ID{2}, "", nil), fullPipe, full.txpool)
|
||||
defer emptyPeer.Close()
|
||||
defer fullPeer.Close()
|
||||
emptyPipeEth, fullPipeEth := p2p.MsgPipe()
|
||||
defer emptyPipeEth.Close()
|
||||
defer fullPipeEth.Close()
|
||||
|
||||
go empty.handler.runEthPeer(emptyPeer, func(peer *eth.Peer) error {
|
||||
emptyPeerEth := eth.NewPeer(ethVer, p2p.NewPeer(enode.ID{1}, "", caps), emptyPipeEth, empty.txpool)
|
||||
fullPeerEth := eth.NewPeer(ethVer, p2p.NewPeer(enode.ID{2}, "", caps), fullPipeEth, full.txpool)
|
||||
defer emptyPeerEth.Close()
|
||||
defer fullPeerEth.Close()
|
||||
|
||||
go empty.handler.runEthPeer(emptyPeerEth, func(peer *eth.Peer) error {
|
||||
return eth.Handle((*ethHandler)(empty.handler), peer)
|
||||
})
|
||||
go full.handler.runEthPeer(fullPeer, func(peer *eth.Peer) error {
|
||||
go full.handler.runEthPeer(fullPeerEth, func(peer *eth.Peer) error {
|
||||
return eth.Handle((*ethHandler)(full.handler), peer)
|
||||
})
|
||||
|
||||
emptyPipeSnap, fullPipeSnap := p2p.MsgPipe()
|
||||
defer emptyPipeSnap.Close()
|
||||
defer fullPipeSnap.Close()
|
||||
|
||||
emptyPeerSnap := snap.NewPeer(snapVer, p2p.NewPeer(enode.ID{1}, "", caps), emptyPipeSnap)
|
||||
fullPeerSnap := snap.NewPeer(snapVer, p2p.NewPeer(enode.ID{2}, "", caps), fullPipeSnap)
|
||||
|
||||
go empty.handler.runSnapExtension(emptyPeerSnap, func(peer *snap.Peer) error {
|
||||
return snap.Handle((*snapHandler)(empty.handler), peer)
|
||||
})
|
||||
go full.handler.runSnapExtension(fullPeerSnap, func(peer *snap.Peer) error {
|
||||
return snap.Handle((*snapHandler)(full.handler), peer)
|
||||
})
|
||||
// Wait a bit for the above handlers to start
|
||||
time.Sleep(250 * time.Millisecond)
|
||||
|
||||
// Check that fast sync was disabled
|
||||
op := peerToSyncOp(downloader.FastSync, empty.handler.peers.peerWithHighestTD())
|
||||
// Check that snap sync was disabled
|
||||
op := peerToSyncOp(downloader.SnapSync, empty.handler.peers.peerWithHighestTD())
|
||||
if err := empty.handler.doSync(op); err != nil {
|
||||
t.Fatal("sync failed:", err)
|
||||
}
|
||||
if atomic.LoadUint32(&empty.handler.fastSync) == 1 {
|
||||
t.Fatalf("fast sync not disabled after successful synchronisation")
|
||||
if atomic.LoadUint32(&empty.handler.snapSync) == 1 {
|
||||
t.Fatalf("snap sync not disabled after successful synchronisation")
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user