@@ -48,6 +48,8 @@ void TxToJSON(const CTransaction& tx, const uint256 hashBlock, UniValue& entry)
48
48
TxToUniv (tx, uint256 (), entry, true , RPCSerializationFlags ());
49
49
50
50
if (!hashBlock.IsNull ()) {
51
+ LOCK (cs_main);
52
+
51
53
entry.pushKV (" blockhash" , hashBlock.GetHex ());
52
54
CBlockIndex* pindex = LookupBlockIndex (hashBlock);
53
55
if (pindex) {
@@ -142,8 +144,6 @@ UniValue getrawtransaction(const JSONRPCRequest& request)
142
144
+ HelpExampleCli (" getrawtransaction" , " \" mytxid\" true \" myblockhash\" " )
143
145
);
144
146
145
- LOCK (cs_main);
146
-
147
147
bool in_active_chain = true ;
148
148
uint256 hash = ParseHashV (request.params [0 ], " parameter 1" );
149
149
CBlockIndex* blockindex = nullptr ;
@@ -160,6 +160,8 @@ UniValue getrawtransaction(const JSONRPCRequest& request)
160
160
}
161
161
162
162
if (!request.params [2 ].isNull ()) {
163
+ LOCK (cs_main);
164
+
163
165
uint256 blockhash = ParseHashV (request.params [2 ], " parameter 3" );
164
166
blockindex = LookupBlockIndex (blockhash);
165
167
if (!blockindex) {
@@ -168,6 +170,11 @@ UniValue getrawtransaction(const JSONRPCRequest& request)
168
170
in_active_chain = chainActive.Contains (blockindex);
169
171
}
170
172
173
+ bool f_txindex_ready = false ;
174
+ if (g_txindex && !blockindex) {
175
+ f_txindex_ready = g_txindex->BlockUntilSyncedToCurrentChain ();
176
+ }
177
+
171
178
CTransactionRef tx;
172
179
uint256 hash_block;
173
180
if (!GetTransaction (hash, tx, Params ().GetConsensus (), hash_block, true , blockindex)) {
@@ -179,6 +186,8 @@ UniValue getrawtransaction(const JSONRPCRequest& request)
179
186
errmsg = " No such transaction found in the provided block" ;
180
187
} else if (!g_txindex) {
181
188
errmsg = " No such mempool transaction. Use -txindex to enable blockchain transaction queries" ;
189
+ } else if (!f_txindex_ready) {
190
+ errmsg = " No such mempool transaction. Blockchain transactions are still in the process of being indexed" ;
182
191
} else {
183
192
errmsg = " No such mempool or blockchain transaction" ;
184
193
}
@@ -230,19 +239,18 @@ UniValue gettxoutproof(const JSONRPCRequest& request)
230
239
oneTxid = hash;
231
240
}
232
241
233
- LOCK (cs_main);
234
-
235
242
CBlockIndex* pblockindex = nullptr ;
236
-
237
243
uint256 hashBlock;
238
- if (!request.params [1 ].isNull ())
239
- {
244
+ if (!request.params [1 ].isNull ()) {
245
+ LOCK (cs_main);
240
246
hashBlock = uint256S (request.params [1 ].get_str ());
241
247
pblockindex = LookupBlockIndex (hashBlock);
242
248
if (!pblockindex) {
243
249
throw JSONRPCError (RPC_INVALID_ADDRESS_OR_KEY, " Block not found" );
244
250
}
245
251
} else {
252
+ LOCK (cs_main);
253
+
246
254
// Loop through txids and try to find which block they're in. Exit loop once a block is found.
247
255
for (const auto & tx : setTxids) {
248
256
const Coin& coin = AccessByTxid (*pcoinsTip, tx);
@@ -253,6 +261,14 @@ UniValue gettxoutproof(const JSONRPCRequest& request)
253
261
}
254
262
}
255
263
264
+
265
+ // Allow txindex to catch up if we need to query it and before we acquire cs_main.
266
+ if (g_txindex && !pblockindex) {
267
+ g_txindex->BlockUntilSyncedToCurrentChain ();
268
+ }
269
+
270
+ LOCK (cs_main);
271
+
256
272
if (pblockindex == nullptr )
257
273
{
258
274
CTransactionRef tx;
0 commit comments