xrpld
Loading...
Searching...
No Matches
VaultInvariant.h
1#pragma once
2
3#include <xrpl/basics/Number.h>
4#include <xrpl/basics/base_uint.h>
5#include <xrpl/beast/utility/Journal.h>
6#include <xrpl/beast/utility/Zero.h>
7#include <xrpl/ledger/ReadView.h>
8#include <xrpl/protocol/AccountID.h>
9#include <xrpl/protocol/Asset.h>
10#include <xrpl/protocol/MPTIssue.h>
11#include <xrpl/protocol/Protocol.h>
12#include <xrpl/protocol/Rules.h>
13#include <xrpl/protocol/STLedgerEntry.h>
14#include <xrpl/protocol/STTx.h>
15#include <xrpl/protocol/TER.h>
16#include <xrpl/protocol/XRPAmount.h>
17
18#include <cstdint>
19#include <optional>
20#include <unordered_map>
21#include <vector>
22
23namespace xrpl {
24
66{
67 static constexpr Number kZero{};
68
89
90 struct Shares final
91 {
95
96 Shares static make(SLE const&);
97 };
98
99public:
100 struct DeltaInfo final
101 {
104
105 // Compute the delta between two Numbers, taking the coarsest scale
106 [[nodiscard]] static DeltaInfo
107 makeDelta(Number const& before, Number const& after, Asset const& asset);
108 };
109
110private:
116
129 [[nodiscard]] std::int32_t
130 computeVaultMinScale(DeltaInfo const& vaultDelta, Rules const& rules) const;
131
142 [[nodiscard]] std::optional<DeltaInfo>
143 deltaAssets(AccountID const& id) const;
144
159 [[nodiscard]] static std::optional<AccountID>
160 feePayerAccountRoot(ReadView const& view, STTx const& tx);
161
190 [[nodiscard]] std::optional<DeltaInfo>
192 ReadView const& view,
193 AccountID const& id,
194 STTx const& tx,
195 XRPAmount fee,
196 bool fix340Enabled) const;
197
208 [[nodiscard]] std::optional<DeltaInfo>
209 deltaShares(AccountID const& id) const;
210
218 [[nodiscard]] static bool
219 isVaultEmpty(Vault const& vault);
220
229 [[nodiscard]] bool
230 finalizeLoanSet(ReadView const& view, beast::Journal const& j) const;
231
253 [[nodiscard]] static bool
255 Vault const& afterVault,
256 Vault const& beforeVault,
257 Asset const& vaultAsset,
258 bool fix340Enabled,
259 std::int32_t minScale,
260 Number const& roundedVaultDelta,
261 Number const& exactVaultDelta,
262 char const* verb,
263 beast::Journal const& j);
264
265public:
266 // Compute the coarsest scale required to represent all numbers
267 [[nodiscard]] static std::int32_t
269
270 void
272
273 bool
274 finalize(STTx const&, TER const, XRPAmount const, ReadView const&, beast::Journal const&);
275};
276
277} // namespace xrpl
A generic endpoint for log messages.
Definition Journal.h:44
Number is a floating point type that can represent a wide range of values.
Definition Number.h:351
A view into a ledger.
Definition ReadView.h:41
Rules controlling protocol behavior.
Definition Rules.h:40
std::shared_ptr< STLedgerEntry const > const & ConstRef
Invariants: Vault object and MPTokenIssuance for vault shares.
static std::optional< AccountID > feePayerAccountRoot(ReadView const &view, STTx const &tx)
Return the AccountRoot whose XRP balance actually absorbed a transaction's fee, if any.
std::unordered_map< UInt256, DeltaInfo > deltas_
std::vector< Shares > afterMPTs_
std::optional< DeltaInfo > deltaAssetsForParty(ReadView const &view, AccountID const &id, STTx const &tx, XRPAmount fee, bool fix340Enabled) const
Return the vault-asset delta for a party inspected as a withdrawal/deposit counterparty,...
static bool isVaultEmpty(Vault const &vault)
Check whether a vault holds no assets.
static bool checkTotalsAddUp(Vault const &afterVault, Vault const &beforeVault, Asset const &vaultAsset, bool fix340Enabled, std::int32_t minScale, Number const &roundedVaultDelta, Number const &exactVaultDelta, char const *verb, beast::Journal const &j)
Check that a vault's AssetsTotal and AssetsAvailable deltas add up against the real transfer,...
std::vector< Vault > afterVault_
std::optional< DeltaInfo > deltaShares(AccountID const &id) const
Return the vault-share balance-change delta for an account.
void visitEntry(bool, SLE::ConstRef, SLE::ConstRef)
std::vector< Shares > beforeMPTs_
static std::int32_t computeCoarsestScale(std::vector< DeltaInfo > const &numbers)
bool finalizeLoanSet(ReadView const &view, beast::Journal const &j) const
Invariant check for ttLOAN_SET.
bool finalize(STTx const &, TER const, XRPAmount const, ReadView const &, beast::Journal const &)
std::vector< Vault > beforeVault_
static constexpr Number kZero
std::optional< DeltaInfo > deltaAssets(AccountID const &id) const
Return the vault-asset balance-change delta for an account.
std::int32_t computeVaultMinScale(DeltaInfo const &vaultDelta, Rules const &rules) const
Compute the minimum STAmount scale for rounding invariant calculations.
constexpr Zero kZero
Definition Zero.h:30
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
static constexpr Number kNumZero
Definition Number.h:667
BaseUInt< 192 > UInt192
Definition base_uint.h:582
STLedgerEntry SLE
BaseUInt< 256 > UInt256
Definition base_uint.h:581
VaultVersion
Vault ledger-entry schema versions, persisted as sfLEVersion.
Definition Protocol.h:342
bool after(NetClock::time_point now, std::uint32_t mark)
Has the specified time passed?
Definition View.cpp:668
BaseUInt< 160, detail::AccountIDTag > AccountID
A 160-bit unsigned that uniquely identifies an account.
Definition AccountID.h:34
TERSubset< CanCvtToTER > TER
Definition TER.h:657
static DeltaInfo makeDelta(Number const &before, Number const &after, Asset const &asset)
std::optional< int > scale
static Shares make(SLE const &)
std::optional< std::uint8_t > vaultKind
std::optional< std::uint32_t > subscriptionDate
std::optional< std::uint32_t > redemptionDate
static Vault make(SLE const &)