xrpld
Loading...
Searching...
No Matches
xrpl::test::TxTest Class Reference

A lightweight transaction testing harness. More...

#include <TxTest.h>

Collaboration diagram for xrpl::test::TxTest:

Public Member Functions

 TxTest (std::optional< FeatureBitset > features=std::nullopt)
 Construct a TxTest environment.
bool isEnabled (uint256 const &feature) const
 Check if a feature is enabled.
Rules const & getRules () const
 Get the current rules.
template<typename T>
TxResult submit (T &&builder, Account const &signer)
 Submit a transaction from a builder.
TxResult submit (std::shared_ptr< STTx const > stx)
 Submit a transaction to the open ledger.
void createAccount (Account const &account, XRPAmount xrp, uint32_t accountFlags=0)
 Create a new account in the ledger.
ledger_entries::AccountRoot getAccountRoot (AccountID const &id) const
 Get the account root object from the current open ledger.
OpenViewgetOpenLedger ()
 Get the current open ledger view.
OpenView const & getOpenLedger () const
 Get the current open ledger view (const).
ReadView const & getClosedLedger () const
 Get the closed (base) ledger view.
void close ()
 Close the current ledger.
void advanceTime (NetClock::duration duration)
 Advance time without closing the ledger.
NetClock::time_point getCloseTime () const
 Get the current ledger close time.
STAmount getBalance (AccountID const &account, IOU const &iou) const
 Get the balance of an IOU for an account.
ServiceRegistrygetServiceRegistry ()
 Get the service registry.

Private Attributes

TestServiceRegistry registry_
std::unordered_set< uint256, beast::Uhash<> > featureSet_
std::optional< Rulesrules_
std::shared_ptr< Ledger const > closedLedger_
std::shared_ptr< OpenViewopenLedger_
std::vector< std::shared_ptr< STTx const > > pendingTxs_
 Transactions submitted to the open ledger, for canonical reordering on close.
NetClock::time_point now_
 Current time (can be advanced arbitrarily for testing).

Detailed Description

A lightweight transaction testing harness.

Unlike the JTx framework which requires a full Application and RPC layer, TxTest applies transactions directly to an OpenView using the transactor pipeline (preflight -> preclaim -> doApply).

This makes it suitable for:

  • Unit testing individual transactors
  • Testing transaction validation logic
  • Fast, focused tests without full server infrastructure
TxTest env;
env.submit(paymentTx).expectSuccess();
env.submit(badTx).expectTer(tecNO_ENTRY);
TxTest(std::optional< FeatureBitset > features=std::nullopt)
Construct a TxTest environment.
Definition TxTest.cpp:62
TxResult submit(T &&builder, Account const &signer)
Submit a transaction from a builder.
Definition TxTest.h:221
@ tecNO_ENTRY
Definition TER.h:304

Definition at line 178 of file TxTest.h.

Constructor & Destructor Documentation

◆ TxTest()

xrpl::test::TxTest::TxTest ( std::optional< FeatureBitset > features = std::nullopt)
explicit

Construct a TxTest environment.

Creates a genesis ledger and an open view on top of it.

Parameters
featuresOptional set of features to enable. If not specified, uses all testable amendments.

Definition at line 62 of file TxTest.cpp.

Member Function Documentation

◆ isEnabled()

bool xrpl::test::TxTest::isEnabled ( uint256 const & feature) const
nodiscard

Check if a feature is enabled.

Parameters
featureThe feature to check.
Returns
True if the feature is enabled.

Definition at line 92 of file TxTest.cpp.

◆ getRules()

Rules const & xrpl::test::TxTest::getRules ( ) const
nodiscard

Get the current rules.

Returns
The current consensus rules.

Definition at line 99 of file TxTest.cpp.

◆ submit() [1/2]

template<typename T>
TxResult xrpl::test::TxTest::submit ( T && builder,
Account const & signer )
nodiscard

Submit a transaction from a builder.

Convenience overload that accepts transaction builders. Automatically sets sequence and fee before submission.

Template Parameters
TA type derived from TransactionBuilderBase.
Parameters
builderThe transaction builder.
signerThe account to sign with.
Returns
TxResult containing the result code, applied status, and metadata.

Definition at line 221 of file TxTest.h.

◆ submit() [2/2]

TxResult xrpl::test::TxTest::submit ( std::shared_ptr< STTx const > stx)
nodiscard

Submit a transaction to the open ledger.

Applies the transaction through the full transactor pipeline: preflight -> preclaim -> doApply -> invariant checks

