From 19900bb87eb8dabcdb056171ed0dcd7026317bba Mon Sep 17 00:00:00 2001 From: Ed Hennis Date: Mon, 31 Mar 2025 20:29:26 -0400 Subject: [PATCH] Finish creating helper classes for JTx fields - Also change the pseudo account field lookup to a function that uses a switch --- src/test/jtx/TestHelpers.h | 204 ++++++++++++++++--------- src/test/jtx/impl/TestHelpers.cpp | 18 +++ src/xrpld/app/tx/detail/Transactor.h | 4 +- src/xrpld/app/tx/detail/applySteps.cpp | 2 +- 4 files changed, 149 insertions(+), 79 deletions(-) diff --git a/src/test/jtx/TestHelpers.h b/src/test/jtx/TestHelpers.h index ac4e9f7c4a..79be616a0a 100644 --- a/src/test/jtx/TestHelpers.h +++ b/src/test/jtx/TestHelpers.h @@ -40,76 +40,150 @@ namespace jtx { Not every helper will be able to use this because of conversions and other issues, but for classes where it's straightforward, this can simplify things. */ -template -struct field +template < + class SField, + class StoredValue = SField::type::value_type, + class OutputValue = StoredValue> +struct JTxField { + using SF = SField; + using SV = StoredValue; + using OV = OutputValue; + protected: - F const& field_; - V value_; + SF const& sfield_; + SV value_; public: - explicit field(F const& field, V const& value) - : field_(field), value_(value) + explicit JTxField(SF const& sfield, SV const& value) + : sfield_(sfield), value_(value) { } - virtual JV + virtual OV value() const = 0; virtual void operator()(Env&, JTx& jt) const { - jt.jv[field_.jsonName] = value(); + jt.jv[sfield_.jsonName] = value(); } }; -template -struct field +template +struct JTxField { + using SF = SField; + using SV = StoredValue; + using OV = SV; + protected: - F const& field_; - V value_; + SF const& sfield_; + SV value_; public: - explicit field(F const& field, V const& value) - : field_(field), value_(value) + explicit JTxField(SF const& sfield, SV const& value) + : sfield_(sfield), value_(value) { } virtual void operator()(Env&, JTx& jt) const { - jt.jv[field_.jsonName] = value_; + jt.jv[sfield_.jsonName] = value_; } }; -template -using simpleField = field; - struct timePointField - : public field + : public JTxField { - using F = SF_UINT32; - using V = NetClock::time_point; - using JV = NetClock::rep; - using base = field; + using SF = SF_UINT32; + using SV = NetClock::time_point; + using OV = NetClock::rep; + using base = JTxField; protected: using base::value_; public: - explicit timePointField(F const& field, V const& value) - : field(field, value) + explicit timePointField(SF const& sfield, SV const& value) + : JTxField(sfield, value) { } - JV + OV value() const override { return value_.time_since_epoch().count(); } }; +struct uint256Field : public JTxField +{ + using SF = SF_UINT256; + using SV = uint256; + using OV = std::string; + using base = JTxField; + +protected: + using base::value_; + +public: + explicit uint256Field(SF const& sfield, SV const& value) + : JTxField(sfield, value) + { + } + + OV + value() const override + { + return to_string(value_); + } +}; + +struct blobField : public JTxField +{ + using SF = SF_VL; + using SV = std::string; + using base = JTxField; + + explicit blobField(SF const& sfield, Slice const& cond) + : JTxField(sfield, strHex(cond)) + { + } + + template + explicit blobField(SF const& sfield, std::array const& c) + : blobField(sfield, makeSlice(c)) + { + } +}; + +template +struct JTxFieldWrapper +{ + using JF = JTxField; + using SF = JF::SF; + using SV = JF::SV; + +protected: + SF const& sfield_; + +public: + explicit JTxFieldWrapper(SF const& sfield) : sfield_(sfield) + { + } + + JTxField const& + operator()(SV const& value) const + { + return JTxField(sfield_, value); + } +}; + +template +using simpleField = JTxFieldWrapper>; + // TODO We only need this long "requires" clause as polyfill, for C++20 // implementations which are missing header. Replace with // `std::ranges::range`, and accordingly use std::ranges::begin/end @@ -339,58 +413,14 @@ std::array constexpr cb1 = { std::array const fb1 = {{0xA0, 0x02, 0x80, 0x00}}; /** Set the "FinishAfter" time tag on a JTx */ -struct finish_time : public timePointField -{ - explicit finish_time(NetClock::time_point const& value) - : timePointField(sfFinishAfter, value) - { - } -}; +auto const finish_time = JTxFieldWrapper(sfFinishAfter); /** Set the "CancelAfter" time tag on a JTx */ -struct cancel_time : public timePointField -{ - explicit cancel_time(NetClock::time_point const& value) - : timePointField(sfCancelAfter, value) - { - } -}; +auto const cancel_time = JTxFieldWrapper(sfCancelAfter); -struct condition : public simpleField -{ - explicit condition(Slice const& cond) : field(sfCondition, strHex(cond)) - { - } +auto const condition = JTxFieldWrapper(sfCondition); - template - explicit condition(std::array const& c) - : condition(makeSlice(c)) - { - } -}; - -struct fulfillment -{ -private: - std::string value_; - -public: - explicit fulfillment(Slice condition) : value_(strHex(condition)) - { - } - - template - explicit fulfillment(std::array f) - : fulfillment(makeSlice(f)) - { - } - - void - operator()(Env&, JTx& jt) const - { - jt.jv[sfFulfillment.jsonName] = value_; - } -}; +auto const fulfllment = JTxFieldWrapper(sfFulfillment); /* Payment Channel */ /******************************************************************************/ @@ -514,16 +544,38 @@ create( namespace loanBroker { Json::Value -create(AccountID const& account, uint256 const& vaultId); +set(AccountID const& account, uint256 const& vaultId, uint32_t flags = 0); /* inline Json::Value -create(Account const& account, uint256 const& vaultId) +set(Account const& account, uint256 const& vaultId, uint32_t flags = 0) { return create(account.id(), vaultId); } */ +auto const loanBrokerID = JTxFieldWrapper(sfLoanBrokerID); + +auto const data = JTxFieldWrapper(sfData); + +auto const managementFeeRate = simpleField(sfManagementFeeRate); + +/* +Data string BLOB None Arbitrary metadata in hex + format.The field is limited to 256 bytes.ManagementFeeRate number + UINT16 0 The 1 / + 10th basis point fee charged by the Lending Protocol Owner.Valid values + are between 0 and + 10000 inclusive + .DebtMaximum number NUMBER 0 The maximum amount the protocol can owe + the Vault + .The default value of 0 means there is no limit to the + debt.CoverRateMinimum number UINT16 0 The 1 / + 10th basis point DebtTotal that the first loss capital must + cover.Valid values are between 0 and + 100000 inclusive.CoverRateLiquidation + */ + } // namespace loanBroker } // namespace jtx diff --git a/src/test/jtx/impl/TestHelpers.cpp b/src/test/jtx/impl/TestHelpers.cpp index e5b136e9c0..8d78d0d16a 100644 --- a/src/test/jtx/impl/TestHelpers.cpp +++ b/src/test/jtx/impl/TestHelpers.cpp @@ -389,6 +389,24 @@ allpe(AccountID const& a, Issue const& iss) iss.account); }; +/* LoanBroker */ +/******************************************************************************/ + +namespace loanBroker { + +Json::Value +set(AccountID const& account, uint256 const& vaultId, uint32_t flags) +{ + Json::Value jv; + jv[sfTransactionType.jsonName] = jss::LoanBrokerSet; + jv[sfAccount.jsonName] = to_string(account); + jv[sfVaultID.jsonName] = to_string(vaultId); + jv[sfFlags.jsonName] = flags; + return jv; +} + +} + } // namespace jtx } // namespace test } // namespace ripple diff --git a/src/xrpld/app/tx/detail/Transactor.h b/src/xrpld/app/tx/detail/Transactor.h index 1bb0853516..873b7a23b1 100644 --- a/src/xrpld/app/tx/detail/Transactor.h +++ b/src/xrpld/app/tx/detail/Transactor.h @@ -270,13 +270,13 @@ Transactor::preflight(PreflightContext const& ctx) if (!T::isEnabled(ctx)) return temDISABLED; - if (auto const ret = detail::preflight1(ctx, T::getFlagsMask(ctx))) + if (auto const ret = ripple::detail::preflight1(ctx, T::getFlagsMask(ctx))) return ret; if (auto const ret = T::doPreflight(ctx)) return ret; - return detail::preflight2(ctx); + return ripple::detail::preflight2(ctx); } template diff --git a/src/xrpld/app/tx/detail/applySteps.cpp b/src/xrpld/app/tx/detail/applySteps.cpp index d222220d9b..a8b5c4e2ab 100644 --- a/src/xrpld/app/tx/detail/applySteps.cpp +++ b/src/xrpld/app/tx/detail/applySteps.cpp @@ -41,7 +41,7 @@ #include #include #include -#include +#include #include #include #include