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