From b3ed8430573adc0c989d8021e7a8b9f634b0d584 Mon Sep 17 00:00:00 2001 From: Chris Kleeschulte Date: Tue, 18 Jul 2017 09:47:28 -0400 Subject: [PATCH] wip --- lib/services/mempool/index.js | 15 ++------ test/services/mempool/index.unit.js | 53 +++++++++++++++++++++++++++-- 2 files changed, 53 insertions(+), 15 deletions(-) diff --git a/lib/services/mempool/index.js b/lib/services/mempool/index.js index 9d46e803..a29f608f 100644 --- a/lib/services/mempool/index.js +++ b/lib/services/mempool/index.js @@ -8,7 +8,6 @@ var log = index.log; var MempoolService = function(options) { BaseService.call(this, options); this._db = this.node.services.db; - this._tx = this.node.services.transaction; }; util.inherits(MempoolService, BaseService); @@ -65,18 +64,8 @@ MempoolService.prototype._onBlock = function(block) { }; MempoolService.prototype._onTransaction = function(tx) { - var txOps = this._getTxOperation(tx); - this._db.batch(txOps); -}; - -MempoolService.prototype._getTxOperations = function(tx) { - var inputValues = this._tx.getInputValues(tx); - tx.__inputValues = inputValues; - return { - type: 'put', - key: this._encoding.encodeMempoolTransactionKey(tx.id), - value: this._encoding.encodeMempoolTransactionValue(tx) - }; + this._db.put(this._encoding.encodeMempoolTransactionKey(tx.id), + this._encoding.encodeMempoolTransactionValue(tx)); }; MempoolService.prototype.getTransaction = function(txid, callback) { diff --git a/test/services/mempool/index.unit.js b/test/services/mempool/index.unit.js index c9108062..7b664eae 100644 --- a/test/services/mempool/index.unit.js +++ b/test/services/mempool/index.unit.js @@ -39,12 +39,61 @@ describe('Mempool Service', function() { done(); }); }); + }); - describe('#getTransaction', function(done) { - var get = sandbox.stub().callsArgWith(1, null, tx.toJSON()); + describe('#stop', function() { + it('should stop the service', function(done) { + mempoolService.stop(function() { + done(); + }); }); }); + describe('#getTransaction', function() { + + it('should get a transaction', function(done) { + var get = sandbox.stub().callsArgWith(1, null, tx.toJSON()); + var key = sandbox.stub(); + mempoolService._encoding = { encodeMempoolTransactionKey: key }; + mempoolService._db = { get: get }; + mempoolService.getTransaction(tx.hash, function(err, mytx) { + if(err) { + return done(err); + } + expect(mytx).to.deep.equal(tx.toJSON()); + done(); + }); + }); + + }); + + describe('#_onTransaction', function() { + + it('should add the transaction to the database', function() { + var key = sandbox.stub(); + var value = sandbox.stub(); + mempoolService._encoding = { + encodeMempoolTransactionKey: key, + encodeMempoolTransactionValue: value + }; + var put = sandbox.stub(); + mempoolService._db = { put: put }; + mempoolService._onTransaction({}); + expect(key.calledOnce).to.be.true; + expect(value.calledOnce).to.be.true; + expect(put.calledOnce).to.be.true; + }); + }); + + describe('#_onBlock', function() { + it('should remove block\'s txs from database', function() { + var batch = sandbox.stub(); + mempoolService._db = { batch: batch }; + var block = { transactions: [ { id: 1 }, { id: 2 } ] }; + mempoolService._onBlock(block); + expect(batch.calledOnce).to.be.true; + }); + }); });