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 "xrpl::LedgerHistory::insert : nonzero hash");
50 ledger->header().hash, ledger);
87 ret->header().seq == index,
88 "xrpl::LedgerHistory::getLedgerBySeq : result sequence match");
96 "xrpl::LedgerHistory::getLedgerBySeq : immutable result ledger");
99 return (ret->header().seq == index) ? ret :
nullptr;
112 "xrpl::LedgerHistory::getLedgerByHash : immutable fetched "
115 ret->header().hash == hash,
116 "xrpl::LedgerHistory::getLedgerByHash : fetched ledger hash "
128 "xrpl::LedgerHistory::getLedgerByHash : immutable loaded ledger");
130 ret->header().hash == hash,
131 "xrpl::LedgerHistory::getLedgerByHash : loaded ledger hash match");
134 ret->header().hash == hash,
135 "xrpl::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 "xrpl::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();
328 built !=
valid,
"xrpl::LedgerHistory::handleMismatch : unequal hashes");
336 JLOG(
j_.
error()) <<
"MISMATCH cannot be analyzed:"
337 <<
" builtLedger: " <<
to_string(built) <<
" -> "
339 <<
" -> " << validLedger;
344 builtLedger->header().seq == validLedger->header().seq,
345 "xrpl::LedgerHistory::handleMismatch : sequence match");
350 stream <<
"Valid: " <<
getJson({*validLedger, {}});
351 stream <<
"Consensus: " << consensus;
358 if (
builtLedger->header().parentHash != validLedger->header().parentHash)
360 JLOG(
j_.
error()) <<
"MISMATCH on prior ledger";
365 if (
builtLedger->header().closeTime != validLedger->header().closeTime)
367 JLOG(
j_.
error()) <<
"MISMATCH on close time";
371 if (builtConsensusHash && validatedConsensusHash)
373 if (builtConsensusHash != validatedConsensusHash)
375 <<
"MISMATCH on consensus transaction set "
376 <<
" built: " <<
to_string(*builtConsensusHash)
377 <<
" validated: " <<
to_string(*validatedConsensusHash);
379 JLOG(
j_.
error()) <<
"MISMATCH with same consensus transaction set: "
385 auto const validTx =
leaves(validLedger->txMap());
387 if (builtTx == validTx)
388 JLOG(
j_.
error()) <<
"MISMATCH with same " << builtTx.size()
391 JLOG(
j_.
error()) <<
"MISMATCH with " << builtTx.size() <<
" built and "
392 << validTx.size() <<
" valid transactions.";
398 auto b = builtTx.
begin();
399 auto v = validTx.begin();
400 while (b != builtTx.end() && v != validTx.end())
402 if ((*b)->key() < (*v)->key())
407 else if ((*b)->key() > (*v)->key())
409 log_one(*validLedger, (*v)->key(),
"built",
j_);
414 if ((*b)->slice() != (*v)->slice())
424 for (; b != builtTx.end(); ++b)
426 for (; v != validTx.end(); ++v)
427 log_one(*validLedger, (*v)->key(),
"built",
j_);
439 !hash.
isZero(),
"xrpl::LedgerHistory::builtLedger : nonzero hash");
446 if (entry->validated && !entry->built)
448 if (entry->validated.value() != hash)
450 JLOG(
j_.
error()) <<
"MISMATCH: seq=" << index
451 <<
" validated:" << entry->validated.value()
455 entry->validated.value(),
457 entry->validatedConsensusHash,
463 JLOG(
j_.
debug()) <<
"MATCH: seq=" << index <<
" late";
467 entry->built.emplace(hash);
468 entry->builtConsensusHash.emplace(consensusHash);
469 entry->consensus.emplace(std::move(consensus));
480 !hash.
isZero(),
"xrpl::LedgerHistory::validatedLedger : nonzero hash");
487 if (entry->built && !entry->validated)
489 if (entry->built.value() != hash)
492 <<
"MISMATCH: seq=" << index
493 <<
" built:" << entry->built.value() <<
" then:" << hash;
495 entry->built.value(),
497 entry->builtConsensusHash,
499 entry->consensus.value());
504 JLOG(
j_.
debug()) <<
"MATCH: seq=" << index;
508 entry->validated.emplace(hash);
509 entry->validatedConsensusHash = consensusHash;
522 it->second = ledgerHash;
534 if (!ledger || ledger->header().seq < seq)
const_iterator begin() const
A generic endpoint for log messages.
std::map< LedgerIndex, LedgerHash > mLedgersByIndex
void validatedLedger(std::shared_ptr< Ledger const > const &, std::optional< uint256 > const &consensusHash)
Report that we have validated a particular ledger.
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 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.
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.
ConsensusValidated m_consensus_validated
LedgerHistory(beast::insight::Collector::ptr const &collector, Application &app)
LedgersByHash m_ledgers_by_hash
void builtLedger(std::shared_ptr< Ledger const > const &, uint256 const &consensusHash, Json::Value)
Report that we have locally built a particular ledger.
beast::insight::Counter mismatch_counter_
std::shared_ptr< Ledger const > getLedgerBySeq(LedgerIndex ledgerIndex)
Get a ledger given its sequence number.
void clearLedgerCachePrior(LedgerIndex seq)
virtual tx_type txRead(key_type const &key) const =0
Read a transaction from the tx map.
LedgerIndex seq() const
Returns the sequence number of the base ledger.
A SHAMap is both a radix tree with a fan-out of 16 and a Merkle tree.
std::vector< key_type > getKeys() const
bool del(key_type const &key, bool valid)
bool canonicalize_replace_cache(key_type const &key, SharedPointerType const &data)
bool canonicalize_replace_client(key_type const &key, SharedPointerType &data)
SharedPointerType fetch(key_type const &key)
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 > loadByHash(uint256 const &ledgerHash, Application &app, bool acquire)
void LogicError(std::string const &how) noexcept
Called when faulty logic causes a broken invariant.
Json::Value getJson(LedgerFill const &fill)
Return a new Json::Value representing the ledger with given options.
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)
static void log_one(ReadView const &ledger, uint256 const &tx, char const *msg, beast::Journal &j)
std::shared_ptr< Ledger > loadByIndex(std::uint32_t ledgerIndex, Application &app, bool acquire)
static std::vector< SHAMapItem const * > leaves(SHAMap const &sm)