forked from XRPLF/rippled
-
Notifications
You must be signed in to change notification settings - Fork 1
/
RCLConsensus.h
474 lines (392 loc) · 15.1 KB
/
RCLConsensus.h
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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
//------------------------------------------------------------------------------
/*
This file is part of rippled: https://github.com/ripple/rippled
Copyright (c) 2012, 2013 Ripple Labs Inc.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
//==============================================================================
#ifndef RIPPLE_APP_CONSENSUS_RCLCONSENSUS_H_INCLUDED
#define RIPPLE_APP_CONSENSUS_RCLCONSENSUS_H_INCLUDED
#include <BeastConfig.h>
#include <ripple/app/consensus/RCLCxLedger.h>
#include <ripple/app/consensus/RCLCxPeerPos.h>
#include <ripple/app/consensus/RCLCxTx.h>
#include <ripple/app/misc/FeeVote.h>
#include <ripple/basics/CountedObject.h>
#include <ripple/basics/Log.h>
#include <ripple/beast/utility/Journal.h>
#include <ripple/consensus/Consensus.h>
#include <ripple/core/JobQueue.h>
#include <ripple/overlay/Message.h>
#include <ripple/protocol/RippleLedgerHash.h>
#include <ripple/protocol/STValidation.h>
#include <ripple/shamap/SHAMap.h>
#include <atomic>
#include <mutex>
namespace ripple {
class InboundTransactions;
class LocalTxs;
class LedgerMaster;
class ValidatorKeys;
/** Manages the generic consensus algorithm for use by the RCL.
*/
class RCLConsensus
{
// Implements the Adaptor template interface required by Consensus.
class Adaptor
{
Application& app_;
std::unique_ptr<FeeVote> feeVote_;
LedgerMaster& ledgerMaster_;
LocalTxs& localTxs_;
InboundTransactions& inboundTransactions_;
beast::Journal j_;
NodeID const nodeID_;
PublicKey const valPublic_;
SecretKey const valSecret_;
// Ledger we most recently needed to acquire
LedgerHash acquiringLedger_;
ConsensusParms parms_;
// The timestamp of the last validation we used
NetClock::time_point lastValidationTime_;
// These members are queried via public accesors and are atomic for
// thread safety.
std::atomic<bool> validating_{false};
std::atomic<std::size_t> prevProposers_{0};
std::atomic<std::chrono::milliseconds> prevRoundTime_{
std::chrono::milliseconds{0}};
std::atomic<ConsensusMode> mode_{ConsensusMode::observing};
public:
using Ledger_t = RCLCxLedger;
using NodeID_t = NodeID;
using TxSet_t = RCLTxSet;
using PeerPosition_t = RCLCxPeerPos;
using Result = ConsensusResult<Adaptor>;
Adaptor(
Application& app,
std::unique_ptr<FeeVote>&& feeVote,
LedgerMaster& ledgerMaster,
LocalTxs& localTxs,
InboundTransactions& inboundTransactions,
ValidatorKeys const & validatorKeys,
beast::Journal journal);
bool
validating() const
{
return validating_;
}
std::size_t
prevProposers() const
{
return prevProposers_;
}
std::chrono::milliseconds
prevRoundTime() const
{
return prevRoundTime_;
}
ConsensusMode
mode() const
{
return mode_;
}
/** Called before kicking off a new consensus round.
@param prevLedger Ledger that will be prior ledger for next round
@return Whether we enter the round proposing
*/
bool
preStartRound(RCLCxLedger const & prevLedger);
/** Consensus simulation parameters
*/
ConsensusParms const&
parms() const
{
return parms_;
}
private:
//---------------------------------------------------------------------
// The following members implement the generic Consensus requirements
// and are marked private to indicate ONLY Consensus<Adaptor> will call
// them (via friendship). Since they are callled only from Consenus<Adaptor>
// methods and since RCLConsensus::consensus_ should only be accessed
// under lock, these will only be called under lock.
//
// In general, the idea is that there is only ONE thread that is running
// consensus code at anytime. The only special case is the dispatched
// onAccept call, which does not take a lock and relies on Consensus not
// changing state until a future call to startRound.
friend class Consensus<Adaptor>;
/** Attempt to acquire a specific ledger.
If not available, asynchronously acquires from the network.
@param ledger The ID/hash of the ledger acquire
@return Optional ledger, will be seated if we locally had the ledger
*/
boost::optional<RCLCxLedger>
acquireLedger(LedgerHash const& ledger);
/** Share the given proposal with all peers
@param peerPos The peer position to share.
*/
void
share(RCLCxPeerPos const& peerPos);
/** Share disputed transaction to peers.
Only share if the provided transaction hasn't been shared recently.
@param tx The disputed transaction to share.
*/
void
share(RCLCxTx const& tx);
/** Acquire the transaction set associated with a proposal.
If the transaction set is not available locally, will attempt
acquire it from the network.
@param setId The transaction set ID associated with the proposal
@return Optional set of transactions, seated if available.
*/
boost::optional<RCLTxSet>
acquireTxSet(RCLTxSet::ID const& setId);
/** Whether the open ledger has any transactions
*/
bool
hasOpenTransactions() const;
/** Number of proposers that have vallidated the given ledger
@param h The hash of the ledger of interest
@return the number of proposers that validated a ledger
*/
std::size_t
proposersValidated(LedgerHash const& h) const;
/** Number of proposers that have validated a ledger descended from
requested ledger.
@param h The hash of the ledger of interest.
@return The number of validating peers that have validated a ledger
succeeding the one provided.
*/
std::size_t
proposersFinished(LedgerHash const& h) const;
/** Propose the given position to my peers.
@param proposal Our proposed position
*/
void
propose(RCLCxPeerPos::Proposal const& proposal);
/** Share the given tx set to peers.
@param set The TxSet to share.
*/
void
share(RCLTxSet const& set);
/** Get the ID of the previous ledger/last closed ledger(LCL) on the
network
@param ledgerID ID of previous ledger used by consensus
@param ledger Previous ledger consensus has available
@param mode Current consensus mode
@return The id of the last closed network
@note ledgerID may not match ledger.id() if we haven't acquired
the ledger matching ledgerID from the network
*/
uint256
getPrevLedger(
uint256 ledgerID,
RCLCxLedger const& ledger,
ConsensusMode mode);
void
onModeChange(ConsensusMode before, ConsensusMode after)
{
mode_ = after;
}
/** Close the open ledger and return initial consensus position.
@param ledger the ledger we are changing to
@param closeTime When consensus closed the ledger
@param mode Current consensus mode
@return Tentative consensus result
*/
Result
onClose(
RCLCxLedger const& ledger,
NetClock::time_point const& closeTime,
ConsensusMode mode);
/** Process the accepted ledger.
@param result The result of consensus
@param prevLedger The closed ledger consensus worked from
@param closeResolution The resolution used in agreeing on an
effective closeTime
@param rawCloseTimes The unrounded closetimes of ourself and our
peers
@param mode Our participating mode at the time consensus was
declared
@param consensusJson Json representation of consensus state
*/
void
onAccept(
Result const& result,
RCLCxLedger const& prevLedger,
NetClock::duration const& closeResolution,
ConsensusCloseTimes const& rawCloseTimes,
ConsensusMode const& mode,
Json::Value&& consensusJson);
/** Process the accepted ledger that was a result of simulation/force
accept.
@ref onAccept
*/
void
onForceAccept(
Result const& result,
RCLCxLedger const& prevLedger,
NetClock::duration const& closeResolution,
ConsensusCloseTimes const& rawCloseTimes,
ConsensusMode const& mode,
Json::Value&& consensusJson);
/** Notify peers of a consensus state change
@param ne Event type for notification
@param ledger The ledger at the time of the state change
@param haveCorrectLCL Whether we believ we have the correct LCL.
*/
void
notify(
protocol::NodeEvent ne,
RCLCxLedger const& ledger,
bool haveCorrectLCL);
/** Accept a new ledger based on the given transactions.
@ref onAccept
*/
void
doAccept(
Result const& result,
RCLCxLedger const& prevLedger,
NetClock::duration closeResolution,
ConsensusCloseTimes const& rawCloseTimes,
ConsensusMode const& mode,
Json::Value&& consensusJson);
/** Build the new last closed ledger.
Accept the given the provided set of consensus transactions and
build the last closed ledger. Since consensus just agrees on which
transactions to apply, but not whether they make it into the closed
ledger, this function also populates retriableTxs with those that
can be retried in the next round.
@param previousLedger Prior ledger building upon
@param set The set of transactions to apply to the ledger
@param closeTime The the ledger closed
@param closeTimeCorrect Whether consensus agreed on close time
@param closeResolution Resolution used to determine consensus close
time
@param roundTime Duration of this consensus rorund
@param retriableTxs Populate with transactions to retry in next
round
@return The newly built ledger
*/
RCLCxLedger
buildLCL(
RCLCxLedger const& previousLedger,
RCLTxSet const& set,
NetClock::time_point closeTime,
bool closeTimeCorrect,
NetClock::duration closeResolution,
std::chrono::milliseconds roundTime,
CanonicalTXSet& retriableTxs);
/** Validate the given ledger and share with peers as necessary
@param ledger The ledger to validate
@param proposing Whether we were proposing transactions while
generating this ledger. If we are not proposing,
a validation can still be sent to inform peers that
we know we aren't fully participating in consensus
but are still around and trying to catch up.
*/
void
validate(RCLCxLedger const& ledger, bool proposing);
};
public:
//! Constructor
RCLConsensus(
Application& app,
std::unique_ptr<FeeVote>&& feeVote,
LedgerMaster& ledgerMaster,
LocalTxs& localTxs,
InboundTransactions& inboundTransactions,
Consensus<Adaptor>::clock_type const& clock,
ValidatorKeys const & validatorKeys,
beast::Journal journal);
RCLConsensus(RCLConsensus const&) = delete;
RCLConsensus&
operator=(RCLConsensus const&) = delete;
//! Whether we are validating consensus ledgers.
bool
validating() const
{
return adaptor_.validating();
}
//! Get the number of proposing peers that participated in the previous
//! round.
std::size_t
prevProposers() const
{
return adaptor_.prevProposers();
}
/** Get duration of the previous round.
The duration of the round is the establish phase, measured from closing
the open ledger to accepting the consensus result.
@return Last round duration in milliseconds
*/
std::chrono::milliseconds
prevRoundTime() const
{
return adaptor_.prevRoundTime();
}
//! @see Consensus::mode
ConsensusMode
mode() const
{
return adaptor_.mode();
}
//! @see Consensus::getJson
Json::Value
getJson(bool full) const;
//! @see Consensus::startRound
void
startRound(
NetClock::time_point const& now,
RCLCxLedger::ID const& prevLgrId,
RCLCxLedger const& prevLgr);
//! @see Consensus::timerEntry
void
timerEntry(NetClock::time_point const& now);
//! @see Consensus::gotTxSet
void
gotTxSet(NetClock::time_point const& now, RCLTxSet const& txSet);
// @see Consensus::prevLedgerID
RCLCxLedger::ID
prevLedgerID() const
{
ScopedLockType _{mutex_};
return consensus_.prevLedgerID();
}
//! @see Consensus::simulate
void
simulate(
NetClock::time_point const& now,
boost::optional<std::chrono::milliseconds> consensusDelay);
//! @see Consensus::proposal
bool
peerProposal(
NetClock::time_point const& now,
RCLCxPeerPos const& newProposal);
ConsensusParms const &
parms() const
{
return adaptor_.parms();
}
private:
// Since Consensus does not provide intrinsic thread-safety, this mutex
// guards all calls to consensus_. adaptor_ uses atomics internally
// to allow concurrent access of its data members that have getters.
mutable std::recursive_mutex mutex_;
using ScopedLockType = std::lock_guard <std::recursive_mutex>;
Adaptor adaptor_;
Consensus<Adaptor> consensus_;
beast::Journal j_;
};
}
#endif