@@ -211,10 +211,21 @@ function OP_RETURN_retrieve($ref, $max_results=1, $testnet=false)
211
211
$ results =array ();
212
212
213
213
foreach ($ heights as $ height ) {
214
- $ txns =OP_RETURN_get_height_txns ($ height , $ testnet ); // gets from mempool if $height==0
214
+ if ($ height ==0 ) {
215
+ $ txids =OP_RETURN_list_mempool_txns ($ testnet ); // if mempool, only get list for now (to save RPC calls)
216
+ $ txns =null ;
217
+ } else {
218
+ $ txns =OP_RETURN_get_block_txns ($ height , $ testnet ); // if block, get all fully unpacked
219
+ $ txids =array_keys ($ txns );
220
+ }
215
221
216
- foreach ($ txns as $ txid => $ txn_unpacked )
222
+ foreach ($ txids as $ txid )
217
223
if (OP_RETURN_match_ref_txid ($ ref , $ txid )) {
224
+ if ($ height ==0 )
225
+ $ txn_unpacked =OP_RETURN_get_mempool_txn ($ txid , $ testnet );
226
+ else
227
+ $ txn_unpacked =$ txns [$ txid ];
228
+
218
229
$ found =OP_RETURN_find_txn_data ($ txn_unpacked );
219
230
220
231
if (is_array ($ found )) {
@@ -231,15 +242,19 @@ function OP_RETURN_retrieve($ref, $max_results=1, $testnet=false)
231
242
$ key_heights =array ($ height => true );
232
243
233
244
if ($ height ==0 )
234
- $ try_heights =array (); // nothing more to do
245
+ $ try_heights =array (); // nowhere else to look if first still in mempool
235
246
else {
236
247
$ result ['ref ' ]=OP_RETURN_calc_ref ($ height , $ txid , array_keys ($ txns ));
237
248
$ try_heights =OP_RETURN_get_try_heights ($ height +1 , $ max_height , false );
238
249
}
239
250
240
251
// Collect the rest of the data, if appropriate
241
252
242
- $ this_txns =$ txns ;
253
+ if ($ height ==0 )
254
+ $ this_txns =OP_RETURN_get_mempool_txns ($ testnet ); // now retrieve all to follow chain
255
+ else
256
+ $ this_txns =$ txns ;
257
+
243
258
$ last_txid =$ txid ;
244
259
$ this_height =$ height ;
245
260
@@ -269,7 +284,11 @@ function OP_RETURN_retrieve($ref, $max_results=1, $testnet=false)
269
284
} else {
270
285
if (count ($ try_heights )) {
271
286
$ this_height =array_shift ($ try_heights );
272
- $ this_txns =OP_RETURN_get_height_txns ($ this_height , $ testnet );
287
+
288
+ if ($ this_height ==0 )
289
+ $ this_txns =OP_RETURN_get_mempool_txns ($ testnet );
290
+ else
291
+ $ this_txns =OP_RETURN_get_block_txns ($ this_height , $ testnet );
273
292
274
293
} else {
275
294
$ result ['error ' ]='Data incomplete - could not find next transaction ' ;
@@ -382,15 +401,24 @@ function OP_RETURN_get_height_txns($height, $testnet)
382
401
return OP_RETURN_get_block_txns ($ height , $ testnet );
383
402
}
384
403
404
+ function OP_RETURN_list_mempool_txns ($ testnet )
405
+ {
406
+ return OP_RETURN_bitcoin_cmd ('getrawmempool ' , $ testnet );
407
+ }
408
+
409
+ function OP_RETURN_get_mempool_txn ($ txid , $ testnet )
410
+ {
411
+ $ raw_txn =OP_RETURN_bitcoin_cmd ('getrawtransaction ' , $ testnet , $ txid );
412
+ return OP_RETURN_unpack_txn (pack ('H* ' , $ raw_txn ));
413
+ }
414
+
385
415
function OP_RETURN_get_mempool_txns ($ testnet )
386
416
{
387
- $ txids =OP_RETURN_bitcoin_cmd ('getrawmempool ' , $ testnet );
417
+ $ txids =OP_RETURN_list_mempool_txns ($ testnet );
418
+
388
419
$ txns =array ();
389
-
390
- foreach ($ txids as $ txid ) {
391
- $ raw_txn =OP_RETURN_bitcoin_cmd ('getrawtransaction ' , $ testnet , $ txid );
392
- $ txns [$ txid ]=OP_RETURN_unpack_txn (pack ('H* ' , $ raw_txn ));
393
- }
420
+ foreach ($ txids as $ txid )
421
+ $ txns [$ txid ]=OP_RETURN_get_mempool_txn ($ txid , $ testnet );
394
422
395
423
return $ txns ;
396
424
}
0 commit comments