Invariant checks are automatically run after doApply. If any invariant fails, the result will be tecINVARIANT_FAILED.

Parameters
stxThe transaction to submit.
Returns
TxResult containing the result code, applied status, and metadata.

Definition at line 106 of file TxTest.cpp.

◆ createAccount()

void xrpl::test::TxTest::createAccount ( Account const & account,
XRPAmount xrp,
uint32_t accountFlags = 0 )

Create a new account in the ledger.

Sends a Payment from the master account to create and fund the account. Closes the ledger after creation. If accountFlags is non-zero, submits an AccountSet transaction and closes again.

Parameters
accountThe account to create.
xrpThe initial XRP balance.
accountFlagsOptional account flags to set. Defaults to 0 (no flags).

Definition at line 123 of file TxTest.cpp.

◆ getAccountRoot()

ledger_entries::AccountRoot xrpl::test::TxTest::getAccountRoot ( AccountID const & id) const
nodiscard

Get the account root object from the current open ledger.

Parameters
idThe account ID.
Returns
The AccountRoot ledger entry.
Exceptions
std::runtime_errorif the account does not exist.

Definition at line 148 of file TxTest.cpp.

◆ getOpenLedger() [1/2]

OpenView & xrpl::test::TxTest::getOpenLedger ( )
nodiscard

Get the current open ledger view.

Returns
A mutable reference to the open ledger.

Definition at line 157 of file TxTest.cpp.

◆ getOpenLedger() [2/2]

OpenView const & xrpl::test::TxTest::getOpenLedger ( ) const
nodiscard

Get the current open ledger view (const).

Returns
A const reference to the open ledger.

Definition at line 163 of file TxTest.cpp.

◆ getClosedLedger()

ReadView const & xrpl::test::TxTest::getClosedLedger ( ) const
nodiscard

Get the closed (base) ledger view.

Returns
A const reference to the closed ledger.

Definition at line 169 of file TxTest.cpp.

◆ close()

void xrpl::test::TxTest::close ( )

Close the current ledger.

Creates a new closed ledger from the current open ledger. All pending transactions are re-applied in canonical order.

Definition at line 175 of file TxTest.cpp.

◆ advanceTime()

void xrpl::test::TxTest::advanceTime ( NetClock::duration duration)

Advance time without closing the ledger.

Useful for testing time-dependent features like escrow release times or offer expirations.

Parameters
durationThe amount of time to advance.

Definition at line 220 of file TxTest.cpp.

◆ getCloseTime()

NetClock::time_point xrpl::test::TxTest::getCloseTime ( ) const
nodiscard

Get the current ledger close time.

Returns
The current close time.

Definition at line 226 of file TxTest.cpp.

◆ getBalance()

STAmount xrpl::test::TxTest::getBalance ( AccountID const & account,
IOU const & iou ) const
nodiscard

Get the balance of an IOU for an account.

Returns the balance from the perspective of the specified account. If the trust line doesn't exist, returns zero.

Parameters
accountThe account to check.
iouThe IOU to check the balance for.
Returns
The balance as an STAmount.

Definition at line 232 of file TxTest.cpp.

◆ getServiceRegistry()

ServiceRegistry & xrpl::test::TxTest::getServiceRegistry ( )

Get the service registry.

Returns
A reference to the service registry.

Definition at line 345 of file TxTest.h.

Member Data Documentation

◆ registry_

TestServiceRegistry xrpl::test::TxTest::registry_
private

Definition at line 351 of file TxTest.h.

◆ featureSet_

std::unordered_set<uint256, beast::Uhash<> > xrpl::test::TxTest::featureSet_
private

Definition at line 352 of file TxTest.h.

◆ rules_

std::optional<Rules> xrpl::test::TxTest::rules_
private

Definition at line 353 of file TxTest.h.

◆ closedLedger_

std::shared_ptr<Ledger const> xrpl::test::TxTest::closedLedger_
private

Definition at line 354 of file TxTest.h.

◆ openLedger_

std::shared_ptr<OpenView> xrpl::test::TxTest::openLedger_
private

Definition at line 355 of file TxTest.h.

◆ pendingTxs_

std::vector<std::shared_ptr<STTx const> > xrpl::test::TxTest::pendingTxs_
private

Transactions submitted to the open ledger, for canonical reordering on close.

Definition at line 358 of file TxTest.h.

◆ now_

NetClock::time_point xrpl::test::TxTest::now_
private

Current time (can be advanced arbitrarily for testing).

Definition at line 361 of file TxTest.h.