Skip to content

Commit 1f42ffe

Browse files
committed
suggestions
1 parent 58d820a commit 1f42ffe

File tree

2 files changed

+63
-67
lines changed

2 files changed

+63
-67
lines changed

src/rpc/client.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ static const CRPCConvertParam vRPCConvertParams[] =
107107
{ "gettransaction", 1, "include_watchonly" },
108108
{ "gettransaction", 2, "verbose" },
109109
{ "getrawtransaction", 1, "verbose" },
110-
{ "gettransactionsarelocked", 0, "txids" },
110+
{ "getttxchainlocks", 0, "txids" },
111111
{ "createrawtransaction", 0, "inputs" },
112112
{ "createrawtransaction", 1, "outputs" },
113113
{ "createrawtransaction", 2, "locktime" },

src/rpc/rawtransaction.cpp

Lines changed: 62 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -50,39 +50,6 @@
5050

5151
#include <univalue.h>
5252

53-
static void TxLockStatusToUniv(const CTransaction* const tx, const uint256 hashBlock, CChainState& active_chainstate, llmq::CChainLocksHandler& clhandler, llmq::CInstantSendManager& isman, UniValue& entry, bool detailed = true)
54-
{
55-
bool chainLock = false;
56-
int height = -1;
57-
if (!hashBlock.IsNull() && tx != nullptr) {
58-
LOCK(cs_main);
59-
60-
if (detailed) entry.pushKV("blockhash", hashBlock.GetHex());
61-
CBlockIndex* pindex = active_chainstate.m_blockman.LookupBlockIndex(hashBlock);
62-
if (pindex) {
63-
if (active_chainstate.m_chain.Contains(pindex)) {
64-
height = pindex->nHeight;
65-
if (detailed) {
66-
entry.pushKV("confirmations", 1 + active_chainstate.m_chain.Height() - pindex->nHeight);
67-
entry.pushKV("time", pindex->GetBlockTime());
68-
entry.pushKV("blocktime", pindex->GetBlockTime());
69-
}
70-
chainLock = clhandler.HasChainLock(pindex->nHeight, pindex->GetBlockHash());
71-
} else {
72-
if (detailed) entry.pushKV("confirmations", 0);
73-
}
74-
}
75-
}
76-
77-
if (detailed) {
78-
bool fLocked = isman.IsLocked(tx->GetHash());
79-
entry.pushKV("instantlock", fLocked || chainLock);
80-
entry.pushKV("instantlock_internal", fLocked);
81-
}
82-
entry.pushKV("height", height);
83-
entry.pushKV("chainlock", chainLock);
84-
}
85-
8653
void TxToJSON(const CTransaction& tx, const uint256 hashBlock, CTxMemPool& mempool, CChainState& active_chainstate, llmq::CChainLocksHandler& clhandler, llmq::CInstantSendManager& isman, UniValue& entry)
8754
{
8855
// Call into TxToUniv() in bitcoin-common to decode the transaction hex.
@@ -113,7 +80,31 @@ void TxToJSON(const CTransaction& tx, const uint256 hashBlock, CTxMemPool& mempo
11380
}
11481

11582
TxToUniv(tx, uint256(), entry, true, &txSpentInfo);
116-
TxLockStatusToUniv(&tx, hashBlock, active_chainstate, clhandler, isman, entry);
83+
84+
bool chainLock = false;
85+
if (!hashBlock.IsNull()) {
86+
LOCK(cs_main);
87+
88+
entry.pushKV("blockhash", hashBlock.GetHex());
89+
CBlockIndex* pindex = active_chainstate.m_blockman.LookupBlockIndex(hashBlock);
90+
if (pindex) {
91+
if (active_chainstate.m_chain.Contains(pindex)) {
92+
entry.pushKV("height", pindex->nHeight);
93+
entry.pushKV("confirmations", 1 + active_chainstate.m_chain.Height() - pindex->nHeight);
94+
entry.pushKV("time", pindex->GetBlockTime());
95+
entry.pushKV("blocktime", pindex->GetBlockTime());
96+
chainLock = clhandler.HasChainLock(pindex->nHeight, pindex->GetBlockHash());
97+
} else {
98+
entry.pushKV("height", -1);
99+
entry.pushKV("confirmations", 0);
100+
}
101+
}
102+
}
103+
104+
bool fLocked = isman.IsLocked(txid);
105+
entry.pushKV("instantlock", fLocked || chainLock);
106+
entry.pushKV("instantlock_internal", fLocked);
107+
entry.pushKV("chainlock", chainLock);
117108
}
118109

