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/core/NetworkIDService.h>
7#include <xrpl/protocol/jss.h>
8
9namespace xrpl {
10namespace test {
11
13{
14public:
15 void
16 run() override
17 {
19 }
20
22 makeNetworkConfig(uint32_t networkID)
23 {
24 using namespace jtx;
25 return envconfig([&](std::unique_ptr<Config> cfg) {
26 cfg->NETWORK_ID = networkID;
27 return cfg;
28 });
29 }
30
31 void
33 {
35 "Require txn NetworkID to be specified (or not) depending on the "
36 "network ID of the node");
37 using namespace jtx;
38
39 auto const alice = Account{"alice"};
40
41 auto const runTx = [&](test::jtx::Env& env, Json::Value const& jv, TER expectedOutcome) {
42 env.memoize(env.master);
43 env.memoize(alice);
44
45 // fund alice
46 {
47 Json::Value jv;
48 jv[jss::Account] = env.master.human();
49 jv[jss::Destination] = alice.human();
50 jv[jss::TransactionType] = "Payment";
51 jv[jss::Amount] = "10000000000";
52 env(jv, fee(1000), sig(env.master));
53 }
54
55 env(jv, fee(1000), ter(expectedOutcome));
56 env.close();
57 };
58
59 // test mainnet
60 {
61 test::jtx::Env env{*this, makeNetworkConfig(0)};
62 BEAST_EXPECT(env.app().getNetworkIDService().getNetworkID() == 0);
63
64 // try to submit a txn without network id, this should work
65 Json::Value jv;
66 jv[jss::Account] = alice.human();
67 jv[jss::TransactionType] = jss::AccountSet;
68 runTx(env, jv, tesSUCCESS);
69
70 // try to submit a txn with NetworkID present against a mainnet
71 // node, this will fail
72 jv[jss::NetworkID] = 0;
74
75 // change network id to something else, should still return same
76 // error
77 jv[jss::NetworkID] = 10000;
79 }
80
81 // any network up to and including networkid 1024 cannot support
82 // NetworkID
83 {
84 test::jtx::Env env{*this, makeNetworkConfig(1024)};
85 BEAST_EXPECT(env.app().getNetworkIDService().getNetworkID() == 1024);
86
87 // try to submit a txn without network id, this should work
88 Json::Value jv;
89 jv[jss::Account] = alice.human();
90 jv[jss::TransactionType] = jss::AccountSet;
91 runTx(env, jv, tesSUCCESS);
92
93 // now submit with a network id, this will fail
94 jv[jss::NetworkID] = 1024;
96
97 jv[jss::NetworkID] = 1000;
99 }
100
101 // any network above networkid 1024 will produce an error if fed a txn
102 // absent networkid
103 {
104 test::jtx::Env env{*this, makeNetworkConfig(1025)};
105 BEAST_EXPECT(env.app().getNetworkIDService().getNetworkID() == 1025);
106 {
107 env.fund(XRP(200), alice);
108 // try to submit a txn without network id, this should not work
109 Json::Value jvn;
110 jvn[jss::Account] = alice.human();
111 jvn[jss::TransactionType] = jss::AccountSet;
112 jvn[jss::Fee] = to_string(env.current()->fees().base);
113 jvn[jss::Sequence] = env.seq(alice);
114 jvn[jss::LastLedgerSequence] = env.current()->header().seq + 2;
115 auto jt = env.jtnofill(jvn);
116 Serializer s;
117 jt.stx->add(s);
118 BEAST_EXPECT(
119 env.rpc("submit", strHex(s.slice()))[jss::result][jss::engine_result] ==
120 "telREQUIRES_NETWORK_ID");
121 env.close();
122 }
123
124 Json::Value jv;
125 jv[jss::Account] = alice.human();
126 jv[jss::TransactionType] = jss::AccountSet;
127
128 // try to submit with wrong network id
129 jv[jss::NetworkID] = 0;
130 runTx(env, jv, telWRONG_NETWORK);
131
132 jv[jss::NetworkID] = 1024;
133 runTx(env, jv, telWRONG_NETWORK);
134
135 // submit the correct network id
136 jv[jss::NetworkID] = 1025;
137 runTx(env, jv, tesSUCCESS);
138 }
139 }
140};
141
142BEAST_DEFINE_TESTSUITE(NetworkID, app, xrpl);
143
144} // namespace test
145} // 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:150
virtual std::uint32_t getNetworkID() const noexcept=0
Get the configured network ID.
Slice slice() const noexcept
Definition Serializer.h:44
virtual NetworkIDService & getNetworkIDService()=0
static 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:122
Application & app()
Definition Env.h:259
bool close(NetClock::time_point closeTime, std::optional< std::chrono::milliseconds > consensusDelay=std::nullopt)
Close and advance the ledger.
Definition Env.cpp:100
void fund(bool setDefaultRipple, STAmount const &amount, Account const &account)
Definition Env.cpp:270
std::uint32_t seq(Account const &account) const
Returns the next sequence number on account.
Definition Env.cpp:249
Account const & master
Definition Env.h:126
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:847
JTx jtnofill(JsonValue &&jv, FN const &... fN)
Create a JTx from parameters.
Definition Env.h:561
void memoize(Account const &account)
Associate AccountID with account.
Definition Env.cpp:141
std::shared_ptr< OpenView const > current() const
Returns the current ledger.
Definition Env.h:329
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:95
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:602
std::string strHex(FwdIt begin, FwdIt end)
Definition strHex.h:10
@ tesSUCCESS
Definition TER.h:225