From 1fa5953deb6930eaa23a53c3cafba24f3010bd3d Mon Sep 17 00:00:00 2001 From: frozen <355847+Frozen@users.noreply.github.com> Date: Fri, 27 Jan 2023 19:14:09 -0300 Subject: [PATCH] Fix usage of private methods. --- consensus/checks.go | 5 +++-- consensus/consensus.go | 4 ++++ consensus/consensus_service.go | 13 +++++++++++-- consensus/consensus_v2.go | 5 +++-- consensus/debug.go | 14 ++++++++++++++ consensus/validator.go | 2 +- consensus/view_change.go | 12 ++++++------ consensus/view_change_msg.go | 10 +++++----- core/blockchain_impl.go | 15 +++++++++++++-- 9 files changed, 60 insertions(+), 20 deletions(-) diff --git a/consensus/checks.go b/consensus/checks.go index 9e095a45c..e4c9a7084 100644 --- a/consensus/checks.go +++ b/consensus/checks.go @@ -56,9 +56,10 @@ func (consensus *Consensus) senderKeySanityChecks(msg *msg_pb.Message, senderKey } func (consensus *Consensus) isRightBlockNumAndViewID(recvMsg *FBFTMessage) bool { - if recvMsg.ViewID != consensus.GetCurBlockViewID() || recvMsg.BlockNum != consensus.BlockNum() { + blockNum := consensus.getBlockNum() + if recvMsg.ViewID != consensus.getCurBlockViewID() || recvMsg.BlockNum != blockNum { consensus.getLogger().Debug(). - Uint64("blockNum", consensus.BlockNum()). + Uint64("blockNum", blockNum). Str("recvMsg", recvMsg.String()). Msg("BlockNum/viewID not match") return false diff --git a/consensus/consensus.go b/consensus/consensus.go index 080b39b33..2dbfa52a0 100644 --- a/consensus/consensus.go +++ b/consensus/consensus.go @@ -224,6 +224,10 @@ func (consensus *Consensus) BlockNum() uint64 { return atomic.LoadUint64(&consensus.blockNum) } +func (consensus *Consensus) getBlockNum() uint64 { + return atomic.LoadUint64(&consensus.blockNum) +} + // New create a new Consensus record func New( host p2p.Host, shard uint32, multiBLSPriKey multibls.PrivateKeys, diff --git a/consensus/consensus_service.go b/consensus/consensus_service.go index a92a71f7e..c43c7fe12 100644 --- a/consensus/consensus_service.go +++ b/consensus/consensus_service.go @@ -486,6 +486,8 @@ func (consensus *Consensus) isLeader() bool { // SetViewIDs set both current view ID and view changing ID to the height // of the blockchain. It is used during client startup to recover the state func (consensus *Consensus) SetViewIDs(height uint64) { + consensus.mutex.Lock() + defer consensus.mutex.Unlock() consensus.setViewIDs(height) } @@ -625,11 +627,18 @@ func (consensus *Consensus) NumSignaturesIncludedInBlock(block *types.Block) uin return count } +// GetLogger returns logger for consensus contexts added. +func (consensus *Consensus) GetLogger() *zerolog.Logger { + consensus.mutex.RLock() + defer consensus.mutex.RUnlock() + return consensus.getLogger() +} + // getLogger returns logger for consensus contexts added func (consensus *Consensus) getLogger() *zerolog.Logger { logger := utils.Logger().With(). - Uint64("myBlock", consensus.BlockNum()). - Uint64("myViewID", consensus.GetCurBlockViewID()). + Uint64("myBlock", consensus.blockNum). + Uint64("myViewID", consensus.getCurBlockViewID()). Str("phase", consensus.phase.String()). Str("mode", consensus.current.Mode().String()). Logger() diff --git a/consensus/consensus_v2.go b/consensus/consensus_v2.go index 44c3ebbd2..1ab33d0bb 100644 --- a/consensus/consensus_v2.go +++ b/consensus/consensus_v2.go @@ -46,6 +46,8 @@ const ( // IsViewChangingMode return true if curernt mode is viewchanging func (consensus *Consensus) IsViewChangingMode() bool { + consensus.mutex.RLock() + defer consensus.mutex.RUnlock() return consensus.isViewChangingMode() } @@ -413,8 +415,7 @@ func (consensus *Consensus) tick() { } func (consensus *Consensus) BlockChannel(newBlock *types.Block) { - //consensus.ReshardingNextLeader(newBlock) - consensus.getLogger().Info(). + consensus.GetLogger().Info(). Uint64("MsgBlockNum", newBlock.NumberU64()). Msg("[ConsensusMainLoop] Received Proposed New Block!") diff --git a/consensus/debug.go b/consensus/debug.go index ae1d2d92b..da3587710 100644 --- a/consensus/debug.go +++ b/consensus/debug.go @@ -12,10 +12,24 @@ func (consensus *Consensus) GetConsensusMode() string { // GetCurBlockViewID returns the current view ID of the consensus func (consensus *Consensus) GetCurBlockViewID() uint64 { + consensus.mutex.RLock() + defer consensus.mutex.RUnlock() + return consensus.getCurBlockViewID() +} + +// GetCurBlockViewID returns the current view ID of the consensus +func (consensus *Consensus) getCurBlockViewID() uint64 { return consensus.current.GetCurBlockViewID() } // GetViewChangingID returns the current view changing ID of the consensus func (consensus *Consensus) GetViewChangingID() uint64 { + consensus.mutex.RLock() + defer consensus.mutex.RUnlock() + return consensus.current.GetViewChangingID() +} + +// GetViewChangingID returns the current view changing ID of the consensus +func (consensus *Consensus) getViewChangingID() uint64 { return consensus.current.GetViewChangingID() } diff --git a/consensus/validator.go b/consensus/validator.go index 06aa9bc08..f85cb8e3d 100644 --- a/consensus/validator.go +++ b/consensus/validator.go @@ -277,7 +277,7 @@ func (consensus *Consensus) onPrepared(recvMsg *FBFTMessage) { if committedMsg != nil { consensus.onCommitted(committedMsg) } - if curBlockNum < consensus.BlockNum() { + if curBlockNum < consensus.getBlockNum() { consensus.getLogger().Info().Msg("[OnPrepared] Successfully caught up with committed message") break } diff --git a/consensus/view_change.go b/consensus/view_change.go index 3ab3d2c9d..13acb86ae 100644 --- a/consensus/view_change.go +++ b/consensus/view_change.go @@ -253,7 +253,7 @@ func (consensus *Consensus) startViewChange() { consensus.getLogger().Warn(). Uint64("nextViewID", nextViewID). - Uint64("viewChangingID", consensus.GetViewChangingID()). + Uint64("viewChangingID", consensus.getViewChangingID()). Dur("timeoutDuration", duration). Str("NextLeader", consensus.LeaderPubKey.Bytes.Hex()). Msg("[startViewChange]") @@ -270,7 +270,7 @@ func (consensus *Consensus) startViewChange() { if err := consensus.vc.InitPayload( consensus.FBFTLog, nextViewID, - consensus.BlockNum(), + consensus.getBlockNum(), consensus.priKey, members); err != nil { consensus.getLogger().Error().Err(err).Msg("[startViewChange] Init Payload Error") @@ -284,7 +284,7 @@ func (consensus *Consensus) startViewChange() { } msgToSend := consensus.constructViewChangeMessage(&key) if err := consensus.msgSender.SendWithRetry( - consensus.BlockNum(), + consensus.getBlockNum(), msg_pb.MessageType_VIEWCHANGE, []nodeconfig.GroupID{ nodeconfig.NewGroupIDByShardID(nodeconfig.ShardID(consensus.ShardID))}, @@ -310,7 +310,7 @@ func (consensus *Consensus) startNewView(viewID uint64, newLeaderPriKey *bls.Pri } if err := consensus.msgSender.SendWithRetry( - consensus.BlockNum(), + consensus.getBlockNum(), msg_pb.MessageType_NEWVIEW, []nodeconfig.GroupID{ nodeconfig.NewGroupIDByShardID(nodeconfig.ShardID(consensus.ShardID))}, @@ -450,10 +450,10 @@ func (consensus *Consensus) onNewView(recvMsg *FBFTMessage) { Msg("[onNewView] Received NewView Message") // change view and leaderKey to keep in sync with network - if consensus.BlockNum() != recvMsg.BlockNum { + if consensus.getBlockNum() != recvMsg.BlockNum { consensus.getLogger().Warn(). Uint64("MsgBlockNum", recvMsg.BlockNum). - Uint64("myBlockNum", consensus.BlockNum()). + Uint64("myBlockNum", consensus.getBlockNum()). Msg("[onNewView] Invalid block number") return } diff --git a/consensus/view_change_msg.go b/consensus/view_change_msg.go index 2433abdcb..c24145030 100644 --- a/consensus/view_change_msg.go +++ b/consensus/view_change_msg.go @@ -23,8 +23,8 @@ func (consensus *Consensus) constructViewChangeMessage(priKey *bls.PrivateKeyWra Type: msg_pb.MessageType_VIEWCHANGE, Request: &msg_pb.Message_Viewchange{ Viewchange: &msg_pb.ViewChangeRequest{ - ViewId: consensus.GetViewChangingID(), - BlockNum: consensus.BlockNum(), + ViewId: consensus.getViewChangingID(), + BlockNum: consensus.getBlockNum(), ShardId: consensus.ShardID, SenderPubkey: priKey.Pub.Bytes[:], LeaderPubkey: consensus.LeaderPubKey.Bytes[:], @@ -33,7 +33,7 @@ func (consensus *Consensus) constructViewChangeMessage(priKey *bls.PrivateKeyWra } preparedMsgs := consensus.FBFTLog.GetMessagesByTypeSeq( - msg_pb.MessageType_PREPARED, consensus.BlockNum(), + msg_pb.MessageType_PREPARED, consensus.getBlockNum(), ) preparedMsg := consensus.FBFTLog.FindMessageByMaxViewID(preparedMsgs) @@ -83,7 +83,7 @@ func (consensus *Consensus) constructViewChangeMessage(priKey *bls.PrivateKeyWra } viewIDBytes := make([]byte, 8) - binary.LittleEndian.PutUint64(viewIDBytes, consensus.GetViewChangingID()) + binary.LittleEndian.PutUint64(viewIDBytes, consensus.getViewChangingID()) sign1 := priKey.Pri.SignHash(viewIDBytes) if sign1 != nil { vcMsg.ViewidSig = sign1.Serialize() @@ -107,7 +107,7 @@ func (consensus *Consensus) constructNewViewMessage(viewID uint64, priKey *bls.P Request: &msg_pb.Message_Viewchange{ Viewchange: &msg_pb.ViewChangeRequest{ ViewId: viewID, - BlockNum: consensus.BlockNum(), + BlockNum: consensus.getBlockNum(), ShardId: consensus.ShardID, SenderPubkey: priKey.Pub.Bytes[:], }, diff --git a/core/blockchain_impl.go b/core/blockchain_impl.go index 783a571e8..8b0683bd6 100644 --- a/core/blockchain_impl.go +++ b/core/blockchain_impl.go @@ -327,8 +327,19 @@ func VerifyBlockCrossLinks(blockchain BlockChain, block *types.Block) error { // ReadCrossLink beacon chain usage. cl, err := blockchain.ReadCrossLink(crossLink.ShardID(), crossLink.BlockNum()) if err == nil && cl != nil { - // Add slash for exist same blocknum but different crosslink - return errAlreadyExist + utils.Logger().Err(errAlreadyExist). + Uint64("beacon-block-number", block.NumberU64()). + Interface("remote", crossLink). + Interface("local", cl). + Msg("[CrossLinkVerification]") + // TODO Add slash for exist same blocknum but different crosslink + return errors.Wrapf( + errAlreadyExist, + "[CrossLinkVerification] shard: %d block: %d on beacon block %d", + crossLink.ShardID(), + crossLink.BlockNum(), + block.NumberU64(), + ) } if err := VerifyCrossLink(blockchain, crossLink); err != nil { return errors.Wrapf(err, "cannot VerifyBlockCrossLinks")