@ -144,8 +144,8 @@ func testForkIDSplit(t *testing.T, protocol uint) {
defer p2pNoFork . Close ( )
defer p2pProFork . Close ( )
peerNoFork := eth . NewPeer ( protocol , p2p . NewPeer ( enode . ID { 1 } , "" , nil ) , p2pNoFork , nil )
peerProFork := eth . NewPeer ( protocol , p2p . NewPeer ( enode . ID { 2 } , "" , nil ) , p2pProFork , nil )
peerNoFork := eth . NewPeer ( protocol , p2p . NewPeerPipe ( enode . ID { 1 } , "" , nil , p2pNoFork ) , p2pNoFork , nil )
peerProFork := eth . NewPeer ( protocol , p2p . NewPeerPipe ( enode . ID { 2 } , "" , nil , p2pProFork ) , p2pProFork , nil )
defer peerNoFork . Close ( )
defer peerProFork . Close ( )
@ -206,8 +206,8 @@ func testForkIDSplit(t *testing.T, protocol uint) {
defer p2pNoFork . Close ( )
defer p2pProFork . Close ( )
peerNoFork = eth . NewPeer ( protocol , p2p . NewPeer ( enode . ID { 1 } , "" , nil ) , p2pNoFork , nil )
peerProFork = eth . NewPeer ( protocol , p2p . NewPeer ( enode . ID { 2 } , "" , nil ) , p2pProFork , nil )
peerNoFork = eth . NewPeer ( protocol , p2p . NewPeerPipe ( enode . ID { 1 } , "" , nil , p2pNoFork ) , p2pNoFork , nil )
peerProFork = eth . NewPeer ( protocol , p2p . NewPeerPipe ( enode . ID { 2 } , "" , nil , p2pProFork ) , p2pProFork , nil )
defer peerNoFork . Close ( )
defer peerProFork . Close ( )
@ -257,8 +257,8 @@ func testRecvTransactions(t *testing.T, protocol uint) {
defer p2pSrc . Close ( )
defer p2pSink . Close ( )
src := eth . NewPeer ( protocol , p2p . NewPeer ( enode . ID { 1 } , "" , nil ) , p2pSrc , handler . txpool )
sink := eth . NewPeer ( protocol , p2p . NewPeer ( enode . ID { 2 } , "" , nil ) , p2pSink , handler . txpool )
src := eth . NewPeer ( protocol , p2p . NewPeerPipe ( enode . ID { 1 } , "" , nil , p2pSrc ) , p2pSrc , handler . txpool )
sink := eth . NewPeer ( protocol , p2p . NewPeerPipe ( enode . ID { 2 } , "" , nil , p2pSink ) , p2pSink , handler . txpool )
defer src . Close ( )
defer sink . Close ( )
@ -319,8 +319,8 @@ func testSendTransactions(t *testing.T, protocol uint) {
defer p2pSrc . Close ( )
defer p2pSink . Close ( )
src := eth . NewPeer ( protocol , p2p . NewPeer ( enode . ID { 1 } , "" , nil ) , p2pSrc , handler . txpool )
sink := eth . NewPeer ( protocol , p2p . NewPeer ( enode . ID { 2 } , "" , nil ) , p2pSink , handler . txpool )
src := eth . NewPeer ( protocol , p2p . NewPeerPipe ( enode . ID { 1 } , "" , nil , p2pSrc ) , p2pSrc , handler . txpool )
sink := eth . NewPeer ( protocol , p2p . NewPeerPipe ( enode . ID { 2 } , "" , nil , p2pSink ) , p2pSink , handler . txpool )
defer src . Close ( )
defer sink . Close ( )
@ -407,8 +407,8 @@ func testTransactionPropagation(t *testing.T, protocol uint) {
defer sourcePipe . Close ( )
defer sinkPipe . Close ( )
sourcePeer := eth . NewPeer ( protocol , p2p . NewPeer ( enode . ID { byte ( i ) } , "" , nil ) , sourcePipe , source . txpool )
sinkPeer := eth . NewPeer ( protocol , p2p . NewPeer ( enode . ID { 0 } , "" , nil ) , sinkPipe , sink . txpool )
sourcePeer := eth . NewPeer ( protocol , p2p . NewPeerPipe ( enode . ID { byte ( i ) } , "" , nil , sourcePipe ) , sourcePipe , source . txpool )
sinkPeer := eth . NewPeer ( protocol , p2p . NewPeerPipe ( enode . ID { 0 } , "" , nil , sinkPipe ) , sinkPipe , sink . txpool )
defer sourcePeer . Close ( )
defer sinkPeer . Close ( )
@ -490,6 +490,8 @@ func TestCheckpointChallenge(t *testing.T) {
}
func testCheckpointChallenge ( t * testing . T , syncmode downloader . SyncMode , checkpoint bool , timeout bool , empty bool , match bool , drop bool ) {
t . Parallel ( )
// Reduce the checkpoint handshake challenge timeout
defer func ( old time . Duration ) { syncChallengeTimeout = old } ( syncChallengeTimeout )
syncChallengeTimeout = 250 * time . Millisecond
@ -513,20 +515,26 @@ func testCheckpointChallenge(t *testing.T, syncmode downloader.SyncMode, checkpo
handler . handler . checkpointNumber = number
handler . handler . checkpointHash = response . Hash ( )
}
// Create a challenger peer and a challenged one
// Create a challenger peer and a challenged one.
p2pLocal , p2pRemote := p2p . MsgPipe ( )
defer p2pLocal . Close ( )
defer p2pRemote . Close ( )
local := eth . NewPeer ( eth . ETH65 , p2p . NewPeer ( enode . ID { 1 } , "" , nil ) , p2pLocal , handler . txpool )
remote := eth . NewPeer ( eth . ETH65 , p2p . NewPeer ( enode . ID { 2 } , "" , nil ) , p2pRemote , handler . txpool )
local := eth . NewPeer ( eth . ETH65 , p2p . NewPeerPipe ( enode . ID { 1 } , "" , nil , p2pLoca l ) , p2pLocal , handler . txpool )
remote := eth . NewPeer ( eth . ETH65 , p2p . NewPeerPipe ( enode . ID { 2 } , "" , nil , p2pRemote ) , p2pRemote , handler . txpool )
defer local . Close ( )
defer remote . Close ( )
go handler . handler . runEthPeer ( local , func ( peer * eth . Peer ) error {
handlerDone := make ( chan struct { } )
go func ( ) {
defer close ( handlerDone )
handler . handler . runEthPeer ( local , func ( peer * eth . Peer ) error {
return eth . Handle ( ( * ethHandler ) ( handler . handler ) , peer )
} )
// Run the handshake locally to avoid spinning up a remote handler
} ( )
// Run the handshake locally to avoid spinning up a remote handler.
var (
genesis = handler . chain . Genesis ( )
head = handler . chain . CurrentBlock ( )
@ -535,12 +543,13 @@ func testCheckpointChallenge(t *testing.T, syncmode downloader.SyncMode, checkpo
if err := remote . Handshake ( 1 , td , head . Hash ( ) , genesis . Hash ( ) , forkid . NewIDWithChain ( handler . chain ) , forkid . NewFilter ( handler . chain ) ) ; err != nil {
t . Fatalf ( "failed to run protocol handshake" )
}
// Connect a new peer and check that we receive the checkpoint challenge
// Connect a new peer and check that we receive the checkpoint challenge.
if checkpoint {
if err := remote . ExpectRequestHeadersByNumber ( response . Number . Uint64 ( ) , 1 , 0 , false ) ; err != nil {
t . Fatalf ( "challenge mismatch: %v" , err )
}
// Create a block to reply to the challenge if no timeout is simulated
// Create a block to reply to the challenge if no timeout is simulated.
if ! timeout {
if empty {
if err := remote . SendBlockHeaders ( [ ] * types . Header { } ) ; err != nil {
@ -557,11 +566,13 @@ func testCheckpointChallenge(t *testing.T, syncmode downloader.SyncMode, checkpo
}
}
}
// Wait until the test timeout passes to ensure proper cleanup
time . Sleep ( syncChallengeTimeout + 300 * time . Millisecond )
// Verify that the remote peer is maintained or dropped
// Verify that the remote peer is maintained or dropped.
if drop {
<- handlerDone
if peers := handler . handler . peers . len ( ) ; peers != 0 {
t . Fatalf ( "peer count mismatch: have %d, want %d" , peers , 0 )
}
@ -608,8 +619,8 @@ func testBroadcastBlock(t *testing.T, peers, bcasts int) {
defer sourcePipe . Close ( )
defer sinkPipe . Close ( )
sourcePeer := eth . NewPeer ( eth . ETH65 , p2p . NewPeer ( enode . ID { byte ( i ) } , "" , nil ) , sourcePipe , nil )
sinkPeer := eth . NewPeer ( eth . ETH65 , p2p . NewPeer ( enode . ID { 0 } , "" , nil ) , sinkPipe , nil )
sourcePeer := eth . NewPeer ( eth . ETH65 , p2p . NewPeerPipe ( enode . ID { byte ( i ) } , "" , nil , sourcePipe ) , sourcePipe , nil )
sinkPeer := eth . NewPeer ( eth . ETH65 , p2p . NewPeerPipe ( enode . ID { 0 } , "" , nil , sinkPipe ) , sinkPipe , nil )
defer sourcePeer . Close ( )
defer sinkPeer . Close ( )
@ -676,8 +687,8 @@ func testBroadcastMalformedBlock(t *testing.T, protocol uint) {
defer p2pSrc . Close ( )
defer p2pSink . Close ( )
src := eth . NewPeer ( protocol , p2p . NewPeer ( enode . ID { 1 } , "" , nil ) , p2pSrc , source . txpool )
sink := eth . NewPeer ( protocol , p2p . NewPeer ( enode . ID { 2 } , "" , nil ) , p2pSink , source . txpool )
src := eth . NewPeer ( protocol , p2p . NewPeerPipe ( enode . ID { 1 } , "" , nil , p2pSrc ) , p2pSrc , source . txpool )
sink := eth . NewPeer ( protocol , p2p . NewPeerPipe ( enode . ID { 2 } , "" , nil , p2pSink ) , p2pSink , source . txpool )
defer src . Close ( )
defer sink . Close ( )