Skip to content

Commit

Permalink
[FOLD] Validate LastUpdateTime within range {close-300sec, close+300sec}
Browse files Browse the repository at this point in the history
  • Loading branch information
gregtatcam committed Jan 13, 2024
1 parent ebb6b28 commit fa876df
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 15 deletions.
5 changes: 3 additions & 2 deletions src/ripple/app/tx/impl/SetOracle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,12 @@ SetOracle::preclaim(PreclaimContext const& ctx)
duration_cast<seconds>(ctx.view.info().closeTime.time_since_epoch())
.count();
std::size_t const lastUpdateTime = ctx.tx[sfLastUpdateTime];
if (lastUpdateTime <= epoch_offset.count())
if (lastUpdateTime < epoch_offset.count())
return tecINVALID_UPDATE_TIME;
std::size_t const lastUpdateTimeEpoch =
lastUpdateTime - epoch_offset.count();
if (lastUpdateTimeEpoch < closeTime ||
if (lastUpdateTimeEpoch <
closeTime - std::min(closeTime, maxLastUpdateTimeDelta) ||
lastUpdateTimeEpoch > (closeTime + maxLastUpdateTimeDelta))
return tecINVALID_UPDATE_TIME;

Expand Down
2 changes: 1 addition & 1 deletion src/ripple/protocol/Protocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ std::size_t constexpr maxOracleSymbolClass = 16;
/** The maximum allowed time difference between lastUpdateTime and the time
of the last closed ledger
*/
std::size_t constexpr maxLastUpdateTimeDelta = 30;
std::size_t constexpr maxLastUpdateTimeDelta = 300;

} // namespace ripple

Expand Down
14 changes: 7 additions & 7 deletions src/test/app/Oracle_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -224,23 +224,23 @@ struct Oracle_test : public beast::unit_test::suite
env.fund(XRP(1'000), owner);
Oracle oracle(env, {.owner = owner});
BEAST_EXPECT(oracle.exists());
env.close(std::chrono::seconds(100));
// Less than the last close time
env.close(std::chrono::seconds(400));
// Less than the last close time - 300s
oracle.set(UpdateArg{
.series = {{"XRP", "USD", 740, 1}},
.lastUpdateTime = 60,
.err = ter(tecINVALID_UPDATE_TIME)});
// Greater than last close time + 30 sec
// Greater than last close time + 300s
oracle.set(UpdateArg{
.series = {{"XRP", "USD", 740, 1}},
.lastUpdateTime = 500,
.lastUpdateTime = 800,
.err = ter(tecINVALID_UPDATE_TIME)});
oracle.set(UpdateArg{.series = {{"XRP", "USD", 740, 1}}});
BEAST_EXPECT(oracle.expectLastUpdateTime(946684950));
BEAST_EXPECT(oracle.expectLastUpdateTime(946685240));
// Less than the previous lastUpdateTime
oracle.set(UpdateArg{
.series = {{"XRP", "USD", 740, 1}},
.lastUpdateTime = 149,
.lastUpdateTime = 439,
.err = ter(tecINVALID_UPDATE_TIME)});
}

Expand Down Expand Up @@ -273,7 +273,7 @@ struct Oracle_test : public beast::unit_test::suite
Oracle oracle(env, {.owner = owner, .series = series});
BEAST_EXPECT(oracle.exists());
BEAST_EXPECT(ownerCount(env, owner) == (count + adj));
BEAST_EXPECT(oracle.expectLastUpdateTime(946684810));
BEAST_EXPECT(oracle.expectLastUpdateTime(946684800));
};

{
Expand Down
2 changes: 1 addition & 1 deletion src/test/jtx/impl/Oracle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ Oracle::set(UpdateArg const& arg)
duration_cast<seconds>(
env_.current()->info().closeTime.time_since_epoch())
.count() +
10 + epoch_offset.count());
epoch_offset.count());
Json::Value dataSeries(Json::arrayValue);
auto assetToStr = [](std::string const& s) {
// assume standard currency
Expand Down
8 changes: 4 additions & 4 deletions src/test/rpc/GetAggregatePrice_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ class GetAggregatePrice_test : public beast::unit_test::suite
ret[jss::entire_set][jss::standard_deviation] ==
"0.3027650354097492");
BEAST_EXPECT(ret[jss::median] == "74.45");
BEAST_EXPECT(ret[jss::time] == 946684900);
BEAST_EXPECT(ret[jss::time] == 946684890);
}

// Aggregate data set includes all price oracle instances
Expand All @@ -154,7 +154,7 @@ class GetAggregatePrice_test : public beast::unit_test::suite
BEAST_EXPECT(
ret[jss::trimmed_set][jss::standard_deviation] ==
"0.187082869338697");
BEAST_EXPECT(ret[jss::time] == 946684900);
BEAST_EXPECT(ret[jss::time] == 946684890);
}

// A reduced dataset, as some price oracles have data beyond three
Expand Down Expand Up @@ -203,7 +203,7 @@ class GetAggregatePrice_test : public beast::unit_test::suite
BEAST_EXPECT(
ret[jss::trimmed_set][jss::standard_deviation] ==
"0.158113883008419");
BEAST_EXPECT(ret[jss::time] == 946684900);
BEAST_EXPECT(ret[jss::time] == 946684890);
}

// Reduced data set because of the time threshold
Expand All @@ -230,7 +230,7 @@ class GetAggregatePrice_test : public beast::unit_test::suite
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] == 946685000);
BEAST_EXPECT(ret[jss::time] == 946684990);
}
}

Expand Down

0 comments on commit fa876df

Please sign in to comment.