@@ -679,6 +679,110 @@ func TestAnnouncedBlockFetchedThroughSink(t *testing.T) {
679679 }
680680}
681681
682+ // TestFilterHeadersIgnoresResponseDuration checks that the header filter uses
683+ // the receive time, not the round trip added to it: a large response duration
684+ // must not stamp the imported block with a time in the future.
685+ func TestFilterHeadersIgnoresResponseDuration (t * testing.T ) {
686+ hashes , blocks := makeChain (2 , 0 , genesis )
687+
688+ // Start from a tester whose head is block 1, so the empty block 2 can be
689+ // imported as soon as its header arrives. makeChain returns its hashes in
690+ // reverse order: hashes[0] is the newest block.
691+ tester := newIdleTester ()
692+ tester .hashes = append (tester .hashes , hashes [1 ])
693+ tester .blocks [hashes [1 ]] = blocks [hashes [1 ]]
694+ tester .fetcher .Start ()
695+ t .Cleanup (tester .fetcher .Stop )
696+
697+ headerFetcher := func (hash common.Hash , sink chan * eth.Response ) (* eth.Request , error ) {
698+ headers := eth.BlockHeadersPacket {blocks [hash ].Header ()}
699+ go func () {
700+ sink <- & eth.Response {Res : & headers , Time : 30 * time .Second , Done : make (chan error , 1 )}
701+ }()
702+ return & eth.Request {}, nil
703+ }
704+ bodyFetcher := func (hashes []common.Hash , sink chan * eth.Response ) (* eth.Request , error ) {
705+ return & eth.Request {}, nil
706+ }
707+ imported := make (chan * types.Block )
708+ tester .watchImports (imported )
709+ if err := tester .fetcher .Notify ("valid" , hashes [0 ], 2 , time .Now ().Add (- arriveTimeout ), headerFetcher , bodyFetcher ); err != nil {
710+ t .Fatalf ("failed to notify: %v" , err )
711+ }
712+ select {
713+ case block := <- imported :
714+ if block .ReceivedAt .After (time .Now ().Add (time .Second )) {
715+ t .Fatalf ("block receive time %v is in the future" , block .ReceivedAt )
716+ }
717+ case <- time .After (2 * time .Second ):
718+ t .Fatal ("block was not imported" )
719+ }
720+ }
721+
722+ // TestWaitResponseDropsSilentPeer checks that a peer which advertises a block
723+ // and then does not serve the requested header is dropped instead of leaving
724+ // the sink waiter (and its dispatcher entry) blocked forever.
725+ func TestWaitResponseDropsSilentPeer (t * testing.T ) {
726+ old := fetchResponseTimeout
727+ fetchResponseTimeout = 100 * time .Millisecond
728+ t .Cleanup (func () { fetchResponseTimeout = old })
729+
730+ tester := newIdleTester ()
731+
732+ type verdict struct {
733+ res * eth.Response
734+ ok bool
735+ }
736+ verdictCh := make (chan verdict , 1 )
737+ go func () {
738+ res , ok := tester .fetcher .waitResponse ("silent" , & eth.Request {}, make (chan * eth.Response ))
739+ verdictCh <- verdict {res , ok }
740+ }()
741+ var got verdict
742+ select {
743+ case got = <- verdictCh :
744+ case <- time .After (2 * time .Second ):
745+ t .Fatal ("waiter did not return after the response timeout" )
746+ }
747+ if got .ok || got .res != nil {
748+ t .Fatalf ("silent peer: have (%v, %v), want (nil, false)" , got .res , got .ok )
749+ }
750+ tester .lock .RLock ()
751+ dropped := tester .drops ["silent" ]
752+ tester .lock .RUnlock ()
753+ if ! dropped {
754+ t .Fatal ("silent peer was not dropped" )
755+ }
756+ }
757+
758+ // TestWaitResponseStopsOnQuit checks that shutting the fetcher down releases a
759+ // sink waiter through quit, not through the timeout path that drops the peer.
760+ func TestWaitResponseStopsOnQuit (t * testing.T ) {
761+ old := fetchResponseTimeout
762+ fetchResponseTimeout = 5 * time .Second
763+ t .Cleanup (func () { fetchResponseTimeout = old })
764+
765+ tester := newTester ()
766+ tester .fetcher .Stop ()
767+
768+ done := make (chan struct {})
769+ go func () {
770+ tester .fetcher .waitResponse ("peer" , & eth.Request {}, make (chan * eth.Response ))
771+ close (done )
772+ }()
773+ select {
774+ case <- done :
775+ case <- time .After (time .Second ):
776+ t .Fatal ("Stop did not release the sink waiter" )
777+ }
778+ tester .lock .RLock ()
779+ dropped := tester .drops ["peer" ]
780+ tester .lock .RUnlock ()
781+ if dropped {
782+ t .Fatal ("Stop released the waiter through the timeout path" )
783+ }
784+ }
785+
682786// Tests that if blocks are announced by multiple peers (or even the same buggy
683787// peer), they will only get downloaded at most once.
684788func TestConcurrentAnnouncements62 (t * testing.T ) { testConcurrentAnnouncements (t , 62 ) }
0 commit comments