@@ -64,12 +64,15 @@ void TxToJSON(const CTransaction& tx, const uint256 hashBlock, UniValue& entry)
64
64
65
65
UniValue getrawtransaction (const JSONRPCRequest& request)
66
66
{
67
- if (request.fHelp || request.params .size () < 1 || request.params .size () > 2 )
67
+ if (request.fHelp || request.params .size () < 1 || request.params .size () > 3 )
68
68
throw std::runtime_error (
69
- " getrawtransaction \" txid\" ( verbose )\n "
69
+ " getrawtransaction \" txid\" ( verbose \" blockhash \" )\n "
70
70
71
71
" \n NOTE: By default this function only works for mempool transactions. If the -txindex option is\n "
72
- " enabled, it also works for blockchain transactions.\n "
72
+ " enabled, it also works for blockchain transactions. If the block which contains the transaction\n "
73
+ " is known, its hash can be provided even for nodes without -txindex. Note that if a blockhash is\n "
74
+ " provided, only that block will be searched and if the transaction is in the mempool or other\n "
75
+ " blocks, or if this node does not have the given block available, the transaction will not be found.\n "
73
76
" DEPRECATED: for now, it also works for transactions with unspent outputs.\n "
74
77
75
78
" \n Return the raw transaction data.\n "
@@ -78,13 +81,15 @@ UniValue getrawtransaction(const JSONRPCRequest& request)
78
81
79
82
" \n Arguments:\n "
80
83
" 1. \" txid\" (string, required) The transaction id\n "
81
- " 2. verbose (bool, optional, default=false) If false, return a string, otherwise return a json object\n "
84
+ " 2. verbose (bool, optional, default=false) If false, return a string, otherwise return a json object\n "
85
+ " 3. \" blockhash\" (string, optional) The block in which to look for the transaction\n "
82
86
83
87
" \n Result (if verbose is not set or set to false):\n "
84
88
" \" data\" (string) The serialized, hex-encoded data for 'txid'\n "
85
89
86
90
" \n Result (if verbose is set to true):\n "
87
91
" {\n "
92
+ " \" in_active_chain\" : b, (bool) Whether specified block is in the active chain or not (only present with explicit \" blockhash\" argument)\n "
88
93
" \" hex\" : \" data\" , (string) The serialized, hex-encoded data for 'txid'\n "
89
94
" \" txid\" : \" id\" , (string) The transaction id (same as provided)\n "
90
95
" \" hash\" : \" id\" , (string) The transaction hash (differs from txid for witness transactions)\n "
@@ -132,30 +137,58 @@ UniValue getrawtransaction(const JSONRPCRequest& request)
132
137
+ HelpExampleCli (" getrawtransaction" , " \" mytxid\" " )
133
138
+ HelpExampleCli (" getrawtransaction" , " \" mytxid\" true" )
134
139
+ HelpExampleRpc (" getrawtransaction" , " \" mytxid\" , true" )
140
+ + HelpExampleCli (" getrawtransaction" , " \" mytxid\" false \" myblockhash\" " )
141
+ + HelpExampleCli (" getrawtransaction" , " \" mytxid\" true \" myblockhash\" " )
135
142
);
136
143
137
144
LOCK (cs_main);
138
145
146
+ bool in_active_chain = true ;
139
147
uint256 hash = ParseHashV (request.params [0 ], " parameter 1" );
148
+ CBlockIndex* blockindex = nullptr ;
140
149
141
150
// Accept either a bool (true) or a num (>=1) to indicate verbose output.
142
151
bool fVerbose = false ;
143
152
if (!request.params [1 ].isNull ()) {
144
153
fVerbose = request.params [1 ].isNum () ? (request.params [1 ].get_int () != 0 ) : request.params [1 ].get_bool ();
145
154
}
146
155
156
+ if (!request.params [2 ].isNull ()) {
157
+ uint256 blockhash = ParseHashV (request.params [2 ], " parameter 3" );
158
+ if (!blockhash.IsNull ()) {
159
+ BlockMap::iterator it = mapBlockIndex.find (blockhash);
160
+ if (it == mapBlockIndex.end ()) {
161
+ throw JSONRPCError (RPC_INVALID_ADDRESS_OR_KEY, " Block hash not found" );
162
+ }
163
+ blockindex = it->second ;
164
+ in_active_chain = chainActive.Contains (blockindex);
165
+ }
166
+ }
167
+
147
168
CTransactionRef tx;
148
- uint256 hashBlock;
149
- if (!GetTransaction (hash, tx, Params ().GetConsensus (), hashBlock, true ))
150
- throw JSONRPCError (RPC_INVALID_ADDRESS_OR_KEY, std::string (fTxIndex ? " No such mempool or blockchain transaction"
151
- : " No such mempool transaction. Use -txindex to enable blockchain transaction queries" ) +
152
- " . Use gettransaction for wallet transactions." );
169
+ uint256 hash_block;
170
+ if (!GetTransaction (hash, tx, Params ().GetConsensus (), hash_block, true , blockindex)) {
171
+ std::string errmsg;
172
+ if (blockindex) {
173
+ if (!(blockindex->nStatus & BLOCK_HAVE_DATA)) {
174
+ throw JSONRPCError (RPC_MISC_ERROR, " Block not available" );
175
+ }
176
+ errmsg = " No such transaction found in the provided block" ;
177
+ } else {
178
+ errmsg = fTxIndex
179
+ ? " No such mempool or blockchain transaction"
180
+ : " No such mempool transaction. Use -txindex to enable blockchain transaction queries" ;
181
+ }
182
+ throw JSONRPCError (RPC_INVALID_ADDRESS_OR_KEY, errmsg + " . Use gettransaction for wallet transactions." );
183
+ }
153
184
154
- if (!fVerbose )
185
+ if (!fVerbose ) {
155
186
return EncodeHexTx (*tx, RPCSerializationFlags ());
187
+ }
156
188
157
189
UniValue result (UniValue::VOBJ);
158
- TxToJSON (*tx, hashBlock, result);
190
+ if (blockindex) result.push_back (Pair (" in_active_chain" , in_active_chain));
191
+ TxToJSON (*tx, hash_block, result);
159
192
return result;
160
193
}
161
194
@@ -983,7 +1016,7 @@ UniValue sendrawtransaction(const JSONRPCRequest& request)
983
1016
static const CRPCCommand commands[] =
984
1017
{ // category name actor (function) argNames
985
1018
// --------------------- ------------------------ ----------------------- ----------
986
- { " rawtransactions" , " getrawtransaction" , &getrawtransaction, {" txid" ," verbose" } },
1019
+ { " rawtransactions" , " getrawtransaction" , &getrawtransaction, {" txid" ," verbose" , " blockhash " } },
987
1020
{ " rawtransactions" , " createrawtransaction" , &createrawtransaction, {" inputs" ," outputs" ," locktime" ," replaceable" } },
988
1021
{ " rawtransactions" , " decoderawtransaction" , &decoderawtransaction, {" hexstring" } },
989
1022
{ " rawtransactions" , " decodescript" , &decodescript, {" hexstring" } },
0 commit comments