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>
15 , collector_(collector)
16 , mismatch_counter_(collector->make_counter(
"ledger.history",
"mismatch"))
22 app_.journal(
"TaggedCache"))
23 , m_consensus_validated(
"ConsensusValidated", 64,
std::chrono::minutes{5},
stopwatch(), app_.journal(
"TaggedCache"))
24 , j_(app.journal(
"LedgerHistory"))
31 if (!ledger->isImmutable())
34 XRPL_ASSERT(ledger->stateMap().getHash().isNonZero(),
"xrpl::LedgerHistory::insert : nonzero hash");
74 XRPL_ASSERT(ret->header().seq == index,
"xrpl::LedgerHistory::getLedgerBySeq : result sequence match");
80 XRPL_ASSERT(ret->isImmutable(),
"xrpl::LedgerHistory::getLedgerBySeq : immutable result ledger");
83 return (ret->header().seq == index) ? ret :
nullptr;
96 "xrpl::LedgerHistory::getLedgerByHash : immutable fetched "
99 ret->header().hash == hash,
100 "xrpl::LedgerHistory::getLedgerByHash : fetched ledger hash "
110 XRPL_ASSERT(ret->isImmutable(),
"xrpl::LedgerHistory::getLedgerByHash : immutable loaded ledger");
111 XRPL_ASSERT(ret->header().hash == hash,
"xrpl::LedgerHistory::getLedgerByHash : loaded ledger hash match");
113 XRPL_ASSERT(ret->header().hash == hash,
"xrpl::LedgerHistory::getLedgerByHash : result hash match");
123 if (metaData !=
nullptr)
125 JLOG(j.
debug()) <<
"MISMATCH on TX " << tx <<
": " << msg <<
" is missing this transaction:\n"
130 JLOG(j.
debug()) <<
"MISMATCH on TX " << tx <<
": " << msg <<
" is missing this transaction.";
144 auto validMetaData = getMeta(validLedger, tx);
145 auto builtMetaData = getMeta(builtLedger, tx);
147 XRPL_ASSERT(validMetaData || builtMetaData,
"xrpl::log_metadata_difference : some metadata present");
149 if (validMetaData && builtMetaData)
151 auto const& validNodes = validMetaData->getNodes();
152 auto const& builtNodes = builtMetaData->getNodes();
154 bool const result_diff = validMetaData->getResultTER() != builtMetaData->getResultTER();
156 bool const index_diff = validMetaData->getIndex() != builtMetaData->getIndex();
158 bool const nodes_diff = validNodes != builtNodes;
160 if (!result_diff && !index_diff && !nodes_diff)
162 JLOG(j.
error()) <<
"MISMATCH on TX " << tx <<
": No apparent mismatches detected!";
168 if (result_diff && index_diff)
170 JLOG(j.
debug()) <<
"MISMATCH on TX " << tx <<
": Different result and index!";
171 JLOG(j.
debug()) <<
" Built:"
172 <<
" Result: " << builtMetaData->getResult() <<
" Index: " << builtMetaData->getIndex();
173 JLOG(j.
debug()) <<
" Valid:"
174 <<
" Result: " << validMetaData->getResult() <<
" Index: " << validMetaData->getIndex();
176 else if (result_diff)
178 JLOG(j.
debug()) <<
"MISMATCH on TX " << tx <<
": Different result!";
179 JLOG(j.
debug()) <<
" Built:"
180 <<
" Result: " << builtMetaData->getResult();
181 JLOG(j.
debug()) <<
" Valid:"
182 <<
" Result: " << validMetaData->getResult();
186 JLOG(j.
debug()) <<
"MISMATCH on TX " << tx <<
": Different index!";
187 JLOG(j.
debug()) <<
" Built:"
188 <<
" Index: " << builtMetaData->getIndex();
189 JLOG(j.
debug()) <<
" Valid:"
190 <<
" Index: " << validMetaData->getIndex();
195 if (result_diff && index_diff)
197 JLOG(j.
debug()) <<
"MISMATCH on TX " << tx <<
": Different result, index and nodes!";
201 else if (result_diff)
203 JLOG(j.
debug()) <<
"MISMATCH on TX " << tx <<
": Different result and nodes!";
204 JLOG(j.
debug()) <<
" Built:"
205 <<
" Result: " << builtMetaData->getResult() <<
" Nodes:\n"
207 JLOG(j.
debug()) <<
" Valid:"
208 <<
" Result: " << validMetaData->getResult() <<
" Nodes:\n"
213 JLOG(j.
debug()) <<
"MISMATCH on TX " << tx <<
": Different index and nodes!";
214 JLOG(j.
debug()) <<
" Built:"
215 <<
" Index: " << builtMetaData->getIndex() <<
" Nodes:\n"
217 JLOG(j.
debug()) <<
" Valid:"
218 <<
" Index: " << validMetaData->getIndex() <<
" Nodes:\n"
223 JLOG(j.
debug()) <<
"MISMATCH on TX " << tx <<
": Different nodes!";
224 JLOG(j.
debug()) <<
" Built:"
227 JLOG(j.
debug()) <<
" Valid:"
238 JLOG(j.
error()) <<
"MISMATCH on TX " << tx <<
": Metadata Difference. Valid=\n"
244 JLOG(j.
error()) <<
"MISMATCH on TX " << tx <<
": Metadata Difference. Built=\n"
256 for (
auto const& item : sm)
270 XRPL_ASSERT(built !=
valid,
"xrpl::LedgerHistory::handleMismatch : unequal hashes");
278 JLOG(
j_.
error()) <<
"MISMATCH cannot be analyzed:"
285 builtLedger->header().seq == validLedger->header().seq,
"xrpl::LedgerHistory::handleMismatch : sequence match");
290 stream <<
"Valid: " <<
getJson({*validLedger, {}});
291 stream <<
"Consensus: " << consensus;
298 if (
builtLedger->header().parentHash != validLedger->header().parentHash)
300 JLOG(
j_.
error()) <<
"MISMATCH on prior ledger";
305 if (
builtLedger->header().closeTime != validLedger->header().closeTime)
307 JLOG(
j_.
error()) <<
"MISMATCH on close time";
311 if (builtConsensusHash && validatedConsensusHash)
313 if (builtConsensusHash != validatedConsensusHash)
314 JLOG(
j_.
error()) <<
"MISMATCH on consensus transaction set "
315 <<
" built: " <<
to_string(*builtConsensusHash)
316 <<
" validated: " <<
to_string(*validatedConsensusHash);
318 JLOG(
j_.
error()) <<
"MISMATCH with same consensus transaction set: " <<
to_string(*builtConsensusHash);
323 auto const validTx =
leaves(validLedger->txMap());
325 if (builtTx == validTx)
326 JLOG(
j_.
error()) <<
"MISMATCH with same " << builtTx.size() <<
" transactions";
328 JLOG(
j_.
error()) <<
"MISMATCH with " << builtTx.size() <<
" built and " << validTx.size()
329 <<
" valid transactions.";
335 auto b = builtTx.
begin();
336 auto v = validTx.begin();
337 while (b != builtTx.end() && v != validTx.end())
339 if ((*b)->key() < (*v)->key())
344 else if ((*b)->key() > (*v)->key())
346 log_one(*validLedger, (*v)->key(),
"built",
j_);
351 if ((*b)->slice() != (*v)->slice())
360 for (; b != builtTx.end(); ++b)
362 for (; v != validTx.end(); ++v)
363 log_one(*validLedger, (*v)->key(),
"built",
j_);
374 XRPL_ASSERT(!hash.
isZero(),
"xrpl::LedgerHistory::builtLedger : nonzero hash");
381 if (entry->validated && !entry->built)
383 if (entry->validated.value() != hash)
385 JLOG(
j_.
error()) <<
"MISMATCH: seq=" << index <<
" validated:" << entry->validated.value()
387 handleMismatch(hash, entry->validated.value(), consensusHash, entry->validatedConsensusHash, consensus);
392 JLOG(
j_.
debug()) <<
"MATCH: seq=" << index <<
" late";
396 entry->built.emplace(hash);
397 entry->builtConsensusHash.emplace(consensusHash);
398 entry->consensus.emplace(std::move(consensus));
406 XRPL_ASSERT(!hash.
isZero(),
"xrpl::LedgerHistory::validatedLedger : nonzero hash");
413 if (entry->built && !entry->validated)
415 if (entry->built.value() != hash)
417 JLOG(
j_.
error()) <<
"MISMATCH: seq=" << index <<
" built:" << entry->built.value() <<
" then:" << hash;
419 entry->built.value(), hash, entry->builtConsensusHash, consensusHash, entry->consensus.value());
424 JLOG(
j_.
debug()) <<
"MATCH: seq=" << index;
428 entry->validated.emplace(hash);
429 entry->validatedConsensusHash = consensusHash;
442 it->second = ledgerHash;
454 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)