Skip to content

Commit b584b9d

Browse files
committed
More efficient checking of mempool
1 parent f336c15 commit b584b9d

File tree

2 files changed

+42
-11
lines changed

2 files changed

+42
-11
lines changed

OP_RETURN.php

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -211,10 +211,21 @@ function OP_RETURN_retrieve($ref, $max_results=1, $testnet=false)
211211
$results=array();
212212

213213
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+
}
215221

216-
foreach ($txns as $txid => $txn_unpacked)
222+
foreach ($txids as $txid)
217223
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+
218229
$found=OP_RETURN_find_txn_data($txn_unpacked);
219230

220231
if (is_array($found)) {
@@ -231,15 +242,19 @@ function OP_RETURN_retrieve($ref, $max_results=1, $testnet=false)
231242
$key_heights=array($height => true);
232243

233244
if ($height==0)
234-
$try_heights=array(); // nothing more to do
245+
$try_heights=array(); // nowhere else to look if first still in mempool
235246
else {
236247
$result['ref']=OP_RETURN_calc_ref($height, $txid, array_keys($txns));
237248
$try_heights=OP_RETURN_get_try_heights($height+1, $max_height, false);
238249
}
239250

240251
// Collect the rest of the data, if appropriate
241252

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+
243258
$last_txid=$txid;
244259
$this_height=$height;
245260

@@ -269,7 +284,11 @@ function OP_RETURN_retrieve($ref, $max_results=1, $testnet=false)
269284
} else {
270285
if (count($try_heights)) {
271286
$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);
273292

274293
} else {
275294
$result['error']='Data incomplete - could not find next transaction';
@@ -382,15 +401,24 @@ function OP_RETURN_get_height_txns($height, $testnet)
382401
return OP_RETURN_get_block_txns($height, $testnet);
383402
}
384403

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+
385415
function OP_RETURN_get_mempool_txns($testnet)
386416
{
387-
$txids=OP_RETURN_bitcoin_cmd('getrawmempool', $testnet);
417+
$txids=OP_RETURN_list_mempool_txns($testnet);
418+
388419
$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);
394422

395423
return $txns;
396424
}

README.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,9 @@ As a library:
151151

152152
VERSION HISTORY
153153
---------------
154+
v2.0.1 - 14 May 2015
155+
* More efficient checking of mempool for the first transaction in the chain
156+
154157
v2.0 - 12 May 2015
155158
* Added functions for general storage and retrieval of data in the blockchain
156159
* Now uses Bitcoin Core JSON-RPC API (bitcoin-cli still an option), so supports Windows

0 commit comments

Comments
 (0)