Skip to content

Commit

Permalink
[FOLD] Update to match the latest XLS-47d
Browse files Browse the repository at this point in the history
* Change LastUpdateTime to Unix Time
* Update the unit-tests for Unix Time
* Rename average to mean
  • Loading branch information
gregtatcam committed Nov 22, 2023
1 parent 6d4c5b0 commit f52a520
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 19 deletions.
11 changes: 8 additions & 3 deletions src/ripple/app/tx/impl/SetOracle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,19 @@ SetOracle::preclaim(PreclaimContext const& ctx)
if (!sleSetter)
return terNO_ACCOUNT;

// lastUpdateTime must be within 30 seconds of the last closed ledger
// lastUpdateTime must be within maxLastUpdateTimeDelta seconds
// of the last closed ledger
using namespace std::chrono;
std::size_t const closeTime =
duration_cast<seconds>(ctx.view.info().closeTime.time_since_epoch())
.count();
std::size_t const lastUpdateTime = ctx.tx[sfLastUpdateTime];
if (lastUpdateTime < closeTime ||
lastUpdateTime > (closeTime + maxLastUpdateTimeDelta))
if (lastUpdateTime <= epoch_offset.count())
return tecINVALID_UPDATE_TIME;
std::size_t const lastUpdateTimeEpoch =
lastUpdateTime - epoch_offset.count();
if (lastUpdateTimeEpoch < closeTime ||
lastUpdateTimeEpoch > (closeTime + maxLastUpdateTimeDelta))
return tecINVALID_UPDATE_TIME;

hash_set<uint256> pairs;
Expand Down
2 changes: 1 addition & 1 deletion src/ripple/protocol/jss.h
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,6 @@ JSS(auth_accounts); // out: amm_info
JSS(auth_change); // out: AccountInfo
JSS(auth_change_queued); // out: AccountInfo
JSS(available); // out: ValidatorList
JSS(average); // out: get_aggregate_price
JSS(avg_bps_recv); // out: Peers
JSS(avg_bps_sent); // out: Peers
JSS(balance); // out: AccountLines
Expand Down Expand Up @@ -476,6 +475,7 @@ JSS(max_ledger); // in/out: LedgerCleaner
JSS(max_queue_size); // out: TxQ
JSS(max_spend_drops); // out: AccountInfo
JSS(max_spend_drops_total); // out: AccountInfo
JSS(mean); // out: get_aggregate_price
JSS(median); // out: get_aggregate_price
JSS(median_fee); // out: TxQ
JSS(median_level); // out: TxQ
Expand Down
4 changes: 2 additions & 2 deletions src/ripple/rpc/handlers/GetAggregatePrice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ doGetAggregatePrice(RPC::JsonContext& context)
// calculate stats
auto const [avg, sd, size] =
getStats(prices.right.begin(), prices.right.end());
result[jss::entire_set][jss::average] = avg.getText();
result[jss::entire_set][jss::mean] = avg.getText();
result[jss::entire_set][jss::size] = size;
result[jss::entire_set][jss::standard_deviation] = to_string(sd);

