|
|
|
@ -11,8 +11,6 @@ import ( |
|
|
|
|
"sync" |
|
|
|
|
"time" |
|
|
|
|
|
|
|
|
|
"github.com/harmony-one/harmony/internal/chain" |
|
|
|
|
|
|
|
|
|
"github.com/Workiva/go-datastructures/queue" |
|
|
|
|
"github.com/ethereum/go-ethereum/common" |
|
|
|
|
"github.com/ethereum/go-ethereum/rlp" |
|
|
|
@ -22,6 +20,7 @@ import ( |
|
|
|
|
"github.com/harmony-one/harmony/consensus/engine" |
|
|
|
|
"github.com/harmony-one/harmony/core" |
|
|
|
|
"github.com/harmony-one/harmony/core/types" |
|
|
|
|
"github.com/harmony-one/harmony/internal/chain" |
|
|
|
|
"github.com/harmony-one/harmony/internal/utils" |
|
|
|
|
"github.com/harmony-one/harmony/node/worker" |
|
|
|
|
"github.com/harmony-one/harmony/p2p" |
|
|
|
@ -758,6 +757,40 @@ func (ss *StateSync) getBlockFromOldBlocksByParentHash(parentHash common.Hash) * |
|
|
|
|
return nil |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (ss *StateSync) getCommonBlockIter(parentHash common.Hash) *commonBlockIter { |
|
|
|
|
return newCommonBlockIter(ss.commonBlocks, parentHash) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
type commonBlockIter struct { |
|
|
|
|
parentToChild map[common.Hash]*types.Block |
|
|
|
|
curParentHash common.Hash |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func newCommonBlockIter(blocks map[int]*types.Block, startHash common.Hash) *commonBlockIter { |
|
|
|
|
m := make(map[common.Hash]*types.Block) |
|
|
|
|
for _, block := range blocks { |
|
|
|
|
m[block.ParentHash()] = block |
|
|
|
|
} |
|
|
|
|
return &commonBlockIter{ |
|
|
|
|
parentToChild: m, |
|
|
|
|
curParentHash: startHash, |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (iter *commonBlockIter) Next() *types.Block { |
|
|
|
|
curBlock, ok := iter.parentToChild[iter.curParentHash] |
|
|
|
|
if !ok || curBlock == nil { |
|
|
|
|
return nil |
|
|
|
|
} |
|
|
|
|
iter.curParentHash = curBlock.Hash() |
|
|
|
|
return curBlock |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (iter *commonBlockIter) HasNext() bool { |
|
|
|
|
_, ok := iter.parentToChild[iter.curParentHash] |
|
|
|
|
return ok |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (ss *StateSync) getBlockFromLastMileBlocksByParentHash(parentHash common.Hash) *types.Block { |
|
|
|
|
for _, block := range ss.lastMileBlocks { |
|
|
|
|
ph := block.ParentHash() |
|
|
|
@ -848,20 +881,21 @@ func (ss *StateSync) generateNewState(bc *core.BlockChain, worker *worker.Worker |
|
|
|
|
parentHash := bc.CurrentBlock().Hash() |
|
|
|
|
|
|
|
|
|
var err error |
|
|
|
|
for block := ss.getBlockFromOldBlocksByParentHash(parentHash); ; { |
|
|
|
|
|
|
|
|
|
commonIter := ss.getCommonBlockIter(parentHash) |
|
|
|
|
for { |
|
|
|
|
block := commonIter.Next() |
|
|
|
|
if block == nil { |
|
|
|
|
break |
|
|
|
|
} |
|
|
|
|
next := ss.getBlockFromOldBlocksByParentHash(block.Hash()) |
|
|
|
|
// Enforce sig check for the last block in a batch
|
|
|
|
|
enforceSigCheck := next == nil |
|
|
|
|
|
|
|
|
|
enforceSigCheck := !commonIter.HasNext() |
|
|
|
|
err = ss.UpdateBlockAndStatus(block, bc, worker, enforceSigCheck) |
|
|
|
|
if err != nil { |
|
|
|
|
break |
|
|
|
|
} |
|
|
|
|
block = next |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
ss.syncMux.Lock() |
|
|
|
|
ss.commonBlocks = make(map[int]*types.Block) |
|
|
|
|
ss.syncMux.Unlock() |
|
|
|
@ -873,7 +907,7 @@ func (ss *StateSync) generateNewState(bc *core.BlockChain, worker *worker.Worker |
|
|
|
|
if block == nil { |
|
|
|
|
break |
|
|
|
|
} |
|
|
|
|
err = ss.UpdateBlockAndStatus(block, bc, worker, true) |
|
|
|
|
err = ss.UpdateBlockAndStatus(block, bc, worker, false) |
|
|
|
|
if err != nil { |
|
|
|
|
break |
|
|
|
|
} |
|
|
|
@ -894,7 +928,7 @@ func (ss *StateSync) generateNewState(bc *core.BlockChain, worker *worker.Worker |
|
|
|
|
if block == nil { |
|
|
|
|
break |
|
|
|
|
} |
|
|
|
|
err = ss.UpdateBlockAndStatus(block, bc, worker, true) |
|
|
|
|
err = ss.UpdateBlockAndStatus(block, bc, worker, false) |
|
|
|
|
if err != nil { |
|
|
|
|
break |
|
|
|
|
} |
|
|
|
|