@@ -634,3 +634,65 @@ outer:
634634 t .Errorf ("block broadcast to %d peers, expected %d" , receivedCount , broadcastExpected )
635635 }
636636}
637+
638+ // Tests that a propagated malformed block (uncles or transactions don't match
639+ // with the hashes in the header) gets discarded and not broadcast forward.
640+ func TestBroadcastMalformedBlock (t * testing.T ) {
641+ // Create a live node to test propagation with
642+ var (
643+ engine = ethash .NewFaker ()
644+ db = rawdb .NewMemoryDatabase ()
645+ config = & params.ChainConfig {}
646+ gspec = & core.Genesis {Config : config }
647+ genesis = gspec .MustCommit (db )
648+ )
649+ blockchain , err := core .NewBlockChain (db , nil , config , engine , vm.Config {}, nil )
650+ if err != nil {
651+ t .Fatalf ("failed to create new blockchain: %v" , err )
652+ }
653+ pm , err := NewProtocolManager (config , nil , downloader .FullSync , DefaultConfig .NetworkId , new (event.TypeMux ), new (testTxPool ), engine , blockchain , db , 1 , nil )
654+ if err != nil {
655+ t .Fatalf ("failed to start test protocol manager: %v" , err )
656+ }
657+ pm .Start (2 )
658+ defer pm .Stop ()
659+
660+ // Create two peers, one to send the malformed block with and one to check
661+ // propagation
662+ source , _ := newTestPeer ("source" , eth63 , pm , true )
663+ defer source .close ()
664+
665+ sink , _ := newTestPeer ("sink" , eth63 , pm , true )
666+ defer sink .close ()
667+
668+ // Create various combinations of malformed blocks
669+ chain , _ := core .GenerateChain (gspec .Config , genesis , ethash .NewFaker (), db , 1 , func (i int , gen * core.BlockGen ) {})
670+
671+ malformedUncles := chain [0 ].Header ()
672+ malformedUncles .UncleHash [0 ]++
673+ malformedTransactions := chain [0 ].Header ()
674+ malformedTransactions .TxHash [0 ]++
675+ malformedEverything := chain [0 ].Header ()
676+ malformedEverything .UncleHash [0 ]++
677+ malformedEverything .TxHash [0 ]++
678+
679+ // Keep listening to broadcasts and notify if any arrives
680+ notify := make (chan struct {})
681+ go func () {
682+ if _ , err := sink .app .ReadMsg (); err == nil {
683+ notify <- struct {}{}
684+ }
685+ }()
686+ // Try to broadcast all malformations and ensure they all get discarded
687+ for _ , header := range []* types.Header {malformedUncles , malformedTransactions , malformedEverything } {
688+ block := types .NewBlockWithHeader (header ).WithBody (chain [0 ].Transactions (), chain [0 ].Uncles ())
689+ if err := p2p .Send (source .app , NewBlockMsg , []interface {}{block , big .NewInt (131136 )}); err != nil {
690+ t .Fatalf ("failed to broadcast block: %v" , err )
691+ }
692+ select {
693+ case <- notify :
694+ t .Fatalf ("malformed block forwarded" )
695+ case <- time .After (100 * time .Millisecond ):
696+ }
697+ }
698+ }
0 commit comments