From b82a21c5ac969efc4317d01ec1e505d40a1922de Mon Sep 17 00:00:00 2001 From: mxaddict Date: Sat, 10 Aug 2024 17:15:29 +0800 Subject: [PATCH] Updated how the errors are handled for RPC in staker --- src/navio-staker.cpp | 202 +++++++++++++++++++++++-------------------- 1 file changed, 106 insertions(+), 96 deletions(-) diff --git a/src/navio-staker.cpp b/src/navio-staker.cpp index 15704a54a14ed8..a367f43336f994 100644 --- a/src/navio-staker.cpp +++ b/src/navio-staker.cpp @@ -425,7 +425,7 @@ static void ParseError(const UniValue& error, std::string& strPrint, int& nRet) } else { strPrint = "error: " + error.write(); } - nRet = abs(error["code"].getInt()); + nRet = error["code"].getInt(); } static std::string rpcPass; @@ -522,126 +522,120 @@ bool TestSetup() try { UniValue reply = ConnectAndCallRPC(rh.get(), "listwallets", /* args=*/{}); - - // Parse reply - const UniValue& error = reply.find_value("error"); + UniValue error = reply.find_value("error"); std::string strError; int nRet; - if (error.isNull()) { - LogPrintf("%s: [%s] Test connection to RPC: OK\n", __func__, walletName); + if (!error.isNull()) { + ParseError(error, strError, nRet); + LogPrintf("%s: [%s] Could not connect to RPC node: (%s)\n", __func__, walletName, strError); + return false; + } - reply = ConnectAndCallRPC(rh.get(), "loadwallet", /* args=*/{walletName}); + LogPrintf("%s: [%s] Test connection to RPC: OK\n", __func__, walletName); - const UniValue& error = reply.find_value("error"); + reply = ConnectAndCallRPC(rh.get(), "loadwallet", /* args=*/{walletName}); + error = reply.find_value("error"); - strError.clear(); - nRet = 0; + strError.clear(); + nRet = 0; - if (!error.isNull()) { - ParseError(error, strError, nRet); - } + if (!error.isNull()) { + ParseError(error, strError, nRet); + } - if (error.isNull() || nRet == 35) { - LogPrintf("%s: [%s] Test load wallet: OK\n", __func__, walletName); + if (nRet != RPC_WALLET_ALREADY_LOADED) { + LogPrintf("%s: [%s] Could not load wallet (%s)\n", __func__, walletName, strError); + return false; + } - reply = ConnectAndCallRPC(rh.get(), "getwalletinfo", /* args=*/{}, walletName); + LogPrintf("%s: [%s] Test load wallet: OK\n", __func__, walletName); - const UniValue& result = reply.find_value("result"); - const UniValue& error = reply.find_value("error"); + reply = ConnectAndCallRPC(rh.get(), "getwalletinfo", /* args=*/{}, walletName); + UniValue result = reply.find_value("result"); + error = reply.find_value("error"); - strError.clear(); - nRet = 0; + strError.clear(); + nRet = 0; - if (!error.isNull()) { - ParseError(error, strError, nRet); - } + if (!error.isNull()) { + ParseError(error, strError, nRet); + LogPrintf("%s: [%s] Could not get wallet info (%s)\n", __func__, walletName, strError); + return false; + } - if (error.isNull()) { - if (!result["blsct"].get_bool()) { - LogPrintf("%s: [%s] Wallet is not of type blsct\n", __func__, walletName); - return false; - } + if (!result["blsct"].get_bool()) { + LogPrintf("%s: [%s] Wallet is not of type blsct\n", __func__, walletName); + return false; + } - if (!result["unlocked_until"].isNull() && result["unlocked_until"].get_real() == 0) { - LogPrintf("%s: [%s] Wallet is locked. Testing password.\n", __func__, walletName); + if (!result["unlocked_until"].isNull() && result["unlocked_until"].get_real() == 0) { + LogPrintf("%s: [%s] Wallet is locked. Testing password.\n", __func__, walletName); - mustUnlockWallet = true; + mustUnlockWallet = true; - reply = ConnectAndCallRPC(rh.get(), "walletpassphrase", /* args=*/{walletPassphrase, "1"}, walletName); + reply = ConnectAndCallRPC(rh.get(), "walletpassphrase", /* args=*/{walletPassphrase, "1"}, walletName); - const UniValue& error = reply.find_value("error"); + const UniValue& error = reply.find_value("error"); - strError.clear(); - nRet = 0; + strError.clear(); + nRet = 0; - if (error.isNull()) { - LogPrintf("%s: [%s] Wallet passphrase test: OK\n", __func__, walletName); - } else { - ParseError(error, strError, nRet); - LogPrintf("%s: [%s] Could not unlock wallet (%s)\n", __func__, walletName, strError); + if (error.isNull()) { + LogPrintf("%s: [%s] Wallet passphrase test: OK\n", __func__, walletName); + } else { + ParseError(error, strError, nRet); + LogPrintf("%s: [%s] Could not unlock wallet (%s)\n", __func__, walletName, strError); - return false; - } - } + return false; + } + } - if (coinbase_dest == "") { - reply = ConnectAndCallRPC(rh.get(), "getaddressesbylabel", /* args=*/{"Staking"}, walletName); + if (coinbase_dest == "") { + reply = ConnectAndCallRPC(rh.get(), "getaddressesbylabel", /* args=*/{"Staking"}, walletName); - const UniValue& result = reply.find_value("result"); - const UniValue& error = reply.find_value("error"); + const UniValue& result = reply.find_value("result"); + const UniValue& error = reply.find_value("error"); - if (error.isNull() && result.isObject()) { - const UniValue& array = result.get_obj(); + if (error.isNull() && result.isObject()) { + const UniValue& array = result.get_obj(); - for (auto& it : array.getKeys()) { - const UniValue& obj = array.find_value(it); + for (auto& it : array.getKeys()) { + const UniValue& obj = array.find_value(it); - if (obj.isObject()) { - if (obj.get_obj().find_value("purpose").get_str() == "receive") { - coinbase_dest = it; - break; - } - } - } + if (obj.isObject()) { + if (obj.get_obj().find_value("purpose").get_str() == "receive") { + coinbase_dest = it; + break; } + } + } + } - if (coinbase_dest == "") { - reply = ConnectAndCallRPC(rh.get(), "getnewaddress", /* args=*/{"Staking", "blsct"}, walletName); - - const UniValue& result = reply.find_value("result"); - const UniValue& error = reply.find_value("error"); - - strError.clear(); - nRet = 0; - - if (error.isNull() || !result.isStr()) { - coinbase_dest = result.get_str(); - } else { - ParseError(error, strError, nRet); - LogPrintf("%s: [%s] Could not get an address for rewards from wallet (%s)\n", __func__, walletName, strError); + if (coinbase_dest == "") { + reply = ConnectAndCallRPC(rh.get(), "getnewaddress", /* args=*/{"Staking", "blsct"}, walletName); - return false; - } - } - } + const UniValue& result = reply.find_value("result"); + const UniValue& error = reply.find_value("error"); - LogPrintf("%s: [%s] Rewards address: (%s)\n", __func__, walletName, coinbase_dest); + strError.clear(); + nRet = 0; - return true; + if (error.isNull() || !result.isStr()) { + coinbase_dest = result.get_str(); } else { - LogPrintf("%s: [%s] Could not get wallet info (%s)\n", __func__, walletName, strError); + ParseError(error, strError, nRet); + LogPrintf("%s: [%s] Could not get an address for rewards from wallet (%s)\n", __func__, walletName, strError); + return false; } - } else { - LogPrintf("%s: [%s] Could not load wallet (%s)\n", __func__, walletName, strError); - return false; } - } else { - LogPrintf("%s: [%s] Could not connect to RPC node: (%s)\n", __func__, walletName, error.getValStr()); - return false; } + + LogPrintf("%s: [%s] Rewards address: (%s)\n", __func__, walletName, coinbase_dest); + + return true; } catch (const std::exception& e) { LogPrintf("%s: [%s] error: (%s)\n", __func__, walletName, e.what()); return false; @@ -715,9 +709,33 @@ std::string EncodeHexBlock(const CBlock& block) std::vector GetStakedCommitments(const std::unique_ptr& rh) { - const UniValue& reply_staked = ConnectAndCallRPC(rh.get(), "liststakedcommitments", /* args=*/{}, walletName); + UniValue response = ConnectAndCallRPC(rh.get(), "loadwallet", /* args=*/{walletName}); + UniValue error = response.find_value("error"); + UniValue result = response.find_value("result"); + + std::string strError; + auto nRet = 0; + + if (!error.isNull()) { + ParseError(error, strError, nRet); + if (nRet != RPC_WALLET_ALREADY_LOADED) { + LogPrintf("%s: [%s] Could not load wallet (%s)\n", __func__, walletName, strError); + return std::vector {}; + } + } - const UniValue& result = reply_staked.find_value("result"); + response = ConnectAndCallRPC(rh.get(), "liststakedcommitments", /* args=*/{}, walletName); + error = response.find_value("error"); + result = response.find_value("result"); + + strError.clear(); + nRet = 0; + + if (!error.isNull()) { + ParseError(error, strError, nRet); + LogPrintf("%s: [%s] Could not load stake commitments (%s)\n", __func__, walletName, strError); + return std::vector {}; + } return UniValueArrayToStakedCommitmentsMine(result.get_array()); } @@ -785,15 +803,7 @@ void Loop() LogPrintf("%s: [%s] Starting staking...\n", __func__, walletName); while (true) { - std::vector staked_commitments; - try { - staked_commitments = GetStakedCommitments(rh); - } catch (const std::exception& e) { - LogPrintf("%s: [%s] Could not load stake commitments, reloading wallet...\n", __func__, walletName); - ConnectAndCallRPC(rh.get(), "loadwallet", /* args=*/{walletName}); - staked_commitments = GetStakedCommitments(rh); - } - + auto staked_commitments = GetStakedCommitments(rh); CBlock proposal; CAmount nTotalMoney = 0; bool found = false;