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 <xrpld/core/ConfigSections.h>
23#include <xrpl/basics/BasicConfig.h>
24#include <xrpl/protocol/jss.h>
25
26namespace ripple {
27namespace test {
28
30{
31public:
32 void
33 run() override
34 {
36 }
37
39 makeNetworkConfig(uint32_t networkID)
40 {
41 using namespace jtx;
42 return envconfig([&](std::unique_ptr<Config> cfg) {
43 cfg->NETWORK_ID = networkID;
44 return cfg;
45 });
46 }
47
48 void
50 {
52 "Require txn NetworkID to be specified (or not) depending on the "
53 "network ID of the node");
54 using namespace jtx;
55
56 auto const alice = Account{"alice"};
57
58 auto const runTx = [&](test::jtx::Env& env,
59 Json::Value const& jv,
60 TER expectedOutcome) {
61 env.memoize(env.master);
62 env.memoize(alice);
63
64 // fund alice
65 {
66 Json::Value jv;
67 jv[jss::Account] = env.master.human();
68 jv[jss::Destination] = alice.human();
69 jv[jss::TransactionType] = "Payment";
70 jv[jss::Amount] = "10000000000";
71 env(jv, fee(1000), sig(env.master));
72 }
73
74 env(jv, fee(1000), ter(expectedOutcome));
75 env.close();
76 };
77
78 // test mainnet
79 {
80 test::jtx::Env env{*this, makeNetworkConfig(0)};
81 BEAST_EXPECT(env.app().config().NETWORK_ID == 0);
82
83 // try to submit a txn without network id, this should work
84 Json::Value jv;
85 jv[jss::Account] = alice.human();
86 jv[jss::TransactionType] = jss::AccountSet;
87 runTx(env, jv, tesSUCCESS);
88
89 // try to submit a txn with NetworkID present against a mainnet
90 // node, this will fail
91 jv[jss::NetworkID] = 0;
93
94 // change network id to something else, should still return same
95 // error
96 jv[jss::NetworkID] = 10000;
98 }
99
100 // any network up to and including networkid 1024 cannot support
101 // NetworkID
102 {
103 test::jtx::Env env{*this, makeNetworkConfig(1024)};
104 BEAST_EXPECT(env.app().config().NETWORK_ID == 1024);
105
106 // try to submit a txn without network id, this should work
107 Json::Value jv;
108 jv[jss::Account] = alice.human();
109 jv[jss::TransactionType] = jss::AccountSet;
110 runTx(env, jv, tesSUCCESS);
111
112 // now submit with a network id, this will fail
113 jv[jss::NetworkID] = 1024;
115
116 jv[jss::NetworkID] = 1000;
118 }
119
120 // any network above networkid 1024 will produce an error if fed a txn
121 // absent networkid
122 {
123 test::jtx::Env env{*this, makeNetworkConfig(1025)};
124 BEAST_EXPECT(env.app().config().NETWORK_ID == 1025);
125 {
126 env.fund(XRP(200), alice);
127 // try to submit a txn without network id, this should not work
128 Json::Value jvn;
129 jvn[jss::Account] = alice.human();
130 jvn[jss::TransactionType] = jss::AccountSet;
131 jvn[jss::Fee] = to_string(env.current()->fees().base);
132 jvn[jss::Sequence] = env.seq(alice);
133 jvn[jss::LastLedgerSequence] = env.current()->info().seq + 2;
134 auto jt = env.jtnofill(jvn);
135 Serializer s;
136 jt.stx->add(s);
137 BEAST_EXPECT(
138 env.rpc(
139 "submit",
140 strHex(s.slice()))[jss::result][jss::engine_result] ==
141 "telREQUIRES_NETWORK_ID");
142 env.close();
143 }
144
145 Json::Value jv;
146 jv[jss::Account] = alice.human();
147 jv[jss::TransactionType] = jss::AccountSet;
148
149 // try to submit with wrong network id
150 jv[jss::NetworkID] = 0;
151 runTx(env, jv, telWRONG_NETWORK);
152
153 jv[jss::NetworkID] = 1024;
154 runTx(env, jv, telWRONG_NETWORK);
155
156 // submit the correct network id
157 jv[jss::NetworkID] = 1025;
158 runTx(env, jv, tesSUCCESS);
159 }
160 }
161};
162
163BEAST_DEFINE_TESTSUITE(NetworkID, app, ripple);
164
165} // namespace test
166} // namespace ripple
Represents a JSON value.
Definition: json_value.h:147
A testsuite class.
Definition: suite.h:53
testcase_t testcase
Memberspace for declaring test cases.
Definition: suite.h:153
virtual Config & config()=0
uint32_t NETWORK_ID
Definition: Config.h:163
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:38
std::string const & human() const
Returns the human readable public key.
Definition: Account.h:113
A transaction testing environment.
Definition: Env.h:117
std::uint32_t seq(Account const &account) const
Returns the next sequence number on account.
Definition: Env.cpp:216
std::shared_ptr< OpenView const > current() const
Returns the current ledger.
Definition: Env.h:325
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:121
Application & app()
Definition: Env.h:255
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:749
void fund(bool setDefaultRipple, STAmount const &amount, Account const &account)
Definition: Env.cpp:237
JTx jtnofill(JsonValue &&jv, FN const &... fN)
Create a JTx from parameters.
Definition: Env.h:488
void memoize(Account const &account)
Associate AccountID with account.
Definition: Env.cpp:156
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:34
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:629