20#include <xrpld/app/ledger/LedgerHistory.h>
21#include <xrpld/app/ledger/LedgerToJson.h>
23#include <xrpl/basics/Log.h>
24#include <xrpl/basics/chrono.h>
25#include <xrpl/basics/contract.h>
26#include <xrpl/json/to_string.h>
36 , collector_(collector)
37 , mismatch_counter_(collector->make_counter(
"ledger.history",
"mismatch"))
43 app_.journal(
"TaggedCache"))
44 , m_consensus_validated(
47 std::chrono::minutes{5},
49 app_.journal(
"TaggedCache"))
50 , j_(app.journal(
"LedgerHistory"))
59 if (!ledger->isImmutable())
63 ledger->stateMap().getHash().isNonZero(),
64 "ripple::LedgerHistory::insert : nonzero hash");
67 ledger->info().hash, ledger);
101 ret->info().seq == index,
102 "ripple::LedgerHistory::getLedgerBySeq : result sequence match");
109 "ripple::LedgerHistory::getLedgerBySeq : immutable result ledger");
112 return (ret->info().seq == index) ? ret :
nullptr;
125 "ripple::LedgerHistory::getLedgerByHash : immutable fetched "
128 ret->info().hash == hash,
129 "ripple::LedgerHistory::getLedgerByHash : fetched ledger hash "
141 "ripple::LedgerHistory::getLedgerByHash : immutable loaded ledger");
143 ret->info().hash == hash,
144 "ripple::LedgerHistory::getLedgerByHash : loaded ledger hash match");
147 ret->info().hash == hash,
148 "ripple::LedgerHistory::getLedgerByHash : result hash match");
162 if (metaData !=
nullptr)
164 JLOG(j.
debug()) <<
"MISMATCH on TX " << tx <<
": " << msg
165 <<
" is missing this transaction:\n"
170 JLOG(j.
debug()) <<
"MISMATCH on TX " << tx <<
": " << msg
171 <<
" is missing this transaction.";
189 auto validMetaData = getMeta(validLedger, tx);
190 auto builtMetaData = getMeta(builtLedger, tx);
193 validMetaData || builtMetaData,
194 "ripple::log_metadata_difference : some metadata present");
196 if (validMetaData && builtMetaData)
198 auto const& validNodes = validMetaData->getNodes();
199 auto const& builtNodes = builtMetaData->getNodes();
201 bool const result_diff =
202 validMetaData->getResultTER() != builtMetaData->getResultTER();
204 bool const index_diff =
205 validMetaData->getIndex() != builtMetaData->getIndex();
207 bool const nodes_diff = validNodes != builtNodes;
209 if (!result_diff && !index_diff && !nodes_diff)
211 JLOG(j.
error()) <<
"MISMATCH on TX " << tx
212 <<
": No apparent mismatches detected!";
218 if (result_diff && index_diff)
220 JLOG(j.
debug()) <<
"MISMATCH on TX " << tx
221 <<
": Different result and index!";
222 JLOG(j.
debug()) <<
" Built:"
223 <<
" Result: " << builtMetaData->getResult()
224 <<
" Index: " << builtMetaData->getIndex();
225 JLOG(j.
debug()) <<
" Valid:"
226 <<
" Result: " << validMetaData->getResult()
227 <<
" Index: " << validMetaData->getIndex();
229 else if (result_diff)
232 <<
"MISMATCH on TX " << tx <<
": Different result!";
233 JLOG(j.
debug()) <<
" Built:"
234 <<
" Result: " << builtMetaData->getResult();
235 JLOG(j.
debug()) <<
" Valid:"
236 <<
" Result: " << validMetaData->getResult();
241 <<
"MISMATCH on TX " << tx <<
": Different index!";
242 JLOG(j.
debug()) <<
" Built:"
243 <<
" Index: " << builtMetaData->getIndex();
244 JLOG(j.
debug()) <<
" Valid:"
245 <<
" Index: " << validMetaData->getIndex();
250 if (result_diff && index_diff)
252 JLOG(j.
debug()) <<
"MISMATCH on TX " << tx
253 <<
": Different result, index and nodes!";
254 JLOG(j.
debug()) <<
" Built:\n"
256 JLOG(j.
debug()) <<
" Valid:\n"
259 else if (result_diff)
261 JLOG(j.
debug()) <<
"MISMATCH on TX " << tx
262 <<
": Different result and nodes!";
265 <<
" Result: " << builtMetaData->getResult() <<
" Nodes:\n"
269 <<
" Result: " << validMetaData->getResult() <<
" Nodes:\n"
274 JLOG(j.
debug()) <<
"MISMATCH on TX " << tx
275 <<
": Different index and nodes!";
278 <<
" Index: " << builtMetaData->getIndex() <<
" Nodes:\n"
282 <<
" Index: " << validMetaData->getIndex() <<
" Nodes:\n"
288 <<
"MISMATCH on TX " << tx <<
": Different nodes!";
289 JLOG(j.
debug()) <<
" Built:"
292 JLOG(j.
debug()) <<
" Valid:"
303 JLOG(j.
error()) <<
"MISMATCH on TX " << tx
304 <<
": Metadata Difference. Valid=\n"
310 JLOG(j.
error()) <<
"MISMATCH on TX " << tx
311 <<
": Metadata Difference. Built=\n"
323 for (
auto const& item : sm)
327 return lhs->key() < rhs->key();
342 "ripple::LedgerHistory::handleMismatch : unequal hashes");
350 JLOG(
j_.
error()) <<
"MISMATCH cannot be analyzed:"
351 <<
" builtLedger: " <<
to_string(built) <<
" -> "
353 <<
" -> " << validLedger;
358 builtLedger->info().seq == validLedger->info().seq,
359 "ripple::LedgerHistory::handleMismatch : sequence match");
364 stream <<
"Valid: " <<
getJson({*validLedger, {}});
365 stream <<
"Consensus: " << consensus;
372 if (
builtLedger->info().parentHash != validLedger->info().parentHash)
374 JLOG(
j_.
error()) <<
"MISMATCH on prior ledger";
379 if (
builtLedger->info().closeTime != validLedger->info().closeTime)
381 JLOG(
j_.
error()) <<
"MISMATCH on close time";
385 if (builtConsensusHash && validatedConsensusHash)
387 if (builtConsensusHash != validatedConsensusHash)
389 <<
"MISMATCH on consensus transaction set "
390 <<
" built: " <<
to_string(*builtConsensusHash)
391 <<
" validated: " <<
to_string(*validatedConsensusHash);
393 JLOG(
j_.
error()) <<
"MISMATCH with same consensus transaction set: "
399 auto const validTx =
leaves(validLedger->txMap());
401 if (builtTx == validTx)
402 JLOG(
j_.
error()) <<
"MISMATCH with same " << builtTx.size()
405 JLOG(
j_.
error()) <<
"MISMATCH with " << builtTx.size() <<
" built and "
406 << validTx.size() <<
" valid transactions.";
412 auto b = builtTx.
begin();
413 auto v = validTx.begin();
414 while (b != builtTx.end() && v != validTx.end())
416 if ((*b)->key() < (*v)->key())
421 else if ((*b)->key() > (*v)->key())
423 log_one(*validLedger, (*v)->key(),
"built",
j_);
428 if ((*b)->slice() != (*v)->slice())
438 for (; b != builtTx.end(); ++b)
440 for (; v != validTx.end(); ++v)
441 log_one(*validLedger, (*v)->key(),
"built",
j_);
453 !hash.
isZero(),
"ripple::LedgerHistory::builtLedger : nonzero hash");
455 auto entry = std::make_shared<cv_entry>();
458 if (entry->validated && !entry->built)
460 if (entry->validated.value() != hash)
462 JLOG(
j_.
error()) <<
"MISMATCH: seq=" << index
463 <<
" validated:" << entry->validated.value()
467 entry->validated.value(),
469 entry->validatedConsensusHash,
475 JLOG(
j_.
debug()) <<
"MATCH: seq=" << index <<
" late";
479 entry->built.emplace(hash);
480 entry->builtConsensusHash.emplace(consensusHash);
481 entry->consensus.emplace(std::move(consensus));
493 "ripple::LedgerHistory::validatedLedger : nonzero hash");
495 auto entry = std::make_shared<cv_entry>();
498 if (entry->built && !entry->validated)
500 if (entry->built.value() != hash)
503 <<
"MISMATCH: seq=" << index
504 <<
" built:" << entry->built.value() <<
" then:" << hash;
506 entry->built.value(),
508 entry->builtConsensusHash,
510 entry->consensus.value());
515 JLOG(
j_.
debug()) <<
"MATCH: seq=" << index;
519 entry->validated.emplace(hash);
520 entry->validatedConsensusHash = consensusHash;
530 if (ledger && (it !=
mLedgersByIndex.end()) && (it->second != ledgerHash))
532 it->second = ledgerHash;
544 if (!ledger || ledger->info().seq < seq)
const_iterator begin() const
A generic endpoint for log messages.
LedgerHistory(beast::insight::Collector::ptr const &collector, Application &app)
std::map< LedgerIndex, LedgerHash > mLedgersByIndex
void builtLedger(std::shared_ptr< Ledger const > const &, uint256 const &consensusHash, Json::Value)
Report that we have locally built a particular ledger.
LedgersByHash m_ledgers_by_hash
ConsensusValidated m_consensus_validated
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.
LedgerHash getLedgerHash(LedgerIndex ledgerIndex)
Get a ledger's hash given its sequence number.
void clearLedgerCachePrior(LedgerIndex seq)
std::shared_ptr< Ledger const > getLedgerBySeq(LedgerIndex ledgerIndex)
Get a ledger given its sequence number.
beast::insight::Counter mismatch_counter_
bool insert(std::shared_ptr< Ledger const > const &ledger, bool validated)
Track a ledger.
bool fixIndex(LedgerIndex ledgerIndex, LedgerHash const &ledgerHash)
Repair a hash to index mapping.
void validatedLedger(std::shared_ptr< Ledger const > const &, std::optional< uint256 > const &consensusHash)
Report that we have validated a particular ledger.
std::shared_ptr< Ledger const > getLedgerByHash(LedgerHash const &ledgerHash)
Retrieve a ledger given its hash.
LedgerIndex seq() const
Returns the sequence number of the base ledger.
virtual tx_type txRead(key_type const &key) const =0
Read a transaction from the tx map.
A SHAMap is both a radix tree with a fan-out of 16 and a Merkle tree.
bool canonicalize_replace_client(key_type const &key, SharedPointerType &data)
bool del(key_type const &key, bool valid)
SharedPointerType fetch(key_type const &key)
std::vector< key_type > getKeys() const
bool canonicalize_replace_cache(key_type const &key, SharedPointerType const &data)
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::shared_ptr< Ledger > loadByIndex(std::uint32_t ledgerIndex, Application &app, bool acquire)
static void log_one(ReadView const &ledger, uint256 const &tx, char const *msg, beast::Journal &j)
static std::vector< SHAMapItem const * > leaves(SHAMap const &sm)
std::shared_ptr< Ledger > loadByHash(uint256 const &ledgerHash, Application &app, bool acquire)
Stopwatch & stopwatch()
Returns an instance of a wall clock.
std::string to_string(base_uint< Bits, Tag > const &a)
static void log_metadata_difference(ReadView const &builtLedger, ReadView const &validLedger, uint256 const &tx, beast::Journal j)
Json::Value getJson(LedgerFill const &fill)
Return a new Json::Value representing the ledger with given options.
void LogicError(std::string const &how) noexcept
Called when faulty logic causes a broken invariant.