Skip to content

Commit a90cc66

Browse files
committed
eth: check propagated block malformation on receiption
1 parent 8bd37a1 commit a90cc66

2 files changed

Lines changed: 70 additions & 0 deletions

File tree

‎eth/handler.go‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -687,6 +687,14 @@ func (pm *ProtocolManager) handleMsg(p *peer) error {
687687
if err := msg.Decode(&request); err != nil {
688688
return errResp(ErrDecode, "%v: %v", msg, err)
689689
}
690+
if hash := types.CalcUncleHash(request.Block.Uncles()); hash != request.Block.UncleHash() {
691+
log.Warn("Propagated block has invalid uncles", "have", hash, "exp", request.Block.UncleHash())
692+
break // TODO(karalabe): return error eventually, but wait a few releases
693+
}
694+
if hash := types.DeriveSha(request.Block.Transactions()); hash != request.Block.TxHash() {
695+
log.Warn("Propagated block has invalid body", "have", hash, "exp", request.Block.TxHash())
696+
break // TODO(karalabe): return error eventually, but wait a few releases
697+
}
690698
if err := request.sanityCheck(); err != nil {
691699
return err
692700
}

‎eth/handler_test.go‎

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)