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

ignore_default option on account_lines #3980

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions src/ripple/protocol/jss.h
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,7 @@ JSS(hotwallet); // in: GatewayBalances
JSS(id); // websocket.
JSS(ident); // in: AccountCurrencies, AccountInfo,
// OwnerInfo
JSS(ignore_default); // in: AccountLines
JSS(inLedger); // out: tx/Transaction
JSS(inbound); // out: PeerImp
JSS(index); // in: LedgerEntry, DownloadShard
Expand Down
74 changes: 64 additions & 10 deletions src/ripple/rpc/handlers/AccountLines.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ struct VisitData
AccountID const& accountID;
bool hasPeer;
AccountID const& raPeerAccount;

bool ignoreDefault;
uint32_t foundCount;
RippleState::pointer lastFound;
};

void
Expand Down Expand Up @@ -78,6 +82,8 @@ addLine(Json::Value& jsonLines, RippleState const& line)
// ledger_index : <ledger_index>
// limit: integer // optional
// marker: opaque // optional, resume previous query
// ignore_default: bool // do not return lines in default state (on
// this account's side)
// }
Json::Value
doAccountLines(RPC::JsonContext& context)
Expand Down Expand Up @@ -124,8 +130,14 @@ doAccountLines(RPC::JsonContext& context)
if (auto err = readLimitField(limit, RPC::Tuning::accountLines, context))
return *err;

// this flag allows the requester to ask incoming trustlines in default
// state be omitted
bool ignoreDefault = params.isMember(jss::ignore_default) &&
params[jss::ignore_default].asBool();

Json::Value& jsonLines(result[jss::lines] = Json::arrayValue);
VisitData visitData = {{}, accountID, hasPeer, raPeerAccount};
VisitData visitData = {
{}, accountID, hasPeer, raPeerAccount, ignoreDefault, 0, nullptr};
unsigned int reserve(limit);
uint256 startAfter;
std::uint64_t startHint;
Expand All @@ -147,20 +159,38 @@ doAccountLines(RPC::JsonContext& context)
if (!sleLine)
return rpcError(rpcINVALID_PARAMS);

bool isDefault = false;
if (sleLine->getFieldAmount(sfLowLimit).getIssuer() == accountID)
{
startHint = sleLine->getFieldU64(sfLowNode);
isDefault = !(sleLine->getFieldU32(sfFlags) & lsfLowReserve);
}
else if (sleLine->getFieldAmount(sfHighLimit).getIssuer() == accountID)
{
startHint = sleLine->getFieldU64(sfHighNode);
isDefault = !(sleLine->getFieldU32(sfFlags) & lsfHighReserve);
}
else
return rpcError(rpcINVALID_PARAMS);

// Caller provided the first line (startAfter), add it as first result
auto const line = RippleState::makeItem(accountID, sleLine);
if (line == nullptr)
return rpcError(rpcINVALID_PARAMS);
// (but only if it meets inclusion criteria)

if (isDefault && ignoreDefault)
{
// even though we're starting our search here we don't include the
// first entry in this edge case
visitData.items.reserve(++reserve);
}
else
{
auto const line = RippleState::makeItem(accountID, sleLine);
if (line == nullptr)
return rpcError(rpcINVALID_PARAMS);

addLine(jsonLines, *line);
visitData.items.reserve(reserve);
addLine(jsonLines, *line);
visitData.items.reserve(reserve);
}
}
else
{
Expand All @@ -177,13 +207,30 @@ doAccountLines(RPC::JsonContext& context)
startHint,
reserve,
[&visitData](std::shared_ptr<SLE const> const& sleCur) {
bool ignore = false;
if (visitData.ignoreDefault)
{
if (sleCur->getFieldAmount(sfLowLimit).getIssuer() ==
visitData.accountID)
ignore =
!(sleCur->getFieldU32(sfFlags) & lsfLowReserve);
else
ignore = !(
sleCur->getFieldU32(sfFlags) & lsfHighReserve);
}

auto const line =
RippleState::makeItem(visitData.accountID, sleCur);
if (line != nullptr &&
(!visitData.hasPeer ||
visitData.raPeerAccount == line->getAccountIDPeer()))
{
visitData.items.emplace_back(line);
if (!ignore)
visitData.items.emplace_back(line);

visitData.lastFound = line;
visitData.foundCount++;
Comment on lines +210 to +232
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think this would be somewhat more readable if ignore was include. So it would look something like:

bool include = true;
if (visitData.ignoreDefault)
{
    if (sleCur->getFieldAmount(sfLowLimit).getIssuer() ==
       visitData.accountID)
       include = sleCur->getFieldU32(sfFlags) & lsfLowReserve;
    else
        ignore = sleCur->getFieldU32(sfFlags) & lsfHighReserve;
}
[...]
{
    if (include)
        visitData.items.emplace_back(line);
[...]


return true;
}

Expand All @@ -194,13 +241,20 @@ doAccountLines(RPC::JsonContext& context)
}
}

if (visitData.items.size() == reserve)
// RH Note:
// If ignore_default flag is present all lines must still be iterated, the
// flag only suppresses output. It does not change how iteration works. This
// means the RPC call may return an empty set AND a marker. In this case
// another query must be made until iteration is complete if a complete set
// of non-default state lines are required.
if (visitData.items.size() == reserve || visitData.foundCount >= reserve)
{
result[jss::limit] = limit;

RippleState::pointer line(visitData.items.back());
RippleState::pointer line(visitData.lastFound);
result[jss::marker] = to_string(line->key());
visitData.items.pop_back();
if (visitData.items.back() == visitData.lastFound)
visitData.items.pop_back();
}

result[jss::account] = context.app.accountIDCache().toBase58(accountID);
Expand Down