rippled
Loading...
Searching...
No Matches
LedgerMaster_test.cpp
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2023 XRPLF
5
6 Permission to use, copy, modify, and/or distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#include <test/jtx.h>
21#include <test/jtx/Env.h>
22
23#include <xrpld/app/ledger/LedgerMaster.h>
24
25namespace ripple {
26namespace test {
27
29{
31 makeNetworkConfig(uint32_t networkID)
32 {
33 using namespace jtx;
34 return envconfig([&](std::unique_ptr<Config> cfg) {
35 cfg->NETWORK_ID = networkID;
36 return cfg;
37 });
38 }
39
40 void
42 {
43 testcase("tx_id_from_index");
44
45 using namespace test::jtx;
46 using namespace std::literals;
47
48 test::jtx::Env env{*this, makeNetworkConfig(11111)};
49
50 auto const alice = Account("alice");
51 env.fund(XRP(1000), alice);
52 env.close();
53
54 // build ledgers
57 auto const startLegSeq = env.current()->info().seq;
58 for (int i = 0; i < 2; ++i)
59 {
60 env(noop(alice));
61 txns.emplace_back(env.tx());
62 env.close();
63 metas.emplace_back(
64 env.closed()->txRead(env.tx()->getTransactionID()).second);
65 }
66 // add last (empty) ledger
67 env.close();
68 auto const endLegSeq = env.closed()->info().seq;
69
70 // test invalid range
71 {
72 std::uint32_t ledgerSeq = -1;
73 std::uint32_t txnIndex = 0;
74 auto result =
75 env.app().getLedgerMaster().txnIdFromIndex(ledgerSeq, txnIndex);
76 BEAST_EXPECT(!result);
77 }
78 // test not in ledger
79 {
80 uint32_t txnIndex = metas[0]->getFieldU32(sfTransactionIndex);
81 auto result =
82 env.app().getLedgerMaster().txnIdFromIndex(0, txnIndex);
83 BEAST_EXPECT(!result);
84 }
85 // test empty ledger
86 {
87 auto result =
88 env.app().getLedgerMaster().txnIdFromIndex(endLegSeq, 0);
89 BEAST_EXPECT(!result);
90 }
91 // ended without result
92 {
93 uint32_t txnIndex = metas[0]->getFieldU32(sfTransactionIndex);
94 auto result = env.app().getLedgerMaster().txnIdFromIndex(
95 endLegSeq + 1, txnIndex);
96 BEAST_EXPECT(!result);
97 }
98 // success (first tx)
99 {
100 uint32_t txnIndex = metas[0]->getFieldU32(sfTransactionIndex);
101 auto result = env.app().getLedgerMaster().txnIdFromIndex(
102 startLegSeq, txnIndex);
103 BEAST_EXPECT(
104 *result ==
105 uint256("277F4FD89C20B92457FEF05FF63F6405563AD0563C73D967A29727"
106 "72679ADC65"));
107 }
108 // success (second tx)
109 {
110 uint32_t txnIndex = metas[1]->getFieldU32(sfTransactionIndex);
111 auto result = env.app().getLedgerMaster().txnIdFromIndex(
112 startLegSeq + 1, txnIndex);
113 BEAST_EXPECT(
114 *result ==
115 uint256("293DF7335EBBAF4420D52E70ABF470EB4C5792CAEA2F91F76193C2"
116 "819F538FDE"));
117 }
118 }
119
120public:
121 void
122 run() override
123 {
124 using namespace test::jtx;
127 }
128
129 void
131 {
132 testTxnIdFromIndex(features);
133 }
134};
135
136BEAST_DEFINE_TESTSUITE(LedgerMaster, app, ripple);
137
138} // namespace test
139} // namespace ripple
A testsuite class.
Definition: suite.h:55
testcase_t testcase
Memberspace for declaring test cases.
Definition: suite.h:155
void testTxnIdFromIndex(FeatureBitset features)
std::unique_ptr< Config > makeNetworkConfig(uint32_t networkID)
void testWithFeats(FeatureBitset features)
void run() override
Runs the suite.
Immutable cryptographic account descriptor.
Definition: Account.h:39
A transaction testing environment.
Definition: Env.h:120
T emplace_back(T... args)
std::unique_ptr< Config > envconfig()
creates and initializes a default configuration for jtx::Env
Definition: envconfig.h:54
Json::Value noop(Account const &account)
The null transaction.
Definition: noop.h:31
XRP_t const XRP
Converts to XRP Issue or STAmount.
Definition: amount.cpp:105
FeatureBitset supported_amendments()
Definition: Env.h:73
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:26
base_uint< 256 > uint256
Definition: base_uint.h:558