core: split out TD from database and all internals
This commit is contained in:
@ -87,6 +87,9 @@ type blockRetrievalFn func(common.Hash) *types.Block
|
||||
// headRetrievalFn is a callback type for retrieving the head block from the local chain.
|
||||
type headRetrievalFn func() *types.Block
|
||||
|
||||
// tdRetrievalFn is a callback type for retrieving the total difficulty of a local block.
|
||||
type tdRetrievalFn func(common.Hash) *big.Int
|
||||
|
||||
// chainInsertFn is a callback type to insert a batch of blocks into the local chain.
|
||||
type chainInsertFn func(types.Blocks) (int, error)
|
||||
|
||||
@ -136,6 +139,7 @@ type Downloader struct {
|
||||
hasBlock hashCheckFn // Checks if a block is present in the chain
|
||||
getBlock blockRetrievalFn // Retrieves a block from the chain
|
||||
headBlock headRetrievalFn // Retrieves the head block from the chain
|
||||
getTd tdRetrievalFn // Retrieves the TD of a block from the chain
|
||||
insertChain chainInsertFn // Injects a batch of blocks into the chain
|
||||
dropPeer peerDropFn // Drops a peer for misbehaving
|
||||
|
||||
@ -168,7 +172,7 @@ type Block struct {
|
||||
}
|
||||
|
||||
// New creates a new downloader to fetch hashes and blocks from remote peers.
|
||||
func New(mux *event.TypeMux, hasBlock hashCheckFn, getBlock blockRetrievalFn, headBlock headRetrievalFn, insertChain chainInsertFn, dropPeer peerDropFn) *Downloader {
|
||||
func New(mux *event.TypeMux, hasBlock hashCheckFn, getBlock blockRetrievalFn, headBlock headRetrievalFn, getTd tdRetrievalFn, insertChain chainInsertFn, dropPeer peerDropFn) *Downloader {
|
||||
return &Downloader{
|
||||
mux: mux,
|
||||
queue: newQueue(),
|
||||
@ -176,6 +180,7 @@ func New(mux *event.TypeMux, hasBlock hashCheckFn, getBlock blockRetrievalFn, he
|
||||
hasBlock: hasBlock,
|
||||
getBlock: getBlock,
|
||||
headBlock: headBlock,
|
||||
getTd: getTd,
|
||||
insertChain: insertChain,
|
||||
dropPeer: dropPeer,
|
||||
newPeerCh: make(chan *peer, 1),
|
||||
@ -582,7 +587,7 @@ func (d *Downloader) fetchHashes61(p *peer, td *big.Int, from uint64) error {
|
||||
// L: Sync begins, and finds common ancestor at 11
|
||||
// L: Request new hashes up from 11 (R's TD was higher, it must have something)
|
||||
// R: Nothing to give
|
||||
if !gotHashes && td.Cmp(d.headBlock().Td) > 0 {
|
||||
if !gotHashes && td.Cmp(d.getTd(d.headBlock().Hash())) > 0 {
|
||||
return errStallingPeer
|
||||
}
|
||||
return nil
|
||||
@ -958,7 +963,7 @@ func (d *Downloader) fetchHeaders(p *peer, td *big.Int, from uint64) error {
|
||||
// L: Sync begins, and finds common ancestor at 11
|
||||
// L: Request new headers up from 11 (R's TD was higher, it must have something)
|
||||
// R: Nothing to give
|
||||
if !gotHeaders && td.Cmp(d.headBlock().Td) > 0 {
|
||||
if !gotHeaders && td.Cmp(d.getTd(d.headBlock().Hash())) > 0 {
|
||||
return errStallingPeer
|
||||
}
|
||||
return nil
|
||||
|
@ -93,21 +93,25 @@ func makeChainFork(n, f int, parent *types.Block) (h1, h2 []common.Hash, b1, b2
|
||||
type downloadTester struct {
|
||||
downloader *Downloader
|
||||
|
||||
ownHashes []common.Hash // Hash chain belonging to the tester
|
||||
ownBlocks map[common.Hash]*types.Block // Blocks belonging to the tester
|
||||
peerHashes map[string][]common.Hash // Hash chain belonging to different test peers
|
||||
peerBlocks map[string]map[common.Hash]*types.Block // Blocks belonging to different test peers
|
||||
ownHashes []common.Hash // Hash chain belonging to the tester
|
||||
ownBlocks map[common.Hash]*types.Block // Blocks belonging to the tester
|
||||
ownChainTd map[common.Hash]*big.Int // Total difficulties of the blocks in the local chain
|
||||
peerHashes map[string][]common.Hash // Hash chain belonging to different test peers
|
||||
peerBlocks map[string]map[common.Hash]*types.Block // Blocks belonging to different test peers
|
||||
peerChainTds map[string]map[common.Hash]*big.Int // Total difficulties of the blocks in the peer chains
|
||||
}
|
||||
|
||||
// newTester creates a new downloader test mocker.
|
||||
func newTester() *downloadTester {
|
||||
tester := &downloadTester{
|
||||
ownHashes: []common.Hash{genesis.Hash()},
|
||||
ownBlocks: map[common.Hash]*types.Block{genesis.Hash(): genesis},
|
||||
peerHashes: make(map[string][]common.Hash),
|
||||
peerBlocks: make(map[string]map[common.Hash]*types.Block),
|
||||
ownHashes: []common.Hash{genesis.Hash()},
|
||||
ownBlocks: map[common.Hash]*types.Block{genesis.Hash(): genesis},
|
||||
ownChainTd: map[common.Hash]*big.Int{genesis.Hash(): genesis.Difficulty()},
|
||||
peerHashes: make(map[string][]common.Hash),
|
||||
peerBlocks: make(map[string]map[common.Hash]*types.Block),
|
||||
peerChainTds: make(map[string]map[common.Hash]*big.Int),
|
||||
}
|
||||
tester.downloader = New(new(event.TypeMux), tester.hasBlock, tester.getBlock, tester.headBlock, tester.insertChain, tester.dropPeer)
|
||||
tester.downloader = New(new(event.TypeMux), tester.hasBlock, tester.getBlock, tester.headBlock, tester.getTd, tester.insertChain, tester.dropPeer)
|
||||
|
||||
return tester
|
||||
}
|
||||
@ -119,8 +123,8 @@ func (dl *downloadTester) sync(id string, td *big.Int) error {
|
||||
// If no particular TD was requested, load from the peer's blockchain
|
||||
if td == nil {
|
||||
td = big.NewInt(1)
|
||||
if block, ok := dl.peerBlocks[id][hash]; ok {
|
||||
td = block.Td
|
||||
if diff, ok := dl.peerChainTds[id][hash]; ok {
|
||||
td = diff
|
||||
}
|
||||
}
|
||||
err := dl.downloader.synchronise(id, hash, td)
|
||||
@ -152,6 +156,11 @@ func (dl *downloadTester) headBlock() *types.Block {
|
||||
return dl.getBlock(dl.ownHashes[len(dl.ownHashes)-1])
|
||||
}
|
||||
|
||||
// getTd retrieves the block's total difficulty from the canonical chain.
|
||||
func (dl *downloadTester) getTd(hash common.Hash) *big.Int {
|
||||
return dl.ownChainTd[hash]
|
||||
}
|
||||
|
||||
// insertChain injects a new batch of blocks into the simulated chain.
|
||||
func (dl *downloadTester) insertChain(blocks types.Blocks) (int, error) {
|
||||
for i, block := range blocks {
|
||||
@ -160,6 +169,7 @@ func (dl *downloadTester) insertChain(blocks types.Blocks) (int, error) {
|
||||
}
|
||||
dl.ownHashes = append(dl.ownHashes, block.Hash())
|
||||
dl.ownBlocks[block.Hash()] = block
|
||||
dl.ownChainTd[block.Hash()] = dl.ownChainTd[block.ParentHash()]
|
||||
}
|
||||
return len(blocks), nil
|
||||
}
|
||||
@ -180,9 +190,16 @@ func (dl *downloadTester) newSlowPeer(id string, version int, hashes []common.Ha
|
||||
// Assign the owned hashes and blocks to the peer (deep copy)
|
||||
dl.peerHashes[id] = make([]common.Hash, len(hashes))
|
||||
copy(dl.peerHashes[id], hashes)
|
||||
|
||||
dl.peerBlocks[id] = make(map[common.Hash]*types.Block)
|
||||
for hash, block := range blocks {
|
||||
dl.peerBlocks[id][hash] = block
|
||||
dl.peerChainTds[id] = make(map[common.Hash]*big.Int)
|
||||
for _, hash := range hashes {
|
||||
if block, ok := blocks[hash]; ok {
|
||||
dl.peerBlocks[id][hash] = block
|
||||
if parent, ok := dl.peerBlocks[id][block.ParentHash()]; ok {
|
||||
dl.peerChainTds[id][hash] = new(big.Int).Add(block.Difficulty(), dl.peerChainTds[id][parent.Hash()])
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return err
|
||||
@ -192,6 +209,7 @@ func (dl *downloadTester) newSlowPeer(id string, version int, hashes []common.Ha
|
||||
func (dl *downloadTester) dropPeer(id string) {
|
||||
delete(dl.peerHashes, id)
|
||||
delete(dl.peerBlocks, id)
|
||||
delete(dl.peerChainTds, id)
|
||||
|
||||
dl.downloader.UnregisterPeer(id)
|
||||
}
|
||||
|
Reference in New Issue
Block a user