-
Notifications
You must be signed in to change notification settings - Fork 54
/
consensus.go
294 lines (248 loc) · 7.49 KB
/
consensus.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
package consensus
import (
"fmt"
"sync"
"github.com/relab/hotstuff"
"github.com/relab/hotstuff/eventloop"
"github.com/relab/hotstuff/logging"
"github.com/relab/hotstuff/modules"
"github.com/relab/hotstuff/synchronizer"
)
// Rules is the minimum interface that a consensus implementations must implement.
// Implementations of this interface can be wrapped in the ConsensusBase struct.
// Together, these provide an implementation of the main Consensus interface.
// Implementors do not need to verify certificates or interact with other modules,
// as this is handled by the ConsensusBase struct.
type Rules interface {
// VoteRule decides whether to vote for the block.
VoteRule(proposal hotstuff.ProposeMsg) bool
// CommitRule decides whether any ancestor of the block can be committed.
// Returns the youngest ancestor of the block that can be committed.
CommitRule(*hotstuff.Block) *hotstuff.Block
// ChainLength returns the number of blocks that need to be chained together in order to commit.
ChainLength() int
}
// ProposeRuler is an optional interface that adds a ProposeRule method.
// This allows implementors to specify how new blocks are created.
type ProposeRuler interface {
// ProposeRule creates a new proposal.
ProposeRule(cert hotstuff.SyncInfo, cmd hotstuff.Command) (proposal hotstuff.ProposeMsg, ok bool)
}
// consensusBase provides a default implementation of the Consensus interface
// for implementations of the ConsensusImpl interface.
type consensusBase struct {
impl Rules
acceptor modules.Acceptor
blockChain modules.BlockChain
commandQueue modules.CommandQueue
configuration modules.Configuration
crypto modules.Crypto
eventLoop *eventloop.EventLoop
executor modules.ExecutorExt
forkHandler modules.ForkHandlerExt
leaderRotation modules.LeaderRotation
logger logging.Logger
opts *modules.Options
synchronizer modules.Synchronizer
handel modules.Handel
lastVote hotstuff.View
mut sync.Mutex
bExec *hotstuff.Block
}
// New returns a new Consensus instance based on the given Rules implementation.
func New(impl Rules) modules.Consensus {
return &consensusBase{
impl: impl,
lastVote: 0,
bExec: hotstuff.GetGenesis(),
}
}
// InitModule initializes the module.
func (cs *consensusBase) InitModule(mods *modules.Core) {
mods.Get(
&cs.acceptor,
&cs.blockChain,
&cs.commandQueue,
&cs.configuration,
&cs.crypto,
&cs.eventLoop,
&cs.executor,
&cs.forkHandler,
&cs.leaderRotation,
&cs.logger,
&cs.opts,
&cs.synchronizer,
)
mods.TryGet(&cs.handel)
if mod, ok := cs.impl.(modules.Module); ok {
mod.InitModule(mods)
}
cs.eventLoop.RegisterHandler(hotstuff.ProposeMsg{}, func(event any) {
cs.OnPropose(event.(hotstuff.ProposeMsg))
})
}
func (cs *consensusBase) CommittedBlock() *hotstuff.Block {
cs.mut.Lock()
defer cs.mut.Unlock()
return cs.bExec
}
// StopVoting ensures that no voting happens in a view earlier than `view`.
func (cs *consensusBase) StopVoting(view hotstuff.View) {
if cs.lastVote < view {
cs.lastVote = view
}
}
// Propose creates a new proposal.
func (cs *consensusBase) Propose(cert hotstuff.SyncInfo) {
cs.logger.Debug("Propose")
qc, ok := cert.QC()
if ok {
// tell the acceptor that the previous proposal succeeded.
if qcBlock, ok := cs.blockChain.Get(qc.BlockHash()); ok {
cs.acceptor.Proposed(qcBlock.Command())
} else {
cs.logger.Errorf("Could not find block for QC: %s", qc)
}
}
ctx, cancel := synchronizer.TimeoutContext(cs.eventLoop.Context(), cs.eventLoop)
defer cancel()
cmd, ok := cs.commandQueue.Get(ctx)
if !ok {
cs.logger.Debug("Propose: No command")
return
}
var proposal hotstuff.ProposeMsg
if proposer, ok := cs.impl.(ProposeRuler); ok {
proposal, ok = proposer.ProposeRule(cert, cmd)
if !ok {
cs.logger.Debug("Propose: No block")
return
}
} else {
proposal = hotstuff.ProposeMsg{
ID: cs.opts.ID(),
Block: hotstuff.NewBlock(
qc.BlockHash(),
qc,
cmd,
cs.synchronizer.View(),
cs.opts.ID(),
),
}
if aggQC, ok := cert.AggQC(); ok && cs.opts.ShouldUseAggQC() {
proposal.AggregateQC = &aggQC
}
}
cs.blockChain.Store(proposal.Block)
cs.configuration.Propose(proposal)
// self vote
cs.OnPropose(proposal)
}
func (cs *consensusBase) OnPropose(proposal hotstuff.ProposeMsg) { //nolint:gocyclo
// TODO: extract parts of this method into helper functions maybe?
cs.logger.Debugf("OnPropose: %v", proposal.Block)
block := proposal.Block
if cs.opts.ShouldUseAggQC() && proposal.AggregateQC != nil {
highQC, ok := cs.crypto.VerifyAggregateQC(*proposal.AggregateQC)
if !ok {
cs.logger.Warn("OnPropose: failed to verify aggregate QC")
return
}
// NOTE: for simplicity, we require that the highQC found in the AggregateQC equals the QC embedded in the block.
if !block.QuorumCert().Equals(highQC) {
cs.logger.Warn("OnPropose: block QC does not equal highQC")
return
}
}
if !cs.crypto.VerifyQuorumCert(block.QuorumCert()) {
cs.logger.Info("OnPropose: invalid QC")
return
}
// ensure the block came from the leader.
if proposal.ID != cs.leaderRotation.GetLeader(block.View()) {
cs.logger.Info("OnPropose: block was not proposed by the expected leader")
return
}
if !cs.impl.VoteRule(proposal) {
cs.logger.Info("OnPropose: Block not voted for")
return
}
if qcBlock, ok := cs.blockChain.Get(block.QuorumCert().BlockHash()); ok {
cs.acceptor.Proposed(qcBlock.Command())
} else {
cs.logger.Info("OnPropose: Failed to fetch qcBlock")
}
if !cs.acceptor.Accept(block.Command()) {
cs.logger.Info("OnPropose: command not accepted")
return
}
// block is safe and was accepted
cs.blockChain.Store(block)
if b := cs.impl.CommitRule(block); b != nil {
cs.commit(b)
}
cs.synchronizer.AdvanceView(hotstuff.NewSyncInfo().WithQC(block.QuorumCert()))
if block.View() <= cs.lastVote {
cs.logger.Info("OnPropose: block view too old")
return
}
pc, err := cs.crypto.CreatePartialCert(block)
if err != nil {
cs.logger.Error("OnPropose: failed to sign block: ", err)
return
}
cs.lastVote = block.View()
if cs.handel != nil {
// let Handel handle the voting
cs.handel.Begin(pc)
return
}
leaderID := cs.leaderRotation.GetLeader(cs.lastVote + 1)
if leaderID == cs.opts.ID() {
cs.eventLoop.AddEvent(hotstuff.VoteMsg{ID: cs.opts.ID(), PartialCert: pc})
return
}
leader, ok := cs.configuration.Replica(leaderID)
if !ok {
cs.logger.Warnf("Replica with ID %d was not found!", leaderID)
return
}
leader.Vote(pc)
}
func (cs *consensusBase) commit(block *hotstuff.Block) {
cs.mut.Lock()
// can't recurse due to requiring the mutex, so we use a helper instead.
err := cs.commitInner(block)
cs.mut.Unlock()
if err != nil {
cs.logger.Warnf("failed to commit: %v", err)
return
}
// prune the blockchain and handle forked blocks
forkedBlocks := cs.blockChain.PruneToHeight(block.View())
for _, block := range forkedBlocks {
cs.forkHandler.Fork(block)
}
}
// recursive helper for commit
func (cs *consensusBase) commitInner(block *hotstuff.Block) error {
if cs.bExec.View() >= block.View() {
return nil
}
if parent, ok := cs.blockChain.Get(block.Parent()); ok {
err := cs.commitInner(parent)
if err != nil {
return err
}
} else {
return fmt.Errorf("failed to locate block: %s", block.Parent())
}
cs.logger.Debug("EXEC: ", block)
cs.executor.Exec(block)
cs.bExec = block
return nil
}
// ChainLength returns the number of blocks that need to be chained together in order to commit.
func (cs *consensusBase) ChainLength() int {
return cs.impl.ChainLength()
}