Skip to content

Commit

Permalink
refactor LedgerEntry
Browse files Browse the repository at this point in the history
  • Loading branch information
mvadari committed Nov 22, 2024
1 parent 0ec17b6 commit f1ec955
Show file tree
Hide file tree
Showing 4 changed files with 992 additions and 886 deletions.
338 changes: 185 additions & 153 deletions src/test/rpc/LedgerRPC_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1877,160 +1877,164 @@ class LedgerRPC_test : public beast::unit_test::suite
env(pay(gw, alice, USD(97)));
env.close();

std::string const ledgerHash{to_string(env.closed()->info().hash)};
{
// Request the trust line using the accounts and currency.
Json::Value jvParams;
jvParams[jss::ripple_state] = Json::objectValue;
jvParams[jss::ripple_state][jss::accounts] = Json::arrayValue;
jvParams[jss::ripple_state][jss::accounts][0u] = alice.human();
jvParams[jss::ripple_state][jss::accounts][1u] = gw.human();
jvParams[jss::ripple_state][jss::currency] = "USD";
jvParams[jss::ledger_hash] = ledgerHash;
Json::Value const jrr = env.rpc(
"json", "ledger_entry", to_string(jvParams))[jss::result];
BEAST_EXPECT(
jrr[jss::node][sfBalance.jsonName][jss::value] == "-97");
BEAST_EXPECT(
jrr[jss::node][sfHighLimit.jsonName][jss::value] == "999");
}
{
// ripple_state is not an object.
Json::Value jvParams;
jvParams[jss::ripple_state] = "ripple_state";
jvParams[jss::ledger_hash] = ledgerHash;
Json::Value const jrr = env.rpc(
"json", "ledger_entry", to_string(jvParams))[jss::result];
checkErrorValue(jrr, "malformedRequest", "");
}
{
// ripple_state.currency is missing.
Json::Value jvParams;
jvParams[jss::ripple_state] = Json::objectValue;
jvParams[jss::ripple_state][jss::accounts] = Json::arrayValue;
jvParams[jss::ripple_state][jss::accounts][0u] = alice.human();
jvParams[jss::ripple_state][jss::accounts][1u] = gw.human();
jvParams[jss::ledger_hash] = ledgerHash;
Json::Value const jrr = env.rpc(
"json", "ledger_entry", to_string(jvParams))[jss::result];
checkErrorValue(jrr, "malformedRequest", "");
}
{
// ripple_state accounts is not an array.
Json::Value jvParams;
jvParams[jss::ripple_state] = Json::objectValue;
jvParams[jss::ripple_state][jss::accounts] = 2;
jvParams[jss::ripple_state][jss::currency] = "USD";
jvParams[jss::ledger_hash] = ledgerHash;
Json::Value const jrr = env.rpc(
"json", "ledger_entry", to_string(jvParams))[jss::result];
checkErrorValue(jrr, "malformedRequest", "");
}
{
// ripple_state one of the accounts is missing.
Json::Value jvParams;
jvParams[jss::ripple_state] = Json::objectValue;
jvParams[jss::ripple_state][jss::accounts] = Json::arrayValue;
jvParams[jss::ripple_state][jss::accounts][0u] = alice.human();
jvParams[jss::ripple_state][jss::currency] = "USD";
jvParams[jss::ledger_hash] = ledgerHash;
Json::Value const jrr = env.rpc(
"json", "ledger_entry", to_string(jvParams))[jss::result];
checkErrorValue(jrr, "malformedRequest", "");
}
{
// ripple_state more than 2 accounts.
Json::Value jvParams;
jvParams[jss::ripple_state] = Json::objectValue;
jvParams[jss::ripple_state][jss::accounts] = Json::arrayValue;
jvParams[jss::ripple_state][jss::accounts][0u] = alice.human();
jvParams[jss::ripple_state][jss::accounts][1u] = gw.human();
jvParams[jss::ripple_state][jss::accounts][2u] = alice.human();
jvParams[jss::ripple_state][jss::currency] = "USD";
jvParams[jss::ledger_hash] = ledgerHash;
Json::Value const jrr = env.rpc(
"json", "ledger_entry", to_string(jvParams))[jss::result];
checkErrorValue(jrr, "malformedRequest", "");
}
// check both aliases
for (auto const& fieldName : {jss::ripple_state, jss::state})
{
// ripple_state account[0] is not a string.
Json::Value jvParams;
jvParams[jss::ripple_state] = Json::objectValue;
jvParams[jss::ripple_state][jss::accounts] = Json::arrayValue;
jvParams[jss::ripple_state][jss::accounts][0u] = 44;
jvParams[jss::ripple_state][jss::accounts][1u] = gw.human();
jvParams[jss::ripple_state][jss::currency] = "USD";
jvParams[jss::ledger_hash] = ledgerHash;
Json::Value const jrr = env.rpc(
"json", "ledger_entry", to_string(jvParams))[jss::result];
checkErrorValue(jrr, "malformedRequest", "");
}
{
// ripple_state account[1] is not a string.
Json::Value jvParams;
jvParams[jss::ripple_state] = Json::objectValue;
jvParams[jss::ripple_state][jss::accounts] = Json::arrayValue;
jvParams[jss::ripple_state][jss::accounts][0u] = alice.human();
jvParams[jss::ripple_state][jss::accounts][1u] = 21;
jvParams[jss::ripple_state][jss::currency] = "USD";
jvParams[jss::ledger_hash] = ledgerHash;
Json::Value const jrr = env.rpc(
"json", "ledger_entry", to_string(jvParams))[jss::result];
checkErrorValue(jrr, "malformedRequest", "");
}
{
// ripple_state account[0] == account[1].
Json::Value jvParams;
jvParams[jss::ripple_state] = Json::objectValue;
jvParams[jss::ripple_state][jss::accounts] = Json::arrayValue;
jvParams[jss::ripple_state][jss::accounts][0u] = alice.human();
jvParams[jss::ripple_state][jss::accounts][1u] = alice.human();
jvParams[jss::ripple_state][jss::currency] = "USD";
jvParams[jss::ledger_hash] = ledgerHash;
Json::Value const jrr = env.rpc(
"json", "ledger_entry", to_string(jvParams))[jss::result];
checkErrorValue(jrr, "malformedRequest", "");
}
{
// ripple_state malformed account[0].
Json::Value jvParams;
jvParams[jss::ripple_state] = Json::objectValue;
jvParams[jss::ripple_state][jss::accounts] = Json::arrayValue;
jvParams[jss::ripple_state][jss::accounts][0u] =
makeBadAddress(alice.human());
jvParams[jss::ripple_state][jss::accounts][1u] = gw.human();
jvParams[jss::ripple_state][jss::currency] = "USD";
jvParams[jss::ledger_hash] = ledgerHash;
Json::Value const jrr = env.rpc(
"json", "ledger_entry", to_string(jvParams))[jss::result];
checkErrorValue(jrr, "malformedAddress", "");
}
{
// ripple_state malformed account[1].
Json::Value jvParams;
jvParams[jss::ripple_state] = Json::objectValue;
jvParams[jss::ripple_state][jss::accounts] = Json::arrayValue;
jvParams[jss::ripple_state][jss::accounts][0u] = alice.human();
jvParams[jss::ripple_state][jss::accounts][1u] =
makeBadAddress(gw.human());
jvParams[jss::ripple_state][jss::currency] = "USD";
jvParams[jss::ledger_hash] = ledgerHash;
Json::Value const jrr = env.rpc(
"json", "ledger_entry", to_string(jvParams))[jss::result];
checkErrorValue(jrr, "malformedAddress", "");
}
{
// ripple_state malformed currency.
Json::Value jvParams;
jvParams[jss::ripple_state] = Json::objectValue;
jvParams[jss::ripple_state][jss::accounts] = Json::arrayValue;
jvParams[jss::ripple_state][jss::accounts][0u] = alice.human();
jvParams[jss::ripple_state][jss::accounts][1u] = gw.human();
jvParams[jss::ripple_state][jss::currency] = "USDollars";
jvParams[jss::ledger_hash] = ledgerHash;
Json::Value const jrr = env.rpc(
"json", "ledger_entry", to_string(jvParams))[jss::result];
checkErrorValue(jrr, "malformedCurrency", "");
std::string const ledgerHash{to_string(env.closed()->info().hash)};
{
// Request the trust line using the accounts and currency.
Json::Value jvParams;
jvParams[fieldName] = Json::objectValue;
jvParams[fieldName][jss::accounts] = Json::arrayValue;
jvParams[fieldName][jss::accounts][0u] = alice.human();
jvParams[fieldName][jss::accounts][1u] = gw.human();
jvParams[fieldName][jss::currency] = "USD";
jvParams[jss::ledger_hash] = ledgerHash;
Json::Value const jrr = env.rpc(
"json", "ledger_entry", to_string(jvParams))[jss::result];
BEAST_EXPECT(
jrr[jss::node][sfBalance.jsonName][jss::value] == "-97");
BEAST_EXPECT(
jrr[jss::node][sfHighLimit.jsonName][jss::value] == "999");
}
{
// ripple_state is not an object.
Json::Value jvParams;
jvParams[fieldName] = "ripple_state";
jvParams[jss::ledger_hash] = ledgerHash;
Json::Value const jrr = env.rpc(
"json", "ledger_entry", to_string(jvParams))[jss::result];
checkErrorValue(jrr, "malformedRequest", "");
}
{
// ripple_state.currency is missing.
Json::Value jvParams;
jvParams[fieldName] = Json::objectValue;
jvParams[fieldName][jss::accounts] = Json::arrayValue;
jvParams[fieldName][jss::accounts][0u] = alice.human();
jvParams[fieldName][jss::accounts][1u] = gw.human();
jvParams[jss::ledger_hash] = ledgerHash;
Json::Value const jrr = env.rpc(
"json", "ledger_entry", to_string(jvParams))[jss::result];
checkErrorValue(jrr, "malformedRequest", "");
}
{
// ripple_state accounts is not an array.
Json::Value jvParams;
jvParams[fieldName] = Json::objectValue;
jvParams[fieldName][jss::accounts] = 2;
jvParams[fieldName][jss::currency] = "USD";
jvParams[jss::ledger_hash] = ledgerHash;
Json::Value const jrr = env.rpc(
"json", "ledger_entry", to_string(jvParams))[jss::result];
checkErrorValue(jrr, "malformedRequest", "");
}
{
// ripple_state one of the accounts is missing.
Json::Value jvParams;
jvParams[fieldName] = Json::objectValue;
jvParams[fieldName][jss::accounts] = Json::arrayValue;
jvParams[fieldName][jss::accounts][0u] = alice.human();
jvParams[fieldName][jss::currency] = "USD";
jvParams[jss::ledger_hash] = ledgerHash;
Json::Value const jrr = env.rpc(
"json", "ledger_entry", to_string(jvParams))[jss::result];
checkErrorValue(jrr, "malformedRequest", "");
}
{
// ripple_state more than 2 accounts.
Json::Value jvParams;
jvParams[fieldName] = Json::objectValue;
jvParams[fieldName][jss::accounts] = Json::arrayValue;
jvParams[fieldName][jss::accounts][0u] = alice.human();
jvParams[fieldName][jss::accounts][1u] = gw.human();
jvParams[fieldName][jss::accounts][2u] = alice.human();
jvParams[fieldName][jss::currency] = "USD";
jvParams[jss::ledger_hash] = ledgerHash;
Json::Value const jrr = env.rpc(
"json", "ledger_entry", to_string(jvParams))[jss::result];
checkErrorValue(jrr, "malformedRequest", "");
}
{
// ripple_state account[0] is not a string.
Json::Value jvParams;
jvParams[fieldName] = Json::objectValue;
jvParams[fieldName][jss::accounts] = Json::arrayValue;
jvParams[fieldName][jss::accounts][0u] = 44;
jvParams[fieldName][jss::accounts][1u] = gw.human();
jvParams[fieldName][jss::currency] = "USD";
jvParams[jss::ledger_hash] = ledgerHash;
Json::Value const jrr = env.rpc(
"json", "ledger_entry", to_string(jvParams))[jss::result];
checkErrorValue(jrr, "malformedRequest", "");
}
{
// ripple_state account[1] is not a string.
Json::Value jvParams;
jvParams[fieldName] = Json::objectValue;
jvParams[fieldName][jss::accounts] = Json::arrayValue;
jvParams[fieldName][jss::accounts][0u] = alice.human();
jvParams[fieldName][jss::accounts][1u] = 21;
jvParams[fieldName][jss::currency] = "USD";
jvParams[jss::ledger_hash] = ledgerHash;
Json::Value const jrr = env.rpc(
"json", "ledger_entry", to_string(jvParams))[jss::result];
checkErrorValue(jrr, "malformedRequest", "");
}
{
// ripple_state account[0] == account[1].
Json::Value jvParams;
jvParams[fieldName] = Json::objectValue;
jvParams[fieldName][jss::accounts] = Json::arrayValue;
jvParams[fieldName][jss::accounts][0u] = alice.human();
jvParams[fieldName][jss::accounts][1u] = alice.human();
jvParams[fieldName][jss::currency] = "USD";
jvParams[jss::ledger_hash] = ledgerHash;
Json::Value const jrr = env.rpc(
"json", "ledger_entry", to_string(jvParams))[jss::result];
checkErrorValue(jrr, "malformedRequest", "");
}
{
// ripple_state malformed account[0].
Json::Value jvParams;
jvParams[fieldName] = Json::objectValue;
jvParams[fieldName][jss::accounts] = Json::arrayValue;
jvParams[fieldName][jss::accounts][0u] =
makeBadAddress(alice.human());
jvParams[fieldName][jss::accounts][1u] = gw.human();
jvParams[fieldName][jss::currency] = "USD";
jvParams[jss::ledger_hash] = ledgerHash;
Json::Value const jrr = env.rpc(
"json", "ledger_entry", to_string(jvParams))[jss::result];
checkErrorValue(jrr, "malformedAddress", "");
}
{
// ripple_state malformed account[1].
Json::Value jvParams;
jvParams[fieldName] = Json::objectValue;
jvParams[fieldName][jss::accounts] = Json::arrayValue;
jvParams[fieldName][jss::accounts][0u] = alice.human();
jvParams[fieldName][jss::accounts][1u] =
makeBadAddress(gw.human());
jvParams[fieldName][jss::currency] = "USD";
jvParams[jss::ledger_hash] = ledgerHash;
Json::Value const jrr = env.rpc(
"json", "ledger_entry", to_string(jvParams))[jss::result];
checkErrorValue(jrr, "malformedAddress", "");
}
{
// ripple_state malformed currency.
Json::Value jvParams;
jvParams[fieldName] = Json::objectValue;
jvParams[fieldName][jss::accounts] = Json::arrayValue;
jvParams[fieldName][jss::accounts][0u] = alice.human();
jvParams[fieldName][jss::accounts][1u] = gw.human();
jvParams[fieldName][jss::currency] = "USDollars";
jvParams[jss::ledger_hash] = ledgerHash;
Json::Value const jrr = env.rpc(
"json", "ledger_entry", to_string(jvParams))[jss::result];
checkErrorValue(jrr, "malformedCurrency", "");
}
}
}

