Skip to content

Commit

Permalink
Do not default construct account ids
Browse files Browse the repository at this point in the history
  • Loading branch information
drlongle committed Mar 30, 2023
1 parent 53ca2c3 commit ef7dae7
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 44 deletions.
28 changes: 11 additions & 17 deletions src/ripple/rpc/handlers/AccountChannels.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,17 +89,12 @@ doAccountChannels(RPC::JsonContext& context)
std::string strDst;
if (params.isMember(jss::destination_account))
strDst = params[jss::destination_account].asString();
auto hasDst = !strDst.empty();

std::optional<AccountID> raDstAccount{AccountID()};
if (hasDst)
{
raDstAccount = parseBase58<AccountID>(strDst);
if (!raDstAccount)
{
rpcError(rpcACT_MALFORMED);
}
}
auto const raDstAccount = [&]() -> std::optional<AccountID> {
return strDst.empty() ? std::nullopt : parseBase58<AccountID>(strDst);
}();
if (!strDst.empty() && !raDstAccount)
return rpcError(rpcACT_MALFORMED);

unsigned int limit;
if (auto err = readLimitField(limit, RPC::Tuning::accountChannels, context))
Expand All @@ -113,10 +108,9 @@ doAccountChannels(RPC::JsonContext& context)
{
std::vector<std::shared_ptr<SLE const>> items;
AccountID const& accountID;
bool hasDst;
AccountID const& raDstAccount;
std::optional<AccountID> const& raDstAccount;
};
VisitData visitData = {{}, *accountID, hasDst, *raDstAccount};
VisitData visitData = {{}, *accountID, raDstAccount};
visitData.items.reserve(limit);
uint256 startAfter = beast::zero;
std::uint64_t startHint = 0;
Expand Down Expand Up @@ -155,7 +149,7 @@ doAccountChannels(RPC::JsonContext& context)
if (!sle)
return rpcError(rpcINVALID_PARAMS);

if (!RPC::isRelatedToAccount(*ledger, sle, accountID))
if (!RPC::isRelatedToAccount(*ledger, sle, *accountID))
return rpcError(rpcINVALID_PARAMS);
}

Expand All @@ -168,7 +162,7 @@ doAccountChannels(RPC::JsonContext& context)
startAfter,
startHint,
limit + 1,
[&visitData, accountID, &count, &limit, &marker, &nextHint](
[&visitData, &accountID, &count, &limit, &marker, &nextHint](
std::shared_ptr<SLE const> const& sleCur) {
if (!sleCur)
{
Expand All @@ -184,8 +178,8 @@ doAccountChannels(RPC::JsonContext& context)

if (count <= limit && sleCur->getType() == ltPAYCHAN &&
(*sleCur)[sfAccount] == *accountID &&
(!visitData.hasDst ||
visitData.raDstAccount == (*sleCur)[sfDestination]))
(!visitData.raDstAccount ||
*visitData.raDstAccount == (*sleCur)[sfDestination]))
{
visitData.items.emplace_back(sleCur);
}
Expand Down
43 changes: 18 additions & 25 deletions src/ripple/rpc/handlers/AccountLines.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,6 @@

namespace ripple {

struct VisitData
{
std::vector<RPCTrustLine> items;
AccountID const& accountID;
bool hasPeer;
AccountID const& raPeerAccount;

bool ignoreDefault;
uint32_t foundCount;
};

void
addLine(Json::Value& jsonLines, RPCTrustLine const& line)
{
Expand Down Expand Up @@ -110,17 +99,14 @@ doAccountLines(RPC::JsonContext& context)
std::string strPeer;
if (params.isMember(jss::peer))
strPeer = params[jss::peer].asString();
auto hasPeer = !strPeer.empty();

std::optional<AccountID> raPeerAccount{AccountID()};
if (hasPeer)
auto const raPeerAccount = [&]() -> std::optional<AccountID> {
return strPeer.empty() ? std::nullopt : parseBase58<AccountID>(strPeer);
}();
if (!strPeer.empty() && !raPeerAccount)
{
raPeerAccount = parseBase58<AccountID>(strPeer);
if (!raPeerAccount)
{
RPC::inject_error(rpcACT_MALFORMED, result);
return result;
}
RPC::inject_error(rpcACT_MALFORMED, result);
return result;
}

unsigned int limit;
Expand All @@ -136,8 +122,15 @@ doAccountLines(RPC::JsonContext& context)
params[jss::ignore_default].asBool();

Json::Value& jsonLines(result[jss::lines] = Json::arrayValue);
VisitData visitData = {
{}, *accountID, hasPeer, *raPeerAccount, ignoreDefault, 0};
struct VisitData
{
std::vector<RPCTrustLine> items;
AccountID const& accountID;
std::optional<AccountID> const& raPeerAccount;
bool ignoreDefault;
uint32_t foundCount;
};
VisitData visitData = {{}, *accountID, raPeerAccount, ignoreDefault, 0};
uint256 startAfter = beast::zero;
std::uint64_t startHint = 0;

Expand Down Expand Up @@ -175,7 +168,7 @@ doAccountLines(RPC::JsonContext& context)
if (!sle)
return rpcError(rpcINVALID_PARAMS);

if (!RPC::isRelatedToAccount(*ledger, sle, accountID))
if (!RPC::isRelatedToAccount(*ledger, sle, *accountID))
return rpcError(rpcINVALID_PARAMS);
}

Expand Down Expand Up @@ -225,8 +218,8 @@ doAccountLines(RPC::JsonContext& context)
RPCTrustLine::makeItem(visitData.accountID, sleCur);

if (line &&
(!visitData.hasPeer ||
visitData.raPeerAccount ==
(!visitData.raPeerAccount ||
*visitData.raPeerAccount ==
line->getAccountIDPeer()))
{
visitData.items.emplace_back(*line);
Expand Down
4 changes: 2 additions & 2 deletions src/ripple/rpc/handlers/AccountOffers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ doAccountOffers(RPC::JsonContext& context)
if (!sle)
return rpcError(rpcINVALID_PARAMS);

if (!RPC::isRelatedToAccount(*ledger, sle, accountID))
if (!RPC::isRelatedToAccount(*ledger, sle, *accountID))
return rpcError(rpcINVALID_PARAMS);
}

Expand All @@ -138,7 +138,7 @@ doAccountOffers(RPC::JsonContext& context)
startAfter,
startHint,
limit + 1,
[&offers, &count, &marker, &limit, &nextHint, accountID](
[&offers, &count, &marker, &limit, &nextHint, &accountID](
std::shared_ptr<SLE const> const& sle) {
if (!sle)
{
Expand Down

0 comments on commit ef7dae7

Please sign in to comment.