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