Removed additional locks and isViewChange.

pull/4377/head
frozen 2 years ago committed by Casey Gardiner
parent bfdd7652aa
commit 8d4876e12c
  1. 4
      consensus/checks.go
  2. 2
      consensus/consensus_service.go
  3. 10
      consensus/consensus_v2.go
  4. 51
      consensus/fbft_log.go
  5. 4
      consensus/validator.go
  6. 8
      consensus/view_change.go

@ -86,7 +86,7 @@ func (consensus *Consensus) onAnnounceSanityChecks(recvMsg *FBFTMessage) bool {
Str("recvMsg", recvMsg.String()).
Str("LeaderKey", consensus.LeaderPubKey.Bytes.Hex()).
Msg("[OnAnnounce] Leader is malicious")
if consensus.IsViewChangingMode() {
if consensus.isViewChangingMode() {
consensus.getLogger().Debug().Msg(
"[OnAnnounce] Already in ViewChanging mode, conflicing announce, doing noop",
)
@ -161,7 +161,7 @@ func (consensus *Consensus) onViewChangeSanityCheck(recvMsg *FBFTMessage) bool {
Msg("[onViewChangeSanityCheck] MsgBlockNum is different from my BlockNumber")
return false
}
if consensus.IsViewChangingMode() &&
if consensus.isViewChangingMode() &&
consensus.GetCurBlockViewID() > recvMsg.ViewID {
consensus.getLogger().Debug().Uint64("curBlockViewID", consensus.GetCurBlockViewID()).
Uint64("msgViewID", recvMsg.ViewID).

@ -97,7 +97,7 @@ func (consensus *Consensus) UpdatePublicKeys(pubKeys, allowlist []bls_cosi.Publi
consensus.ResetState()
// do not reset view change state if it is in view changing mode
if !consensus.IsViewChangingMode() {
if !consensus.isViewChangingMode() {
consensus.ResetViewChangeState()
}
return consensus.Decider.ParticipantsCount()

@ -47,6 +47,12 @@ const (
// IsViewChangingMode return true if curernt mode is viewchanging
func (consensus *Consensus) IsViewChangingMode() bool {
//consensus.mutex.RLock()
//defer consensus.mutex.RUnlock()
return consensus.isViewChangingMode()
}
func (consensus *Consensus) isViewChangingMode() bool {
return consensus.current.Mode() == ViewChanging
}
@ -56,7 +62,7 @@ func (consensus *Consensus) HandleMessageUpdate(ctx context.Context, msg *msg_pb
// in order to avoid possible trap forever but drop PREPARE and COMMIT
// which are message types specifically for a node acting as leader
// so we just ignore those messages
if consensus.IsViewChangingMode() &&
if consensus.isViewChangingMode() &&
(msg.Type == msg_pb.MessageType_PREPARE ||
msg.Type == msg_pb.MessageType_COMMIT) {
return nil
@ -784,7 +790,7 @@ func (consensus *Consensus) postCatchup(initBN uint64) {
consensus.switchPhase("TryCatchup", FBFTAnnounce)
}
// catch up and skip from view change trap
if initBN < consensus.BlockNum() && consensus.IsViewChangingMode() {
if initBN < consensus.BlockNum() && consensus.isViewChangingMode() {
consensus.current.SetMode(Normal)
consensus.consensusTimeout[timeoutViewChange].Stop()
}

@ -3,7 +3,6 @@ package consensus
import (
"encoding/binary"
"fmt"
"sync"
"github.com/ethereum/go-ethereum/common"
bls_core "github.com/harmony-one/bls/ffi/go/bls"
@ -102,10 +101,7 @@ func (m *FBFTMessage) id() fbftMsgID {
type FBFTLog struct {
blocks map[common.Hash]*types.Block // store blocks received in FBFT
verifiedBlocks map[common.Hash]struct{} // store block hashes for blocks that has already been verified
blockLock sync.RWMutex
messages map[fbftMsgID]*FBFTMessage // store messages received in FBFT
msgLock sync.RWMutex
messages map[fbftMsgID]*FBFTMessage // store messages received in FBFT
}
// NewFBFTLog returns new instance of FBFTLog
@ -120,42 +116,27 @@ func NewFBFTLog() *FBFTLog {
// AddBlock add a new block into the log
func (log *FBFTLog) AddBlock(block *types.Block) {
log.blockLock.Lock()
defer log.blockLock.Unlock()
log.blocks[block.Hash()] = block
}
// MarkBlockVerified marks the block as verified
func (log *FBFTLog) MarkBlockVerified(block *types.Block) {
log.blockLock.Lock()
defer log.blockLock.Unlock()
log.verifiedBlocks[block.Hash()] = struct{}{}
}
// IsBlockVerified checks whether the block is verified
func (log *FBFTLog) IsBlockVerified(hash common.Hash) bool {
log.blockLock.RLock()
defer log.blockLock.RUnlock()
_, exist := log.verifiedBlocks[hash]
return exist
}
// GetBlockByHash returns the block matches the given block hash
func (log *FBFTLog) GetBlockByHash(hash common.Hash) *types.Block {
log.blockLock.RLock()
defer log.blockLock.RUnlock()
return log.blocks[hash]
}
// GetBlocksByNumber returns the blocks match the given block number
func (log *FBFTLog) GetBlocksByNumber(number uint64) []*types.Block {
log.blockLock.RLock()
defer log.blockLock.RUnlock()
var blocks []*types.Block
for _, block := range log.blocks {
if block.NumberU64() == number {
@ -167,9 +148,6 @@ func (log *FBFTLog) GetBlocksByNumber(number uint64) []*types.Block {
// DeleteBlocksLessThan deletes blocks less than given block number
func (log *FBFTLog) DeleteBlocksLessThan(number uint64) {
log.blockLock.Lock()
defer log.blockLock.Unlock()
for h, block := range log.blocks {
if block.NumberU64() < number {
delete(log.blocks, h)
@ -180,9 +158,6 @@ func (log *FBFTLog) DeleteBlocksLessThan(number uint64) {
// DeleteBlockByNumber deletes block of specific number
func (log *FBFTLog) DeleteBlockByNumber(number uint64) {
log.blockLock.Lock()
defer log.blockLock.Unlock()
for h, block := range log.blocks {
if block.NumberU64() == number {
delete(log.blocks, h)
@ -193,9 +168,6 @@ func (log *FBFTLog) DeleteBlockByNumber(number uint64) {
// DeleteMessagesLessThan deletes messages less than given block number
func (log *FBFTLog) DeleteMessagesLessThan(number uint64) {
log.msgLock.Lock()
defer log.msgLock.Unlock()
for h, msg := range log.messages {
if msg.BlockNum < number {
delete(log.messages, h)
@ -205,9 +177,6 @@ func (log *FBFTLog) DeleteMessagesLessThan(number uint64) {
// AddVerifiedMessage adds a signature verified pbft message into the log
func (log *FBFTLog) AddVerifiedMessage(msg *FBFTMessage) {
log.msgLock.Lock()
defer log.msgLock.Unlock()
msg.Verified = true
log.messages[msg.id()] = msg
@ -215,9 +184,6 @@ func (log *FBFTLog) AddVerifiedMessage(msg *FBFTMessage) {
// AddNotVerifiedMessage adds a not signature verified pbft message into the log
func (log *FBFTLog) AddNotVerifiedMessage(msg *FBFTMessage) {
log.msgLock.Lock()
defer log.msgLock.Unlock()
msg.Verified = false
log.messages[msg.id()] = msg
@ -225,9 +191,6 @@ func (log *FBFTLog) AddNotVerifiedMessage(msg *FBFTMessage) {
// GetNotVerifiedCommittedMessages returns not verified committed pbft messages with matching blockNum, viewID and blockHash
func (log *FBFTLog) GetNotVerifiedCommittedMessages(blockNum uint64, viewID uint64, blockHash common.Hash) []*FBFTMessage {
log.msgLock.RLock()
defer log.msgLock.RUnlock()
var found []*FBFTMessage
for _, msg := range log.messages {
if msg.MessageType == msg_pb.MessageType_COMMITTED && msg.BlockNum == blockNum && msg.ViewID == viewID && msg.BlockHash == blockHash && !msg.Verified {
@ -239,9 +202,6 @@ func (log *FBFTLog) GetNotVerifiedCommittedMessages(blockNum uint64, viewID uint
// GetMessagesByTypeSeqViewHash returns pbft messages with matching type, blockNum, viewID and blockHash
func (log *FBFTLog) GetMessagesByTypeSeqViewHash(typ msg_pb.MessageType, blockNum uint64, viewID uint64, blockHash common.Hash) []*FBFTMessage {
log.msgLock.RLock()
defer log.msgLock.RUnlock()
var found []*FBFTMessage
for _, msg := range log.messages {
if msg.MessageType == typ && msg.BlockNum == blockNum && msg.ViewID == viewID && msg.BlockHash == blockHash && msg.Verified {
@ -253,9 +213,6 @@ func (log *FBFTLog) GetMessagesByTypeSeqViewHash(typ msg_pb.MessageType, blockNu
// GetMessagesByTypeSeq returns pbft messages with matching type, blockNum
func (log *FBFTLog) GetMessagesByTypeSeq(typ msg_pb.MessageType, blockNum uint64) []*FBFTMessage {
log.msgLock.RLock()
defer log.msgLock.RUnlock()
var found []*FBFTMessage
for _, msg := range log.messages {
if msg.MessageType == typ && msg.BlockNum == blockNum && msg.Verified {
@ -267,9 +224,6 @@ func (log *FBFTLog) GetMessagesByTypeSeq(typ msg_pb.MessageType, blockNum uint64
// GetMessagesByTypeSeqHash returns pbft messages with matching type, blockNum
func (log *FBFTLog) GetMessagesByTypeSeqHash(typ msg_pb.MessageType, blockNum uint64, blockHash common.Hash) []*FBFTMessage {
log.msgLock.RLock()
defer log.msgLock.RUnlock()
var found []*FBFTMessage
for _, msg := range log.messages {
if msg.MessageType == typ && msg.BlockNum == blockNum && msg.BlockHash == blockHash && msg.Verified {
@ -305,9 +259,6 @@ func (log *FBFTLog) HasMatchingViewPrepared(blockNum uint64, viewID uint64, bloc
// GetMessagesByTypeSeqView returns pbft messages with matching type, blockNum and viewID
func (log *FBFTLog) GetMessagesByTypeSeqView(typ msg_pb.MessageType, blockNum uint64, viewID uint64) []*FBFTMessage {
log.msgLock.RLock()
defer log.msgLock.RUnlock()
var found []*FBFTMessage
for _, msg := range log.messages {
if msg.MessageType != typ || msg.BlockNum != blockNum || msg.ViewID != viewID && msg.Verified {

@ -47,7 +47,7 @@ func (consensus *Consensus) onAnnounce(msg *msg_pb.Message) {
consensus.blockHash = recvMsg.BlockHash
// we have already added message and block, skip check viewID
// and send prepare message if is in ViewChanging mode
if consensus.IsViewChangingMode() {
if consensus.isViewChangingMode() {
consensus.getLogger().Debug().
Msg("[OnAnnounce] Still in ViewChanging Mode, Exiting !!")
return
@ -392,7 +392,7 @@ func (consensus *Consensus) onCommitted(recvMsg *FBFTMessage) {
return
}
if consensus.IsViewChangingMode() {
if consensus.isViewChangingMode() {
consensus.getLogger().Info().Msg("[OnCommitted] Still in ViewChanging mode, Exiting!!")
return
}

@ -237,8 +237,6 @@ func (consensus *Consensus) startViewChange() {
if consensus.disableViewChange || consensus.IsBackup() {
return
}
consensus.mutex.Lock()
defer consensus.mutex.Unlock()
consensus.consensusTimeout[timeoutConsensus].Stop()
consensus.consensusTimeout[timeoutBootstrap].Stop()
@ -300,7 +298,7 @@ func (consensus *Consensus) startViewChange() {
// startNewView stops the current view change
func (consensus *Consensus) startNewView(viewID uint64, newLeaderPriKey *bls.PrivateKeyWrapper, reset bool) error {
if !consensus.IsViewChangingMode() {
if !consensus.isViewChangingMode() {
return errors.New("not in view changing mode anymore")
}
@ -421,7 +419,7 @@ func (consensus *Consensus) onViewChange(recvMsg *FBFTMessage) {
}
// received enough view change messages, change state to normal consensus
if consensus.Decider.IsQuorumAchievedByMask(consensus.vc.GetViewIDBitmap(recvMsg.ViewID)) && consensus.IsViewChangingMode() {
if consensus.Decider.IsQuorumAchievedByMask(consensus.vc.GetViewIDBitmap(recvMsg.ViewID)) && consensus.isViewChangingMode() {
// no previous prepared message, go straight to normal mode
// and start proposing new block
if consensus.vc.IsM1PayloadEmpty() {
@ -537,7 +535,7 @@ func (consensus *Consensus) onNewView(recvMsg *FBFTMessage) {
}
}
if !consensus.IsViewChangingMode() {
if !consensus.isViewChangingMode() {
consensus.getLogger().Info().Msg("Not in ViewChanging Mode.")
return
}

Loading…
Cancel
Save