Skip to content

Commit 7664ecf

Browse files
kwvgknst
andcommitted
refactor: consolidate input processing in ProcessNetInfo*, update errs
Co-Authored-By: Konstantin Akimov <knstqq@gmail.com>
1 parent e155529 commit 7664ecf

File tree

3 files changed

+44
-47
lines changed

3 files changed

+44
-47
lines changed

src/rpc/evo_util.cpp

Lines changed: 33 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -13,61 +13,55 @@
1313

1414
#include <univalue.h>
1515

16-
template <typename T1>
17-
void ProcessNetInfoCore(T1& ptx, const UniValue& input, const bool optional)
16+
namespace {
17+
template <typename ProTx>
18+
void ParseInput(ProTx& ptx, std::string_view field_name, const std::string& input_str, NetInfoPurpose purpose,
19+
size_t idx, bool optional)
1820
{
19-
CHECK_NONFATAL(ptx.netInfo);
20-
21-
if (input.isStr()) {
22-
const std::string& entry = input.get_str();
23-
if (entry.empty()) {
24-
if (!optional) {
25-
throw JSONRPCError(RPC_INVALID_PARAMETER, "Empty param for coreP2PAddrs not allowed");
26-
}
27-
return; // Nothing to do
28-
}
29-
if (auto entryRet = ptx.netInfo->AddEntry(NetInfoPurpose::CORE_P2P, entry); entryRet != NetInfoStatus::Success) {
21+
if (input_str.empty()) {
22+
if (!optional) {
3023
throw JSONRPCError(RPC_INVALID_PARAMETER,
31-
strprintf("Error setting coreP2PAddrs[0] to '%s' (%s)", entry, NISToString(entryRet)));
24+
strprintf("Invalid param for %s[%zu], cannot be empty", field_name, idx));
3225
}
33-
return; // Parsing complete
26+
return; // Nothing to do
3427
}
28+
if (auto ret = ptx.netInfo->AddEntry(purpose, input_str); ret != NetInfoStatus::Success) {
29+
throw JSONRPCError(RPC_INVALID_PARAMETER,
30+
strprintf("Error setting %s[%zu] to '%s' (%s)", field_name, idx, input_str, NISToString(ret)));
31+
}
32+
}
33+
} // anonymous namespace
3534

36-
if (input.isArray()) {
35+
template <typename ProTx>
36+
void ProcessNetInfoCore(ProTx& ptx, const UniValue& input, const bool optional)
37+
{
38+
CHECK_NONFATAL(ptx.netInfo);
39+
40+
if (input.isStr()) {
41+
ParseInput(ptx, /*field_name=*/"coreP2PAddrs", input.get_str(), NetInfoPurpose::CORE_P2P, /*idx=*/0, optional);
42+
} else if (input.isArray()) {
3743
const UniValue& entries = input.get_array();
38-
if (entries.empty()) {
39-
if (!optional) {
40-
throw JSONRPCError(RPC_INVALID_PARAMETER, "Empty params for coreP2PAddrs not allowed");
41-
}
42-
return; // Nothing to do
44+
if (!optional && entries.empty()) {
45+
throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid param for coreP2PAddrs, cannot be empty");
4346
}
4447
for (size_t idx{0}; idx < entries.size(); idx++) {
4548
const UniValue& entry_uv{entries[idx]};
4649
if (!entry_uv.isStr()) {
4750
throw JSONRPCError(RPC_INVALID_PARAMETER,
48-
strprintf("Invalid param for coreP2PAddrs[%d], must be string", idx));
49-
}
50-
const std::string& entry = entry_uv.get_str();
51-
if (entry.empty()) {
52-
throw JSONRPCError(RPC_INVALID_PARAMETER,
53-
strprintf("Invalid param for coreP2PAddrs[%d], cannot be empty string", idx));
54-
}
55-
if (auto entryRet = ptx.netInfo->AddEntry(NetInfoPurpose::CORE_P2P, entry); entryRet != NetInfoStatus::Success) {
56-
throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("Error setting coreP2PAddrs[%d] to '%s' (%s)", idx,
57-
entry, NISToString(entryRet)));
51+
strprintf("Invalid param for coreP2PAddrs[%zu], must be string", idx));
5852
}
53+
ParseInput(ptx, /*field_name=*/"coreP2PAddrs", entry_uv.get_str(), NetInfoPurpose::CORE_P2P, idx,
54+
/*optional=*/false);
5955
}
60-
return; // Parsing complete
56+
} else {
57+
throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid param for coreP2PAddrs, must be string or array");
6158
}
62-
63-
// Invalid input
64-
throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid param for coreP2PAddrs, must be string or array");
6559
}
6660
template void ProcessNetInfoCore(CProRegTx& ptx, const UniValue& input, const bool optional);
6761
template void ProcessNetInfoCore(CProUpServTx& ptx, const UniValue& input, const bool optional);
6862

69-
template <typename T1>
70-
void ProcessNetInfoPlatform(T1& ptx, const UniValue& input_p2p, const UniValue& input_http)
63+
template <typename ProTx>
64+
void ProcessNetInfoPlatform(ProTx& ptx, const UniValue& input_p2p, const UniValue& input_http)
7165
{
7266
CHECK_NONFATAL(ptx.netInfo);
7367

@@ -78,7 +72,8 @@ void ProcessNetInfoPlatform(T1& ptx, const UniValue& input_p2p, const UniValue&
7872
if (int32_t port{ParseInt32V(input, field_name)}; port >= 1 && port <= std::numeric_limits<uint16_t>::max()) {
7973
target = static_cast<uint16_t>(port);
8074
} else {
81-
throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("%s must be a valid port [1-65535]", field_name));
75+
throw JSONRPCError(RPC_INVALID_PARAMETER,
76+
strprintf("Invalid param for %s, must be a valid port [1-65535]", field_name));
8277
}
8378
};
8479
process_field(ptx.platformP2PPort, input_p2p, "platformP2PPort");

src/rpc/evo_util.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@
77

88
class UniValue;
99

10-
template <typename T1>
11-
void ProcessNetInfoCore(T1& ptx, const UniValue& input, const bool optional);
10+
/** Process setting (legacy) Core network information field based on ProTx version */
11+
template <typename ProTx>
12+
void ProcessNetInfoCore(ProTx& ptx, const UniValue& input, const bool optional);
1213

13-
template <typename T1>
14-
void ProcessNetInfoPlatform(T1& ptx, const UniValue& input_p2p, const UniValue& input_http);
14+
/** Process setting (legacy) Platform network information fields based on ProTx version */
15+
template <typename ProTx>
16+
void ProcessNetInfoPlatform(ProTx& ptx, const UniValue& input_p2p, const UniValue& input_http);
1517

1618
#endif // BITCOIN_RPC_EVO_UTIL_H

test/functional/rpc_netinfo.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -176,20 +176,20 @@ def test_validation_common(self):
176176
-8, "Invalid param for coreP2PAddrs[0], must be string")
177177
self.node_evo.register_mn(self, False, [f"127.0.0.1:{self.node_evo.mn.nodePort}", ""],
178178
DEFAULT_PORT_PLATFORM_P2P, DEFAULT_PORT_PLATFORM_HTTP,
179-
-8, "Invalid param for coreP2PAddrs[1], cannot be empty string")
179+
-8, "Invalid param for coreP2PAddrs[1], cannot be empty")
180180
self.node_evo.register_mn(self, False, [f"127.0.0.1:{self.node_evo.mn.nodePort}", self.node_evo.mn.nodePort],
181181
DEFAULT_PORT_PLATFORM_P2P, DEFAULT_PORT_PLATFORM_HTTP,
182182
-8, "Invalid param for coreP2PAddrs[1], must be string")
183183

