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