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/ledger/ReadView.h>
11#include <xrpl/protocol/AccountID.h>
12#include <xrpl/protocol/Feature.h>
13#include <xrpl/protocol/LedgerFormats.h>
14#include <xrpl/protocol/Rules.h>
15#include <xrpl/protocol/SField.h>
16#include <xrpl/protocol/STAmount.h>
17#include <xrpl/protocol/STTx.h>
18#include <xrpl/protocol/TER.h>
19#include <xrpl/protocol/TxFlags.h>
20#include <xrpl/protocol/TxMeta.h>
21#include <xrpl/protocol/XRPAmount.h>
22#include <xrpl/protocol_autogen/TransactionBuilderBase.h>
23#include <xrpl/protocol_autogen/ledger_entries/AccountRoot.h>
24#include <xrpl/tx/applySteps.h>
25
26#include <helpers/Account.h>
27#include <helpers/IOU.h>
28#include <helpers/TestServiceRegistry.h>
29
30#include <cmath>
31#include <concepts>
32#include <cstdint>
33#include <memory>
34#include <optional>
35#include <stdexcept>
36#include <type_traits>
37#include <unordered_set>
38#include <vector>
39
40namespace xrpl::test {
41
42//------------------------------------------------------------------------------
43// Amount helpers
44//------------------------------------------------------------------------------
45
51template <std::integral T>
52constexpr XRPAmount
53XRP(T xrp) // NOLINT(readability-identifier-naming)
54{
55 return XRPAmount{static_cast<std::int64_t>(xrp) * kDropsPerXrp.drops()};
56}
57
63template <std::floating_point T>
65XRP(T xrp)
66{
67 return XRPAmount{static_cast<std::int64_t>(std::round(xrp * kDropsPerXrp.drops()))};
68}
69
75inline XRPAmount
76XRP(Number const& xrp)
77{
78 return XRPAmount{static_cast<std::int64_t>(xrp * kDropsPerXrp.drops())};
79}
80
81//------------------------------------------------------------------------------
82// Flag helpers
83//------------------------------------------------------------------------------
84
98constexpr std::uint32_t
100{
101 switch (asf)
102 {
103 case asfRequireDest:
104 return lsfRequireDestTag;
105 case asfRequireAuth:
106 return lsfRequireAuth;
107 case asfDisallowXRP:
108 return lsfDisallowXRP;
109 case asfDisableMaster:
110 return lsfDisableMaster;
111 case asfNoFreeze:
112 return lsfNoFreeze;
113 case asfGlobalFreeze:
114 return lsfGlobalFreeze;
115 case asfDefaultRipple:
116 return lsfDefaultRipple;
117 case asfDepositAuth:
118 return lsfDepositAuth;
119 case asfAllowTrustLineClawback:
120 return lsfAllowTrustLineClawback;
121 case asfDisallowIncomingCheck:
122 return lsfDisallowIncomingCheck;
123 case asfDisallowIncomingNFTokenOffer:
124 return lsfDisallowIncomingNFTokenOffer;
125 case asfDisallowIncomingPayChan:
126 return lsfDisallowIncomingPayChan;
127 case asfDisallowIncomingTrustline:
128 return lsfDisallowIncomingTrustline;
129 case asfAllowTrustLineLocking:
130 return lsfAllowTrustLineLocking;
131 default:
132 throw std::runtime_error("Unknown asf flag");
133 }
134}
135
136//------------------------------------------------------------------------------
137// Feature helpers
138//------------------------------------------------------------------------------
139
146
147//------------------------------------------------------------------------------
148// TxResult
149//------------------------------------------------------------------------------
150
165
185{
186public:
195 explicit TxTest(std::optional<FeatureBitset> features = std::nullopt);
196
202 [[nodiscard]] bool
203 isEnabled(uint256 const& feature) const;
204
209 [[nodiscard]] Rules const&
210 getRules() const;
211
223 template <typename T>
224 requires std::
225 derived_from<std::decay_t<T>, transactions::TransactionBuilderBase<std::decay_t<T>>>
226 [[nodiscard]] TxResult
227 submit(T&& builder, Account const& signer)
228 {
229 auto const& obj = builder.getSTObject();
230 auto accountId = obj[sfAccount];
231 // Only set sequence if not using a ticket (ticket sets sequence to 0)
232 if (!obj.isFieldPresent(sfTicketSequence))
233 {
234 builder.setSequence(getAccountRoot(accountId).getSequence());
235 }
236 else
237 {
238 builder.setSequence(0);
239 }
240 builder.setFee(XRPAmount(10));
241 return submit(builder.build(signer.pk(), signer.sk()).getSTTx());
242 }
243
256 [[nodiscard]] TxResult
258
271 void
272 createAccount(Account const& account, XRPAmount xrp, uint32_t accountFlags = 0);
273
281 [[nodiscard]] ledger_entries::AccountRoot
282 getAccountRoot(AccountID const& id) const;
283
288 [[nodiscard]] OpenView&
290
295 [[nodiscard]] OpenView const&
296 getOpenLedger() const;
297
302 [[nodiscard]] ReadView const&
303 getClosedLedger() const;
304
311 void
312 close();
313
322 void
324
329 [[nodiscard]] NetClock::time_point
330 getCloseTime() const;
331
343 [[nodiscard]] STAmount
344 getBalance(AccountID const& account, IOU const& iou) const;
345
352 {
353 return registry_;
354 }
355
356private:
362
367
372};
373
374} // 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
A view into a ledger.
Definition ReadView.h:41
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:62
void createAccount(Account const &account, XRPAmount xrp, uint32_t accountFlags=0)
Create a new account in the ledger.
Definition TxTest.cpp:125
std::unordered_set< uint256, beast::Uhash<> > featureSet_
Definition TxTest.h:358
NetClock::time_point getCloseTime() const
Get the current ledger close time.
Definition TxTest.cpp:228
std::shared_ptr< Ledger const > closedLedger_
Definition TxTest.h:360
OpenView & getOpenLedger()
Get the current open ledger view.
Definition TxTest.cpp:159
NetClock::time_point now_
Current time (can be advanced arbitrarily for testing).
Definition TxTest.h:371
ReadView const & getClosedLedger() const
Get the closed (base) ledger view.
Definition TxTest.cpp:171
TestServiceRegistry registry_
Definition TxTest.h:357
bool isEnabled(uint256 const &feature) const
Check if a feature is enabled.
Definition TxTest.cpp:94
Rules const & getRules() const
Get the current rules.
Definition TxTest.cpp:101
void close()
Close the current ledger.
Definition TxTest.cpp:177
void advanceTime(NetClock::duration duration)
Advance time without closing the ledger.
Definition TxTest.cpp:222
std::vector< std::shared_ptr< STTx const > > pendingTxs_
Transactions submitted to the open ledger, for canonical reordering on close.
Definition TxTest.h:366
std::optional< Rules > rules_
Definition TxTest.h:359
STAmount getBalance(AccountID const &account, IOU const &iou) const
Get the balance of an IOU for an account.
Definition TxTest.cpp:234
std::shared_ptr< OpenView > openLedger_
Definition TxTest.h:361
ledger_entries::AccountRoot getAccountRoot(AccountID const &id) const
Get the account root object from the current open ledger.
Definition TxTest.cpp:150
TxResult submit(T &&builder, Account const &signer)
Submit a transaction from a builder.
Definition TxTest.h:227
ServiceRegistry & getServiceRegistry()
Get the service registry.
Definition TxTest.h:351
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:42
constexpr XRPAmount
Convert XRP to drops (integral types).
Definition TxTest.h:54
constexpr std::uint32_t asfToLsf(std::uint32_t asf)
Convert AccountSet flag (asf) to LedgerState flag (lsf).
Definition TxTest.h:99
constexpr XRPAmount kDropsPerXrp
Number of drops per 1 XRP.
Definition XRPAmount.h:254
BaseUInt< 160, detail::AccountIDTag > AccountID
A 160-bit unsigned that uniquely identifies an account.
Definition AccountID.h:34
TERSubset< CanCvtToTER > TER
Definition TER.h:647
BaseUInt< 256 > uint256
Definition base_uint.h:580
T round(T... args)
Result of a transaction submission in TxTest.
Definition TxTest.h:159
TER ter
The transaction engine result code.
Definition TxTest.h:160
bool applied
Whether the transaction was applied to the ledger.
Definition TxTest.h:161
std::shared_ptr< STTx const > tx
Pointer to the submitted transaction.
Definition TxTest.h:163
std::optional< TxMeta > metadata
Transaction metadata, if available.
Definition TxTest.h:162