mirror of
https://github.com/XRPLF/rippled.git
synced 2025-11-23 12:35:50 +00:00
Ugh. This is all wrong.
This commit is contained in:
@@ -418,6 +418,12 @@ public:
|
|||||||
operator=(NumberRoundModeGuard const&) = delete;
|
operator=(NumberRoundModeGuard const&) = delete;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class NumberOverflow : public std::overflow_error
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
using overflow_error::overflow_error;
|
||||||
|
};
|
||||||
|
|
||||||
} // namespace ripple
|
} // namespace ripple
|
||||||
|
|
||||||
#endif // XRPL_BASICS_NUMBER_H_INCLUDED
|
#endif // XRPL_BASICS_NUMBER_H_INCLUDED
|
||||||
|
|||||||
@@ -87,7 +87,14 @@ public:
|
|||||||
bool
|
bool
|
||||||
integral() const
|
integral() const
|
||||||
{
|
{
|
||||||
return !holds<Issue>() || get<Issue>().native();
|
return std::visit(
|
||||||
|
[]<ValidIssueType TIss>(TIss const& issue) {
|
||||||
|
if constexpr (std::is_same_v<TIss, Issue>)
|
||||||
|
return issue.native();
|
||||||
|
if constexpr (std::is_same_v<TIss, MPTIssue>)
|
||||||
|
return true;
|
||||||
|
},
|
||||||
|
issue_);
|
||||||
}
|
}
|
||||||
|
|
||||||
friend constexpr bool
|
friend constexpr bool
|
||||||
|
|||||||
@@ -24,6 +24,10 @@ class STNumber : public STBase, public CountedObject<STNumber>
|
|||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
Number value_;
|
Number value_;
|
||||||
|
// isInteger_ is not serialized or transmitted in any way. It is used only
|
||||||
|
// for internal validation of integer types. It is a one-way switch. Once
|
||||||
|
// it's on, it stays on.
|
||||||
|
bool isInteger_ = false;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
using value_type = Number;
|
using value_type = Number;
|
||||||
@@ -51,6 +55,35 @@ public:
|
|||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Tell the STNumber whether the value it is holding represents an integer,
|
||||||
|
// and must fit within the allowable range.
|
||||||
|
void
|
||||||
|
usesAsset(Asset const& a);
|
||||||
|
// The asset isn't stored, only whether it's an integral type. Get that flag
|
||||||
|
// back out.
|
||||||
|
bool
|
||||||
|
isIntegral() const;
|
||||||
|
// Returns whether the value fits within Number::maxIntValue. Transactors
|
||||||
|
// should check this whenever interacting with an STNumber.
|
||||||
|
bool
|
||||||
|
safeNumber() const;
|
||||||
|
/// Combines usesAsset(a) and safeNumber()
|
||||||
|
static std::int64_t
|
||||||
|
safeNumberLimit();
|
||||||
|
bool
|
||||||
|
safeNumber(Asset const& a);
|
||||||
|
// Returns whether the value fits within Number::maxMantissa. Transactors
|
||||||
|
// may check this, too, but are not required to. It will be checked when
|
||||||
|
// serializing, and will throw if false, thus preventing the value from
|
||||||
|
// being silently truncated.
|
||||||
|
bool
|
||||||
|
validNumber() const;
|
||||||
|
/// Combines usesAsset(a) and validAsset()
|
||||||
|
bool
|
||||||
|
validNumber(Asset const& a);
|
||||||
|
static std::int64_t
|
||||||
|
validNumberLimit();
|
||||||
|
|
||||||
bool
|
bool
|
||||||
isEquivalent(STBase const& t) const override;
|
isEquivalent(STBase const& t) const override;
|
||||||
bool
|
bool
|
||||||
|
|||||||
@@ -487,6 +487,10 @@ public:
|
|||||||
T const*
|
T const*
|
||||||
operator->() const;
|
operator->() const;
|
||||||
|
|
||||||
|
/// Access the underlying STObject without necessarily dereferencing it
|
||||||
|
T*
|
||||||
|
stValue() const;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
STObject* st_;
|
STObject* st_;
|
||||||
SOEStyle style_;
|
SOEStyle style_;
|
||||||
@@ -726,7 +730,15 @@ template <class T>
|
|||||||
T const*
|
T const*
|
||||||
STObject::Proxy<T>::operator->() const
|
STObject::Proxy<T>::operator->() const
|
||||||
{
|
{
|
||||||
return this->find();
|
return stValue();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Access the underlying STObject without necessarily dereferencing it
|
||||||
|
template <class T>
|
||||||
|
T*
|
||||||
|
STObject::Proxy<T>::stValue() const
|
||||||
|
{
|
||||||
|
return dynamic_cast<T*>(st_->getPField(*f_));
|
||||||
}
|
}
|
||||||
|
|
||||||
template <class T>
|
template <class T>
|
||||||
|
|||||||
@@ -12,6 +12,7 @@
|
|||||||
#include <xrpl/protocol/SField.h>
|
#include <xrpl/protocol/SField.h>
|
||||||
#include <xrpl/protocol/STBase.h>
|
#include <xrpl/protocol/STBase.h>
|
||||||
#include <xrpl/protocol/STLedgerEntry.h>
|
#include <xrpl/protocol/STLedgerEntry.h>
|
||||||
|
#include <xrpl/protocol/STNumber.h>
|
||||||
#include <xrpl/protocol/STObject.h>
|
#include <xrpl/protocol/STObject.h>
|
||||||
#include <xrpl/protocol/Serializer.h>
|
#include <xrpl/protocol/Serializer.h>
|
||||||
#include <xrpl/protocol/jss.h>
|
#include <xrpl/protocol/jss.h>
|
||||||
@@ -67,6 +68,32 @@ STLedgerEntry::setSLEType()
|
|||||||
|
|
||||||
type_ = format->getType();
|
type_ = format->getType();
|
||||||
applyTemplate(format->getSOTemplate()); // May throw
|
applyTemplate(format->getSOTemplate()); // May throw
|
||||||
|
|
||||||
|
// Per object type overrides
|
||||||
|
// Currently only covers STNumber fields to link them to appropriate assets
|
||||||
|
switch (type_)
|
||||||
|
{
|
||||||
|
case ltVAULT: {
|
||||||
|
auto const asset = at(sfAsset);
|
||||||
|
for (auto const& field :
|
||||||
|
{~sfAssetsAvailable,
|
||||||
|
~sfAssetsTotal,
|
||||||
|
~sfAssetsMaximum,
|
||||||
|
~sfLossUnrealized})
|
||||||
|
{
|
||||||
|
if (auto proxy = at(field))
|
||||||
|
if (auto stNumber = proxy.stValue())
|
||||||
|
stNumber->usesAsset(asset);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/*
|
||||||
|
// TODO: If possible, set up the loan-related STNumber fields, too.
|
||||||
|
// May not be possible because we don't have a view available.
|
||||||
|
|
||||||
|
case ltLOAN_BROKER:
|
||||||
|
case ltLOAN:
|
||||||
|
*/
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string
|
std::string
|
||||||
|
|||||||
@@ -50,6 +50,8 @@ STNumber::add(Serializer& s) const
|
|||||||
XRPL_ASSERT(
|
XRPL_ASSERT(
|
||||||
getFName().fieldType == getSType(),
|
getFName().fieldType == getSType(),
|
||||||
"ripple::STNumber::add : field type match");
|
"ripple::STNumber::add : field type match");
|
||||||
|
if (!validNumber())
|
||||||
|
throw NumberOverflow(to_string(value_));
|
||||||
s.add64(value_.mantissa());
|
s.add64(value_.mantissa());
|
||||||
s.add32(value_.exponent());
|
s.add32(value_.exponent());
|
||||||
}
|
}
|
||||||
@@ -66,6 +68,87 @@ STNumber::setValue(Number const& v)
|
|||||||
value_ = v;
|
value_ = v;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Tell the STNumber whether the value it is holding represents an integer, and
|
||||||
|
// must fit within the allowable range.
|
||||||
|
void
|
||||||
|
STNumber::usesAsset(Asset const& a)
|
||||||
|
{
|
||||||
|
XRPL_ASSERT_PARTS(
|
||||||
|
!isInteger_ || a.integral(),
|
||||||
|
"ripple::STNumber::value",
|
||||||
|
"asset check only gets stricter");
|
||||||
|
// isInteger_ is a one-way switch. Once it's on, it stays on.
|
||||||
|
if (isInteger_)
|
||||||
|
return;
|
||||||
|
isInteger_ = a.integral();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool
|
||||||
|
STNumber::isIntegral() const
|
||||||
|
{
|
||||||
|
return isInteger_;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Returns whether the value fits within Number::maxIntValue. Transactors
|
||||||
|
// should check this whenever interacting with an STNumber.
|
||||||
|
bool
|
||||||
|
STNumber::safeNumber() const
|
||||||
|
{
|
||||||
|
if (!isInteger_)
|
||||||
|
return true;
|
||||||
|
|
||||||
|
static Number const max = safeNumberLimit();
|
||||||
|
static Number const maxNeg = -max;
|
||||||
|
// Avoid making a copy
|
||||||
|
if (value_ < 0)
|
||||||
|
return value_ >= maxNeg;
|
||||||
|
return value_ <= max;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool
|
||||||
|
STNumber::safeNumber(Asset const& a)
|
||||||
|
{
|
||||||
|
usesAsset(a);
|
||||||
|
return safeNumber();
|
||||||
|
}
|
||||||
|
|
||||||
|
std::int64_t
|
||||||
|
STNumber::safeNumberLimit()
|
||||||
|
{
|
||||||
|
return Number::maxIntValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Returns whether the value fits within Number::maxMantissa. Transactors
|
||||||
|
// may check this, too, but are not required to. It will be checked when
|
||||||
|
// serializing, and will throw if false, thus preventing the value from
|
||||||
|
// being silently truncated.
|
||||||
|
bool
|
||||||
|
STNumber::validNumber() const
|
||||||
|
{
|
||||||
|
if (!isInteger_)
|
||||||
|
return true;
|
||||||
|
|
||||||
|
static Number const max = validNumberLimit();
|
||||||
|
static Number const maxNeg = -max;
|
||||||
|
// Avoid making a copy
|
||||||
|
if (value_ < 0)
|
||||||
|
return value_ >= maxNeg;
|
||||||
|
return value_ <= max;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool
|
||||||
|
STNumber::validNumber(Asset const& a)
|
||||||
|
{
|
||||||
|
usesAsset(a);
|
||||||
|
return validNumber();
|
||||||
|
}
|
||||||
|
|
||||||
|
std::int64_t
|
||||||
|
STNumber::validNumberLimit()
|
||||||
|
{
|
||||||
|
return Number::maxMantissa;
|
||||||
|
}
|
||||||
|
|
||||||
STBase*
|
STBase*
|
||||||
STNumber::copy(std::size_t n, void* buf) const
|
STNumber::copy(std::size_t n, void* buf) const
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -3684,32 +3684,7 @@ class Vault_test : public beast::unit_test::suite
|
|||||||
});
|
});
|
||||||
|
|
||||||
testCase(18, [&, this](Env& env, Data d) {
|
testCase(18, [&, this](Env& env, Data d) {
|
||||||
testcase("MPT scale deposit overflow");
|
testcase("Scale deposit overflow on second deposit");
|
||||||
// The computed number of shares can not be represented as an MPT
|
|
||||||
// without truncation
|
|
||||||
|
|
||||||
{
|
|
||||||
auto tx = d.vault.deposit(
|
|
||||||
{.depositor = d.depositor,
|
|
||||||
.id = d.keylet.key,
|
|
||||||
.amount = d.asset(5)});
|
|
||||||
env(tx, ter{tecPRECISION_LOSS});
|
|
||||||
env.close();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
testCase(13, [&, this](Env& env, Data d) {
|
|
||||||
testcase("MPT scale deposit overflow on first deposit");
|
|
||||||
auto tx = d.vault.deposit(
|
|
||||||
{.depositor = d.depositor,
|
|
||||||
.id = d.keylet.key,
|
|
||||||
.amount = d.asset(10)});
|
|
||||||
env(tx, ter{tecPRECISION_LOSS});
|
|
||||||
env.close();
|
|
||||||
});
|
|
||||||
|
|
||||||
testCase(13, [&, this](Env& env, Data d) {
|
|
||||||
testcase("MPT scale deposit overflow on second deposit");
|
|
||||||
|
|
||||||
{
|
{
|
||||||
auto tx = d.vault.deposit(
|
auto tx = d.vault.deposit(
|
||||||
@@ -3730,8 +3705,8 @@ class Vault_test : public beast::unit_test::suite
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
testCase(13, [&, this](Env& env, Data d) {
|
testCase(18, [&, this](Env& env, Data d) {
|
||||||
testcase("No MPT scale deposit overflow on total shares");
|
testcase("Scale deposit overflow on total shares");
|
||||||
|
|
||||||
{
|
{
|
||||||
auto tx = d.vault.deposit(
|
auto tx = d.vault.deposit(
|
||||||
@@ -3747,7 +3722,7 @@ class Vault_test : public beast::unit_test::suite
|
|||||||
{.depositor = d.depositor,
|
{.depositor = d.depositor,
|
||||||
.id = d.keylet.key,
|
.id = d.keylet.key,
|
||||||
.amount = d.asset(5)});
|
.amount = d.asset(5)});
|
||||||
env(tx);
|
env(tx, ter{tecPATH_DRY});
|
||||||
env.close();
|
env.close();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@@ -4031,28 +4006,6 @@ class Vault_test : public beast::unit_test::suite
|
|||||||
testCase(18, [&, this](Env& env, Data d) {
|
testCase(18, [&, this](Env& env, Data d) {
|
||||||
testcase("Scale withdraw overflow");
|
testcase("Scale withdraw overflow");
|
||||||
|
|
||||||
{
|
|
||||||
auto tx = d.vault.deposit(
|
|
||||||
{.depositor = d.depositor,
|
|
||||||
.id = d.keylet.key,
|
|
||||||
.amount = d.asset(5)});
|
|
||||||
env(tx, ter{tecPRECISION_LOSS});
|
|
||||||
env.close();
|
|
||||||
}
|
|
||||||
|
|
||||||
{
|
|
||||||
auto tx = d.vault.withdraw(
|
|
||||||
{.depositor = d.depositor,
|
|
||||||
.id = d.keylet.key,
|
|
||||||
.amount = STAmount(d.asset, Number(10, 0))});
|
|
||||||
env(tx, ter{tecPRECISION_LOSS});
|
|
||||||
env.close();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
testCase(13, [&, this](Env& env, Data d) {
|
|
||||||
testcase("MPT scale withdraw overflow");
|
|
||||||
|
|
||||||
{
|
{
|
||||||
auto tx = d.vault.deposit(
|
auto tx = d.vault.deposit(
|
||||||
{.depositor = d.depositor,
|
{.depositor = d.depositor,
|
||||||
@@ -4271,29 +4224,6 @@ class Vault_test : public beast::unit_test::suite
|
|||||||
testCase(18, [&, this](Env& env, Data d) {
|
testCase(18, [&, this](Env& env, Data d) {
|
||||||
testcase("Scale clawback overflow");
|
testcase("Scale clawback overflow");
|
||||||
|
|
||||||
{
|
|
||||||
auto tx = d.vault.deposit(
|
|
||||||
{.depositor = d.depositor,
|
|
||||||
.id = d.keylet.key,
|
|
||||||
.amount = d.asset(5)});
|
|
||||||
env(tx, ter(tecPRECISION_LOSS));
|
|
||||||
env.close();
|
|
||||||
}
|
|
||||||
|
|
||||||
{
|
|
||||||
auto tx = d.vault.clawback(
|
|
||||||
{.issuer = d.issuer,
|
|
||||||
.id = d.keylet.key,
|
|
||||||
.holder = d.depositor,
|
|
||||||
.amount = STAmount(d.asset, Number(10, 0))});
|
|
||||||
env(tx, ter{tecPRECISION_LOSS});
|
|
||||||
env.close();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
testCase(13, [&, this](Env& env, Data d) {
|
|
||||||
testcase("MPT Scale clawback overflow");
|
|
||||||
|
|
||||||
{
|
{
|
||||||
auto tx = d.vault.deposit(
|
auto tx = d.vault.deposit(
|
||||||
{.depositor = d.depositor,
|
{.depositor = d.depositor,
|
||||||
|
|||||||
@@ -2164,6 +2164,28 @@ ValidAMM::finalize(
|
|||||||
|
|
||||||
//------------------------------------------------------------------------------
|
//------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
ValidVault::NumberInfo
|
||||||
|
ValidVault::NumberInfo::make(
|
||||||
|
SLE const& from,
|
||||||
|
SF_NUMBER const& field,
|
||||||
|
Asset const& asset)
|
||||||
|
{
|
||||||
|
bool valid = true;
|
||||||
|
|
||||||
|
// Poke around in the internals of STObject to get the STNumber object
|
||||||
|
if (auto const stNumber =
|
||||||
|
dynamic_cast<STNumber const*>(from.peekAtPField(field)))
|
||||||
|
valid = stNumber->isIntegral() == asset.integral() &&
|
||||||
|
stNumber->validNumber();
|
||||||
|
|
||||||
|
return {.n = from.at(field), .valid = valid};
|
||||||
|
}
|
||||||
|
|
||||||
|
ValidVault::NumberInfo::operator Number const&() const
|
||||||
|
{
|
||||||
|
return n;
|
||||||
|
}
|
||||||
|
|
||||||
ValidVault::Vault
|
ValidVault::Vault
|
||||||
ValidVault::Vault::make(SLE const& from)
|
ValidVault::Vault::make(SLE const& from)
|
||||||
{
|
{
|
||||||
@@ -2176,10 +2198,11 @@ ValidVault::Vault::make(SLE const& from)
|
|||||||
self.asset = from.at(sfAsset);
|
self.asset = from.at(sfAsset);
|
||||||
self.pseudoId = from.getAccountID(sfAccount);
|
self.pseudoId = from.getAccountID(sfAccount);
|
||||||
self.shareMPTID = from.getFieldH192(sfShareMPTID);
|
self.shareMPTID = from.getFieldH192(sfShareMPTID);
|
||||||
self.assetsTotal = from.at(sfAssetsTotal);
|
self.assetsTotal = NumberInfo::make(from, sfAssetsTotal, self.asset);
|
||||||
self.assetsAvailable = from.at(sfAssetsAvailable);
|
self.assetsAvailable =
|
||||||
self.assetsMaximum = from.at(sfAssetsMaximum);
|
NumberInfo::make(from, sfAssetsAvailable, self.asset);
|
||||||
self.lossUnrealized = from.at(sfLossUnrealized);
|
self.assetsMaximum = NumberInfo::make(from, sfAssetsMaximum, self.asset);
|
||||||
|
self.lossUnrealized = NumberInfo::make(from, sfLossUnrealized, self.asset);
|
||||||
return self;
|
return self;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -2413,10 +2436,8 @@ ValidVault::finalize(
|
|||||||
beforeVault_.empty() || beforeVault_[0].key == afterVault.key,
|
beforeVault_.empty() || beforeVault_[0].key == afterVault.key,
|
||||||
"ripple::ValidVault::finalize : single vault operation");
|
"ripple::ValidVault::finalize : single vault operation");
|
||||||
|
|
||||||
if (!afterVault.assetsTotal.representable() ||
|
if (!afterVault.assetsTotal.valid || !afterVault.assetsAvailable.valid ||
|
||||||
!afterVault.assetsAvailable.representable() ||
|
!afterVault.assetsMaximum.valid || !afterVault.lossUnrealized.valid)
|
||||||
!afterVault.assetsMaximum.representable() ||
|
|
||||||
!afterVault.lossUnrealized.representable())
|
|
||||||
{
|
{
|
||||||
JLOG(j.fatal()) << "Invariant failed: vault overflowed maximum current "
|
JLOG(j.fatal()) << "Invariant failed: vault overflowed maximum current "
|
||||||
"representable integer value";
|
"representable integer value";
|
||||||
@@ -2500,7 +2521,7 @@ ValidVault::finalize(
|
|||||||
result = false;
|
result = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (afterVault.assetsAvailable > afterVault.assetsTotal)
|
if (afterVault.assetsAvailable.n > afterVault.assetsTotal)
|
||||||
{
|
{
|
||||||
JLOG(j.fatal()) << "Invariant failed: assets available must "
|
JLOG(j.fatal()) << "Invariant failed: assets available must "
|
||||||
"not be greater than assets outstanding";
|
"not be greater than assets outstanding";
|
||||||
@@ -2541,7 +2562,7 @@ ValidVault::finalize(
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!beforeVault_.empty() &&
|
if (!beforeVault_.empty() &&
|
||||||
afterVault.lossUnrealized != beforeVault_[0].lossUnrealized)
|
afterVault.lossUnrealized.n != beforeVault_[0].lossUnrealized)
|
||||||
{
|
{
|
||||||
JLOG(j.fatal()) << //
|
JLOG(j.fatal()) << //
|
||||||
"Invariant failed: vault transaction must not change loss "
|
"Invariant failed: vault transaction must not change loss "
|
||||||
@@ -2711,7 +2732,7 @@ ValidVault::finalize(
|
|||||||
result = false;
|
result = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (beforeVault.assetsTotal != afterVault.assetsTotal)
|
if (beforeVault.assetsTotal.n != afterVault.assetsTotal)
|
||||||
{
|
{
|
||||||
JLOG(j.fatal()) << //
|
JLOG(j.fatal()) << //
|
||||||
"Invariant failed: set must not change assets "
|
"Invariant failed: set must not change assets "
|
||||||
@@ -2720,7 +2741,7 @@ ValidVault::finalize(
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (afterVault.assetsMaximum > zero &&
|
if (afterVault.assetsMaximum > zero &&
|
||||||
afterVault.assetsTotal > afterVault.assetsMaximum)
|
afterVault.assetsTotal.n > afterVault.assetsMaximum)
|
||||||
{
|
{
|
||||||
JLOG(j.fatal()) << //
|
JLOG(j.fatal()) << //
|
||||||
"Invariant failed: set assets outstanding must not "
|
"Invariant failed: set assets outstanding must not "
|
||||||
@@ -2728,7 +2749,7 @@ ValidVault::finalize(
|
|||||||
result = false;
|
result = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (beforeVault.assetsAvailable != afterVault.assetsAvailable)
|
if (beforeVault.assetsAvailable.n != afterVault.assetsAvailable)
|
||||||
{
|
{
|
||||||
JLOG(j.fatal()) << //
|
JLOG(j.fatal()) << //
|
||||||
"Invariant failed: set must not change assets "
|
"Invariant failed: set must not change assets "
|
||||||
@@ -2816,7 +2837,7 @@ ValidVault::finalize(
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (afterVault.assetsMaximum > zero &&
|
if (afterVault.assetsMaximum > zero &&
|
||||||
afterVault.assetsTotal > afterVault.assetsMaximum)
|
afterVault.assetsTotal.n > afterVault.assetsMaximum)
|
||||||
{
|
{
|
||||||
JLOG(j.fatal()) << //
|
JLOG(j.fatal()) << //
|
||||||
"Invariant failed: deposit assets outstanding must not "
|
"Invariant failed: deposit assets outstanding must not "
|
||||||
|
|||||||
@@ -5,6 +5,7 @@
|
|||||||
#include <xrpl/basics/base_uint.h>
|
#include <xrpl/basics/base_uint.h>
|
||||||
#include <xrpl/beast/utility/Journal.h>
|
#include <xrpl/beast/utility/Journal.h>
|
||||||
#include <xrpl/protocol/MPTIssue.h>
|
#include <xrpl/protocol/MPTIssue.h>
|
||||||
|
#include <xrpl/protocol/SField.h>
|
||||||
#include <xrpl/protocol/STLedgerEntry.h>
|
#include <xrpl/protocol/STLedgerEntry.h>
|
||||||
#include <xrpl/protocol/STTx.h>
|
#include <xrpl/protocol/STTx.h>
|
||||||
#include <xrpl/protocol/TER.h>
|
#include <xrpl/protocol/TER.h>
|
||||||
@@ -738,16 +739,38 @@ class ValidVault
|
|||||||
{
|
{
|
||||||
Number static constexpr zero{};
|
Number static constexpr zero{};
|
||||||
|
|
||||||
|
struct Vault;
|
||||||
|
|
||||||
|
struct NumberInfo final
|
||||||
|
{
|
||||||
|
Number n;
|
||||||
|
bool valid;
|
||||||
|
|
||||||
|
// Make this Number wrapper as transparent as possible, except when
|
||||||
|
// checking validity. However, rather than fleshing out all the
|
||||||
|
// comparison operators, etc, a few places will still need to specify
|
||||||
|
// "n".
|
||||||
|
operator Number const&() const;
|
||||||
|
|
||||||
|
private:
|
||||||
|
friend class ValidVault::Vault;
|
||||||
|
|
||||||
|
NumberInfo static make(
|
||||||
|
SLE const& from,
|
||||||
|
SF_NUMBER const& field,
|
||||||
|
Asset const& asset);
|
||||||
|
};
|
||||||
|
|
||||||
struct Vault final
|
struct Vault final
|
||||||
{
|
{
|
||||||
uint256 key = beast::zero;
|
uint256 key = beast::zero;
|
||||||
Asset asset = {};
|
Asset asset = {};
|
||||||
AccountID pseudoId = {};
|
AccountID pseudoId = {};
|
||||||
uint192 shareMPTID = beast::zero;
|
uint192 shareMPTID = beast::zero;
|
||||||
Number assetsTotal = 0;
|
NumberInfo assetsTotal{0, true};
|
||||||
Number assetsAvailable = 0;
|
NumberInfo assetsAvailable{0, true};
|
||||||
Number assetsMaximum = 0;
|
NumberInfo assetsMaximum{0, true};
|
||||||
Number lossUnrealized = 0;
|
NumberInfo lossUnrealized{0, true};
|
||||||
|
|
||||||
Vault static make(SLE const&);
|
Vault static make(SLE const&);
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -71,13 +71,9 @@ VaultClawback::preclaim(PreclaimContext const& ctx)
|
|||||||
}
|
}
|
||||||
|
|
||||||
Asset const vaultAsset = vault->at(sfAsset);
|
Asset const vaultAsset = vault->at(sfAsset);
|
||||||
if (auto const amount = ctx.tx[~sfAmount])
|
if (auto const amount = ctx.tx[~sfAmount];
|
||||||
{
|
amount && vaultAsset != amount->asset())
|
||||||
if (vaultAsset != amount->asset())
|
return tecWRONG_ASSET;
|
||||||
return tecWRONG_ASSET;
|
|
||||||
else if (!amount->validNumber())
|
|
||||||
return tecPRECISION_LOSS;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (vaultAsset.native())
|
if (vaultAsset.native())
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -193,7 +193,28 @@ VaultCreate::doApply()
|
|||||||
vault->at(sfLossUnrealized) = Number(0);
|
vault->at(sfLossUnrealized) = Number(0);
|
||||||
// Leave default values for AssetTotal and AssetAvailable, both zero.
|
// Leave default values for AssetTotal and AssetAvailable, both zero.
|
||||||
if (auto value = tx[~sfAssetsMaximum])
|
if (auto value = tx[~sfAssetsMaximum])
|
||||||
vault->at(sfAssetsMaximum) = *value;
|
{
|
||||||
|
auto assetsMaximumProxy = vault->at(~sfAssetsMaximum);
|
||||||
|
assetsMaximumProxy = *value;
|
||||||
|
if (auto const stNumber = assetsMaximumProxy.stValue();
|
||||||
|
stNumber && !stNumber->validNumber(asset))
|
||||||
|
{
|
||||||
|
JLOG(j_.warn()) << "VaultCreate: Invalid assets maximum value for "
|
||||||
|
"integral asset type: "
|
||||||
|
<< *value << " > " << STNumber::validNumberLimit();
|
||||||
|
return tecPRECISION_LOSS;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// TODO: Should integral types automatically set a limit to the
|
||||||
|
// Number::validNumberLimit() value? Or safeNumberLimit()?
|
||||||
|
/*
|
||||||
|
else if (asset.integral())
|
||||||
|
{
|
||||||
|
auto assetsMaximumProxy = vault->at(~sfAssetsMaximum);
|
||||||
|
assetsMaximumProxy = STNumber::validNumberLimit();
|
||||||
|
assetsMaximumProxy.stValue()->usesAsset(asset);
|
||||||
|
}
|
||||||
|
*/
|
||||||
vault->at(sfShareMPTID) = mptIssuanceID;
|
vault->at(sfShareMPTID) = mptIssuanceID;
|
||||||
if (auto value = tx[~sfData])
|
if (auto value = tx[~sfData])
|
||||||
vault->at(sfData) = *value;
|
vault->at(sfData) = *value;
|
||||||
@@ -204,13 +225,6 @@ VaultCreate::doApply()
|
|||||||
vault->at(sfWithdrawalPolicy) = vaultStrategyFirstComeFirstServe;
|
vault->at(sfWithdrawalPolicy) = vaultStrategyFirstComeFirstServe;
|
||||||
if (scale)
|
if (scale)
|
||||||
vault->at(sfScale) = scale;
|
vault->at(sfScale) = scale;
|
||||||
if (asset.integral())
|
|
||||||
{
|
|
||||||
// Only the Maximum can be a non-zero value, so only it needs to be
|
|
||||||
// checked.
|
|
||||||
if (!vault->at(sfAssetsMaximum).value().valid(Number::compatible))
|
|
||||||
return tecLIMIT_EXCEEDED;
|
|
||||||
}
|
|
||||||
view().insert(vault);
|
view().insert(vault);
|
||||||
|
|
||||||
// Explicitly create MPToken for the vault owner
|
// Explicitly create MPToken for the vault owner
|
||||||
|
|||||||
@@ -42,9 +42,6 @@ VaultDeposit::preclaim(PreclaimContext const& ctx)
|
|||||||
if (assets.asset() != vaultAsset)
|
if (assets.asset() != vaultAsset)
|
||||||
return tecWRONG_ASSET;
|
return tecWRONG_ASSET;
|
||||||
|
|
||||||
if (!assets.validNumber())
|
|
||||||
return tecPRECISION_LOSS;
|
|
||||||
|
|
||||||
if (vaultAsset.native())
|
if (vaultAsset.native())
|
||||||
; // No special checks for XRP
|
; // No special checks for XRP
|
||||||
else if (vaultAsset.holds<MPTIssue>())
|
else if (vaultAsset.holds<MPTIssue>())
|
||||||
@@ -230,14 +227,14 @@ VaultDeposit::doApply()
|
|||||||
return tecINTERNAL; // LCOV_EXCL_LINE
|
return tecINTERNAL; // LCOV_EXCL_LINE
|
||||||
sharesCreated = *maybeShares;
|
sharesCreated = *maybeShares;
|
||||||
}
|
}
|
||||||
if (sharesCreated == beast::zero || !sharesCreated.validNumber())
|
if (sharesCreated == beast::zero)
|
||||||
return tecPRECISION_LOSS;
|
return tecPRECISION_LOSS;
|
||||||
|
|
||||||
auto const maybeAssets =
|
auto const maybeAssets =
|
||||||
sharesToAssetsDeposit(vault, sleIssuance, sharesCreated);
|
sharesToAssetsDeposit(vault, sleIssuance, sharesCreated);
|
||||||
if (!maybeAssets)
|
if (!maybeAssets)
|
||||||
return tecINTERNAL; // LCOV_EXCL_LINE
|
return tecINTERNAL; // LCOV_EXCL_LINE
|
||||||
else if (*maybeAssets > amount || !maybeAssets->validNumber())
|
else if (*maybeAssets > amount)
|
||||||
{
|
{
|
||||||
// LCOV_EXCL_START
|
// LCOV_EXCL_START
|
||||||
JLOG(j_.error()) << "VaultDeposit: would take more than offered.";
|
JLOG(j_.error()) << "VaultDeposit: would take more than offered.";
|
||||||
@@ -263,13 +260,43 @@ VaultDeposit::doApply()
|
|||||||
sharesCreated.asset() != assetsDeposited.asset(),
|
sharesCreated.asset() != assetsDeposited.asset(),
|
||||||
"ripple::VaultDeposit::doApply : assets are not shares");
|
"ripple::VaultDeposit::doApply : assets are not shares");
|
||||||
|
|
||||||
vault->at(sfAssetsTotal) += assetsDeposited;
|
auto assetsTotalProxy = vault->at(sfAssetsTotal);
|
||||||
vault->at(sfAssetsAvailable) += assetsDeposited;
|
auto assetsAvailableProxy = vault->at(sfAssetsAvailable);
|
||||||
|
|
||||||
|
assetsTotalProxy += assetsDeposited;
|
||||||
|
assetsAvailableProxy += assetsDeposited;
|
||||||
view().update(vault);
|
view().update(vault);
|
||||||
|
|
||||||
|
auto const asset = *vault->at(sfAsset);
|
||||||
|
if (auto stNumber = assetsTotalProxy.stValue();
|
||||||
|
stNumber && !stNumber->safeNumber(asset))
|
||||||
|
{
|
||||||
|
JLOG(j_.warn()) << "VaultDeposit: Invalid assets total value for "
|
||||||
|
"integral asset type: "
|
||||||
|
<< *assetsTotalProxy << " > "
|
||||||
|
<< STNumber::safeNumberLimit();
|
||||||
|
return tecPRECISION_LOSS;
|
||||||
|
}
|
||||||
|
if (auto stNumber = assetsAvailableProxy.stValue();
|
||||||
|
stNumber && !stNumber->safeNumber(asset))
|
||||||
|
{
|
||||||
|
// LCOV_EXCL_START
|
||||||
|
// This should be impossible to reach because total should never be less
|
||||||
|
// than available, so if total is ok, available should be ok.
|
||||||
|
UNREACHABLE(
|
||||||
|
"ripple::VaultDeposit::doApply() : AssetsAvailable exceeds "
|
||||||
|
"AssetsTotal");
|
||||||
|
JLOG(j_.warn()) << "VaultDeposit: Invalid assets available value for "
|
||||||
|
"integral asset type: "
|
||||||
|
<< *assetsAvailableProxy << " > "
|
||||||
|
<< STNumber::safeNumberLimit();
|
||||||
|
return tecPRECISION_LOSS;
|
||||||
|
// LCOV_EXCL_STOP
|
||||||
|
}
|
||||||
|
|
||||||
// A deposit must not push the vault over its limit.
|
// A deposit must not push the vault over its limit.
|
||||||
auto const maximum = *vault->at(sfAssetsMaximum);
|
auto const maximum = *vault->at(sfAssetsMaximum);
|
||||||
if (maximum != 0 && *vault->at(sfAssetsTotal) > maximum)
|
if (maximum != 0 && *assetsTotalProxy > maximum)
|
||||||
return tecLIMIT_EXCEEDED;
|
return tecLIMIT_EXCEEDED;
|
||||||
|
|
||||||
// Transfer assets from depositor to vault.
|
// Transfer assets from depositor to vault.
|
||||||
|
|||||||
@@ -143,11 +143,18 @@ VaultSet::doApply()
|
|||||||
if (tx[sfAssetsMaximum] != 0 &&
|
if (tx[sfAssetsMaximum] != 0 &&
|
||||||
tx[sfAssetsMaximum] < *vault->at(sfAssetsTotal))
|
tx[sfAssetsMaximum] < *vault->at(sfAssetsTotal))
|
||||||
return tecLIMIT_EXCEEDED;
|
return tecLIMIT_EXCEEDED;
|
||||||
vault->at(sfAssetsMaximum) = tx[sfAssetsMaximum];
|
auto assetsMaximumProxy = vault->at(~sfAssetsMaximum);
|
||||||
if (vault->at(sfAsset).value().integral())
|
assetsMaximumProxy = tx[sfAssetsMaximum];
|
||||||
|
if (auto const stNumber = assetsMaximumProxy.stValue();
|
||||||
|
stNumber && !stNumber->validNumber(vault->at(sfAsset)))
|
||||||
{
|
{
|
||||||
if (!vault->at(sfAssetsMaximum).value().valid(Number::compatible))
|
// LCOV_EXCL_START
|
||||||
return tecLIMIT_EXCEEDED;
|
// This should be impossible, because invalid values would have been
|
||||||
|
// stopped by `VaultCreate`.
|
||||||
|
UNREACHABLE(
|
||||||
|
"ripple::VaultSet::doApply : invalid assets maximum value");
|
||||||
|
return tecLIMIT_EXCEEDED;
|
||||||
|
// LCOV_EXCL_STOP
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -174,8 +174,6 @@ VaultWithdraw::doApply()
|
|||||||
if (!maybeAssets)
|
if (!maybeAssets)
|
||||||
return tecINTERNAL; // LCOV_EXCL_LINE
|
return tecINTERNAL; // LCOV_EXCL_LINE
|
||||||
assetsWithdrawn = *maybeAssets;
|
assetsWithdrawn = *maybeAssets;
|
||||||
if (!assetsWithdrawn.validNumber())
|
|
||||||
return tecPRECISION_LOSS;
|
|
||||||
}
|
}
|
||||||
else if (amount.asset() == share)
|
else if (amount.asset() == share)
|
||||||
{
|
{
|
||||||
@@ -186,8 +184,6 @@ VaultWithdraw::doApply()
|
|||||||
if (!maybeAssets)
|
if (!maybeAssets)
|
||||||
return tecINTERNAL; // LCOV_EXCL_LINE
|
return tecINTERNAL; // LCOV_EXCL_LINE
|
||||||
assetsWithdrawn = *maybeAssets;
|
assetsWithdrawn = *maybeAssets;
|
||||||
if (!assetsWithdrawn.validNumber())
|
|
||||||
return tecPRECISION_LOSS;
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
return tefINTERNAL; // LCOV_EXCL_LINE
|
return tefINTERNAL; // LCOV_EXCL_LINE
|
||||||
|
|||||||
Reference in New Issue
Block a user