Skip to content

Commit 1d36005

Browse files
kwvgknst
andcommitted
rpc: set platform{HTTP,P2P}Port with netInfo, allow addr:port str
Co-Authored-By: Konstantin Akimov <knstqq@gmail.com>
1 parent 7664ecf commit 1d36005

File tree

3 files changed

+43
-14
lines changed

3 files changed

+43
-14
lines changed

src/rpc/evo.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -184,12 +184,14 @@ static RPCArg GetRpcArg(const std::string& strParamName)
184184
"Platform P2P node ID, derived from P2P public key."}
185185
},
186186
{"platformP2PPort",
187-
{"platformP2PPort", RPCArg::Type::NUM, RPCArg::Optional::NO,
188-
"TCP port of Dash Platform peer-to-peer communication between nodes (network byte order)."}
187+
{"platformP2PPort", RPCArg::Type::STR, RPCArg::Optional::NO,
188+
"Address in the form \"ADDR:PORT\" used by Platform for peer-to-peer connection.\n"
189+
"Must be unique on the network."}
189190
},
190191
{"platformHTTPPort",
191-
{"platformHTTPPort", RPCArg::Type::NUM, RPCArg::Optional::NO,
192-
"TCP port of Platform HTTP/API interface (network byte order)."}
192+
{"platformHTTPPort", RPCArg::Type::STR, RPCArg::Optional::NO,
193+
"Address in the form \"ADDR:PORT\" used by Platform for their HTTPS API.\n"
194+
"Must be unique on the network."}
193195
},
194196
};
195197

src/rpc/evo_util.cpp

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
#include <univalue.h>
1515

1616
namespace {
17+
bool IsNumeric(std::string_view input) { return input.find_first_not_of("0123456789") == std::string::npos; }
18+
1719
template <typename ProTx>
1820
void ParseInput(ProTx& ptx, std::string_view field_name, const std::string& input_str, NetInfoPurpose purpose,
1921
size_t idx, bool optional)
@@ -65,19 +67,44 @@ void ProcessNetInfoPlatform(ProTx& ptx, const UniValue& input_p2p, const UniValu
6567
{
6668
CHECK_NONFATAL(ptx.netInfo);
6769

68-
auto process_field = [](uint16_t& target, const UniValue& input, const std::string& field_name) {
70+
auto process_field = [&](uint16_t& maybe_target, const UniValue& input, const NetInfoPurpose purpose,
71+
std::string_view field_name) {
6972
if (!input.isNum() && !input.isStr()) {
70-
throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("Invalid param for %s, must be number", field_name));
73+
throw JSONRPCError(RPC_INVALID_PARAMETER,
74+
strprintf("Invalid param for %s, must be number or string", field_name));
7175
}
72-
if (int32_t port{ParseInt32V(input, field_name)}; port >= 1 && port <= std::numeric_limits<uint16_t>::max()) {
73-
target = static_cast<uint16_t>(port);
76+
77+
const auto& input_str{input.getValStr()};
78+
if (!IsNumeric(input_str)) {
79+
// Cannot be parsed as a number (port) so must be an addr:port string
80+
if (!ptx.netInfo->CanStorePlatform()) {
81+
throw JSONRPCError(RPC_INVALID_PARAMETER,
82+
strprintf("Invalid param for %s, ProTx version only supports ports", field_name));
83+
}
84+
ParseInput(ptx, field_name, input.get_str(), purpose, /*idx=*/0, /*optional=*/false);
7485
} else {
75-
throw JSONRPCError(RPC_INVALID_PARAMETER,
76-
strprintf("Invalid param for %s, must be a valid port [1-65535]", field_name));
86+
if (int32_t port{0}; ParseInt32(input_str, &port) && port >= 1 && port <= std::numeric_limits<uint16_t>::max()) {
87+
// Valid port
88+
if (!ptx.netInfo->CanStorePlatform()) {
89+
maybe_target = static_cast<uint16_t>(port);
90+
return; // Parsing complete
91+
}
92+
// We cannot store *only* a port number in netInfo so we need to associate it with the primary service of CORE_P2P manually
93+
if (!ptx.netInfo->HasEntries(NetInfoPurpose::CORE_P2P)) {
94+
throw JSONRPCError(RPC_INVALID_PARAMETER,
95+
strprintf("Cannot set param for %s, must specify coreP2PAddrs first", field_name));
96+
}
97+
const CService service{CNetAddr{ptx.netInfo->GetPrimary()}, static_cast<uint16_t>(port)};
98+
CHECK_NONFATAL(service.IsValid());
99+
ParseInput(ptx, field_name, service.ToStringAddrPort(), purpose, /*idx=*/0, /*optional=*/false);
100+
} else {
101+
throw JSONRPCError(RPC_INVALID_PARAMETER,
102+
strprintf("Invalid param for %s, must be a valid port [1-65535]", field_name));
103+
}
77104
}
78105
};
79-
process_field(ptx.platformP2PPort, input_p2p, "platformP2PPort");
80-
process_field(ptx.platformHTTPPort, input_http, "platformHTTPPort");
106+
process_field(ptx.platformP2PPort, input_p2p, NetInfoPurpose::PLATFORM_P2P, "platformP2PPort");
107+
process_field(ptx.platformHTTPPort, input_http, NetInfoPurpose::PLATFORM_HTTPS, "platformHTTPPort");
81108
}
82109
template void ProcessNetInfoPlatform(CProRegTx& ptx, const UniValue& input_p2p, const UniValue& input_http);
83110
template void ProcessNetInfoPlatform(CProUpServTx& ptx, const UniValue& input_p2p, const UniValue& input_http);

test/functional/rpc_netinfo.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,11 +204,11 @@ def test_validation_legacy(self):
204204

205205
# platformP2PPort and platformHTTPPort doesn't accept non-numeric inputs
206206
self.node_evo.register_mn(self, False, f"127.0.0.1:{self.node_evo.mn.nodePort}", f"127.0.0.1:{DEFAULT_PORT_PLATFORM_P2P}", DEFAULT_PORT_PLATFORM_HTTP,
207-
-8, f"platformP2PPort must be a 32bit integer (not '127.0.0.1:{DEFAULT_PORT_PLATFORM_P2P}')")
207+
-8, "Invalid param for platformP2PPort, ProTx version only supports ports")
208208
self.node_evo.register_mn(self, False, f"127.0.0.1:{self.node_evo.mn.nodePort}", [f"127.0.0.1:{DEFAULT_PORT_PLATFORM_P2P}"], DEFAULT_PORT_PLATFORM_HTTP,
209209
-8, "Invalid param for platformP2PPort, must be number")
210210
self.node_evo.register_mn(self, False, f"127.0.0.1:{self.node_evo.mn.nodePort}", DEFAULT_PORT_PLATFORM_P2P, f"127.0.0.1:{DEFAULT_PORT_PLATFORM_HTTP}",
211-
-8, f"platformHTTPPort must be a 32bit integer (not '127.0.0.1:{DEFAULT_PORT_PLATFORM_HTTP}')")
211+
-8, "Invalid param for platformHTTPPort, ProTx version only supports ports")
212212
self.node_evo.register_mn(self, False, f"127.0.0.1:{self.node_evo.mn.nodePort}", DEFAULT_PORT_PLATFORM_P2P, [f"127.0.0.1:{DEFAULT_PORT_PLATFORM_HTTP}"],
213213
-8, "Invalid param for platformHTTPPort, must be number")
214214

0 commit comments

Comments
 (0)