xrpld
Loading...
Searching...
No Matches
TxTest.h
1#pragma once
2
3#include <xrpl/basics/Number.h>
4#include <xrpl/basics/chrono.h>
5#include <xrpl/beast/utility/Journal.h>
6#include <xrpl/core/ServiceRegistry.h>
7#include <xrpl/ledger/ApplyViewImpl.h>
8#include <xrpl/ledger/Ledger.h>
9#include <xrpl/ledger/OpenView.h>
10#include <xrpl/protocol/Feature.h>
11#include <xrpl/protocol/LedgerFormats.h>
12#include <xrpl/protocol/Rules.h>
13#include <xrpl/protocol/STTx.h>
14#include <xrpl/protocol/TER.h>
15#include <xrpl/protocol/TxFlags.h>
16#include <xrpl/protocol/XRPAmount.h>
17#include <xrpl/protocol_autogen/TransactionBuilderBase.h>
18#include <xrpl/protocol_autogen/ledger_entries/AccountRoot.h>
19#include <xrpl/tx/applySteps.h>
20
21#include <helpers/Account.h>
22#include <helpers/IOU.h>
23#include <helpers/TestServiceRegistry.h>
24
25#include <cmath>
26#include <concepts>
27#include <memory>
28#include <optional>
29#include <stdexcept>
30#include <type_traits>
31#include <unordered_set>
32#include <vector>
33
34namespace xrpl::test {
35
36//------------------------------------------------------------------------------
37// Amount helpers
38//------------------------------------------------------------------------------
39
45template <std::integral T>
46constexpr XRPAmount
47XRP(T xrp) // NOLINT(readability-identifier-naming)
48{
49 return XRPAmount{static_cast<std::int64_t>(xrp) * kDropsPerXrp.drops()};
50}
51
57template <std::floating_point T>
59XRP(T xrp)
60{
61 return XRPAmount{static_cast<std::int64_t>(std::round(xrp * kDropsPerXrp.drops()))};
62}
63
69inline XRPAmount
70XRP(Number const& xrp)
71{
72 return XRPAmount{static_cast<std::int64_t>(xrp * kDropsPerXrp.drops())};
73}
74
75//------------------------------------------------------------------------------
76// Flag helpers
77//------------------------------------------------------------------------------
78
92constexpr std::uint32_t
94{
95 switch (asf)
96 {
97 case asfRequireDest:
98 return lsfRequireDestTag;
99 case asfRequireAuth:
100 return lsfRequireAuth;
101 case asfDisallowXRP:
102 return lsfDisallowXRP;
103 case asfDisableMaster:
104 return lsfDisableMaster;
105 case asfNoFreeze:
106 return lsfNoFreeze;
107 case asfGlobalFreeze:
108 return lsfGlobalFreeze;
109 case asfDefaultRipple:
110 return lsfDefaultRipple;
111 case asfDepositAuth:
112 return lsfDepositAuth;
113 case asfAllowTrustLineClawback:
114 return lsfAllowTrustLineClawback;
115 case asfDisallowIncomingCheck:
116 return lsfDisallowIncomingCheck;
117 case asfDisallowIncomingNFTokenOffer:
118 return lsfDisallowIncomingNFTokenOffer;
119 case asfDisallowIncomingPayChan:
120 return lsfDisallowIncomingPayChan;
121 case asfDisallowIncomingTrustline:
122 return lsfDisallowIncomingTrustline;
123 case asfAllowTrustLineLocking:
124 return lsfAllowTrustLineLocking;
125 default:
126 throw std::runtime_error("Unknown asf flag");
127 }
128}
129
130//------------------------------------------------------------------------------
131// Feature helpers
132//------------------------------------------------------------------------------
133
140
141//------------------------------------------------------------------------------
142// TxResult
143//------------------------------------------------------------------------------
144
159
179{
180public:
189 explicit TxTest(std::optional<FeatureBitset> features = std::nullopt);
190
196 [[nodiscard]] bool
197 isEnabled(uint256 const& feature) const;
198
203 [[nodiscard]] Rules const&
204 getRules() const;
205
217 template <typename T>
218 requires std::
219 derived_from<std::decay_t<T>, transactions::TransactionBuilderBase<std::decay_t<T>>>
220 [[nodiscard]] TxResult
221 submit(T&& builder, Account const& signer)
222 {
223 auto const& obj = builder.getSTObject();
224 auto accountId = obj[sfAccount];
225 // Only set sequence if not using a ticket (ticket sets sequence to 0)
226 if (!obj.isFieldPresent(sfTicketSequence))
227 {
228 builder.setSequence(getAccountRoot(accountId).getSequence());
229 }
230 else
231 {
232 builder.setSequence(0);
233 }
234 builder.setFee(XRPAmount(10));
235 return submit(builder.build(signer.pk(), signer.sk()).getSTTx());
236 }
237
250 [[nodiscard]] TxResult
252
265 void
266 createAccount(Account const& account, XRPAmount xrp, uint32_t accountFlags = 0);
267
275 [[nodiscard]] ledger_entries::AccountRoot
276 getAccountRoot(AccountID const& id) const;
277
282 [[nodiscard]] OpenView&
284
289 [[nodiscard]] OpenView const&
290 getOpenLedger() const;
291
296 [[nodiscard]] ReadView const&
297 getClosedLedger() const;
298
305 void
306 close();
307
316 void
318
323 [[nodiscard]] NetClock::time_point
324 getCloseTime() const;
325
337 [[nodiscard]] STAmount
338 getBalance(AccountID const& account, IOU const& iou) const;
339
346 {
347 return registry_;
348 }
349
350private:
356
359
362};
363
364} // namespace xrpl::test
std::chrono::time_point< NetClock > time_point
Definition chrono.h:46
std::chrono::duration< rep, period > duration
Definition chrono.h:45
Number is a floating point type that can represent a wide range of values.
Definition Number.h:306
Writable ledger view that accumulates state and tx changes.
Definition OpenView.h:45
A view into a ledger.
Definition ReadView.h:31
Rules controlling protocol behavior.
Definition Rules.h:33
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:123
std::unordered_set< uint256, beast::Uhash<> > featureSet_
Definition TxTest.h:352
NetClock::time_point getCloseTime() const
Get the current ledger close time.
Definition TxTest.cpp:226
std::shared_ptr< Ledger const > closedLedger_
Definition TxTest.h:354
OpenView & getOpenLedger()
Get the current open ledger view.
Definition TxTest.cpp:157
NetClock::time_point now_
Current time (can be advanced arbitrarily for testing).
Definition TxTest.h:361
ReadView const & getClosedLedger() const
Get the closed (base) ledger view.
Definition TxTest.cpp:169
TestServiceRegistry registry_
Definition TxTest.h:351
bool isEnabled(uint256 const &feature) const
Check if a feature is enabled.
Definition TxTest.cpp:92
Rules const & getRules() const
Get the current rules.
Definition TxTest.cpp:99
void close()
Close the current ledger.
Definition TxTest.cpp:175
void advanceTime(NetClock::duration duration)
Advance time without closing the ledger.
Definition TxTest.cpp:220
std::vector< std::shared_ptr< STTx const > > pendingTxs_
Transactions submitted to the open ledger, for canonical reordering on close.
Definition TxTest.h:358
std::optional< Rules > rules_
Definition TxTest.h:353
STAmount getBalance(AccountID const &account, IOU const &iou) const
Get the balance of an IOU for an account.
Definition TxTest.cpp:232
std::shared_ptr< OpenView > openLedger_
Definition TxTest.h:355
ledger_entries::AccountRoot getAccountRoot(AccountID const &id) const
Get the account root object from the current open ledger.
Definition TxTest.cpp:148
TxResult submit(T &&builder, Account const &signer)
Submit a transaction from a builder.
Definition TxTest.h:221
ServiceRegistry & getServiceRegistry()
Get the service registry.
Definition TxTest.h:345
Immutable cryptographic account descriptor.
Definition jtx/Account.h:17
SecretKey const & sk() const
Return the secret key.
Definition jtx/Account.h:75
PublicKey const & pk() const
Return the public key.
Definition jtx/Account.h:68
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:48
constexpr std::uint32_t asfToLsf(std::uint32_t asf)
Convert AccountSet flag (asf) to LedgerState flag (lsf).
Definition TxTest.h:93
constexpr XRPAmount kDropsPerXrp
Number of drops per 1 XRP.
Definition XRPAmount.h:240
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
T round(T... args)
Result of a transaction submission in TxTest.
Definition TxTest.h:153
TER ter
The transaction engine result code.
Definition TxTest.h:154
bool applied
Whether the transaction was applied to the ledger.
Definition TxTest.h:155
std::shared_ptr< STTx const > tx
Pointer to the submitted transaction.
Definition TxTest.h:157
std::optional< TxMeta > metadata
Transaction metadata, if available.
Definition TxTest.h:156