Commit a8a2b2a4 authored by obscuren's avatar obscuren

downloader: added missing blocks catchup functionality

When a parent is missing in the block list an attempt should be made to
fetch the missing parent and grandparents.
parent 7dcb9825
This diff is collapsed.
...@@ -14,7 +14,7 @@ import ( ...@@ -14,7 +14,7 @@ import (
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} 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}
func createHashes(amount int) (hashes []common.Hash) { func createHashes(start, amount int) (hashes []common.Hash) {
hashes = make([]common.Hash, amount+1) hashes = make([]common.Hash, amount+1)
hashes[len(hashes)-1] = knownHash hashes[len(hashes)-1] = knownHash
...@@ -49,7 +49,7 @@ type downloadTester struct { ...@@ -49,7 +49,7 @@ type downloadTester struct {
func newTester(t *testing.T, hashes []common.Hash, blocks map[common.Hash]*types.Block) *downloadTester { 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)} tester := &downloadTester{t: t, hashes: hashes, blocks: blocks, done: make(chan bool)}
downloader := New(tester.hasBlock, tester.insertChain) downloader := New(tester.hasBlock, tester.insertChain, func() *big.Int { return new(big.Int) })
tester.downloader = downloader tester.downloader = downloader
return tester return tester
...@@ -84,7 +84,7 @@ func (dl *downloadTester) getBlocks(id string) func([]common.Hash) error { ...@@ -84,7 +84,7 @@ func (dl *downloadTester) getBlocks(id string) func([]common.Hash) error {
blocks[i] = dl.blocks[hash] blocks[i] = dl.blocks[hash]
} }
go dl.downloader.DeliverBlocks(id, blocks) go dl.downloader.DeliverChunk(id, blocks)
return nil return nil
} }
...@@ -109,11 +109,11 @@ func TestDownload(t *testing.T) { ...@@ -109,11 +109,11 @@ func TestDownload(t *testing.T) {
glog.SetV(logger.Detail) glog.SetV(logger.Detail)
glog.SetToStderr(true) glog.SetToStderr(true)
hashes := createHashes(1000) hashes := createHashes(0, 1000)
blocks := createBlocksFromHashes(hashes) blocks := createBlocksFromHashes(hashes)
tester := newTester(t, hashes, blocks) tester := newTester(t, hashes, blocks)
tester.newPeer("peer1", big.NewInt(10000), hashes[len(hashes)-1]) tester.newPeer("peer1", big.NewInt(10000), hashes[0])
tester.newPeer("peer2", big.NewInt(0), common.Hash{}) tester.newPeer("peer2", big.NewInt(0), common.Hash{})
tester.badBlocksPeer("peer3", big.NewInt(0), common.Hash{}) tester.badBlocksPeer("peer3", big.NewInt(0), common.Hash{})
tester.badBlocksPeer("peer4", big.NewInt(0), common.Hash{}) tester.badBlocksPeer("peer4", big.NewInt(0), common.Hash{})
...@@ -126,3 +126,30 @@ success: ...@@ -126,3 +126,30 @@ success:
t.Error("timout") t.Error("timout")
} }
} }
func TestMissing(t *testing.T) {
t.Skip()
glog.SetV(logger.Detail)
glog.SetToStderr(true)
hashes := createHashes(0, 1000)
extraHashes := createHashes(1001, 1003)
blocks := createBlocksFromHashes(append(extraHashes, hashes...))
tester := newTester(t, hashes, blocks)
tester.newPeer("peer1", big.NewInt(10000), hashes[len(hashes)-1])
hashes = append(extraHashes, hashes[:len(hashes)-1]...)
tester.newPeer("peer2", big.NewInt(0), common.Hash{})
success1:
select {
case <-tester.done:
break success1
case <-time.After(10 * time.Second): // XXX this could actually fail on a slow computer
t.Error("timout")
}
tester.downloader.AddBlock("peer2", blocks[hashes[len(hashes)-1]], big.NewInt(10001))
}
...@@ -13,9 +13,51 @@ const ( ...@@ -13,9 +13,51 @@ const (
idleState = 4 idleState = 4
) )
type hashFetcherFn func(common.Hash) error
type blockFetcherFn func([]common.Hash) error
// XXX make threadsafe!!!!
type peers map[string]*peer
func (p peers) get(state int) []*peer {
var peers []*peer
for _, peer := range p {
peer.mu.RLock()
if peer.state == state {
peers = append(peers, peer)
}
peer.mu.RUnlock()
}
return peers
}
func (p peers) setState(id string, state int) {
if peer, exist := p[id]; exist {
peer.mu.Lock()
defer peer.mu.Unlock()
peer.state = state
}
}
func (p peers) getPeer(id string) *peer {
return p[id]
}
func (p peers) bestPeer() *peer {
var peer *peer
for _, cp := range p {
if peer == nil || cp.td.Cmp(peer.td) > 0 {
peer = cp
}
}
return peer
}
// peer represents an active peer // peer represents an active peer
type peer struct { type peer struct {
state int state int // Peer state (working, idle)
rep int // TODO peer reputation
mu sync.RWMutex mu sync.RWMutex
id string id string
......
...@@ -2,9 +2,11 @@ package downloader ...@@ -2,9 +2,11 @@ package downloader
import ( import (
"math" "math"
"math/big"
"sync" "sync"
"time" "time"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/types"
"gopkg.in/fatih/set.v0" "gopkg.in/fatih/set.v0"
) )
...@@ -12,6 +14,8 @@ import ( ...@@ -12,6 +14,8 @@ import (
// queue represents hashes that are either need fetching or are being fetched // queue represents hashes that are either need fetching or are being fetched
type queue struct { type queue struct {
hashPool *set.Set hashPool *set.Set
fetchPool *set.Set
blockHashes *set.Set
mu sync.Mutex mu sync.Mutex
fetching map[string]*chunk fetching map[string]*chunk
...@@ -21,6 +25,8 @@ type queue struct { ...@@ -21,6 +25,8 @@ type queue struct {
func newqueue() *queue { func newqueue() *queue {
return &queue{ return &queue{
hashPool: set.New(), hashPool: set.New(),
fetchPool: set.New(),
blockHashes: set.New(),
fetching: make(map[string]*chunk), fetching: make(map[string]*chunk),
} }
} }
...@@ -50,6 +56,8 @@ func (c *queue) get(p *peer, max int) *chunk { ...@@ -50,6 +56,8 @@ func (c *queue) get(p *peer, max int) *chunk {
}) })
// remove the fetchable hashes from hash pool // remove the fetchable hashes from hash pool
c.hashPool.Separate(hashes) c.hashPool.Separate(hashes)
c.fetchPool.Merge(hashes)
// Create a new chunk for the seperated hashes. The time is being used // Create a new chunk for the seperated hashes. The time is being used
// to reset the chunk (timeout) // to reset the chunk (timeout)
chunk := &chunk{hashes, time.Now()} chunk := &chunk{hashes, time.Now()}
...@@ -60,6 +68,22 @@ func (c *queue) get(p *peer, max int) *chunk { ...@@ -60,6 +68,22 @@ func (c *queue) get(p *peer, max int) *chunk {
return chunk return chunk
} }
func (c *queue) has(hash common.Hash) bool {
return c.hashPool.Has(hash) || c.fetchPool.Has(hash)
}
func (c *queue) addBlock(id string, block *types.Block, td *big.Int) {
c.mu.Lock()
defer c.mu.Unlock()
// when adding a block make sure it doesn't already exist
if !c.blockHashes.Has(block.Hash()) {
c.hashPool.Remove(block.Hash())
c.blocks = append(c.blocks, block)
}
}
// deliver delivers a chunk to the queue that was requested of the peer
func (c *queue) deliver(id string, blocks []*types.Block) { func (c *queue) deliver(id string, blocks []*types.Block) {
c.mu.Lock() c.mu.Lock()
defer c.mu.Unlock() defer c.mu.Unlock()
...@@ -70,15 +94,19 @@ func (c *queue) deliver(id string, blocks []*types.Block) { ...@@ -70,15 +94,19 @@ func (c *queue) deliver(id string, blocks []*types.Block) {
delete(c.fetching, id) delete(c.fetching, id)
// seperate the blocks and the hashes // seperate the blocks and the hashes
chunk.seperate(blocks) blockHashes := chunk.fetchedHashes(blocks)
// merge block hashes
c.blockHashes.Merge(blockHashes)
// Add the blocks // Add the blocks
c.blocks = append(c.blocks, blocks...) c.blocks = append(c.blocks, blocks...)
// Add back whatever couldn't be delivered // Add back whatever couldn't be delivered
c.hashPool.Merge(chunk.hashes) c.hashPool.Merge(chunk.hashes)
c.fetchPool.Separate(chunk.hashes)
} }
} }
// puts puts sets of hashes on to the queue for fetching
func (c *queue) put(hashes *set.Set) { func (c *queue) put(hashes *set.Set) {
c.mu.Lock() c.mu.Lock()
defer c.mu.Unlock() defer c.mu.Unlock()
...@@ -91,8 +119,12 @@ type chunk struct { ...@@ -91,8 +119,12 @@ type chunk struct {
itime time.Time itime time.Time
} }
func (ch *chunk) seperate(blocks []*types.Block) { func (ch *chunk) fetchedHashes(blocks []*types.Block) *set.Set {
fhashes := set.New()
for _, block := range blocks { for _, block := range blocks {
ch.hashes.Remove(block.Hash()) fhashes.Add(block.Hash())
} }
ch.hashes.Separate(fhashes)
return fhashes
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment