Skip to content

Commit 557d2ae

Browse files
authored
Merge pull request #4893 from harmony-one/feature/consensus-getlogger-without-locks
Removed locks from GetLogger method.
2 parents 61816f7 + 9b0c617 commit 557d2ae

File tree

8 files changed

+17
-21
lines changed

8 files changed

+17
-21
lines changed

consensus/consensus_service.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,6 @@ func signConsensusMessage(message *msg_pb.Message,
135135
// UpdateBitmaps update the bitmaps for prepare and commit phase
136136
func (consensus *Consensus) updateBitmaps() {
137137
consensus.getLogger().Debug().
138-
Str("MessageType", consensus.current.phase.String()).
139138
Msg("[UpdateBitmaps] Updating consensus bitmaps")
140139
members := consensus.decider.Participants()
141140
prepareBitmap := bls_cosi.NewMask(members)
@@ -685,8 +684,6 @@ func (consensus *Consensus) NumSignaturesIncludedInBlock(block *types.Block) uin
685684

686685
// GetLogger returns logger for consensus contexts added.
687686
func (consensus *Consensus) GetLogger() *zerolog.Logger {
688-
consensus.mutex.RLock()
689-
defer consensus.mutex.RUnlock()
690687
return consensus.getLogger()
691688
}
692689

@@ -696,7 +693,7 @@ func (consensus *Consensus) getLogger() *zerolog.Logger {
696693
Uint32("shardID", consensus.ShardID).
697694
Uint64("myBlock", consensus.current.getBlockNum()).
698695
Uint64("myViewID", consensus.current.getCurBlockViewID()).
699-
Str("phase", consensus.current.phase.String()).
696+
Str("phase", consensus.getConsensusPhase().String()).
700697
Str("mode", consensus.current.Mode().String()).
701698
Logger()
702699
return &logger

consensus/consensus_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func TestConsensusInitialization(t *testing.T) {
3737
// FBFTLog
3838
assert.NotNil(t, consensus.FBFTLog())
3939

40-
assert.Equal(t, FBFTAnnounce, consensus.current.phase)
40+
assert.Equal(t, FBFTAnnounce, consensus.getConsensusPhase())
4141

4242
// State / consensus.current
4343
assert.Equal(t, state.mode, consensus.current.mode)

consensus/debug.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
package consensus
22

33
// GetConsensusPhase returns the current phase of the consensus.
4-
func (consensus *Consensus) GetConsensusPhase() string {
5-
consensus.mutex.RLock()
6-
defer consensus.mutex.RUnlock()
4+
func (consensus *Consensus) GetConsensusPhase() FBFTPhase {
75
return consensus.getConsensusPhase()
86
}
97

108
// GetConsensusPhase returns the current phase of the consensus.
11-
func (consensus *Consensus) getConsensusPhase() string {
12-
return consensus.current.phase.String()
9+
func (consensus *Consensus) getConsensusPhase() FBFTPhase {
10+
return consensus.current.phase.Load().(FBFTPhase)
1311
}
1412

1513
// GetConsensusMode returns the current mode of the consensus

consensus/state.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,20 @@ type State struct {
3434
block []byte
3535

3636
// FBFT phase: Announce, Prepare, Commit
37-
phase FBFTPhase
37+
phase atomic.Value // FBFTPhase
3838

3939
// ShardID of the consensus
4040
ShardID uint32
4141
}
4242

4343
func NewState(mode Mode, shardID uint32) State {
44-
return State{
44+
state := State{
4545
mode: uint32(mode),
4646
ShardID: shardID,
47+
phase: atomic.Value{},
4748
}
49+
state.phase.Store(FBFTAnnounce)
50+
return state
4851
}
4952

5053
func (pm *State) getBlockNum() uint64 {
@@ -79,7 +82,7 @@ func (pm *State) getLogger() *zerolog.Logger {
7982
Uint32("shardID", pm.ShardID).
8083
Uint64("myBlock", pm.getBlockNum()).
8184
Uint64("myViewID", pm.GetCurBlockViewID()).
82-
Str("phase", pm.phase.String()).
85+
Str("phase", pm.phase.Load().(FBFTPhase).String()).
8386
Str("mode", pm.Mode().String()).
8487
Logger()
8588
return &logger
@@ -88,11 +91,11 @@ func (pm *State) getLogger() *zerolog.Logger {
8891
// switchPhase will switch FBFTPhase to desired phase.
8992
func (pm *State) switchPhase(subject string, desired FBFTPhase) {
9093
pm.getLogger().Info().
91-
Str("from:", pm.phase.String()).
94+
Str("from:", pm.phase.Load().(FBFTPhase).String()).
9295
Str("to:", desired.String()).
9396
Str("switchPhase:", subject)
9497

95-
pm.phase = desired
98+
pm.phase.Store(desired)
9699
}
97100

98101
// GetCurBlockViewID returns the current view ID of the consensus

consensus/threshold.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,6 @@ func (consensus *Consensus) didReachPrepareQuorum() error {
8686
consensus.msgSender.StopRetry(msg_pb.MessageType_COMMITTED)
8787

8888
consensus.getLogger().Debug().
89-
Str("From", consensus.current.phase.String()).
9089
Str("To", FBFTCommit.String()).
9190
Msg("[OnPrepare] Switching phase")
9291

consensus/view_change.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -560,7 +560,6 @@ func (consensus *Consensus) ResetViewChangeState() {
560560
// ResetViewChangeState resets the view change structure
561561
func (consensus *Consensus) resetViewChangeState() {
562562
consensus.getLogger().Info().
563-
Str("Phase", consensus.current.phase.String()).
564563
Msg("[ResetViewChangeState] Resetting view change state")
565564
consensus.current.SetMode(Normal)
566565
consensus.vc.Reset()

consensus/view_change_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func TestPhaseSwitching(t *testing.T) {
4242
_, _, consensus, _, err := GenerateConsensusForTesting()
4343
assert.NoError(t, err)
4444

45-
assert.Equal(t, FBFTAnnounce, consensus.current.phase) // It's a new consensus, we should be at the FBFTAnnounce phase.
45+
assert.Equal(t, FBFTAnnounce, consensus.getConsensusPhase()) // It's a new consensus, we should be at the FBFTAnnounce phase.
4646

4747
switches := []phaseSwitch{
4848
{start: FBFTAnnounce, end: FBFTPrepare},
@@ -72,10 +72,10 @@ func TestPhaseSwitching(t *testing.T) {
7272
func testPhaseGroupSwitching(t *testing.T, consensus *Consensus, phases []FBFTPhase, startPhase FBFTPhase, desiredPhase FBFTPhase) {
7373
for range phases {
7474
consensus.switchPhase("test", desiredPhase)
75-
assert.Equal(t, desiredPhase, consensus.current.phase)
75+
assert.Equal(t, desiredPhase, consensus.getConsensusPhase())
7676
}
7777

78-
assert.Equal(t, desiredPhase, consensus.current.phase)
78+
assert.Equal(t, desiredPhase, consensus.getConsensusPhase())
7979

8080
return
8181
}

node/harmony/api.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ func (node *Node) GetConsensusMode() string {
112112

113113
// GetConsensusPhase returns the current consensus phase
114114
func (node *Node) GetConsensusPhase() string {
115-
return node.Consensus.GetConsensusPhase()
115+
return node.Consensus.GetConsensusPhase().String()
116116
}
117117

118118
// GetConsensusViewChangingID returns the view changing ID

0 commit comments

Comments
 (0)