mirror of
https://github.com/Xahau/xahaud.git
synced 2026-06-04 17:26:39 +00:00
* Add jss fields used by Clio `nft_info`: (#4320) Add Clio-specific JSS constants to ensure a common vocabulary of keywords in Clio and this project. By providing visibility of the full API keyword namespace, it reduces the likelihood of developers introducing minor variations on names used by Clio, or unknowingly claiming a keyword that Clio has already claimed. This change moves this project slightly away from having only the code necessary for running the core server, but it is a step toward the goal of keeping this server's and Clio's APIs similar. The added JSS constants are annotated to indicate their relevance to Clio. Clio can be found here: https://github.com/XRPLF/clio Signed-off-by: ledhed2222 <ledhed2222@users.noreply.github.com> * Introduce support for a slabbed allocator: (#4218) When instantiating a large amount of fixed-sized objects on the heap the overhead that dynamic memory allocation APIs impose will quickly become significant. In some cases, allocating a large amount of memory at once and using a slabbing allocator to carve the large block into fixed-sized units that are used to service requests for memory out will help to reduce memory fragmentation significantly and, potentially, improve overall performance. This commit introduces a new `SlabAllocator<>` class that exposes an API that is _similar_ to the C++ concept of an `Allocator` but it is not meant to be a general-purpose allocator. It should not be used unless profiling and analysis of specific memory allocation patterns indicates that the additional complexity introduced will improve the performance of the system overall, and subsequent profiling proves it. A helper class, `SlabAllocatorSet<>` simplifies handling of variably sized objects that benefit from slab allocations. This commit incorporates improvements suggested by Greg Popovitch (@greg7mdp). Commit 1 of 3 in #4218. * Optimize `SHAMapItem` and leverage new slab allocator: (#4218) The `SHAMapItem` class contains a variable-sized buffer that holds the serialized data associated with a particular item inside a `SHAMap`. Prior to this commit, the buffer for the serialized data was allocated separately. Coupled with the fact that most instances of `SHAMapItem` were wrapped around a `std::shared_ptr` meant that an instantiation might result in up to three separate memory allocations. This commit switches away from `std::shared_ptr` for `SHAMapItem` and uses `boost::intrusive_ptr` instead, allowing the reference count for an instance to live inside the instance itself. Coupled with using a slab-based allocator to optimize memory allocation for the most commonly sized buffers, the net result is significant memory savings. In testing, the reduction in memory usage hovers between 400MB and 650MB. Other scenarios might result in larger savings. In performance testing with NFTs, this commit reduces memory size by about 15% sustained over long duration. Commit 2 of 3 in #4218. * Avoid using std::shared_ptr when not necessary: (#4218) The `Ledger` class contains two `SHAMap` instances: the state and transaction maps. Previously, the maps were dynamically allocated using `std::make_shared` despite the fact that they did not require lifetime management separate from the lifetime of the `Ledger` instance to which they belong. The two `SHAMap` instances are now regular member variables. Some smart pointers and dynamic memory allocation was avoided by using stack-based alternatives. Commit 3 of 3 in #4218. * Prevent replay attacks with NetworkID field: (#4370) Add a `NetworkID` field to help prevent replay attacks on and from side-chains. The new field must be used when the server is using a network id > 1024. To preserve legacy behavior, all chains with a network ID less than 1025 retain the existing behavior. This includes Mainnet, Testnet, Devnet, and hooks-testnet. If `sfNetworkID` is present in any transaction submitted to any of the nodes on one of these chains, then `telNETWORK_ID_MAKES_TX_NON_CANONICAL` is returned. Since chains with a network ID less than 1025, including Mainnet, retain the existing behavior, there is no need for an amendment. The `NetworkID` helps to prevent replay attacks because users specify a `NetworkID` field in every transaction for that chain. This change introduces a new UINT32 field, `sfNetworkID` ("NetworkID"). There are also three new local error codes for transaction results: - `telNETWORK_ID_MAKES_TX_NON_CANONICAL` - `telREQUIRES_NETWORK_ID` - `telWRONG_NETWORK` To learn about the other transaction result codes, see: https://xrpl.org/transaction-results.html Local error codes were chosen because a transaction is not necessarily malformed if it is submitted to a node running on the incorrect chain. This is a local error specific to that node and could be corrected by switching to a different node or by changing the `network_id` on that node. See: https://xrpl.org/connect-your-rippled-to-the-xrp-test-net.html In addition to using `NetworkID`, it is still generally recommended to use different accounts and keys on side-chains. However, people will undoubtedly use the same keys on multiple chains; for example, this is common practice on other blockchain networks. There are also some legitimate use cases for this. A `app.NetworkID` test suite has been added, and `core.Config` was updated to include some network_id tests. * Fix the fix for std::result_of (#4496) Newer compilers, such as Apple Clang 15.0, have removed `std::result_of` as part of C++20. The build instructions provided a fix for this (by adding a preprocessor definition), but the fix was broken. This fixes the fix by: * Adding the `conf` prefix for tool configurations (which had been forgotten). * Passing `extra_b2_flags` to `boost` package to fix its build. * Define `BOOST_ASIO_HAS_STD_INVOKE_RESULT` in order to build boost 1.77 with a newer compiler. * Use quorum specified via command line: (#4489) If `--quorum` setting is present on the command line, use the specified value as the minimum quorum. This allows for the use of a potentially fork-unsafe quorum, but it is sometimes necessary for small and test networks. Fix #4488. --------- Co-authored-by: RichardAH <richard.holland@starstone.co.nz> * Fix errors for Clang 16: (#4501) Address issues related to the removal of `std::{u,bi}nary_function` in C++17 and some warnings with Clang 16. Some warnings appeared with the upgrade to Apple clang version 14.0.3 (clang-1403.0.22.14.1). - `std::{u,bi}nary_function` were removed in C++17. They were empty classes with a few associated types. We already have conditional code to define the types. Just make it unconditional. - libc++ checks a cast in an unevaluated context to see if a type inherits from a binary function class in the standard library, e.g. `std::equal_to`, and this causes an error when the type privately inherits from such a class. Change these instances to public inheritance. - We don't need a middle-man for the empty base optimization. Prefer to inherit directly from an empty class than from `beast::detail::empty_base_optimization`. - Clang warns when all the uses of a variable are removed by conditional compilation of assertions. Add a `[[maybe_unused]]` annotation to suppress it. - As a drive-by clean-up, remove commented code. See related work in #4486. * Fix typo (#4508) * fix!: Prevent API from accepting seed or public key for account (#4404) The API would allow seeds (and public keys) to be used in place of accounts at several locations in the API. For example, when calling account_info, you could pass `"account": "foo"`. The string "foo" is treated like a seed, so the method returns `actNotFound` (instead of `actMalformed`, as most developers would expect). In the early days, this was a convenience to make testing easier. However, it allows for poor security practices, so it is no longer a good idea. Allowing a secret or passphrase is now considered a bug. Previously, it was controlled by the `strict` option on some methods. With this commit, since the API does not interpret `account` as `seed`, the option `strict` is no longer needed and is removed. Removing this behavior from the API is a [breaking change](https://xrpl.org/request-formatting.html#breaking-changes). One could argue that it shouldn't be done without bumping the API version; however, in this instance, there is no evidence that anyone is using the API in the "legacy" way. Furthermore, it is a potential security hole, as it allows users to send secrets to places where they are not needed, where they could end up in logs, error messages, etc. There's no reason to take such a risk with a seed/secret, since only the public address is needed. Resolves: #3329, #3330, #4337 BREAKING CHANGE: Remove non-strict account parsing (#3330) * Add nftoken_id, nftoken_ids, offer_id fields for NFTokens (#4447) Three new fields are added to the `Tx` responses for NFTs: 1. `nftoken_id`: This field is included in the `Tx` responses for `NFTokenMint` and `NFTokenAcceptOffer`. This field indicates the `NFTokenID` for the `NFToken` that was modified on the ledger by the transaction. 2. `nftoken_ids`: This array is included in the `Tx` response for `NFTokenCancelOffer`. This field provides a list of all the `NFTokenID`s for the `NFToken`s that were modified on the ledger by the transaction. 3. `offer_id`: This field is included in the `Tx` response for `NFTokenCreateOffer` transactions and shows the OfferID of the `NFTokenOffer` created. The fields make it easier to track specific tokens and offers. The implementation includes code (by @ledhed2222) from the Clio project to extract NFTokenIDs from mint transactions. * Ensure that switchover vars are initialized before use: (#4527) Global variables in different TUs are initialized in an undefined order. At least one global variable was accessing a global switchover variable. This caused the switchover variable to be accessed in an uninitialized state. Since the switchover is always explicitly set before transaction processing, this bug can not effect transaction processing, but could effect unit tests (and potentially the value of some global variables). Note: at the time of this patch the offending bug is not yet in production. * Move faulty assert (#4533) This assert was put in the wrong place, but it only triggers if shards are configured. This change moves the assert to the right place and updates it to ensure correctness. The assert could be hit after the server downloads some shards. It may be necessary to restart after the shards are downloaded. Note that asserts are normally checked only in debug builds, so release packages should not be affected. Introduced in: #4319 (66627b26cf) * Fix unaligned load and stores: (#4528) (#4531) Misaligned load and store operations are supported by both Intel and ARM CPUs. However, in C++, these operations are undefined behavior (UB). Substituting these operations with a `memcpy` fixes this UB. The compiled assembly code is equivalent to the original, so there is no performance penalty to using memcpy. For context: The unaligned load and store operations fixed here were originally introduced in the slab allocator (#4218). * Add missing includes for gcc 13.1: (#4555) gcc 13.1 failed to compile due to missing headers. This patch adds the needed headers. * Trivial: add comments for NFToken-related invariants (#4558) * fix node size estimation (#4536) Fix a bug in the `NODE_SIZE` auto-detection feature in `Config.cpp`. Specifically, this patch corrects the calculation for the total amount of RAM available, which was previously returned in bytes, but is now being returned in units of the system's memory unit. Additionally, the patch adjusts the node size based on the number of available hardware threads of execution. * fix: remove redundant moves (#4565) - Resolve gcc compiler warning: AccountObjects.cpp:182:47: warning: redundant move in initialization [-Wredundant-move] - The std::move() operation on trivially copyable types may generate a compile warning in newer versions of gcc. - Remove extraneous header (unused imports) from a unit test file. * Revert "Fix the fix for std::result_of (#4496)" This reverts commitcee8409d60. * Revert "Fix typo (#4508)" This reverts commit2956f14de8. * clang * [fold] bad merge * [fold] fix bad merge - add back filter for ripple state on account_channels - add back network id test (env auto adds network id in xahau) * [fold] fix build error --------- Signed-off-by: ledhed2222 <ledhed2222@users.noreply.github.com> Co-authored-by: ledhed2222 <ledhed2222@users.noreply.github.com> Co-authored-by: Nik Bougalis <nikb@bougalis.net> Co-authored-by: RichardAH <richard.holland@starstone.co.nz> Co-authored-by: John Freeman <jfreeman08@gmail.com> Co-authored-by: Mark Travis <mtrippled@users.noreply.github.com> Co-authored-by: solmsted <steven.olm@gmail.com> Co-authored-by: drlongle <drlongle@gmail.com> Co-authored-by: Shawn Xie <35279399+shawnxie999@users.noreply.github.com> Co-authored-by: Scott Determan <scott.determan@yahoo.com> Co-authored-by: Ed Hennis <ed@ripple.com> Co-authored-by: Scott Schurr <scott@ripple.com> Co-authored-by: Chenna Keshava B S <21219765+ckeshava@users.noreply.github.com>
620 lines
14 KiB
C++
620 lines
14 KiB
C++
//------------------------------------------------------------------------------
|
|
/*
|
|
This file is part of rippled: https://github.com/ripple/rippled
|
|
Copyright (c) 2012, 2013 Ripple Labs Inc.
|
|
|
|
Permission to use, copy, modify, and/or distribute this software for any
|
|
purpose with or without fee is hereby granted, provided that the above
|
|
copyright notice and this permission notice appear in all copies.
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
*/
|
|
//==============================================================================
|
|
|
|
#ifndef RIPPLE_PROTOCOL_STAMOUNT_H_INCLUDED
|
|
#define RIPPLE_PROTOCOL_STAMOUNT_H_INCLUDED
|
|
|
|
#include <ripple/basics/CountedObject.h>
|
|
#include <ripple/basics/IOUAmount.h>
|
|
#include <ripple/basics/LocalValue.h>
|
|
#include <ripple/basics/Number.h>
|
|
#include <ripple/basics/XRPAmount.h>
|
|
#include <ripple/protocol/Issue.h>
|
|
#include <ripple/protocol/SField.h>
|
|
#include <ripple/protocol/STBase.h>
|
|
#include <ripple/protocol/Serializer.h>
|
|
|
|
namespace ripple {
|
|
|
|
// Internal form:
|
|
// 1: If amount is zero, then value is zero and offset is -100
|
|
// 2: Otherwise:
|
|
// legal offset range is -96 to +80 inclusive
|
|
// value range is 10^15 to (10^16 - 1) inclusive
|
|
// amount = value * [10 ^ offset]
|
|
|
|
// Wire form:
|
|
// High 8 bits are (offset+142), legal range is, 80 to 22 inclusive
|
|
// Low 56 bits are value, legal range is 10^15 to (10^16 - 1) inclusive
|
|
class STAmount final : public STBase, public CountedObject<STAmount>
|
|
{
|
|
public:
|
|
using mantissa_type = std::uint64_t;
|
|
using exponent_type = int;
|
|
using rep = std::pair<mantissa_type, exponent_type>;
|
|
|
|
private:
|
|
Issue mIssue;
|
|
mantissa_type mValue;
|
|
exponent_type mOffset;
|
|
bool mIsNative; // A shorthand for isXRP(mIssue).
|
|
bool mIsNegative;
|
|
|
|
public:
|
|
using value_type = STAmount;
|
|
|
|
static const int cMinOffset = -96;
|
|
static const int cMaxOffset = 80;
|
|
|
|
// Maximum native value supported by the code
|
|
static const std::uint64_t cMinValue = 1000000000000000ull;
|
|
static const std::uint64_t cMaxValue = 9999999999999999ull;
|
|
static const std::uint64_t cMaxNative = 9000000000000000000ull;
|
|
|
|
// Max native value on network.
|
|
static const std::uint64_t cMaxNativeN = 100000000000000000ull;
|
|
static const std::uint64_t cNotNative = 0x8000000000000000ull;
|
|
static const std::uint64_t cPosNative = 0x4000000000000000ull;
|
|
|
|
static std::uint64_t const uRateOne;
|
|
|
|
//--------------------------------------------------------------------------
|
|
STAmount(SerialIter& sit, SField const& name);
|
|
|
|
struct unchecked
|
|
{
|
|
explicit unchecked() = default;
|
|
};
|
|
|
|
// Do not call canonicalize
|
|
STAmount(
|
|
SField const& name,
|
|
Issue const& issue,
|
|
mantissa_type mantissa,
|
|
exponent_type exponent,
|
|
bool native,
|
|
bool negative,
|
|
unchecked);
|
|
|
|
STAmount(
|
|
Issue const& issue,
|
|
mantissa_type mantissa,
|
|
exponent_type exponent,
|
|
bool native,
|
|
bool negative,
|
|
unchecked);
|
|
|
|
// Call canonicalize
|
|
STAmount(
|
|
SField const& name,
|
|
Issue const& issue,
|
|
mantissa_type mantissa,
|
|
exponent_type exponent,
|
|
bool native,
|
|
bool negative);
|
|
|
|
STAmount(SField const& name, std::int64_t mantissa);
|
|
|
|
STAmount(
|
|
SField const& name,
|
|
std::uint64_t mantissa = 0,
|
|
bool negative = false);
|
|
|
|
STAmount(
|
|
SField const& name,
|
|
Issue const& issue,
|
|
std::uint64_t mantissa = 0,
|
|
int exponent = 0,
|
|
bool negative = false);
|
|
|
|
explicit STAmount(std::uint64_t mantissa = 0, bool negative = false);
|
|
|
|
STAmount(
|
|
Issue const& issue,
|
|
std::uint64_t mantissa = 0,
|
|
int exponent = 0,
|
|
bool negative = false);
|
|
|
|
// VFALCO Is this needed when we have the previous signature?
|
|
STAmount(
|
|
Issue const& issue,
|
|
std::uint32_t mantissa,
|
|
int exponent = 0,
|
|
bool negative = false);
|
|
|
|
STAmount(Issue const& issue, std::int64_t mantissa, int exponent = 0);
|
|
|
|
STAmount(Issue const& issue, int mantissa, int exponent = 0);
|
|
|
|
// Legacy support for new-style amounts
|
|
STAmount(IOUAmount const& amount, Issue const& issue);
|
|
STAmount(XRPAmount const& amount);
|
|
operator Number() const;
|
|
|
|
//--------------------------------------------------------------------------
|
|
//
|
|
// Observers
|
|
//
|
|
//--------------------------------------------------------------------------
|
|
|
|
int
|
|
exponent() const noexcept;
|
|
|
|
bool
|
|
native() const noexcept;
|
|
|
|
bool
|
|
negative() const noexcept;
|
|
|
|
std::uint64_t
|
|
mantissa() const noexcept;
|
|
|
|
Issue const&
|
|
issue() const;
|
|
|
|
// These three are deprecated
|
|
Currency const&
|
|
getCurrency() const;
|
|
|
|
AccountID const&
|
|
getIssuer() const;
|
|
|
|
int
|
|
signum() const noexcept;
|
|
|
|
/** Returns a zero value with the same issuer and currency. */
|
|
STAmount
|
|
zeroed() const;
|
|
|
|
void
|
|
setJson(Json::Value&) const;
|
|
|
|
STAmount const&
|
|
value() const noexcept;
|
|
|
|
//--------------------------------------------------------------------------
|
|
//
|
|
// Operators
|
|
//
|
|
//--------------------------------------------------------------------------
|
|
|
|
explicit operator bool() const noexcept;
|
|
|
|
STAmount&
|
|
operator+=(STAmount const&);
|
|
STAmount&
|
|
operator-=(STAmount const&);
|
|
|
|
STAmount& operator=(beast::Zero);
|
|
|
|
STAmount&
|
|
operator=(XRPAmount const& amount);
|
|
|
|
//--------------------------------------------------------------------------
|
|
//
|
|
// Modification
|
|
//
|
|
//--------------------------------------------------------------------------
|
|
|
|
void
|
|
negate();
|
|
|
|
void
|
|
clear();
|
|
|
|
// Zero while copying currency and issuer.
|
|
void
|
|
clear(STAmount const& saTmpl);
|
|
|
|
void
|
|
clear(Issue const& issue);
|
|
|
|
void
|
|
setIssuer(AccountID const& uIssuer);
|
|
|
|
/** Set the Issue for this amount and update mIsNative. */
|
|
void
|
|
setIssue(Issue const& issue);
|
|
|
|
//--------------------------------------------------------------------------
|
|
//
|
|
// STBase
|
|
//
|
|
//--------------------------------------------------------------------------
|
|
|
|
SerializedTypeID
|
|
getSType() const override;
|
|
|
|
std::string
|
|
getFullText() const override;
|
|
|
|
std::string
|
|
getText() const override;
|
|
|
|
Json::Value getJson(JsonOptions) const override;
|
|
|
|
void
|
|
add(Serializer& s) const override;
|
|
|
|
bool
|
|
isEquivalent(const STBase& t) const override;
|
|
|
|
bool
|
|
isDefault() const override;
|
|
|
|
XRPAmount
|
|
xrp() const;
|
|
IOUAmount
|
|
iou() const;
|
|
|
|
private:
|
|
static std::unique_ptr<STAmount>
|
|
construct(SerialIter&, SField const& name);
|
|
|
|
void
|
|
set(std::int64_t v);
|
|
void
|
|
canonicalize();
|
|
|
|
STBase*
|
|
copy(std::size_t n, void* buf) const override;
|
|
STBase*
|
|
move(std::size_t n, void* buf) override;
|
|
|
|
STAmount&
|
|
operator=(IOUAmount const& iou);
|
|
|
|
friend class detail::STVar;
|
|
|
|
friend STAmount
|
|
operator+(STAmount const& v1, STAmount const& v2);
|
|
};
|
|
|
|
//------------------------------------------------------------------------------
|
|
//
|
|
// Creation
|
|
//
|
|
//------------------------------------------------------------------------------
|
|
|
|
// VFALCO TODO The parameter type should be Quality not uint64_t
|
|
STAmount
|
|
amountFromQuality(std::uint64_t rate);
|
|
|
|
STAmount
|
|
amountFromString(Issue const& issue, std::string const& amount);
|
|
|
|
STAmount
|
|
amountFromJson(SField const& name, Json::Value const& v);
|
|
|
|
bool
|
|
amountFromJsonNoThrow(STAmount& result, Json::Value const& jvSource);
|
|
|
|
// IOUAmount and XRPAmount define toSTAmount, defining this
|
|
// trivial conversion here makes writing generic code easier
|
|
inline STAmount const&
|
|
toSTAmount(STAmount const& a)
|
|
{
|
|
return a;
|
|
}
|
|
|
|
//------------------------------------------------------------------------------
|
|
//
|
|
// Observers
|
|
//
|
|
//------------------------------------------------------------------------------
|
|
|
|
inline int
|
|
STAmount::exponent() const noexcept
|
|
{
|
|
return mOffset;
|
|
}
|
|
|
|
inline bool
|
|
STAmount::native() const noexcept
|
|
{
|
|
return mIsNative;
|
|
}
|
|
|
|
inline bool
|
|
STAmount::negative() const noexcept
|
|
{
|
|
return mIsNegative;
|
|
}
|
|
|
|
inline std::uint64_t
|
|
STAmount::mantissa() const noexcept
|
|
{
|
|
return mValue;
|
|
}
|
|
|
|
inline Issue const&
|
|
STAmount::issue() const
|
|
{
|
|
return mIssue;
|
|
}
|
|
|
|
inline Currency const&
|
|
STAmount::getCurrency() const
|
|
{
|
|
return mIssue.currency;
|
|
}
|
|
|
|
inline AccountID const&
|
|
STAmount::getIssuer() const
|
|
{
|
|
return mIssue.account;
|
|
}
|
|
|
|
inline int
|
|
STAmount::signum() const noexcept
|
|
{
|
|
return mValue ? (mIsNegative ? -1 : 1) : 0;
|
|
}
|
|
|
|
inline STAmount
|
|
STAmount::zeroed() const
|
|
{
|
|
return STAmount(mIssue);
|
|
}
|
|
|
|
inline STAmount::operator bool() const noexcept
|
|
{
|
|
return *this != beast::zero;
|
|
}
|
|
|
|
inline STAmount::operator Number() const
|
|
{
|
|
if (mIsNative)
|
|
return xrp();
|
|
return iou();
|
|
}
|
|
|
|
inline STAmount& STAmount::operator=(beast::Zero)
|
|
{
|
|
clear();
|
|
return *this;
|
|
}
|
|
|
|
inline STAmount&
|
|
STAmount::operator=(XRPAmount const& amount)
|
|
{
|
|
*this = STAmount(amount);
|
|
return *this;
|
|
}
|
|
|
|
inline void
|
|
STAmount::negate()
|
|
{
|
|
if (*this != beast::zero)
|
|
mIsNegative = !mIsNegative;
|
|
}
|
|
|
|
inline void
|
|
STAmount::clear()
|
|
{
|
|
// The -100 is used to allow 0 to sort less than a small positive values
|
|
// which have a negative exponent.
|
|
mOffset = mIsNative ? 0 : -100;
|
|
mValue = 0;
|
|
mIsNegative = false;
|
|
}
|
|
|
|
// Zero while copying currency and issuer.
|
|
inline void
|
|
STAmount::clear(STAmount const& saTmpl)
|
|
{
|
|
clear(saTmpl.mIssue);
|
|
}
|
|
|
|
inline void
|
|
STAmount::clear(Issue const& issue)
|
|
{
|
|
setIssue(issue);
|
|
clear();
|
|
}
|
|
|
|
inline void
|
|
STAmount::setIssuer(AccountID const& uIssuer)
|
|
{
|
|
mIssue.account = uIssuer;
|
|
setIssue(mIssue);
|
|
}
|
|
|
|
inline STAmount const&
|
|
STAmount::value() const noexcept
|
|
{
|
|
return *this;
|
|
}
|
|
|
|
inline bool
|
|
isLegalNet(STAmount const& value)
|
|
{
|
|
return !value.native() || (value.mantissa() <= STAmount::cMaxNativeN);
|
|
}
|
|
|
|
//------------------------------------------------------------------------------
|
|
//
|
|
// Operators
|
|
//
|
|
//------------------------------------------------------------------------------
|
|
|
|
bool
|
|
operator==(STAmount const& lhs, STAmount const& rhs);
|
|
bool
|
|
operator<(STAmount const& lhs, STAmount const& rhs);
|
|
|
|
inline bool
|
|
operator!=(STAmount const& lhs, STAmount const& rhs)
|
|
{
|
|
return !(lhs == rhs);
|
|
}
|
|
|
|
inline bool
|
|
operator>(STAmount const& lhs, STAmount const& rhs)
|
|
{
|
|
return rhs < lhs;
|
|
}
|
|
|
|
inline bool
|
|
operator<=(STAmount const& lhs, STAmount const& rhs)
|
|
{
|
|
return !(rhs < lhs);
|
|
}
|
|
|
|
inline bool
|
|
operator>=(STAmount const& lhs, STAmount const& rhs)
|
|
{
|
|
return !(lhs < rhs);
|
|
}
|
|
|
|
STAmount
|
|
operator-(STAmount const& value);
|
|
|
|
//------------------------------------------------------------------------------
|
|
//
|
|
// Arithmetic
|
|
//
|
|
//------------------------------------------------------------------------------
|
|
|
|
STAmount
|
|
operator+(STAmount const& v1, STAmount const& v2);
|
|
STAmount
|
|
operator-(STAmount const& v1, STAmount const& v2);
|
|
|
|
STAmount
|
|
divide(STAmount const& v1, STAmount const& v2, Issue const& issue);
|
|
|
|
STAmount
|
|
multiply(STAmount const& v1, STAmount const& v2, Issue const& issue);
|
|
|
|
// multiply, or divide rounding result in specified direction
|
|
STAmount
|
|
mulRound(
|
|
STAmount const& v1,
|
|
STAmount const& v2,
|
|
Issue const& issue,
|
|
bool roundUp);
|
|
|
|
STAmount
|
|
divRound(
|
|
STAmount const& v1,
|
|
STAmount const& v2,
|
|
Issue const& issue,
|
|
bool roundUp);
|
|
|
|
// Someone is offering X for Y, what is the rate?
|
|
// Rate: smaller is better, the taker wants the most out: in/out
|
|
// VFALCO TODO Return a Quality object
|
|
std::uint64_t
|
|
getRate(STAmount const& offerOut, STAmount const& offerIn);
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
inline bool
|
|
isXRP(STAmount const& amount)
|
|
{
|
|
return isXRP(amount.issue().currency);
|
|
}
|
|
|
|
inline bool
|
|
isBadCurrency(STAmount const& amount)
|
|
{
|
|
if (amount.native())
|
|
return false;
|
|
|
|
return isBadCurrency(amount.issue().currency);
|
|
}
|
|
|
|
/** returns true iff adding or subtracting results in less than or equal to
|
|
* 0.01% precision loss **/
|
|
inline bool
|
|
isAddable(STAmount const& amt1, STAmount const& amt2)
|
|
{
|
|
// special case: adding anything to zero is always fine
|
|
if (amt1 == beast::zero || amt2 == beast::zero)
|
|
return true;
|
|
|
|
// special case: adding two xrp amounts together.
|
|
// this is just an overflow check
|
|
if (isXRP(amt1) && isXRP(amt2))
|
|
{
|
|
XRPAmount A = (amt1.signum() == -1 ? -(amt1.xrp()) : amt1.xrp());
|
|
XRPAmount B = (amt2.signum() == -1 ? -(amt2.xrp()) : amt2.xrp());
|
|
|
|
XRPAmount finalAmt = A + B;
|
|
return (finalAmt >= A && finalAmt >= B);
|
|
}
|
|
|
|
static const STAmount one{IOUAmount{1, 0}, noIssue()};
|
|
static const STAmount maxLoss{IOUAmount{1, -4}, noIssue()};
|
|
|
|
STAmount A = amt1;
|
|
STAmount B = amt2;
|
|
|
|
if (isXRP(A))
|
|
A = STAmount{IOUAmount{A.xrp().drops(), -6}, noIssue()};
|
|
|
|
if (isXRP(B))
|
|
B = STAmount{IOUAmount{B.xrp().drops(), -6}, noIssue()};
|
|
|
|
A.setIssue(noIssue());
|
|
B.setIssue(noIssue());
|
|
|
|
STAmount lhs = divide((A - B) + B, A, noIssue()) - one;
|
|
STAmount rhs = divide((B - A) + A, B, noIssue()) - one;
|
|
|
|
return ((rhs.negative() ? -rhs : rhs) + (lhs.negative() ? -lhs : lhs)) <=
|
|
maxLoss;
|
|
}
|
|
|
|
// Since `canonicalize` does not have access to a ledger, this is needed to put
|
|
// the low-level routine stAmountCanonicalize on an amendment switch. Only
|
|
// transactions need to use this switchover. Outside of a transaction it's safe
|
|
// to unconditionally use the new behavior.
|
|
|
|
bool
|
|
getSTAmountCanonicalizeSwitchover();
|
|
|
|
void
|
|
setSTAmountCanonicalizeSwitchover(bool v);
|
|
|
|
/** RAII class to set and restore the STAmount canonicalize switchover.
|
|
*/
|
|
|
|
class STAmountSO
|
|
{
|
|
public:
|
|
explicit STAmountSO(bool v) : saved_(getSTAmountCanonicalizeSwitchover())
|
|
{
|
|
setSTAmountCanonicalizeSwitchover(v);
|
|
}
|
|
|
|
~STAmountSO()
|
|
{
|
|
setSTAmountCanonicalizeSwitchover(saved_);
|
|
}
|
|
|
|
private:
|
|
bool saved_;
|
|
};
|
|
|
|
} // namespace ripple
|
|
|
|
#endif
|