Skip to content

Commit

Permalink
Move isValidated from RPCHelpers to LedgerMaster
Browse files Browse the repository at this point in the history
  • Loading branch information
Bronek committed Oct 26, 2023
1 parent 2035bbc commit 1115cef
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 74 deletions.
2 changes: 2 additions & 0 deletions src/ripple/app/ledger/LedgerMaster.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
49 changes: 49 additions & 0 deletions src/ripple/app/ledger/impl/LedgerMaster.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
5 changes: 2 additions & 3 deletions src/ripple/app/ledger/impl/LedgerToJson.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
#include <ripple/protocol/jss.h>
#include <ripple/rpc/Context.h>
#include <ripple/rpc/DeliveredAmount.h>
#include <ripple/rpc/impl/RPCHelpers.h>

namespace ripple {

Expand Down Expand Up @@ -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)
{
Expand Down
4 changes: 2 additions & 2 deletions src/ripple/rpc/handlers/AMMInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
//==============================================================================
#include <ripple/app/ledger/LedgerMaster.h>
#include <ripple/app/misc/AMMUtils.h>
#include <ripple/json/json_value.h>
#include <ripple/ledger/ReadView.h>
Expand Down Expand Up @@ -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;
}
Expand Down
5 changes: 2 additions & 3 deletions src/ripple/rpc/handlers/AccountTx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
#include <ripple/rpc/Context.h>
#include <ripple/rpc/DeliveredAmount.h>
#include <ripple/rpc/Role.h>
#include <ripple/rpc/impl/RPCHelpers.h>

#include <grpcpp/grpcpp.h>

Expand Down Expand Up @@ -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)
Expand Down
4 changes: 1 addition & 3 deletions src/ripple/rpc/handlers/LedgerHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
#include <ripple/rpc/GRPCHandlers.h>
#include <ripple/rpc/Role.h>
#include <ripple/rpc/handlers/LedgerHandler.h>
#include <ripple/rpc/impl/RPCHelpers.h>

namespace ripple {
namespace RPC {
Expand Down Expand Up @@ -301,8 +300,7 @@ doLedgerGrpc(RPC::GRPCContext<org::xrpl::rpc::v1::GetLedgerRequest>& 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 =
Expand Down
4 changes: 2 additions & 2 deletions src/ripple/rpc/handlers/TransactionEntry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
56 changes: 1 addition & 55 deletions src/ripple/rpc/impl/RPCHelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -600,59 +600,6 @@ getLedger<>(
template Status
getLedger<>(std::shared_ptr<ReadView const>&, 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
Expand Down Expand Up @@ -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;
}

Expand Down
6 changes: 0 additions & 6 deletions src/ripple/rpc/impl/RPCHelpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<AccountID>
parseAccountIds(Json::Value const& jvArray);

Expand Down

0 comments on commit 1115cef

Please sign in to comment.