|
|
@ -7,6 +7,7 @@ import ( |
|
|
|
"time" |
|
|
|
"time" |
|
|
|
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common" |
|
|
|
"github.com/ethereum/go-ethereum/common" |
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/core" |
|
|
|
"github.com/ethereum/go-ethereum/core/types" |
|
|
|
"github.com/ethereum/go-ethereum/core/types" |
|
|
|
"github.com/ethereum/go-ethereum/event" |
|
|
|
"github.com/ethereum/go-ethereum/event" |
|
|
|
) |
|
|
|
) |
|
|
@ -14,6 +15,7 @@ import ( |
|
|
|
var ( |
|
|
|
var ( |
|
|
|
knownHash = common.Hash{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} |
|
|
|
knownHash = common.Hash{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} |
|
|
|
unknownHash = common.Hash{9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9} |
|
|
|
unknownHash = common.Hash{9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9} |
|
|
|
|
|
|
|
bannedHash = common.Hash{5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5} |
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
func createHashes(start, amount int) (hashes []common.Hash) { |
|
|
|
func createHashes(start, amount int) (hashes []common.Hash) { |
|
|
@ -21,7 +23,7 @@ func createHashes(start, amount int) (hashes []common.Hash) { |
|
|
|
hashes[len(hashes)-1] = knownHash |
|
|
|
hashes[len(hashes)-1] = knownHash |
|
|
|
|
|
|
|
|
|
|
|
for i := range hashes[:len(hashes)-1] { |
|
|
|
for i := range hashes[:len(hashes)-1] { |
|
|
|
binary.BigEndian.PutUint64(hashes[i][:8], uint64(i+2)) |
|
|
|
binary.BigEndian.PutUint64(hashes[i][:8], uint64(start+i+2)) |
|
|
|
} |
|
|
|
} |
|
|
|
return |
|
|
|
return |
|
|
|
} |
|
|
|
} |
|
|
@ -56,7 +58,6 @@ type downloadTester struct { |
|
|
|
maxHashFetch int // Overrides the maximum number of retrieved hashes
|
|
|
|
maxHashFetch int // Overrides the maximum number of retrieved hashes
|
|
|
|
|
|
|
|
|
|
|
|
t *testing.T |
|
|
|
t *testing.T |
|
|
|
pcount int |
|
|
|
|
|
|
|
done chan bool |
|
|
|
done chan bool |
|
|
|
activePeerId string |
|
|
|
activePeerId string |
|
|
|
} |
|
|
|
} |
|
|
@ -114,12 +115,6 @@ func (dl *downloadTester) syncTake(peerId string, head common.Hash) ([]*Block, e |
|
|
|
return took, err |
|
|
|
return took, err |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (dl *downloadTester) insertBlocks(blocks types.Blocks) { |
|
|
|
|
|
|
|
for _, block := range blocks { |
|
|
|
|
|
|
|
dl.chain = append(dl.chain, block.Hash()) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (dl *downloadTester) hasBlock(hash common.Hash) bool { |
|
|
|
func (dl *downloadTester) hasBlock(hash common.Hash) bool { |
|
|
|
for _, h := range dl.chain { |
|
|
|
for _, h := range dl.chain { |
|
|
|
if h == hash { |
|
|
|
if h == hash { |
|
|
@ -174,158 +169,131 @@ func (dl *downloadTester) getBlocks(id string) func([]common.Hash) error { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (dl *downloadTester) newPeer(id string, td *big.Int, hash common.Hash) { |
|
|
|
// newPeer registers a new block download source into the syncer.
|
|
|
|
dl.pcount++ |
|
|
|
func (dl *downloadTester) newPeer(id string, td *big.Int, hash common.Hash) error { |
|
|
|
|
|
|
|
return dl.downloader.RegisterPeer(id, hash, dl.getHashes, dl.getBlocks(id)) |
|
|
|
dl.downloader.RegisterPeer(id, hash, dl.getHashes, dl.getBlocks(id)) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (dl *downloadTester) badBlocksPeer(id string, td *big.Int, hash common.Hash) { |
|
|
|
|
|
|
|
dl.pcount++ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// This bad peer never returns any blocks
|
|
|
|
|
|
|
|
dl.downloader.RegisterPeer(id, hash, dl.getHashes, func([]common.Hash) error { |
|
|
|
|
|
|
|
return nil |
|
|
|
|
|
|
|
}) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func TestDownload(t *testing.T) { |
|
|
|
// Tests that simple synchronization, without throttling from a good peer works.
|
|
|
|
minDesiredPeerCount = 4 |
|
|
|
func TestSynchronisation(t *testing.T) { |
|
|
|
blockHardTTL = 1 * time.Second |
|
|
|
// Create a small enough block chain to download and the tester
|
|
|
|
|
|
|
|
targetBlocks := blockCacheLimit - 15 |
|
|
|
targetBlocks := 1000 |
|
|
|
|
|
|
|
hashes := createHashes(0, targetBlocks) |
|
|
|
hashes := createHashes(0, targetBlocks) |
|
|
|
blocks := createBlocksFromHashes(hashes) |
|
|
|
blocks := createBlocksFromHashes(hashes) |
|
|
|
tester := newTester(t, hashes, blocks) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
tester.newPeer("peer1", big.NewInt(10000), hashes[0]) |
|
|
|
|
|
|
|
tester.newPeer("peer2", big.NewInt(0), common.Hash{}) |
|
|
|
|
|
|
|
tester.badBlocksPeer("peer3", big.NewInt(0), common.Hash{}) |
|
|
|
|
|
|
|
tester.badBlocksPeer("peer4", big.NewInt(0), common.Hash{}) |
|
|
|
|
|
|
|
tester.activePeerId = "peer1" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
err := tester.sync("peer1", hashes[0]) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
t.Error("download error", err) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
inqueue := len(tester.downloader.queue.blockCache) |
|
|
|
|
|
|
|
if inqueue != targetBlocks { |
|
|
|
|
|
|
|
t.Error("expected", targetBlocks, "have", inqueue) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func TestMissing(t *testing.T) { |
|
|
|
|
|
|
|
targetBlocks := 1000 |
|
|
|
|
|
|
|
hashes := createHashes(0, 1000) |
|
|
|
|
|
|
|
extraHashes := createHashes(1001, 1003) |
|
|
|
|
|
|
|
blocks := createBlocksFromHashes(append(extraHashes, hashes...)) |
|
|
|
|
|
|
|
tester := newTester(t, hashes, blocks) |
|
|
|
tester := newTester(t, hashes, blocks) |
|
|
|
|
|
|
|
tester.newPeer("peer", big.NewInt(10000), hashes[0]) |
|
|
|
|
|
|
|
|
|
|
|
tester.newPeer("peer1", big.NewInt(10000), hashes[len(hashes)-1]) |
|
|
|
// Synchronise with the peer and make sure all blocks were retrieved
|
|
|
|
|
|
|
|
if err := tester.sync("peer", hashes[0]); err != nil { |
|
|
|
hashes = append(extraHashes, hashes[:len(hashes)-1]...) |
|
|
|
t.Fatalf("failed to synchronise blocks: %v", err) |
|
|
|
tester.newPeer("peer2", big.NewInt(0), common.Hash{}) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
err := tester.sync("peer1", hashes[0]) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
t.Error("download error", err) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
if queued := len(tester.downloader.queue.blockPool); queued != targetBlocks { |
|
|
|
inqueue := len(tester.downloader.queue.blockCache) |
|
|
|
t.Fatalf("synchronised block mismatch: have %v, want %v", queued, targetBlocks) |
|
|
|
if inqueue != targetBlocks { |
|
|
|
|
|
|
|
t.Error("expected", targetBlocks, "have", inqueue) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func TestTaking(t *testing.T) { |
|
|
|
// Tests that the synchronized blocks can be correctly retrieved.
|
|
|
|
minDesiredPeerCount = 4 |
|
|
|
func TestBlockTaking(t *testing.T) { |
|
|
|
blockHardTTL = 1 * time.Second |
|
|
|
// Create a small enough block chain to download and the tester
|
|
|
|
|
|
|
|
targetBlocks := blockCacheLimit - 15 |
|
|
|
targetBlocks := 1000 |
|
|
|
|
|
|
|
hashes := createHashes(0, targetBlocks) |
|
|
|
hashes := createHashes(0, targetBlocks) |
|
|
|
blocks := createBlocksFromHashes(hashes) |
|
|
|
blocks := createBlocksFromHashes(hashes) |
|
|
|
tester := newTester(t, hashes, blocks) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
tester.newPeer("peer1", big.NewInt(10000), hashes[0]) |
|
|
|
tester := newTester(t, hashes, blocks) |
|
|
|
tester.newPeer("peer2", big.NewInt(0), common.Hash{}) |
|
|
|
tester.newPeer("peer", big.NewInt(10000), hashes[0]) |
|
|
|
tester.badBlocksPeer("peer3", big.NewInt(0), common.Hash{}) |
|
|
|
|
|
|
|
tester.badBlocksPeer("peer4", big.NewInt(0), common.Hash{}) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
err := tester.sync("peer1", hashes[0]) |
|
|
|
// Synchronise with the peer and test block retrieval
|
|
|
|
if err != nil { |
|
|
|
if err := tester.sync("peer", hashes[0]); err != nil { |
|
|
|
t.Error("download error", err) |
|
|
|
t.Fatalf("failed to synchronise blocks: %v", err) |
|
|
|
} |
|
|
|
} |
|
|
|
bs := tester.downloader.TakeBlocks() |
|
|
|
if took := tester.downloader.TakeBlocks(); len(took) != targetBlocks { |
|
|
|
if len(bs) != targetBlocks { |
|
|
|
t.Fatalf("took block mismatch: have %v, want %v", len(took), targetBlocks) |
|
|
|
t.Error("retrieved block mismatch: have %v, want %v", len(bs), targetBlocks) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Tests that an inactive downloader will not accept incoming hashes and blocks.
|
|
|
|
func TestInactiveDownloader(t *testing.T) { |
|
|
|
func TestInactiveDownloader(t *testing.T) { |
|
|
|
targetBlocks := 1000 |
|
|
|
// Create a small enough block chain to download and the tester
|
|
|
|
|
|
|
|
targetBlocks := blockCacheLimit - 15 |
|
|
|
hashes := createHashes(0, targetBlocks) |
|
|
|
hashes := createHashes(0, targetBlocks) |
|
|
|
blocks := createBlocksFromHashSet(createHashSet(hashes)) |
|
|
|
blocks := createBlocksFromHashSet(createHashSet(hashes)) |
|
|
|
tester := newTester(t, hashes, nil) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
err := tester.downloader.DeliverHashes("bad peer 001", hashes) |
|
|
|
tester := newTester(t, nil, nil) |
|
|
|
if err != errNoSyncActive { |
|
|
|
|
|
|
|
t.Error("expected no sync error, got", err) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
err = tester.downloader.DeliverBlocks("bad peer 001", blocks) |
|
|
|
// Check that neither hashes nor blocks are accepted
|
|
|
|
if err != errNoSyncActive { |
|
|
|
if err := tester.downloader.DeliverHashes("bad peer", hashes); err != errNoSyncActive { |
|
|
|
t.Error("expected no sync error, got", err) |
|
|
|
t.Errorf("error mismatch: have %v, want %v", err, errNoSyncActive) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if err := tester.downloader.DeliverBlocks("bad peer", blocks); err != errNoSyncActive { |
|
|
|
|
|
|
|
t.Errorf("error mismatch: have %v, want %v", err, errNoSyncActive) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Tests that a canceled download wipes all previously accumulated state.
|
|
|
|
func TestCancel(t *testing.T) { |
|
|
|
func TestCancel(t *testing.T) { |
|
|
|
minDesiredPeerCount = 4 |
|
|
|
// Create a small enough block chain to download and the tester
|
|
|
|
blockHardTTL = 1 * time.Second |
|
|
|
targetBlocks := blockCacheLimit - 15 |
|
|
|
|
|
|
|
|
|
|
|
targetBlocks := 1000 |
|
|
|
|
|
|
|
hashes := createHashes(0, targetBlocks) |
|
|
|
hashes := createHashes(0, targetBlocks) |
|
|
|
blocks := createBlocksFromHashes(hashes) |
|
|
|
blocks := createBlocksFromHashes(hashes) |
|
|
|
tester := newTester(t, hashes, blocks) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
tester.newPeer("peer1", big.NewInt(10000), hashes[0]) |
|
|
|
tester := newTester(t, hashes, blocks) |
|
|
|
|
|
|
|
tester.newPeer("peer", big.NewInt(10000), hashes[0]) |
|
|
|
|
|
|
|
|
|
|
|
err := tester.sync("peer1", hashes[0]) |
|
|
|
// Synchronise with the peer, but cancel afterwards
|
|
|
|
if err != nil { |
|
|
|
if err := tester.sync("peer", hashes[0]); err != nil { |
|
|
|
t.Error("download error", err) |
|
|
|
t.Fatalf("failed to synchronise blocks: %v", err) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if !tester.downloader.Cancel() { |
|
|
|
if !tester.downloader.Cancel() { |
|
|
|
t.Error("cancel operation unsuccessfull") |
|
|
|
t.Fatalf("cancel operation failed") |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// Make sure the queue reports empty and no blocks can be taken
|
|
|
|
hashSize, blockSize := tester.downloader.queue.Size() |
|
|
|
hashCount, blockCount := tester.downloader.queue.Size() |
|
|
|
if hashSize > 0 || blockSize > 0 { |
|
|
|
if hashCount > 0 || blockCount > 0 { |
|
|
|
t.Error("block (", blockSize, ") or hash (", hashSize, ") not 0") |
|
|
|
t.Errorf("block or hash count mismatch: %d hashes, %d blocks, want 0", hashCount, blockCount) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if took := tester.downloader.TakeBlocks(); len(took) != 0 { |
|
|
|
|
|
|
|
t.Errorf("taken blocks mismatch: have %d, want %d", len(took), 0) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Tests that if a large batch of blocks are being downloaded, it is throttled
|
|
|
|
|
|
|
|
// until the cached blocks are retrieved.
|
|
|
|
func TestThrottling(t *testing.T) { |
|
|
|
func TestThrottling(t *testing.T) { |
|
|
|
minDesiredPeerCount = 4 |
|
|
|
// Create a long block chain to download and the tester
|
|
|
|
blockHardTTL = 1 * time.Second |
|
|
|
targetBlocks := 8 * blockCacheLimit |
|
|
|
|
|
|
|
|
|
|
|
targetBlocks := 16 * blockCacheLimit |
|
|
|
|
|
|
|
hashes := createHashes(0, targetBlocks) |
|
|
|
hashes := createHashes(0, targetBlocks) |
|
|
|
blocks := createBlocksFromHashes(hashes) |
|
|
|
blocks := createBlocksFromHashes(hashes) |
|
|
|
tester := newTester(t, hashes, blocks) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
tester.newPeer("peer1", big.NewInt(10000), hashes[0]) |
|
|
|
tester := newTester(t, hashes, blocks) |
|
|
|
tester.newPeer("peer2", big.NewInt(0), common.Hash{}) |
|
|
|
tester.newPeer("peer", big.NewInt(10000), hashes[0]) |
|
|
|
tester.badBlocksPeer("peer3", big.NewInt(0), common.Hash{}) |
|
|
|
|
|
|
|
tester.badBlocksPeer("peer4", big.NewInt(0), common.Hash{}) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Concurrently download and take the blocks
|
|
|
|
// Start a synchronisation concurrently
|
|
|
|
took, err := tester.syncTake("peer1", hashes[0]) |
|
|
|
errc := make(chan error) |
|
|
|
if err != nil { |
|
|
|
go func() { |
|
|
|
t.Fatalf("failed to synchronise blocks: %v", err) |
|
|
|
errc <- tester.sync("peer", hashes[0]) |
|
|
|
|
|
|
|
}() |
|
|
|
|
|
|
|
// Iteratively take some blocks, always checking the retrieval count
|
|
|
|
|
|
|
|
for total := 0; total < targetBlocks; { |
|
|
|
|
|
|
|
// Wait a bit for sync to complete
|
|
|
|
|
|
|
|
for start := time.Now(); time.Since(start) < 3*time.Second; { |
|
|
|
|
|
|
|
time.Sleep(25 * time.Millisecond) |
|
|
|
|
|
|
|
if len(tester.downloader.queue.blockPool) == blockCacheLimit { |
|
|
|
|
|
|
|
break |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
// Fetch the next batch of blocks
|
|
|
|
|
|
|
|
took := tester.downloader.TakeBlocks() |
|
|
|
|
|
|
|
if len(took) != blockCacheLimit { |
|
|
|
|
|
|
|
t.Fatalf("block count mismatch: have %v, want %v", len(took), blockCacheLimit) |
|
|
|
} |
|
|
|
} |
|
|
|
if len(took) != targetBlocks { |
|
|
|
total += len(took) |
|
|
|
t.Fatalf("downloaded block mismatch: have %v, want %v", len(took), targetBlocks) |
|
|
|
if total > targetBlocks { |
|
|
|
|
|
|
|
t.Fatalf("target block count mismatch: have %v, want %v", total, targetBlocks) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if err := <-errc; err != nil { |
|
|
|
|
|
|
|
t.Fatalf("block synchronization failed: %v", err) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -559,3 +527,86 @@ func TestMadeupParentBlockChainAttack(t *testing.T) { |
|
|
|
t.Fatalf("failed to synchronise blocks: %v", err) |
|
|
|
t.Fatalf("failed to synchronise blocks: %v", err) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Tests that if one/multiple malicious peers try to feed a banned blockchain to
|
|
|
|
|
|
|
|
// the downloader, it will not keep refetching the same chain indefinitely, but
|
|
|
|
|
|
|
|
// gradually block pieces of it, until it's head is also blocked.
|
|
|
|
|
|
|
|
func TestBannedChainStarvationAttack(t *testing.T) { |
|
|
|
|
|
|
|
// Construct a valid chain, but ban one of the hashes in it
|
|
|
|
|
|
|
|
hashes := createHashes(0, 8*blockCacheLimit) |
|
|
|
|
|
|
|
hashes[len(hashes)/2+23] = bannedHash // weird index to have non multiple of ban chunk size
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
blocks := createBlocksFromHashes(hashes) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Create the tester and ban the selected hash
|
|
|
|
|
|
|
|
tester := newTester(t, hashes, blocks) |
|
|
|
|
|
|
|
tester.downloader.banned.Add(bannedHash) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Iteratively try to sync, and verify that the banned hash list grows until
|
|
|
|
|
|
|
|
// the head of the invalid chain is blocked too.
|
|
|
|
|
|
|
|
tester.newPeer("attack", big.NewInt(10000), hashes[0]) |
|
|
|
|
|
|
|
for banned := tester.downloader.banned.Size(); ; { |
|
|
|
|
|
|
|
// Try to sync with the attacker, check hash chain failure
|
|
|
|
|
|
|
|
if _, err := tester.syncTake("attack", hashes[0]); err != ErrInvalidChain { |
|
|
|
|
|
|
|
t.Fatalf("synchronisation error mismatch: have %v, want %v", err, ErrInvalidChain) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
// Check that the ban list grew with at least 1 new item, or all banned
|
|
|
|
|
|
|
|
bans := tester.downloader.banned.Size() |
|
|
|
|
|
|
|
if bans < banned+1 { |
|
|
|
|
|
|
|
if tester.downloader.banned.Has(hashes[0]) { |
|
|
|
|
|
|
|
break |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
t.Fatalf("ban count mismatch: have %v, want %v+", bans, banned+1) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
banned = bans |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
// Check that after banning an entire chain, bad peers get dropped
|
|
|
|
|
|
|
|
if err := tester.newPeer("new attacker", big.NewInt(10000), hashes[0]); err != errBannedHead { |
|
|
|
|
|
|
|
t.Fatalf("peer registration mismatch: have %v, want %v", err, errBannedHead) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if peer := tester.downloader.peers.Peer("net attacker"); peer != nil { |
|
|
|
|
|
|
|
t.Fatalf("banned attacker registered: %v", peer) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Tests that if a peer sends excessively many/large invalid chains that are
|
|
|
|
|
|
|
|
// gradually banned, it will have an upper limit on the consumed memory and also
|
|
|
|
|
|
|
|
// the origin bad hashes will not be evacuated.
|
|
|
|
|
|
|
|
func TestBannedChainMemoryExhaustionAttack(t *testing.T) { |
|
|
|
|
|
|
|
// Reduce the test size a bit
|
|
|
|
|
|
|
|
MaxBlockFetch = 4 |
|
|
|
|
|
|
|
maxBannedHashes = 256 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Construct a banned chain with more chunks than the ban limit
|
|
|
|
|
|
|
|
hashes := createHashes(0, maxBannedHashes*MaxBlockFetch) |
|
|
|
|
|
|
|
hashes[len(hashes)-1] = bannedHash // weird index to have non multiple of ban chunk size
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
blocks := createBlocksFromHashes(hashes) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Create the tester and ban the selected hash
|
|
|
|
|
|
|
|
tester := newTester(t, hashes, blocks) |
|
|
|
|
|
|
|
tester.downloader.banned.Add(bannedHash) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Iteratively try to sync, and verify that the banned hash list grows until
|
|
|
|
|
|
|
|
// the head of the invalid chain is blocked too.
|
|
|
|
|
|
|
|
tester.newPeer("attack", big.NewInt(10000), hashes[0]) |
|
|
|
|
|
|
|
for { |
|
|
|
|
|
|
|
// Try to sync with the attacker, check hash chain failure
|
|
|
|
|
|
|
|
if _, err := tester.syncTake("attack", hashes[0]); err != ErrInvalidChain { |
|
|
|
|
|
|
|
t.Fatalf("synchronisation error mismatch: have %v, want %v", err, ErrInvalidChain) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
// Short circuit if the entire chain was banned
|
|
|
|
|
|
|
|
if tester.downloader.banned.Has(hashes[0]) { |
|
|
|
|
|
|
|
break |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
// Otherwise ensure we never exceed the memory allowance and the hard coded bans are untouched
|
|
|
|
|
|
|
|
if bans := tester.downloader.banned.Size(); bans > maxBannedHashes { |
|
|
|
|
|
|
|
t.Fatalf("ban cap exceeded: have %v, want max %v", bans, maxBannedHashes) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
for hash, _ := range core.BadHashes { |
|
|
|
|
|
|
|
if !tester.downloader.banned.Has(hash) { |
|
|
|
|
|
|
|
t.Fatalf("hard coded ban evacuated: %x", hash) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|