Skip to content
This repository has been archived by the owner on Jun 11, 2024. It is now read-only.

Fix transactionInPool bug - Closes #707 #821

Merged
merged 8 commits into from
Oct 6, 2017
Merged
8 changes: 5 additions & 3 deletions logic/transactionPool.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,9 @@ TransactionPool.prototype.transactionInPool = function (id) {
self.bundled.index[id],
self.queued.index[id],
self.multisignature.index[id]
].filter(Boolean).length > 0;
].filter(function (inList) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return inList !== undefined;
}).length > 0;
};

/**
Expand Down Expand Up @@ -314,7 +316,7 @@ TransactionPool.prototype.addQueuedTransaction = function (transaction) {
};

/**
* Removes id from queued index and transactions.
* Removes id from queued index and transactions.
* @param {string} id
*/
TransactionPool.prototype.removeQueuedTransaction = function (id) {
Expand Down Expand Up @@ -347,7 +349,7 @@ TransactionPool.prototype.addMultisignatureTransaction = function (transaction)
};

/**
* Removes id from multisignature index and transactions.
* Removes id from multisignature index and transactions.
* @param {string} id
*/
TransactionPool.prototype.removeMultisignatureTransaction = function (id) {
Expand Down
1 change: 1 addition & 0 deletions test/unit/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ require('./logic/peer');
require('./logic/peers');
require('./logic/multisignature');
require('./logic/transaction');
require('./logic/transactionPool');
require('./logic/transfer');
require('./logic/vote');

Expand Down
102 changes: 102 additions & 0 deletions test/unit/logic/transactionPool.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
'use strict';

var async = require('async');
var expect = require('chai').expect;
var sinon = require('sinon');

var node = require('../../node');

var jobsQueue = require('../../../helpers/jobsQueue');
var TransactionPool = require('../../../logic/transactionPool');
var TransactionLogic = require('../../../logic/transaction');
var TransferLogic = require('../../../logic/transfer');
var modulesLoader = require('../../common/initModule').modulesLoader;
var transactionTypes = require('../../../helpers/transactionTypes');

describe('txPool', function () {

var txPool;
var jobsQueueRegisterStub;

before(function (done) {
// Init transaction logic
modulesLoader.initLogic(TransactionLogic, modulesLoader.scope, function (err, __trsLogic) {
expect(err).to.not.exist;
txPool = new TransactionPool(
modulesLoader.scope.config.broadcasts.broadcastInterval,
modulesLoader.scope.config.broadcasts.releaseLimit,
__trsLogic,
modulesLoader.scope.bus,
modulesLoader.scope.logger
);

modulesLoader.initModules([
{accounts: require('../../../modules/accounts')},
], [
{'transaction': require('../../../logic/transaction')},
{'account': require('../../../logic/account')}
], {}, function (err, __modules) {
expect(err).to.not.exist;

txPool.bind(
__modules.accounts,
null,
__modules.loader
);
__trsLogic.attachAssetType(transactionTypes.SEND, new TransferLogic(modulesLoader.scope.logger, modulesLoader.scope.schema));
done();
});
});
});

beforeEach(function () {
jobsQueueRegisterStub = sinon.stub(jobsQueue, 'register');
});

afterEach(function () {
jobsQueueRegisterStub.restore();
});

describe('receiveTransactions', function () {

it('should return empty array when using empty array', function (done) {
txPool.receiveTransactions([], false, function (err, data) {
expect(err).to.not.exist;
expect(data).to.be.an('array').that.is.empty;
done();
});
});

it('should return error when using empty object', function (done) {
txPool.receiveTransactions([{}], false, function (err, data) {
expect(err).to.be.equal('Invalid public key');
done();
});
});

it('should return error when using invalid tx', function (done) {
txPool.receiveTransactions([{ id: '123' }], false, function (err, data) {
expect(err).to.exist;
done();
});
});

it('should process tx if valid and insert tx into queue', function (done) {
var account = node.randomAccount();
const tx = node.lisk.transaction.createTransaction(account.address, 100000000000, node.gAccount.password);

txPool.receiveTransactions([tx], false, function (err, data) {
expect(err).to.not.exist;
expect(txPool.transactionInPool(tx.id)).to.be.true;
done();
});
});
});

describe('transactionInPool', function () {

it('should return false for an unknown id', function () {
expect(txPool.transactionInPool('11111')).to.be.false;
});
});
});