1#include <xrpld/app/ledger/LedgerHistory.h>
2#include <xrpld/app/ledger/LedgerToJson.h>
4#include <xrpl/basics/Log.h>
5#include <xrpl/basics/chrono.h>
6#include <xrpl/basics/contract.h>
7#include <xrpl/json/to_string.h>
17 , collector_(collector)
18 , mismatch_counter_(collector->make_counter(
"ledger.history",
"mismatch"))
24 app_.journal(
"TaggedCache"))
25 , m_consensus_validated(
28 std::chrono::minutes{5},
30 app_.journal(
"TaggedCache"))
31 , j_(app.journal(
"LedgerHistory"))
40 if (!ledger->isImmutable())
44 ledger->stateMap().getHash().isNonZero(),
45 "ripple::LedgerHistory::insert : nonzero hash");
50 ledger->info().hash, ledger);
87 ret->info().seq == index,
88 "ripple::LedgerHistory::getLedgerBySeq : result sequence match");
96 "ripple::LedgerHistory::getLedgerBySeq : immutable result ledger");
99 return (ret->info().seq == index) ? ret :
nullptr;
112 "ripple::LedgerHistory::getLedgerByHash : immutable fetched "
115 ret->info().hash == hash,
116 "ripple::LedgerHistory::getLedgerByHash : fetched ledger hash "
128 "ripple::LedgerHistory::getLedgerByHash : immutable loaded ledger");
130 ret->info().hash == hash,
131 "ripple::LedgerHistory::getLedgerByHash : loaded ledger hash match");
134 ret->info().hash == hash,
135 "ripple::LedgerHistory::getLedgerByHash : result hash match");
149 if (metaData !=
nullptr)
151 JLOG(j.
debug()) <<
"MISMATCH on TX " << tx <<
": " << msg
152 <<
" is missing this transaction:\n"
157 JLOG(j.
debug()) <<
"MISMATCH on TX " << tx <<
": " << msg
158 <<
" is missing this transaction.";
176 auto validMetaData = getMeta(validLedger, tx);
177 auto builtMetaData = getMeta(builtLedger, tx);
180 validMetaData || builtMetaData,
181 "ripple::log_metadata_difference : some metadata present");
183 if (validMetaData && builtMetaData)
185 auto const& validNodes = validMetaData->getNodes();
186 auto const& builtNodes = builtMetaData->getNodes();
188 bool const result_diff =
189 validMetaData->getResultTER() != builtMetaData->getResultTER();
191 bool const index_diff =
192 validMetaData->getIndex() != builtMetaData->getIndex();
194 bool const nodes_diff = validNodes != builtNodes;
196 if (!result_diff && !index_diff && !nodes_diff)
198 JLOG(j.
error()) <<
"MISMATCH on TX " << tx
199 <<
": No apparent mismatches detected!";
205 if (result_diff && index_diff)
207 JLOG(j.
debug()) <<
"MISMATCH on TX " << tx
208 <<
": Different result and index!";
209 JLOG(j.
debug()) <<
" Built:"
210 <<
" Result: " << builtMetaData->getResult()
211 <<
" Index: " << builtMetaData->getIndex();
212 JLOG(j.
debug()) <<
" Valid:"
213 <<
" Result: " << validMetaData->getResult()
214 <<
" Index: " << validMetaData->getIndex();
216 else if (result_diff)
219 <<
"MISMATCH on TX " << tx <<
": Different result!";
220 JLOG(j.
debug()) <<
" Built:"
221 <<
" Result: " << builtMetaData->getResult();
222 JLOG(j.
debug()) <<
" Valid:"
223 <<
" Result: " << validMetaData->getResult();
228 <<
"MISMATCH on TX " << tx <<
": Different index!";
229 JLOG(j.
debug()) <<
" Built:"
230 <<
" Index: " << builtMetaData->getIndex();
231 JLOG(j.
debug()) <<
" Valid:"
232 <<
" Index: " << validMetaData->getIndex();
237 if (result_diff && index_diff)
239 JLOG(j.
debug()) <<
"MISMATCH on TX " << tx
240 <<
": Different result, index and nodes!";
241 JLOG(j.
debug()) <<
" Built:\n"
243 JLOG(j.
debug()) <<
" Valid:\n"
246 else if (result_diff)
248 JLOG(j.
debug()) <<
"MISMATCH on TX " << tx
249 <<
": Different result and nodes!";
252 <<
" Result: " << builtMetaData->getResult() <<
" Nodes:\n"
256 <<
" Result: " << validMetaData->getResult() <<
" Nodes:\n"
261 JLOG(j.
debug()) <<
"MISMATCH on TX " << tx
262 <<
": Different index and nodes!";
265 <<
" Index: " << builtMetaData->getIndex() <<
" Nodes:\n"
269 <<
" Index: " << validMetaData->getIndex() <<
" Nodes:\n"
275 <<
"MISMATCH on TX " << tx <<
": Different nodes!";
276 JLOG(j.
debug()) <<
" Built:"
279 JLOG(j.
debug()) <<
" Valid:"
290 JLOG(j.
error()) <<
"MISMATCH on TX " << tx
291 <<
": Metadata Difference. Valid=\n"
297 JLOG(j.
error()) <<
"MISMATCH on TX " << tx
298 <<
": Metadata Difference. Built=\n"
310 for (
auto const& item : sm)
314 return lhs->key() < rhs->key();
329 "ripple::LedgerHistory::handleMismatch : unequal hashes");
337 JLOG(
j_.
error()) <<
"MISMATCH cannot be analyzed:"
338 <<
" builtLedger: " <<
to_string(built) <<
" -> "
340 <<
" -> " << validLedger;
345 builtLedger->info().seq == validLedger->info().seq,
346 "ripple::LedgerHistory::handleMismatch : sequence match");
351 stream <<
"Valid: " <<
getJson({*validLedger, {}});
352 stream <<
"Consensus: " << consensus;
359 if (
builtLedger->info().parentHash != validLedger->info().parentHash)
361 JLOG(
j_.
error()) <<
"MISMATCH on prior ledger";
366 if (
builtLedger->info().closeTime != validLedger->info().closeTime)
368 JLOG(
j_.
error()) <<
"MISMATCH on close time";
372 if (builtConsensusHash && validatedConsensusHash)
374 if (builtConsensusHash != validatedConsensusHash)
376 <<
"MISMATCH on consensus transaction set "
377 <<
" built: " <<
to_string(*builtConsensusHash)
378 <<
" validated: " <<
to_string(*validatedConsensusHash);
380 JLOG(
j_.
error()) <<
"MISMATCH with same consensus transaction set: "
386 auto const validTx =
leaves(validLedger->txMap());
388 if (builtTx == validTx)
389 JLOG(
j_.
error()) <<
"MISMATCH with same " << builtTx.size()
392 JLOG(
j_.
error()) <<
"MISMATCH with " << builtTx.size() <<
" built and "
393 << validTx.size() <<
" valid transactions.";
399 auto b = builtTx.
begin();
400 auto v = validTx.begin();
401 while (b != builtTx.end() && v != validTx.end())
403 if ((*b)->key() < (*v)->key())
408 else if ((*b)->key() > (*v)->key())
410 log_one(*validLedger, (*v)->key(),
"built",
j_);
415 if ((*b)->slice() != (*v)->slice())
425 for (; b != builtTx.end(); ++b)
427 for (; v != validTx.end(); ++v)
428 log_one(*validLedger, (*v)->key(),
"built",
j_);
440 !hash.
isZero(),
"ripple::LedgerHistory::builtLedger : nonzero hash");
447 if (entry->validated && !entry->built)
449 if (entry->validated.value() != hash)
451 JLOG(
j_.
error()) <<
"MISMATCH: seq=" << index
452 <<
" validated:" << entry->validated.value()
456 entry->validated.value(),
458 entry->validatedConsensusHash,
464 JLOG(
j_.
debug()) <<
"MATCH: seq=" << index <<
" late";
468 entry->built.emplace(hash);
469 entry->builtConsensusHash.emplace(consensusHash);
470 entry->consensus.emplace(std::move(consensus));
482 "ripple::LedgerHistory::validatedLedger : nonzero hash");
489 if (entry->built && !entry->validated)
491 if (entry->built.value() != hash)
494 <<
"MISMATCH: seq=" << index
495 <<
" built:" << entry->built.value() <<
" then:" << hash;
497 entry->built.value(),
499 entry->builtConsensusHash,
501 entry->consensus.value());
506 JLOG(
j_.
debug()) <<
"MATCH: seq=" << index;
510 entry->validated.emplace(hash);
511 entry->validatedConsensusHash = consensusHash;
524 it->second = ledgerHash;
536 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.