184184
# platformP2PPort and platformHTTPPort must be within acceptable range (i.e. a valid port number)
185185
self.node_evo.register_mn(self, False, f"127.0.0.1:{self.node_evo.mn.nodePort}", "0", DEFAULT_PORT_PLATFORM_HTTP,
186-
-8, "platformP2PPort must be a valid port [1-65535]")
186+
-8, "Invalid param for platformP2PPort, must be a valid port [1-65535]")
187187
self.node_evo.register_mn(self, False, f"127.0.0.1:{self.node_evo.mn.nodePort}", "65536", DEFAULT_PORT_PLATFORM_HTTP,
188-
-8, "platformP2PPort must be a valid port [1-65535]")
188+
-8, "Invalid param for platformP2PPort, must be a valid port [1-65535]")
189189
self.node_evo.register_mn(self, False, f"127.0.0.1:{self.node_evo.mn.nodePort}", DEFAULT_PORT_PLATFORM_P2P, "0",
190-
-8, "platformHTTPPort must be a valid port [1-65535]")
190+
-8, "Invalid param for platformHTTPPort, must be a valid port [1-65535]")
191191
self.node_evo.register_mn(self, False, f"127.0.0.1:{self.node_evo.mn.nodePort}", DEFAULT_PORT_PLATFORM_P2P, "65536",
192-
-8, "platformHTTPPort must be a valid port [1-65535]")
192+
-8, "Invalid param for platformHTTPPort, must be a valid port [1-65535]")
193193

194194
def test_validation_legacy(self):
195195
# Using mainnet P2P port gets refused

0 commit comments

Comments
 (0)