Skip to content

Commit

Permalink
[FOLD] Update get_aggregate_price with historical data, and other ref…
Browse files Browse the repository at this point in the history
…actoring
  • Loading branch information
gregtatcam committed Oct 23, 2023
1 parent f1f233e commit 25a4219
Show file tree
Hide file tree
Showing 7 changed files with 464 additions and 152 deletions.
28 changes: 17 additions & 11 deletions src/ripple/app/tx/impl/SetOracle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,17 @@ SetOracle::preflight(PreflightContext const& ctx)
return temINVALID_FLAG;
}

if (ctx.tx.getFieldArray(sfPriceDataSeries).size() > maxOracleDataSeries)
auto const dataSeries = ctx.tx.getFieldArray(sfPriceDataSeries);
if (dataSeries.size() == 0 || dataSeries.size() > maxOracleDataSeries)
{
JLOG(ctx.j.debug()) << "Oracle Set: price data series too large";
JLOG(ctx.j.debug()) << "Oracle Set: invalid price data series size";
return temARRAY_SIZE;
}
for (auto const& entry : dataSeries)
{
if (!entry.isFieldPresent(sfSymbolPrice))
return temMALFORMED;
}

if (ctx.tx.getFieldVL(sfProvider).size() > maxOracleProvider)
{
Expand Down Expand Up @@ -84,6 +90,7 @@ SetOracle::preclaim(PreclaimContext const& ctx)
pairs.emplace(hash);
}

// update
if (auto const sle = ctx.view.read(keylet::oracle(
ctx.tx.getAccountID(sfAccount), ctx.tx[sfOracleSequence])))
{
Expand All @@ -103,14 +110,15 @@ SetOracle::preclaim(PreclaimContext const& ctx)
pairs.emplace(hash);
}
}
// create
else
{
if (!ctx.tx.isFieldPresent(sfProvider) ||
!ctx.tx.isFieldPresent(sfSymbolClass))
return temMALFORMED;
}

if (pairs.size() == 0 || pairs.size() > 10)
if (pairs.size() > maxOracleDataSeries)
return temARRAY_SIZE;

auto const sleSetter =
Expand Down Expand Up @@ -140,10 +148,9 @@ applySet(
{
auto const oracleID = keylet::oracle(account_, ctx_.tx[sfOracleSequence]);

// update
if (auto sle = sb.peek(oracleID))
{
// update Oracle

hash_map<uint256, STObject> pairs;
// collect current pairs
for (auto const& entry : sle->getFieldArray(sfPriceDataSeries))
Expand All @@ -153,8 +160,6 @@ applySet(
sfSymbol, entry.getFieldCurrency(sfSymbol));
priceData.setFieldCurrency(
sfPriceUnit, entry.getFieldCurrency(sfPriceUnit));
priceData.setFieldU64(sfSymbolPrice, 0);
priceData.setFieldU8(sfScale, 0);
pairs.emplace(
sha512Half(
entry.getFieldCurrency(sfSymbol).currency(),
Expand All @@ -171,7 +176,8 @@ applySet(
{
iter->second.setFieldU64(
sfSymbolPrice, entry.getFieldU64(sfSymbolPrice));
iter->second.setFieldU8(sfScale, entry.getFieldU8(sfScale));
if (entry.isFieldPresent(sfScale))
iter->second.setFieldU8(sfScale, entry.getFieldU8(sfScale));
}
else
{
Expand All @@ -182,7 +188,8 @@ applySet(
sfPriceUnit, entry.getFieldCurrency(sfPriceUnit));
priceData.setFieldU64(
sfSymbolPrice, entry.getFieldU64(sfSymbolPrice));
priceData.setFieldU8(sfScale, entry.getFieldU8(sfScale));
if (entry.isFieldPresent(sfScale))
priceData.setFieldU8(sfScale, entry.getFieldU8(sfScale));
pairs.emplace(
sha512Half(
entry.getFieldCurrency(sfSymbol).currency(),
Expand All @@ -200,10 +207,9 @@ applySet(

sb.update(sle);
}
// create
else
{
// create new Oracle

sle = std::make_shared<SLE>(oracleID);
sle->setAccountID(sfOwner, ctx_.tx.getAccountID(sfAccount));
sle->setFieldVL(sfProvider, ctx_.tx[sfProvider]);
Expand Down
4 changes: 2 additions & 2 deletions src/ripple/protocol/impl/InnerObjectFormats.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,8 @@ InnerObjectFormats::InnerObjectFormats()
{
{sfSymbol, soeREQUIRED},
{sfPriceUnit, soeREQUIRED},
{sfSymbolPrice, soeREQUIRED},
{sfScale, soeREQUIRED},
{sfSymbolPrice, soeOPTIONAL},
{sfScale, soeDEFAULT},
});
}

Expand Down
4 changes: 3 additions & 1 deletion src/ripple/protocol/jss.h
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ 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 @@ -307,6 +308,7 @@ JSS(enabled); // out: AmendmentTable
JSS(engine_result); // out: NetworkOPs, TransactionSign, Submit
JSS(engine_result_code); // out: NetworkOPs, TransactionSign, Submit
JSS(engine_result_message); // out: NetworkOPs, TransactionSign, Submit
JSS(entire_set); // out: get_aggregate_price
JSS(ephemeral_key); // out: ValidatorInfo
// in/out: Manifest
JSS(error); // out: error
Expand Down Expand Up @@ -616,7 +618,6 @@ JSS(signing_keys); // out: ValidatorList
JSS(signing_time); // out: NetworkOPs
JSS(signer_list); // in: AccountObjects
JSS(signer_lists); // in/out: AccountInfo
JSS(simple_average); // out: get_aggregate_price
JSS(size); // out: get_aggregate_price
JSS(snapshot); // in: Subscribe
JSS(source_account); // in: PathRequest, RipplePathFind
Expand Down Expand Up @@ -660,6 +661,7 @@ JSS(time_interval); // out: AMM Auction Slot
JSS(track); // out: PeerImp
JSS(traffic); // out: Overlay
JSS(trim); // in: get_aggregate_price
JSS(trimmed_set); // out: get_aggregate_price
JSS(total); // out: counters
JSS(total_bytes_recv); // out: Peers
JSS(total_bytes_sent); // out: Peers
Expand Down
Loading

0 comments on commit 25a4219

Please sign in to comment.