xrpld
Loading...
Searching...
No Matches
LedgerMaster_test.cpp
1#include <test/jtx/Account.h>
2#include <test/jtx/Env.h>
3#include <test/jtx/amount.h>
4#include <test/jtx/envconfig.h>
5#include <test/jtx/noop.h>
6
7#include <xrpld/app/ledger/LedgerMaster.h>
8#include <xrpld/core/Config.h>
9
10#include <xrpl/basics/base_uint.h>
11#include <xrpl/beast/unit_test/suite.h>
12#include <xrpl/protocol/Feature.h>
13#include <xrpl/protocol/SField.h>
14#include <xrpl/protocol/STObject.h>
15#include <xrpl/protocol/STTx.h>
16
17#include <cstdint>
18#include <memory>
19#include <vector>
20
21namespace xrpl::test {
22
24{
26 makeNetworkConfig(uint32_t networkID)
27 {
28 using namespace jtx;
29 return envconfig([&](std::unique_ptr<Config> cfg) {
30 cfg->networkId = networkID;
31 // This test relies on ledger hash so must lock it to fee 10.
32 cfg->fees.referenceFee = 10;
33 return cfg;
34 });
35 }
36
37 void
39 {
40 testcase("tx_id_from_index");
41
42 using namespace test::jtx;
43 using namespace std::literals;
44
45 test::jtx::Env env{*this, makeNetworkConfig(11111)};
46
47 auto const alice = Account("alice");
48 env.fund(XRP(1000), alice);
49 env.close();
50
51 // build ledgers
54 auto const startLegSeq = env.current()->header().seq;
55 for (int i = 0; i < 2; ++i)
56 {
57 env(noop(alice));
58 txns.emplace_back(env.tx());
59 env.close();
60 metas.emplace_back(env.closed()->txRead(env.tx()->getTransactionID()).second);
61 }
62 // add last (empty) ledger
63 env.close();
64 auto const endLegSeq = env.closed()->header().seq;
65
66 // test invalid range
67 {
68 std::uint32_t const ledgerSeq = -1;
69 std::uint32_t const txnIndex = 0;
70 auto result = env.app().getLedgerMaster().txnIdFromIndex(ledgerSeq, txnIndex);
71 BEAST_EXPECT(!result);
72 }
73 // test not in ledger
74 {
75 uint32_t const txnIndex = metas[0]->getFieldU32(sfTransactionIndex);
76 auto result = env.app().getLedgerMaster().txnIdFromIndex(0, txnIndex);
77 BEAST_EXPECT(!result);
78 }
79 // test empty ledger
80 {
81 auto result = env.app().getLedgerMaster().txnIdFromIndex(endLegSeq, 0);
82 BEAST_EXPECT(!result);
83 }
84 // ended without result
85 {
86 uint32_t const txnIndex = metas[0]->getFieldU32(sfTransactionIndex);
87 auto result = env.app().getLedgerMaster().txnIdFromIndex(endLegSeq + 1, txnIndex);
88 BEAST_EXPECT(!result);
89 }
90 // success (first tx)
91 {
92 uint32_t const txnIndex = metas[0]->getFieldU32(sfTransactionIndex);
93 auto result = env.app().getLedgerMaster().txnIdFromIndex(startLegSeq, txnIndex);
94 BEAST_EXPECT(
95 // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
96 *result ==
97 uint256(
98 "277F4FD89C20B92457FEF05FF63F6405563AD0563C73D967A29727"
99 "72679ADC65"));
100 }
101 // success (second tx)
102 {
103 uint32_t const txnIndex = metas[1]->getFieldU32(sfTransactionIndex);
104 auto result = env.app().getLedgerMaster().txnIdFromIndex(startLegSeq + 1, txnIndex);
105 BEAST_EXPECT(
106 // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
107 *result ==
108 uint256(
109 "293DF7335EBBAF4420D52E70ABF470EB4C5792CAEA2F91F76193C2"
110 "819F538FDE"));
111 }
112 }
113
114public:
115 void
116 run() override
117 {
118 using namespace test::jtx;
120 testWithFeats(all);
121 }
122
123 void
125 {
126 testTxnIdFromIndex(features);
127 }
128};
129
131
132} // namespace xrpl::test
A testsuite class.
Definition suite.h:50
TestcaseT testcase
Memberspace for declaring test cases.
Definition suite.h:149
std::optional< uint256 > txnIdFromIndex(uint32_t ledgerSeq, uint32_t txnIndex)
virtual LedgerMaster & getLedgerMaster()=0
void testWithFeats(FeatureBitset features)
void testTxnIdFromIndex(FeatureBitset features)
void run() override
Runs the suite.
static std::unique_ptr< Config > makeNetworkConfig(uint32_t networkID)
A transaction testing environment.
Definition Env.h:143
Application & app()
Definition Env.h:280
bool close(NetClock::time_point closeTime, std::optional< std::chrono::milliseconds > consensusDelay=std::nullopt)
Close and advance the ledger.
Definition Env.cpp:133
std::shared_ptr< ReadView const > closed()
Returns the last closed ledger.
Definition Env.cpp:127
void fund(bool setDefaultRipple, STAmount const &amount, Account const &account)
Definition Env.cpp:296
std::shared_ptr< STTx const > tx() const
Return the tx data for the last JTx.
Definition Env.cpp:533
std::shared_ptr< OpenView const > current() const
Returns the current ledger.
Definition Env.h:353
T emplace_back(T... args)
XrpT const XRP
Converts to XRP Issue or STAmount.
Definition amount.cpp:92
json::Value noop(Account const &account)
The null transaction.
Definition noop.h:9
FeatureBitset testableAmendments()
Definition Env.h:76
std::unique_ptr< Config > envconfig()
creates and initializes a default configuration for jtx::Env
Definition envconfig.h:28
BEAST_DEFINE_TESTSUITE(AMMClawback, app, xrpl)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
BaseUInt< 256 > uint256
Definition base_uint.h:562