core/blockchain, downloader: abort early on sidechain import with bad blocks

This commit is contained in:
Martin Holst Swende 2018-10-17 19:47:10 +02:00
parent 4466c7b971
commit dba64ca903
No known key found for this signature in database
GPG key ID: 683B438C05A5DDF0
2 changed files with 10 additions and 2 deletions

View file

@ -1144,12 +1144,17 @@ func (bc *BlockChain) insertChain(chain types.Blocks) (int, []interface{}, []*ty
for !bc.HasState(parent.Root()) { for !bc.HasState(parent.Root()) {
winner = append(winner, parent) winner = append(winner, parent)
parent = bc.GetBlock(parent.ParentHash(), parent.NumberU64()-1) parent = bc.GetBlock(parent.ParentHash(), parent.NumberU64()-1)
if bc.isBadHash(parent.Hash()) {
log.Info("Found bad hash in competing sidechain, aborting import", "hash", parent.Hash(), "number", parent.Number())
return i, events, coalescedLogs, ErrBlacklistedHash
}
} }
for j := 0; j < len(winner)/2; j++ { for j := 0; j < len(winner)/2; j++ {
winner[j], winner[len(winner)-1-j] = winner[len(winner)-1-j], winner[j] winner[j], winner[len(winner)-1-j] = winner[len(winner)-1-j], winner[j]
} }
// Import all the pruned blocks to make the state available // Import all the pruned blocks to make the state available
bc.chainmu.Unlock() bc.chainmu.Unlock()
log.Info("Importing competing sidechain", "len", len(winner), "from", winner[0].Number(), "to", winner[len(winner)-1].Number())
_, evs, logs, err := bc.insertChain(winner) _, evs, logs, err := bc.insertChain(winner)
bc.chainmu.Lock() bc.chainmu.Lock()
events, coalescedLogs = evs, logs events, coalescedLogs = evs, logs
@ -1442,6 +1447,9 @@ func (bc *BlockChain) BadBlocks() []*types.Block {
} }
return blocks return blocks
} }
func (bc *BlockChain) isBadHash(hash common.Hash) bool {
return bc.badBlocks.Contains(hash)
}
// addBadBlock adds a bad block to the bad-block LRU cache // addBadBlock adds a bad block to the bad-block LRU cache
func (bc *BlockChain) addBadBlock(block *types.Block) { func (bc *BlockChain) addBadBlock(block *types.Block) {

View file

@ -1400,7 +1400,7 @@ func (d *Downloader) importBlockResults(results []*fetchResult) error {
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.blockchain.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(), "blocks", len(blocks), "err", err)
return errInvalidChain return errInvalidChain
} }
return nil return nil
@ -1621,7 +1621,7 @@ func (d *Downloader) qosTuner() {
atomic.StoreUint64(&d.rttConfidence, conf) atomic.StoreUint64(&d.rttConfidence, conf)
// Log the new QoS values and sleep until the next RTT // Log the new QoS values and sleep until the next RTT
log.Debug("Recalculated downloader QoS values", "rtt", rtt, "confidence", float64(conf)/1000000.0, "ttl", d.requestTTL()) //log.Debug("Recalculated downloader QoS values", "rtt", rtt, "confidence", float64(conf)/1000000.0, "ttl", d.requestTTL())
select { select {
case <-d.quitCh: case <-d.quitCh:
return return