mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-06-10 00:41:35 +00:00
eth/downloader: add invalid body test
This commit is contained in:
parent
6bd6799a1e
commit
a03afefe2e
1 changed files with 34 additions and 1 deletions
|
|
@ -96,6 +96,7 @@ func (dl *downloadTester) newPeer(id string, version uint, blocks []*types.Block
|
||||||
id: id,
|
id: id,
|
||||||
chain: newTestBlockchain(blocks),
|
chain: newTestBlockchain(blocks),
|
||||||
withholdBodies: make(map[common.Hash]struct{}),
|
withholdBodies: make(map[common.Hash]struct{}),
|
||||||
|
dropped: make(chan error, 1),
|
||||||
}
|
}
|
||||||
dl.peers[id] = peer
|
dl.peers[id] = peer
|
||||||
|
|
||||||
|
|
@ -121,8 +122,11 @@ func (dl *downloadTester) dropPeer(id string) {
|
||||||
type downloadTesterPeer struct {
|
type downloadTesterPeer struct {
|
||||||
dl *downloadTester
|
dl *downloadTester
|
||||||
withholdBodies map[common.Hash]struct{}
|
withholdBodies map[common.Hash]struct{}
|
||||||
|
corruptBodies bool
|
||||||
id string
|
id string
|
||||||
chain *core.BlockChain
|
chain *core.BlockChain
|
||||||
|
|
||||||
|
dropped chan error // signaled when res.Done receives an error
|
||||||
}
|
}
|
||||||
|
|
||||||
func unmarshalRlpHeaders(rlpdata []rlp.RawValue) []*types.Header {
|
func unmarshalRlpHeaders(rlpdata []rlp.RawValue) []*types.Header {
|
||||||
|
|
@ -236,6 +240,11 @@ func (dlp *downloadTesterPeer) RequestBodies(hashes []common.Hash, sink chan *et
|
||||||
txsHashes[i] = hash
|
txsHashes[i] = hash
|
||||||
uncleHashes[i] = types.CalcUncleHash(body.Uncles)
|
uncleHashes[i] = types.CalcUncleHash(body.Uncles)
|
||||||
}
|
}
|
||||||
|
if dlp.corruptBodies {
|
||||||
|
for i := range txsHashes {
|
||||||
|
txsHashes[i] = common.Hash{0xff}
|
||||||
|
}
|
||||||
|
}
|
||||||
req := ð.Request{
|
req := ð.Request{
|
||||||
Peer: dlp.id,
|
Peer: dlp.id,
|
||||||
}
|
}
|
||||||
|
|
@ -248,10 +257,16 @@ func (dlp *downloadTesterPeer) RequestBodies(hashes []common.Hash, sink chan *et
|
||||||
WithdrawalRoots: withdrawalHashes,
|
WithdrawalRoots: withdrawalHashes,
|
||||||
},
|
},
|
||||||
Time: 1,
|
Time: 1,
|
||||||
Done: make(chan error, 1), // Ignore the returned status
|
Done: make(chan error),
|
||||||
}
|
}
|
||||||
go func() {
|
go func() {
|
||||||
sink <- res
|
sink <- res
|
||||||
|
if err := <-res.Done; err != nil {
|
||||||
|
select {
|
||||||
|
case dlp.dropped <- err:
|
||||||
|
default:
|
||||||
|
}
|
||||||
|
}
|
||||||
}()
|
}()
|
||||||
return req, nil
|
return req, nil
|
||||||
}
|
}
|
||||||
|
|
@ -704,3 +719,21 @@ func testSyncProgress(t *testing.T, protocol uint, mode SyncMode) {
|
||||||
t.Fatalf("Failed to sync chain in three seconds")
|
t.Fatalf("Failed to sync chain in three seconds")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestInvalidBodyPeerDrop(t *testing.T) {
|
||||||
|
tester := newTester(t, FullSync)
|
||||||
|
defer tester.terminate()
|
||||||
|
|
||||||
|
chain := testChainBase.shorten(blockCacheMaxItems - 15)
|
||||||
|
peer := tester.newPeer("corrupt", eth.ETH69, chain.blocks[1:])
|
||||||
|
peer.corruptBodies = true
|
||||||
|
|
||||||
|
if err := tester.downloader.BeaconSync(chain.blocks[len(chain.blocks)-1].Header(), nil); err != nil {
|
||||||
|
t.Fatalf("failed to beacon-sync chain: %v", err)
|
||||||
|
}
|
||||||
|
select {
|
||||||
|
case <-peer.dropped:
|
||||||
|
case <-time.After(1 * time.Minute):
|
||||||
|
t.Fatal("peer was not dropped")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue