Skip to content

Commit 7be0671

Browse files
committed
rpc/rawtx: Use existing NodeContext
Also pass in appropriate object to: - TxToJSON
1 parent 60dc05a commit 7be0671

File tree

1 file changed

+19
-16
lines changed

1 file changed

+19
-16
lines changed

src/rpc/rawtransaction.cpp

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040

4141
#include <univalue.h>
4242

43-
static void TxToJSON(const CTransaction& tx, const uint256 hashBlock, UniValue& entry)
43+
static void TxToJSON(const CTransaction& tx, const uint256 hashBlock, UniValue& entry, CChainState& active_chainstate)
4444
{
4545
// Call into TxToUniv() in bitcoin-common to decode the transaction hex.
4646
//
@@ -53,10 +53,10 @@ static void TxToJSON(const CTransaction& tx, const uint256 hashBlock, UniValue&
5353
LOCK(cs_main);
5454

5555
entry.pushKV("blockhash", hashBlock.GetHex());
56-
CBlockIndex* pindex = g_chainman.m_blockman.LookupBlockIndex(hashBlock);
56+
CBlockIndex* pindex = active_chainstate.m_blockman.LookupBlockIndex(hashBlock);
5757
if (pindex) {
58-
if (::ChainActive().Contains(pindex)) {
59-
entry.pushKV("confirmations", 1 + ::ChainActive().Height() - pindex->nHeight);
58+
if (active_chainstate.m_chain.Contains(pindex)) {
59+
entry.pushKV("confirmations", 1 + active_chainstate.m_chain.Height() - pindex->nHeight);
6060
entry.pushKV("time", pindex->GetBlockTime());
6161
entry.pushKV("blocktime", pindex->GetBlockTime());
6262
}
@@ -158,6 +158,7 @@ static RPCHelpMan getrawtransaction()
158158
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
159159
{
160160
const NodeContext& node = EnsureNodeContext(request.context);
161+
ChainstateManager& chainman = EnsureChainman(request.context);
161162

162163
bool in_active_chain = true;
163164
uint256 hash = ParseHashV(request.params[0], "parameter 1");
@@ -178,11 +179,11 @@ static RPCHelpMan getrawtransaction()
178179
LOCK(cs_main);
179180

180181
uint256 blockhash = ParseHashV(request.params[2], "parameter 3");
181-
blockindex = g_chainman.m_blockman.LookupBlockIndex(blockhash);
182+
blockindex = chainman.m_blockman.LookupBlockIndex(blockhash);
182183
if (!blockindex) {
183184
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Block hash not found");
184185
}
185-
in_active_chain = ::ChainActive().Contains(blockindex);
186+
in_active_chain = chainman.ActiveChain().Contains(blockindex);
186187
}
187188

188189
bool f_txindex_ready = false;
@@ -215,7 +216,7 @@ static RPCHelpMan getrawtransaction()
215216

216217
UniValue result(UniValue::VOBJ);
217218
if (blockindex) result.pushKV("in_active_chain", in_active_chain);
218-
TxToJSON(*tx, hash_block, result);
219+
TxToJSON(*tx, hash_block, result, chainman.ActiveChainstate());
219220
return result;
220221
},
221222
};
@@ -257,10 +258,11 @@ static RPCHelpMan gettxoutproof()
257258

258259
CBlockIndex* pblockindex = nullptr;
259260
uint256 hashBlock;
261+
ChainstateManager& chainman = EnsureChainman(request.context);
260262
if (!request.params[1].isNull()) {
261263
LOCK(cs_main);
262264
hashBlock = ParseHashV(request.params[1], "blockhash");
263-
pblockindex = g_chainman.m_blockman.LookupBlockIndex(hashBlock);
265+
pblockindex = chainman.m_blockman.LookupBlockIndex(hashBlock);
264266
if (!pblockindex) {
265267
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Block not found");
266268
}
@@ -269,9 +271,9 @@ static RPCHelpMan gettxoutproof()
269271

270272
// Loop through txids and try to find which block they're in. Exit loop once a block is found.
271273
for (const auto& tx : setTxids) {
272-
const Coin& coin = AccessByTxid(::ChainstateActive().CoinsTip(), tx);
274+
const Coin& coin = AccessByTxid(chainman.ActiveChainstate().CoinsTip(), tx);
273275
if (!coin.IsSpent()) {
274-
pblockindex = ::ChainActive()[coin.nHeight];
276+
pblockindex = chainman.ActiveChain()[coin.nHeight];
275277
break;
276278
}
277279
}
@@ -290,7 +292,7 @@ static RPCHelpMan gettxoutproof()
290292
if (!tx || hashBlock.IsNull()) {
291293
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Transaction not yet in block");
292294
}
293-
pblockindex = g_chainman.m_blockman.LookupBlockIndex(hashBlock);
295+
pblockindex = chainman.m_blockman.LookupBlockIndex(hashBlock);
294296
if (!pblockindex) {
295297
throw JSONRPCError(RPC_INTERNAL_ERROR, "Transaction index corrupt");
296298
}
@@ -350,8 +352,9 @@ static RPCHelpMan verifytxoutproof()
350352

351353
LOCK(cs_main);
352354

353-
const CBlockIndex* pindex = g_chainman.m_blockman.LookupBlockIndex(merkleBlock.header.GetHash());
354-
if (!pindex || !::ChainActive().Contains(pindex) || pindex->nTx == 0) {
355+
ChainstateManager& chainman = EnsureChainman(request.context);
356+
const CBlockIndex* pindex = chainman.m_blockman.LookupBlockIndex(merkleBlock.header.GetHash());
357+
if (!pindex || !chainman.ActiveChain().Contains(pindex) || pindex->nTx == 0) {
355358
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Block not found in chain");
356359
}
357360

@@ -678,7 +681,7 @@ static RPCHelpMan combinerawtransaction()
678681
const CTxMemPool& mempool = EnsureMemPool(request.context);
679682
LOCK(cs_main);
680683
LOCK(mempool.cs);
681-
CCoinsViewCache &viewChain = ::ChainstateActive().CoinsTip();
684+
CCoinsViewCache &viewChain = EnsureChainman(request.context).ActiveChainstate().CoinsTip();
682685
CCoinsViewMemPool viewMempool(&viewChain, mempool);
683686
view.SetBackend(viewMempool); // temporarily switch cache backend to db+mempool view
684687

@@ -949,7 +952,7 @@ static RPCHelpMan testmempoolaccept()
949952
result_0.pushKV("txid", tx->GetHash().GetHex());
950953
result_0.pushKV("wtxid", tx->GetWitnessHash().GetHex());
951954

952-
const MempoolAcceptResult accept_result = WITH_LOCK(cs_main, return AcceptToMemoryPool(::ChainstateActive(), mempool, std::move(tx),
955+
const MempoolAcceptResult accept_result = WITH_LOCK(cs_main, return AcceptToMemoryPool(EnsureChainman(request.context).ActiveChainstate(), mempool, std::move(tx),
953956
false /* bypass_limits */, /* test_accept */ true));
954957

955958
// Only return the fee and vsize if the transaction would pass ATMP.
@@ -1600,7 +1603,7 @@ static RPCHelpMan utxoupdatepsbt()
16001603
{
16011604
const CTxMemPool& mempool = EnsureMemPool(request.context);
16021605
LOCK2(cs_main, mempool.cs);
1603-
CCoinsViewCache &viewChain = ::ChainstateActive().CoinsTip();
1606+
CCoinsViewCache &viewChain = EnsureChainman(request.context).ActiveChainstate().CoinsTip();
16041607
CCoinsViewMemPool viewMempool(&viewChain, mempool);
16051608
view.SetBackend(viewMempool); // temporarily switch cache backend to db+mempool view
16061609

0 commit comments

Comments
 (0)