Skip to content

Commit 320e727

Browse files
committed
consensus/pbft: use RWMutex in snapshot
1 parent 471a729 commit 320e727

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

consensus/pbft/core/snapshot.go

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func newSnapshot(view *pbft.View, validatorSet pbft.ValidatorSet) *snapshot {
3333
Prepares: newMessageSet(validatorSet),
3434
Commits: newMessageSet(validatorSet),
3535
Checkpoints: newMessageSet(validatorSet),
36-
mu: new(sync.Mutex),
36+
mu: new(sync.RWMutex),
3737
}
3838
}
3939

@@ -45,12 +45,12 @@ type snapshot struct {
4545
Commits *messageSet
4646
Checkpoints *messageSet
4747

48-
mu *sync.Mutex
48+
mu *sync.RWMutex
4949
}
5050

5151
func (s *snapshot) Proposal() pbft.Proposal {
52-
s.mu.Lock()
53-
defer s.mu.Unlock()
52+
s.mu.RLock()
53+
defer s.mu.RUnlock()
5454

5555
if s.Preprepare != nil {
5656
return s.Preprepare.Proposal
@@ -67,8 +67,8 @@ func (s *snapshot) SetRound(r *big.Int) {
6767
}
6868

6969
func (s *snapshot) Round() *big.Int {
70-
s.mu.Lock()
71-
defer s.mu.Unlock()
70+
s.mu.RLock()
71+
defer s.mu.RUnlock()
7272

7373
return s.round
7474
}
@@ -81,8 +81,8 @@ func (s *snapshot) SetSequence(seq *big.Int) {
8181
}
8282

8383
func (s *snapshot) Sequence() *big.Int {
84-
s.mu.Lock()
85-
defer s.mu.Unlock()
84+
s.mu.RLock()
85+
defer s.mu.RUnlock()
8686

8787
return s.sequence
8888
}
@@ -109,7 +109,7 @@ func (s *snapshot) DecodeRLP(stream *rlp.Stream) error {
109109
s.Prepares = ss.Prepares
110110
s.Commits = ss.Commits
111111
s.Checkpoints = ss.Checkpoints
112-
s.mu = new(sync.Mutex)
112+
s.mu = new(sync.RWMutex)
113113

114114
return nil
115115
}
@@ -123,6 +123,9 @@ func (s *snapshot) DecodeRLP(stream *rlp.Stream) error {
123123
// recommended to write only a single value but writing multiple
124124
// values or no value at all is also permitted.
125125
func (s *snapshot) EncodeRLP(w io.Writer) error {
126+
s.mu.RLock()
127+
defer s.mu.RUnlock()
128+
126129
return rlp.Encode(w, []interface{}{
127130
s.round,
128131
s.sequence,

0 commit comments

Comments
 (0)