xrpld
Loading...
Searching...
No Matches
TxTest.h
1#pragma once
2
3#include <xrpl/basics/Number.h>
4#include <xrpl/basics/base_uint.h>
5#include <xrpl/basics/chrono.h>
6#include <xrpl/beast/hash/uhash.h>
7#include <xrpl/core/ServiceRegistry.h>
8#include <xrpl/ledger/Ledger.h>
9#include <xrpl/ledger/OpenView.h>
10#include <xrpl/protocol/AccountID.h>
11#include <xrpl/protocol/Feature.h>
12#include <xrpl/protocol/LedgerFormats.h>
13#include <xrpl/protocol/Rules.h>
14#include <xrpl/protocol/SField.h>
15#include <xrpl/protocol/STAmount.h>
16#include <xrpl/protocol/STTx.h>
17#include <xrpl/protocol/TER.h>
18#include <xrpl/protocol/TxFlags.h>
19#include <xrpl/protocol/TxMeta.h>
20#include <xrpl/protocol/XRPAmount.h>
21#include <xrpl/protocol_autogen/TransactionBuilderBase.h>
22#include <xrpl/protocol_autogen/ledger_entries/AccountRoot.h>
23#include <xrpl/tx/applySteps.h>
24
25#include <helpers/Account.h>
26#include <helpers/IOU.h>
27#include <helpers/TestServiceRegistry.h>
28
29#include <cmath>
30#include <concepts>
31#include <cstdint>
32#include <memory>
33#include <optional>
34#include <stdexcept>
35#include <type_traits>
36#include <unordered_set>
37#include <vector>
38
39namespace xrpl::test {
40
41//------------------------------------------------------------------------------
42// Amount helpers
43//------------------------------------------------------------------------------
44
50template <std::integral T>
51constexpr XRPAmount
52XRP(T xrp) // NOLINT(readability-identifier-naming)
53{
54 return XRPAmount{static_cast<std::int64_t>(xrp) * kDropsPerXrp.drops()};
55}
56
62template <std::floating_point T>
64XRP(T xrp)
65{
66 return XRPAmount{static_cast<std::int64_t>(std::round(xrp * kDropsPerXrp.drops()))};
67}
68
74inline XRPAmount
75XRP(Number const& xrp)
76{
77 return XRPAmount{static_cast<std::int64_t>(xrp * kDropsPerXrp.drops())};
78}
79
80//------------------------------------------------------------------------------
81// Flag helpers
82//------------------------------------------------------------------------------
83
97constexpr std::uint32_t
99{
100 switch (asf)
101 {
102 case asfRequireDest:
103 return lsfRequireDestTag;
104 case asfRequireAuth:
105 return lsfRequireAuth;
106 case asfDisallowXRP:
107 return lsfDisallowXRP;
108 case asfDisableMaster:
109 return lsfDisableMaster;
110 case asfNoFreeze:
111 return lsfNoFreeze;
112 case asfGlobalFreeze:
113 return lsfGlobalFreeze;
114 case asfDefaultRipple:
115 return lsfDefaultRipple;
116 case asfDepositAuth:
117 return lsfDepositAuth;
118 case asfAllowTrustLineClawback:
119 return lsfAllowTrustLineClawback;
120 case asfDisallowIncomingCheck:
121 return lsfDisallowIncomingCheck;
122 case asfDisallowIncomingNFTokenOffer:
123 return lsfDisallowIncomingNFTokenOffer;
124 case asfDisallowIncomingPayChan:
125 return lsfDisallowIncomingPayChan;
126 case asfDisallowIncomingTrustline:
127 return lsfDisallowIncomingTrustline;
128 case asfAllowTrustLineLocking:
129 return lsfAllowTrustLineLocking;
130 default:
131 throw std::runtime_error("Unknown asf flag");
132 }
133}
134
135//------------------------------------------------------------------------------
136// Feature helpers
137//------------------------------------------------------------------------------
138
145
146//------------------------------------------------------------------------------
147// TxResult
148//------------------------------------------------------------------------------
149
164
184{
185public:
194 explicit TxTest(std::optional<FeatureBitset> features = std::nullopt);
195
201 [[nodiscard]] bool
202 isEnabled(UInt256 const& feature) const;
203
208 [[nodiscard]] Rules const&
209 getRules() const;
210
222 template <typename T>
223 requires std::
224 derived_from<std::decay_t<T>, transactions::TransactionBuilderBase<std::decay_t<T>>>
225 [[nodiscard]] TxResult
226 submit(T&& builder, Account const& signer)
227 {
228 auto const& obj = builder.getSTObject();
229 auto const accountId = obj[sfAccount];
230 // Only set sequence if not using a ticket (ticket sets sequence to 0)
231 if (!obj.isFieldPresent(sfTicketSequence))
232 {
233 builder.setSequence(getAccountRoot(accountId).getSequence());
234 }
235 else
236 {
237 builder.setSequence(0);
238 }
239 builder.setFee(XRPAmount(10));
240 return submit(builder.build(signer.pk(), signer.sk()).getSTTx());
241 }
242
255 [[nodiscard]] TxResult
257
270 void
271 createAccount(Account const& account, XRPAmount xrp, uint32_t accountFlags = 0);
272
280 [[nodiscard]] ledger_entries::AccountRoot
281 getAccountRoot(AccountID const& id) const;
282
287 [[nodiscard]] OpenView&
289
294 [[nodiscard]] OpenView const&
295 getOpenLedger() const;
296
301 [[nodiscard]] Ledger const&
302 getClosedLedger() const;
303
310 void
311 close();
312
321 void
323
328 [[nodiscard]] NetClock::time_point
329 getCloseTime() const;
330
342 [[nodiscard]] STAmount
343 getBalance(AccountID const& account, IOU const& iou) const;
344
351 {
352 return registry_;
353 }
354
355private:
361
366
371};
372
373} // namespace xrpl::test
std::chrono::time_point< NetClock > time_point
Definition chrono.h:48
std::chrono::duration< rep, period > duration
Definition chrono.h:47
Number is a floating point type that can represent a wide range of values.
Definition Number.h:351
Writable ledger view that accumulates state and tx changes.
Definition OpenView.h:59
Rules controlling protocol behavior.
Definition Rules.h:40
Service registry for dependency injection.
Ledger Entry: AccountRoot.
Definition AccountRoot.h:28
Test implementation of ServiceRegistry for unit tests.
TxTest(std::optional< FeatureBitset > features=std::nullopt)
Construct a TxTest environment.
Definition TxTest.cpp:61
void createAccount(Account const &account, XRPAmount xrp, uint32_t accountFlags=0)
Create a new account in the ledger.
Definition TxTest.cpp:131
NetClock::time_point getCloseTime() const
Get the current ledger close time.
Definition TxTest.cpp:238
std::shared_ptr< Ledger const > closedLedger_
Definition TxTest.h:359
OpenView & getOpenLedger()
Get the current open ledger view.
Definition TxTest.cpp:165
bool isEnabled(UInt256 const &feature) const
Check if a feature is enabled.
Definition TxTest.cpp:99
NetClock::time_point now_
Current time (can be advanced arbitrarily for testing).
Definition TxTest.h:370
std::unordered_set< UInt256, beast::Uhash<> > featureSet_
Definition TxTest.h:357
TestServiceRegistry registry_
Definition TxTest.h:356
Rules const & getRules() const
Get the current rules.
Definition TxTest.cpp:106
void close()
Close the current ledger.
Definition TxTest.cpp:183
TxResult submit(T &&builder, Account const &signer)
Submit a transaction from a builder.
Definition TxTest.h:226
void advanceTime(NetClock::duration duration)
Advance time without closing the ledger.
Definition TxTest.cpp:232
std::vector< std::shared_ptr< STTx const > > pendingTxs_
Transactions submitted to the open ledger, for canonical reordering on close.
Definition TxTest.h:365
std::optional< Rules > rules_
Definition TxTest.h:358
Ledger const & getClosedLedger() const
Get the closed (base) ledger view.
Definition TxTest.cpp:177
STAmount getBalance(AccountID const &account, IOU const &iou) const
Get the balance of an IOU for an account.
Definition TxTest.cpp:244
std::shared_ptr< OpenView > openLedger_
Definition TxTest.h:360
ledger_entries::AccountRoot getAccountRoot(AccountID const &id) const
Get the account root object from the current open ledger.
Definition TxTest.cpp:156
ServiceRegistry & getServiceRegistry()
Get the service registry.
Definition TxTest.h:350
Immutable cryptographic account descriptor.
Definition jtx/Account.h:21
SecretKey const & sk() const
Return the secret key.
Definition jtx/Account.h:93
PublicKey const & pk() const
Return the public key.
Definition jtx/Account.h:84
Converts to IOU Issue or STAmount.
Base class for all transaction builders.
XrpT const XRP
Converts to XRP Issue or STAmount.
Definition amount.cpp:92
FeatureBitset allFeatures()
Returns all testable amendments.
Definition TxTest.cpp:41
constexpr XRPAmount
Convert XRP to drops (integral types).
Definition TxTest.h:53
constexpr std::uint32_t asfToLsf(std::uint32_t asf)
Convert AccountSet flag (asf) to LedgerState flag (lsf).
Definition TxTest.h:98
constexpr XRPAmount kDropsPerXrp
Number of drops per 1 XRP.
Definition XRPAmount.h:247
BaseUInt< 256 > UInt256
Definition base_uint.h:581
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
T round(T... args)
Result of a transaction submission in TxTest.
Definition TxTest.h:158
TER ter
The transaction engine result code.
Definition TxTest.h:159
bool applied
Whether the transaction was applied to the ledger.
Definition TxTest.h:160
std::shared_ptr< STTx const > tx
Pointer to the submitted transaction.
Definition TxTest.h:162
std::optional< TxMeta > metadata
Transaction metadata, if available.
Definition TxTest.h:161