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

fix(gateway_balances): handle overflow exception #4355

Merged
merged 2 commits into from
Mar 16, 2023
Merged
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
18 changes: 17 additions & 1 deletion src/ripple/rpc/handlers/GatewayBalances.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,23 @@ doGatewayBalances(RPC::JsonContext& context)
bal = -rs->getBalance();
}
else
bal -= rs->getBalance();
{
try
{
bal -= rs->getBalance();
}
catch (std::runtime_error const&)
{
// 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