Skip to content

Commit

Permalink
Updated how the errors are handled for RPC in staker
Browse files Browse the repository at this point in the history
  • Loading branch information
mxaddict committed Aug 10, 2024
1 parent cc48ad3 commit b82a21c
Showing 1 changed file with 106 additions and 96 deletions.
202 changes: 106 additions & 96 deletions src/navio-staker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<int>());
nRet = error["code"].getInt<int>();
}

static std::string rpcPass;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -715,9 +709,33 @@ std::string EncodeHexBlock(const CBlock& block)

std::vector<StakedCommitment> GetStakedCommitments(const std::unique_ptr<BaseRequestHandler>& 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<StakedCommitment> {};
}
}

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<StakedCommitment> {};
}

return UniValueArrayToStakedCommitmentsMine(result.get_array());
}
Expand Down Expand Up @@ -785,15 +803,7 @@ void Loop()
LogPrintf("%s: [%s] Starting staking...\n", __func__, walletName);

while (true) {
std::vector<StakedCommitment> 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;
Expand Down

0 comments on commit b82a21c

Please sign in to comment.