From 11b64e049cdac77147167304bc4136bc1ce8a2f8 Mon Sep 17 00:00:00 2001 From: JoelKatz Date: Wed, 20 Jul 2016 03:34:08 -0700 Subject: [PATCH] Report frozen lines in gateway_balances(RIPD-1217): * Report lines frozen by the gateway separately * Add unit test for gateway_balances * Clean up some existing code --- Builds/VisualStudio2015/RippleD.vcxproj | 4 + .../VisualStudio2015/RippleD.vcxproj.filters | 3 + src/ripple/protocol/JsonFields.h | 2 + src/ripple/rpc/handlers/GatewayBalances.cpp | 67 ++++---- src/ripple/rpc/tests/GatewayBalances.test.cpp | 156 ++++++++++++++++++ src/ripple/unity/rpcx.cpp | 1 + 6 files changed, 202 insertions(+), 31 deletions(-) create mode 100644 src/ripple/rpc/tests/GatewayBalances.test.cpp diff --git a/Builds/VisualStudio2015/RippleD.vcxproj b/Builds/VisualStudio2015/RippleD.vcxproj index bf452d01e9..b09046c845 100644 --- a/Builds/VisualStudio2015/RippleD.vcxproj +++ b/Builds/VisualStudio2015/RippleD.vcxproj @@ -3357,6 +3357,10 @@ True True + + True + True + True True diff --git a/Builds/VisualStudio2015/RippleD.vcxproj.filters b/Builds/VisualStudio2015/RippleD.vcxproj.filters index 115ffbb8e1..8cb871246d 100644 --- a/Builds/VisualStudio2015/RippleD.vcxproj.filters +++ b/Builds/VisualStudio2015/RippleD.vcxproj.filters @@ -3816,6 +3816,9 @@ ripple\rpc\tests + + ripple\rpc\tests + ripple\rpc\tests diff --git a/src/ripple/protocol/JsonFields.h b/src/ripple/protocol/JsonFields.h index 87f1e01a03..dcc200f3b1 100644 --- a/src/ripple/protocol/JsonFields.h +++ b/src/ripple/protocol/JsonFields.h @@ -172,6 +172,7 @@ JSS ( flags ); // out: paths/Node, AccountOffers, JSS ( forward ); // in: AccountTx JSS ( freeze ); // out: AccountLines JSS ( freeze_peer ); // out: AccountLines +JSS ( frozen_balances ); // out: GatewayBalances JSS ( full ); // in: LedgerClearer, handlers/Ledger JSS ( full_reply ); // out: PathFind JSS ( fullbelow_size ); // in: GetCounts @@ -184,6 +185,7 @@ JSS ( have_header ); // out: InboundLedger JSS ( have_state ); // out: InboundLedger JSS ( have_transactions ); // out: InboundLedger JSS ( hostid ); // out: NetworkOPs +JSS ( hotwallet ); // in: GatewayBalances JSS ( id ); // websocket. JSS ( ident ); // in: AccountCurrencies, AccountInfo, // OwnerInfo diff --git a/src/ripple/rpc/handlers/GatewayBalances.cpp b/src/ripple/rpc/handlers/GatewayBalances.cpp index 01088d28ac..087ca2213d 100644 --- a/src/ripple/rpc/handlers/GatewayBalances.cpp +++ b/src/ripple/rpc/handlers/GatewayBalances.cpp @@ -85,10 +85,8 @@ Json::Value doGatewayBalances (RPC::Context& context) // Parse the specified hotwallet(s), if any std::set hotWallets; - if (params.isMember ("hotwallet")) + if (params.isMember (jss::hotwallet)) { - Json::Value const& hw = params["hotwallet"]; - bool valid = true; auto addHotWallet = [&hotWallets](Json::Value const& j) { @@ -115,6 +113,9 @@ Json::Value doGatewayBalances (RPC::Context& context) return false; }; + Json::Value const& hw = params[jss::hotwallet]; + bool valid = true; + if (hw.isArray()) { for (unsigned i = 0; i < hw.size(); ++i) @@ -140,6 +141,7 @@ Json::Value doGatewayBalances (RPC::Context& context) std::map sums; std::map > hotBalances; std::map > assets; + std::map > frozenBalances; // Traverse the cold wallet's trust lines { @@ -162,7 +164,7 @@ Json::Value doGatewayBalances (RPC::Context& context) if (hotWallets.count (peer) > 0) { - // This is a specified hot wallt + // This is a specified hot wallet hotBalances[peer].push_back (-rs->getBalance ()); } else if (balSign > 0) @@ -170,6 +172,11 @@ Json::Value doGatewayBalances (RPC::Context& context) // This is a gateway asset assets[peer].push_back (rs->getBalance ()); } + else if (rs->getFreeze()) + { + // An obligation the gateway has frozen + frozenBalances[peer].push_back (-rs->getBalance ()); + } else { // normal negative balance, obligation to customer @@ -187,43 +194,41 @@ Json::Value doGatewayBalances (RPC::Context& context) if (! sums.empty()) { - Json::Value& j = (result [jss::obligations] = Json::objectValue); + Json::Value j; for (auto const& e : sums) { j[to_string (e.first)] = e.second.getText (); } + result [jss::obligations] = std::move (j); } - if (! hotBalances.empty()) - { - Json::Value& j = (result [jss::balances] = Json::objectValue); - for (auto const& account : hotBalances) + auto populate = []( + std::map > const& array, + Json::Value& result, + Json::StaticString const& name) { - Json::Value& balanceArray = (j[to_string (account.first)] = Json::arrayValue); - for (auto const& balance : account.second) + if (!array.empty()) { - Json::Value& entry = balanceArray.append (Json::objectValue); - entry[jss::currency] = to_string (balance.issue ().currency); - entry[jss::value] = balance.getText(); + Json::Value j; + for (auto const& account : array) + { + Json::Value balanceArray; + for (auto const& balance : account.second) + { + Json::Value entry; + entry[jss::currency] = to_string (balance.issue ().currency); + entry[jss::value] = balance.getText(); + balanceArray.append (std::move (entry)); + } + j [to_string (account.first)] = std::move (balanceArray); + } + result [name] = std::move (j); } - } - } + }; - if (! assets.empty()) - { - Json::Value& j = (result [jss::assets] = Json::objectValue); - - for (auto const& account : assets) - { - Json::Value& balanceArray = (j[to_string (account.first)] = Json::arrayValue); - for (auto const& balance : account.second) - { - Json::Value& entry = balanceArray.append (Json::objectValue); - entry[jss::currency] = to_string (balance.issue ().currency); - entry[jss::value] = balance.getText(); - } - } - } + populate (hotBalances, result, jss::balances); + populate (frozenBalances, result, jss::frozen_balances); + populate (assets, result, jss::assets); return result; } diff --git a/src/ripple/rpc/tests/GatewayBalances.test.cpp b/src/ripple/rpc/tests/GatewayBalances.test.cpp new file mode 100644 index 0000000000..4f5664e37e --- /dev/null +++ b/src/ripple/rpc/tests/GatewayBalances.test.cpp @@ -0,0 +1,156 @@ +//------------------------------------------------------------------------------ +/* + This file is part of rippled: https://github.com/ripple/rippled + Copyright (c) 2012, 2013 Ripple Labs Inc. + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ +//============================================================================== + +#include +#include +#include +#include +#include + +namespace ripple { +namespace test { + +class GatewayBalances_test : public beast::unit_test::suite +{ +public: + + void + testGWB() + { + using namespace std::chrono_literals; + using namespace jtx; + Env env(*this); + + // Gateway account and assets + Account const alice {"alice"}; + env.fund(XRP(10000), "alice"); + auto USD = alice["USD"]; + auto CNY = alice["CNY"]; + auto JPY = alice["JPY"]; + + // Create a hotwallet + Account const hw {"hw"}; + env.fund(XRP(10000), "hw"); + env(trust(hw, USD(10000))); + env(trust(hw, JPY(10000))); + env(pay(alice, hw, USD(5000))); + env(pay(alice, hw, JPY(5000))); + + // Create some clients + Account const bob {"bob"}; + env.fund(XRP(10000), "bob"); + env(trust(bob, USD(100))); + env(trust(bob, CNY(100))); + env(pay(alice, bob, USD(50))); + + Account const charley {"charley"}; + env.fund(XRP(10000), "charley"); + env(trust(charley, CNY(500))); + env(trust(charley, JPY(500))); + env(pay(alice, charley, CNY(250))); + env(pay(alice, charley, JPY(250))); + + Account const dave {"dave"}; + env.fund(XRP(10000), "dave"); + env(trust(dave, CNY(100))); + env(pay(alice, dave, CNY(30))); + + // give the gateway an asset + env(trust(alice, charley["USD"](50))); + env(pay(charley, alice, USD(10))); + + // freeze dave + env(trust(alice, dave["CNY"](0), dave, tfSetFreeze)); + + env.close(); + + auto wsc = makeWSClient(env.app().config()); + + Json::Value qry; + qry[jss::account] = alice.human(); + qry[jss::hotwallet] = hw.human(); + + auto jv = wsc->invoke("gateway_balances", qry); + expect(jv[jss::status] == "success"); + + auto const& result = jv[jss::result]; + expect(result[jss::account] == alice.human()); + expect(result[jss::status] == "success"); + + { + auto const& balances = result[jss::balances]; + expect (balances.isObject(), "balances is not an object"); + expect (balances.size() == 1, "balances size is not 1"); + + auto const& hwBalance = balances[hw.human()]; + expect (hwBalance.isArray(), "hwBalance is not an array"); + expect (hwBalance.size() == 2); + auto c1 = hwBalance[0u][jss::currency]; + auto c2 = hwBalance[1u][jss::currency]; + expect (c1 == "USD" || c2 == "USD"); + expect (c1 == "JPY" || c2 == "JPY"); + expect (hwBalance[0u][jss::value] == "5000" && + hwBalance[1u][jss::value] == "5000"); + } + + { + auto const& fBalances = result[jss::frozen_balances]; + expect (fBalances.isObject()); + expect (fBalances.size() == 1); + + auto const& fBal = fBalances[dave.human()]; + expect (fBal.isArray()); + expect (fBal.size() == 1); + expect (fBal[0u].isObject()); + expect (fBal[0u][jss::currency] == "CNY"); + expect (fBal[0u][jss::value] == "30"); + } + + { + auto const& assets = result[jss::assets]; + expect (assets.isObject(), "assets it not an object"); + expect (assets.size() == 1, "assets size is not 1"); + + auto const& cAssets = assets[charley.human()]; + expect (cAssets.isArray()); + expect (cAssets.size() == 1); + expect (cAssets[0u][jss::currency] == "USD"); + expect (cAssets[0u][jss::value] == "10"); + } + + { + auto const& obligations = result[jss::obligations]; + expect (obligations.isObject(), "obligations is not an object"); + expect (obligations.size() == 3); + expect (obligations["CNY"] == "250"); + expect (obligations["JPY"] == "250"); + expect (obligations["USD"] == "50"); + } + + } + + void + run() override + { + testGWB(); + } +}; + +BEAST_DEFINE_TESTSUITE(GatewayBalances,app,ripple); + +} // test +} // ripple diff --git a/src/ripple/unity/rpcx.cpp b/src/ripple/unity/rpcx.cpp index 1ddfc28e8b..48e48f4639 100644 --- a/src/ripple/unity/rpcx.cpp +++ b/src/ripple/unity/rpcx.cpp @@ -98,6 +98,7 @@ #include #include #include +#include #include #include #include