feat: Move VaultSet invariants from ValidVault to VaultSet

This commit is contained in:
Vito
2026-06-09 15:39:12 +02:00
parent d0a54d1159
commit 65724aeac1
5 changed files with 330 additions and 61 deletions

View File

@@ -7,7 +7,9 @@
#include <xrpl/protocol/MPTIssue.h>
#include <xrpl/protocol/STLedgerEntry.h>
#include <cstdint>
#include <optional>
#include <unordered_map>
#include <vector>
namespace xrpl {
@@ -15,9 +17,8 @@ namespace xrpl {
/**
* @brief Collects vault and share-issuance snapshots from ledger entry visits.
*
* Used by per-transaction invariant checks (e.g. VaultCreate) that need
* vault and MPTokenIssuance state without the full balance-delta tracking
* that ValidVault maintains.
* Used by per-transaction invariant checks (e.g. VaultCreate, VaultSet) that
* need vault and MPTokenIssuance state, optionally with balance-delta tracking.
*/
class VaultInvariantData
{
@@ -48,6 +49,24 @@ public:
make(SLE const&);
};
/**
* @brief Balance-change delta for a single ledger entry.
*
* Mirrors ValidVault::DeltaInfo. @c scale carries the STAmount exponent
* so that callers can round to the coarsest representable precision.
*/
struct DeltaInfo final
{
Number delta = kNumZero;
std::optional<int> scale;
/**
* @brief Compute the delta between two Numbers at the coarsest scale.
*/
[[nodiscard]] static DeltaInfo
makeDelta(Number const& before, Number const& after, Asset const& asset);
};
void
visitEntry(bool isDelete, SLE::const_ref before, SLE::const_ref after);
@@ -67,10 +86,47 @@ public:
[[nodiscard]] std::optional<Shares>
findShares(uint192 const& mptID) const;
/**
* @brief Find shares in beforeMPTs_ whose mptID matches (deleted entries).
*/
[[nodiscard]] std::optional<Shares>
findDeletedShares(uint192 const& mptID) const;
/**
* @brief Access the raw vector of before-state MPTokenIssuance snapshots.
*/
[[nodiscard]] std::vector<Shares> const&
beforeMPTIssuances() const
{
return beforeMPTs_;
}
/**
* @brief Return the vault-asset balance-change delta for an account.
*
* Looks up the ledger-entry delta recorded during visitEntry for the
* account entry (XRP), trust line (IOU), or MPToken (MPT) that corresponds
* to the vault asset held by @p id.
*
* @param vaultAsset The asset held by the vault.
* @param id Account whose asset delta is requested.
* @returns The delta, or std::nullopt if the entry was not touched.
*/
[[nodiscard]] std::optional<DeltaInfo>
deltaAssets(Asset const& vaultAsset, AccountID const& id) const;
/**
* @brief Compute the coarsest scale required to represent all numbers.
*/
[[nodiscard]] static std::int32_t
computeCoarsestScale(std::vector<DeltaInfo> const& numbers);
private:
std::vector<Vault> afterVault_;
std::vector<Vault> beforeVault_;
std::vector<Shares> afterMPTs_;
std::vector<Shares> beforeMPTs_;
std::unordered_map<uint256, DeltaInfo> deltas_;
};
} // namespace xrpl

View File

@@ -1,6 +1,7 @@
#pragma once
#include <xrpl/tx/Transactor.h>
#include <xrpl/tx/invariants/VaultInvariantData.h>
namespace xrpl {
@@ -35,6 +36,9 @@ public:
XRPAmount fee,
ReadView const& view,
beast::Journal const& j) override;
private:
VaultInvariantData data_;
};
} // namespace xrpl