Skip to content

Commit

Permalink
[FOLD] Check if price included in set, other minor refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
gregtatcam committed Oct 24, 2023
1 parent c46ae2e commit f52896b
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 11 deletions.
3 changes: 2 additions & 1 deletion src/ripple/app/tx/impl/DeleteOracle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,9 @@ DeleteOracle::preclaim(PreclaimContext const& ctx)
}
else if (ctx.tx.getAccountID(sfAccount) != sle->getAccountID(sfOwner))
{
// this can't happen because of the above check
JLOG(ctx.j.debug()) << "Oracle Delete: invalid account.";
return tecNO_PERMISSION;
return tecINTERNAL;
}
return tesSUCCESS;
}
Expand Down
12 changes: 8 additions & 4 deletions src/ripple/app/tx/impl/SetOracle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,15 @@ SetOracle::preclaim(PreclaimContext const& ctx)
entry.getFieldCurrency(sfPriceUnit).currency());
if (pairs.contains(hash))
return tecDUPLICATE;
if (!entry.isFieldPresent(sfSymbolPrice))
return temMALFORMED;
pairs.emplace(hash);
}

// update
if (auto const sle = ctx.view.read(keylet::oracle(
ctx.tx.getAccountID(sfAccount), ctx.tx[sfOracleSequence])))
{
// update
if (ctx.tx[sfAccount] != sle->getAccountID(sfOwner))
return tecNO_PERMISSION;

Expand All @@ -110,9 +112,9 @@ SetOracle::preclaim(PreclaimContext const& ctx)
pairs.emplace(hash);
}
}
// create
else
{
// create
if (!ctx.tx.isFieldPresent(sfProvider) ||
!ctx.tx.isFieldPresent(sfSymbolClass))
return temMALFORMED;
Expand Down Expand Up @@ -148,9 +150,9 @@ applySet(
{
auto const oracleID = keylet::oracle(account_, ctx_.tx[sfOracleSequence]);

// update
if (auto sle = sb.peek(oracleID))
{
// update
hash_map<uint256, STObject> pairs;
// collect current pairs
for (auto const& entry : sle->getFieldArray(sfPriceDataSeries))
Expand All @@ -174,13 +176,15 @@ applySet(
entry.getFieldCurrency(sfPriceUnit).currency());
if (auto iter = pairs.find(hash); iter != pairs.end())
{
// update the price
iter->second.setFieldU64(
sfSymbolPrice, entry.getFieldU64(sfSymbolPrice));
if (entry.isFieldPresent(sfScale))
iter->second.setFieldU8(sfScale, entry.getFieldU8(sfScale));
}
else
{
// add a token pair with the price
STObject priceData{sfPriceData};
priceData.setFieldCurrency(
sfSymbol, entry.getFieldCurrency(sfSymbol));
Expand All @@ -207,9 +211,9 @@ applySet(

sb.update(sle);
}
// create
else
{
// create
sle = std::make_shared<SLE>(oracleID);
sle->setAccountID(sfOwner, ctx_.tx.getAccountID(sfAccount));
sle->setFieldVL(sfProvider, ctx_.tx[sfProvider]);
Expand Down
14 changes: 14 additions & 0 deletions src/test/app/Oracle_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,20 @@ struct Oracle_test : public beast::unit_test::suite
0,
ter(tecDUPLICATE));

// Price is not included
oracle.create(
owner,
{{"XRP", "USD", 740, 1}, {"XRP", "USD", std::nullopt, 1}},
std::nullopt,
"currency",
"provider",
"URI",
std::nullopt,
0,
std::nullopt,
0,
ter(temMALFORMED));

// Array of token pair is 0 or exceeds 10
oracle.create(
owner,
Expand Down
7 changes: 5 additions & 2 deletions src/test/jtx/Oracle.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,11 @@ namespace jtx {
class Oracle
{
// symbol, price unit, price, scale
using DataSeries = std::vector<
std::tuple<std::string, std::string, std::uint32_t, std::uint8_t>>;
using DataSeries = std::vector<std::tuple<
std::string,
std::string,
std::optional<std::uint32_t>,
std::optional<std::uint8_t>>>;

private:
static inline std::uint32_t id_ = 0;
Expand Down
8 changes: 4 additions & 4 deletions src/test/jtx/impl/Oracle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -331,10 +331,10 @@ Oracle::set(
Json::Value price;
price[jss::Symbol] = std::get<0>(data);
price[jss::PriceUnit] = std::get<1>(data);
// std::stringstream str;
// str << std::hex << std::get<3>(data);
price[jss::SymbolPrice] = std::get<2>(data);
price[jss::Scale] = std::get<3>(data);
if (std::get<2>(data))
price[jss::SymbolPrice] = *std::get<2>(data);
if (std::get<3>(data))
price[jss::Scale] = *std::get<3>(data);
priceData[jss::PriceData] = price;
dataSeries.append(priceData);
}
Expand Down

0 comments on commit f52896b

Please sign in to comment.