Skip to content
This repository has been archived by the owner on Dec 17, 2024. It is now read-only.

Commit

Permalink
[consensus] replace infoMutex with atomic bool
Browse files Browse the repository at this point in the history
Signed-off-by: Leo Chen <[email protected]>
  • Loading branch information
Leo Chen committed Jul 2, 2020
1 parent 68ab784 commit 70884eb
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 10 deletions.
2 changes: 1 addition & 1 deletion consensus/checks.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func (consensus *Consensus) validatorSanityChecks(msg *msg_pb.Message) bool {
return false
}
if !senderKey.IsEqual(consensus.LeaderPubKey) &&
consensus.current.Mode() == Normal && !consensus.ignoreViewIDCheck {
consensus.current.Mode() == Normal && !consensus.ignoreViewIDCheck.IsSet() {
consensus.getLogger().Warn().Msgf(
"[%s] SenderKey not match leader PubKey",
msg.GetType().String(),
Expand Down
6 changes: 3 additions & 3 deletions consensus/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/harmony-one/harmony/p2p"
"github.com/harmony-one/harmony/staking/slash"
"github.com/pkg/errors"
"github.com/tevino/abool"
)

const (
Expand Down Expand Up @@ -88,12 +89,10 @@ type Consensus struct {
// Shard Id which this node belongs to
ShardID uint32
// whether to ignore viewID check
ignoreViewIDCheck bool
ignoreViewIDCheck *abool.AtomicBool
// global consensus mutex
// TODO(optimization): Avoid mutex and use more efficient locking primitives
mutex sync.Mutex
// consensus information update mutex
infoMutex sync.Mutex
// Signal channel for starting a new consensus process
ReadySignal chan struct{}
// The post-consensus processing func passed from Node object
Expand Down Expand Up @@ -211,5 +210,6 @@ func New(
consensus.ReadySignal = make(chan struct{})
// channel for receiving newly generated VDF
consensus.RndChannel = make(chan [vdfAndSeedSize]byte)
consensus.ignoreViewIDCheck = abool.NewBool(false)
return &consensus, nil
}
14 changes: 8 additions & 6 deletions consensus/consensus_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,11 @@ func (consensus *Consensus) ResetState() {

// ToggleConsensusCheck flip the flag of whether ignore viewID check during consensus process
func (consensus *Consensus) ToggleConsensusCheck() {
consensus.infoMutex.Lock()
defer consensus.infoMutex.Unlock()
consensus.ignoreViewIDCheck = !consensus.ignoreViewIDCheck
if consensus.ignoreViewIDCheck.IsSet() {
consensus.ignoreViewIDCheck.UnSet()
} else {
consensus.ignoreViewIDCheck.Set()
}
}

// IsValidatorInCommittee returns whether the given validator BLS address is part of my committee
Expand Down Expand Up @@ -254,14 +256,14 @@ func (consensus *Consensus) RegisterRndChannel(rndChannel chan [548]byte) {
// Check viewID, caller's responsibility to hold lock when change ignoreViewIDCheck
func (consensus *Consensus) checkViewID(msg *FBFTMessage) error {
// just ignore consensus check for the first time when node join
if consensus.ignoreViewIDCheck {
if consensus.ignoreViewIDCheck.IsSet() {
//in syncing mode, node accepts incoming messages without viewID/leaderKey checking
//so only set mode to normal when new node enters consensus and need checking viewID
consensus.current.SetMode(Normal)
consensus.viewID = msg.ViewID
consensus.current.SetViewID(msg.ViewID)
consensus.LeaderPubKey = msg.SenderPubkey
consensus.ignoreViewIDCheck = false
consensus.ignoreViewIDCheck.UnSet()
consensus.consensusTimeout[timeoutConsensus].Start()
utils.Logger().Debug().
Uint64("viewID", consensus.viewID).
Expand Down Expand Up @@ -485,7 +487,7 @@ func (consensus *Consensus) UpdateConsensusInformation() Mode {
if err != nil || leaderPubKey == nil {
consensus.getLogger().Debug().Err(err).
Msg("[UpdateConsensusInformation] Unable to get leaderPubKey from coinbase")
consensus.ignoreViewIDCheck = true
consensus.ignoreViewIDCheck.Set()
hasError = true
} else {
consensus.getLogger().Debug().
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ require (
github.com/spf13/viper v1.6.1
github.com/stretchr/testify v1.5.1
github.com/syndtr/goleveldb v1.0.1-0.20190923125748-758128399b1d
github.com/tevino/abool v0.0.0-20170917061928-9b9efcf221b5
github.com/uber/jaeger-client-go v2.20.1+incompatible // indirect
github.com/uber/jaeger-lib v2.2.0+incompatible // indirect
golang.org/x/crypto v0.0.0-20200510223506-06a226fb4e37
Expand Down

0 comments on commit 70884eb

Please sign in to comment.