Skip to content

Commit 1f42c51

Browse files
authored
Merge pull request ethereum#288 from nextyio/consensus-sync
Resolve chain desync due to chain fork
2 parents 640ed82 + 3db12f4 commit 1f42c51

File tree

4 files changed

+22
-7
lines changed

4 files changed

+22
-7
lines changed

consensus/dccs/2_dccs.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,18 @@ func (c *Context) verifySeal2() error {
277277
return nil
278278
}
279279

280+
func (c *Context) getHeader(hash common.Hash, number uint64) *types.Header {
281+
header := c.getHeaderByNumber(number)
282+
if header != nil && header.Hash() == hash {
283+
return header
284+
}
285+
header = c.getHeaderByHash(hash)
286+
if header != nil && header.Number.Uint64() == number {
287+
return header
288+
}
289+
return nil
290+
}
291+
280292
// getHeaderByNumber returns either:
281293
// + the context head, if number == head.Number
282294
// + the header in parents if available (nessesary for batch headers processing)

consensus/dccs/2_queue.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,7 @@ func (c *Context) getSealingQueue(parentHash common.Hash) (*SealingQueue, error)
257257
}
258258

259259
var maxDiff uint64
260+
hash := parentHash
260261

261262
// scan backward atmost LeakDurations blocks from number
262263
for i := uint64(0); i < c.engine.config.LeakDuration; i++ {
@@ -275,9 +276,9 @@ func (c *Context) getSealingQueue(parentHash common.Hash) (*SealingQueue, error)
275276
// TODO: optimization for leakage case
276277

277278
n := number - i
278-
header := c.getHeaderByNumber(n)
279+
header := c.getHeader(hash, n)
279280
if header == nil {
280-
log.Error("getSealingQueue: getHeaderByNumber returns nil", "n", n, "len(parents)", len(c.parents))
281+
log.Error("Header not found", "number", n, "hash", hash, "len(parents)", len(c.parents))
281282
return nil, errUnknownBlock
282283
}
283284
sealer, err := c.ecrecover(header)
@@ -295,6 +296,9 @@ func (c *Context) getSealingQueue(parentHash common.Hash) (*SealingQueue, error)
295296
if i < minBlockToScan || len(recents) < int(maxDiff)/2 {
296297
addRecent(sealer)
297298
}
299+
300+
// next parent in the hash chain
301+
hash = header.ParentHash
298302
}
299303

300304
apps, err := c.crawlSealerApplications(parent)

core/blockchain.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1378,8 +1378,7 @@ func (bc *BlockChain) writeBlockWithState(block *types.Block, receipts []*types.
13781378

13791379
// ChainCompare compares the weight of remote chain to local chain.
13801380
func ChainCompare(remoteTD, localTD *big.Int, remoteHash, localHash common.Hash) int {
1381-
cmp := remoteTD.Cmp(localTD)
1382-
if cmp != 0 {
1381+
if cmp := remoteTD.Cmp(localTD); cmp != 0 {
13831382
return cmp
13841383
}
13851384
return bytes.Compare(localHash.Bytes(), remoteHash.Bytes())
@@ -1495,7 +1494,7 @@ func (bc *BlockChain) insertChain(chain types.Blocks, verifySeals bool) (int, []
14951494
)
14961495
for block != nil && err == ErrKnownBlock {
14971496
externTd = new(big.Int).Add(externTd, block.Difficulty())
1498-
if localTd.Cmp(externTd) < 0 {
1497+
if ChainCompare(localTd, externTd, current.Hash(), block.Hash()) < 0 {
14991498
break
15001499
}
15011500
log.Debug("Ignoring already known block", "number", block.Number(), "hash", block.Hash())
@@ -1787,7 +1786,7 @@ func (bc *BlockChain) insertSideChain(block *types.Block, it *insertIterator) (i
17871786
// If the externTd was larger than our local TD, we now need to reimport the previous
17881787
// blocks to regenerate the required state
17891788
localTd := bc.GetTd(current.Hash(), current.NumberU64())
1790-
if localTd.Cmp(externTd) > 0 {
1789+
if ChainCompare(localTd, externTd, current.Hash(), block.Hash()) > 0 {
17911790
log.Info("Sidechain written to disk", "start", it.first().NumberU64(), "end", it.previous().Number, "sidetd", externTd, "localtd", localTd)
17921791
return it.index, nil, nil, err
17931792
}

core/headerchain.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ func (hc *HeaderChain) InsertHeaderChain(chain []*types.Header, writeHeader WhCa
280280
if hc.HasHeader(hash, header.Number.Uint64()) {
281281
externTd := hc.GetTd(hash, header.Number.Uint64())
282282
localTd := hc.GetTd(hc.currentHeaderHash, hc.CurrentHeader().Number.Uint64())
283-
if externTd == nil || externTd.Cmp(localTd) <= 0 {
283+
if externTd == nil || ChainCompare(externTd, localTd, hash, hc.currentHeaderHash) <= 0 {
284284
stats.ignored++
285285
continue
286286
}

0 commit comments

Comments
 (0)