Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: clean up LedgerEntry.cpp #5199

Open
wants to merge 6 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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})
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These tests were only updated to cover the two aliases, and are otherwise completely unchanged.

{
// 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
Loading