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
23#include <xrpl/protocol/jss.h>
24
25namespace ripple {
26namespace test {
27
29{
30public:
31 void
32 run() override
33 {
35 }
36
38 makeNetworkConfig(uint32_t networkID)
39 {
40 using namespace jtx;
41 return envconfig([&](std::unique_ptr<Config> cfg) {
42 cfg->NETWORK_ID = networkID;
43 return cfg;
44 });
45 }
46
47 void
49 {
51 "Require txn NetworkID to be specified (or not) depending on the "
52 "network ID of the node");
53 using namespace jtx;
54
55 auto const alice = Account{"alice"};
56
57 auto const runTx = [&](test::jtx::Env& env,
58 Json::Value const& jv,
59 TER expectedOutcome) {
60 env.memoize(env.master);
61 env.memoize(alice);
62
63 // fund alice
64 {
65 Json::Value jv;
66 jv[jss::Account] = env.master.human();
67 jv[jss::Destination] = alice.human();
68 jv[jss::TransactionType] = "Payment";
69 jv[jss::Amount] = "10000000000";
70 env(jv, fee(1000), sig(env.master));
71 }
72
73 env(jv, fee(1000), ter(expectedOutcome));
74 env.close();
75 };
76
77 // test mainnet
78 {
79 test::jtx::Env env{*this, makeNetworkConfig(0)};
80 BEAST_EXPECT(env.app().config().NETWORK_ID == 0);
81
82 // try to submit a txn without network id, this should work
83 Json::Value jv;
84 jv[jss::Account] = alice.human();
85 jv[jss::TransactionType] = jss::AccountSet;
86 runTx(env, jv, tesSUCCESS);
87
88 // try to submit a txn with NetworkID present against a mainnet
89 // node, this will fail
90 jv[jss::NetworkID] = 0;
92
93 // change network id to something else, should still return same
94 // error
95 jv[jss::NetworkID] = 10000;
97 }
98
99 // any network up to and including networkid 1024 cannot support
100 // NetworkID
101 {
102 test::jtx::Env env{*this, makeNetworkConfig(1024)};
103 BEAST_EXPECT(env.app().config().NETWORK_ID == 1024);
104
105 // try to submit a txn without network id, this should work
106 Json::Value jv;
107 jv[jss::Account] = alice.human();
108 jv[jss::TransactionType] = jss::AccountSet;
109 runTx(env, jv, tesSUCCESS);
110
111 // now submit with a network id, this will fail
112 jv[jss::NetworkID] = 1024;
114
115 jv[jss::NetworkID] = 1000;
117 }
118
119 // any network above networkid 1024 will produce an error if fed a txn
120 // absent networkid
121 {
122 test::jtx::Env env{*this, makeNetworkConfig(1025)};
123 BEAST_EXPECT(env.app().config().NETWORK_ID == 1025);
124 {
125 env.fund(XRP(200), alice);
126 // try to submit a txn without network id, this should not work
127 Json::Value jvn;
128 jvn[jss::Account] = alice.human();
129 jvn[jss::TransactionType] = jss::AccountSet;
130 jvn[jss::Fee] = to_string(env.current()->fees().base);
131 jvn[jss::Sequence] = env.seq(alice);
132 jvn[jss::LastLedgerSequence] = env.current()->info().seq + 2;
133 auto jt = env.jtnofill(jvn);
134 Serializer s;
135 jt.stx->add(s);
136 BEAST_EXPECT(
137 env.rpc(
138 "submit",
139 strHex(s.slice()))[jss::result][jss::engine_result] ==
140 "telREQUIRES_NETWORK_ID");
141 env.close();
142 }
143
144 Json::Value jv;
145 jv[jss::Account] = alice.human();
146 jv[jss::TransactionType] = jss::AccountSet;
147
148 // try to submit with wrong network id
149 jv[jss::NetworkID] = 0;
150 runTx(env, jv, telWRONG_NETWORK);
151
152 jv[jss::NetworkID] = 1024;
153 runTx(env, jv, telWRONG_NETWORK);
154
155 // submit the correct network id
156 jv[jss::NetworkID] = 1025;
157 runTx(env, jv, tesSUCCESS);
158 }
159 }
160};
161
162BEAST_DEFINE_TESTSUITE(NetworkID, app, ripple);
163
164} // namespace test
165} // namespace ripple
Represents a JSON value.
Definition json_value.h:149
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:66
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:118
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:268
std::shared_ptr< OpenView const > current() const
Returns the current ledger.
Definition Env.h:331
bool close(NetClock::time_point closeTime, std::optional< std::chrono::milliseconds > consensusDelay=std::nullopt)
Close and advance the ledger.
Definition Env.cpp:121
Account const & master
Definition Env.h:125
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:791
void fund(bool setDefaultRipple, STAmount const &amount, Account const &account)
Definition Env.cpp:289
JTx jtnofill(JsonValue &&jv, FN const &... fN)
Create a JTx from parameters.
Definition Env.h:520
void memoize(Account const &account)
Associate AccountID with account.
Definition Env.cpp:156
Set the fee on a JTx.
Definition fee.h:37
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:111
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:25
@ 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:244
std::string to_string(base_uint< Bits, Tag > const &a)
Definition base_uint.h:630