Skip to content

Commit

Permalink
Updated decodecustomtx RPC as per new requirements
Browse files Browse the repository at this point in the history
  • Loading branch information
surangap committed Apr 27, 2021
1 parent 45f061c commit 1b94660
Showing 1 changed file with 18 additions and 83 deletions.
101 changes: 18 additions & 83 deletions src/masternodes/rpc_tokens.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -738,8 +738,7 @@ UniValue minttokens(const JSONRPCRequest& request) {
UniValue decodecustomtx(const JSONRPCRequest& request)
{
RPCHelpMan{"decodecustomtx",
"\nGet detailed information about a DeFiChain custom transaction.\n"
"can be enabled to return details for any transaction.",
"\nGet detailed information about a DeFiChain custom transaction.\n",
{
{"hexstring", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, "The transaction hex string"},
{"iswitness", RPCArg::Type::BOOL, /* default */ "depends on heuristic tests", "Whether the transaction hex is a serialized witness transaction.\n"
Expand All @@ -752,13 +751,10 @@ UniValue decodecustomtx(const JSONRPCRequest& request)
},
RPCResult{
"{\n"
" \"txid\": (string) The transaction id.\n"
" \"type\": (string) The transaction type.\n"
" \"valid\" (bool) Whether the transaction was valid.\n"
" \"results\" (json object) Set of results related to the transaction type\n"
" \"block height\" (string) The block height containing the transaction.\n"
" \"blockhash\" (string) The block hash containing the transaction.\n"
" \"confirmations\": n, (numeric) The number of confirmations for the transaction."
" \"warnings\": (string) any network and blockchain warnings."
"}\n"
},
RPCExamples{
Expand Down Expand Up @@ -790,98 +786,37 @@ UniValue decodecustomtx(const JSONRPCRequest& request)

if (tx)
{
// Search wallet if available
std::shared_ptr<CWallet> const wallet = GetWalletForJSONRPCRequest(request);
if (wallet) {
LOCK(wallet->cs_wallet);
if (auto wtx = wallet->GetWalletTx(tx->GetHash()))
{
hashBlock = wtx->hashBlock;
}
}

// No wallet or not a wallet TX, try mempool, txindex
if (!wallet || hashBlock.IsNull())
{
bool f_txindex_ready{false};
if (g_txindex) {
f_txindex_ready = g_txindex->BlockUntilSyncedToCurrentChain();
}

//get block hash details
if (!GetBlockHash(tx->GetHash(), Params().GetConsensus(), hashBlock, blockindex)) {
if (!g_txindex) {
warnings = "Failed to find the relavant block. Use -txindex";
} else if (!f_txindex_ready) {
warnings = "Failed to find the relavant block. Transactions are still in the process of being indexed.";
} else {
warnings = "Failed to find the relavant block";
}
}
}

LOCK(cs_main);
// Found a block hash but no block index yet
if (!hashBlock.IsNull() && !blockindex) {
blockindex = LookupBlockIndex(hashBlock);
}

// Default to next block height
nHeight = ::ChainActive().Height() + 1;

// Get actual height if blockindex avaiable
if (blockindex) {
nHeight = blockindex->nHeight;
actualHeight = true;
}

// Skip coinbase TXs except for genesis block
if ((tx->IsCoinBase() && nHeight > 0)) {
return "Coinbase transaction. Not a custom transaction.";
}

if (!GetCustomTXInfo(nHeight, tx, guess, res, txResults)) {
//get custom tx info. We pass nHeight as (::ChainActive().Height() + 1),
//just to get over hardfork validations. txResults are based on transaction metadata.
res = RpcInfo(*tx, nHeight, guess, txResults);
if (guess == CustomTxType::None) {
return "Not a custom transaction";
}

} else {
// Should not get here
return "Could not decode the input transaction hexstring.";
}

UniValue result(UniValue::VOBJ);

result.pushKV("type", ToString(guess));
if (!actualHeight) {
result.pushKV("valid", res.ok ? true : false);
} else {
auto undo = pcustomcsview->GetUndo(UndoKey{static_cast<uint32_t>(nHeight), tx->GetHash()});
result.pushKV("valid", undo || res.msg == "AutoAuth" ? true : false);
}
UniValue result(UniValue::VOBJ);
result.pushKV("txid", tx->GetHash().GetHex());
result.pushKV("type", ToString(guess));
result.pushKV("valid", res.ok ? true : false); // no actual block height

if (!res.ok) {
result.pushKV("error", res.msg);
} else {
result.pushKV("results", txResults);
}

if (!hashBlock.IsNull()) {
LOCK(cs_main);

result.pushKV("blockhash", hashBlock.GetHex());
if (blockindex) {
result.pushKV("blockHeight", blockindex->nHeight);
result.pushKV("blockTime", blockindex->GetBlockTime());
result.pushKV("confirmations", 1 + ::ChainActive().Height() - blockindex->nHeight);
if (!res.ok) {
result.pushKV("error", res.msg);
} else {
result.pushKV("confirmations", 0);
result.pushKV("results", txResults);
}
}
else{
result.pushKV("warnings", warnings);
}

return result;
return result;
} else {
// Should not get here without prior failure.
return "Could not decode the input transaction hexstring.";
}
}

static const CRPCCommand commands[] =
Expand Down

0 comments on commit 1b94660

Please sign in to comment.