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
This commit is contained in:
obscuren
2015-05-01 00:23:51 +02:00
parent 8595198c1b
commit 016f152b36
6 changed files with 347 additions and 291 deletions

View File

@ -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))
}
}