Skip to content

Commit 18d76d3

Browse files
authored
fix delivered amount (#310)
* fix delivered amount
1 parent 849a443 commit 18d76d3

File tree

4 files changed

+93
-123
lines changed

4 files changed

+93
-123
lines changed

src/ripple/rpc/impl/DeliveredAmount.cpp

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -61,22 +61,7 @@ getDeliveredAmount(
6161

6262
if (serializedTx->isFieldPresent(sfAmount))
6363
{
64-
using namespace std::chrono_literals;
65-
66-
// Ledger 4594095 is the first ledger in which the DeliveredAmount field
67-
// was present when a partial payment was made and its absence indicates
68-
// that the amount delivered is listed in the Amount field.
69-
//
70-
// If the ledger closed long after the DeliveredAmount code was deployed
71-
// then its absence indicates that the amount delivered is listed in the
72-
// Amount field. DeliveredAmount went live January 24, 2014.
73-
// 446000000 is in Feb 2014, well after DeliveredAmount went live
74-
if (getLedgerIndex() >= 4594095 ||
75-
getCloseTime() > NetClock::time_point{446000000s} ||
76-
(serializedTx && serializedTx->isFieldPresent(sfNetworkID)))
77-
{
78-
return serializedTx->getFieldAmount(sfAmount);
79-
}
64+
return serializedTx->getFieldAmount(sfAmount);
8065
}
8166

8267
return {};

src/test/app/SetHookTSH_test.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5466,6 +5466,7 @@ struct SetHookTSH_test : public beast::unit_test::suite
54665466
params[jss::transaction] = txIds[i];
54675467
auto const jrr = env.rpc("json", "tx", to_string(params));
54685468
auto const meta = jrr[jss::result][jss::meta];
5469+
BEAST_EXPECT(meta[jss::delivered_amount] == "1000000");
54695470
for (auto const& node : meta[sfAffectedNodes.jsonName])
54705471
{
54715472
auto const nodeType = node[sfLedgerEntryType.jsonName];

src/test/rpc/DeliveredAmount_test.cpp

Lines changed: 91 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -191,80 +191,73 @@ class DeliveredAmount_test : public beast::unit_test::suite
191191
auto const gw = Account("gateway");
192192
auto const USD = gw["USD"];
193193

194-
for (bool const afterSwitchTime : {true, false})
194+
Env env{*this, features};
195+
env.fund(XRP(10000), alice, bob, carol, gw);
196+
env.trust(USD(1000), alice, bob, carol);
197+
env.close();
198+
199+
CheckDeliveredAmount checkDeliveredAmount{true};
195200
{
196-
Env env{*this, features};
197-
env.fund(XRP(10000), alice, bob, carol, gw);
198-
env.trust(USD(1000), alice, bob, carol);
199-
if (afterSwitchTime)
200-
env.close(NetClock::time_point{446000000s});
201-
else
202-
env.close();
201+
// add payments, but do no close until subscribed
203202

204-
CheckDeliveredAmount checkDeliveredAmount{afterSwitchTime};
205-
{
206-
// add payments, but do no close until subscribed
207-
208-
// normal payments
209-
env(pay(gw, alice, USD(50)));
210-
checkDeliveredAmount.adjCountersSuccess();
211-
env(pay(gw, alice, XRP(50)));
212-
checkDeliveredAmount.adjCountersSuccess();
213-
214-
// partial payment
215-
env(pay(gw, bob, USD(9999999)), txflags(tfPartialPayment));
216-
checkDeliveredAmount.adjCountersPartialPayment();
217-
env.require(balance(bob, USD(1000)));
218-
219-
// failed payment
220-
env(pay(bob, carol, USD(9999999)), ter(tecPATH_PARTIAL));
221-
checkDeliveredAmount.adjCountersFail();
222-
env.require(balance(carol, USD(0)));
223-
}
203+
// normal payments
204+
env(pay(gw, alice, USD(50)));
205+
checkDeliveredAmount.adjCountersSuccess();
206+
env(pay(gw, alice, XRP(50)));
207+
checkDeliveredAmount.adjCountersSuccess();
224208

225-
auto wsc = makeWSClient(env.app().config());
209+
// partial payment
210+
env(pay(gw, bob, USD(9999999)), txflags(tfPartialPayment));
211+
checkDeliveredAmount.adjCountersPartialPayment();
212+
env.require(balance(bob, USD(1000)));
226213

214+
// failed payment
215+
env(pay(bob, carol, USD(9999999)), ter(tecPATH_PARTIAL));
216+
checkDeliveredAmount.adjCountersFail();
217+
env.require(balance(carol, USD(0)));
218+
}
219+
220+
auto wsc = makeWSClient(env.app().config());
221+
222+
{
223+
Json::Value stream;
224+
// RPC subscribe to ledger stream
225+
stream[jss::streams] = Json::arrayValue;
226+
stream[jss::streams].append("ledger");
227+
stream[jss::accounts] = Json::arrayValue;
228+
stream[jss::accounts].append(toBase58(alice.id()));
229+
stream[jss::accounts].append(toBase58(bob.id()));
230+
stream[jss::accounts].append(toBase58(carol.id()));
231+
auto jv = wsc->invoke("subscribe", stream);
232+
if (wsc->version() == 2)
227233
{
228-
Json::Value stream;
229-
// RPC subscribe to ledger stream
230-
stream[jss::streams] = Json::arrayValue;
231-
stream[jss::streams].append("ledger");
232-
stream[jss::accounts] = Json::arrayValue;
233-
stream[jss::accounts].append(toBase58(alice.id()));
234-
stream[jss::accounts].append(toBase58(bob.id()));
235-
stream[jss::accounts].append(toBase58(carol.id()));
236-
auto jv = wsc->invoke("subscribe", stream);
237-
if (wsc->version() == 2)
238-
{
239-
BEAST_EXPECT(
240-
jv.isMember(jss::jsonrpc) && jv[jss::jsonrpc] == "2.0");
241-
BEAST_EXPECT(
242-
jv.isMember(jss::ripplerpc) &&
243-
jv[jss::ripplerpc] == "2.0");
244-
BEAST_EXPECT(jv.isMember(jss::id) && jv[jss::id] == 5);
245-
}
246-
BEAST_EXPECT(jv[jss::result][jss::ledger_index] == 3);
234+
BEAST_EXPECT(
235+
jv.isMember(jss::jsonrpc) && jv[jss::jsonrpc] == "2.0");
236+
BEAST_EXPECT(
237+
jv.isMember(jss::ripplerpc) && jv[jss::ripplerpc] == "2.0");
238+
BEAST_EXPECT(jv.isMember(jss::id) && jv[jss::id] == 5);
247239
}
240+
BEAST_EXPECT(jv[jss::result][jss::ledger_index] == 3);
241+
}
242+
{
243+
env.close();
244+
// Check stream update
245+
while (true)
248246
{
249-
env.close();
250-
// Check stream update
251-
while (true)
252-
{
253-
auto const r = wsc->findMsg(1s, [&](auto const& jv) {
254-
return jv[jss::ledger_index] == 4;
255-
});
256-
if (!r)
257-
break;
247+
auto const r = wsc->findMsg(1s, [&](auto const& jv) {
248+
return jv[jss::ledger_index] == 4;
249+
});
250+
if (!r)
251+
break;
258252

259-
if (!r->isMember(jss::transaction))
260-
continue;
253+
if (!r->isMember(jss::transaction))
254+
continue;
261255

262-
BEAST_EXPECT(checkDeliveredAmount.checkTxn(
263-
(*r)[jss::transaction], (*r)[jss::meta]));
264-
}
256+
BEAST_EXPECT(checkDeliveredAmount.checkTxn(
257+
(*r)[jss::transaction], (*r)[jss::meta]));
265258
}
266-
BEAST_EXPECT(checkDeliveredAmount.checkExpectedCounters());
267259
}
260+
BEAST_EXPECT(checkDeliveredAmount.checkExpectedCounters());
268261
}
269262
void
270263
testTxDeliveredAmountRPC(FeatureBitset features)
@@ -280,49 +273,41 @@ class DeliveredAmount_test : public beast::unit_test::suite
280273
auto const gw = Account("gateway");
281274
auto const USD = gw["USD"];
282275

283-
for (bool const afterSwitchTime : {true, false})
284-
{
285-
Env env{*this, features};
286-
env.fund(XRP(10000), alice, bob, carol, gw);
287-
env.trust(USD(1000), alice, bob, carol);
288-
if (afterSwitchTime)
289-
env.close(NetClock::time_point{446000000s});
290-
else
291-
env.close();
292-
293-
CheckDeliveredAmount checkDeliveredAmount{afterSwitchTime};
294-
// normal payments
295-
env(pay(gw, alice, USD(50)));
296-
checkDeliveredAmount.adjCountersSuccess();
297-
env(pay(gw, alice, XRP(50)));
298-
checkDeliveredAmount.adjCountersSuccess();
299-
300-
// partial payment
301-
env(pay(gw, bob, USD(9999999)), txflags(tfPartialPayment));
302-
checkDeliveredAmount.adjCountersPartialPayment();
303-
env.require(balance(bob, USD(1000)));
304-
305-
// failed payment
306-
env(pay(gw, carol, USD(9999999)), ter(tecPATH_PARTIAL));
307-
checkDeliveredAmount.adjCountersFail();
308-
env.require(balance(carol, USD(0)));
309-
310-
env.close();
311-
std::string index;
312-
Json::Value jvParams;
313-
jvParams[jss::ledger_index] = 4u;
314-
jvParams[jss::transactions] = true;
315-
jvParams[jss::expand] = true;
316-
auto const jtxn = env.rpc(
317-
"json",
318-
"ledger",
319-
to_string(
320-
jvParams))[jss::result][jss::ledger][jss::transactions];
321-
for (auto const& t : jtxn)
322-
BEAST_EXPECT(
323-
checkDeliveredAmount.checkTxn(t, t[jss::metaData]));
324-
BEAST_EXPECT(checkDeliveredAmount.checkExpectedCounters());
325-
}
276+
Env env{*this, features};
277+
env.fund(XRP(10000), alice, bob, carol, gw);
278+
env.trust(USD(1000), alice, bob, carol);
279+
env.close();
280+
281+
CheckDeliveredAmount checkDeliveredAmount{true};
282+
// normal payments
283+
env(pay(gw, alice, USD(50)));
284+
checkDeliveredAmount.adjCountersSuccess();
285+
env(pay(gw, alice, XRP(50)));
286+
checkDeliveredAmount.adjCountersSuccess();
287+
288+
// partial payment
289+
env(pay(gw, bob, USD(9999999)), txflags(tfPartialPayment));
290+
checkDeliveredAmount.adjCountersPartialPayment();
291+
env.require(balance(bob, USD(1000)));
292+
293+
// failed payment
294+
env(pay(gw, carol, USD(9999999)), ter(tecPATH_PARTIAL));
295+
checkDeliveredAmount.adjCountersFail();
296+
env.require(balance(carol, USD(0)));
297+
298+
env.close();
299+
std::string index;
300+
Json::Value jvParams;
301+
jvParams[jss::ledger_index] = 4u;
302+
jvParams[jss::transactions] = true;
303+
jvParams[jss::expand] = true;
304+
auto const jtxn = env.rpc(
305+
"json",
306+
"ledger",
307+
to_string(jvParams))[jss::result][jss::ledger][jss::transactions];
308+
for (auto const& t : jtxn)
309+
BEAST_EXPECT(checkDeliveredAmount.checkTxn(t, t[jss::metaData]));
310+
BEAST_EXPECT(checkDeliveredAmount.checkExpectedCounters());
326311
}
327312

328313
public:

src/test/rpc/ServerDefinitions_test.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ class ServerDefinitions_test : public beast::unit_test::suite
5757
{
5858
Env env(*this);
5959
auto const result = env.rpc("server_definitions");
60-
std::cout << "RESULT: " << result << "\n";
6160
BEAST_EXPECT(!result[jss::result].isMember(jss::error));
6261
BEAST_EXPECT(result[jss::result].isMember(jss::FIELDS));
6362
BEAST_EXPECT(result[jss::result].isMember(jss::LEDGER_ENTRY_TYPES));

0 commit comments

Comments
 (0)