Skip to content

Commit

Permalink
eth/downloader: fix downloadTesterPeer to use fake TD
Browse files Browse the repository at this point in the history
.*Starvation.* tests were failing because
errStallingPeer was expected but no error received.
The TD is advertised and assigned to the downloader
as expected, but the handshake with the peer's Head
method was causing the reassignment of the downloader.td
field to the actual (honest) TD value by virtue of
the virtuous dlp.Head method, which returned the actual
chain TD.
This caused the tests to fail because the sync TD was
not the fake one.
So this patch provides a field in the tester peer type
to actually handle lying about TD, which then
make the errStallingPeer error get returned.

Date: 2023-05-17 07:27:56-07:00
Signed-off-by: meows <b5c6@protonmail.com>
  • Loading branch information
meowsbits committed May 17, 2023
1 parent 1d5eed2 commit 45b5465
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions eth/downloader/downloader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,13 +154,18 @@ type downloadTesterPeer struct {
chain *core.BlockChain

withholdHeaders map[common.Hash]struct{}
fakeTD *big.Int
}

// Head constructs a function to retrieve a peer's current head hash
// and total difficulty.
func (dlp *downloadTesterPeer) Head() (common.Hash, *big.Int, *big.Int) {
head := dlp.chain.CurrentBlock()
return head.Hash(), dlp.chain.GetTd(head.Hash(), head.Number.Uint64()), new(big.Int).Set(dlp.chain.CurrentBlock().Difficulty)
td := dlp.chain.GetTd(head.Hash(), head.Number.Uint64())
if dlp.fakeTD != nil {
td.Set(dlp.fakeTD)
}
return head.Hash(), td, new(big.Int).Set(dlp.chain.CurrentBlock().Difficulty)
}

// SetHead constructs a function to retrieve a peer's current head hash
Expand Down Expand Up @@ -992,8 +997,10 @@ func testHighTDStarvationAttack(t *testing.T, protocol uint, mode SyncMode) {
defer tester.terminate()

chain := testChainBase.shorten(1)
tester.newPeer("attack", protocol, chain.blocks[1:])
if err := tester.sync("attack", big.NewInt(1000000), mode); err != errStallingPeer {
dlp := tester.newPeer("attack", protocol, chain.blocks[1:])
fakeTD := big.NewInt(1000000)
dlp.fakeTD = fakeTD
if err := tester.sync("attack", fakeTD, mode); err != errStallingPeer {
t.Fatalf("synchronisation error mismatch: have %v, want %v", err, errStallingPeer)
}
}
Expand Down

0 comments on commit 45b5465

Please sign in to comment.