rippled
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 <ripple/app/ledger/LedgerMaster.h>
21 #include <ripple/protocol/jss.h>
22 #include <test/jtx.h>
23 #include <test/jtx/Env.h>
24 
25 namespace ripple {
26 namespace test {
27 
28 class LedgerMaster_test : public beast::unit_test::suite
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 
120 public:
121  void
122  run() override
123  {
124  using namespace test::jtx;
127  }
128 
129  void
131  {
132  testTxnIdFromIndex(features);
133  }
134 };
135 
137 
138 } // namespace test
139 } // namespace ripple
ripple::test::jtx::noop
Json::Value noop(Account const &account)
The null transaction.
Definition: noop.h:31
ripple::test::jtx::XRP
const XRP_t XRP
Converts to XRP Issue or STAmount.
Definition: amount.cpp:105
ripple::TxSearched::all
@ all
ripple::LedgerMaster
Definition: LedgerMaster.h:70
std::vector
STL class.
ripple::test::jtx::envconfig
std::unique_ptr< Config > envconfig()
creates and initializes a default configuration for jtx::Env
Definition: envconfig.h:49
ripple::test::LedgerMaster_test::makeNetworkConfig
std::unique_ptr< Config > makeNetworkConfig(uint32_t networkID)
Definition: LedgerMaster_test.cpp:31
ripple::uint256
base_uint< 256 > uint256
Definition: base_uint.h:550
ripple::test::LedgerMaster_test::run
void run() override
Definition: LedgerMaster_test.cpp:122
ripple::test::LedgerMaster_test
Definition: LedgerMaster_test.cpp:28
ripple::test::jtx::supported_amendments
FeatureBitset supported_amendments()
Definition: Env.h:71
std::uint32_t
ripple::sfTransactionIndex
const SF_UINT32 sfTransactionIndex
std::vector::emplace_back
T emplace_back(T... args)
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::test::LedgerMaster_test::testTxnIdFromIndex
void testTxnIdFromIndex(FeatureBitset features)
Definition: LedgerMaster_test.cpp:41
ripple::FeatureBitset
Definition: Feature.h:113
ripple::test::jtx::Account
Immutable cryptographic account descriptor.
Definition: Account.h:37
std::unique_ptr
STL class.
ripple::test::jtx::Env
A transaction testing environment.
Definition: Env.h:117
ripple::test::LedgerMaster_test::testWithFeats
void testWithFeats(FeatureBitset features)
Definition: LedgerMaster_test.cpp:130
ripple::test::BEAST_DEFINE_TESTSUITE
BEAST_DEFINE_TESTSUITE(DeliverMin, app, ripple)