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/ledger/ReadView.h>
7#include <xrpl/protocol/AccountID.h>
8#include <xrpl/protocol/MPTIssue.h>
9#include <xrpl/protocol/STTx.h>
10#include <xrpl/protocol/TER.h>
11#include <xrpl/protocol/XRPAmount.h>
12
13#include <optional>
14#include <unordered_map>
15#include <vector>
16
17namespace xrpl {
18
19/*
20 * @brief Invariants: Vault object and MPTokenIssuance for vault shares
21 *
22 * - vault deleted and vault created is empty
23 * - vault created must be linked to pseudo-account for shares and assets
24 * - vault must have MPTokenIssuance for shares
25 * - vault without shares outstanding must have no shares
26 * - loss unrealized does not exceed the difference between assets total and
27 * assets available
28 * - assets available do not exceed assets total
29 * - vault deposit increases assets and share issuance, and adds to:
30 * total assets, assets available, shares outstanding
31 * - vault withdrawal and clawback reduce assets and share issuance, and
32 * subtracts from: total assets, assets available, shares outstanding
33 * - vault set must not alter the vault assets or shares balance
34 * - no vault transaction can change loss unrealized (it's updated by loan
35 * transactions)
36 *
37 */
39{
40 static constexpr Number kZero{};
41
56
57 struct Shares final
58 {
62
63 Shares static make(SLE const&);
64 };
65
66public:
67 struct DeltaInfo final
68 {
71
72 // Compute the delta between two Numbers, taking the coarsest scale
73 [[nodiscard]] static DeltaInfo
74 makeDelta(Number const& before, Number const& after, Asset const& asset);
75 };
76
77private:
83
96 [[nodiscard]] std::int32_t
97 computeVaultMinScale(DeltaInfo const& vaultDelta, Rules const& rules) const;
98
109 [[nodiscard]] std::optional<DeltaInfo>
110 deltaAssets(AccountID const& id) const;
111
125 [[nodiscard]] std::optional<DeltaInfo>
126 deltaAssetsTxAccount(STTx const& tx, XRPAmount fee) const;
127
138 [[nodiscard]] std::optional<DeltaInfo>
139 deltaShares(AccountID const& id) const;
140
148 [[nodiscard]] static bool
149 isVaultEmpty(Vault const& vault);
150
151public:
152 // Compute the coarsest scale required to represent all numbers
153 [[nodiscard]] static std::int32_t
155
156 void
158
159 bool
160 finalize(STTx const&, TER const, XRPAmount const, ReadView const&, beast::Journal const&);
161};
162
163} // namespace xrpl
A generic endpoint for log messages.
Definition Journal.h:38
Number is a floating point type that can represent a wide range of values.
Definition Number.h:306
A view into a ledger.
Definition ReadView.h:31
Rules controlling protocol behavior.
Definition Rules.h:33
std::shared_ptr< STLedgerEntry const > const & const_ref
std::unordered_map< uint256, DeltaInfo > deltas_
std::vector< Shares > afterMPTs_
static bool isVaultEmpty(Vault const &vault)
Check whether a vault holds no assets.
std::vector< Vault > afterVault_
std::optional< DeltaInfo > deltaShares(AccountID const &id) const
Return the vault-share balance-change delta for an account.
std::vector< Shares > beforeMPTs_
void visitEntry(bool, SLE::const_ref, SLE::const_ref)
static std::int32_t computeCoarsestScale(std::vector< DeltaInfo > const &numbers)
std::optional< DeltaInfo > deltaAssetsTxAccount(STTx const &tx, XRPAmount fee) const
Return the vault-asset delta for the transaction's sending account, adjusted for the fee.
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.
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:612
BaseUInt< 192 > uint192
Definition base_uint.h:563
STLedgerEntry SLE
bool after(NetClock::time_point now, std::uint32_t mark)
Has the specified time passed?
Definition View.cpp:554
BaseUInt< 160, detail::AccountIDTag > AccountID
A 160-bit unsigned that uniquely identifies an account.
Definition AccountID.h:28
TERSubset< CanCvtToTER > TER
Definition TER.h:634
BaseUInt< 256 > uint256
Definition base_uint.h:562
static DeltaInfo makeDelta(Number const &before, Number const &after, Asset const &asset)
std::optional< int > scale
static Shares make(SLE const &)
static Vault make(SLE const &)