rippled
Loading...
Searching...
No Matches
NetworkID_test.cpp
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2020 Dev Null Productions
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#include <xrpl/protocol/jss.h>
23
24namespace ripple {
25namespace test {
26
28{
29public:
30 void
31 run() override
32 {
34 }
35
37 makeNetworkConfig(uint32_t networkID)
38 {
39 using namespace jtx;
40 return envconfig([&](std::unique_ptr<Config> cfg) {
41 cfg->NETWORK_ID = networkID;
42 return cfg;
43 });
44 }
45
46 void
48 {
50 "Require txn NetworkID to be specified (or not) depending on the "
51 "network ID of the node");
52 using namespace jtx;
53
54 auto const alice = Account{"alice"};
55
56 auto const runTx = [&](test::jtx::Env& env,
57 Json::Value const& jv,
58 TER expectedOutcome) {
59 env.memoize(env.master);
60 env.memoize(alice);
61
62 // fund alice
63 {
64 Json::Value jv;
65 jv[jss::Account] = env.master.human();
66 jv[jss::Destination] = alice.human();
67 jv[jss::TransactionType] = "Payment";
68 jv[jss::Amount] = "10000000000";
69 env(jv, fee(1000), sig(env.master));
70 }
71
72 env(jv, fee(1000), ter(expectedOutcome));
73 env.close();
74 };
75
76 // test mainnet
77 {
78 test::jtx::Env env{*this, makeNetworkConfig(0)};
79 BEAST_EXPECT(env.app().config().NETWORK_ID == 0);
80
81 // try to submit a txn without network id, this should work
82 Json::Value jv;
83 jv[jss::Account] = alice.human();
84 jv[jss::TransactionType] = jss::AccountSet;
85 runTx(env, jv, tesSUCCESS);
86
87 // try to submit a txn with NetworkID present against a mainnet
88 // node, this will fail
89 jv[jss::NetworkID] = 0;
91
92 // change network id to something else, should still return same
93 // error
94 jv[jss::NetworkID] = 10000;
96 }
97
98 // any network up to and including networkid 1024 cannot support
99 // NetworkID
100 {
101 test::jtx::Env env{*this, makeNetworkConfig(1024)};
102 BEAST_EXPECT(env.app().config().NETWORK_ID == 1024);
103
104 // try to submit a txn without network id, this should work
105 Json::Value jv;
106 jv[jss::Account] = alice.human();
107 jv[jss::TransactionType] = jss::AccountSet;
108 runTx(env, jv, tesSUCCESS);
109
110 // now submit with a network id, this will fail
111 jv[jss::NetworkID] = 1024;
113
114 jv[jss::NetworkID] = 1000;
116 }
117
118 // any network above networkid 1024 will produce an error if fed a txn
119 // absent networkid
120 {
121 test::jtx::Env env{*this, makeNetworkConfig(1025)};
122 BEAST_EXPECT(env.app().config().NETWORK_ID == 1025);
123 {
124 env.fund(XRP(200), alice);
125 // try to submit a txn without network id, this should not work
126 Json::Value jvn;
127 jvn[jss::Account] = alice.human();
128 jvn[jss::TransactionType] = jss::AccountSet;
129 jvn[jss::Fee] = to_string(env.current()->fees().base);
130 jvn[jss::Sequence] = env.seq(alice);
131 jvn[jss::LastLedgerSequence] = env.current()->info().seq + 2;
132 auto jt = env.jtnofill(jvn);
133 Serializer s;
134 jt.stx->add(s);
135 BEAST_EXPECT(
136 env.rpc(
137 "submit",
138 strHex(s.slice()))[jss::result][jss::engine_result] ==
139 "telREQUIRES_NETWORK_ID");
140 env.close();
141 }
142
143 Json::Value jv;
144 jv[jss::Account] = alice.human();
145 jv[jss::TransactionType] = jss::AccountSet;
146
147 // try to submit with wrong network id
148 jv[jss::NetworkID] = 0;
149 runTx(env, jv, telWRONG_NETWORK);
150
151 jv[jss::NetworkID] = 1024;
152 runTx(env, jv, telWRONG_NETWORK);
153
154 // submit the correct network id
155 jv[jss::NetworkID] = 1025;
156 runTx(env, jv, tesSUCCESS);
157 }
158 }
159};
160
161BEAST_DEFINE_TESTSUITE(NetworkID, app, ripple);
162
163} // namespace test
164} // namespace ripple
Represents a JSON value.
Definition: json_value.h:148
A testsuite class.
Definition: suite.h:55
testcase_t testcase
Memberspace for declaring test cases.
Definition: suite.h:155
virtual Config & config()=0
uint32_t NETWORK_ID
Definition: Config.h:156
Slice slice() const noexcept
Definition: Serializer.h:67
void run() override
Runs the suite.
std::unique_ptr< Config > makeNetworkConfig(uint32_t networkID)
Immutable cryptographic account descriptor.
Definition: Account.h:39
std::string const & human() const
Returns the human readable public key.
Definition: Account.h:114
A transaction testing environment.
Definition: Env.h:118
std::uint32_t seq(Account const &account) const
Returns the next sequence number on account.
Definition: Env.cpp:210
std::shared_ptr< OpenView const > current() const
Returns the current ledger.
Definition: Env.h:326
bool close(NetClock::time_point closeTime, std::optional< std::chrono::milliseconds > consensusDelay=std::nullopt)
Close and advance the ledger.
Definition: Env.cpp:115
Account const & master
Definition: Env.h:122
Application & app()
Definition: Env.h:256
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:765
void fund(bool setDefaultRipple, STAmount const &amount, Account const &account)
Definition: Env.cpp:231
JTx jtnofill(JsonValue &&jv, FN const &... fN)
Create a JTx from parameters.
Definition: Env.h:503
void memoize(Account const &account)
Associate AccountID with account.
Definition: Env.cpp:150
Set the fee on a JTx.
Definition: fee.h:36
Set the regular signature on a JTx.
Definition: sig.h:35
Set the expected result code for a JTx The test will fail if the code doesn't match.
Definition: ter.h:35
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:104
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:26
@ telWRONG_NETWORK
Definition: TER.h:65
@ telNETWORK_ID_MAKES_TX_NON_CANONICAL
Definition: TER.h:67
std::string strHex(FwdIt begin, FwdIt end)
Definition: strHex.h:30
@ tesSUCCESS
Definition: TER.h:242
std::string to_string(base_uint< Bits, Tag > const &a)
Definition: base_uint.h:630