Skip to content

Commit

Permalink
🐛 Fix applying penalty on transaction processing error
Browse files Browse the repository at this point in the history
  • Loading branch information
shuse2 committed Nov 18, 2019
1 parent 8353f6d commit b2c1191
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 9 deletions.
26 changes: 26 additions & 0 deletions framework/src/modules/chain/transport/errors.js
@@ -0,0 +1,26 @@
/*
* Copyright © 2019 Lisk Foundation
*
* See the LICENSE file at the top-level directory of this distribution
* for licensing information.
*
* Unless otherwise agreed in a custom licensing agreement with the Lisk Foundation,
* no part of this software, including this file, may be copied, modified,
* propagated, or distributed except according to the terms contained in the
* LICENSE file.
*
* Removal or modification of this copyright notice is prohibited.
*/

'use strict';

class InvalidTransactionError extends Error {
constructor(message, id) {
super(message);
this.id = id;
}
}

module.exports = {
InvalidTransactionError,
};
17 changes: 10 additions & 7 deletions framework/src/modules/chain/transport/transport.js
Expand Up @@ -17,6 +17,7 @@
const { TransactionError } = require('@liskhq/lisk-transactions');
const { validator } = require('@liskhq/lisk-validator');
const { convertErrorsToString } = require('../utils/error_handlers');
const { InvalidTransactionError } = require('./errors');
const Broadcaster = require('./broadcaster');
const schemas = require('./schemas');

Expand Down Expand Up @@ -502,10 +503,12 @@ class Transport {
}
} catch (err) {
this.logger.warn({ err, peerId }, 'Received invalid transactions.');
await this.channel.invoke('network:applyPenalty', {
peerId,
penalty: 100,
});
if (err instanceof InvalidTransactionError) {
await this.channel.invoke('network:applyPenalty', {
peerId,
penalty: 100,
});
}
}
}

Expand Down Expand Up @@ -572,16 +575,16 @@ class Transport {
}
} catch (errors) {
const errString = convertErrorsToString(errors);
const err = new InvalidTransactionError(errString, id);
this.logger.error(
{
id,
err: errString,
err,
module: 'transport',
},
'Transaction normalization failed',
);

throw errors;
throw err;
}

this.logger.debug({ id: transaction.id }, 'Received transaction');
Expand Down
Expand Up @@ -44,6 +44,7 @@ describe('Transport', () => {
loggerStub = {
info: jest.fn(),
warn: jest.fn(),
error: jest.fn(),
debug: jest.fn(),
};
storageStub = {
Expand Down Expand Up @@ -940,7 +941,7 @@ describe('Transport', () => {
);
});

it('should apply penalty when processUnconfirmedTransaction fails', async () => {
it('should not apply penalty when processUnconfirmedTransaction fails', async () => {
const error = new Error('validate error');
transactionPoolStub.processUnconfirmedTransaction.mockRejectedValue(
error,
Expand All @@ -949,7 +950,7 @@ describe('Transport', () => {
validTransactionsRequest,
defaultPeerId,
);
expect(channelStub.invoke).toHaveBeenCalledWith(
expect(channelStub.invoke).not.toHaveBeenCalledWith(
'network:applyPenalty',
{
peerId: defaultPeerId,
Expand Down

0 comments on commit b2c1191

Please sign in to comment.