xrpld
Loading...
Searching...
No Matches
RCLConsensus.cpp
1#include <xrpld/app/consensus/RCLConsensus.h>
2
3#include <xrpld/app/consensus/RCLCxLedger.h>
4#include <xrpld/app/consensus/RCLCxPeerPos.h>
5#include <xrpld/app/consensus/RCLCxTx.h>
6#include <xrpld/app/consensus/RCLValidations.h>
7#include <xrpld/app/ledger/BuildLedger.h>
8#include <xrpld/app/ledger/InboundLedger.h>
9#include <xrpld/app/ledger/InboundLedgers.h>
10#include <xrpld/app/ledger/InboundTransactions.h>
11#include <xrpld/app/ledger/LedgerMaster.h>
12#include <xrpld/app/ledger/LocalTxs.h>
13#include <xrpld/app/ledger/OpenLedger.h>
14#include <xrpld/app/misc/FeeVote.h>
15#include <xrpld/app/misc/NegativeUNLVote.h>
16#include <xrpld/app/misc/TxQ.h>
17#include <xrpld/app/misc/ValidatorKeys.h>
18#include <xrpld/app/misc/ValidatorList.h>
19#include <xrpld/overlay/Overlay.h>
20#include <xrpld/overlay/predicates.h>
21
22#include <xrpl/basics/Log.h>
23#include <xrpl/basics/Slice.h>
24#include <xrpl/basics/UnorderedContainers.h>
25#include <xrpl/basics/base_uint.h>
26#include <xrpl/basics/chrono.h>
27#include <xrpl/basics/contract.h>
28#include <xrpl/basics/random.h>
29#include <xrpl/beast/utility/Journal.h>
30#include <xrpl/beast/utility/Zero.h>
31#include <xrpl/beast/utility/instrumentation.h>
32#include <xrpl/consensus/CensorshipDetector.h>
33#include <xrpl/consensus/Consensus.h>
34#include <xrpl/consensus/ConsensusTypes.h>
35#include <xrpl/core/HashRouter.h>
36#include <xrpl/core/Job.h>
37#include <xrpl/crypto/csprng.h>
38#include <xrpl/json/json_value.h>
39#include <xrpl/json/json_writer.h>
40#include <xrpl/ledger/AmendmentTable.h>
41#include <xrpl/ledger/ApplyView.h>
42#include <xrpl/ledger/Ledger.h>
43#include <xrpl/ledger/LedgerTiming.h>
44#include <xrpl/ledger/OpenView.h>
45#include <xrpl/ledger/ReadView.h>
46#include <xrpl/ledger/View.h>
47#include <xrpl/protocol/BuildInfo.h>
48#include <xrpl/protocol/Protocol.h>
49#include <xrpl/protocol/PublicKey.h>
50#include <xrpl/protocol/RippleLedgerHash.h>
51#include <xrpl/protocol/Rules.h>
52#include <xrpl/protocol/SField.h>
53#include <xrpl/protocol/STTx.h>
54#include <xrpl/protocol/STValidation.h>
55#include <xrpl/protocol/SecretKey.h>
56#include <xrpl/protocol/Serializer.h>
57#include <xrpl/protocol/TxFlags.h>
58#include <xrpl/protocol/UintTypes.h>
59#include <xrpl/protocol/digest.h>
60#include <xrpl/protocol/tokens.h>
61#include <xrpl/server/LoadFeeTrack.h>
62#include <xrpl/server/NetworkOPs.h>
63#include <xrpl/shamap/SHAMapItem.h>
64#include <xrpl/shamap/SHAMapMissingNode.h>
65#include <xrpl/shamap/SHAMapTreeNode.h>
66
67#include <boost/smart_ptr/intrusive_ptr.hpp>
68
69#include <xrpl.pb.h>
70
71#include <algorithm>
72#include <chrono>
73#include <cstddef>
74#include <cstdint>
75#include <exception>
76#include <iomanip>
77#include <limits>
78#include <memory>
79#include <mutex>
80#include <optional>
81#include <set>
82#include <sstream>
83#include <string>
84#include <type_traits>
85#include <utility>
86#include <vector>
87
88namespace xrpl {
89
91 Application& app,
93 LedgerMaster& ledgerMaster,
94 LocalTxs& localTxs,
95 InboundTransactions& inboundTransactions,
97 ValidatorKeys const& validatorKeys,
98 beast::Journal journal)
99 : adaptor_(
100 app,
101 std::move(feeVote),
102 ledgerMaster,
103 localTxs,
104 inboundTransactions,
105 validatorKeys,
106 journal)
107 , consensus_(clock, adaptor_, journal)
108 , j_(journal)
109{
110}
111
113 Application& app,
114 std::unique_ptr<FeeVote>&& feeVote,
115 LedgerMaster& ledgerMaster,
116 LocalTxs& localTxs,
117 InboundTransactions& inboundTransactions,
118 ValidatorKeys const& validatorKeys,
119 beast::Journal journal)
120 : app_(app)
121 , feeVote_(std::move(feeVote))
122 , ledgerMaster_(ledgerMaster)
123 , localTxs_(localTxs)
124 , inboundTransactions_{inboundTransactions}
125 , j_(journal)
126 , validatorKeys_(validatorKeys)
127 , valCookie_(1 + randInt(cryptoPrng(), std::numeric_limits<std::uint64_t>::max() - 1))
128 , nUnlVote_(validatorKeys_.nodeID, j_)
129{
130 XRPL_ASSERT(valCookie_, "xrpl::RCLConsensus::Adaptor::Adaptor : nonzero cookie");
131
132 JLOG(j_.info()) << "Consensus engine started (cookie: " + std::to_string(valCookie_) + ")";
133
134 if (validatorKeys_.nodeID != beast::kZero && validatorKeys_.keys)
135 {
136 JLOG(j_.info()) << "Validator identity: "
137 << toBase58(TokenType::NodePublic, validatorKeys_.keys->masterPublicKey);
138
139 if (validatorKeys_.keys->masterPublicKey != validatorKeys_.keys->publicKey)
140 {
141 JLOG(j_.debug()) << "Validator ephemeral signing key: "
143 << " (seq: " << std::to_string(validatorKeys_.sequence) << ")";
144 }
145 }
146}
147
150{
151 // we need to switch the ledger we're working from
152 auto const built = ledgerMaster_.getLedgerByHash(hash);
153 if (!built)
154 {
155 if (acquiringLedger_ != hash)
156 {
157 // need to start acquiring the correct consensus LCL
158 JLOG(j_.warn()) << "Need consensus ledger " << hash;
159
160 // Tell the ledger acquire system that we need the consensus ledger
161 acquiringLedger_ = hash;
162
163 app_.getJobQueue().addJob(JtAdvance, "GetConsL1", [id = hash, &app = app_, this] {
164 JLOG(j_.debug()) << "JOB advanceLedger getConsensusLedger1 started";
165 app.getInboundLedgers().acquireAsync(id, 0, InboundLedger::Reason::CONSENSUS);
166 });
167 }
168 return std::nullopt;
169 }
170
171 XRPL_ASSERT(
172 !built->open() && built->isImmutable(),
173 "xrpl::RCLConsensus::Adaptor::acquireLedger : valid ledger state");
174 XRPL_ASSERT(
175 built->header().hash == hash,
176 "xrpl::RCLConsensus::Adaptor::acquireLedger : ledger hash match");
177
178 // Notify inbound transactions of the new ledger sequence number
179 inboundTransactions_.newRound(built->header().seq);
180
181 return RCLCxLedger(built);
182}
183
184void
186{
187 protocol::TMProposeSet prop;
188
189 auto const& proposal = peerPos.proposal();
190
191 prop.set_proposeseq(proposal.proposeSeq());
192 prop.set_closetime(proposal.closeTime().time_since_epoch().count());
193
194 prop.set_currenttxhash(proposal.position().begin(), proposal.position().size());
195 prop.set_previousledger(proposal.prevLedger().begin(), proposal.prevLedger().size());
196
197 auto const pk = peerPos.publicKey().slice();
198 prop.set_nodepubkey(pk.data(), pk.size());
199
200 auto const sig = peerPos.signature();
201 prop.set_signature(sig.data(), sig.size());
202
203 app_.getOverlay().relay(prop, peerPos.suppressionID(), peerPos.publicKey());
204}
205
206void
208{
209 // If we didn't relay this transaction recently, relay it to all peers
210 if (app_.getHashRouter().shouldRelay(tx.id()))
211 {
212 JLOG(j_.debug()) << "Relaying disputed tx " << tx.id();
213 auto const slice = tx.tx->slice();
214 protocol::TMTransaction msg;
215 msg.set_rawtransaction(slice.data(), slice.size());
216 msg.set_status(protocol::tsNEW);
217 static std::set<Peer::ID> const kSkip{};
218 app_.getOverlay().relay(tx.id(), msg, kSkip);
219 }
220 else
221 {
222 JLOG(j_.debug()) << "Not relaying disputed tx " << tx.id();
223 }
224}
225void
227{
228 JLOG(j_.trace()) << (proposal.isBowOut() ? "We bow out: " : "We propose: ")
229 << xrpl::to_string(proposal.prevLedger()) << " -> "
230 << xrpl::to_string(proposal.position());
231
232 protocol::TMProposeSet prop;
233
234 prop.set_currenttxhash(proposal.position().begin(), proposal.position().size());
235 prop.set_previousledger(proposal.prevLedger().begin(), proposal.prevLedger().size());
236 prop.set_proposeseq(proposal.proposeSeq());
237 prop.set_closetime(proposal.closeTime().time_since_epoch().count());
238
239 if (!validatorKeys_.keys)
240 {
241 JLOG(j_.warn()) << "RCLConsensus::Adaptor::propose: ValidatorKeys "
242 "not set: \n";
243 return;
244 }
245
246 auto const& keys = *validatorKeys_.keys;
247
248 prop.set_nodepubkey(keys.publicKey.data(), keys.publicKey.size());
249
250 auto sig = signDigest(keys.publicKey, keys.secretKey, proposal.signingHash());
251
252 prop.set_signature(sig.data(), sig.size());
253
254 auto const suppression = proposalUniqueId(
255 proposal.position(),
256 proposal.prevLedger(),
257 proposal.proposeSeq(),
258 proposal.closeTime(),
259 keys.publicKey,
260 sig);
261
262 app_.getHashRouter().addSuppression(suppression);
263
264 app_.getOverlay().broadcast(prop);
265}
266
267void
269{
270 inboundTransactions_.giveSet(txns.id(), txns.map, false);
271}
272
275{
276 if (auto txns = inboundTransactions_.getSet(setId, true))
277 {
278 return RCLTxSet{std::move(txns)};
279 }
280 return std::nullopt;
281}
282
283bool
285{
286 return !app_.getOpenLedger().empty();
287}
288
291{
292 return app_.getValidations().numTrustedForLedger(h);
293}
294
297{
298 RCLValidations& vals = app_.getValidations();
299 return vals.getNodesAfter(RCLValidatedLedger(ledger.ledger, vals.adaptor().journal()), h);
300}
301
304 UInt256 ledgerID,
305 RCLCxLedger const& ledger,
307{
308 RCLValidations& vals = app_.getValidations();
309 UInt256 netLgr = vals.getPreferred(
310 RCLValidatedLedger{ledger.ledger, vals.adaptor().journal()},
311 ledgerMaster_.getValidLedgerIndex());
312
313 if (netLgr != ledgerID)
314 {
316 app_.getOPs().consensusViewChange();
317
318 JLOG(j_.debug()) << json::Compact(app_.getValidations().getJsonTrie());
319 }
320
321 return netLgr;
322}
323
324auto
326 RCLCxLedger const& ledger,
327 NetClock::time_point const& closeTime,
329{
330 bool const wrongLCL = mode == ConsensusMode::WrongLedger;
331 bool const proposing = mode == ConsensusMode::Proposing;
332
333 notify(protocol::neCLOSING_LEDGER, ledger, !wrongLCL);
334
335 auto const& prevLedger = ledger.ledger;
336
337 ledgerMaster_.applyHeldTransactions();
338 // Tell the ledger master not to acquire the ledger we're probably building
339 ledgerMaster_.setBuildingLedger(prevLedger->header().seq + 1);
340
341 auto const initialLedger = app_.getOpenLedger().current();
342
343 auto initialSet = std::make_shared<SHAMap>(SHAMapType::TRANSACTION, app_.getNodeFamily());
344 initialSet->setUnbacked();
345
346 // Build SHAMap containing all transactions in our open ledger
347 for (auto const& tx : initialLedger->txs)
348 {
349 JLOG(j_.trace()) << "Adding open ledger TX " << tx.first->getTransactionID();
350 // Inner batch txns are applied only when building from the agreed set, never to the open
351 // ledger, so they cannot leak into the proposal.
352 XRPL_ASSERT(
353 !tx.first->isFlag(tfInnerBatchTxn),
354 "xrpl::RCLConsensus::Adaptor::onClose : no inner batch txn in open ledger");
355 Serializer s(2048);
356 tx.first->add(s);
357 initialSet->addItem(
359 makeShamapitem(tx.first->getTransactionID(), s.slice()));
360 }
361
362 // Add pseudo-transactions to the set
363 if (app_.config().standalone() || (proposing && !wrongLCL))
364 {
365 if (prevLedger->isFlagLedger())
366 {
367 // previous ledger was flag ledger, add fee and amendment
368 // pseudo-transactions
369 auto const validations =
370 app_.getValidators().negativeUNLFilter(app_.getValidations().getTrustedForLedger(
371 prevLedger->header().parentHash, prevLedger->seq() - 1));
372 if (validations.size() >= app_.getValidators().quorum())
373 {
374 feeVote_->doVoting(prevLedger, validations, initialSet);
375 app_.getAmendmentTable().doVoting(prevLedger, validations, initialSet, j_);
376 }
377 }
378 else if (prevLedger->isVotingLedger())
379 {
380 // previous ledger was a voting ledger,
381 // so the current consensus session is for a flag ledger,
382 // add negative UNL pseudo-transactions
383 nUnlVote_.doVoting(
384 prevLedger,
385 app_.getValidators().getTrustedMasterKeys(),
386 app_.getValidations(),
387 initialSet);
388 }
389 }
390
391 // Now we need an immutable snapshot
392 initialSet = initialSet->snapShot(false);
393
394 if (!wrongLCL)
395 {
396 LedgerIndex const seq = prevLedger->header().seq + 1;
398
399 initialSet->visitLeaves(
400 [&proposed, seq](boost::intrusive_ptr<SHAMapItem const> const& item) {
401 proposed.emplace_back(item->key(), seq);
402 });
403
404 censorshipDetector_.propose(std::move(proposed));
405 }
406
407 // Needed because of the move below.
408 auto const setHash = initialSet->getHash().asUInt256();
409
410 return Result{
411 std::move(initialSet),
413 initialLedger->header().parentHash,
415 setHash,
416 closeTime,
417 app_.getTimeKeeper().closeTime(),
418 validatorKeys_.nodeID}};
419}
420
421void
423 Result const& result,
424 RCLCxLedger const& prevLedger,
425 NetClock::duration const& closeResolution,
426 ConsensusCloseTimes const& rawCloseTimes,
427 ConsensusMode const& mode,
428 json::Value&& consensusJson)
429{
430 doAccept(result, prevLedger, closeResolution, rawCloseTimes, mode, std::move(consensusJson));
431}
432
433void
435 Result const& result,
436 RCLCxLedger const& prevLedger,
437 NetClock::duration const& closeResolution,
438 ConsensusCloseTimes const& rawCloseTimes,
439 ConsensusMode const& mode,
440 json::Value&& consensusJson,
441 bool const validating)
442{
443 app_.getJobQueue().addJob(
444 JtAccept,
445 "AcceptLedger",
446 // NOLINTNEXTLINE(cppcoreguidelines-misleading-capture-default-by-value)
447 [=, this, cj = std::move(consensusJson)] mutable {
448 // Note that no lock is held or acquired during this job.
449 // This is because generic Consensus guarantees that once a ledger
450 // is accepted, the consensus results and capture by reference state
451 // will not change until startRound is called (which happens via
452 // endConsensus).
453 RclConsensusLogger clog("onAccept", validating, j_);
454 this->doAccept(result, prevLedger, closeResolution, rawCloseTimes, mode, std::move(cj));
455 this->app_.getOPs().endConsensus(clog.ss());
456 });
457}
458
459void
461 Result const& result,
462 RCLCxLedger const& prevLedger,
463 NetClock::duration closeResolution,
464 ConsensusCloseTimes const& rawCloseTimes,
465 ConsensusMode const& mode,
466 json::Value&& consensusJson)
467{
468 prevProposers_ = result.proposers;
469 prevRoundTime_ = result.roundTime.read();
470
471 bool closeTimeCorrect = false;
472
473 bool const proposing = mode == ConsensusMode::Proposing;
474 bool const haveCorrectLCL = mode != ConsensusMode::WrongLedger;
475 bool const consensusFail = result.state == ConsensusState::MovedOn;
476
477 auto consensusCloseTime = result.position.closeTime();
478
479 if (consensusCloseTime == NetClock::time_point{})
480 {
481 // We agreed to disagree on the close time
482 using namespace std::chrono_literals;
483 consensusCloseTime = prevLedger.closeTime() + 1s;
484 closeTimeCorrect = false;
485 }
486 else
487 {
488 // We agreed on a close time
489 consensusCloseTime =
490 effCloseTime(consensusCloseTime, closeResolution, prevLedger.closeTime());
491 closeTimeCorrect = true;
492 }
493
494 JLOG(j_.debug()) << "Report: Prop=" << (proposing ? "yes" : "no")
495 << " val=" << (validating_ ? "yes" : "no")
496 << " corLCL=" << (haveCorrectLCL ? "yes" : "no")
497 << " fail=" << (consensusFail ? "yes" : "no");
498 JLOG(j_.debug()) << "Report: Prev = " << prevLedger.id() << ":" << prevLedger.seq();
499
500 //--------------------------------------------------------------------------
501 std::set<TxID> failed;
502
503 // We want to put transactions in an unpredictable but deterministic order:
504 // we use the hash of the set.
505 //
506 // FIXME: Use a std::vector and a custom sorter instead of CanonicalTXSet?
507 CanonicalTXSet retriableTxs{result.txns.map->getHash().asUInt256()};
508
509 JLOG(j_.debug()) << "Building canonical tx set: " << retriableTxs.key();
510
511 for (auto const& item : *result.txns.map)
512 {
513 try
514 {
515 retriableTxs.insert(std::make_shared<STTx const>(SerialIter{item.slice()}));
516 JLOG(j_.debug()) << " Tx: " << item.key();
517 }
518 catch (std::exception const& ex)
519 {
520 failed.insert(item.key());
521 JLOG(j_.warn()) << " Tx: " << item.key() << " throws: " << ex.what();
522 }
523 }
524
525 auto const built = buildLCL(
526 prevLedger,
527 retriableTxs,
528 consensusCloseTime,
529 closeTimeCorrect,
530 closeResolution,
531 result.roundTime.read(),
532 failed);
533
534 auto const newLCLHash = built.id();
535 JLOG(j_.debug()) << "Built ledger #" << built.seq() << ": " << newLCLHash;
536
537 // Tell directly connected peers that we have a new LCL
538 notify(protocol::neACCEPTED_LEDGER, built, haveCorrectLCL);
539
540 // As long as we're in sync with the network, attempt to detect attempts
541 // at censorship of transaction by tracking which ones don't make it in
542 // after a period of time.
543 if (haveCorrectLCL && result.state == ConsensusState::Yes)
544 {
545 std::vector<TxID> accepted;
546
547 result.txns.map->visitLeaves(
548 [&accepted](boost::intrusive_ptr<SHAMapItem const> const& item) {
549 accepted.push_back(item->key());
550 });
551
552 // Track all the transactions which failed or were marked as retriable
553 for (auto const& r : retriableTxs)
554 failed.insert(r.first.getTXID());
555
557 std::move(accepted),
558 [curr = built.seq(), j = app_.getJournal("CensorshipDetector"), &failed](
559 UInt256 const& id, LedgerIndex seq) {
560 if (failed.contains(id))
561 return true;
562
563 auto const wait = curr - seq;
564
565 if (wait && (wait % kCensorshipWarnInternal == 0))
566 {
567 std::ostringstream ss;
568 ss << "Potential Censorship: Eligible tx " << id
569 << ", which we are tracking since ledger " << seq
570 << " has not been included as of ledger " << curr << ".";
571
572 JLOG(j.warn()) << ss.str();
573 }
574
575 return false;
576 });
577 }
578
579 if (validating_)
580 validating_ = ledgerMaster_.isCompatible(*built.ledger, j_.warn(), "Not validating");
581
582 if (validating_ && !consensusFail && app_.getValidations().canValidateSeq(built.seq()))
583 {
584 validate(built, result.txns, proposing);
585 JLOG(j_.info()) << "CNF Val " << newLCLHash;
586 }
587 else
588 {
589 JLOG(j_.info()) << "CNF buildLCL " << newLCLHash;
590 }
591
592 // See if we can accept a ledger as fully-validated
593 ledgerMaster_.consensusBuilt(built.ledger, result.txns.id(), std::move(consensusJson));
594
595 //-------------------------------------------------------------------------
596 {
597 // Apply disputed transactions that didn't get in
598 //
599 // The first crack of transactions to get into the new
600 // open ledger goes to transactions proposed by a validator
601 // we trust but not included in the consensus set.
602 //
603 // These are done first because they are the most likely
604 // to receive agreement during consensus. They are also
605 // ordered logically "sooner" than transactions not mentioned
606 // in the previous consensus round.
607 //
608 bool anyDisputes = false;
609 for (auto const& [_, dispute] : result.disputes)
610 {
611 (void)_;
612 if (!dispute.getOurVote())
613 {
614 // we voted NO
615 try
616 {
617 JLOG(j_.debug()) << "Test applying disputed transaction that did"
618 << " not get in " << dispute.tx().id();
619
620 SerialIter sit(dispute.tx().tx->slice());
621 auto const txn = std::make_shared<STTx const>(sit);
622
623 // Disputed pseudo-transactions that were not accepted
624 // can't be successfully applied in the next ledger
625 if (isPseudoTx(*txn))
626 continue;
627
628 retriableTxs.insert(txn);
629
630 anyDisputes = true;
631 }
632 catch (std::exception const& ex)
633 {
634 JLOG(j_.debug()) << "Failed to apply transaction we voted "
635 "NO on. Exception: "
636 << ex.what();
637 }
638 }
639 }
640
641 // Build new open ledger
642 std::unique_lock lock{app_.getMasterMutex(), std::defer_lock};
643 std::unique_lock sl{ledgerMaster_.peekMutex(), std::defer_lock};
644 std::lock(lock, sl);
645
646 auto const lastVal = ledgerMaster_.getValidatedLedger();
647 std::optional<Rules> rules;
648 if (lastVal)
649 {
650 rules = makeRulesGivenLedger(*lastVal, app_.config().features);
651 }
652 else
653 {
654 rules.emplace(app_.config().features);
655 }
656 app_.getOpenLedger().accept(
657 app_,
658 *rules,
659 built.ledger,
660 localTxs_.getTxSet(),
661 anyDisputes,
662 retriableTxs,
663 TapNone,
664 "consensus",
665 [&](OpenView& view, beast::Journal j) {
666 // Stuff the ledger with transactions from the queue.
667 return app_.getTxQ().accept(app_, view);
668 });
669
670 // Signal a potential fee change to subscribers after the open ledger
671 // is created
672 app_.getOPs().reportFeeChange();
673 }
674
675 //-------------------------------------------------------------------------
676 {
677 ledgerMaster_.switchLCL(built.ledger);
678
679 // Do these need to exist?
680 XRPL_ASSERT(
681 ledgerMaster_.getClosedLedger()->header().hash == built.id(),
682 "xrpl::RCLConsensus::Adaptor::doAccept : ledger hash match");
683 XRPL_ASSERT(
684 app_.getOpenLedger().current()->header().parentHash == built.id(),
685 "xrpl::RCLConsensus::Adaptor::doAccept : parent hash match");
686 }
687
688 //-------------------------------------------------------------------------
689 // we entered the round with the network,
690 // see how close our close time is to other node's
691 // close time reports, and update our clock.
692 if ((mode == ConsensusMode::Proposing || mode == ConsensusMode::Observing) && !consensusFail)
693 {
694 JLOG(j_.info()) << "We closed at " << rawCloseTimes.self.time_since_epoch().count();
695 int closeCount = 1;
696 for (auto const& [t, v] : rawCloseTimes.peers)
697 {
698 JLOG(j_.info()) << std::to_string(v) << " time votes for "
699 << std::to_string(t.time_since_epoch().count());
700 closeCount += v;
701 }
702
703 // Median handles outliers better than mean.
704 auto const offset = medianCloseOffset(rawCloseTimes);
705 JLOG(j_.info()) << "Our close offset is estimated at " << offset.count() << " ("
706 << closeCount << ")";
707
708 app_.getTimeKeeper().adjustCloseTime(offset);
709 }
710}
711
712void
714 protocol::NodeEvent ne,
715 RCLCxLedger const& ledger,
716 bool haveCorrectLCL)
717{
718 protocol::TMStatusChange s;
719
720 if (!haveCorrectLCL)
721 {
722 s.set_newevent(protocol::neLOST_SYNC);
723 }
724 else
725 {
726 s.set_newevent(ne);
727 }
728
729 s.set_ledgerseq(ledger.seq());
730 s.set_networktime(app_.getTimeKeeper().now().time_since_epoch().count());
731 s.set_ledgerhashprevious(
732 ledger.parentID().begin(), std::decay_t<decltype(ledger.parentID())>::kBytes);
733 s.set_ledgerhash(ledger.id().begin(), std::decay_t<decltype(ledger.id())>::kBytes);
734
735 std::uint32_t uMin = 0, uMax = 0;
736 if (!ledgerMaster_.getFullValidatedRange(uMin, uMax))
737 {
738 uMin = 0;
739 uMax = 0;
740 }
741 else
742 {
743 // Don't advertise ledgers we're not willing to serve
744 uMin = std::max(uMin, ledgerMaster_.getEarliestFetch());
745 }
746 s.set_firstseq(uMin);
747 s.set_lastseq(uMax);
748 app_.getOverlay().foreach(SendAlways(std::make_shared<Message>(s, protocol::mtSTATUS_CHANGE)));
749 JLOG(j_.trace()) << "send status change to peer";
750}
751
754 RCLCxLedger const& previousLedger,
755 CanonicalTXSet& retriableTxs,
756 NetClock::time_point closeTime,
757 bool closeTimeCorrect,
758 NetClock::duration closeResolution,
760 std::set<TxID>& failedTxs)
761{
762 std::shared_ptr<Ledger> built = [&] {
763 if (auto const replayData = ledgerMaster_.releaseReplay())
764 {
765 XRPL_ASSERT(
766 replayData->parent()->header().hash == previousLedger.id(),
767 "xrpl::RCLConsensus::Adaptor::buildLCL : parent hash match");
768 return buildLedger(*replayData, TapNone, app_, j_);
769 }
770 return buildLedger(
771 previousLedger.ledger,
772 closeTime,
773 closeTimeCorrect,
774 closeResolution,
775 app_,
776 retriableTxs,
777 failedTxs,
778 j_);
779 }();
780
781 // Update fee computations based on accepted txs
782 using namespace std::chrono_literals;
783 app_.getTxQ().processClosedLedger(app_, *built, roundTime > 5s);
784
785 // And stash the ledger in the ledger master
786 if (ledgerMaster_.storeLedger(built))
787 {
788 JLOG(j_.debug()) << "Consensus built ledger we already had";
789 }
790 else if (app_.getInboundLedgers().find(built->header().hash))
791 {
792 JLOG(j_.debug()) << "Consensus built ledger we were acquiring";
793 }
794 else
795 {
796 JLOG(j_.debug()) << "Consensus built new ledger";
797 }
798 return RCLCxLedger{std::move(built)};
799}
800
801void
802RCLConsensus::Adaptor::validate(RCLCxLedger const& ledger, RCLTxSet const& txns, bool proposing)
803{
804 using namespace std::chrono_literals;
805
806 auto validationTime = app_.getTimeKeeper().closeTime();
807 if (validationTime <= lastValidationTime_)
808 validationTime = lastValidationTime_ + 1s;
809 lastValidationTime_ = validationTime;
810
811 if (!validatorKeys_.keys)
812 {
813 JLOG(j_.warn()) << "RCLConsensus::Adaptor::validate: ValidatorKeys "
814 "not set\n";
815 return;
816 }
817
818 auto const& keys = *validatorKeys_.keys;
819
820 auto const v = std::make_shared<STValidation>(
822 keys.publicKey,
823 keys.secretKey,
824 validatorKeys_.nodeID,
825 [&](STValidation& v) {
826 v.setFieldH256(sfLedgerHash, ledger.id());
827 v.setFieldH256(sfConsensusHash, txns.id());
828
829 v.setFieldU32(sfLedgerSequence, ledger.seq());
830
831 if (proposing)
832 v.setFlag(kVfFullValidation);
833
834 // Attest to the hash of what we consider to be the last fully
835 // validated ledger. This may be the hash of the ledger we are
836 // validating here, and that's fine.
837 if (auto const vl = ledgerMaster_.getValidatedLedger())
838 v.setFieldH256(sfValidatedHash, vl->header().hash);
839
840 v.setFieldU64(sfCookie, valCookie_);
841
842 // Report our server version every flag ledger:
843 if (ledger.ledger->isVotingLedger())
844 v.setFieldU64(sfServerVersion, build_info::getEncodedVersion());
845
846 // Report our load
847 {
848 auto const& ft = app_.getFeeTrack();
849 auto const fee = std::max(ft.getLocalFee(), ft.getClusterFee());
850 if (fee > ft.getLoadBase())
851 v.setFieldU32(sfLoadFee, fee);
852 }
853
854 // If the next ledger is a flag ledger, suggest fee changes and
855 // new features:
856 if (ledger.ledger->isVotingLedger())
857 {
858 // Fees:
859 feeVote_->doValidation(ledger.ledger->fees(), ledger.ledger->rules(), v);
860
861 // Amendments
862 // FIXME: pass `v` and have the function insert the array
863 // directly?
864 auto const amendments =
865 app_.getAmendmentTable().doValidation(getEnabledAmendments(*ledger.ledger));
866
867 if (!amendments.empty())
868 v.setFieldV256(sfAmendments, STVector256(sfAmendments, amendments));
869 }
870 });
871
872 auto const serialized = v->getSerialized();
873
874 // suppress it if we receive it
875 app_.getHashRouter().addSuppression(sha512Half(makeSlice(serialized)));
876
877 handleNewValidation(app_, v, "local");
878
879 // Broadcast to all our peers:
880 protocol::TMValidation val;
881 val.set_validation(serialized.data(), serialized.size());
882 app_.getOverlay().broadcast(val);
883
884 // Publish to all our subscribers:
885 app_.getOPs().pubValidation(v);
886}
887
888void
890{
891 JLOG(j_.info()) << "Consensus mode change before=" << to_string(before)
892 << ", after=" << to_string(after);
893
894 // If we were proposing but aren't any longer, we need to reset the
895 // censorship tracking to avoid bogus warnings.
896 if ((before == ConsensusMode::Proposing || before == ConsensusMode::Observing) &&
897 before != after)
898 censorshipDetector_.reset();
899
900 mode_ = after;
901}
902
904RCLConsensus::getJson(bool full) const
905{
906 json::Value ret;
907 {
908 std::scoped_lock const _{mutex_};
909 ret = consensus_.getJson(full);
910 }
911 ret["validating"] = adaptor_.validating();
912 return ret;
913}
914
915void
917 NetClock::time_point const& now,
919{
920 try
921 {
922 std::scoped_lock const _{mutex_};
923 consensus_.timerEntry(now, clog);
924 }
925 catch (SHAMapMissingNode const& mn)
926 {
927 // This should never happen
929 ss << "During consensus timerEntry: " << mn.what();
930 JLOG(j_.error()) << ss.str();
931 CLOG(clog) << ss.str();
932 rethrow();
933 }
934}
935
936void
938{
939 try
940 {
941 std::scoped_lock const _{mutex_};
942 consensus_.gotTxSet(now, txSet);
943 }
944 catch (SHAMapMissingNode const& mn)
945 {
946 // This should never happen
947 JLOG(j_.error()) << "During consensus gotTxSet: " << mn.what();
948 rethrow();
949 }
950}
951
955
956void
958 NetClock::time_point const& now,
960{
961 std::scoped_lock const _{mutex_};
962 consensus_.simulate(now, consensusDelay);
963}
964
965bool
967{
968 std::scoped_lock const _{mutex_};
969 return consensus_.peerProposal(now, newProposal);
970}
971
972bool
974{
975 // We have a key, we do not want out of sync validations after a restart
976 // and are not amendment blocked.
977 validating_ = validatorKeys_.keys && prevLgr.seq() >= app_.getMaxDisallowedLedger() &&
978 !app_.getOPs().isBlocked();
979
980 // If we are not running in standalone mode and there's a configured UNL,
981 // check to make sure that it's not expired.
982 if (validating_ && !app_.config().standalone() && (app_.getValidators().count() != 0u))
983 {
984 auto const when = app_.getValidators().expires();
985
986 if (!when || *when < app_.getTimeKeeper().now())
987 {
988 JLOG(j_.error()) << "Voluntarily bowing out of consensus process "
989 "because of an expired validator list.";
990 validating_ = false;
991 }
992 }
993
994 bool const synced = app_.getOPs().getOperatingMode() == OperatingMode::FULL;
995
996 if (validating_)
997 {
998 JLOG(j_.info()) << "Entering consensus process, validating, synced="
999 << (synced ? "yes" : "no");
1000 }
1001 else
1002 {
1003 // Otherwise we just want to monitor the validation process.
1004 JLOG(j_.info()) << "Entering consensus process, watching, synced="
1005 << (synced ? "yes" : "no");
1006 }
1007
1008 // Notify inbound ledgers that we are starting a new round
1009 inboundTransactions_.newRound(prevLgr.seq());
1010
1011 // Notify NegativeUNLVote that new validators are added
1012 if (!nowTrusted.empty())
1013 nUnlVote_.newValidators(prevLgr.seq() + 1, nowTrusted);
1014
1015 // propose only if we're in sync with the network (and validating)
1016 return validating_ && synced;
1017}
1018
1019bool
1021{
1022 return ledgerMaster_.haveValidated();
1023}
1024
1027{
1028 return ledgerMaster_.getValidLedgerIndex();
1029}
1030
1033{
1034 return app_.getValidators().getQuorumKeys();
1035}
1036
1039 LedgerT::Seq const seq,
1041{
1042 return app_.getValidations().laggards(seq, trustedKeys);
1043}
1044
1045bool
1047{
1048 return validatorKeys_.keys.has_value();
1049}
1050
1051void
1053{
1054 if ((positions == 0u) && app_.getOPs().isFull())
1055 app_.getOPs().setMode(OperatingMode::CONNECTED);
1056}
1057
1058void
1060 NetClock::time_point const& now,
1061 RCLCxLedger::ID const& prevLgrId,
1062 RCLCxLedger const& prevLgr,
1063 HashSet<NodeID> const& nowUntrusted,
1064 HashSet<NodeID> const& nowTrusted,
1066{
1067 std::scoped_lock const _{mutex_};
1068 consensus_.startRound(
1069 now, prevLgrId, prevLgr, nowUntrusted, adaptor_.preStartRound(prevLgr, nowTrusted), clog);
1070}
1071
1073 : j_(j)
1074{
1075 if (!validating && !j.info())
1076 return;
1079 header_ = "ConsensusLogger ";
1080 header_ += label;
1081 header_ += ": ";
1082}
1083
1085{
1086 if (!ss_)
1087 return;
1090 std::stringstream outSs;
1091 outSs << header_ << "duration " << (duration.count() / 1000) << '.' << std::setw(3)
1092 << std::setfill('0') << (duration.count() % 1000) << "s. " << ss_->str();
1093 j_.sink().writeAlways(beast::Severity::Info, outSs.str());
1094}
1095
1096} // namespace xrpl
A generic endpoint for log messages.
Definition Journal.h:44
Stream debug() const
Definition Journal.h:344
Stream info() const
Definition Journal.h:350
Decorator for streaming out compact json.
Represents a JSON value.
Definition json_value.h:121
iterator begin()
Definition base_uint.h:128
Holds transactions which were deferred to the next pass of consensus.
UInt256 const & key() const
void insert(std::shared_ptr< STTx const > txn)
std::vector< TxIDSeq > TxIDSeqVec
NetClock::time_point const & closeTime() const
The current position on the consensus close time.
std::chrono::milliseconds read() const
beast::AbstractClock< std::chrono::steady_clock > ClockType
Clock type for measuring time within the consensus code.
Definition Consensus.h:328
Manages the acquisition and lifetime of transaction sets.
Tracks the current ledger and any ledgers in the process of closing.
std::chrono::time_point< NetClock > time_point
Definition chrono.h:48
std::chrono::duration< rep, period > duration
Definition chrono.h:47
Slice slice() const noexcept
Definition PublicKey.h:115
InboundTransactions & inboundTransactions_
CensorshipDetector< TxID, LedgerIndex > censorshipDetector_
Result onClose(RCLCxLedger const &ledger, NetClock::time_point const &closeTime, ConsensusMode mode)
Close the open ledger and return initial consensus position.
bool validator() const
Whether I am a validator.
LedgerIndex getValidLedgerIndex() const
Adaptor(Application &app, std::unique_ptr< FeeVote > &&feeVote, LedgerMaster &ledgerMaster, LocalTxs &localTxs, InboundTransactions &inboundTransactions, ValidatorKeys const &validatorKeys, beast::Journal journal)
void updateOperatingMode(std::size_t const positions) const
Update operating mode based on current peer positions.
std::size_t proposersFinished(RCLCxLedger const &ledger, LedgerHash const &h) const
Number of proposers that have validated a ledger descended from requested ledger.
void onAccept(Result const &result, RCLCxLedger const &prevLedger, NetClock::duration const &closeResolution, ConsensusCloseTimes const &rawCloseTimes, ConsensusMode const &mode, json::Value &&consensusJson, bool const validating)
Process the accepted ledger.
void validate(RCLCxLedger const &ledger, RCLTxSet const &txns, bool proposing)
Validate the given ledger and share with peers as necessary.
std::unique_ptr< FeeVote > feeVote_
std::atomic< bool > validating_
void propose(RCLCxPeerPos::Proposal const &proposal)
Propose the given position to my peers.
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.
ConsensusMode mode() const
NetClock::time_point lastValidationTime_
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.
void notify(protocol::NodeEvent ne, RCLCxLedger const &ledger, bool haveCorrectLCL)
Notify peers of a consensus state change.
std::atomic< ConsensusMode > mode_
std::optional< RCLTxSet > acquireTxSet(RCLTxSet::ID const &setId)
Acquire the transaction set associated with a proposal.
ValidatorKeys const & validatorKeys_
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.
beast::Journal const j_
std::atomic< std::chrono::milliseconds > prevRoundTime_
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.
ConsensusResult< Adaptor > Result
std::pair< std::size_t, HashSet< NodeKeyT > > getQuorumKeys() const
bool preStartRound(RCLCxLedger const &prevLedger, HashSet< NodeID > const &nowTrusted)
Called before kicking off a new consensus round.
std::optional< RCLCxLedger > acquireLedger(LedgerHash const &hash)
Attempt to acquire a specific ledger.
bool hasOpenTransactions() const
Whether the open ledger has any transactions.
std::atomic< std::size_t > prevProposers_
void onModeChange(ConsensusMode before, ConsensusMode after)
Notified of change in consensus mode.
std::uint64_t const valCookie_
std::size_t laggards(LedgerT::Seq const seq, HashSet< NodeKeyT > &trustedKeys) const
void share(RCLCxPeerPos const &peerPos)
Share the given proposal with all peers.
Consensus< Adaptor > consensus_
void timerEntry(NetClock::time_point const &now, std::unique_ptr< std::stringstream > const &clog={})
bool validating() const
Whether we are validating consensus ledgers.
json::Value getJson(bool full) const
beast::Journal const j_
bool peerProposal(NetClock::time_point const &now, RCLCxPeerPos const &newProposal)
void simulate(NetClock::time_point const &now, std::optional< std::chrono::milliseconds > consensusDelay)
std::recursive_mutex mutex_
void startRound(NetClock::time_point const &now, RCLCxLedger::ID const &prevLgrId, RCLCxLedger const &prevLgr, HashSet< NodeID > const &nowUntrusted, HashSet< NodeID > const &nowTrusted, std::unique_ptr< std::stringstream > const &clog)
Adjust the set of trusted validators and kick-off the next round of consensus.
ConsensusMode mode() const
RCLConsensus(Application &app, std::unique_ptr< FeeVote > &&feeVote, LedgerMaster &ledgerMaster, LocalTxs &localTxs, InboundTransactions &inboundTransactions, Consensus< Adaptor >::ClockType const &clock, ValidatorKeys const &validatorKeys, beast::Journal journal)
Constructor.
void gotTxSet(NetClock::time_point const &now, RCLTxSet const &txSet)
Represents a ledger in RCLConsensus.
Definition RCLCxLedger.h:23
LedgerHash ID
Unique identifier of a ledger.
Definition RCLCxLedger.h:28
LedgerIndex Seq
Sequence number of a ledger.
Definition RCLCxLedger.h:32
Seq const & seq() const
Sequence number of the ledger.
Definition RCLCxLedger.h:56
ID const & parentID() const
Unique identifier (hash) of this ledger's parent.
Definition RCLCxLedger.h:74
std::shared_ptr< Ledger const > ledger
The ledger instance.
ID const & id() const
Unique identifier (hash) of this ledger.
Definition RCLCxLedger.h:65
NetClock::time_point closeTime() const
The close time of this ledger.
A peer's signed, proposed position for use in RCLConsensus.
PublicKey const & publicKey() const
Public key of peer that sent the proposal.
ConsensusProposal< NodeID, UInt256, UInt256 > Proposal
Slice signature() const
Signature of the proposal (not necessarily verified).
UInt256 const & suppressionID() const
Unique id used by hash router to suppress duplicates.
Proposal const & proposal() const
Represents a transaction in RCLConsensus.
Definition RCLCxTx.h:22
boost::intrusive_ptr< SHAMapItem const > tx
The SHAMapItem that represents the transaction.
Definition RCLCxTx.h:50
ID const & id() const
The unique identifier/hash of the transaction.
Definition RCLCxTx.h:42
Represents a set of transactions in RCLConsensus.
Definition RCLCxTx.h:60
UInt256 ID
Unique identifier/hash of the set of transactions.
Definition RCLCxTx.h:65
ID id() const
The unique ID/hash of the transaction set.
Definition RCLCxTx.h:163
std::shared_ptr< SHAMap > map
The SHAMap representing the transactions.
Definition RCLCxTx.h:201
Wraps a ledger instance for use in generic Validations LedgerTrie.
beast::Journal journal() const
Collects logging information.
std::unique_ptr< std::stringstream > const & ss()
std::chrono::steady_clock::time_point start_
RclConsensusLogger(char const *label, bool validating, beast::Journal j)
std::unique_ptr< std::stringstream > ss_
Slice slice() const noexcept
Definition Serializer.h:141
std::size_t getNodesAfter(Ledger const &ledger, ID const &ledgerID)
Count the number of current trusted validators working on a ledger after the specified one.
Adaptor const & adaptor() const
Return the adaptor instance.
std::optional< std::pair< Seq, ID > > getPreferred(Ledger const &curr)
Return the sequence number and ID of the preferred working ledger.
Validator keys and manifest as set in configuration file.
T duration_cast(T... args)
T emplace_back(T... args)
T emplace(T... args)
T empty(T... args)
T insert(T... args)
T lock(T... args)
T make_shared(T... args)
T make_unique(T... args)
T max(T... args)
constexpr Zero kZero
Definition Zero.h:30
STL namespace.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
constexpr FlagValue tfInnerBatchTxn
Definition TxFlags.h:44
void handleNewValidation(Application &app, std::shared_ptr< STValidation > const &val, std::string const &source, BypassAccept const bypassAccept, std::optional< beast::Journal > j)
Handle a new validation.
std::chrono::time_point< Clock, Duration > effCloseTime(std::chrono::time_point< Clock, Duration > closeTime, std::chrono::duration< Rep, Period > resolution, std::chrono::time_point< Clock, Duration > priorCloseTime)
Calculate the effective ledger close time.
ConsensusMode
Represents how a node currently participates in Consensus.
@ WrongLedger
We have the wrong ledger and are attempting to acquire it.
@ Proposing
We are normal participant in consensus and propose our position.
@ Observing
We are observing peer positions, but not proposing our position.
std::uint32_t LedgerIndex
A ledger index.
Definition Protocol.h:409
Rules makeRulesGivenLedger(DigestAwareReadView const &ledger, Rules const &current)
Definition ReadView.cpp:62
UInt256 proposalUniqueId(UInt256 const &proposeHash, UInt256 const &previousLedger, std::uint32_t proposeSeq, NetClock::time_point closeTime, Slice const &publicKey, Slice const &signature)
Calculate a unique identifier for a signed proposal.
std::string toBase58(AccountID const &v)
Convert AccountID to base58 checked string.
Definition AccountID.cpp:95
CsprngEngine & cryptoPrng()
The default cryptographically secure PRNG.
UInt256 LedgerHash
std::string to_string(BaseUInt< Bits, Tag > const &a)
Definition base_uint.h:658
@ MovedOn
The network has consensus without us.
@ Yes
We have consensus along with the network.
Slice makeSlice(std::array< T, N > const &a)
Definition Slice.h:228
XRPL_NO_SANITIZE_ADDRESS void rethrow()
Rethrow the exception currently being handled.
Definition contract.h:36
BaseUInt< 256 > UInt256
Definition base_uint.h:581
boost::intrusive_ptr< SHAMapItem > makeShamapitem(UInt256 const &tag, Slice data)
Definition SHAMapItem.h:148
std::chrono::seconds medianCloseOffset(ConsensusCloseTimes const &times)
Offset of the network's close time relative to ours, using a weighted median.
@ JtAccept
Definition Job.h:59
@ JtAdvance
Definition Job.h:53
bool after(NetClock::time_point now, std::uint32_t mark)
Has the specified time passed?
Definition View.cpp:668
Validations< RCLValidationsAdaptor > RCLValidations
Alias for RCL-specific instantiation of generic Validations.
Integral randInt(Engine &engine, Integral min, Integral max)
Return a uniformly distributed random integer.
Buffer signDigest(PublicKey const &pk, SecretKey const &sk, UInt256 const &digest)
Generate a signature for a message digest.
std::unordered_set< Value, Hash, Pred, Allocator > HashSet
std::shared_ptr< Ledger > buildLedger(std::shared_ptr< Ledger const > const &parent, NetClock::time_point closeTime, bool const closeTimeCorrect, NetClock::duration closeResolution, Application &app, CanonicalTXSet &txns, std::set< TxID > &failedTxs, beast::Journal j)
Build a new ledger by applying consensus transactions.
@ TapNone
Definition ApplyView.h:28
@ CONNECTED
convinced we are talking to the network
Definition NetworkOPs.h:62
@ FULL
we have the ledger and can even validate
Definition NetworkOPs.h:65
bool isPseudoTx(STObject const &tx)
Check whether a transaction is a pseudo-transaction.
Definition STTx.cpp:889
Sha512HalfHasher::result_type sha512Half(Args const &... args)
Returns the SHA512-Half of a series of objects.
Definition digest.h:215
T push_back(T... args)
T setfill(T... args)
T setw(T... args)
T str(T... args)
Stores the set of initial close times.
ProposalT position
Our proposed position on transactions/close time.
ConsensusTimer roundTime
TxSetT txns
The set of transactions consensus agrees go in the ledger.
Sends a message to all peers.
Definition predicates.h:15
T to_string(T... args)
T what(T... args)