/
core.go
356 lines (306 loc) · 11 KB
/
core.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
// 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 (
"bytes"
"math"
"math/big"
"sync"
"time"
"github.com/simplechain-org/go-simplechain/common"
"github.com/simplechain-org/go-simplechain/common/prque"
"github.com/simplechain-org/go-simplechain/consensus/istanbul"
"github.com/simplechain-org/go-simplechain/core/types"
"github.com/simplechain-org/go-simplechain/event"
"github.com/simplechain-org/go-simplechain/log"
metrics "github.com/simplechain-org/go-simplechain/metrics"
)
// New creates an Istanbul consensus core
func New(backend istanbul.Backend, config *istanbul.Config) Engine {
r := metrics.NewRegistry()
c := &core{
config: config,
address: backend.Address(),
state: StateAcceptRequest,
handlerWg: new(sync.WaitGroup),
logger: log.New("address", backend.Address()),
backend: backend,
backlogs: make(map[common.Address]*prque.Prque),
backlogsMu: new(sync.Mutex),
pendingRequests: prque.New(nil),
pendingRequestsMu: new(sync.Mutex),
consensusTimestamp: time.Time{},
roundMeter: metrics.NewMeter(),
sequenceMeter: metrics.NewMeter(),
consensusTimer: metrics.NewTimer(),
}
r.Register("consensus/istanbul/core/round", c.roundMeter)
r.Register("consensus/istanbul/core/sequence", c.sequenceMeter)
r.Register("consensus/istanbul/core/consensus", c.consensusTimer)
c.validateFn = c.checkValidatorSignature
return c
}
// ----------------------------------------------------------------------------
type core struct {
config *istanbul.Config
address common.Address
state State
logger log.Logger
backend istanbul.Backend
events *event.TypeMuxSubscription
finalCommittedSub *event.TypeMuxSubscription
timeoutSub *event.TypeMuxSubscription
futurePreprepareTimer *time.Timer
valSet istanbul.ValidatorSet
waitingForRoundChange bool
validateFn func([]byte, []byte) (common.Address, error)
backlogs map[common.Address]*prque.Prque
backlogsMu *sync.Mutex
current *roundState
handlerWg *sync.WaitGroup
roundChangeSet *roundChangeSet
roundChangeTimer *time.Timer
pendingRequests *prque.Prque
pendingRequestsMu *sync.Mutex
consensusTimestamp time.Time
// the meter to record the round change rate
roundMeter metrics.Meter
// the meter to record the sequence update rate
sequenceMeter metrics.Meter
// the timer to record consensus duration (from accepting a preprepare to final committed stage)
consensusTimer metrics.Timer
}
func (c *core) finalizeMessage(msg *message) ([]byte, error) {
var err error
// Add sender address
msg.Address = c.Address()
// Add proof of consensus
msg.CommittedSeal = []byte{}
// Assign the CommittedSeal if it's a COMMIT message and proposal is not nil
if msg.Code == msgCommit && c.current.Proposal() != nil {
seal := PrepareCommittedSeal(c.current.Proposal().Hash())
msg.CommittedSeal, err = c.backend.Sign(seal)
if err != nil {
return nil, err
}
}
// Sign message
data, err := msg.PayloadNoSig()
if err != nil {
return nil, err
}
msg.Signature, err = c.backend.Sign(data)
if err != nil {
return nil, err
}
// Convert to payload
payload, err := msg.Payload()
if err != nil {
return nil, err
}
return payload, nil
}
func (c *core) broadcast(msg *message) {
logger := c.logger.New("state", c.state)
payload, err := c.finalizeMessage(msg)
if err != nil {
logger.Error("Failed to finalize message", "msg", msg, "err", err)
return
}
// Broadcast payload
if err = c.backend.Broadcast(c.valSet, payload); err != nil {
logger.Error("Failed to broadcast message", "msg", msg, "err", err)
return
}
}
func (c *core) currentView() *istanbul.View {
return &istanbul.View{
Sequence: new(big.Int).Set(c.current.Sequence()),
Round: new(big.Int).Set(c.current.Round()),
}
}
func (c *core) IsProposer() bool {
v := c.valSet
if v == nil {
return false
}
return v.IsProposer(c.backend.Address())
}
func (c *core) IsCurrentProposal(blockHash common.Hash) bool {
return c.current != nil && c.current.pendingRequest != nil && c.current.pendingRequest.Proposal.Hash() == blockHash
}
func (c *core) commit() {
c.setState(StateCommitted)
proposal := c.current.Proposal()
if proposal != nil {
committedSeals := make([][]byte, c.current.Commits.Size())
for i, v := range c.current.Commits.Values() {
committedSeals[i] = make([]byte, types.IstanbulExtraSeal)
copy(committedSeals[i][:], v.CommittedSeal[:])
}
if err := c.backend.Commit(proposal, committedSeals); err != nil {
c.current.UnlockHash() //Unlock block when insertion fails
c.sendNextRoundChange()
return
}
}
}
// startNewRound starts a new round. if round equals to 0, it means to starts a new sequence
func (c *core) startNewRound(round *big.Int) {
var logger log.Logger
if c.current == nil {
logger = c.logger.New("old_round", -1, "old_seq", 0)
} else {
logger = c.logger.New("old_round", c.current.Round(), "old_seq", c.current.Sequence())
}
roundChange := false
// Try to get last proposal
lastProposal, lastProposer := c.backend.LastProposal()
if c.current == nil {
logger.Trace("Start to the initial round")
} else if lastProposal.Number().Cmp(c.current.Sequence()) >= 0 {
diff := new(big.Int).Sub(lastProposal.Number(), c.current.Sequence())
c.sequenceMeter.Mark(new(big.Int).Add(diff, common.Big1).Int64())
if !c.consensusTimestamp.IsZero() {
c.consensusTimer.UpdateSince(c.consensusTimestamp)
c.consensusTimestamp = time.Time{}
}
logger.Trace("Catch up latest proposal", "number", lastProposal.Number().Uint64(), "hash", lastProposal.Hash())
} else if lastProposal.Number().Cmp(big.NewInt(c.current.Sequence().Int64()-1)) == 0 {
if round.Cmp(common.Big0) == 0 {
// same seq and round, don't need to start new round
return
} else if round.Cmp(c.current.Round()) < 0 {
logger.Warn("New round should not be smaller than current round", "seq", lastProposal.Number().Int64(), "new_round", round, "old_round", c.current.Round())
return
}
roundChange = true
} else {
logger.Warn("New sequence should be larger than current sequence", "new_seq", lastProposal.Number().Int64())
return
}
var newView *istanbul.View
if roundChange {
newView = &istanbul.View{
Sequence: new(big.Int).Set(c.current.Sequence()),
Round: new(big.Int).Set(round),
}
} else {
newView = &istanbul.View{
Sequence: new(big.Int).Add(lastProposal.Number(), common.Big1),
Round: new(big.Int),
}
c.valSet = c.backend.Validators(lastProposal)
}
// Update logger
logger = logger.New("old_proposer", c.valSet.GetProposer())
// Clear invalid ROUND CHANGE messages
c.roundChangeSet = newRoundChangeSet(c.valSet)
// New snapshot for new round
c.updateRoundState(newView, c.valSet, roundChange)
// Calculate new proposer
c.valSet.CalcProposer(lastProposer, newView.Round.Uint64())
c.waitingForRoundChange = false
c.setState(StateAcceptRequest)
if roundChange && c.IsProposer() && c.current != nil {
// If it is locked, propose the old proposal
// If we have pending request, propose pending request
if c.current.IsHashLocked() {
r := &istanbul.Request{
Proposal: c.current.Proposal(), //c.current.Proposal would be the locked proposal by previous proposer, see updateRoundState
}
c.sendPreprepare(r)
} else if c.current.pendingRequest != nil {
c.sendPreprepare(c.current.pendingRequest)
}
}
c.newRoundChangeTimer()
logger.Debug("New round", "new_round", newView.Round, "new_seq", newView.Sequence, "new_proposer", c.valSet.GetProposer(), "valSet", c.valSet.List(), "size", c.valSet.Size(), "IsProposer", c.IsProposer())
}
func (c *core) catchUpRound(view *istanbul.View) {
logger := c.logger.New("old_round", c.current.Round(), "old_seq", c.current.Sequence(), "old_proposer", c.valSet.GetProposer())
if view.Round.Cmp(c.current.Round()) > 0 {
c.roundMeter.Mark(new(big.Int).Sub(view.Round, c.current.Round()).Int64())
}
c.waitingForRoundChange = true
// Need to keep block locked for round catching up
c.updateRoundState(view, c.valSet, true)
c.roundChangeSet.Clear(view.Round)
c.newRoundChangeTimer()
logger.Trace("Catch up round", "new_round", view.Round, "new_seq", view.Sequence, "new_proposer", c.valSet)
}
// updateRoundState updates round state by checking if locking block is necessary
func (c *core) updateRoundState(view *istanbul.View, validatorSet istanbul.ValidatorSet, roundChange bool) {
// Lock only if both roundChange is true and it is locked
if roundChange && c.current != nil {
if c.current.IsHashLocked() {
c.current = newRoundState(view, validatorSet, c.current.GetLockedHash(), c.current.Preprepare, c.current.pendingRequest, c.backend.HasBadProposal)
} else {
c.current = newRoundState(view, validatorSet, common.Hash{}, nil, c.current.pendingRequest, c.backend.HasBadProposal)
}
} else {
c.current = newRoundState(view, validatorSet, common.Hash{}, nil, nil, c.backend.HasBadProposal)
}
}
func (c *core) setState(state State) {
if c.state != state {
c.state = state
}
if state == StateAcceptRequest {
c.processPendingRequests()
}
c.processBacklog()
}
func (c *core) Address() common.Address {
return c.address
}
func (c *core) stopFuturePreprepareTimer() {
if c.futurePreprepareTimer != nil {
c.futurePreprepareTimer.Stop()
}
}
func (c *core) stopTimer() {
c.stopFuturePreprepareTimer()
if c.roundChangeTimer != nil {
c.roundChangeTimer.Stop()
}
}
func (c *core) newRoundChangeTimer() {
c.stopTimer()
// set timeout based on the round number
timeout := time.Duration(c.config.RequestTimeout) * time.Millisecond
round := c.current.Round().Uint64()
if round > 0 {
timeout += time.Duration(math.Pow(2, float64(round))) * time.Second
}
c.roundChangeTimer = time.AfterFunc(timeout, func() {
c.sendEvent(timeoutEvent{})
})
}
func (c *core) checkValidatorSignature(data []byte, sig []byte) (common.Address, error) {
return istanbul.CheckValidatorSignature(c.valSet, data, sig)
}
func (c *core) Confirmations() int {
c.logger.Trace("Confirmation Formula used ceil(2N/3)")
return int(math.Ceil(float64(2*c.valSet.Size()) / 3))
}
// PrepareCommittedSeal returns a committed seal for the given hash
func PrepareCommittedSeal(hash common.Hash) []byte {
var buf bytes.Buffer
buf.Write(hash.Bytes())
buf.Write([]byte{byte(msgCommit)})
return buf.Bytes()
}