aboutsummaryrefslogtreecommitdiffstats
path: root/eth/downloader/downloader_test.go
diff options
context:
space:
mode:
authorobscuren <geffobscura@gmail.com>2015-05-01 06:23:51 +0800
committerobscuren <geffobscura@gmail.com>2015-05-01 21:58:44 +0800
commit016f152b36106130fa42514ef6cfacc09dfc3142 (patch)
treea38fa42c59a8a4e0c18b68fc8e5dcb6bd533719b /eth/downloader/downloader_test.go
parent8595198c1b56364bb9589a912d2a9797b93a3357 (diff)
downloadgo-tangerine-016f152b36106130fa42514ef6cfacc09dfc3142.tar.gz
go-tangerine-016f152b36106130fa42514ef6cfacc09dfc3142.tar.zst
go-tangerine-016f152b36106130fa42514ef6cfacc09dfc3142.zip
eth, eth/downloader: Moved block processing & graceful shutdown
The downloader is no longer responsible for processing blocks. The eth-protocol handler now takes care of this instead. Added graceful shutdown during block processing. Closes #846
Diffstat (limited to 'eth/downloader/downloader_test.go')
-rw-r--r--eth/downloader/downloader_test.go55
1 files changed, 35 insertions, 20 deletions
diff --git a/eth/downloader/downloader_test.go b/eth/downloader/downloader_test.go
index 5518163ca..d13818b37 100644
--- a/eth/downloader/downloader_test.go
+++ b/eth/downloader/downloader_test.go
@@ -8,8 +8,6 @@ import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
- "github.com/ethereum/go-ethereum/logger"
- "github.com/ethereum/go-ethereum/logger/glog"
)
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}
@@ -28,7 +26,7 @@ func createHashes(start, amount int) (hashes []common.Hash) {
func createBlocksFromHashes(hashes []common.Hash) map[common.Hash]*types.Block {
blocks := make(map[common.Hash]*types.Block)
for i, hash := range hashes {
- header := &types.Header{Number: big.NewInt(int64(i))}
+ header := &types.Header{Number: big.NewInt(int64(len(hashes) - i))}
blocks[hash] = types.NewBlockWithHeader(header)
blocks[hash].HeaderHash = hash
}
@@ -43,13 +41,11 @@ type downloadTester struct {
t *testing.T
pcount int
done chan bool
-
- insertedBlocks int
}
func newTester(t *testing.T, hashes []common.Hash, blocks map[common.Hash]*types.Block) *downloadTester {
tester := &downloadTester{t: t, hashes: hashes, blocks: blocks, done: make(chan bool)}
- downloader := New(tester.hasBlock, tester.insertChain)
+ downloader := New(tester.hasBlock, tester.getBlock)
tester.downloader = downloader
return tester
@@ -62,10 +58,8 @@ func (dl *downloadTester) hasBlock(hash common.Hash) bool {
return false
}
-func (dl *downloadTester) insertChain(blocks types.Blocks) (int, error) {
- dl.insertedBlocks += len(blocks)
-
- return 0, nil
+func (dl *downloadTester) getBlock(hash common.Hash) *types.Block {
+ return dl.blocks[knownHash]
}
func (dl *downloadTester) getHashes(hash common.Hash) error {
@@ -102,9 +96,6 @@ func (dl *downloadTester) badBlocksPeer(id string, td *big.Int, hash common.Hash
}
func TestDownload(t *testing.T) {
- glog.SetV(logger.Detail)
- glog.SetToStderr(true)
-
minDesiredPeerCount = 4
blockTtl = 1 * time.Second
@@ -123,15 +114,13 @@ func TestDownload(t *testing.T) {
t.Error("download error", err)
}
- if tester.insertedBlocks != targetBlocks {
- t.Error("expected", targetBlocks, "have", tester.insertedBlocks)
+ inqueue := len(tester.downloader.queue.blocks)
+ if inqueue != targetBlocks {
+ t.Error("expected", targetBlocks, "have", inqueue)
}
}
func TestMissing(t *testing.T) {
- glog.SetV(logger.Detail)
- glog.SetToStderr(true)
-
targetBlocks := 1000
hashes := createHashes(0, 1000)
extraHashes := createHashes(1001, 1003)
@@ -148,7 +137,33 @@ func TestMissing(t *testing.T) {
t.Error("download error", err)
}
- if tester.insertedBlocks != targetBlocks {
- t.Error("expected", targetBlocks, "have", tester.insertedBlocks)
+ inqueue := len(tester.downloader.queue.blocks)
+ if inqueue != targetBlocks {
+ t.Error("expected", targetBlocks, "have", inqueue)
+ }
+}
+
+func TestTaking(t *testing.T) {
+ minDesiredPeerCount = 4
+ blockTtl = 1 * time.Second
+
+ targetBlocks := 1000
+ hashes := createHashes(0, targetBlocks)
+ 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{})
+
+ err := tester.downloader.Synchronise("peer1", hashes[0])
+ if err != nil {
+ t.Error("download error", err)
+ }
+
+ bs1 := tester.downloader.TakeBlocks(1000)
+ if len(bs1) != 1000 {
+ t.Error("expected to take 1000, got", len(bs1))
}
}