rippled
Loading...
Searching...
No Matches
LedgerHistory_test.cpp
1#include <test/jtx.h>
2#include <test/jtx/CheckMessageLogs.h>
3
4#include <xrpld/app/ledger/LedgerHistory.h>
5#include <xrpld/app/ledger/LedgerMaster.h>
6#include <xrpld/app/tx/apply.h>
7
8#include <xrpl/beast/insight/NullCollector.h>
9#include <xrpl/beast/unit_test.h>
10#include <xrpl/ledger/OpenView.h>
11
12#include <chrono>
13#include <sstream>
14
15namespace ripple {
16namespace test {
17
19{
20public:
31 jtx::Env& env,
32 LedgerHistory& lh,
33 NetClock::duration closeOffset,
35 {
36 if (!prev)
37 {
38 assert(!stx);
41 env.app().config(),
43 env.app().getNodeFamily());
44 }
45 auto res = std::make_shared<Ledger>(
46 *prev, prev->info().closeTime + closeOffset);
47
48 if (stx)
49 {
50 OpenView accum(&*res);
52 env.app(), accum, *stx, false, tapNONE, env.journal);
53 accum.apply(*res);
54 }
55 res->updateSkipList();
56
57 {
58 res->stateMap().flushDirty(hotACCOUNT_NODE);
59 res->txMap().flushDirty(hotTRANSACTION_NODE);
60 }
61 res->unshare();
62
63 // Accept ledger
64 res->setAccepted(
65 res->info().closeTime,
66 res->info().closeTimeResolution,
67 true /* close time correct*/);
68 lh.insert(res, false);
69 return res;
70 }
71
72 void
74 {
75 testcase("LedgerHistory mismatch");
76 using namespace jtx;
77 using namespace std::chrono;
78
79 // No mismatch
80 {
81 bool found = false;
82 Env env{
83 *this,
84 envconfig(),
85 std::make_unique<CheckMessageLogs>("MISMATCH ", &found)};
87 auto const genesis = makeLedger({}, env, lh, 0s);
88 uint256 const dummyTxHash{1};
89 lh.builtLedger(genesis, dummyTxHash, {});
90 lh.validatedLedger(genesis, dummyTxHash);
91
92 BEAST_EXPECT(!found);
93 }
94
95 // Close time mismatch
96 {
97 bool found = false;
98 Env env{
99 *this,
100 envconfig(),
102 "MISMATCH on close time", &found)};
104 auto const genesis = makeLedger({}, env, lh, 0s);
105 auto const ledgerA = makeLedger(genesis, env, lh, 4s);
106 auto const ledgerB = makeLedger(genesis, env, lh, 40s);
107
108 uint256 const dummyTxHash{1};
109 lh.builtLedger(ledgerA, dummyTxHash, {});
110 lh.validatedLedger(ledgerB, dummyTxHash);
111
112 BEAST_EXPECT(found);
113 }
114
115 // Prior ledger mismatch
116 {
117 bool found = false;
118 Env env{
119 *this,
120 envconfig(),
122 "MISMATCH on prior ledger", &found)};
124 auto const genesis = makeLedger({}, env, lh, 0s);
125 auto const ledgerA = makeLedger(genesis, env, lh, 4s);
126 auto const ledgerB = makeLedger(genesis, env, lh, 40s);
127 auto const ledgerAC = makeLedger(ledgerA, env, lh, 4s);
128 auto const ledgerBD = makeLedger(ledgerB, env, lh, 4s);
129
130 uint256 const dummyTxHash{1};
131 lh.builtLedger(ledgerAC, dummyTxHash, {});
132 lh.validatedLedger(ledgerBD, dummyTxHash);
133
134 BEAST_EXPECT(found);
135 }
136
137 // Simulate a bug in which consensus may agree on transactions, but
138 // somehow generate different ledgers
139 for (bool const txBug : {true, false})
140 {
141 std::string const msg = txBug
142 ? "MISMATCH with same consensus transaction set"
143 : "MISMATCH on consensus transaction set";
144 bool found = false;
145 Env env{
146 *this,
147 envconfig(),
150
151 Account alice{"A1"};
152 Account bob{"A2"};
153 env.fund(XRP(1000), alice, bob);
154 env.close();
155
156 auto const ledgerBase =
158
159 JTx txAlice = env.jt(noop(alice));
160 auto const ledgerA =
161 makeLedger(ledgerBase, env, lh, 4s, txAlice.stx);
162
163 JTx txBob = env.jt(noop(bob));
164 auto const ledgerB = makeLedger(ledgerBase, env, lh, 4s, txBob.stx);
165
166 lh.builtLedger(ledgerA, txAlice.stx->getTransactionID(), {});
167 // Simulate the bug by claiming ledgerB had the same consensus hash
168 // as ledgerA, but somehow generated different ledgers
170 ledgerB,
171 txBug ? txAlice.stx->getTransactionID()
172 : txBob.stx->getTransactionID());
173
174 BEAST_EXPECT(found);
175 }
176 }
177
178 void
179 run() override
180 {
182 }
183};
184
185BEAST_DEFINE_TESTSUITE(LedgerHistory, app, ripple);
186
187} // namespace test
188} // namespace ripple
static std::shared_ptr< Collector > New()
A testsuite class.
Definition suite.h:52
testcase_t testcase
Memberspace for declaring test cases.
Definition suite.h:152
virtual Config & config()=0
virtual Family & getNodeFamily()=0
virtual LedgerMaster & getLedgerMaster()=0
Retains historical ledgers.
void builtLedger(std::shared_ptr< Ledger const > const &, uint256 const &consensusHash, Json::Value)
Report that we have locally built a particular ledger.
bool insert(std::shared_ptr< Ledger const > const &ledger, bool validated)
Track a ledger.
void validatedLedger(std::shared_ptr< Ledger const > const &, std::optional< uint256 > const &consensusHash)
Report that we have validated a particular ledger.
std::shared_ptr< Ledger const > getClosedLedger()
Writable ledger view that accumulates state and tx changes.
Definition OpenView.h:46
void run() override
Runs the suite.
static std::shared_ptr< Ledger > makeLedger(std::shared_ptr< Ledger const > const &prev, jtx::Env &env, LedgerHistory &lh, NetClock::duration closeOffset, std::shared_ptr< STTx const > stx={})
Generate a new ledger by hand, applying a specific close time offset and optionally inserting a trans...
Immutable cryptographic account descriptor.
Definition Account.h:20
A transaction testing environment.
Definition Env.h:102
bool close(NetClock::time_point closeTime, std::optional< std::chrono::milliseconds > consensusDelay=std::nullopt)
Close and advance the ledger.
Definition Env.cpp:103
JTx jt(JsonValue &&jv, FN const &... fN)
Create a JTx from parameters.
Definition Env.h:489
Application & app()
Definition Env.h:242
beast::Journal const journal
Definition Env.h:143
void fund(bool setDefaultRipple, STAmount const &amount, Account const &account)
Definition Env.cpp:271
T is_same_v
std::unique_ptr< Config > envconfig()
creates and initializes a default configuration for jtx::Env
Definition envconfig.h:35
XRP_t const XRP
Converts to XRP Issue or STAmount.
Definition amount.cpp:92
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:6
@ hotACCOUNT_NODE
Definition NodeObject.h:16
@ hotTRANSACTION_NODE
Definition NodeObject.h:17
create_genesis_t const create_genesis
Definition Ledger.cpp:32
@ tapNONE
Definition ApplyView.h:12
ApplyTransactionResult applyTransaction(Application &app, OpenView &view, STTx const &tx, bool retryAssured, ApplyFlags flags, beast::Journal journal)
Transaction application helper.
Definition apply.cpp:220
Execution context for applying a JSON transaction.
Definition JTx.h:26
std::shared_ptr< STTx const > stx
Definition JTx.h:37