Skip to content

Commit

Permalink
[FOLD] Treat gateway_balances obligations overflow as max STAmount
Browse files Browse the repository at this point in the history
  • Loading branch information
scottschurr authored and RichardAH committed Mar 15, 2023
1 parent 0d0464f commit 36ffe0c
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 3 deletions.
1 change: 0 additions & 1 deletion src/ripple/protocol/jss.h
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,6 @@ JSS(open); // out: handlers/Ledger
JSS(open_ledger_cost); // out: SubmitTransaction
JSS(open_ledger_fee); // out: TxQ
JSS(open_ledger_level); // out: TxQ
JSS(overflow); // out: gateway_balances
JSS(owner); // in: LedgerEntry, out: NetworkOPs
JSS(owner_funds); // in/out: Ledger, NetworkOPs, AcceptedLedgerTx
JSS(page_index);
Expand Down
11 changes: 9 additions & 2 deletions src/ripple/rpc/handlers/GatewayBalances.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,9 +189,16 @@ doGatewayBalances(RPC::JsonContext& context)
{
bal -= rs->getBalance();
}
catch (std::runtime_error& e)
catch (std::runtime_error const&)
{
result[jss::overflow] = "true";
// Presumably the exception was caused by overflow.
// On overflow return the largest valid STAmount.
// Very large sums of STAmount are approximations
// anyway.
bal = STAmount(
bal.issue(),
STAmount::cMaxValue,
STAmount::cMaxOffset);
}
}
}
Expand Down
56 changes: 56 additions & 0 deletions src/test/rpc/GatewayBalances_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,13 +148,69 @@ class GatewayBalances_test : public beast::unit_test::suite
}
}

void
testGWBOverflow()
{
using namespace std::chrono_literals;
using namespace jtx;
Env env(*this);

// Gateway account and assets
Account const alice{"alice"};
env.fund(XRP(10000), alice);
env.close();
auto USD = alice["USD"];

// The largest valid STAmount of USD:
STAmount const maxUSD(
USD.issue(), STAmount::cMaxValue, STAmount::cMaxOffset);

// Create a hotwallet
Account const hw{"hw"};
env.fund(XRP(10000), hw);
env(trust(hw, maxUSD));
env.close();
env(pay(alice, hw, maxUSD));

// Create some clients
Account const bob{"bob"};
env.fund(XRP(10000), bob);
env(trust(bob, maxUSD));
env.close();
env(pay(alice, bob, maxUSD));

Account const charley{"charley"};
env.fund(XRP(10000), charley);
env(trust(charley, maxUSD));
env.close();
env(pay(alice, charley, maxUSD));

env.close();

auto wsc = makeWSClient(env.app().config());

Json::Value query;
query[jss::account] = alice.human();
query[jss::hotwallet] = hw.human();

// Note that the sum of bob's and charley's USD balances exceeds
// the amount that can be represented in an STAmount. Nevertheless
// we get a valid "obligations" that shows the maximum valid
// STAmount.
auto jv = wsc->invoke("gateway_balances", query);
expect(jv[jss::status] == "success");
expect(jv[jss::result][jss::obligations]["USD"] == maxUSD.getText());
}

void
run() override
{
using namespace jtx;
auto const sa = supported_amendments();
testGWB(sa - featureFlowCross);
testGWB(sa);

testGWBOverflow();
}
};

Expand Down

0 comments on commit 36ffe0c

Please sign in to comment.