From 2d8f1244cf6905f8752f543b3cec72830b83ab1e Mon Sep 17 00:00:00 2001 From: Alexander Block Date: Fri, 31 Aug 2018 11:47:14 +0200 Subject: [PATCH] Implement 'masternode info ' RPC --- src/rpc/masternode.cpp | 91 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) diff --git a/src/rpc/masternode.cpp b/src/rpc/masternode.cpp index 23ac0788e2..34495b4be3 100644 --- a/src/rpc/masternode.cpp +++ b/src/rpc/masternode.cpp @@ -19,6 +19,10 @@ #include "rpc/server.h" #include "util.h" #include "utilmoneystr.h" +#include "txmempool.h" + +#include "evo/specialtx.h" +#include "evo/deterministicmns.h" #include #include @@ -573,6 +577,91 @@ UniValue masternode_genkey(const JSONRPCRequest& request) return CBitcoinSecret(secret).ToString(); } +void masternode_info_help() +{ + throw std::runtime_error( + "masternode info \"proTxHash\"\n" + "Print masternode information of specified masternode\n" + "\nArguments:\n" + "1. proTxHash (string, required) proTxHash of masternode\n" + ); +} + +UniValue masternode_info(const JSONRPCRequest& request) +{ + if(request.fHelp || request.params.size() != 2) + masternode_info_help(); + + std::string strProTxHash = request.params[1].get_str(); + if (!IsHex(strProTxHash) || strProTxHash.size() != 64) + throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid \"proTxHash\""); + + uint256 proTxHash; + proTxHash.SetHex(strProTxHash); + + CTransactionRef tx; + uint256 hashBlock; + bool fromMempool = false; + + auto dmn = deterministicMNManager->GetListAtChainTip().GetMN(proTxHash); + if (!dmn) { + tx = mempool.get(proTxHash); + if (tx) { + fromMempool = true; + if (tx->nVersion < 3 || tx->nType != TRANSACTION_PROVIDER_REGISTER) + throw JSONRPCError(RPC_INVALID_PARAMETER, "TX is not a ProTx"); + CProRegTx tmpProTx; + if (!GetTxPayload(*tx, tmpProTx)) + throw JSONRPCError(RPC_INVALID_PARAMETER, "TX is not a valid ProTx"); + dmn = std::make_shared(tx->GetHash(), tmpProTx); + } else { + throw JSONRPCError(RPC_INVALID_PARAMETER, "ProTx not found"); + } + } else { + if (!GetTransaction(proTxHash, tx, Params().GetConsensus(), hashBlock, true)) + throw JSONRPCError(RPC_INVALID_PARAMETER, "Parent transaction of ProTx not found"); + + if (!mapBlockIndex.count(hashBlock)) + throw JSONRPCError(RPC_INVALID_PARAMETER, "Parent transaction of ProTx not found"); + } + + UniValue obj(UniValue::VOBJ); + + UniValue stateObj; + dmn->pdmnState->ToJson(stateObj); + obj.push_back(Pair("state", stateObj)); + + if (!hashBlock.IsNull()) { + UniValue blockObj(UniValue::VOBJ); + blockObj.push_back(Pair("blockhash", hashBlock.GetHex())); + + LOCK(cs_main); + BlockMap::iterator mi = mapBlockIndex.find(hashBlock); + if (mi != mapBlockIndex.end() && (*mi).second) { + CBlockIndex *pindex = (*mi).second; + if (chainActive.Contains(pindex)) { + blockObj.push_back(Pair("height", pindex->nHeight)); + blockObj.push_back(Pair("confirmations", 1 + chainActive.Height() - pindex->nHeight)); + blockObj.push_back(Pair("time", pindex->GetBlockTime())); + blockObj.push_back(Pair("blocktime", pindex->GetBlockTime())); + } else { + blockObj.push_back(Pair("height", -1)); + blockObj.push_back(Pair("confirmations", 0)); + } + } + obj.push_back(Pair("block", blockObj)); + + if (GetUTXOHeight(COutPoint(proTxHash, dmn->nCollateralIndex)) < 0) { + obj.push_back(Pair("isSpent", true)); + } + + } else { + obj.push_back(Pair("fromMempool", true)); + } + + return obj; +} + void masternode_list_conf_help() { throw std::runtime_error( @@ -804,6 +893,8 @@ UniValue masternode(const JSONRPCRequest& request) #endif // ENABLE_WALLET } else if (strCommand == "genkey") { return masternode_genkey(request); + } else if (strCommand == "info") { + return masternode_info(request); } else if (strCommand == "list-conf") { return masternode_list_conf(request); #ifdef ENABLE_WALLET