119110
static UniValue getrawtransaction(const JSONRPCRequest& request)
@@ -269,12 +260,12 @@ static UniValue getrawtransaction(const JSONRPCRequest& request)
269260
TxToJSON(*tx, hash_block, mempool, chainman.ActiveChainstate(), *llmq_ctx.clhandler, *llmq_ctx.isman, result);
270261
return result;
271262
}
272-
static UniValue gettransactionsarelocked(const JSONRPCRequest& request)
263+
264+
static UniValue getttxchainlocks(const JSONRPCRequest& request)
273265
{
274266
RPCHelpMan{
275-
"gettransactionsarelocked",
276-
"\nReturn the raw transactions data.\n"
277-
"\nHint: Use gettransaction for wallet transactions.\n",
267+
"getttxchainlocks",
268+
"\nReturns a block height each transaction was mined at and whether it is chainlocked or not.\n",
278269
{
279270
{"txids", RPCArg::Type::ARR, RPCArg::Optional::NO, "The transaction ids (no more than 100)",
280271
{
@@ -283,51 +274,56 @@ static UniValue gettransactionsarelocked(const JSONRPCRequest& request)
283274
},
284275
},
285276
RPCResult{
286-
RPCResult::Type::ARR, "", "Response is an array with the same size as the input txids",
277+
RPCResult::Type::ARR, "", "Response is an array with the same size as the input txids",
278+
{
279+
{RPCResult::Type::OBJ, "", "",
287280
{
288-
{RPCResult::Type::OBJ, "", "",
289-
{
290-
{RPCResult::Type::STR_HEX, "txid", "The transaction hash"},
291-
{RPCResult::Type::BOOL, "instantlock", "Current transaction lock state"},
292-
{RPCResult::Type::BOOL, "chainlock", "he state of the corresponding block chainlock"},
293-
{RPCResult::Type::NUM, "height", "The block height"},
294-
}},
295-
}
281+
{RPCResult::Type::NUM, "height", "The block height"},
282+
{RPCResult::Type::BOOL, "chainlock", "Chainlock status for the block containing the transaction"},
283+
}},
284+
}
296285
},
297286
RPCExamples{
298-
HelpExampleCli("gettransactionsarelocked", "\"mytxids\"")
299-
+ HelpExampleCli("gettransactionsarelocked", "\"[tx1, tx2, ...]\"")
287+
HelpExampleCli("getttxchainlocks", "'[\"mytxid\",...]'")
288+
+ HelpExampleRpc("getttxchainlocks", "[\"mytxid\",...]")
300289
},
301290
}.Check(request);
302291

303292
const NodeContext& node = EnsureAnyNodeContext(request.context);
304-
ChainstateManager& chainman = EnsureChainman(node);
305-
306-
const CBlockIndex* blockindex = nullptr;
307-
308-
bool f_txindex_ready = false;
309-
if (g_txindex && !blockindex) {
310-
f_txindex_ready = g_txindex->BlockUntilSyncedToCurrentChain();
311-
}
312-
313-
LLMQContext& llmq_ctx = EnsureLLMQContext(node);
293+
const LLMQContext& llmq_ctx = EnsureLLMQContext(node);
294+
const ChainstateManager& chainman = EnsureChainman(node);
295+
const CChainState& active_chainstate = chainman.ActiveChainstate();
314296

315297
UniValue result_arr(UniValue::VARR);
316298
UniValue txids = request.params[0].get_array();
317-
for (size_t idx = 0; idx < std::min<size_t>(100, txids.size()); ++idx) {
299+
if (txids.size() > 100) {
300+
throw JSONRPCError(RPC_INVALID_PARAMETER, "Up to 100 txids only");
301+
}
302+
303+
for (size_t idx = 0; idx < txids.size(); ++idx) {
318304
UniValue result(UniValue::VOBJ);
319-
const UniValue& txid = txids[idx];
320-
uint256 hash(ParseHashV(txid, "txid"));
321-
if (hash == Params().GenesisBlock().hashMerkleRoot) {
305+
uint256 txid(ParseHashV(txids[idx], "txid"));
306+
if (txid == Params().GenesisBlock().hashMerkleRoot) {
322307
// Special exception for the genesis block coinbase transaction
323308
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "The genesis block coinbase is not considered an ordinary transaction and cannot be retrieved");
324309
}
325310

326311
uint256 hash_block;
327-
CTransactionRef tx = GetTransaction(nullptr, nullptr, hash, Params().GetConsensus(), hash_block);
312+
int height = -1;
313+
bool chainLock = false;
328314

329-
TxLockStatusToUniv(tx.get(), hash_block, chainman.ActiveChainstate(), *llmq_ctx.clhandler, *llmq_ctx.isman, result, false);
315+
GetTransaction(nullptr, nullptr, txid, Params().GetConsensus(), hash_block);
330316

317+
if (!hash_block.IsNull()) {
318+
LOCK(cs_main);
319+
CBlockIndex* pindex = active_chainstate.m_blockman.LookupBlockIndex(hash_block);
320+
if (pindex && active_chainstate.m_chain.Contains(pindex)) {
321+
height = pindex->nHeight;
322+
chainLock = llmq_ctx.clhandler->HasChainLock(pindex->nHeight, pindex->GetBlockHash());
323+
}
324+
}
325+
result.pushKV("height", height);
326+
result.pushKV("chainlock", chainLock);
331327
result_arr.push_back(result);
332328
}
333329
return result_arr;
@@ -1755,7 +1751,7 @@ static const CRPCCommand commands[] =
17551751
{ // category name actor (function) argNames
17561752
// --------------------- ------------------------ ----------------------- ----------
17571753
{ "rawtransactions", "getrawtransaction", &getrawtransaction, {"txid","verbose","blockhash"} },
1758-
{ "rawtransactions", "gettransactionsarelocked", &gettransactionsarelocked, {"txids"} },
1754+
{ "rawtransactions", "getttxchainlocks", &getttxchainlocks, {"txids"} },
17591755
{ "rawtransactions", "createrawtransaction", &createrawtransaction, {"inputs","outputs","locktime"} },
17601756
{ "rawtransactions", "decoderawtransaction", &decoderawtransaction, {"hexstring"} },
17611757
{ "rawtransactions", "decodescript", &decodescript, {"hexstring"} },

0 commit comments

Comments
 (0)