Skip to content

Commit 02620a5

Browse files
committed
Merge pull request #352 from braydonf/mem
Optimized address service mempool index size
2 parents cda3259 + 0ea035c commit 02620a5

File tree

12 files changed

+1280
-261
lines changed

12 files changed

+1280
-261
lines changed

docs/services/bitcoind.md

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,11 @@ node.services.bitcoind.on('tip', function(blockHash) {
101101
});
102102

103103
node.services.bitcoind.on('tx', function(txInfo) {
104-
// a new transaction has been broadcast in the network
104+
// a new transaction has entered the mempool
105+
});
106+
107+
node.services.bitcoind.on('txleave', function(txLeaveInfo) {
108+
// a new transaction has left the mempool
105109
});
106110
```
107111
@@ -110,7 +114,16 @@ The `txInfo` object will have the format:
110114
```js
111115
{
112116
buffer: <Buffer...>,
113-
mempool: true,
117+
mempool: true, // will currently always be true
118+
hash: '7426c707d0e9705bdd8158e60983e37d0f5d63529086d6672b07d9238d5aa623'
119+
}
120+
```
121+
122+
The `txLeaveInfo` object will have the format:
123+
124+
```js
125+
{
126+
buffer: <Buffer...>,
114127
hash: '7426c707d0e9705bdd8158e60983e37d0f5d63529086d6672b07d9238d5aa623'
115128
}
116129
```

docs/services/db.md

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,6 @@ CustomService.prototype.blockHandler = function(block, add, callback) {
1919

2020
Take a look at the Address Service implementation for more details about how to encode the key, value for the best efficiency and ways to format the keys for streaming reads.
2121

22-
Additionally the mempool can have an index, the mempool index will be updated once bitcoind and the db have both fully synced. A service can implement a `resetMempoolIndex` method that will be run during this time, and the "synced" event will wait until this task has been finished:
23-
24-
```js
25-
CustomService.prototype.resetMempoolIndex = function(callback) {
26-
var transactionBuffers = this.node.services.bitcoind.getMempoolTransactions();
27-
// interact over the transactions asynchronously here
28-
callback();
29-
};
30-
```
31-
3222
## API Documentation
3323
These methods are exposed over the JSON-RPC interface and can be called directly from a node via:
3424

etc/bitcoin.patch

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -367,14 +367,27 @@ index f94771a..72ee00e 100644
367367

368368

369369
diff --git a/src/net.h b/src/net.h
370-
index 17502b9..e181d68 100644
370+
index 17502b9..c9ae1b2 100644
371371
--- a/src/net.h
372372
+++ b/src/net.h
373-
@@ -99,6 +99,7 @@ struct CNodeSignals
373+
@@ -99,6 +99,8 @@ struct CNodeSignals
374374
{
375375
boost::signals2::signal<int ()> GetHeight;
376376
boost::signals2::signal<bool (CNode*), CombinerAll> ProcessMessages;
377377
+ boost::signals2::signal<bool (const CTransaction&)> TxToMemPool;
378+
+ boost::signals2::signal<bool (const CTransaction&)> TxLeaveMemPool;
378379
boost::signals2::signal<bool (CNode*, bool), CombinerAll> SendMessages;
379380
boost::signals2::signal<void (NodeId, const CNode*)> InitializeNode;
380381
boost::signals2::signal<void (NodeId)> FinalizeNode;
382+
diff --git a/src/txmempool.cpp b/src/txmempool.cpp
383+
index c3d1b60..03e265d 100644
384+
--- a/src/txmempool.cpp
385+
+++ b/src/txmempool.cpp
386+
@@ -133,6 +133,7 @@ void CTxMemPool::remove(const CTransaction &origTx, std::list<CTransaction>& rem
387+
if (!mapTx.count(hash))
388+
continue;
389+
const CTransaction& tx = mapTx[hash].GetTx();
390+
+ GetNodeSignals().TxLeaveMemPool(tx);
391+
if (fRecursive) {
392+
for (unsigned int i = 0; i < tx.vout.size(); i++) {
393+
std::map<COutPoint, CInPoint>::iterator it = mapNextTx.find(COutPoint(hash, i));

integration/regtest-node.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -726,12 +726,15 @@ describe('Node Functionality', function() {
726726
node.services.bitcoind.sendTransaction(tx.serialize());
727727

728728
setImmediate(function() {
729-
var length = node.services.address.mempoolOutputIndex[address].length;
730-
length.should.equal(1);
731-
should.exist(node.services.address.mempoolOutputIndex[address]);
732-
done();
729+
var hashBuffer = bitcore.Address(address).hashBuffer;
730+
node.services.address._getOutputsMempool(address, hashBuffer, function(err, outs) {
731+
if (err) {
732+
throw err;
733+
}
734+
outs.length.should.equal(1);
735+
done();
736+
});
733737
});
734-
735738
});
736739

737740
});

integration/regtest.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,30 @@ describe('Daemon Binding Functionality', function() {
360360
});
361361
});
362362

363+
describe('transactions leaving the mempool', function() {
364+
it('receive event when transaction leaves', function(done) {
365+
366+
// add transaction to build a new block
367+
var tx = bitcore.Transaction();
368+
tx.from(utxos[4]);
369+
tx.change(privateKey.toAddress());
370+
tx.to(destKey.toAddress(), utxos[4].amount * 1e8 - 1000);
371+
tx.sign(bitcore.PrivateKey.fromWIF(utxos[4].privateKeyWIF));
372+
bitcoind.sendTransaction(tx.serialize());
373+
374+
bitcoind.once('txleave', function(txInfo) {
375+
txInfo.hash.should.equal(tx.hash);
376+
done();
377+
});
378+
379+
client.generate(1, function(err, response) {
380+
if (err) {
381+
throw err;
382+
}
383+
});
384+
});
385+
});
386+
363387
describe('mempool functionality', function() {
364388

365389
var fromAddress = 'mszYqVnqKoQx4jcTdJXxwKAissE3Jbrrc1';

0 commit comments

Comments
 (0)