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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
|
#ifndef _HOTSTUFF_LIVENESS_H
#define _HOTSTUFF_LIVENESS_H
#include "salticidae/util.h"
#include "hotstuff/consensus.h"
namespace hotstuff {
using salticidae::_1;
using salticidae::_2;
/** Abstraction for liveness gadget (oracle). */
class PaceMaker {
protected:
HotStuffCore *hsc;
public:
virtual ~PaceMaker() = default;
/** Initialize the PaceMaker. A derived class should also call the
* default implementation to set `hsc`. */
virtual void init(HotStuffCore *_hsc) { hsc = _hsc; }
/** Get a promise resolved when the pace maker thinks it is a *good* time
* to issue new commands. When promise is resolved, the replica should
* propose the command. */
virtual promise_t beat() = 0;
/** Get the current proposer. */
virtual ReplicaID get_proposer() = 0;
/** Select the parent blocks for a new block.
* @return Parent blocks. The block at index 0 is the direct parent, while
* the others are uncles/aunts. The returned vector should be non-empty. */
virtual std::vector<block_t> get_parents() = 0;
/** Get a promise resolved when the pace maker thinks it is a *good* time
* to vote for a block. The promise is resolved with the next proposer's ID
* */
virtual promise_t next_proposer(ReplicaID last_proposer) = 0;
};
using pacemaker_bt = BoxObj<PaceMaker>;
/** Parent selection implementation for PaceMaker: select all parents.
* PaceMakers derived from this class will select the highest block as the
* direct parent, while including other tail blocks (up to parent_limit) as
* uncles/aunts. */
class PMAllParents: public virtual PaceMaker {
const int32_t parent_limit; /**< maximum number of parents */
public:
PMAllParents(int32_t parent_limit): parent_limit(parent_limit) {}
std::vector<block_t> get_parents() override {
auto tails = hsc->get_tails();
size_t nparents = tails.size();
if (parent_limit > 0)
nparents = std::min(nparents, (size_t)parent_limit);
assert(nparents > 0);
block_t p = *tails.rbegin();
std::vector<block_t> parents{p};
nparents--;
/* add the rest of tails as "uncles/aunts" */
while (nparents--)
{
auto it = tails.begin();
parents.push_back(*it);
tails.erase(it);
}
return std::move(parents);
}
};
/** Beat implementation for PaceMaker: simply wait for the QC of last proposed
* block. PaceMakers derived from this class will beat only when the last
* block proposed by itself gets its QC. */
class PMWaitQC: public virtual PaceMaker {
std::queue<promise_t> pending_beats;
block_t last_proposed;
bool locked;
protected:
void schedule_next() {
if (!pending_beats.empty() && !locked)
{
auto pm = pending_beats.front();
pending_beats.pop();
hsc->async_qc_finish(last_proposed).then([this, pm]() {
pm.resolve(get_proposer());
});
locked = true;
}
}
void update_last_proposed() {
hsc->async_wait_propose().then([this](block_t blk) {
update_last_proposed();
last_proposed = blk;
locked = false;
schedule_next();
});
}
public:
void init(HotStuffCore *hsc) override {
PaceMaker::init(hsc);
last_proposed = hsc->get_genesis();
locked = false;
update_last_proposed();
}
ReplicaID get_proposer() override {
return hsc->get_id();
}
promise_t beat() override {
promise_t pm;
pending_beats.push(pm);
schedule_next();
return pm;
}
promise_t next_proposer(ReplicaID last_proposer) override {
return promise_t([last_proposer](promise_t &pm) {
pm.resolve(last_proposer);
});
}
};
/** Naive PaceMaker where everyone can be a proposer at any moment. */
struct PaceMakerDummy: public PMAllParents, public PMWaitQC {
PaceMakerDummy(int32_t parent_limit):
PMAllParents(parent_limit), PMWaitQC() {}
};
/** PaceMakerDummy with a fixed proposer. */
class PaceMakerDummyFixed: public PaceMakerDummy {
ReplicaID proposer;
public:
PaceMakerDummyFixed(ReplicaID proposer,
int32_t parent_limit):
PaceMakerDummy(parent_limit),
proposer(proposer) {}
ReplicaID get_proposer() override {
return proposer;
}
promise_t next_proposer(ReplicaID) override {
return promise_t([this](promise_t &pm) {
pm.resolve(proposer);
});
}
};
/**
* Simple long-standing proposer liveness gadget.
* There are three roles for each replica: proposer, candidate and follower.
*
* For a proposer, it proposes a new block and refrains itself from proposing
* the next block unless it receives the QC for the previous block. It will
* give up the leadership and turn into a candidate when it hasn't seen such QC
* for a while.
*
* For a follower, it never proposes any block, but keeps a timer for the QC
* for the block last proposed by the proposer (the proposer it believes to
* be). When it times out without seeing such QC, the follower turns into a
* candidate.
*
* For a candidate, it periodically proposes empty blocks to synchronize the
* preferred branch, with randomized timeout, and check for any new QC. Once it
* sees such new QC, if the QC is given by itself, it becomes the proposer,
* otherwise yields to the creator of the QC as a follower.
*/
class PMStickyProposer: virtual public PaceMaker {
enum {
PROPOSER,
FOLLOWER,
CANDIDATE
} role;
double qc_timeout;
double candidate_timeout;
EventContext ec;
/** QC timer or randomized timeout */
Event timer;
block_t last_proposed;
/** the proposer it believes when it is a follower */
ReplicaID proposer;
/* extra state needed for a proposer */
std::queue<promise_t> pending_beats;
bool locked;
/* extra state needed for a candidate */
std::unordered_map<ReplicaID, promise_t> last_proposed_by;
promise_t pm_wait_receive_proposal;
promise_t pm_wait_propose;
promise_t pm_qc_finish;
void reset_qc_timer() {
timer.del();
timer.add_with_timeout(qc_timeout);
HOTSTUFF_LOG_INFO("QC timer reset");
}
void clear_promises() {
pm_wait_receive_proposal.reject();
pm_wait_propose.reject();
pm_qc_finish.reject();
for (auto &p: last_proposed_by)
p.second.reject();
last_proposed_by.clear();
}
void reg_follower_receive_proposal() {
pm_wait_receive_proposal =
hsc->async_wait_receive_proposal().then(
salticidae::generic_bind(
&PMStickyProposer::follower_receive_proposal, this, _1));
}
void follower_receive_proposal(const Proposal &prop) {
if (prop.proposer == proposer)
{
auto &qc_ref = prop.blk->get_qc_ref();
if (last_proposed)
{
if (qc_ref != last_proposed)
{
HOTSTUFF_LOG_INFO("proposer misbehave");
to_candidate(); /* proposer misbehave */
}
}
HOTSTUFF_LOG_INFO("proposer emits new QC");
last_proposed = prop.blk;
reset_qc_timer();
}
reg_follower_receive_proposal();
}
void proposer_schedule_next() {
if (!pending_beats.empty() && !locked)
{
auto pm = pending_beats.front();
pending_beats.pop();
pm_qc_finish =
hsc->async_qc_finish(last_proposed).then([this, pm]() {
reset_qc_timer();
pm.resolve(proposer);
});
locked = true;
}
}
void reg_proposer_propose() {
pm_wait_propose = hsc->async_wait_propose().then(
salticidae::generic_bind(
&PMStickyProposer::proposer_propose, this, _1));
}
void proposer_propose(const block_t &blk) {
last_proposed = blk;
locked = false;
proposer_schedule_next();
reg_proposer_propose();
}
void candidate_qc_timeout() {
pm_qc_finish.reject();
hsc->async_wait_propose().then([this](const block_t &blk) {
pm_qc_finish = hsc->async_qc_finish(blk).then([this]() {
/* managed to collect a QC */
to_proposer();
});
});
reset_qc_timer();
hsc->on_propose(std::vector<command_t>{}, get_parents());
}
void reg_candidate_receive_proposal() {
pm_wait_receive_proposal =
hsc->async_wait_receive_proposal().then(
salticidae::generic_bind(
&PMStickyProposer::candidate_receive_proposal, this, _1));
}
void candidate_receive_proposal(const Proposal &prop) {
auto proposer = prop.proposer;
auto &p = last_proposed_by[proposer];
HOTSTUFF_LOG_INFO("got block from %d", proposer);
p.reject();
p = hsc->async_qc_finish(prop.blk).then([this, proposer]() {
to_follower(proposer);
});
reg_candidate_receive_proposal();
}
void to_follower(ReplicaID new_proposer) {
HOTSTUFF_LOG_INFO("new role: follower");
clear_promises();
role = FOLLOWER;
proposer = new_proposer;
last_proposed = nullptr;
timer = Event(ec, -1, 0, [this](int, short) {
/* unable to get a QC in time */
to_candidate();
});
/* redirect all pending cmds to the new proposer */
while (!pending_beats.empty())
{
pending_beats.front().resolve(proposer);
pending_beats.pop();
}
reg_follower_receive_proposal();
}
void to_proposer() {
HOTSTUFF_LOG_INFO("new role: proposer");
clear_promises();
role = PROPOSER;
proposer = hsc->get_id();
last_proposed = hsc->get_genesis();
timer = Event(ec, -1, 0, [this](int, short) {
/* proposer unable to get a QC in time */
to_candidate();
});
/* prepare the variables for the role of a proposer */
locked = false;
reg_proposer_propose();
}
void to_candidate() {
HOTSTUFF_LOG_INFO("new role: candidate");
clear_promises();
role = CANDIDATE;
proposer = hsc->get_id();
last_proposed = nullptr;
timer = Event(ec, -1, 0, [this](int, short) {
candidate_qc_timeout();
});
candidate_timeout = qc_timeout;
timer.add_with_timeout(salticidae::gen_rand_timeout(candidate_timeout));
reg_candidate_receive_proposal();
}
public:
PMStickyProposer(double qc_timeout, const EventContext &ec):
qc_timeout(qc_timeout), ec(ec) {}
void init(HotStuffCore *hsc) override {
PaceMaker::init(hsc);
to_candidate();
}
ReplicaID get_proposer() override {
return proposer;
}
promise_t beat() override {
if (role != FOLLOWER)
{
promise_t pm;
pending_beats.push(pm);
if (role == PROPOSER)
proposer_schedule_next();
return std::move(pm);
}
else
return promise_t([proposer=proposer](promise_t &pm) {
pm.resolve(proposer);
});
}
promise_t next_proposer(ReplicaID) override {
return promise_t([proposer=proposer](promise_t &pm) {
pm.resolve(proposer);
});
}
};
struct PaceMakerSticky: public PMAllParents, public PMStickyProposer {
PaceMakerSticky(int32_t parent_limit, double qc_timeout, EventContext eb):
PMAllParents(parent_limit), PMStickyProposer(qc_timeout, eb) {}
};
}
#endif
|