forked from umbracle/minimal
-
Notifications
You must be signed in to change notification settings - Fork 4
/
roundstate.go
205 lines (168 loc) · 4.44 KB
/
roundstate.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package core
import (
"io"
"math/big"
"sync"
"github.com/0xPolygon/minimal/consensus/ibft"
"github.com/0xPolygon/minimal/types"
"github.com/ethereum/go-ethereum/rlp"
)
// newRoundState creates a new roundState instance with the given view and validatorSet
// lockedHash and preprepare are for round change when lock exists,
// we need to keep a reference of preprepare in order to propose locked proposal when there is a lock and itself is the proposer
func newRoundState(view *ibft.View, validatorSet ibft.ValidatorSet, lockedHash types.Hash, preprepare *ibft.Preprepare, pendingRequest *ibft.Request, hasBadProposal func(hash types.Hash) bool) *roundState {
return &roundState{
round: view.Round,
sequence: view.Sequence,
Preprepare: preprepare,
Prepares: newMessageSet(validatorSet),
Commits: newMessageSet(validatorSet),
lockedHash: lockedHash,
mu: new(sync.RWMutex),
pendingRequest: pendingRequest,
hasBadProposal: hasBadProposal,
}
}
// roundState stores the consensus state
type roundState struct {
round *big.Int
sequence *big.Int
Preprepare *ibft.Preprepare
Prepares *messageSet
Commits *messageSet
lockedHash types.Hash
pendingRequest *ibft.Request
mu *sync.RWMutex
hasBadProposal func(hash types.Hash) bool
}
func (s *roundState) GetPrepareOrCommitSize() int {
s.mu.RLock()
defer s.mu.RUnlock()
result := s.Prepares.Size() + s.Commits.Size()
// find duplicate one
for _, m := range s.Prepares.Values() {
if s.Commits.Get(m.Address) != nil {
result--
}
}
return result
}
func (s *roundState) Subject() *ibft.Subject {
s.mu.RLock()
defer s.mu.RUnlock()
if s.Preprepare == nil {
return nil
}
return &ibft.Subject{
View: &ibft.View{
Round: new(big.Int).Set(s.round),
Sequence: new(big.Int).Set(s.sequence),
},
Digest: s.Preprepare.Proposal.Hash(),
}
}
func (s *roundState) SetPreprepare(preprepare *ibft.Preprepare) {
s.mu.Lock()
defer s.mu.Unlock()
s.Preprepare = preprepare
}
func (s *roundState) Proposal() ibft.Proposal {
s.mu.RLock()
defer s.mu.RUnlock()
if s.Preprepare != nil {
return s.Preprepare.Proposal
}
return nil
}
func (s *roundState) SetRound(r *big.Int) {
s.mu.Lock()
defer s.mu.Unlock()
s.round = new(big.Int).Set(r)
}
func (s *roundState) Round() *big.Int {
s.mu.RLock()
defer s.mu.RUnlock()
return s.round
}
func (s *roundState) SetSequence(seq *big.Int) {
s.mu.Lock()
defer s.mu.Unlock()
s.sequence = seq
}
func (s *roundState) Sequence() *big.Int {
s.mu.RLock()
defer s.mu.RUnlock()
return s.sequence
}
func (s *roundState) LockHash() {
s.mu.Lock()
defer s.mu.Unlock()
if s.Preprepare != nil {
s.lockedHash = s.Preprepare.Proposal.Hash()
}
}
func (s *roundState) UnlockHash() {
s.mu.Lock()
defer s.mu.Unlock()
s.lockedHash = types.Hash{}
}
func (s *roundState) IsHashLocked() bool {
s.mu.RLock()
defer s.mu.RUnlock()
if types.EmptyHash(s.lockedHash) {
return false
}
return !s.hasBadProposal(s.GetLockedHash())
}
func (s *roundState) GetLockedHash() types.Hash {
s.mu.RLock()
defer s.mu.RUnlock()
return s.lockedHash
}
// The DecodeRLP method should read one value from the given
// Stream. It is not forbidden to read less or more, but it might
// be confusing.
func (s *roundState) DecodeRLP(stream *rlp.Stream) error {
var ss struct {
Round *big.Int
Sequence *big.Int
Preprepare *ibft.Preprepare
Prepares *messageSet
Commits *messageSet
lockedHash types.Hash
pendingRequest *ibft.Request
}
if err := stream.Decode(&ss); err != nil {
return err
}
s.round = ss.Round
s.sequence = ss.Sequence
s.Preprepare = ss.Preprepare
s.Prepares = ss.Prepares
s.Commits = ss.Commits
s.lockedHash = ss.lockedHash
s.pendingRequest = ss.pendingRequest
s.mu = new(sync.RWMutex)
return nil
}
// EncodeRLP should write the RLP encoding of its receiver to w.
// If the implementation is a pointer method, it may also be
// called for nil pointers.
//
// Implementations should generate valid RLP. The data written is
// not verified at the moment, but a future version might. It is
// recommended to write only a single value but writing multiple
// values or no value at all is also permitted.
func (s *roundState) EncodeRLP(w io.Writer) error {
s.mu.RLock()
defer s.mu.RUnlock()
return rlp.Encode(w, []interface{}{
s.round,
s.sequence,
s.Preprepare,
s.Prepares,
s.Commits,
s.lockedHash,
s.pendingRequest,
})
}