Expand Down Expand Up @@ -3055,6 +3059,33 @@ class LedgerRPC_test : public beast::unit_test::suite
}
}

void
testLedgerEntryCLI()
{
testcase("ledger_entry command-line");
using namespace test::jtx;

Env env{*this};
Account const alice{"alice"};
env.fund(XRP(10000), alice);
env.close();

auto const checkId = keylet::check(env.master, env.seq(env.master));

env(check::create(env.master, alice, XRP(100)));
env.close();

std::string const ledgerHash{to_string(env.closed()->info().hash)};
{
// Request a check.
Json::Value const jrr =
env.rpc("ledger_entry", to_string(checkId.key))[jss::result];
BEAST_EXPECT(
jrr[jss::node][sfLedgerEntryType.jsonName] == jss::Check);
BEAST_EXPECT(jrr[jss::node][sfSendMax.jsonName] == "100000000");
}
}

public:
void
run() override
Expand Down Expand Up @@ -3085,6 +3116,7 @@ class LedgerRPC_test : public beast::unit_test::suite
testInvalidOracleLedgerEntry();
testOracleLedgerEntry();
testLedgerEntryMPT();
testLedgerEntryCLI();

forAllApiVersions(std::bind_front(
&LedgerRPC_test::testLedgerEntryInvalidParams, this));
Expand Down
18 changes: 16 additions & 2 deletions src/xrpld/net/detail/RPCCall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -668,6 +668,21 @@ class RPCParser
return jvRequest;
}

