This commit is contained in:
Chris Kleeschulte 2017-07-18 09:47:28 -04:00
parent 17bbfcc6ce
commit b3ed843057
2 changed files with 53 additions and 15 deletions

View File

@ -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) {

View File

@ -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;
});
});
});