rippled
SetOracle.cpp
1 //------------------------------------------------------------------------------
2 /*
3  This file is part of rippled: https://github.com/ripple/rippled
4  Copyright (c) 2023 Ripple Labs Inc.
5 
6  Permission to use, copy, modify, and/or distribute this software for any
7  purpose with or without fee is hereby granted, provided that the above
8  copyright notice and this permission notice appear in all copies.
9 
10  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18 //==============================================================================
19 
20 #include <ripple/app/tx/impl/SetOracle.h>
21 #include <ripple/basics/UnorderedContainers.h>
22 #include <ripple/ledger/Sandbox.h>
23 #include <ripple/ledger/View.h>
24 #include <ripple/protocol/Feature.h>
25 #include <ripple/protocol/InnerObjectFormats.h>
26 #include <ripple/protocol/Rules.h>
27 #include <ripple/protocol/TxFlags.h>
28 #include <ripple/protocol/digest.h>
29 
30 namespace ripple {
31 
33 tokenPairKey(STObject const& pair)
34 {
35  return std::make_pair(
38 }
39 
40 NotTEC
42 {
44  return temDISABLED;
45 
46  if (auto const ret = preflight1(ctx); !isTesSuccess(ret))
47  return ret;
48 
49  if (ctx.tx.getFlags() & tfUniversalMask)
50  return temINVALID_FLAG;
51 
52  auto const& dataSeries = ctx.tx.getFieldArray(sfPriceDataSeries);
53  if (dataSeries.empty())
54  return temARRAY_EMPTY;
55  if (dataSeries.size() > maxOracleDataSeries)
56  return temARRAY_TOO_LARGE;
57 
58  auto isInvalidLength = [&](auto const& sField, std::size_t length) {
59  return ctx.tx.isFieldPresent(sField) &&
60  (ctx.tx[sField].length() == 0 || ctx.tx[sField].length() > length);
61  };
62 
63  if (isInvalidLength(sfProvider, maxOracleProvider) ||
64  isInvalidLength(sfURI, maxOracleURI) ||
65  isInvalidLength(sfAssetClass, maxOracleSymbolClass))
66  return temMALFORMED;
67 
68  return preflight2(ctx);
69 }
70 
71 TER
73 {
74  auto const sleSetter =
76  if (!sleSetter)
77  return terNO_ACCOUNT;
78 
79  // lastUpdateTime must be within maxLastUpdateTimeDelta seconds
80  // of the last closed ledger
81  using namespace std::chrono;
82  std::size_t const closeTime =
83  duration_cast<seconds>(ctx.view.info().closeTime.time_since_epoch())
84  .count();
85  std::size_t const lastUpdateTime = ctx.tx[sfLastUpdateTime];
86  if (lastUpdateTime < epoch_offset.count())
88  std::size_t const lastUpdateTimeEpoch =
89  lastUpdateTime - epoch_offset.count();
90  if (closeTime < maxLastUpdateTimeDelta)
91  Throw<std::runtime_error>(
92  "Oracle: close time is less than maxLastUpdateTimeDelta");
93  if (lastUpdateTimeEpoch < (closeTime - maxLastUpdateTimeDelta) ||
94  lastUpdateTimeEpoch > (closeTime + maxLastUpdateTimeDelta))
96 
97  auto const sle = ctx.view.read(keylet::oracle(
99 
100  // token pairs to add/update
102  // token pairs to delete. if a token pair doesn't include
103  // the price then this pair should be deleted from the object.
105  for (auto const& entry : ctx.tx.getFieldArray(sfPriceDataSeries))
106  {
107  if (entry[sfBaseAsset] == entry[sfQuoteAsset])
108  return temMALFORMED;
109  auto const key = tokenPairKey(entry);
110  if (pairs.contains(key) || pairsDel.contains(key))
111  return temMALFORMED;
112  if (entry[~sfScale] > maxPriceScale)
113  return temMALFORMED;
114  if (entry.isFieldPresent(sfAssetPrice))
115  pairs.emplace(key);
116  else if (sle)
117  pairsDel.emplace(key);
118  else
119  return temMALFORMED;
120  }
121 
122  // Lambda is used to check if the value of a field, passed
123  // in the transaction, is equal to the value of that field
124  // in the on-ledger object.
125  auto isConsistent = [&ctx, &sle](auto const& field) {
126  auto const v = ctx.tx[~field];
127  return !v || *v == (*sle)[field];
128  };
129 
130  std::uint32_t adjustReserve = 0;
131  if (sle)
132  {
133  // update
134  // Account is the Owner since we can get sle
135 
136  // lastUpdateTime must be more recent than the previous one
137  if (ctx.tx[sfLastUpdateTime] <= (*sle)[sfLastUpdateTime])
138  return tecINVALID_UPDATE_TIME;
139 
141  return temMALFORMED;
142 
143  for (auto const& entry : sle->getFieldArray(sfPriceDataSeries))
144  {
145  auto const key = tokenPairKey(entry);
146  if (!pairs.contains(key))
147  {
148  if (pairsDel.contains(key))
149  pairsDel.erase(key);
150  else
151  pairs.emplace(key);
152  }
153  }
154  if (!pairsDel.empty())
156 
157  auto const oldCount =
158  sle->getFieldArray(sfPriceDataSeries).size() > 5 ? 2 : 1;
159  auto const newCount = pairs.size() > 5 ? 2 : 1;
160  adjustReserve = newCount - oldCount;
161  }
162  else
163  {
164  // create
165 
166  if (!ctx.tx.isFieldPresent(sfProvider) ||
168  return temMALFORMED;
169  adjustReserve = pairs.size() > 5 ? 2 : 1;
170  }
171 
172  if (pairs.empty())
173  return tecARRAY_EMPTY;
174  if (pairs.size() > maxOracleDataSeries)
175  return tecARRAY_TOO_LARGE;
176 
177  auto const reserve = ctx.view.fees().accountReserve(
178  sleSetter->getFieldU32(sfOwnerCount) + adjustReserve);
179  auto const& balance = sleSetter->getFieldAmount(sfBalance);
180 
181  if (balance < reserve)
183 
184  return tesSUCCESS;
185 }
186 
187 static bool
189 {
190  if (auto const sleAccount =
191  ctx.view().peek(keylet::account(ctx.tx[sfAccount])))
192  {
193  adjustOwnerCount(ctx.view(), sleAccount, count, ctx.journal);
194  return true;
195  }
196 
197  return false;
198 }
199 
200 static void
202 {
203  if (SOTemplate const* elements =
204  InnerObjectFormats::getInstance().findSOTemplateBySField(
205  sfPriceData))
206  obj.set(*elements);
207 }
208 
209 TER
211 {
212  auto const oracleID = keylet::oracle(account_, ctx_.tx[sfOracleDocumentID]);
213 
214  if (auto sle = ctx_.view().peek(oracleID))
215  {
216  // update
217  // the token pair that doesn't have their price updated will not
218  // include neither price nor scale in the updated PriceDataSeries
219 
221  // collect current token pairs
222  for (auto const& entry : sle->getFieldArray(sfPriceDataSeries))
223  {
224  STObject priceData{sfPriceData};
225  setPriceDataInnerObjTemplate(priceData);
226  priceData.setFieldCurrency(
227  sfBaseAsset, entry.getFieldCurrency(sfBaseAsset));
228  priceData.setFieldCurrency(
229  sfQuoteAsset, entry.getFieldCurrency(sfQuoteAsset));
230  pairs.emplace(tokenPairKey(entry), std::move(priceData));
231  }
232  auto const oldCount = pairs.size() > 5 ? 2 : 1;
233  // update/add/delete pairs
234  for (auto const& entry : ctx_.tx.getFieldArray(sfPriceDataSeries))
235  {
236  auto const key = tokenPairKey(entry);
237  if (!entry.isFieldPresent(sfAssetPrice))
238  {
239  // delete token pair
240  pairs.erase(key);
241  }
242  else if (auto iter = pairs.find(key); iter != pairs.end())
243  {
244  // update the price
245  iter->second.setFieldU64(
246  sfAssetPrice, entry.getFieldU64(sfAssetPrice));
247  if (entry.isFieldPresent(sfScale))
248  iter->second.setFieldU8(sfScale, entry.getFieldU8(sfScale));
249  }
250  else
251  {
252  // add a token pair with the price
253  STObject priceData{sfPriceData};
254  setPriceDataInnerObjTemplate(priceData);
255  priceData.setFieldCurrency(
256  sfBaseAsset, entry.getFieldCurrency(sfBaseAsset));
257  priceData.setFieldCurrency(
258  sfQuoteAsset, entry.getFieldCurrency(sfQuoteAsset));
259  priceData.setFieldU64(
260  sfAssetPrice, entry.getFieldU64(sfAssetPrice));
261  if (entry.isFieldPresent(sfScale))
262  priceData.setFieldU8(sfScale, entry.getFieldU8(sfScale));
263  pairs.emplace(key, std::move(priceData));
264  }
265  }
266  STArray updatedSeries;
267  for (auto const& iter : pairs)
268  updatedSeries.push_back(std::move(iter.second));
269  sle->setFieldArray(sfPriceDataSeries, updatedSeries);
271  sle->setFieldVL(sfURI, ctx_.tx[sfURI]);
272  sle->setFieldU32(sfLastUpdateTime, ctx_.tx[sfLastUpdateTime]);
273 
274  auto const newCount = pairs.size() > 5 ? 2 : 1;
275  auto const adjust = newCount - oldCount;
276  if (adjust != 0 && !adjustOwnerCount(ctx_, adjust))
277  return tefINTERNAL;
278 
279  ctx_.view().update(sle);
280  }
281  else
282  {
283  // create
284 
285  sle = std::make_shared<SLE>(oracleID);
286  sle->setAccountID(sfOwner, ctx_.tx.getAccountID(sfAccount));
287  sle->setFieldVL(sfProvider, ctx_.tx[sfProvider]);
289  sle->setFieldVL(sfURI, ctx_.tx[sfURI]);
290  auto const& series = ctx_.tx.getFieldArray(sfPriceDataSeries);
291  sle->setFieldArray(sfPriceDataSeries, series);
292  sle->setFieldVL(sfAssetClass, ctx_.tx[sfAssetClass]);
293  sle->setFieldU32(sfLastUpdateTime, ctx_.tx[sfLastUpdateTime]);
294 
295  auto page = ctx_.view().dirInsert(
297  if (!page)
298  return tecDIR_FULL;
299 
300  (*sle)[sfOwnerNode] = *page;
301 
302  auto const count = series.size() > 5 ? 2 : 1;
303  if (!adjustOwnerCount(ctx_, count))
304  return tefINTERNAL;
305 
306  ctx_.view().insert(sle);
307  }
308 
309  return tesSUCCESS;
310 }
311 
312 } // namespace ripple
ripple::ReadView::info
virtual LedgerInfo const & info() const =0
Returns information about the ledger.
ripple::keylet::ownerDir
Keylet ownerDir(AccountID const &id) noexcept
The root page of an account's directory.
Definition: Indexes.cpp:313
ripple::sfOwnerCount
const SF_UINT32 sfOwnerCount
ripple::sfScale
const SF_UINT8 sfScale
ripple::STObject::getFieldArray
const STArray & getFieldArray(SField const &field) const
Definition: STObject.cpp:640
ripple::preflight2
NotTEC preflight2(PreflightContext const &ctx)
Checks whether the signature appears valid.
Definition: Transactor.cpp:133
ripple::sfLastUpdateTime
const SF_UINT32 sfLastUpdateTime
ripple::tefINTERNAL
@ tefINTERNAL
Definition: TER.h:170
ripple::Rules::enabled
bool enabled(uint256 const &feature) const
Returns true if a feature is enabled.
Definition: Rules.cpp:94
ripple::tecTOKEN_PAIR_NOT_FOUND
@ tecTOKEN_PAIR_NOT_FOUND
Definition: TER.h:338
ripple::PreclaimContext::view
ReadView const & view
Definition: Transactor.h:56
ripple::sfOwnerNode
const SF_UINT64 sfOwnerNode
ripple::SetOracle::preflight
static NotTEC preflight(PreflightContext const &ctx)
Definition: SetOracle.cpp:41
ripple::isConsistent
bool isConsistent(Book const &book)
Definition: Book.cpp:25
ripple::tecARRAY_EMPTY
@ tecARRAY_EMPTY
Definition: TER.h:339
ripple::ApplyView::peek
virtual std::shared_ptr< SLE > peek(Keylet const &k)=0
Prepare to modify the SLE associated with key.
ripple::temARRAY_EMPTY
@ temARRAY_EMPTY
Definition: TER.h:139
ripple::describeOwnerDir
std::function< void(SLE::ref)> describeOwnerDir(AccountID const &account)
Definition: View.cpp:748
ripple::sfBaseAsset
const SF_CURRENCY sfBaseAsset
ripple::isTesSuccess
bool isTesSuccess(TER x)
Definition: TER.h:643
std::unordered_set
STL class.
std::pair
ripple::sfOwner
const SF_ACCOUNT sfOwner
ripple::sfProvider
const SF_VL sfProvider
ripple::keylet::oracle
Keylet oracle(AccountID const &account, std::uint32_t const &documentID) noexcept
Definition: Indexes.cpp:449
std::unordered_map::find
T find(T... args)
std::unordered_set::size
T size(T... args)
ripple::ReadView::fees
virtual Fees const & fees() const =0
Returns the fees for the base ledger.
ripple::maxOracleURI
constexpr std::size_t maxOracleURI
The maximum length of a URI inside an Oracle.
Definition: Protocol.h:113
ripple::adjustOwnerCount
static bool adjustOwnerCount(ApplyContext &ctx, int count)
Definition: SetOracle.cpp:188
ripple::SetOracle::doApply
TER doApply() override
Definition: SetOracle.cpp:210
std::unordered_set::emplace
T emplace(T... args)
ripple::STArray::push_back
void push_back(STObject const &object)
Definition: STArray.h:212
ripple::ApplyView::update
virtual void update(std::shared_ptr< SLE > const &sle)=0
Indicate changes to a peeked SLE.
ripple::ApplyContext::journal
const beast::Journal journal
Definition: ApplyContext.h:51
ripple::STObject::getFieldCurrency
const STCurrency & getFieldCurrency(SField const &field) const
Definition: STObject.cpp:647
ripple::preflight1
NotTEC preflight1(PreflightContext const &ctx)
Performs early sanity checks on the account and fee fields.
Definition: Transactor.cpp:81
std::unordered_set::contains
T contains(T... args)
ripple::epoch_offset
constexpr static std::chrono::seconds epoch_offset
Clock for measuring the network time.
Definition: chrono.h:56
ripple::Keylet::key
uint256 key
Definition: Keylet.h:40
ripple::temINVALID_FLAG
@ temINVALID_FLAG
Definition: TER.h:110
std::chrono::time_point::time_since_epoch
T time_since_epoch(T... args)
ripple::SOTemplate
Defines the fields and their attributes within a STObject.
Definition: SOTemplate.h:84
ripple::keylet::account
Keylet account(AccountID const &id) noexcept
AccountID root.
Definition: Indexes.cpp:143
ripple::maxOracleSymbolClass
constexpr std::size_t maxOracleSymbolClass
The maximum length of a SymbolClass inside an Oracle.
Definition: Protocol.h:122
ripple::STObject::getAccountID
AccountID getAccountID(SField const &field) const
Definition: STObject.cpp:605
ripple::TERSubset< CanCvtToTER >
ripple::STArray
Definition: STArray.h:28
ripple::TER
TERSubset< CanCvtToTER > TER
Definition: TER.h:614
ripple::tecINVALID_UPDATE_TIME
@ tecINVALID_UPDATE_TIME
Definition: TER.h:337
ripple::sfOracleDocumentID
const SF_UINT32 sfOracleDocumentID
std::unordered_set::erase
T erase(T... args)
ripple::STObject::getFlags
std::uint32_t getFlags() const
Definition: STObject.cpp:497
ripple::ApplyContext
State information when applying a tx.
Definition: ApplyContext.h:35
std::uint32_t
ripple::tokenPairKey
static std::pair< Currency, Currency > tokenPairKey(STObject const &pair)
Definition: SetOracle.cpp:33
ripple::sfAssetClass
const SF_VL sfAssetClass
ripple::ReadView::read
virtual std::shared_ptr< SLE const > read(Keylet const &k) const =0
Return the state item associated with a key.
ripple::ApplyContext::view
ApplyView & view()
Definition: ApplyContext.h:54
ripple::PreclaimContext::tx
STTx const & tx
Definition: Transactor.h:58
ripple::LedgerHeader::closeTime
NetClock::time_point closeTime
Definition: LedgerHeader.h:72
ripple::temARRAY_TOO_LARGE
@ temARRAY_TOO_LARGE
Definition: TER.h:140
ripple::tecDIR_FULL
@ tecDIR_FULL
Definition: TER.h:270
ripple::terNO_ACCOUNT
@ terNO_ACCOUNT
Definition: TER.h:213
ripple::PreclaimContext
State information when determining if a tx is likely to claim a fee.
Definition: Transactor.h:52
ripple::STObject
Definition: STObject.h:55
ripple::sfURI
const SF_VL sfURI
ripple::ApplyView::insert
virtual void insert(std::shared_ptr< SLE > const &sle)=0
Insert a new state SLE.
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::maxLastUpdateTimeDelta
constexpr std::size_t maxLastUpdateTimeDelta
The maximum allowed time difference between lastUpdateTime and the time of the last closed ledger.
Definition: Protocol.h:127
ripple::SetOracle::preclaim
static TER preclaim(PreclaimContext const &ctx)
Definition: SetOracle.cpp:72
ripple::temDISABLED
@ temDISABLED
Definition: TER.h:113
ripple::Fees::accountReserve
XRPAmount accountReserve(std::size_t ownerCount) const
Returns the account reserve given the owner count, in drops.
Definition: protocol/Fees.h:49
ripple::STObject::isFieldPresent
bool isFieldPresent(SField const &field) const
Definition: STObject.cpp:444
ripple::featurePriceOracle
const uint256 featurePriceOracle
ripple::sfBalance
const SF_AMOUNT sfBalance
std::chrono::seconds::count
T count(T... args)
ripple::setPriceDataInnerObjTemplate
static void setPriceDataInnerObjTemplate(STObject &obj)
Definition: SetOracle.cpp:201
ripple::tecINSUFFICIENT_RESERVE
@ tecINSUFFICIENT_RESERVE
Definition: TER.h:290
std::unordered_set::empty
T empty(T... args)
ripple::maxPriceScale
constexpr std::size_t maxPriceScale
The maximum price scaling factor.
Definition: Protocol.h:131
ripple::Transactor::ctx_
ApplyContext & ctx_
Definition: Transactor.h:88
std::size_t
std::make_pair
T make_pair(T... args)
ripple::sfAccount
const SF_ACCOUNT sfAccount
ripple::InnerObjectFormats::getInstance
static InnerObjectFormats const & getInstance()
Definition: InnerObjectFormats.cpp:153
std::unordered_map::end
T end(T... args)
ripple::temMALFORMED
@ temMALFORMED
Definition: TER.h:86
ripple::PreflightContext::tx
STTx const & tx
Definition: Transactor.h:35
ripple::PreflightContext
State information when preflighting a tx.
Definition: Transactor.h:31
ripple::STCurrency::currency
Currency const & currency() const
Definition: STCurrency.h:89
ripple::ApplyView::dirInsert
std::optional< std::uint64_t > dirInsert(Keylet const &directory, uint256 const &key, std::function< void(std::shared_ptr< SLE > const &)> const &describe)
Insert an entry to a directory.
Definition: ApplyView.h:306
ripple::PreflightContext::rules
const Rules rules
Definition: Transactor.h:36
ripple::sfQuoteAsset
const SF_CURRENCY sfQuoteAsset
ripple::tfUniversalMask
constexpr std::uint32_t tfUniversalMask
Definition: TxFlags.h:60
ripple::maxOracleProvider
constexpr std::size_t maxOracleProvider
The maximum length of a Provider inside an Oracle.
Definition: Protocol.h:116
ripple::tecARRAY_TOO_LARGE
@ tecARRAY_TOO_LARGE
Definition: TER.h:340
std::unordered_map
STL class.
ripple::STObject::set
void set(const SOTemplate &)
Definition: STObject.cpp:116
ripple::sfPriceDataSeries
const SField sfPriceDataSeries
ripple::tesSUCCESS
@ tesSUCCESS
Definition: TER.h:238
ripple::Transactor::account_
const AccountID account_
Definition: Transactor.h:91
ripple::ApplyContext::tx
STTx const & tx
Definition: ApplyContext.h:48
ripple::sfPriceData
const SField sfPriceData
ripple::sfAssetPrice
const SF_UINT64 sfAssetPrice
ripple::NotTEC
TERSubset< CanCvtToNotTEC > NotTEC
Definition: TER.h:574
ripple::maxOracleDataSeries
constexpr std::size_t maxOracleDataSeries
The maximum size of a data series array inside an Oracle.
Definition: Protocol.h:119
std::chrono