-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
roundstate.go
221 lines (183 loc) · 5.29 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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
// Copyright 2017 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
package core
import (
"io"
"math/big"
"sync"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/consensus/istanbul"
"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 *istanbul.View, validatorSet istanbul.ValidatorSet, lockedHash common.Hash, preprepare *istanbul.Preprepare, pendingRequest *istanbul.Request, hasBadProposal func(hash common.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 *istanbul.Preprepare
Prepares *messageSet
Commits *messageSet
lockedHash common.Hash
pendingRequest *istanbul.Request
mu *sync.RWMutex
hasBadProposal func(hash common.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() *istanbul.Subject {
s.mu.RLock()
defer s.mu.RUnlock()
if s.Preprepare == nil {
return nil
}
return &istanbul.Subject{
View: &istanbul.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 *istanbul.Preprepare) {
s.mu.Lock()
defer s.mu.Unlock()
s.Preprepare = preprepare
}
func (s *roundState) Proposal() istanbul.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 = common.Hash{}
}
func (s *roundState) IsHashLocked() bool {
s.mu.RLock()
defer s.mu.RUnlock()
if common.EmptyHash(s.lockedHash) {
return false
}
return !s.hasBadProposal(s.GetLockedHash())
}
func (s *roundState) GetLockedHash() common.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 *istanbul.Preprepare
Prepares *messageSet
Commits *messageSet
lockedHash common.Hash
pendingRequest *istanbul.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,
})
}