From 1115cef43b0bf024bb4e5a1d664088e0644a62a9 Mon Sep 17 00:00:00 2001 From: Bronek Kozicki Date: Thu, 26 Oct 2023 13:22:26 +0100 Subject: [PATCH] Move isValidated from RPCHelpers to LedgerMaster --- src/ripple/app/ledger/LedgerMaster.h | 2 + src/ripple/app/ledger/impl/LedgerMaster.cpp | 49 +++++++++++++++++ src/ripple/app/ledger/impl/LedgerToJson.cpp | 5 +- src/ripple/rpc/handlers/AMMInfo.cpp | 4 +- src/ripple/rpc/handlers/AccountTx.cpp | 5 +- src/ripple/rpc/handlers/LedgerHandler.cpp | 4 +- src/ripple/rpc/handlers/TransactionEntry.cpp | 4 +- src/ripple/rpc/impl/RPCHelpers.cpp | 56 +------------------- src/ripple/rpc/impl/RPCHelpers.h | 6 --- 9 files changed, 61 insertions(+), 74 deletions(-) diff --git a/src/ripple/app/ledger/LedgerMaster.h b/src/ripple/app/ledger/LedgerMaster.h index 26738844536..e2ca3039935 100644 --- a/src/ripple/app/ledger/LedgerMaster.h +++ b/src/ripple/app/ledger/LedgerMaster.h @@ -215,6 +215,8 @@ class LedgerMaster : public AbstractFetchPackContainer void clearLedger(std::uint32_t seq); bool + isValidated(ReadView const& ledger); + bool getValidatedRange(std::uint32_t& minVal, std::uint32_t& maxVal); bool getFullValidatedRange(std::uint32_t& minVal, std::uint32_t& maxVal); diff --git a/src/ripple/app/ledger/impl/LedgerMaster.cpp b/src/ripple/app/ledger/impl/LedgerMaster.cpp index 050e2f3ef3d..60897504c91 100644 --- a/src/ripple/app/ledger/impl/LedgerMaster.cpp +++ b/src/ripple/app/ledger/impl/LedgerMaster.cpp @@ -603,6 +603,55 @@ LedgerMaster::clearLedger(std::uint32_t seq) mCompleteLedgers.erase(seq); } +bool +LedgerMaster::isValidated(ReadView const& ledger) +{ + if (app_.config().reporting()) + return haveLedger(ledger.seq()); // TODO Is this correct ? + + if (ledger.open()) + return false; + + if (ledger.info().validated) + return true; + + auto const seq = ledger.info().seq; + try + { + // Use the skip list in the last validated ledger to see if ledger + // comes before the last validated ledger (and thus has been + // validated). + auto hash = walkHashBySeq(seq, InboundLedger::Reason::GENERIC); + + if (!hash || ledger.info().hash != *hash) + { + // This ledger's hash is not the hash of the validated ledger + if (hash) + { + assert(hash->isNonZero()); + uint256 valHash = + app_.getRelationalDatabase().getHashByIndex(seq); + if (valHash == ledger.info().hash) + { + // SQL database doesn't match ledger chain + clearLedger(seq); + } + } + return false; + } + } + catch (SHAMapMissingNode const& mn) + { + auto stream = app_.journal("IsValidated").warn(); // TODO Better name ? + JLOG(stream) << "Ledger #" << seq << ": " << mn.what(); + return false; + } + + // Mark ledger as validated to save time if we see it again. + ledger.info().validated = true; + return true; +} + // returns Ledgers we have all the nodes for bool LedgerMaster::getFullValidatedRange( diff --git a/src/ripple/app/ledger/impl/LedgerToJson.cpp b/src/ripple/app/ledger/impl/LedgerToJson.cpp index f3721156a0c..6b3f42d62df 100644 --- a/src/ripple/app/ledger/impl/LedgerToJson.cpp +++ b/src/ripple/app/ledger/impl/LedgerToJson.cpp @@ -27,7 +27,6 @@ #include #include #include -#include namespace ripple { @@ -156,8 +155,8 @@ fillJsonTx( {txn->getTransactionID(), fill.ledger.seq(), *stMeta}); } - const bool validated = RPC::isValidated( - fill.context->ledgerMaster, fill.ledger, fill.context->app); + const bool validated = + fill.context->ledgerMaster.isValidated(fill.ledger); txJson[jss::validated] = validated; if (validated) { diff --git a/src/ripple/rpc/handlers/AMMInfo.cpp b/src/ripple/rpc/handlers/AMMInfo.cpp index 47d3e117e8e..a1be636cafd 100644 --- a/src/ripple/rpc/handlers/AMMInfo.cpp +++ b/src/ripple/rpc/handlers/AMMInfo.cpp @@ -16,6 +16,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ //============================================================================== +#include #include #include #include @@ -244,8 +245,7 @@ doAMMInfo(RPC::JsonContext& context) if (!result.isMember(jss::ledger_index) && !result.isMember(jss::ledger_hash)) result[jss::ledger_current_index] = ledger->info().seq; - result[jss::validated] = - RPC::isValidated(context.ledgerMaster, *ledger, context.app); + result[jss::validated] = context.ledgerMaster.isValidated(*ledger); return result; } diff --git a/src/ripple/rpc/handlers/AccountTx.cpp b/src/ripple/rpc/handlers/AccountTx.cpp index 6ec7efd3d6c..052b8ae9edb 100644 --- a/src/ripple/rpc/handlers/AccountTx.cpp +++ b/src/ripple/rpc/handlers/AccountTx.cpp @@ -37,7 +37,6 @@ #include #include #include -#include #include @@ -195,8 +194,8 @@ getLedgerRange( return status; } - bool validated = RPC::isValidated( - context.ledgerMaster, *ledgerView, context.app); + bool validated = + context.ledgerMaster.isValidated(*ledgerView); if (!validated || ledgerView->info().seq > uValidatedMax || ledgerView->info().seq < uValidatedMin) diff --git a/src/ripple/rpc/handlers/LedgerHandler.cpp b/src/ripple/rpc/handlers/LedgerHandler.cpp index 6b4fc77367b..623cb8d75ac 100644 --- a/src/ripple/rpc/handlers/LedgerHandler.cpp +++ b/src/ripple/rpc/handlers/LedgerHandler.cpp @@ -27,7 +27,6 @@ #include #include #include -#include namespace ripple { namespace RPC { @@ -301,8 +300,7 @@ doLedgerGrpc(RPC::GRPCContext& context) response.set_skiplist_included(true); } - response.set_validated( - RPC::isValidated(context.ledgerMaster, *ledger, context.app)); + response.set_validated(context.ledgerMaster.isValidated(*ledger)); auto end = std::chrono::system_clock::now(); auto duration = diff --git a/src/ripple/rpc/handlers/TransactionEntry.cpp b/src/ripple/rpc/handlers/TransactionEntry.cpp index db826f57d37..c729cc4956c 100644 --- a/src/ripple/rpc/handlers/TransactionEntry.cpp +++ b/src/ripple/rpc/handlers/TransactionEntry.cpp @@ -79,8 +79,8 @@ doTransactionEntry(RPC::JsonContext& context) sttx->getJson(JsonOptions::none, false, {std::ref(hash)}); jvResult[jss::hash] = hash; - bool const validated = RPC::isValidated( - context.ledgerMaster, *lpLedger, context.app); + bool const validated = + context.ledgerMaster.isValidated(*lpLedger); jvResult[jss::validated] = validated; if (validated) diff --git a/src/ripple/rpc/impl/RPCHelpers.cpp b/src/ripple/rpc/impl/RPCHelpers.cpp index a9cc0f9fffe..672095fe950 100644 --- a/src/ripple/rpc/impl/RPCHelpers.cpp +++ b/src/ripple/rpc/impl/RPCHelpers.cpp @@ -600,59 +600,6 @@ getLedger<>( template Status getLedger<>(std::shared_ptr&, uint256 const&, Context&); -bool -isValidated( - LedgerMaster& ledgerMaster, - ReadView const& ledger, - Application& app) -{ - if (app.config().reporting()) - return true; - - if (ledger.open()) - return false; - - if (ledger.info().validated) - return true; - - auto seq = ledger.info().seq; - try - { - // Use the skip list in the last validated ledger to see if ledger - // comes before the last validated ledger (and thus has been - // validated). - auto hash = - ledgerMaster.walkHashBySeq(seq, InboundLedger::Reason::GENERIC); - - if (!hash || ledger.info().hash != *hash) - { - // This ledger's hash is not the hash of the validated ledger - if (hash) - { - assert(hash->isNonZero()); - uint256 valHash = - app.getRelationalDatabase().getHashByIndex(seq); - if (valHash == ledger.info().hash) - { - // SQL database doesn't match ledger chain - ledgerMaster.clearLedger(seq); - } - } - return false; - } - } - catch (SHAMapMissingNode const& mn) - { - auto stream = app.journal("RPCHandler").warn(); - JLOG(stream) << "Ledger #" << seq << ": " << mn.what(); - return false; - } - - // Mark ledger as validated to save time if we see it again. - ledger.info().validated = true; - return true; -} - // The previous version of the lookupLedger command would accept the // "ledger_index" argument as a string and silently treat it as a request to // return the current ledger which, while not strictly wrong, could cause a lot @@ -693,8 +640,7 @@ lookupLedger( result[jss::ledger_current_index] = info.seq; } - result[jss::validated] = - isValidated(context.ledgerMaster, *ledger, context.app); + result[jss::validated] = context.ledgerMaster.isValidated(*ledger); return Status::OK; } diff --git a/src/ripple/rpc/impl/RPCHelpers.h b/src/ripple/rpc/impl/RPCHelpers.h index 516f66fc620..97015f1a35d 100644 --- a/src/ripple/rpc/impl/RPCHelpers.h +++ b/src/ripple/rpc/impl/RPCHelpers.h @@ -168,12 +168,6 @@ ledgerFromSpecifier( org::xrpl::rpc::v1::LedgerSpecifier const& specifier, Context& context); -bool -isValidated( - LedgerMaster& ledgerMaster, - ReadView const& ledger, - Application& app); - hash_set parseAccountIds(Json::Value const& jvArray);