Skip to content

Commit 86bec48

Browse files
committed
refactor: rearrange checks in ProTx RPC wrapper to bail out faster
1 parent 14b7cf3 commit 86bec48

File tree

1 file changed

+20
-18
lines changed

1 file changed

+20
-18
lines changed

src/rpc/evo.cpp

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -655,6 +655,7 @@ static UniValue protx_register_common_wrapper(const JSONRPCRequest& request,
655655

656656
CProRegTx ptx;
657657
ptx.nType = mnType;
658+
ptx.nVersion = CProRegTx::GetMaxVersion(/*is_basic_scheme_active=*/!use_legacy);
658659

659660
if (action == ProTxRegisterAction::Fund) {
660661
CTxDestination collateralDest = DecodeDestination(request.params[paramIdx].get_str());
@@ -689,7 +690,6 @@ static UniValue protx_register_common_wrapper(const JSONRPCRequest& request,
689690

690691
ptx.keyIDOwner = ParsePubKeyIDFromAddress(request.params[paramIdx + 1].get_str(), "owner address");
691692
ptx.pubKeyOperator.Set(ParseBLSPubKey(request.params[paramIdx + 2].get_str(), "operator BLS address", use_legacy), use_legacy);
692-
ptx.nVersion = CProRegTx::GetMaxVersion(/*is_basic_scheme_active=*/!use_legacy);
693693
CHECK_NONFATAL(ptx.pubKeyOperator.IsLegacy() == (ptx.nVersion == ProTxVersion::LegacyBLS));
694694

695695
CKeyID keyIDVoting = ptx.keyIDOwner;
@@ -963,8 +963,18 @@ static UniValue protx_update_service_common_wrapper(const JSONRPCRequest& reques
963963
EnsureWalletIsUnlocked(*wallet);
964964

965965
CProUpServTx ptx;
966-
ptx.nType = mnType;
967966
ptx.proTxHash = ParseHashV(request.params[0], "proTxHash");
967+
auto dmn = dmnman.GetListAtChainTip().GetMN(ptx.proTxHash);
968+
if (!dmn) {
969+
throw std::runtime_error(strprintf("masternode with proTxHash %s not found", ptx.proTxHash.ToString()));
970+
}
971+
972+
ptx.nType = mnType;
973+
if (dmn->nType != mnType) {
974+
throw std::runtime_error(strprintf("masternode with proTxHash %s is not a %s", ptx.proTxHash.ToString(), GetMnType(mnType).description));
975+
}
976+
977+
ptx.nVersion = dmn->pdmnState->nVersion;
968978

969979
if (auto addr = Lookup(request.params[1].get_str().c_str(), Params().GetDefaultPort(), false); addr.has_value()) {
970980
ptx.addr = addr.value();
@@ -996,15 +1006,6 @@ static UniValue protx_update_service_common_wrapper(const JSONRPCRequest& reques
9961006
paramIdx += 3;
9971007
}
9981008

999-
auto dmn = dmnman.GetListAtChainTip().GetMN(ptx.proTxHash);
1000-
if (!dmn) {
1001-
throw std::runtime_error(strprintf("masternode with proTxHash %s not found", ptx.proTxHash.ToString()));
1002-
}
1003-
if (dmn->nType != mnType) {
1004-
throw std::runtime_error(strprintf("masternode with proTxHash %s is not a %s", ptx.proTxHash.ToString(), GetMnType(mnType).description));
1005-
}
1006-
ptx.nVersion = dmn->pdmnState->nVersion;
1007-
10081009
if (keyOperator.GetPublicKey() != dmn->pdmnState->pubKeyOperator.Get()) {
10091010
throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("the operator key does not belong to the registered public key"));
10101011
}
@@ -1094,12 +1095,14 @@ static RPCHelpMan protx_update_registrar_wrapper(const bool specific_legacy_bls_
10941095
EnsureWalletIsUnlocked(*wallet);
10951096

10961097
CProUpRegTx ptx;
1097-
ptx.proTxHash = ParseHashV(request.params[0], "proTxHash");
1098+
ptx.nVersion = CProUpRegTx::GetMaxVersion(/*is_basic_scheme_active=*/!use_legacy);
10981099

1100+
ptx.proTxHash = ParseHashV(request.params[0], "proTxHash");
10991101
auto dmn = dmnman.GetListAtChainTip().GetMN(ptx.proTxHash);
11001102
if (!dmn) {
11011103
throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("masternode %s not found", ptx.proTxHash.ToString()));
11021104
}
1105+
11031106
ptx.keyIDVoting = dmn->pdmnState->keyIDVoting;
11041107
ptx.scriptPayout = dmn->pdmnState->scriptPayout;
11051108

@@ -1111,7 +1114,6 @@ static RPCHelpMan protx_update_registrar_wrapper(const bool specific_legacy_bls_
11111114
ptx.pubKeyOperator = dmn->pdmnState->pubKeyOperator;
11121115
}
11131116

1114-
ptx.nVersion = CProUpRegTx::GetMaxVersion(/*is_basic_scheme_active=*/!use_legacy);
11151117
CHECK_NONFATAL(ptx.pubKeyOperator.IsLegacy() == (ptx.nVersion == ProTxVersion::LegacyBLS));
11161118

11171119
if (request.params[2].get_str() != "") {
@@ -1205,7 +1207,12 @@ static RPCHelpMan protx_revoke()
12051207
const bool isV19active{DeploymentActiveAfter(WITH_LOCK(cs_main, return chainman.ActiveChain().Tip();), Params().GetConsensus(), Consensus::DEPLOYMENT_V19)};
12061208
CProUpRevTx ptx;
12071209
ptx.nVersion = CProUpRevTx::GetMaxVersion(/*is_basic_scheme_active=*/isV19active);
1210+
12081211
ptx.proTxHash = ParseHashV(request.params[0], "proTxHash");
1212+
auto dmn = dmnman.GetListAtChainTip().GetMN(ptx.proTxHash);
1213+
if (!dmn) {
1214+
throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("masternode %s not found", ptx.proTxHash.ToString()));
1215+
}
12091216

12101217
CBLSSecretKey keyOperator = ParseBLSSecretKey(request.params[1].get_str(), "operatorKey");
12111218

@@ -1217,11 +1224,6 @@ static RPCHelpMan protx_revoke()
12171224
ptx.nReason = (uint16_t)nReason;
12181225
}
12191226

1220-
auto dmn = dmnman.GetListAtChainTip().GetMN(ptx.proTxHash);
1221-
if (!dmn) {
1222-
throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("masternode %s not found", ptx.proTxHash.ToString()));
1223-
}
1224-
12251227
if (keyOperator.GetPublicKey() != dmn->pdmnState->pubKeyOperator.Get()) {
12261228
throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("the operator key does not belong to the registered public key"));
12271229
}

0 commit comments

Comments
 (0)