1#include <xrpld/app/ledger/LedgerHistory.h>
3#include <xrpld/app/ledger/LedgerPersistence.h>
4#include <xrpld/app/ledger/LedgerToJson.h>
5#include <xrpld/app/main/Application.h>
6#include <xrpld/core/Config.h>
8#include <xrpl/basics/Log.h>
9#include <xrpl/basics/base_uint.h>
10#include <xrpl/basics/chrono.h>
11#include <xrpl/basics/contract.h>
12#include <xrpl/beast/insight/Collector.h>
13#include <xrpl/beast/utility/Journal.h>
14#include <xrpl/beast/utility/instrumentation.h>
15#include <xrpl/json/json_value.h>
16#include <xrpl/json/json_writer.h>
17#include <xrpl/ledger/Ledger.h>
18#include <xrpl/ledger/ReadView.h>
19#include <xrpl/protocol/Protocol.h>
20#include <xrpl/protocol/RippleLedgerHash.h>
21#include <xrpl/protocol/Rules.h>
22#include <xrpl/protocol/TxMeta.h>
23#include <xrpl/shamap/SHAMapItem.h>
41 std::chrono::minutes{5},
43 app_.getJournal(
"TaggedCache"))
44 ,
j_(app.getJournal(
"LedgerHistory"))
52 app_.getJournal(
"TaggedCache"));
58 if (!ledger->isImmutable())
62 ledger->stateMap().getHash().isNonZero(),
"xrpl::LedgerHistory::insert : nonzero hash");
65 bool const alreadyHad =
66 lockedMaps->byHash->canonicalizeReplaceCache(ledger->header().hash, ledger);
68 lockedMaps->byIndex[ledger->header().seq] = ledger->header().hash;
77 if (
auto const it = lockedMaps->byIndex.find(index); it != lockedMaps->byIndex.end())
88 if (
auto const it = lockedMaps->byIndex.find(index); it != lockedMaps->byIndex.end())
95 Rules const rules{
app_.config().features};
96 Fees const fees =
app_.config().fees.toFees();
103 ret->header().seq == index,
"xrpl::LedgerHistory::getLedgerBySeq : result sequence match");
109 ret->isImmutable(),
"xrpl::LedgerHistory::getLedgerBySeq : immutable result ledger");
110 lockedMaps->byHash->canonicalizeReplaceClient(ret->header().hash, ret);
111 lockedMaps->byIndex[ret->header().seq] = ret->header().hash;
112 return (ret->header().seq == index) ? ret :
nullptr;
122 ret = lockedMaps->byHash->fetch(hash);
129 "xrpl::LedgerHistory::getLedgerByHash : immutable fetched "
132 ret->header().hash == hash,
133 "xrpl::LedgerHistory::getLedgerByHash : fetched ledger hash "
138 Rules const rules{
app_.config().features};
139 Fees const fees =
app_.config().fees.toFees();
146 ret->isImmutable(),
"xrpl::LedgerHistory::getLedgerByHash : immutable loaded ledger");
148 ret->header().hash == hash,
149 "xrpl::LedgerHistory::getLedgerByHash : loaded ledger hash match");
152 lockedMaps->byHash->canonicalizeReplaceClient(ret->header().hash, ret);
155 ret->header().hash == hash,
"xrpl::LedgerHistory::getLedgerByHash : result hash match");
178 if (metaData !=
nullptr)
180 JLOG(j.
debug()) <<
"MISMATCH on TX " << tx <<
": " << msg
181 <<
" is missing this transaction:\n"
186 JLOG(j.
debug()) <<
"MISMATCH on TX " << tx <<
": " << msg
187 <<
" is missing this transaction.";
205 auto validMetaData = getMeta(validLedger, tx);
206 auto builtMetaData = getMeta(builtLedger, tx);
209 validMetaData || builtMetaData,
"xrpl::logMetadataDifference : some metadata present");
211 if (validMetaData && builtMetaData)
213 auto const& validNodes = validMetaData->getNodes();
214 auto const& builtNodes = builtMetaData->getNodes();
216 bool const resultDiff = validMetaData->getResultTER() != builtMetaData->getResultTER();
218 bool const indexDiff = validMetaData->getIndex() != builtMetaData->getIndex();
220 bool const nodesDiff = validNodes != builtNodes;
222 if (!resultDiff && !indexDiff && !nodesDiff)
224 JLOG(j.
error()) <<
"MISMATCH on TX " << tx <<
": No apparent mismatches detected!";
230 if (resultDiff && indexDiff)
232 JLOG(j.
debug()) <<
"MISMATCH on TX " << tx <<
": Different result and index!";
233 JLOG(j.
debug()) <<
" Built:"
234 <<
" Result: " << builtMetaData->getResult()
235 <<
" Index: " << builtMetaData->getIndex();
236 JLOG(j.
debug()) <<
" Valid:"
237 <<
" Result: " << validMetaData->getResult()
238 <<
" Index: " << validMetaData->getIndex();
242 JLOG(j.
debug()) <<
"MISMATCH on TX " << tx <<
": Different result!";
243 JLOG(j.
debug()) <<
" Built:"
244 <<
" Result: " << builtMetaData->getResult();
245 JLOG(j.
debug()) <<
" Valid:"
246 <<
" Result: " << validMetaData->getResult();
250 JLOG(j.
debug()) <<
"MISMATCH on TX " << tx <<
": Different index!";
251 JLOG(j.
debug()) <<
" Built:"
252 <<
" Index: " << builtMetaData->getIndex();
253 JLOG(j.
debug()) <<
" Valid:"
254 <<
" Index: " << validMetaData->getIndex();
259 if (resultDiff && indexDiff)
261 JLOG(j.
debug()) <<
"MISMATCH on TX " << tx
262 <<
": Different result, index and nodes!";
268 JLOG(j.
debug()) <<
"MISMATCH on TX " << tx <<
": Different result and nodes!";
269 JLOG(j.
debug()) <<
" Built:"
270 <<
" Result: " << builtMetaData->getResult() <<
" Nodes:\n"
272 JLOG(j.
debug()) <<
" Valid:"
273 <<
" Result: " << validMetaData->getResult() <<
" Nodes:\n"
278 JLOG(j.
debug()) <<
"MISMATCH on TX " << tx <<
": Different index and nodes!";
279 JLOG(j.
debug()) <<
" Built:"
280 <<
" Index: " << builtMetaData->getIndex() <<
" Nodes:\n"
282 JLOG(j.
debug()) <<
" Valid:"
283 <<
" Index: " << validMetaData->getIndex() <<
" Nodes:\n"
288 JLOG(j.
debug()) <<
"MISMATCH on TX " << tx <<
": Different nodes!";
289 JLOG(j.
debug()) <<
" Built:"
292 JLOG(j.
debug()) <<
" Valid:"
303 JLOG(j.
error()) <<
"MISMATCH on TX " << tx <<
": Metadata Difference. Valid=\n"
309 JLOG(j.
error()) <<
"MISMATCH on TX " << tx <<
": Metadata Difference. Built=\n"
321 for (
auto const& item : sm)
336 XRPL_ASSERT(built !=
valid,
"xrpl::LedgerHistory::handleMismatch : unequal hashes");
344 JLOG(
j_.error()) <<
"MISMATCH cannot be analyzed:"
351 builtLedger->header().seq == validLedger->header().seq,
352 "xrpl::LedgerHistory::handleMismatch : sequence match");
354 if (
auto const stream =
j_.debug())
357 stream <<
"Valid: " <<
getJson({*validLedger, {}});
358 stream <<
"Consensus: " << consensus;
365 if (
builtLedger->header().parentHash != validLedger->header().parentHash)
367 JLOG(
j_.error()) <<
"MISMATCH on prior ledger";
372 if (
builtLedger->header().closeTime != validLedger->header().closeTime)
374 JLOG(
j_.error()) <<
"MISMATCH on close time";
378 if (builtConsensusHash && validatedConsensusHash)
380 if (builtConsensusHash != validatedConsensusHash)
382 JLOG(
j_.error()) <<
"MISMATCH on consensus transaction set "
383 <<
" built: " <<
to_string(*builtConsensusHash)
384 <<
" validated: " <<
to_string(*validatedConsensusHash);
388 JLOG(
j_.error()) <<
"MISMATCH with same consensus transaction set: "
395 auto const validTx =
leaves(validLedger->txMap());
397 if (builtTx == validTx)
399 JLOG(
j_.error()) <<
"MISMATCH with same " << builtTx.size() <<
" transactions";
403 JLOG(
j_.error()) <<
"MISMATCH with " << builtTx.size() <<
" built and " << validTx.size()
404 <<
" valid transactions.";
408 JLOG(
j_.error()) <<
"valid\n" <<
getJson({*validLedger, {}});
411 auto b = builtTx.
begin();
412 auto v = validTx.begin();
413 while (b != builtTx.end() && v != validTx.end())
415 if ((*b)->key() < (*v)->key())
420 else if ((*b)->key() > (*v)->key())
422 logOne(*validLedger, (*v)->key(),
"built",
j_);
427 if ((*b)->slice() != (*v)->slice())
436 for (; b != builtTx.end(); ++b)
438 for (; v != validTx.end(); ++v)
439 logOne(*validLedger, (*v)->key(),
"built",
j_);
449 LedgerHash const hash = ledger->header().hash;
450 XRPL_ASSERT(!hash.
isZero(),
"xrpl::LedgerHistory::builtLedger : nonzero hash");
455 if (entry.validated && !entry.built)
458 entry.validated.value() != hash,
459 "xrpl::LedgerHistory::builtLedger : validated-first mismatch");
460 if (entry.validated.value() != hash)
462 JLOG(
j_.error()) <<
"MISMATCH: seq=" << index
463 <<
" validated:" << entry.validated.value() <<
" then:" << hash;
465 .otherHash = entry.validated.value(),
466 .otherConsensusHash = entry.validatedConsensusHash,
467 .consensus = consensus,
473 JLOG(
j_.debug()) <<
"MATCH: seq=" << index <<
" late";
477 entry.built.emplace(hash);
478 entry.builtConsensusHash.emplace(consensusHash);
479 entry.consensus.emplace(std::move(consensus));
499 LedgerHash const hash = ledger->header().hash;
500 XRPL_ASSERT(!hash.
isZero(),
"xrpl::LedgerHistory::validatedLedger : nonzero hash");
505 if (entry.built && !entry.validated)
508 entry.consensus.has_value(),
509 "xrpl::LedgerHistory::validatedLedger : consensus set when built set");
511 entry.built.value() != hash,
512 "xrpl::LedgerHistory::validatedLedger : built-first mismatch");
513 if (entry.built.value() != hash)
515 JLOG(
j_.error()) <<
"MISMATCH: seq=" << index <<
" built:" << entry.built.value()
518 .otherHash = entry.built.value(),
519 .otherConsensusHash = entry.builtConsensusHash,
521 .consensus = entry.consensus.value(),
527 JLOG(
j_.debug()) <<
"MATCH: seq=" << index;
531 entry.validated.emplace(hash);
532 entry.validatedConsensusHash = consensusHash;
553 if (
auto const it = lockedMaps->byIndex.find(ledgerIndex); it != lockedMaps->byIndex.end())
555 if (it->second != ledgerHash)
557 it->second = ledgerHash;
576 auto const*
const byHash = [
this] {
578 return lockedMaps->byHash.get();
585 if (!ledger || ledger->header().seq < seq)
588 lockedMaps->byHash->del(it,
false);
595 cacheSize = lockedMaps->byHash->size();
597 auto& byIndex = lockedMaps->byIndex;
599 byIndex.erase(byIndex.begin(), byIndex.lower_bound(seq));
600 indexSize = byIndex.size();
601 indexesCleared = sizeBefore - indexSize;
604 byIndex.empty() || byIndex.begin()->first >= seq,
605 "xrpl::LedgerHistory::clearLedgerCachePrior : byIndex pruned to seq");
608 JLOG(
j_.debug()) <<
"LedgersByHash: cleared " << hashesCleared <<
" entries before seq " << seq
609 <<
" (total now " << cacheSize <<
")";
611 JLOG(
j_.debug()) <<
"LedgersByIndex: cleared " << indexesCleared <<
" index entries before seq "
612 << seq <<
" (total now " << indexSize <<
")";
A generic endpoint for log messages.
std::shared_ptr< Collector > Ptr
const_iterator begin() const
xrpl::Mutex< LedgerMaps > ledgerMaps_
ConsensusValidated consensusValidated_
beast::insight::Collector::Ptr collector_
bool insert(std::shared_ptr< Ledger const > const &ledger, bool validated)
Track a ledger.
LedgerHash getLedgerHash(LedgerIndex ledgerIndex)
Get a ledger's hash given its sequence number.
void handleMismatch(LedgerHash const &built, LedgerHash const &valid, std::optional< UInt256 > const &builtConsensusHash, std::optional< UInt256 > const &validatedConsensusHash, json::Value const &consensus)
Log details in the case where we build one ledger but validate a different one.
bool fixIndex(LedgerIndex ledgerIndex, LedgerHash const &ledgerHash)
Repair a hash to index mapping.
std::shared_ptr< Ledger const > getLedgerByHash(LedgerHash const &ledgerHash)
Retrieve a ledger given its hash.
void builtLedger(std::shared_ptr< Ledger const > const &, UInt256 const &consensusHash, json::Value)
Report that we have locally built a particular ledger.
LedgerHistory(beast::insight::Collector::Ptr const &collector, Application &app)
beast::insight::Counter mismatchCounter_
std::shared_ptr< Ledger const > getLedgerBySeq(LedgerIndex ledgerIndex)
Get a ledger given its sequence number.
void validatedLedger(std::shared_ptr< Ledger const > const &, std::optional< UInt256 > const &consensusHash)
Report that we have validated a particular ledger.
void clearLedgerCachePrior(LedgerIndex seq)
LedgerIndex seq() const
Returns the sequence number of the base ledger.
virtual TxType txRead(key_type const &key) const =0
Read a transaction from the tx map.
Rules controlling protocol behavior.
UInt256 const & key() const
TER valid(STTx const &tx, ReadView const &view, AccountID const &src, beast::Journal j)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
std::uint32_t LedgerIndex
A ledger index.
Stopwatch & stopwatch()
Returns an instance of a wall clock.
static void logOne(ReadView const &ledger, UInt256 const &tx, char const *msg, beast::Journal const &j)
std::shared_ptr< Ledger > loadByIndex(std::uint32_t ledgerIndex, Rules const &rules, Fees const &fees, ServiceRegistry ®istry, bool acquire)
Load a ledger by its sequence number.
std::string to_string(BaseUInt< Bits, Tag > const &a)
void logicError(std::string const &how) noexcept
Called when faulty logic causes a broken invariant.
std::shared_ptr< Ledger > loadByHash(UInt256 const &ledgerHash, Rules const &rules, Fees const &fees, ServiceRegistry ®istry, bool acquire)
Load a ledger by its hash.
static std::vector< SHAMapItem const * > leaves(SHAMap const &sm)
json::Value getJson(LedgerFill const &fill)
Return a new json::Value representing the ledger with given options.
static void logMetadataDifference(ReadView const &builtLedger, ReadView const &validLedger, UInt256 const &tx, beast::Journal j)
RCLValidatedLedger::Seq mismatch(RCLValidatedLedger const &a, RCLValidatedLedger const &b)
Reflects the fee settings for a particular ledger.