Skip to content

Commit

Permalink
Ensure the consistency of wal message and chainState
Browse files Browse the repository at this point in the history
  • Loading branch information
niuxiaojie81 committed Mar 5, 2024
1 parent 69147c3 commit 278396b
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions consensus/cbft/wal_bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ func (cbft *Cbft) trySwitchValidator(blockNumber uint64) {
if err := cbft.validatorPool.Update(blockNumber, cbft.state.Epoch()+1, cbft.eventMux); err != nil {
cbft.log.Debug("Update validator error", "err", err.Error())
}
cbft.log.Trace("Update validator success", "blockNumber", blockNumber, "epoch", cbft.state.Epoch()+1)
cbft.log.Trace("Update validator success", "blockNumber", blockNumber)
}
}

Expand Down Expand Up @@ -404,24 +404,33 @@ func (cbft *Cbft) recoveryMsg(msg interface{}) error {

isContiguous, parentBlock := cbft.contiguousQCBlock(m.Block)
if !isContiguous {
cbft.log.Warn("ConfirmedViewChange msg status and chainState are not continuous", "confirmedViewChange", m.String(), "highestQCBn", cbft.state.HighestQCBlock().NumberU64(), "highestQCHash", cbft.state.HighestQCBlock().Hash())
// 如果状态不连续,则丢弃该消息及后面所有的消息
if m.Block.NumberU64() > cbft.state.HighestQCBlock().NumberU64() {
cbft.log.Warn("ConfirmedViewChange msg status and chainState are not continuous", "confirmedViewChange", m.String(),
"highestQCBn", cbft.state.HighestQCBlock().NumberU64(), "highestQCHash", cbft.state.HighestQCBlock().Hash())
}
// 如果状态不连续(ConfirmedViewChange消息落后于ChainState状态是正常的),则丢弃该消息
return nil
}

// 如果 wal 消息的状态和 ChainState 状态是连续的,且当前消息区块领先
if parentBlock != nil {
// 执行 wal 消息中的区块
if err := cbft.executeBlock(m.Block, parentBlock, math.MaxUint32); err != nil {
cbft.log.Error("Failed to execute block on recovery confirmedViewChange msg", "confirmedViewChange", m.String(), "parentBlockNumber", parentBlock.NumberU64(), "parentBlockHash", parentBlock.Hash())
cbft.log.Error("Failed to execute block on recovery confirmedViewChange msg", "confirmedViewChange", m.String(),
"parentBlockNumber", parentBlock.NumberU64(), "parentBlockHash", parentBlock.Hash())
return nil
}
// 更新 ChainState 状态
lockBlock, lockQC := cbft.blockTree.FindBlockAndQC(cbft.state.HighestLockBlock().Hash(), cbft.state.HighestLockBlock().NumberU64())
qcBlock, qc := cbft.blockTree.FindBlockAndQC(parentBlock.Hash(), parentBlock.NumberU64())
cbft.recoveryChainStateProcess(protocols.CommitState, &protocols.State{Block: lockBlock, QuorumCert: lockQC})
cbft.recoveryChainStateProcess(protocols.LockState, &protocols.State{Block: qcBlock, QuorumCert: qc})
cbft.recoveryChainStateProcess(protocols.QCState, &protocols.State{Block: m.Block, QuorumCert: m.QC})

commitState := &protocols.State{Block: lockBlock, QuorumCert: lockQC}
lockState := &protocols.State{Block: qcBlock, QuorumCert: qc}
qcState := &protocols.State{Block: m.Block, QuorumCert: m.QC}
cbft.recoveryChainStateProcess(protocols.CommitState, commitState)
cbft.recoveryChainStateProcess(protocols.LockState, lockState)
cbft.recoveryChainStateProcess(protocols.QCState, qcState)
cbft.bridge.UpdateChainState(qcState, lockState, commitState)
// 将新的 commit 区块写入 blockchain
extra, _ := ctypes.EncodeExtra(byte(cbftVersion), lockQC)
lockBlock.SetExtraData(extra)
Expand Down

0 comments on commit 278396b

Please sign in to comment.