rippled
Loading...
Searching...
No Matches
Transaction_ordering_test.cpp
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2012, 2013 Ripple Labs Inc.
5 Permission to use, copy, modify, and/or distribute this software for any
6 purpose with or without fee is hereby granted, provided that the above
7 copyright notice and this permission notice appear in all copies.
8 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15*/
16//==============================================================================
17
18#include <test/jtx.h>
19
20#include <xrpld/core/JobQueue.h>
21
22namespace ripple {
23namespace test {
24
26{
27 void
29 {
30 using namespace jtx;
31 testcase("Correct Order");
32
33 Env env(*this);
34 auto const alice = Account("alice");
35 env.fund(XRP(1000), noripple(alice));
36
37 auto const aliceSequence = env.seq(alice);
38
39 auto const tx1 = env.jt(noop(alice), seq(aliceSequence));
40 auto const tx2 = env.jt(
41 noop(alice),
42 seq(aliceSequence + 1),
43 json(R"({"LastLedgerSequence":7})"));
44
45 env(tx1);
46 env.close();
47 BEAST_EXPECT(env.seq(alice) == aliceSequence + 1);
48 env(tx2);
49 env.close();
50 BEAST_EXPECT(env.seq(alice) == aliceSequence + 2);
51
52 env.close();
53
54 {
55 auto const result =
56 env.rpc("tx", to_string(tx1.stx->getTransactionID()));
57 BEAST_EXPECT(
58 result["result"]["meta"]["TransactionResult"] == "tesSUCCESS");
59 }
60 {
61 auto const result =
62 env.rpc("tx", to_string(tx2.stx->getTransactionID()));
63 BEAST_EXPECT(
64 result["result"]["meta"]["TransactionResult"] == "tesSUCCESS");
65 }
66 }
67
68 void
70 {
71 using namespace jtx;
72
73 testcase("Incorrect order");
74
75 Env env(*this, envconfig([](std::unique_ptr<Config> cfg) {
76 cfg->FORCE_MULTI_THREAD = false;
77 return cfg;
78 }));
79
80 auto const alice = Account("alice");
81 env.fund(XRP(1000), noripple(alice));
82
83 auto const aliceSequence = env.seq(alice);
84
85 auto const tx1 = env.jt(noop(alice), seq(aliceSequence));
86 auto const tx2 = env.jt(
87 noop(alice),
88 seq(aliceSequence + 1),
89 json(R"({"LastLedgerSequence":7})"));
90
91 env(tx2, ter(terPRE_SEQ));
92 BEAST_EXPECT(env.seq(alice) == aliceSequence);
93 env(tx1);
94 env.app().getJobQueue().rendezvous();
95 BEAST_EXPECT(env.seq(alice) == aliceSequence + 2);
96
97 env.close();
98
99 {
100 auto const result =
101 env.rpc("tx", to_string(tx1.stx->getTransactionID()));
102 BEAST_EXPECT(
103 result["result"]["meta"]["TransactionResult"] == "tesSUCCESS");
104 }
105 {
106 auto const result =
107 env.rpc("tx", to_string(tx2.stx->getTransactionID()));
108 BEAST_EXPECT(
109 result["result"]["meta"]["TransactionResult"] == "tesSUCCESS");
110 }
111 }
112
113 void
115 {
116 using namespace jtx;
117
118 testcase("Incorrect order multiple intermediaries");
119
120 Env env(*this, envconfig([](std::unique_ptr<Config> cfg) {
121 cfg->FORCE_MULTI_THREAD = true;
122 return cfg;
123 }));
124
125 auto const alice = Account("alice");
126 env.fund(XRP(1000), noripple(alice));
127
128 auto const aliceSequence = env.seq(alice);
129
131 for (auto i = 0; i < 5; ++i)
132 {
133 tx.emplace_back(env.jt(
134 noop(alice),
135 seq(aliceSequence + i),
136 json(R"({"LastLedgerSequence":7})")));
137 }
138
139 for (auto i = 1; i < 5; ++i)
140 {
141 env(tx[i], ter(terPRE_SEQ));
142 BEAST_EXPECT(env.seq(alice) == aliceSequence);
143 }
144
145 env(tx[0]);
146 env.app().getJobQueue().rendezvous();
147 BEAST_EXPECT(env.seq(alice) == aliceSequence + 5);
148
149 env.close();
150
151 for (auto i = 0; i < 5; ++i)
152 {
153 auto const result =
154 env.rpc("tx", to_string(tx[i].stx->getTransactionID()));
155 BEAST_EXPECT(
156 result["result"]["meta"]["TransactionResult"] == "tesSUCCESS");
157 }
158 }
159
160 void
161 run() override
162 {
166 }
167};
168
169BEAST_DEFINE_TESTSUITE(Transaction_ordering, app, ripple);
170
171} // namespace test
172} // namespace ripple
A testsuite class.
Definition: suite.h:55
testcase_t testcase
Memberspace for declaring test cases.
Definition: suite.h:155
virtual JobQueue & getJobQueue()=0
void rendezvous()
Block until no jobs running.
Definition: JobQueue.cpp:273
Immutable cryptographic account descriptor.
Definition: Account.h:39
A transaction testing environment.
Definition: Env.h:121
std::uint32_t seq(Account const &account) const
Returns the next sequence number on account.
Definition: Env.cpp:254
bool close(NetClock::time_point closeTime, std::optional< std::chrono::milliseconds > consensusDelay=std::nullopt)
Close and advance the ledger.
Definition: Env.cpp:117
JTx jt(JsonValue &&jv, FN const &... fN)
Create a JTx from parameters.
Definition: Env.h:505
Application & app()
Definition: Env.h:261
Json::Value rpc(unsigned apiVersion, std::unordered_map< std::string, std::string > const &headers, std::string const &cmd, Args &&... args)
Execute an RPC command.
Definition: Env.h:788
void fund(bool setDefaultRipple, STAmount const &amount, Account const &account)
Definition: Env.cpp:275
Inject raw JSON.
Definition: jtx_json.h:33
Set the expected result code for a JTx The test will fail if the code doesn't match.
Definition: ter.h:35
T emplace_back(T... args)
std::unique_ptr< Config > envconfig()
creates and initializes a default configuration for jtx::Env
Definition: envconfig.h:54
XRP_t const XRP
Converts to XRP Issue or STAmount.
Definition: amount.cpp:105
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:25
std::string to_string(base_uint< Bits, Tag > const &a)
Definition: base_uint.h:630
@ terPRE_SEQ
Definition: TER.h:221
Set the sequence number on a JTx.
Definition: seq.h:34