Expand Down Expand Up @@ -308,7 +308,7 @@ doGetAggregatePrice(RPC::JsonContext& context)
auto const [avg, sd, size] = getStats(
itAdvance(prices.right.begin(), trimCount),
itAdvance(prices.right.end(), -trimCount));
result[jss::trimmed_set][jss::average] = avg.getText();
result[jss::trimmed_set][jss::mean] = avg.getText();
result[jss::trimmed_set][jss::size] = size;
result[jss::trimmed_set][jss::standard_deviation] = to_string(sd);
}
Expand Down
2 changes: 1 addition & 1 deletion src/test/app/Oracle_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ struct Oracle_test : public beast::unit_test::suite
.lastUpdateTime = 500,
.ter = ter(tecINVALID_UPDATE_TIME)});
oracle.set(UpdateArg{.series = {{"XRP", "USD", 740, 1}}});
BEAST_EXPECT(oracle.expectLastUpdateTime(150));
BEAST_EXPECT(oracle.expectLastUpdateTime(946684950));
// Less than the previous lastUpdateTime
oracle.set(UpdateArg{
.series = {{"XRP", "USD", 740, 1}},
Expand Down
5 changes: 3 additions & 2 deletions src/test/jtx/impl/Oracle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -197,13 +197,14 @@ Oracle::set(UpdateArg const& arg)
else
jv[jss::Fee] = std::to_string(env_.current()->fees().increment.drops());
if (arg.lastUpdateTime)
jv[jss::LastUpdateTime] = *arg.lastUpdateTime;
jv[jss::LastUpdateTime] = *arg.lastUpdateTime +
static_cast<std::uint32_t>(epoch_offset.count());
else
jv[jss::LastUpdateTime] = to_string(
duration_cast<seconds>(
env_.current()->info().closeTime.time_since_epoch())
.count() +
10);
10 + epoch_offset.count());
Json::Value dataSeries(Json::arrayValue);
auto assetToStr = [](std::string const& s) {
// assume standard currency
Expand Down
20 changes: 10 additions & 10 deletions src/test/rpc/GetAggregatePrice_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,13 +127,13 @@ class GetAggregatePrice_test : public beast::unit_test::suite
prep(env, oracles);
// entire and trimmed stats
auto ret = Oracle::aggregatePrice(env, "XRP", "USD", oracles);
BEAST_EXPECT(ret[jss::entire_set][jss::average] == "74.45");
BEAST_EXPECT(ret[jss::entire_set][jss::mean] == "74.45");
BEAST_EXPECT(ret[jss::entire_set][jss::size].asUInt() == 10);
BEAST_EXPECT(
ret[jss::entire_set][jss::standard_deviation] ==
"0.3027650354097492");
BEAST_EXPECT(ret[jss::median] == "74.45");
BEAST_EXPECT(ret[jss::time] == 100);
BEAST_EXPECT(ret[jss::time] == 946684900);
}

// Aggregate data set includes all price oracle instances
Expand All @@ -144,18 +144,18 @@ class GetAggregatePrice_test : public beast::unit_test::suite
// entire and trimmed stats
auto ret =
Oracle::aggregatePrice(env, "XRP", "USD", oracles, 20, 100);
BEAST_EXPECT(ret[jss::entire_set][jss::average] == "74.45");
BEAST_EXPECT(ret[jss::entire_set][jss::mean] == "74.45");
BEAST_EXPECT(ret[jss::entire_set][jss::size].asUInt() == 10);
BEAST_EXPECT(
ret[jss::entire_set][jss::standard_deviation] ==
"0.3027650354097492");
BEAST_EXPECT(ret[jss::median] == "74.45");
BEAST_EXPECT(ret[jss::trimmed_set][jss::average] == "74.45");
BEAST_EXPECT(ret[jss::trimmed_set][jss::mean] == "74.45");
BEAST_EXPECT(ret[jss::trimmed_set][jss::size].asUInt() == 6);
BEAST_EXPECT(
ret[jss::trimmed_set][jss::standard_deviation] ==
"0.187082869338697");
BEAST_EXPECT(ret[jss::time] == 100);
BEAST_EXPECT(ret[jss::time] == 946684900);
}

// A reduced dataset, as some price oracles have data beyond three
Expand Down Expand Up @@ -191,18 +191,18 @@ class GetAggregatePrice_test : public beast::unit_test::suite
// entire and trimmed stats
auto ret =
Oracle::aggregatePrice(env, "XRP", "USD", oracles, 20, 200);
BEAST_EXPECT(ret[jss::entire_set][jss::average] == "74.6");
BEAST_EXPECT(ret[jss::entire_set][jss::mean] == "74.6");
BEAST_EXPECT(ret[jss::entire_set][jss::size].asUInt() == 7);
BEAST_EXPECT(
ret[jss::entire_set][jss::standard_deviation] ==
"0.2160246899469287");
BEAST_EXPECT(ret[jss::median] == "74.6");
BEAST_EXPECT(ret[jss::trimmed_set][jss::average] == "74.6");
BEAST_EXPECT(ret[jss::trimmed_set][jss::mean] == "74.6");
BEAST_EXPECT(ret[jss::trimmed_set][jss::size].asUInt() == 5);
BEAST_EXPECT(
ret[jss::trimmed_set][jss::standard_deviation] ==
"0.158113883008419");
BEAST_EXPECT(ret[jss::time] == 100);
BEAST_EXPECT(ret[jss::time] == 946684900);
}

// Reduced data set because of the time threshold
Expand All @@ -224,11 +224,11 @@ class GetAggregatePrice_test : public beast::unit_test::suite
// entire stats only, limit lastUpdateTime to {200, 125}
auto ret = Oracle::aggregatePrice(
env, "XRP", "USD", oracles, std::nullopt, 75);
BEAST_EXPECT(ret[jss::entire_set][jss::average] == "74");
BEAST_EXPECT(ret[jss::entire_set][jss::mean] == "74");
BEAST_EXPECT(ret[jss::entire_set][jss::size].asUInt() == 8);
BEAST_EXPECT(ret[jss::entire_set][jss::standard_deviation] == "0");
BEAST_EXPECT(ret[jss::median] == "74");
BEAST_EXPECT(ret[jss::time] == 200);
BEAST_EXPECT(ret[jss::time] == 946685000);
}
}

Expand Down

0 comments on commit f52a520

Please sign in to comment.