From cb9d29151c4c4883e0a3875da2995b292892bad7 Mon Sep 17 00:00:00 2001 From: Manav Darji Date: Tue, 24 May 2022 16:29:39 +0530 Subject: [PATCH] add more tests --- eth/downloader/downloader_test.go | 35 +++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/eth/downloader/downloader_test.go b/eth/downloader/downloader_test.go index f141e9d019..cb0e656e39 100644 --- a/eth/downloader/downloader_test.go +++ b/eth/downloader/downloader_test.go @@ -43,6 +43,7 @@ import ( "github.com/ethereum/go-ethereum/params" "github.com/ethereum/go-ethereum/rlp" "github.com/ethereum/go-ethereum/trie" + "github.com/stretchr/testify/assert" ) // downloadTester is a test simulator for mocking out local block chain. @@ -1464,3 +1465,37 @@ func TestFakedSyncProgress66WhitelistMatch(t *testing.T) { t.Fatal("succeeded attacker synchronisation") } } + +// TestFakedSyncProgress66NoRemoteCheckpoint tests if in case of missing/invalid +// checkpointed blocks with opposite peer, the sync should fail initially but +// with the retry mechanism, it should succeed eventually. +func TestFakedSyncProgress66NoRemoteCheckpoint(t *testing.T) { + protocol := uint(eth.ETH66) + mode := FullSync + + tester := newTester() + validate := func(count int) (bool, error) { + // only return the `ErrNoRemoteCheckoint` error for the first call + if count == 0 { + return false, whitelist.ErrNoRemoteCheckoint + } + return true, nil + } + tester.downloader.ChainValidator = newWhitelistFake(validate) + + defer tester.terminate() + + chainA := testChainForkLightA.blocks + tester.newPeer("light", protocol, chainA[1:]) + + // Synchronise with the peer and make sure all blocks were retrieved + // Should fail in first attempt + if err := tester.sync("light", nil, mode); err != nil { + assert.Equal(t, whitelist.ErrNoRemoteCheckoint, err, "failed synchronisation") + } + + // Try syncing again, should succeed + if err := tester.sync("light", nil, mode); err != nil { + t.Fatal("succeeded attacker synchronisation") + } +}