rippled
Loading...
Searching...
No Matches
RCLConsensus.h
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2012, 2013 Ripple Labs Inc.
5
6 Permission to use, copy, modify, and/or distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#ifndef RIPPLE_APP_CONSENSUS_RCLCONSENSUS_H_INCLUDED
21#define RIPPLE_APP_CONSENSUS_RCLCONSENSUS_H_INCLUDED
22
23#include <xrpld/app/consensus/RCLCensorshipDetector.h>
24#include <xrpld/app/consensus/RCLCxLedger.h>
25#include <xrpld/app/consensus/RCLCxPeerPos.h>
26#include <xrpld/app/consensus/RCLCxTx.h>
27#include <xrpld/app/misc/FeeVote.h>
28#include <xrpld/app/misc/NegativeUNLVote.h>
29#include <xrpld/consensus/Consensus.h>
30#include <xrpld/core/JobQueue.h>
31#include <xrpld/overlay/Message.h>
32#include <xrpld/shamap/SHAMap.h>
33#include <xrpl/basics/CountedObject.h>
34#include <xrpl/basics/Log.h>
35#include <xrpl/beast/utility/Journal.h>
36#include <xrpl/protocol/RippleLedgerHash.h>
37#include <xrpl/protocol/STValidation.h>
38#include <atomic>
39#include <mutex>
40#include <set>
41namespace ripple {
42
43class InboundTransactions;
44class LocalTxs;
45class LedgerMaster;
46class ValidatorKeys;
47
51{
54 constexpr static unsigned int censorshipWarnInternal = 15;
55
56 // Implements the Adaptor template interface required by Consensus.
57 class Adaptor
58 {
65
66 // If the server is validating, the necessary keying information:
68
69 // A randomly selected non-zero value used to tag our validations
71
72 // Ledger we most recently needed to acquire
75
76 // The timestamp of the last validation we used
78
79 // These members are queried via public accesors and are atomic for
80 // thread safety.
86
89
90 public:
96
98
99 Adaptor(
100 Application& app,
101 std::unique_ptr<FeeVote>&& feeVote,
103 LocalTxs& localTxs,
104 InboundTransactions& inboundTransactions,
105 ValidatorKeys const& validatorKeys,
106 beast::Journal journal);
107
108 bool
110 {
111 return validating_;
112 }
113
116 {
117 return prevProposers_;
118 }
119
122 {
123 return prevRoundTime_;
124 }
125
127 mode() const
128 {
129 return mode_;
130 }
131
138 bool
140 RCLCxLedger const& prevLedger,
141 hash_set<NodeID> const& nowTrusted);
142
143 bool
144 haveValidated() const;
145
147 getValidLedgerIndex() const;
148
150 getQuorumKeys() const;
151
153 laggards(Ledger_t::Seq const seq, hash_set<NodeKey_t>& trustedKeys)
154 const;
155
160 bool
161 validator() const;
162
170 void
171 updateOperatingMode(std::size_t const positions) const;
172
175 ConsensusParms const&
176 parms() const
177 {
178 return parms_;
179 }
180
181 private:
182 //---------------------------------------------------------------------
183 // The following members implement the generic Consensus requirements
184 // and are marked private to indicate ONLY Consensus<Adaptor> will call
185 // them (via friendship). Since they are called only from
186 // Consensus<Adaptor> methods and since RCLConsensus::consensus_ should
187 // only be accessed under lock, these will only be called under lock.
188 //
189 // In general, the idea is that there is only ONE thread that is running
190 // consensus code at anytime. The only special case is the dispatched
191 // onAccept call, which does not take a lock and relies on Consensus not
192 // changing state until a future call to startRound.
193 friend class Consensus<Adaptor>;
194
203 acquireLedger(LedgerHash const& hash);
204
209 void
210 share(RCLCxPeerPos const& peerPos);
211
218 void
219 share(RCLCxTx const& tx);
220
230 acquireTxSet(RCLTxSet::ID const& setId);
231
234 bool
235 hasOpenTransactions() const;
236
243 proposersValidated(LedgerHash const& h) const;
244
254 proposersFinished(RCLCxLedger const& ledger, LedgerHash const& h) const;
255
260 void
262
267 void
268 share(RCLTxSet const& txns);
269
281 uint256
283 uint256 ledgerID,
284 RCLCxLedger const& ledger,
286
292 void
294
302 Result
303 onClose(
304 RCLCxLedger const& ledger,
305 NetClock::time_point const& closeTime,
307
320 void
321 onAccept(
322 Result const& result,
323 RCLCxLedger const& prevLedger,
324 NetClock::duration const& closeResolution,
325 ConsensusCloseTimes const& rawCloseTimes,
326 ConsensusMode const& mode,
327 Json::Value&& consensusJson);
328
334 void
336 Result const& result,
337 RCLCxLedger const& prevLedger,
338 NetClock::duration const& closeResolution,
339 ConsensusCloseTimes const& rawCloseTimes,
340 ConsensusMode const& mode,
341 Json::Value&& consensusJson);
342
349 void
350 notify(
351 protocol::NodeEvent ne,
352 RCLCxLedger const& ledger,
353 bool haveCorrectLCL);
354
359 void
360 doAccept(
361 Result const& result,
362 RCLCxLedger const& prevLedger,
363 NetClock::duration closeResolution,
364 ConsensusCloseTimes const& rawCloseTimes,
365 ConsensusMode const& mode,
366 Json::Value&& consensusJson);
367
390 buildLCL(
391 RCLCxLedger const& previousLedger,
392 CanonicalTXSet& retriableTxs,
393 NetClock::time_point closeTime,
394 bool closeTimeCorrect,
395 NetClock::duration closeResolution,
397 std::set<TxID>& failedTxs);
398
409 void
410 validate(
411 RCLCxLedger const& ledger,
412 RCLTxSet const& txns,
413 bool proposing);
414 };
415
416public:
419 Application& app,
420 std::unique_ptr<FeeVote>&& feeVote,
422 LocalTxs& localTxs,
423 InboundTransactions& inboundTransactions,
425 ValidatorKeys const& validatorKeys,
426 beast::Journal journal);
427
428 RCLConsensus(RCLConsensus const&) = delete;
429
431 operator=(RCLConsensus const&) = delete;
432
434 bool
436 {
437 return adaptor_.validating();
438 }
439
444 {
445 return adaptor_.prevProposers();
446 }
447
457 {
458 return adaptor_.prevRoundTime();
459 }
460
463 mode() const
464 {
465 return adaptor_.mode();
466 }
467
469 phase() const
470 {
471 return consensus_.phase();
472 }
473
476 getJson(bool full) const;
477
481 void
483 NetClock::time_point const& now,
484 RCLCxLedger::ID const& prevLgrId,
485 RCLCxLedger const& prevLgr,
486 hash_set<NodeID> const& nowUntrusted,
487 hash_set<NodeID> const& nowTrusted);
488
490 void
492
494 void
495 gotTxSet(NetClock::time_point const& now, RCLTxSet const& txSet);
496
497 // @see Consensus::prevLedgerID
500 {
502 return consensus_.prevLedgerID();
503 }
504
506 void
507 simulate(
508 NetClock::time_point const& now,
510
512 bool
514 NetClock::time_point const& now,
515 RCLCxPeerPos const& newProposal);
516
517 ConsensusParms const&
518 parms() const
519 {
520 return adaptor_.parms();
521 }
522
523private:
524 // Since Consensus does not provide intrinsic thread-safety, this mutex
525 // guards all calls to consensus_. adaptor_ uses atomics internally
526 // to allow concurrent access of its data members that have getters.
528
532};
533} // namespace ripple
534
535#endif
Represents a JSON value.
Definition: json_value.h:147
A generic endpoint for log messages.
Definition: Journal.h:59
Holds transactions which were deferred to the next pass of consensus.
Generic implementation of consensus algorithm.
Definition: Consensus.h:288
Manages the acquisition and lifetime of transaction sets.
Manager to create NegativeUNL votes.
A public key.
Definition: PublicKey.h:62
void propose(RCLCxPeerPos::Proposal const &proposal)
Propose the given position to my peers.
ValidatorKeys const & validatorKeys_
Definition: RCLConsensus.h:67
beast::Journal const j_
Definition: RCLConsensus.h:64
std::atomic< ConsensusMode > mode_
Definition: RCLConsensus.h:85
InboundTransactions & inboundTransactions_
Definition: RCLConsensus.h:63
std::atomic< std::size_t > prevProposers_
Definition: RCLConsensus.h:82
Result onClose(RCLCxLedger const &ledger, NetClock::time_point const &closeTime, ConsensusMode mode)
Close the open ledger and return initial consensus position.
void share(RCLCxPeerPos const &peerPos)
Share the given proposal with all peers.
void doAccept(Result const &result, RCLCxLedger const &prevLedger, NetClock::duration closeResolution, ConsensusCloseTimes const &rawCloseTimes, ConsensusMode const &mode, Json::Value &&consensusJson)
Accept a new ledger based on the given transactions.
ConsensusMode mode() const
Definition: RCLConsensus.h:127
std::chrono::milliseconds prevRoundTime() const
Definition: RCLConsensus.h:121
void onModeChange(ConsensusMode before, ConsensusMode after)
Notified of change in consensus mode.
std::size_t laggards(Ledger_t::Seq const seq, hash_set< NodeKey_t > &trustedKeys) const
RCLCensorshipDetector< TxID, LedgerIndex > censorshipDetector_
Definition: RCLConsensus.h:87
void onForceAccept(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.
bool validator() const
Whether I am a validator.
RCLCxLedger buildLCL(RCLCxLedger const &previousLedger, CanonicalTXSet &retriableTxs, NetClock::time_point closeTime, bool closeTimeCorrect, NetClock::duration closeResolution, std::chrono::milliseconds roundTime, std::set< TxID > &failedTxs)
Build the new last closed ledger.
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.
std::optional< RCLCxLedger > acquireLedger(LedgerHash const &hash)
Attempt to acquire a specific ledger.
std::unique_ptr< FeeVote > feeVote_
Definition: RCLConsensus.h:60
LedgerIndex getValidLedgerIndex() const
std::size_t proposersFinished(RCLCxLedger const &ledger, LedgerHash const &h) const
Number of proposers that have validated a ledger descended from requested ledger.
std::size_t prevProposers() const
Definition: RCLConsensus.h:115
ConsensusParms const & parms() const
Consensus simulation parameters.
Definition: RCLConsensus.h:176
NetClock::time_point lastValidationTime_
Definition: RCLConsensus.h:77
std::optional< RCLTxSet > acquireTxSet(RCLTxSet::ID const &setId)
Acquire the transaction set associated with a proposal.
bool hasOpenTransactions() const
Whether the open ledger has any transactions.
void validate(RCLCxLedger const &ledger, RCLTxSet const &txns, bool proposing)
Validate the given ledger and share with peers as necessary.
std::atomic< std::chrono::milliseconds > prevRoundTime_
Definition: RCLConsensus.h:83
std::uint64_t const valCookie_
Definition: RCLConsensus.h:70
bool preStartRound(RCLCxLedger const &prevLedger, hash_set< NodeID > const &nowTrusted)
Called before kicking off a new consensus round.
uint256 getPrevLedger(uint256 ledgerID, RCLCxLedger const &ledger, ConsensusMode mode)
Get the ID of the previous ledger/last closed ledger(LCL) on the network.
std::size_t proposersValidated(LedgerHash const &h) const
Number of proposers that have validated the given ledger.
std::atomic< bool > validating_
Definition: RCLConsensus.h:81
void notify(protocol::NodeEvent ne, RCLCxLedger const &ledger, bool haveCorrectLCL)
Notify peers of a consensus state change.
void updateOperatingMode(std::size_t const positions) const
Update operating mode based on current peer positions.
std::pair< std::size_t, hash_set< NodeKey_t > > getQuorumKeys() const
Manages the generic consensus algorithm for use by the RCL.
Definition: RCLConsensus.h:51
RCLConsensus(RCLConsensus const &)=delete
void startRound(NetClock::time_point const &now, RCLCxLedger::ID const &prevLgrId, RCLCxLedger const &prevLgr, hash_set< NodeID > const &nowUntrusted, hash_set< NodeID > const &nowTrusted)
Adjust the set of trusted validators and kick-off the next round of consensus.
void gotTxSet(NetClock::time_point const &now, RCLTxSet const &txSet)
std::size_t prevProposers() const
Get the number of proposing peers that participated in the previous round.
Definition: RCLConsensus.h:443
void simulate(NetClock::time_point const &now, std::optional< std::chrono::milliseconds > consensusDelay)
ConsensusPhase phase() const
Definition: RCLConsensus.h:469
bool validating() const
Whether we are validating consensus ledgers.
Definition: RCLConsensus.h:435
RCLConsensus & operator=(RCLConsensus const &)=delete
std::chrono::milliseconds prevRoundTime() const
Get duration of the previous round.
Definition: RCLConsensus.h:456
void timerEntry(NetClock::time_point const &now)
ConsensusMode mode() const
Definition: RCLConsensus.h:463
static constexpr unsigned int censorshipWarnInternal
Warn for transactions that haven't been included every so many ledgers.
Definition: RCLConsensus.h:54
std::recursive_mutex mutex_
Definition: RCLConsensus.h:527
Consensus< Adaptor > consensus_
Definition: RCLConsensus.h:530
bool peerProposal(NetClock::time_point const &now, RCLCxPeerPos const &newProposal)
ConsensusParms const & parms() const
Definition: RCLConsensus.h:518
Json::Value getJson(bool full) const
RCLCxLedger::ID prevLedgerID() const
Definition: RCLConsensus.h:499
beast::Journal const j_
Definition: RCLConsensus.h:531
Represents a ledger in RCLConsensus.
Definition: RCLCxLedger.h:36
LedgerIndex Seq
Sequence number of a ledger.
Definition: RCLCxLedger.h:41
A peer's signed, proposed position for use in RCLConsensus.
Definition: RCLCxPeerPos.h:44
Represents a transaction in RCLConsensus.
Definition: RCLCxTx.h:36
Represents a set of transactions in RCLConsensus.
Definition: RCLCxTx.h:66
Validator keys and manifest as set in configuration file.
Definition: ValidatorKeys.h:37
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:26
ConsensusMode
Represents how a node currently participates in Consensus.
@ proposing
We are normal participant in consensus and propose our position.
@ observing
We are observing peer positions, but not proposing our position.
base_uint< 160, detail::NodeIDTag > NodeID
NodeID is a 160-bit hash representing one node.
Definition: UintTypes.h:59
boost::outcome_v2::result< T, std::error_code > Result
Definition: b58_utils.h:38
ConsensusPhase
Phases of consensus for a single ledger round.
@ ledgerMaster
ledger master data for signing
@ proposal
proposal for signing
static bool after(NetClock::time_point now, std::uint32_t mark)
Has the specified time passed?
Definition: Escrow.cpp:89
Stores the set of initial close times.
Consensus algorithm parameters.
Encapsulates the result of consensus.