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}
6660template void ProcessNetInfoCore (CProRegTx& ptx, const UniValue& input, const bool optional);
6761template 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" );
0 commit comments