forked from cerc-io/plugeth
eth/downloader, les, light: Changes in response to review
This commit is contained in:
parent
ae11545bc5
commit
be8f8409bc
@ -115,7 +115,7 @@ type Downloader struct {
|
|||||||
syncStatsLock sync.RWMutex // Lock protecting the sync stats fields
|
syncStatsLock sync.RWMutex // Lock protecting the sync stats fields
|
||||||
|
|
||||||
lightchain LightChain
|
lightchain LightChain
|
||||||
chain BlockChain
|
blockchain BlockChain
|
||||||
|
|
||||||
// Callbacks
|
// Callbacks
|
||||||
dropPeer peerDropFn // Drops a peer for misbehaving
|
dropPeer peerDropFn // Drops a peer for misbehaving
|
||||||
@ -212,7 +212,7 @@ func New(mode SyncMode, stateDb ethdb.Database, mux *event.TypeMux, chain BlockC
|
|||||||
peers: newPeerSet(),
|
peers: newPeerSet(),
|
||||||
rttEstimate: uint64(rttMaxEstimate),
|
rttEstimate: uint64(rttMaxEstimate),
|
||||||
rttConfidence: uint64(1000000),
|
rttConfidence: uint64(1000000),
|
||||||
chain: chain,
|
blockchain: chain,
|
||||||
lightchain: lightchain,
|
lightchain: lightchain,
|
||||||
dropPeer: dropPeer,
|
dropPeer: dropPeer,
|
||||||
headerCh: make(chan dataPack, 1),
|
headerCh: make(chan dataPack, 1),
|
||||||
@ -246,9 +246,9 @@ func (d *Downloader) Progress() ethereum.SyncProgress {
|
|||||||
current := uint64(0)
|
current := uint64(0)
|
||||||
switch d.mode {
|
switch d.mode {
|
||||||
case FullSync:
|
case FullSync:
|
||||||
current = d.chain.CurrentBlock().NumberU64()
|
current = d.blockchain.CurrentBlock().NumberU64()
|
||||||
case FastSync:
|
case FastSync:
|
||||||
current = d.chain.CurrentFastBlock().NumberU64()
|
current = d.blockchain.CurrentFastBlock().NumberU64()
|
||||||
case LightSync:
|
case LightSync:
|
||||||
current = d.lightchain.CurrentHeader().Number.Uint64()
|
current = d.lightchain.CurrentHeader().Number.Uint64()
|
||||||
}
|
}
|
||||||
@ -281,6 +281,11 @@ func (d *Downloader) RegisterPeer(id string, version int, peer Peer) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// RegisterLightPeer injects a light client peer
|
||||||
|
func (d *Downloader) RegisterLightPeer(id string, version int, peer LightPeer) error {
|
||||||
|
return d.RegisterPeer(id, version, &lightPeerWrapper{peer})
|
||||||
|
}
|
||||||
|
|
||||||
// UnregisterPeer remove a peer from the known list, preventing any action from
|
// UnregisterPeer remove a peer from the known list, preventing any action from
|
||||||
// the specified peer. An effort is also made to return any pending fetches into
|
// the specified peer. An effort is also made to return any pending fetches into
|
||||||
// the queue.
|
// the queue.
|
||||||
@ -597,9 +602,9 @@ func (d *Downloader) findAncestor(p *peerConnection, height uint64) (uint64, err
|
|||||||
|
|
||||||
p.log.Debug("Looking for common ancestor", "local", ceil, "remote", height)
|
p.log.Debug("Looking for common ancestor", "local", ceil, "remote", height)
|
||||||
if d.mode == FullSync {
|
if d.mode == FullSync {
|
||||||
ceil = d.chain.CurrentBlock().NumberU64()
|
ceil = d.blockchain.CurrentBlock().NumberU64()
|
||||||
} else if d.mode == FastSync {
|
} else if d.mode == FastSync {
|
||||||
ceil = d.chain.CurrentFastBlock().NumberU64()
|
ceil = d.blockchain.CurrentFastBlock().NumberU64()
|
||||||
}
|
}
|
||||||
if ceil >= MaxForkAncestry {
|
if ceil >= MaxForkAncestry {
|
||||||
floor = int64(ceil - MaxForkAncestry)
|
floor = int64(ceil - MaxForkAncestry)
|
||||||
@ -659,7 +664,7 @@ func (d *Downloader) findAncestor(p *peerConnection, height uint64) (uint64, err
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
// Otherwise check if we already know the header or not
|
// Otherwise check if we already know the header or not
|
||||||
if (d.mode == FullSync && d.chain.HasBlockAndState(headers[i].Hash())) || (d.mode != FullSync && d.lightchain.HasHeader(headers[i].Hash())) {
|
if (d.mode == FullSync && d.blockchain.HasBlockAndState(headers[i].Hash())) || (d.mode != FullSync && d.lightchain.HasHeader(headers[i].Hash())) {
|
||||||
number, hash = headers[i].Number.Uint64(), headers[i].Hash()
|
number, hash = headers[i].Number.Uint64(), headers[i].Hash()
|
||||||
|
|
||||||
// If every header is known, even future ones, the peer straight out lied about its head
|
// If every header is known, even future ones, the peer straight out lied about its head
|
||||||
@ -724,7 +729,7 @@ func (d *Downloader) findAncestor(p *peerConnection, height uint64) (uint64, err
|
|||||||
arrived = true
|
arrived = true
|
||||||
|
|
||||||
// Modify the search interval based on the response
|
// Modify the search interval based on the response
|
||||||
if (d.mode == FullSync && !d.chain.HasBlockAndState(headers[0].Hash())) || (d.mode != FullSync && !d.lightchain.HasHeader(headers[0].Hash())) {
|
if (d.mode == FullSync && !d.blockchain.HasBlockAndState(headers[0].Hash())) || (d.mode != FullSync && !d.lightchain.HasHeader(headers[0].Hash())) {
|
||||||
end = check
|
end = check
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
@ -1147,14 +1152,14 @@ func (d *Downloader) processHeaders(origin uint64, td *big.Int) error {
|
|||||||
}
|
}
|
||||||
lastHeader, lastFastBlock, lastBlock := d.lightchain.CurrentHeader().Number, common.Big0, common.Big0
|
lastHeader, lastFastBlock, lastBlock := d.lightchain.CurrentHeader().Number, common.Big0, common.Big0
|
||||||
if d.mode != LightSync {
|
if d.mode != LightSync {
|
||||||
lastFastBlock = d.chain.CurrentFastBlock().Number()
|
lastFastBlock = d.blockchain.CurrentFastBlock().Number()
|
||||||
lastBlock = d.chain.CurrentBlock().Number()
|
lastBlock = d.blockchain.CurrentBlock().Number()
|
||||||
}
|
}
|
||||||
d.lightchain.Rollback(hashes)
|
d.lightchain.Rollback(hashes)
|
||||||
curFastBlock, curBlock := common.Big0, common.Big0
|
curFastBlock, curBlock := common.Big0, common.Big0
|
||||||
if d.mode != LightSync {
|
if d.mode != LightSync {
|
||||||
curFastBlock = d.chain.CurrentFastBlock().Number()
|
curFastBlock = d.blockchain.CurrentFastBlock().Number()
|
||||||
curBlock = d.chain.CurrentBlock().Number()
|
curBlock = d.blockchain.CurrentBlock().Number()
|
||||||
}
|
}
|
||||||
log.Warn("Rolled back headers", "count", len(hashes),
|
log.Warn("Rolled back headers", "count", len(hashes),
|
||||||
"header", fmt.Sprintf("%d->%d", lastHeader, d.lightchain.CurrentHeader().Number),
|
"header", fmt.Sprintf("%d->%d", lastHeader, d.lightchain.CurrentHeader().Number),
|
||||||
@ -1207,7 +1212,7 @@ func (d *Downloader) processHeaders(origin uint64, td *big.Int) error {
|
|||||||
// L: Request new headers up from 11 (R's TD was higher, it must have something)
|
// L: Request new headers up from 11 (R's TD was higher, it must have something)
|
||||||
// R: Nothing to give
|
// R: Nothing to give
|
||||||
if d.mode != LightSync {
|
if d.mode != LightSync {
|
||||||
if !gotHeaders && td.Cmp(d.chain.GetTdByHash(d.chain.CurrentBlock().Hash())) > 0 {
|
if !gotHeaders && td.Cmp(d.blockchain.GetTdByHash(d.blockchain.CurrentBlock().Hash())) > 0 {
|
||||||
return errStallingPeer
|
return errStallingPeer
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1345,7 +1350,7 @@ func (d *Downloader) importBlockResults(results []*fetchResult) error {
|
|||||||
for i, result := range results[:items] {
|
for i, result := range results[:items] {
|
||||||
blocks[i] = types.NewBlockWithHeader(result.Header).WithBody(result.Transactions, result.Uncles)
|
blocks[i] = types.NewBlockWithHeader(result.Header).WithBody(result.Transactions, result.Uncles)
|
||||||
}
|
}
|
||||||
if index, err := d.chain.InsertChain(blocks); err != nil {
|
if index, err := d.blockchain.InsertChain(blocks); err != nil {
|
||||||
log.Debug("Downloaded item processing failed", "number", results[index].Header.Number, "hash", results[index].Header.Hash(), "err", err)
|
log.Debug("Downloaded item processing failed", "number", results[index].Header.Number, "hash", results[index].Header.Hash(), "err", err)
|
||||||
return errInvalidChain
|
return errInvalidChain
|
||||||
}
|
}
|
||||||
@ -1434,7 +1439,7 @@ func (d *Downloader) commitFastSyncData(results []*fetchResult, stateSync *state
|
|||||||
blocks[i] = types.NewBlockWithHeader(result.Header).WithBody(result.Transactions, result.Uncles)
|
blocks[i] = types.NewBlockWithHeader(result.Header).WithBody(result.Transactions, result.Uncles)
|
||||||
receipts[i] = result.Receipts
|
receipts[i] = result.Receipts
|
||||||
}
|
}
|
||||||
if index, err := d.chain.InsertReceiptChain(blocks, receipts); err != nil {
|
if index, err := d.blockchain.InsertReceiptChain(blocks, receipts); err != nil {
|
||||||
log.Debug("Downloaded item processing failed", "number", results[index].Header.Number, "hash", results[index].Header.Hash(), "err", err)
|
log.Debug("Downloaded item processing failed", "number", results[index].Header.Number, "hash", results[index].Header.Hash(), "err", err)
|
||||||
return errInvalidChain
|
return errInvalidChain
|
||||||
}
|
}
|
||||||
@ -1452,10 +1457,10 @@ func (d *Downloader) commitPivotBlock(result *fetchResult) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
log.Debug("Committing fast sync pivot as new head", "number", b.Number(), "hash", b.Hash())
|
log.Debug("Committing fast sync pivot as new head", "number", b.Number(), "hash", b.Hash())
|
||||||
if _, err := d.chain.InsertReceiptChain([]*types.Block{b}, []types.Receipts{result.Receipts}); err != nil {
|
if _, err := d.blockchain.InsertReceiptChain([]*types.Block{b}, []types.Receipts{result.Receipts}); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return d.chain.FastSyncCommitHead(b.Hash())
|
return d.blockchain.FastSyncCommitHead(b.Hash())
|
||||||
}
|
}
|
||||||
|
|
||||||
// DeliverHeaders injects a new batch of block headers received from a remote
|
// DeliverHeaders injects a new batch of block headers received from a remote
|
||||||
|
@ -288,7 +288,7 @@ func (dl *downloadTester) CurrentFastBlock() *types.Block {
|
|||||||
return dl.genesis
|
return dl.genesis
|
||||||
}
|
}
|
||||||
|
|
||||||
// FastSynccommitHead manually sets the head block to a given hash.
|
// FastSyncCommitHead manually sets the head block to a given hash.
|
||||||
func (dl *downloadTester) FastSyncCommitHead(hash common.Hash) error {
|
func (dl *downloadTester) FastSyncCommitHead(hash common.Hash) error {
|
||||||
// For now only check that the state trie is correct
|
// For now only check that the state trie is correct
|
||||||
if block := dl.GetBlockByHash(hash); block != nil {
|
if block := dl.GetBlockByHash(hash); block != nil {
|
||||||
|
@ -75,19 +75,43 @@ type peerConnection struct {
|
|||||||
lock sync.RWMutex
|
lock sync.RWMutex
|
||||||
}
|
}
|
||||||
|
|
||||||
type Peer interface {
|
type LightPeer interface {
|
||||||
Head() (common.Hash, *big.Int)
|
Head() (common.Hash, *big.Int)
|
||||||
RequestHeadersByHash(common.Hash, int, int, bool) error
|
RequestHeadersByHash(common.Hash, int, int, bool) error
|
||||||
RequestHeadersByNumber(uint64, int, int, bool) error
|
RequestHeadersByNumber(uint64, int, int, bool) error
|
||||||
|
}
|
||||||
|
|
||||||
|
type Peer interface {
|
||||||
|
LightPeer
|
||||||
RequestBodies([]common.Hash) error
|
RequestBodies([]common.Hash) error
|
||||||
RequestReceipts([]common.Hash) error
|
RequestReceipts([]common.Hash) error
|
||||||
RequestNodeData([]common.Hash) error
|
RequestNodeData([]common.Hash) error
|
||||||
}
|
}
|
||||||
|
|
||||||
// newPeer create a new downloader peer, with specific hash and block retrieval
|
// lightPeerWrapper wraps a LightPeer struct, stubbing out the Peer-only methods.
|
||||||
// mechanisms.
|
type lightPeerWrapper struct {
|
||||||
func newPeerConnection(id string, version int, peer Peer, logger log.Logger) *peerConnection {
|
peer LightPeer
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *lightPeerWrapper) Head() (common.Hash, *big.Int) { return w.peer.Head() }
|
||||||
|
func (w *lightPeerWrapper) RequestHeadersByHash(h common.Hash, amount int, skip int, reverse bool) error {
|
||||||
|
return w.peer.RequestHeadersByHash(h, amount, skip, reverse)
|
||||||
|
}
|
||||||
|
func (w *lightPeerWrapper) RequestHeadersByNumber(i uint64, amount int, skip int, reverse bool) error {
|
||||||
|
return w.peer.RequestHeadersByNumber(i, amount, skip, reverse)
|
||||||
|
}
|
||||||
|
func (w *lightPeerWrapper) RequestBodies([]common.Hash) error {
|
||||||
|
panic("RequestBodies not supported in light client mode sync")
|
||||||
|
}
|
||||||
|
func (w *lightPeerWrapper) RequestReceipts([]common.Hash) error {
|
||||||
|
panic("RequestReceipts not supported in light client mode sync")
|
||||||
|
}
|
||||||
|
func (w *lightPeerWrapper) RequestNodeData([]common.Hash) error {
|
||||||
|
panic("RequestNodeData not supported in light client mode sync")
|
||||||
|
}
|
||||||
|
|
||||||
|
// newPeerConnection creates a new downloader peer
|
||||||
|
func newPeerConnection(id string, version int, peer Peer, logger log.Logger) *peerConnection {
|
||||||
return &peerConnection{
|
return &peerConnection{
|
||||||
id: id,
|
id: id,
|
||||||
lacking: make(map[common.Hash]struct{}),
|
lacking: make(map[common.Hash]struct{}),
|
||||||
|
@ -895,26 +895,13 @@ func (pc *peerConnection) RequestHeadersByNumber(origin uint64, amount int, skip
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (pc *peerConnection) RequestBodies(hashes []common.Hash) error {
|
|
||||||
panic("RequestBodies not supported in light client mode sync")
|
|
||||||
|
|
||||||
}
|
|
||||||
func (pc *peerConnection) RequestReceipts(hashes []common.Hash) error {
|
|
||||||
panic("RequestReceipts not supported in light client mode sync")
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
func (pc *peerConnection) RequestNodeData(hashes []common.Hash) error {
|
|
||||||
panic("RequestNodeData not supported in light client mode sync")
|
|
||||||
}
|
|
||||||
|
|
||||||
func (d *downloaderPeerNotify) registerPeer(p *peer) {
|
func (d *downloaderPeerNotify) registerPeer(p *peer) {
|
||||||
pm := (*ProtocolManager)(d)
|
pm := (*ProtocolManager)(d)
|
||||||
pc := &peerConnection{
|
pc := &peerConnection{
|
||||||
manager: pm,
|
manager: pm,
|
||||||
peer: p,
|
peer: p,
|
||||||
}
|
}
|
||||||
pm.downloader.RegisterPeer(p.id, ethVersion, pc)
|
pm.downloader.RegisterLightPeer(p.id, ethVersion, pc)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *downloaderPeerNotify) unregisterPeer(p *peer) {
|
func (d *downloaderPeerNotify) unregisterPeer(p *peer) {
|
||||||
|
@ -389,21 +389,6 @@ func (self *LightChain) CurrentHeader() *types.Header {
|
|||||||
return self.hc.CurrentHeader()
|
return self.hc.CurrentHeader()
|
||||||
}
|
}
|
||||||
|
|
||||||
// CurrentBlock exists for interface compatibility and always returns nil
|
|
||||||
func (self *LightChain) CurrentBlock() *types.Block {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// CurrentFastBlock exists for interface compatibility and always returns nil
|
|
||||||
func (self *LightChain) CurrentFastBlock() *types.Block {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// FastSyncCommitHead exists for interface compatibility and does nothing
|
|
||||||
func (self *LightChain) FastSyncCommitHead(h common.Hash) error {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetTd retrieves a block's total difficulty in the canonical chain from the
|
// GetTd retrieves a block's total difficulty in the canonical chain from the
|
||||||
// database by hash and number, caching it if found.
|
// database by hash and number, caching it if found.
|
||||||
func (self *LightChain) GetTd(hash common.Hash, number uint64) *big.Int {
|
func (self *LightChain) GetTd(hash common.Hash, number uint64) *big.Int {
|
||||||
|
Loading…
Reference in New Issue
Block a user