// ledger_entry [id] [<index>]
Json::Value
parseLedgerEntry(Json::Value const& jvParams)
{
Json::Value jvRequest{Json::objectValue};

jvRequest[jss::index] = jvParams[0u].asString();

if (jvParams.size() == 2 &&
!jvParseLedger(jvRequest, jvParams[1u].asString()))
return rpcError(rpcLGR_IDX_MALFORMED);

return jvRequest;
}

// log_level: Get log levels
// log_level <severity>: Set master log level to the
// specified severity log_level <partition> <severity>: Set specified
Expand Down Expand Up @@ -1181,8 +1196,7 @@ class RPCParser
{"ledger_accept", &RPCParser::parseAsIs, 0, 0},
{"ledger_closed", &RPCParser::parseAsIs, 0, 0},
{"ledger_current", &RPCParser::parseAsIs, 0, 0},
// { "ledger_entry", &RPCParser::parseLedgerEntry,
// -1, -1 },
{"ledger_entry", &RPCParser::parseLedgerEntry, 1, 2},
{"ledger_header", &RPCParser::parseLedgerId, 1, 1},
{"ledger_request", &RPCParser::parseLedgerId, 1, 1},
{"log_level", &RPCParser::parseLogLevel, 0, 2},
Expand Down
Loading

0 comments on commit f1ec955

Please sign in to comment.