xrpld
Loading...
Searching...
No Matches
InboundLedger.cpp
1#include <xrpld/app/ledger/InboundLedger.h>
2
3#include <xrpld/app/ledger/AccountStateSF.h>
4#include <xrpld/app/ledger/InboundLedgers.h>
5#include <xrpld/app/ledger/LedgerMaster.h>
6#include <xrpld/app/ledger/LedgerNodeHelpers.h>
7#include <xrpld/app/ledger/TransactionStateSF.h>
8#include <xrpld/app/ledger/detail/TimeoutCounter.h>
9#include <xrpld/app/main/Application.h>
10#include <xrpld/overlay/Message.h>
11#include <xrpld/overlay/Overlay.h>
12#include <xrpld/overlay/PeerSet.h>
13
14#include <xrpl/basics/Blob.h>
15#include <xrpl/basics/Log.h>
16#include <xrpl/basics/Slice.h>
17#include <xrpl/basics/base_uint.h>
18#include <xrpl/beast/utility/instrumentation.h>
19#include <xrpl/core/Job.h>
20#include <xrpl/core/JobQueue.h>
21#include <xrpl/json/json_value.h>
22#include <xrpl/ledger/entries/FeeSettingsEntry.h>
23#include <xrpl/nodestore/Database.h>
24#include <xrpl/nodestore/NodeObject.h>
25#include <xrpl/protocol/HashPrefix.h>
26#include <xrpl/protocol/Indexes.h> // IWYU pragma: keep
27#include <xrpl/protocol/LedgerHeader.h>
28#include <xrpl/protocol/Rules.h>
29#include <xrpl/protocol/Serializer.h>
30#include <xrpl/protocol/SystemParameters.h> // IWYU pragma: keep
31#include <xrpl/protocol/jss.h>
32#include <xrpl/resource/Fees.h>
33#include <xrpl/shamap/SHAMapNodeID.h>
34#include <xrpl/shamap/SHAMapSyncFilter.h>
35
36#include <boost/iterator/function_output_iterator.hpp>
37
38#include <xrpl.pb.h>
39
40#include <algorithm>
41#include <chrono>
42#include <cstddef>
43#include <cstdint>
44#include <exception>
45#include <memory>
46#include <mutex>
47#include <random>
48#include <sstream>
49#include <string>
50#include <string_view>
51#include <tuple>
52#include <unordered_map>
53#include <utility>
54#include <vector>
55
56namespace xrpl {
57
58static constexpr auto kPeerCountStart = 5; // Number of peers to start with
59static constexpr auto kPeerCountAdd = 3; // Number of peers to add on a timeout
60static constexpr auto kLedgerTimeoutRetriesMax = 6; // how many timeouts before we give up
61static constexpr auto kLedgerBecomeAggressiveThreshold =
62 4; // how many timeouts before we get aggressive
63static constexpr auto kMissingNodesFind = 256; // Number of nodes to find initially
64static constexpr auto kReqNodesReply = 128; // Number of nodes to request for a reply
65static constexpr auto kReqNodes = 12; // Number of nodes to request blindly
66
68 Application& app,
69 UInt256 const& hash,
70 std::uint32_t seq,
71 Reason reason,
72 ClockType& clock,
74 std::chrono::milliseconds retryInterval)
76 app,
77 hash,
78 retryInterval,
79 {.jobType = JtLedgerData, .jobName = "InboundLedger", .jobLimit = 5},
80 app.getJournal("InboundLedger"))
81 , clock_(clock)
82 , seq_(seq)
83 , reason_(reason)
84 , peerSet_(std::move(peerSet))
85{
86 JLOG(journal_.trace()) << "Acquiring ledger " << hash_;
87 touch();
88}
89
90void
92{
94 collectionLock.unlock();
95
96 tryDB(app_.getNodeFamily().db());
97 // done() is what wakes whatever is waiting and records the hash in recentFailures_.
98 if (failed_)
99 {
100 done();
101 return;
102 }
103
104 if (!complete_)
105 {
106 addPeers();
107 queueJob(sl);
108 return;
109 }
110
111 JLOG(journal_.debug()) << "Acquiring ledger we already have in "
112 << " local store. " << hash_;
113 XRPL_ASSERT(
115 "xrpl::InboundLedger::init : valid ledger fees");
116 // tryDB() verified both maps before setting complete_ and mtx_ has been held since, so
117 // nothing can have invalidated them.
118 if (!ledger_->setImmutable())
119 {
120 // LCOV_EXCL_START
121 // Recorded before the UNREACHABLE, which is not guaranteed to stop here.
122 // Withdrawn as well as failed, to match done() and TransactionAcquire::done(),
123 // for a caller that checks complete_ before failed_.
124 complete_ = false;
125 failed_ = true;
126 done();
127 UNREACHABLE("xrpl::InboundLedger::init : map is invalid");
128 return;
129 // LCOV_EXCL_STOP
130 }
131
133 return;
134
135 app_.getLedgerMaster().storeLedger(ledger_);
136
137 // Check if this could be a newer fully-validated ledger
139 app_.getLedgerMaster().checkAccept(ledger_);
140}
141
144{
145 auto const& peerIds = peerSet_->getPeerIds();
147 peerIds, [this](auto id) { return (app_.getOverlay().findPeerByShortID(id) != nullptr); });
148}
149
150void
152{
153 ScopedLockType const sl(mtx_);
154
155 // If we didn't know the sequence number, but now do, save it
156 if ((seq != 0) && (seq_ == 0))
157 seq_ = seq;
158
159 // Prevent this from being swept
160 touch();
161}
162
163bool
165{
166 ScopedLockType const sl(mtx_);
167 if (!isDone())
168 {
169 if (ledger_)
170 {
171 tryDB(ledger_->stateMap().family().db());
172 }
173 else
174 {
175 tryDB(app_.getNodeFamily().db());
176 }
177 if (failed_ || complete_)
178 {
179 done();
180 return true;
181 }
182 }
183 return false;
184}
185
187{
188 // Save any received AS data not processed. It could be useful
189 // for populating a different ledger
190 for (auto const& entry : receivedData_)
191 {
192 if (entry.second->type() == protocol::liAS_NODE)
193 app_.getInboundLedgers().gotStaleData(entry.second);
194 }
195 if (!isDone())
196 {
197 JLOG(journal_.debug()) << "Acquire " << hash_ << " abort "
198 << ((timeouts_ == 0) ? std::string()
199 : (std::string("timeouts:") +
201 << stats_.get();
202 }
203}
204
206neededHashes(UInt256 const& root, SHAMap& map, int max, SHAMapSyncFilter const* filter)
207{
209
210 if (!root.isZero())
211 {
212 if (map.getHash().isZero())
213 {
214 ret.push_back(root);
215 }
216 else
217 {
218 auto const mn = map.getMissingNodes(max, filter);
219 ret.reserve(mn.size());
220 for (auto const& n : mn)
221 ret.push_back(n.second);
222 }
223 }
224
225 return ret;
226}
227
230{
231 return neededHashes(ledger_->header().txHash, ledger_->txMap(), max, filter);
232}
233
236{
237 return neededHashes(ledger_->header().accountHash, ledger_->stateMap(), max, filter);
238}
239
240// See how much of the ledger data is stored locally
241// Data found in a fetch pack will be stored
242void
244{
245 if (!haveHeader_)
246 {
247 auto const makeLedger = [&, this](Blob const& data) {
248 JLOG(journal_.trace()) << "Ledger header found in fetch pack";
249 Rules const rules{app_.config().features};
251 deserializePrefixedHeader(makeSlice(data)), rules, app_.getNodeFamily());
252 if (ledger_->header().hash != hash_ || (seq_ != 0 && seq_ != ledger_->header().seq))
253 {
254 // We know for a fact the ledger can never be acquired
255 JLOG(journal_.warn())
256 << "hash " << hash_ << " seq " << std::to_string(seq_) << " cannot be a ledger";
257 ledger_.reset();
258 failed_ = true;
259 return;
260 }
261
262 // A zero account hash cannot be a ledger either. The helper sets failed_ and drops the
263 // ledger, so the callers below store and publish nothing for such a header.
265 };
266
267 // Try to fetch the ledger header from the DB
268 if (auto const nodeObject = srcDB.fetchNodeObject(hash_, seq_))
269 {
270 JLOG(journal_.trace()) << "Ledger header found in local store";
271
272 makeLedger(nodeObject->getData());
273 if (failed_)
274 return;
275
276 // Store the ledger header if the source and destination differ
277 auto& dstDB{ledger_->stateMap().family().db()};
278 if (std::addressof(dstDB) != std::addressof(srcDB))
279 {
280 Blob blob{nodeObject->getData()};
281 dstDB.store(NodeObjectType::Ledger, std::move(blob), hash_, ledger_->header().seq);
282 }
283 }
284 else
285 {
286 // Try to fetch the ledger header from a fetch pack
287 auto data = app_.getLedgerMaster().getFetchPack(hash_);
288 if (!data)
289 return;
290
291 JLOG(journal_.trace()) << "Ledger header found in fetch pack";
292
293 makeLedger(*data);
294 if (failed_)
295 return;
296
297 // Store the ledger header in the ledger's database
298 ledger_->stateMap().family().db().store(
299 NodeObjectType::Ledger, std::move(*data), hash_, ledger_->header().seq);
300 }
301
302 if (seq_ == 0)
303 seq_ = ledger_->header().seq;
304 ledger_->stateMap().setLedgerSeq(seq_);
305 ledger_->txMap().setLedgerSeq(seq_);
306 haveHeader_ = true;
307 }
308
310 {
311 if (ledger_->header().txHash.isZero())
312 {
313 JLOG(journal_.trace()) << "No TXNs to fetch";
314 haveTransactions_ = true;
315 }
316 else
317 {
318 TransactionStateSF filter(ledger_->txMap().family().db(), app_.getLedgerMaster());
319 if (ledger_->txMap().fetchRoot(SHAMapHash{ledger_->header().txHash}, &filter))
320 {
321 if (neededTxHashes(1, &filter).empty())
322 {
323 JLOG(journal_.trace()) << "Had full txn map locally";
324 haveTransactions_ = true;
325 }
326 }
327 }
328 }
329
330 if (!haveState_)
331 {
332 AccountStateSF filter(ledger_->stateMap().family().db(), app_.getLedgerMaster());
333 if (ledger_->stateMap().fetchRoot(SHAMapHash{ledger_->header().accountHash}, &filter))
334 {
335 if (neededStateHashes(1, &filter).empty())
336 {
337 JLOG(journal_.trace()) << "Had full AS map locally";
338 haveState_ = true;
339 }
340 }
341 }
342
344 {
345 XRPL_ASSERT(
347 "xrpl::InboundLedger::tryDB : valid ledger fees");
348 // Settled before complete_ is published, so a caller that reads the flag never sees a
349 // ledger this function has not finished with.
350 if (!ledger_->setImmutable())
351 {
352 JLOG(journal_.warn()) << "Ledger " << hash_ << " found locally is invalid";
353 failed_ = true;
354 return;
355 }
356
357 JLOG(journal_.debug()) << "Had everything locally";
358 complete_ = true;
359 }
360}
361
365void
367{
368 recentNodes_.clear();
369
370 if (isDone())
371 {
372 JLOG(journal_.info()) << "Already done " << hash_;
373 return;
374 }
375
377 {
378 if (seq_ != 0)
379 {
380 JLOG(journal_.warn()) << timeouts_ << " timeouts for ledger " << seq_;
381 }
382 else
383 {
384 JLOG(journal_.warn()) << timeouts_ << " timeouts for ledger " << hash_;
385 }
386 failed_ = true;
387 done();
388 return;
389 }
390
391 if (!wasProgress)
392 {
393 checkLocal();
394
395 byHash_ = true;
396
397 std::size_t const pc = getPeerCount();
398 JLOG(journal_.debug()) << "No progress(" << pc << ") for ledger " << hash_;
399
400 // addPeers triggers if the reason is not HISTORY
401 // So if the reason IS HISTORY, need to trigger after we add
402 // otherwise, we need to trigger before we add
403 // so each peer gets triggered once
406 addPeers();
409 }
410}
411
415void
417{
418 peerSet_->addPeers(
420 [this](auto peer) { return peer->hasLedger(hash_, seq_); },
421 [this](auto peer) {
422 // For historical nodes, do not trigger too soon
423 // since a fetch pack is probably coming
426 });
427}
428
434
435void
437{
438 if (signaled_)
439 return;
440
441 signaled_ = true;
442 touch();
443
444 // Settled before the outcome below is logged or acted on, so nothing reports a ledger this
445 // function has since refused.
446 if (complete_ && !failed_ && ledger_)
447 {
448 XRPL_ASSERT(
450 "xrpl::InboundLedger::done : valid ledger fees");
451 // Recovers rather than asserting: peer data produces this verdict, so a caller cannot know
452 // its map is still sound. Recovering rather than asserting is therefore the contract.
453 // Best-effort even so: setInvalid() outranks Immutable, so a walk that reaches the verdict
454 // after both maps have been settled leaves an immutable ledger with an invalid map. It
455 // narrows the window rather than closing it.
456 if (!ledger_->setImmutable())
457 {
458 JLOG(journal_.warn()) << "Acquired ledger " << hash_ << " is invalid";
459 // Withdrawn as well as failed, so a caller that already read complete_ - or that checks
460 // it before failed_ - cannot go on treating this ledger as delivered.
461 complete_ = false;
462 failed_ = true;
463 }
464 else
465 {
466 switch (reason_)
467 {
468 case Reason::HISTORY:
469 app_.getInboundLedgers().onLedgerFetched();
470 break;
471 default:
472 app_.getLedgerMaster().storeLedger(ledger_);
473 break;
474 }
475 }
476 }
477
478 JLOG(journal_.debug()) << "Acquire " << hash_ << (failed_ ? " fail " : " ")
479 << ((timeouts_ == 0)
480 ? std::string()
481 : (std::string("timeouts:") + std::to_string(timeouts_) + " "))
482 << stats_.get();
483
484 XRPL_ASSERT(complete_ || failed_, "xrpl::InboundLedger::done : complete or failed");
485
486 // mtx_ is held, so this may only post the work rather than do it.
487 app_.getJobQueue().addJob(JtLedgerData, "AcqDone", [self = shared_from_this()] {
488 if (self->complete_ && !self->failed_)
489 {
490 self->app_.getLedgerMaster().checkAccept(self->getLedger());
491 self->app_.getLedgerMaster().tryAdvance();
492 }
493 else
494 {
495 self->app_.getInboundLedgers().logFailure(self->hash_, self->seq_);
496 }
497 });
498}
499
503void
505{
507
508 if (isDone())
509 {
510 JLOG(journal_.debug()) << "Trigger on ledger: " << hash_ << (complete_ ? " completed" : "")
511 << (failed_ ? " failed" : "");
512 return;
513 }
514
515 if (auto const stream = journal_.debug())
516 {
518 ss << "Trigger acquiring ledger " << hash_;
519 if (peer)
520 ss << " from " << peer;
521
522 if (complete_ || failed_)
523 {
524 ss << " complete=" << complete_ << " failed=" << failed_;
525 }
526 else
527 {
528 ss << " header=" << haveHeader_ << " tx=" << haveTransactions_ << " as=" << haveState_;
529 }
530 stream << ss.str();
531 }
532
533 if (!haveHeader_)
534 {
535 tryDB(app_.getNodeFamily().db());
536 if (failed_)
537 {
538 JLOG(journal_.warn()) << " failed local for " << hash_;
539 done();
540 return;
541 }
542 }
543
544 protocol::TMGetLedger tmGL;
545 tmGL.set_ledgerhash(hash_.begin(), hash_.size());
546
547 if (timeouts_ != 0)
548 {
549 // Be more aggressive if we've timed out at least once
550 tmGL.set_querytype(protocol::qtINDIRECT);
551
553 {
554 auto const need = getNeededHashes();
555
556 if (!need.empty())
557 {
558 protocol::TMGetObjectByHash tmBH;
559 bool typeSet = false;
560 tmBH.set_query(true);
561 tmBH.set_ledgerhash(hash_.begin(), hash_.size());
562 for (auto const& p : need)
563 {
564 JLOG(journal_.debug()) << "Want: " << p.second;
565
566 if (!typeSet)
567 {
568 tmBH.set_type(p.first);
569 typeSet = true;
570 }
571
572 if (p.first == tmBH.type())
573 {
574 protocol::TMIndexedObject* io = tmBH.add_objects();
575 io->set_hash(p.second.begin(), p.second.size());
576 if (seq_ != 0)
577 io->set_ledgerseq(seq_);
578 }
579 }
580
581 auto packet = std::make_shared<Message>(tmBH, protocol::mtGET_OBJECTS);
582 auto const& peerIds = peerSet_->getPeerIds();
583 std::ranges::for_each(peerIds, [this, &packet](auto id) {
584 if (auto const p = app_.getOverlay().findPeerByShortID(id))
585 {
586 byHash_ = false;
587 p->send(packet);
588 }
589 });
590 }
591 else
592 {
593 JLOG(journal_.info()) << "getNeededHashes says acquire is complete";
594 haveHeader_ = true;
595 haveTransactions_ = true;
596 haveState_ = true;
597 complete_ = true;
598 }
599 }
600 }
601
602 // We can't do much without the header data because we don't know the
603 // state or transaction root hashes.
604 if (!haveHeader_ && !failed_)
605 {
606 tmGL.set_itype(protocol::liBASE);
607 if (seq_ != 0)
608 tmGL.set_ledgerseq(seq_);
609 JLOG(journal_.trace()) << "Sending header request to "
610 << (peer ? "selected peer" : "all peers");
611 peerSet_->sendRequest(tmGL, peer);
612 return;
613 }
614
615 if (ledger_)
616 tmGL.set_ledgerseq(ledger_->header().seq);
617
618 if (reason != TriggerReason::Reply)
619 {
620 // If we're querying blind, don't query deep
621 tmGL.set_querydepth(0);
622 }
623 else if (peer && peer->isHighLatency())
624 {
625 // If the peer has high latency, query extra deep
626 tmGL.set_querydepth(2);
627 }
628 else
629 {
630 tmGL.set_querydepth(1);
631 }
632
633 // Get the state data first because it's the most likely to be useful
634 // if we wind up abandoning this fetch.
635 if (haveHeader_ && !haveState_ && !failed_)
636 {
637 XRPL_ASSERT(
638 ledger_,
639 "xrpl::InboundLedger::trigger : non-null ledger to read state "
640 "from");
641
642 if (!ledger_->stateMap().isValid())
643 {
644 failed_ = true;
645 }
646 else if (ledger_->stateMap().getHash().isZero())
647 {
648 // we need the root node
649 tmGL.set_itype(protocol::liAS_NODE);
650 *tmGL.add_nodeids() = SHAMapNodeID().getRawString();
651 JLOG(journal_.trace())
652 << "Sending AS root request to " << (peer ? "selected peer" : "all peers");
653 peerSet_->sendRequest(tmGL, peer);
654 return;
655 }
656 else
657 {
658 AccountStateSF filter(ledger_->stateMap().family().db(), app_.getLedgerMaster());
659
660 // Release the lock while we process the large state map
661 sl.unlock();
662 auto nodes = ledger_->stateMap().getMissingNodes(kMissingNodesFind, &filter);
663 sl.lock();
664
665 // Make sure nothing happened while we released the lock
666 if (!failed_ && !complete_ && !haveState_)
667 {
668 if (nodes.empty())
669 {
670 if (!ledger_->stateMap().isValid())
671 {
672 failed_ = true;
673 }
674 else
675 {
676 haveState_ = true;
677
679 complete_ = true;
680 }
681 }
682 else
683 {
684 filterNodes(nodes, reason);
685
686 if (!nodes.empty())
687 {
688 tmGL.set_itype(protocol::liAS_NODE);
689 for (auto const& id : nodes)
690 {
691 *tmGL.add_nodeids() = id.first.getRawString();
692 }
693
694 JLOG(journal_.trace()) << "Sending AS node request (" << nodes.size()
695 << ") to " << (peer ? "selected peer" : "all peers");
696 peerSet_->sendRequest(tmGL, peer);
697 return;
698 }
699
700 JLOG(journal_.trace()) << "All AS nodes filtered";
701 }
702 }
703 }
704 }
705
707 {
708 XRPL_ASSERT(
709 ledger_,
710 "xrpl::InboundLedger::trigger : non-null ledger to read "
711 "transactions from");
712
713 if (!ledger_->txMap().isValid())
714 {
715 failed_ = true;
716 }
717 else if (ledger_->txMap().getHash().isZero())
718 {
719 // we need the root node
720 tmGL.set_itype(protocol::liTX_NODE);
721 *tmGL.add_nodeids() = SHAMapNodeID().getRawString();
722 JLOG(journal_.trace())
723 << "Sending TX root request to " << (peer ? "selected peer" : "all peers");
724 peerSet_->sendRequest(tmGL, peer);
725 return;
726 }
727 else
728 {
729 TransactionStateSF filter(ledger_->txMap().family().db(), app_.getLedgerMaster());
730
731 auto nodes = ledger_->txMap().getMissingNodes(kMissingNodesFind, &filter);
732
733 if (nodes.empty())
734 {
735 if (!ledger_->txMap().isValid())
736 {
737 failed_ = true;
738 }
739 else
740 {
741 haveTransactions_ = true;
742
743 if (haveState_)
744 complete_ = true;
745 }
746 }
747 else
748 {
749 filterNodes(nodes, reason);
750
751 if (!nodes.empty())
752 {
753 tmGL.set_itype(protocol::liTX_NODE);
754 for (auto const& n : nodes)
755 {
756 *tmGL.add_nodeids() = n.first.getRawString();
757 }
758 JLOG(journal_.trace()) << "Sending TX node request (" << nodes.size() << ") to "
759 << (peer ? "selected peer" : "all peers");
760 peerSet_->sendRequest(tmGL, peer);
761 return;
762 }
763
764 JLOG(journal_.trace()) << "All TX nodes filtered";
765 }
766 }
767 }
768
769 if (complete_ || failed_)
770 {
771 JLOG(journal_.debug()) << "Done:" << (complete_ ? " complete" : "")
772 << (failed_ ? " failed " : " ") << ledger_->header().seq;
773 // Called with mtx_ still held, so the flags done() writes are not written unlocked; mtx_
774 // is recursive, so a caller that already holds it further up is unaffected.
775 done();
776 }
777}
778
779void
782 TriggerReason reason)
783{
784 // Sort nodes so that the ones we haven't recently
785 // requested come before the ones we have.
786 auto const dup = std::ranges::stable_partition(
787 nodes, [this](auto const& item) { return recentNodes_.count(item.second) == 0; });
788
789 // If everything is a duplicate we don't want to send
790 // any query at all except on a timeout where we need
791 // to query everyone:
792 if (dup.begin() == nodes.begin())
793 {
794 JLOG(journal_.trace()) << "filterNodes: all duplicates";
795
796 if (reason != TriggerReason::Timeout)
797 {
798 nodes.clear();
799 return;
800 }
801 }
802 else
803 {
804 JLOG(journal_.trace()) << "filterNodes: pruning duplicates";
805
806 nodes.erase(dup.begin(), dup.end());
807 }
808
809 std::size_t const limit = (reason == TriggerReason::Reply) ? kReqNodesReply : kReqNodes;
810
811 if (nodes.size() > limit)
812 nodes.resize(limit);
813
814 for (auto const& n : nodes)
815 recentNodes_.insert(n.second);
816}
817
818bool
820{
821 bool const zero = ledger_->header().accountHash.isZero();
822 SOMETIMES(zero, "xrpl::InboundLedger::failOnZeroAccountHash : zero account hash");
823 if (!zero)
824 return false;
825
826 JLOG(journal_.warn()) << "We are acquiring a ledger with a zero account hash: " << hash_;
827 failed_ = true;
828 ledger_.reset();
829 return true;
830}
831
840bool
842{
843 JLOG(journal_.trace()) << "got header acquiring ledger " << hash_;
844
845 if (complete_ || failed_ || haveHeader_)
846 return true;
847
848 auto* f = &app_.getNodeFamily();
849 Rules const rules{app_.config().features};
851 if (ledger_->header().hash != hash_ || (seq_ != 0 && seq_ != ledger_->header().seq))
852 {
853 JLOG(journal_.warn()) << "Acquire hash mismatch: " << ledger_->header().hash
854 << "!=" << hash_;
855 ledger_.reset();
856 return false;
857 }
858
859 // The header is the one asked for, so the peer is not charged and the caller reads failed_.
860 // The helper drops the ledger, and haveHeader_ stays false, as tryDB() leaves them.
862 return true;
863
864 if (seq_ == 0)
865 seq_ = ledger_->header().seq;
866 ledger_->stateMap().setLedgerSeq(seq_);
867 ledger_->txMap().setLedgerSeq(seq_);
868 haveHeader_ = true;
869
870 Serializer s(data.size() + 4);
872 s.addRaw(data.data(), data.size());
873 f->db().store(NodeObjectType::Ledger, std::move(s.modData()), hash_, seq_);
874
875 if (ledger_->header().txHash.isZero())
876 haveTransactions_ = true;
877
878 ledger_->txMap().setSynching();
879 ledger_->stateMap().setSynching();
880
881 return true;
882}
883
888void
890 std::shared_ptr<Peer> const& peer,
891 protocol::TMLedgerData const& packet,
892 SHAMapAddNode& san)
893{
894 if (!haveHeader_)
895 {
896 JLOG(journal_.warn()) << "Missing ledger header";
897 san.incInvalid();
898 return;
899 }
900 if (packet.type() == protocol::liTX_NODE)
901 {
903 {
904 san.incDuplicate();
905 return;
906 }
907 }
908 else if (haveState_ || failed_)
909 {
910 san.incDuplicate();
911 return;
912 }
913
914 auto [map, rootHash, filter] =
916 if (packet.type() == protocol::liTX_NODE)
917 {
918 return {
919 ledger_->txMap(),
920 SHAMapHash{ledger_->header().txHash},
922 ledger_->txMap().family().db(), app_.getLedgerMaster())};
923 }
924 return {
925 ledger_->stateMap(),
926 SHAMapHash{ledger_->header().accountHash},
928 ledger_->stateMap().family().db(), app_.getLedgerMaster())};
929 }();
930
931 try
932 {
933 auto const f = filter.get();
934
935 for (auto const& ledgerNode : packet.nodes())
936 {
937 auto treeNode = getTreeNode(ledgerNode.nodedata());
938 if (!treeNode)
939 {
940 JLOG(journal_.warn())
941 << "Got invalid node data for ledger " << hash_ << " from peer " << peer->id();
942 peer->charge(resource::kFeeInvalidData, "ledger_node.node_data invalid");
943 san.incInvalid();
944 return;
945 }
946
947 auto const nodeID = getSHAMapNodeID(ledgerNode, *treeNode);
948 if (!nodeID)
949 {
950 JLOG(journal_.warn())
951 << "Got invalid node id for ledger " << hash_ << " from peer " << peer->id();
952 peer->charge(resource::kFeeInvalidData, "ledger_node.node_id invalid");
953 san.incInvalid();
954 return;
955 }
956
957 auto const result = nodeID->isRoot()
958 ? map.addRootNode(rootHash, std::move(treeNode), f)
959 : map.addKnownNode(*nodeID, std::move(treeNode), f);
960 san += result;
961
962 if (result.isInvalid())
963 {
964 JLOG(journal_.warn()) << "Got invalid node " << *nodeID << " for ledger " << hash_
965 << " from peer " << peer->id();
966 peer->charge(resource::kFeeInvalidData, "ledger_node invalid");
967 return;
968 }
969 }
970 }
971 catch (std::exception const& e)
972 {
973 // If we get here it is not necessarily because the node was bad, so don't charge the peer.
974 JLOG(journal_.error()) << "Could not process node for ledger " << hash_ << " from peer "
975 << peer->id() << ": " << e.what();
976 san.incInvalid();
977 return;
978 }
979
980 if (!map.isSynching())
981 {
982 if (packet.type() == protocol::liTX_NODE)
983 {
984 haveTransactions_ = true;
985 }
986 else
987 {
988 haveState_ = true;
989 }
990
992 {
993 complete_ = true;
994 done();
995 }
996 }
997}
998
1003bool
1005{
1006 if (failed_ || haveState_)
1007 {
1008 san.incDuplicate();
1009 return true;
1010 }
1011
1012 if (!haveHeader_)
1013 {
1014 // LCOV_EXCL_START
1015 UNREACHABLE("xrpl::InboundLedger::takeAsRootNode : no ledger header");
1016 return false;
1017 // LCOV_EXCL_STOP
1018 }
1019
1020 auto treeNode = getTreeNode(data);
1021 if (!treeNode)
1022 {
1023 JLOG(journal_.warn()) << "Got invalid AS root node data for ledger " << hash_;
1024 san.incInvalid();
1025 return false;
1026 }
1027
1028 AccountStateSF filter(ledger_->stateMap().family().db(), app_.getLedgerMaster());
1029 auto const result = ledger_->stateMap().addRootNode(
1030 SHAMapHash{ledger_->header().accountHash}, std::move(treeNode), &filter);
1031 san += result;
1032 return !result.isInvalid();
1033}
1034
1039bool
1041{
1043 {
1044 san.incDuplicate();
1045 return true;
1046 }
1047
1048 if (!haveHeader_)
1049 {
1050 // LCOV_EXCL_START
1051 UNREACHABLE("xrpl::InboundLedger::takeTxRootNode : no ledger header");
1052 return false;
1053 // LCOV_EXCL_STOP
1054 }
1055
1056 auto treeNode = getTreeNode(data);
1057 if (!treeNode)
1058 {
1059 JLOG(journal_.warn()) << "Got invalid TX root node data for ledger " << hash_;
1060 san.incInvalid();
1061 return false;
1062 }
1063
1064 TransactionStateSF filter(ledger_->txMap().family().db(), app_.getLedgerMaster());
1065 auto const result = ledger_->txMap().addRootNode(
1066 SHAMapHash{ledger_->header().txHash}, std::move(treeNode), &filter);
1067 san += result;
1068 return !result.isInvalid();
1069}
1070
1073{
1075
1076 if (!haveHeader_)
1077 {
1078 ret.emplace_back(protocol::TMGetObjectByHash::otLEDGER, hash_);
1079 return ret;
1080 }
1081
1082 if (!haveState_)
1083 {
1084 AccountStateSF filter(ledger_->stateMap().family().db(), app_.getLedgerMaster());
1085 for (auto const& h : neededStateHashes(4, &filter))
1086 {
1087 ret.emplace_back(protocol::TMGetObjectByHash::otSTATE_NODE, h);
1088 }
1089 }
1090
1091 if (!haveTransactions_)
1092 {
1093 TransactionStateSF filter(ledger_->txMap().family().db(), app_.getLedgerMaster());
1094 for (auto const& h : neededTxHashes(4, &filter))
1095 {
1096 ret.emplace_back(protocol::TMGetObjectByHash::otTRANSACTION_NODE, h);
1097 }
1098 }
1099
1100 return ret;
1101}
1102
1107bool
1111{
1113
1114 if (isDone())
1115 return false;
1116
1117 receivedData_.emplace_back(peer, data);
1118
1120 return false;
1121
1122 receiveDispatched_ = true;
1123 return true;
1124}
1125
1130// VFALCO NOTE, it is not necessary to pass the entire Peer,
1131// we can get away with just a resource::Consumer endpoint.
1132//
1133// TODO Change peer to Consumer
1134//
1135int
1136InboundLedger::processData(std::shared_ptr<Peer> peer, protocol::TMLedgerData const& packet)
1137{
1138 if (packet.type() == protocol::liBASE)
1139 {
1140 if (packet.nodes().empty())
1141 {
1142 JLOG(journal_.warn()) << peer->id() << ": empty header data";
1143 peer->charge(resource::kFeeMalformedRequest, "ledger_data empty header");
1144 return -1;
1145 }
1146
1147 SHAMapAddNode san;
1148
1149 ScopedLockType const sl(mtx_);
1150
1151 try
1152 {
1153 if (!haveHeader_)
1154 {
1155 if (!takeHeader(packet.nodes(0).nodedata()))
1156 {
1157 JLOG(journal_.warn()) << "Got invalid header data";
1158 peer->charge(resource::kFeeMalformedRequest, "ledger_data invalid header");
1159 return -1;
1160 }
1161
1162 // takeHeader() failed the acquisition. Nothing else signals that after isDone().
1163 if (failed_)
1164 {
1165 done();
1166 return 0;
1167 }
1168
1169 san.incUseful();
1170 }
1171
1172 if (!haveState_ && (packet.nodes().size() > 1) &&
1173 !takeAsRootNode(packet.nodes(1).nodedata(), san))
1174 {
1175 JLOG(journal_.warn()) << "Included AS root invalid for ledger " << hash_
1176 << " from peer " << peer->id();
1177 if (san.isInvalid())
1178 {
1179 peer->charge(resource::kFeeInvalidData, "ledger_data invalid AS root");
1180 return -1;
1181 }
1182 }
1183
1184 if (!haveTransactions_ && (packet.nodes().size() > 2) &&
1185 !takeTxRootNode(packet.nodes(2).nodedata(), san))
1186 {
1187 JLOG(journal_.warn()) << "Included TX root invalid for ledger " << hash_
1188 << " from peer " << peer->id();
1189 if (san.isInvalid())
1190 {
1191 peer->charge(resource::kFeeInvalidData, "ledger_data invalid TX root");
1192 return -1;
1193 }
1194 }
1195 }
1196 catch (std::exception const& ex)
1197 {
1198 JLOG(journal_.warn()) << "Included AS/TX root invalid for ledger " << hash_
1199 << " from peer " << peer->id() << ": " << ex.what();
1200 using namespace std::string_literals;
1201 peer->charge(resource::kFeeInvalidData, "ledger_data "s + ex.what());
1202 return -1;
1203 }
1204
1205 if (san.isUseful())
1206 progress_ = true;
1207
1208 stats_ += san;
1209 return san.getGood();
1210 }
1211
1212 if ((packet.type() == protocol::liTX_NODE) || (packet.type() == protocol::liAS_NODE))
1213 {
1214 if (packet.nodes().empty())
1215 {
1216 JLOG(journal_.info()) << peer->id() << ": response with no nodes";
1217 peer->charge(resource::kFeeMalformedRequest, "ledger_data no nodes");
1218 return -1;
1219 }
1220
1221 ScopedLockType const sl(mtx_);
1222
1223 SHAMapAddNode san;
1224 receiveNode(peer, packet, san);
1225
1226 JLOG(journal_.debug()) << "Ledger "
1227 << ((packet.type() == protocol::liTX_NODE) ? "TX" : "AS")
1228 << " node stats: " << san.get();
1229
1230 // `san` accumulates across the whole packet, so `isInvalid()` (bad_ > 0) does not mean the
1231 // packet had no useful nodes: credit whatever good/useful nodes were sent rather than
1232 // discarding everything because one node in an otherwise-good packet was bad.
1233 // Note: Peer charges for invalid/malformed data are issued from within receiveNode at the
1234 // exact failure site, so the peer is only charged for problems they are responsible for.
1235 if (san.isUseful())
1236 progress_ = true;
1237
1238 stats_ += san;
1239 return san.getGood();
1240 }
1241
1242 return -1;
1243}
1244
1245namespace detail {
1246// Track the amount of useful data that each peer returns
1248{
1249 // Map from peer to amount of useful the peer returned
1251 // The largest amount of useful data that any peer returned
1252 int maxCount = 0;
1253
1254 // Update the data count for a peer
1255 void
1256 update(std::shared_ptr<Peer>&& peer, int dataCount)
1257 {
1258 if (dataCount <= 0)
1259 return;
1260 maxCount = std::max(maxCount, dataCount);
1261 auto const i = counts.find(peer);
1262 if (i == counts.end())
1263 {
1264 counts.emplace(std::move(peer), dataCount);
1265 return;
1266 }
1267 i->second = std::max(i->second, dataCount);
1268 }
1269
1270 // Prune all the peers that didn't return enough data.
1271 void
1273 {
1274 // Remove all the peers that didn't return at least half as much data as
1275 // the best peer
1276 auto const thresh = maxCount / 2;
1277 auto i = counts.begin();
1278 while (i != counts.end())
1279 {
1280 if (i->second < thresh)
1281 {
1282 i = counts.erase(i);
1283 }
1284 else
1285 {
1286 ++i;
1287 }
1288 }
1289 }
1290
1291 // call F with the `peer` parameter with a random sample of at most n values
1292 // of the counts vector.
1293 template <class F>
1294 void
1296 {
1297 if (counts.empty())
1298 return;
1299
1300 auto const outFunc = [&f](auto&& v) { f(v.first); };
1302#if _MSC_VER
1304 s.reserve(n);
1305 std::sample(counts.begin(), counts.end(), std::back_inserter(s), n, rng);
1306 for (auto& v : s)
1307 {
1308 outFunc(v);
1309 }
1310#else
1312 counts.begin(), counts.end(), boost::make_function_output_iterator(outFunc), n, rng);
1313#endif
1314 }
1315};
1316} // namespace detail
1317
1322void
1324{
1325 // Maximum number of peers to request data from
1326 static constexpr std::size_t kMaxUsefulPeers = 6;
1327
1328 decltype(receivedData_) data;
1329
1330 // Reserve some memory so the first couple iterations don't reallocate
1331 data.reserve(8);
1332
1333 detail::PeerDataCounts dataCounts;
1334
1335 for (;;)
1336 {
1337 data.clear();
1338
1339 {
1341
1342 if (receivedData_.empty())
1343 {
1344 receiveDispatched_ = false;
1345 break;
1346 }
1347
1348 data.swap(receivedData_);
1349 }
1350
1351 for (auto const& entry : data)
1352 {
1353 if (auto peer = entry.first.lock())
1354 {
1355 int const count = processData(peer, *entry.second);
1356 dataCounts.update(std::move(peer), count);
1357 }
1358 }
1359 }
1360
1361 // Select a random sample of the peers that gives us the most nodes that are
1362 // useful
1363 dataCounts.prune();
1364 dataCounts.sampleN(kMaxUsefulPeers, [&](std::shared_ptr<Peer> const& peer) {
1366 });
1367}
1368
1371{
1373
1374 ScopedLockType const sl(mtx_);
1375
1376 ret[jss::hash] = to_string(hash_);
1377
1378 if (complete_)
1379 ret[jss::complete] = true;
1380
1381 if (failed_)
1382 ret[jss::failed] = true;
1383
1384 if (!complete_ && !failed_)
1385 ret[jss::peers] = static_cast<int>(peerSet_->getPeerIds().size());
1386
1387 ret[jss::have_header] = haveHeader_;
1388
1389 if (haveHeader_)
1390 {
1391 ret[jss::have_state] = haveState_;
1392 ret[jss::have_transactions] = haveTransactions_;
1393 }
1394
1395 ret[jss::timeouts] = timeouts_;
1396
1397 if (haveHeader_ && !haveState_)
1398 {
1400 for (auto const& h : neededStateHashes(16, nullptr))
1401 {
1402 hv.append(to_string(h));
1403 }
1404 ret[jss::needed_state_hashes] = hv;
1405 }
1406
1408 {
1410 for (auto const& h : neededTxHashes(16, nullptr))
1411 {
1412 hv.append(to_string(h));
1413 }
1414 ret[jss::needed_transaction_hashes] = hv;
1415 }
1416
1417 return ret;
1418}
1419
1420} // namespace xrpl
T addressof(T... args)
T back_inserter(T... args)
Represents a JSON value.
Definition json_value.h:121
Value & append(Value const &value)
Append value to array at the end.
ValueType type() const
InboundLedger(Application &app, UInt256 const &hash, std::uint32_t seq, Reason reason, ClockType &clock, std::unique_ptr< PeerSet > peerSet, std::chrono::milliseconds retryInterval=kRetryInterval)
json::Value getJson(int)
Return a json::ValueType::Object.
void tryDB(node_store::Database &srcDB)
std::weak_ptr< TimeoutCounter > pmDowncast() override
Return a weak pointer to this.
void trigger(std::shared_ptr< Peer > const &peer, TriggerReason reason)
Ask for more nodes, or judge what has been collected.
void runData()
Process pending TMLedgerData Query the a random sample of the 'best' peers.
void filterNodes(std::vector< std::pair< SHAMapNodeID, UInt256 > > &nodes, TriggerReason reason)
std::size_t getPeerCount() const
bool failOnZeroAccountHash()
Fail the acquisition when the header's account hash is zero.
std::set< UInt256 > recentNodes_
void receiveNode(std::shared_ptr< Peer > const &peer, protocol::TMLedgerData const &packet, SHAMapAddNode &san)
Process node data received from a peer Call with a lock.
std::vector< UInt256 > neededStateHashes(int max, SHAMapSyncFilter const *filter) const
SHAMapAddNode stats_
int processData(std::shared_ptr< Peer > peer, protocol::TMLedgerData const &data)
Process one TMLedgerData Returns the number of useful nodes.
bool takeHeader(std::string_view data)
Build the ledger from a header a peer supplied.
bool takeAsRootNode(std::string_view data, SHAMapAddNode &san)
Process AS root node received from a peer Call with a lock.
std::shared_ptr< Ledger > ledger_
std::vector< std::pair< std::weak_ptr< Peer >, std::shared_ptr< protocol::TMLedgerData > > > receivedData_
std::mutex receivedDataLock_
std::unique_ptr< PeerSet > peerSet_
void addPeers()
Add more peers to the set, if possible.
bool takeTxRootNode(std::string_view data, SHAMapAddNode &san)
Process AS root node received from a peer Call with a lock.
void onTimer(bool progress, ScopedLockType &sl) override
Called with a lock by the PeerSet when the timer expires.
void init(ScopedLockType &collectionLock)
void update(std::uint32_t seq)
void done()
Settle the acquisition and signal whatever is waiting on it.
bool gotData(std::weak_ptr< Peer >, std::shared_ptr< protocol::TMLedgerData > const &)
Stash a TMLedgerData received from a peer for later processing Returns 'true' if we need to dispatch.
std::vector< NeededHashT > getNeededHashes()
beast::AbstractClock< std::chrono::steady_clock > ClockType
std::vector< UInt256 > neededTxHashes(int max, SHAMapSyncFilter const *filter) const
Rules controlling protocol behavior.
Definition Rules.h:40
void incDuplicate()
Record one node that was not needed: the map already held it, or the map or its acquisition was no lo...
void incUseful()
Record one node that produced a good result.
bool isInvalid() const
std::string get() const
Render the tally as a log line.
void incInvalid()
Record one node that was rejected.
bool isUseful() const
bool isZero() const
Definition SHAMapHash.h:36
Identifies a node inside a SHAMap.
std::vector< std::pair< SHAMapNodeID, UInt256 > > getMissingNodes(int maxNodes, SHAMapSyncFilter const *filter)
Check for nodes in the SHAMap not available.
SHAMapHash getHash() const
int addRaw(Blob const &vector)
std::recursive_mutex mtx_
std::unique_lock< std::recursive_mutex > ScopedLockType
UInt256 const hash_
The hash of the object (in practice, always a ledger) we are trying to fetch.
void queueJob(ScopedLockType &)
Queue a job to call invokeOnTimer().
TimeoutCounter(Application &app, UInt256 const &targetHash, std::chrono::milliseconds timeoutInterval, QueueJobParameter &&jobParameter, beast::Journal journal)
bool progress_
Whether forward progress has been made.
beast::Journal journal_
Persistency layer for NodeObject.
Definition Database.h:45
std::shared_ptr< NodeObject > fetchNodeObject(UInt256 const &hash, std::uint32_t ledgerSeq=0, FetchType fetchType=FetchType::Synchronous, bool duplicate=false)
Fetch a node object.
T count_if(T... args)
T emplace_back(T... args)
T for_each(T... args)
T lock(T... args)
T make_shared(T... args)
T make_unique(T... args)
T max(T... args)
@ Array
array value (ordered list)
Definition json_value.h:32
@ Object
object value (collection of name/value pairs).
Definition json_value.h:33
Charge const kFeeMalformedRequest
Schedule of fees charged for imposing load on the server.
Charge const kFeeInvalidData
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
static constexpr auto kReqNodesReply
Number root(Number f, unsigned d)
std::optional< SHAMapNodeID > getSHAMapNodeID(protocol::TMLedgerNode const &ledgerNode, SHAMapTreeNode const &treeNode)
Extracts or reconstructs the SHAMapNodeID from a ledger node proto message.
LedgerHeader deserializeHeader(Slice data, bool hasHash=false)
Deserialize a ledger header from a byte array.
std::string to_string(BaseUInt< Bits, Tag > const &a)
Definition base_uint.h:658
static constexpr std::uint32_t kXrpLedgerEarliestFees
The XRP Ledger mainnet's earliest ledger with a FeeSettings object.
SHAMapTreeNodePtr getTreeNode(std::string_view data)
Deserializes a SHAMapTreeNode from wire format data.
static constexpr auto kMissingNodesFind
Slice makeSlice(std::array< T, N > const &a)
Definition Slice.h:228
static std::vector< UInt256 > neededHashes(UInt256 const &root, SHAMap &map, int max, SHAMapSyncFilter const *filter)
BaseUInt< 256 > UInt256
Definition base_uint.h:581
@ JtLedgerData
Definition Job.h:52
static constexpr auto kLedgerBecomeAggressiveThreshold
static constexpr auto kPeerCountAdd
static constexpr auto kLedgerTimeoutRetriesMax
static constexpr auto kPeerCountStart
@ LedgerMaster
ledger master data for signing
Definition HashPrefix.h:59
FeeSettingsEntry< ReadView > FeeSettingsEntryR
std::vector< unsigned char > Blob
Storage for linear binary data.
Definition Blob.h:11
static constexpr auto kReqNodes
LedgerHeader deserializePrefixedHeader(Slice data, bool hasHash=false)
Deserialize a ledger header (prefixed with 4 bytes) from a byte array.
T push_back(T... args)
T reserve(T... args)
T sample(T... args)
T stable_partition(T... args)
T str(T... args)
std::unordered_map< std::shared_ptr< Peer >, int > counts
void update(std::shared_ptr< Peer > &&peer, int dataCount)
void sampleN(std::size_t n, F &&f)
T to_string(T... args)
T unlock(T... args)
T what(T... args)