From c5f704fcd06b9ececa3cf28f1f03497fc538c5ac Mon Sep 17 00:00:00 2001 From: Pablo Vicente Date: Fri, 28 Jun 2019 14:39:20 +0200 Subject: [PATCH 01/14] Make fee an static property in base transaction and transfer transaction --- .../lisk-transactions/src/0_transfer_transaction.ts | 13 +------------ elements/lisk-transactions/src/base_transaction.ts | 13 +++++++++++++ 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/elements/lisk-transactions/src/0_transfer_transaction.ts b/elements/lisk-transactions/src/0_transfer_transaction.ts index 5e5cc88a8bd..f336d998561 100644 --- a/elements/lisk-transactions/src/0_transfer_transaction.ts +++ b/elements/lisk-transactions/src/0_transfer_transaction.ts @@ -48,6 +48,7 @@ export const transferAssetFormatSchema = { export class TransferTransaction extends BaseTransaction { public readonly asset: TransferAsset; public static TYPE = 0; + public static FEE = TRANSFER_FEE.toString(); public constructor(rawTransaction: unknown) { super(rawTransaction); @@ -104,18 +105,6 @@ export class TransferTransaction extends BaseTransaction { ); } - if (!this.fee.eq(TRANSFER_FEE)) { - errors.push( - new TransactionError( - `Fee must be equal to ${TRANSFER_FEE}`, - this.id, - '.fee', - this.fee.toString(), - TRANSFER_FEE, - ), - ); - } - if (!this.recipientId) { errors.push( new TransactionError( diff --git a/elements/lisk-transactions/src/base_transaction.ts b/elements/lisk-transactions/src/base_transaction.ts index 5fcbe023a39..0bca061592b 100644 --- a/elements/lisk-transactions/src/base_transaction.ts +++ b/elements/lisk-transactions/src/base_transaction.ts @@ -115,6 +115,7 @@ export abstract class BaseTransaction { public receivedAt?: Date; public static TYPE: number; + public static FEE: string; protected _id?: string; protected _signature?: string; @@ -277,6 +278,18 @@ export abstract class BaseTransaction { ); } + if (!this.fee.eq((this.constructor as typeof BaseTransaction).FEE)) { + errors.push( + new TransactionError( + `Invalid fee`, + this.id, + '.fee', + this.fee.toString(), + (this.constructor as typeof BaseTransaction).FEE.toString(), + ), + ); + } + return createResponse(this.id, errors); } From ed1c2a0fc17b0b8e0d03d9c51f0d359f19b7bde6 Mon Sep 17 00:00:00 2001 From: Pablo Vicente Date: Mon, 1 Jul 2019 10:21:34 +0200 Subject: [PATCH 02/14] Add FEE prop to transaction types 1,2,3,5 --- .../src/1_second_signature_transaction.ts | 13 +------------ .../lisk-transactions/src/2_delegate_transaction.ts | 13 +------------ .../lisk-transactions/src/3_vote_transaction.ts | 13 +------------ .../lisk-transactions/src/5_dapp_transaction.ts | 12 +----------- 4 files changed, 4 insertions(+), 47 deletions(-) diff --git a/elements/lisk-transactions/src/1_second_signature_transaction.ts b/elements/lisk-transactions/src/1_second_signature_transaction.ts index 368dc9c227e..20a9cae4c47 100644 --- a/elements/lisk-transactions/src/1_second_signature_transaction.ts +++ b/elements/lisk-transactions/src/1_second_signature_transaction.ts @@ -49,6 +49,7 @@ export const secondSignatureAssetFormatSchema = { export class SecondSignatureTransaction extends BaseTransaction { public readonly asset: SecondSignatureAsset; public static TYPE = 1; + public static FEE = SIGNATURE_FEE.toString(); public constructor(rawTransaction: unknown) { super(rawTransaction); @@ -116,18 +117,6 @@ export class SecondSignatureTransaction extends BaseTransaction { ); } - if (!this.fee.eq(SIGNATURE_FEE)) { - errors.push( - new TransactionError( - `Fee must be equal to ${SIGNATURE_FEE}`, - this.id, - '.fee', - this.fee.toString(), - SIGNATURE_FEE, - ), - ); - } - if (this.recipientId) { errors.push( new TransactionError( diff --git a/elements/lisk-transactions/src/2_delegate_transaction.ts b/elements/lisk-transactions/src/2_delegate_transaction.ts index fd4c4d2fbaf..7fe0733da36 100644 --- a/elements/lisk-transactions/src/2_delegate_transaction.ts +++ b/elements/lisk-transactions/src/2_delegate_transaction.ts @@ -51,6 +51,7 @@ export class DelegateTransaction extends BaseTransaction { public readonly asset: DelegateAsset; public readonly containsUniqueData: boolean; public static TYPE = 2; + public static FEE = DELEGATE_FEE.toString(); public constructor(rawTransaction: unknown) { super(rawTransaction); @@ -121,18 +122,6 @@ export class DelegateTransaction extends BaseTransaction { ); } - if (!this.fee.eq(DELEGATE_FEE)) { - errors.push( - new TransactionError( - `Fee must be equal to ${DELEGATE_FEE}`, - this.id, - '.fee', - this.fee.toString(), - DELEGATE_FEE, - ), - ); - } - if (this.recipientId) { errors.push( new TransactionError( diff --git a/elements/lisk-transactions/src/3_vote_transaction.ts b/elements/lisk-transactions/src/3_vote_transaction.ts index 18cbab6e462..3f8d0384859 100644 --- a/elements/lisk-transactions/src/3_vote_transaction.ts +++ b/elements/lisk-transactions/src/3_vote_transaction.ts @@ -63,6 +63,7 @@ export class VoteTransaction extends BaseTransaction { public readonly containsUniqueData: boolean; public readonly asset: VoteAsset; public static TYPE = 3; + public static FEE = VOTE_FEE.toString(); public constructor(rawTransaction: unknown) { super(rawTransaction); @@ -177,18 +178,6 @@ export class VoteTransaction extends BaseTransaction { ); } - if (!this.fee.eq(VOTE_FEE)) { - errors.push( - new TransactionError( - `Fee must be equal to ${VOTE_FEE}`, - this.id, - '.fee', - this.fee.toString(), - VOTE_FEE, - ), - ); - } - return errors; } diff --git a/elements/lisk-transactions/src/5_dapp_transaction.ts b/elements/lisk-transactions/src/5_dapp_transaction.ts index 00562e4c8cc..e03f5bb8170 100644 --- a/elements/lisk-transactions/src/5_dapp_transaction.ts +++ b/elements/lisk-transactions/src/5_dapp_transaction.ts @@ -91,6 +91,7 @@ export class DappTransaction extends BaseTransaction { public readonly containsUniqueData: boolean; public readonly asset: DappAsset; public static TYPE = 5; + public static FEE = DAPP_FEE.toString(); public constructor(rawTransaction: unknown) { super(rawTransaction); @@ -234,17 +235,6 @@ export class DappTransaction extends BaseTransaction { ); } - if (!this.fee.eq(DAPP_FEE)) { - errors.push( - new TransactionError( - `Fee must be equal to ${DAPP_FEE}`, - this.id, - '.fee', - this.fee.toString(), - DAPP_FEE, - ), - ); - } const validLinkSuffix = ['.zip']; if (errors.length > 0) { From aa69b1d23144a007bce1fca924575c183baf3123 Mon Sep 17 00:00:00 2001 From: Pablo Vicente Date: Mon, 1 Jul 2019 11:47:50 +0200 Subject: [PATCH 03/14] Do not use static property for multi-signatures due to its dynamic nature based on number of present keys --- elements/lisk-transactions/src/base_transaction.ts | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/elements/lisk-transactions/src/base_transaction.ts b/elements/lisk-transactions/src/base_transaction.ts index 0bca061592b..9aed3857d6c 100644 --- a/elements/lisk-transactions/src/base_transaction.ts +++ b/elements/lisk-transactions/src/base_transaction.ts @@ -94,6 +94,8 @@ export enum MultisignatureStatus { FAIL = 4, } +const typeMultiSignature = 4; + export const ENTITY_ACCOUNT = 'account'; export const ENTITY_TRANSACTION = 'transaction'; @@ -278,7 +280,11 @@ export abstract class BaseTransaction { ); } - if (!this.fee.eq((this.constructor as typeof BaseTransaction).FEE)) { + // Skip for type 4 due to dynamic nature of fee for multi-signature. Validation happens on multisignature class. + if ( + this.type !== typeMultiSignature && + !this.fee.eq((this.constructor as typeof BaseTransaction).FEE) + ) { errors.push( new TransactionError( `Invalid fee`, From c6095acdf8497d989a6dd5b51254ae60c817898e Mon Sep 17 00:00:00 2001 From: Pablo Vicente Date: Mon, 1 Jul 2019 12:14:26 +0200 Subject: [PATCH 04/14] Fix test_transaction --- .../lisk-transactions/test/helpers/test_transaction_class.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/elements/lisk-transactions/test/helpers/test_transaction_class.ts b/elements/lisk-transactions/test/helpers/test_transaction_class.ts index 6e57db6df05..e2a74221a89 100644 --- a/elements/lisk-transactions/test/helpers/test_transaction_class.ts +++ b/elements/lisk-transactions/test/helpers/test_transaction_class.ts @@ -15,9 +15,11 @@ import { BaseTransaction } from '../../src/base_transaction'; import { TransactionJSON } from '../../src/transaction_types'; import { TransactionError } from '../../src/errors'; +import { TRANSFER_FEE } from '../../src/constants'; export class TestTransaction extends BaseTransaction { public static TYPE = 0; + public static FEE = TRANSFER_FEE.toString(); public assetToJSON(): object { return {}; From 477e0f48812334b5da8e50a9434babeba0ae1efe Mon Sep 17 00:00:00 2001 From: Pablo Vicente Date: Mon, 1 Jul 2019 13:46:52 +0200 Subject: [PATCH 05/14] Add FEE property to matcher CustomTransationClass --- framework/test/mocha/integration/matcher.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/framework/test/mocha/integration/matcher.js b/framework/test/mocha/integration/matcher.js index 5cd8eb21966..dd8f0d4ce2c 100644 --- a/framework/test/mocha/integration/matcher.js +++ b/framework/test/mocha/integration/matcher.js @@ -49,6 +49,10 @@ class CustomTransationClass extends BaseTransaction { return 7; } + static get FEE() { + return TransferTransaction.FEE; + } + assetToJSON() { return this.asset; } From 15495595c33163a3a940c28c34ac04fbc6a6e3bc Mon Sep 17 00:00:00 2001 From: Pablo Vicente Date: Mon, 1 Jul 2019 14:59:28 +0200 Subject: [PATCH 06/14] Allow zero fee transactions --- elements/lisk-transactions/src/utils/validation/validation.ts | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/elements/lisk-transactions/src/utils/validation/validation.ts b/elements/lisk-transactions/src/utils/validation/validation.ts index f55bcd6f0c2..940ed4a77f0 100644 --- a/elements/lisk-transactions/src/utils/validation/validation.ts +++ b/elements/lisk-transactions/src/utils/validation/validation.ts @@ -164,9 +164,7 @@ export const isValidTransferData = (data: string): boolean => Buffer.byteLength(data, 'utf8') <= MAX_TRANSFER_ASSET_DATA_LENGTH; export const validateFee = (data: string) => - isNumberString(data) && - isGreaterThanZero(new BigNum(data)) && - !isGreaterThanMaxTransactionAmount(new BigNum(data)); + isNumberString(data) && !isGreaterThanMaxTransactionAmount(new BigNum(data)); export const isValidInteger = (num: unknown) => typeof num === 'number' ? Math.floor(num) === num : false; From e05cbd23871c14a854b09f0350e7c608139f536a Mon Sep 17 00:00:00 2001 From: Pablo Vicente Date: Mon, 1 Jul 2019 15:15:18 +0200 Subject: [PATCH 07/14] Update tests related to zero fee --- .../lisk-transactions/test/utils/validation/validation.ts | 4 ++-- elements/lisk-transactions/test/utils/validation/validator.ts | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/elements/lisk-transactions/test/utils/validation/validation.ts b/elements/lisk-transactions/test/utils/validation/validation.ts index 3d942469c21..7c9d54ce4b4 100644 --- a/elements/lisk-transactions/test/utils/validation/validation.ts +++ b/elements/lisk-transactions/test/utils/validation/validation.ts @@ -289,8 +289,8 @@ describe('validation', () => { }); describe('#validateFee', () => { - it('should return false is amount is 0', () => { - return expect(validateFee('0')).to.be.false; + it('should return true if amount is 0', () => { + return expect(validateFee('0')).to.be.true; }); it('should return true when amount is a number greater than 0 and less than maximum transaction amount', () => { diff --git a/elements/lisk-transactions/test/utils/validation/validator.ts b/elements/lisk-transactions/test/utils/validation/validator.ts index e7398a723dd..79c24fc4517 100644 --- a/elements/lisk-transactions/test/utils/validation/validator.ts +++ b/elements/lisk-transactions/test/utils/validation/validator.ts @@ -294,8 +294,8 @@ describe('validator', () => { return expect(validate({ target: '000000100' })).to.be.true; }); - it('should validate to false when amount is 0', () => { - return expect(validate({ target: '0' })).to.be.false; + it('should validate to true when amount is 0', () => { + return expect(validate({ target: '0' })).to.be.true; }); it('should validate to false when number greater than maximum is provided', () => { From 510a7ca925dd4fc8bddec3830abc9ac3e8a73199 Mon Sep 17 00:00:00 2001 From: Pablo Vicente Date: Tue, 2 Jul 2019 12:39:45 +0200 Subject: [PATCH 08/14] Add isGreaterThanOrEqualToZero based on PR feedback --- .../src/utils/validation/validation.ts | 7 ++++++- .../test/utils/validation/validation.ts | 15 +++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/elements/lisk-transactions/src/utils/validation/validation.ts b/elements/lisk-transactions/src/utils/validation/validation.ts index 940ed4a77f0..a1064846ba5 100644 --- a/elements/lisk-transactions/src/utils/validation/validation.ts +++ b/elements/lisk-transactions/src/utils/validation/validation.ts @@ -139,6 +139,9 @@ export const validateAddress = (address: string): boolean => { export const isGreaterThanZero = (amount: BigNum) => amount.cmp(0) > 0; +export const isGreaterThanOrEqualToZero = (amount: BigNum) => + amount.cmp(0) >= 0; + export const isGreaterThanMaxTransactionAmount = (amount: BigNum) => amount.cmp(MAX_TRANSACTION_AMOUNT) > 0; @@ -164,7 +167,9 @@ export const isValidTransferData = (data: string): boolean => Buffer.byteLength(data, 'utf8') <= MAX_TRANSFER_ASSET_DATA_LENGTH; export const validateFee = (data: string) => - isNumberString(data) && !isGreaterThanMaxTransactionAmount(new BigNum(data)); + isNumberString(data) && + isGreaterThanOrEqualToZero(new BigNum(data)) && + !isGreaterThanMaxTransactionAmount(new BigNum(data)); export const isValidInteger = (num: unknown) => typeof num === 'number' ? Math.floor(num) === num : false; diff --git a/elements/lisk-transactions/test/utils/validation/validation.ts b/elements/lisk-transactions/test/utils/validation/validation.ts index 7c9d54ce4b4..c87660c19b2 100644 --- a/elements/lisk-transactions/test/utils/validation/validation.ts +++ b/elements/lisk-transactions/test/utils/validation/validation.ts @@ -26,6 +26,7 @@ import { validateFee, isGreaterThanMaxTransactionAmount, isGreaterThanZero, + isGreaterThanOrEqualToZero, isGreaterThanMaxTransactionId, isNumberString, isValidInteger, @@ -310,6 +311,20 @@ describe('validation', () => { }); }); + describe('#isGreaterThanOrEqualToZero', () => { + it('should return true when amount is 0', () => { + return expect(isGreaterThanOrEqualToZero(new BigNum('0'))).to.be.true; + }); + + it('should return true when amount is greater than 0', () => { + return expect( + isGreaterThanOrEqualToZero( + new BigNum('9223372036854775808987234289782357'), + ), + ).to.be.true; + }); + }); + describe('#isGreaterThanMaxTransactionAmount', () => { it('should return false when amount is less than maximum transaction amount', () => { return expect( From 5c426d5b0a1fb606a36cc0975af3d1ed6f0e0ceb Mon Sep 17 00:00:00 2001 From: Pablo Vicente Date: Tue, 2 Jul 2019 13:55:22 +0200 Subject: [PATCH 09/14] Use MultisignatureTransaction.FEE instead of constant --- elements/lisk-transactions/src/4_multisignature_transaction.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/elements/lisk-transactions/src/4_multisignature_transaction.ts b/elements/lisk-transactions/src/4_multisignature_transaction.ts index 2c2fc76c999..0d33446b52e 100644 --- a/elements/lisk-transactions/src/4_multisignature_transaction.ts +++ b/elements/lisk-transactions/src/4_multisignature_transaction.ts @@ -93,6 +93,7 @@ export interface MultiSignatureAsset { export class MultisignatureTransaction extends BaseTransaction { public readonly asset: MultiSignatureAsset; public static TYPE = 4; + public static FEE = MULTISIGNATURE_FEE.toString(); protected _multisignatureStatus: MultisignatureStatus = MultisignatureStatus.PENDING; @@ -171,7 +172,7 @@ export class MultisignatureTransaction extends BaseTransaction { return errors; } - const expectedFee = new BigNum(MULTISIGNATURE_FEE).mul( + const expectedFee = new BigNum(MultisignatureTransaction.FEE).mul( this.asset.multisignature.keysgroup.length + 1, ); if (!this.fee.eq(expectedFee)) { From 2f165f8108fe07e3a2d7724a66564dd6811deba9 Mon Sep 17 00:00:00 2001 From: Pablo Vicente Date: Tue, 2 Jul 2019 13:57:09 +0200 Subject: [PATCH 10/14] Rename typeMultiSignature to MULTISIGNATURE_TYPE --- elements/lisk-transactions/src/base_transaction.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/elements/lisk-transactions/src/base_transaction.ts b/elements/lisk-transactions/src/base_transaction.ts index f4448d0bee4..33930ebd2ad 100644 --- a/elements/lisk-transactions/src/base_transaction.ts +++ b/elements/lisk-transactions/src/base_transaction.ts @@ -94,7 +94,7 @@ export enum MultisignatureStatus { FAIL = 4, } -const typeMultiSignature = 4; +const MULTISIGNATURE_TYPE = 4; export const ENTITY_ACCOUNT = 'account'; export const ENTITY_TRANSACTION = 'transaction'; @@ -282,7 +282,7 @@ export abstract class BaseTransaction { // Skip for type 4 due to dynamic nature of fee for multi-signature. Validation happens on multisignature class. if ( - this.type !== typeMultiSignature && + this.type !== MULTISIGNATURE_TYPE && !this.fee.eq((this.constructor as typeof BaseTransaction).FEE) ) { errors.push( From 98c5217950c1ecc9e1405412313f35a7f67f4fe6 Mon Sep 17 00:00:00 2001 From: Pablo Vicente Date: Tue, 2 Jul 2019 14:50:52 +0200 Subject: [PATCH 11/14] Add megative test case to isGreaterThanOrEqualToZero --- .../lisk-transactions/test/utils/validation/validation.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/elements/lisk-transactions/test/utils/validation/validation.ts b/elements/lisk-transactions/test/utils/validation/validation.ts index c87660c19b2..e3421369866 100644 --- a/elements/lisk-transactions/test/utils/validation/validation.ts +++ b/elements/lisk-transactions/test/utils/validation/validation.ts @@ -323,6 +323,10 @@ describe('validation', () => { ), ).to.be.true; }); + + it('should return false when amount is less than 0', () => { + return expect(isGreaterThanOrEqualToZero(new BigNum('-1'))).to.be.false; + }); }); describe('#isGreaterThanMaxTransactionAmount', () => { From 36f5555520d968ab3e9c130bd956b8ca6a090240 Mon Sep 17 00:00:00 2001 From: Pablo Vicente Date: Tue, 2 Jul 2019 16:36:44 +0200 Subject: [PATCH 12/14] Refactor validation of fees out of validate() into its own method so it can be overriden by transaction --- .../src/4_multisignature_transaction.ts | 31 ++++++++++--------- .../lisk-transactions/src/base_transaction.ts | 28 ++++++++--------- 2 files changed, 30 insertions(+), 29 deletions(-) diff --git a/elements/lisk-transactions/src/4_multisignature_transaction.ts b/elements/lisk-transactions/src/4_multisignature_transaction.ts index 0d33446b52e..99af766acb7 100644 --- a/elements/lisk-transactions/src/4_multisignature_transaction.ts +++ b/elements/lisk-transactions/src/4_multisignature_transaction.ts @@ -172,21 +172,6 @@ export class MultisignatureTransaction extends BaseTransaction { return errors; } - const expectedFee = new BigNum(MultisignatureTransaction.FEE).mul( - this.asset.multisignature.keysgroup.length + 1, - ); - if (!this.fee.eq(expectedFee)) { - errors.push( - new TransactionError( - `Fee must be equal to ${expectedFee.toString()}`, - this.id, - '.fee', - this.fee.toString(), - expectedFee.toString(), - ), - ); - } - if ( this.asset.multisignature.min > this.asset.multisignature.keysgroup.length ) { @@ -225,6 +210,22 @@ export class MultisignatureTransaction extends BaseTransaction { return errors; } + public validateFee(): TransactionError | undefined { + const expectedFee = new BigNum(MultisignatureTransaction.FEE).mul( + this.asset.multisignature.keysgroup.length + 1, + ); + + return !this.fee.eq(expectedFee) + ? new TransactionError( + `Fee must be equal to ${expectedFee.toString()}`, + this.id, + '.fee', + this.fee.toString(), + expectedFee.toString(), + ) + : undefined; + } + public processMultisignatures(_: StateStore): TransactionResponse { const transactionBytes = this.getBasicBytes(); diff --git a/elements/lisk-transactions/src/base_transaction.ts b/elements/lisk-transactions/src/base_transaction.ts index 33930ebd2ad..df5d4ee5f09 100644 --- a/elements/lisk-transactions/src/base_transaction.ts +++ b/elements/lisk-transactions/src/base_transaction.ts @@ -94,8 +94,6 @@ export enum MultisignatureStatus { FAIL = 4, } -const MULTISIGNATURE_TYPE = 4; - export const ENTITY_ACCOUNT = 'account'; export const ENTITY_TRANSACTION = 'transaction'; @@ -280,23 +278,25 @@ export abstract class BaseTransaction { ); } - // Skip for type 4 due to dynamic nature of fee for multi-signature. Validation happens on multisignature class. - if ( - this.type !== MULTISIGNATURE_TYPE && - !this.fee.eq((this.constructor as typeof BaseTransaction).FEE) - ) { - errors.push( - new TransactionError( + const feeError = this.validateFee(); + + if (feeError) { + errors.push(feeError); + } + + return createResponse(this.id, errors); + } + + public validateFee(): TransactionError | undefined { + return !this.fee.eq((this.constructor as typeof BaseTransaction).FEE) + ? new TransactionError( `Invalid fee`, this.id, '.fee', this.fee.toString(), (this.constructor as typeof BaseTransaction).FEE.toString(), - ), - ); - } - - return createResponse(this.id, errors); + ) + : undefined; } public verifyAgainstOtherTransactions( From eb4f095161a199daf496f2bb1f38f8458424195c Mon Sep 17 00:00:00 2001 From: Pablo Vicente Date: Tue, 2 Jul 2019 17:01:04 +0200 Subject: [PATCH 13/14] Add tests related to validateFee() --- .../test/base_transaction.ts | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/elements/lisk-transactions/test/base_transaction.ts b/elements/lisk-transactions/test/base_transaction.ts index 8853a46a7f7..20ae920d74e 100644 --- a/elements/lisk-transactions/test/base_transaction.ts +++ b/elements/lisk-transactions/test/base_transaction.ts @@ -493,6 +493,13 @@ describe('Base transaction class', () => { ); }); + it('should call validateFee', async () => { + sandbox.spy(validTestTransaction, 'validateFee'); + validTestTransaction.validate(); + + expect(validTestTransaction.validateFee).to.be.called; + }); + it('should return a failed transaction response with invalid signature', async () => { const invalidSignature = defaultTransaction.signature.replace('1', '0'); const invalidSignatureTransaction = { @@ -542,6 +549,36 @@ describe('Base transaction class', () => { }); }); + describe('#validateFee', () => { + it('should return undefined if fee is valid', async () => { + const validFee = validTestTransaction.validateFee(); + + expect(validFee).to.be.undefined; + }); + + it('should return transactionError if fee is invalid', async () => { + const invalidTransaction = { + type: 0, + amount: '00001', + fee: '0000', + recipientId: '', + senderPublicKey: '11111111', + senderId: '11111111', + timestamp: 79289378, + asset: {}, + signature: '1111111111', + id: '1', + }; + const invalidTestTransaction = new TestTransaction( + invalidTransaction as any, + ); + + const validFee = invalidTestTransaction.validateFee(); + + expect(validFee).to.be.instanceOf(TransactionError); + }); + }); + describe('#verifyAgainstOtherTransactions', () => { it('should return a transaction response', async () => { const otherTransactions = [defaultTransaction, defaultTransaction]; From 843de502fae0c0bde36696f549430ea2f13f8ec8 Mon Sep 17 00:00:00 2001 From: Pablo Vicente Date: Tue, 2 Jul 2019 17:28:58 +0200 Subject: [PATCH 14/14] Apply suggestions from code review Co-Authored-By: Lucas --- elements/lisk-transactions/test/base_transaction.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/elements/lisk-transactions/test/base_transaction.ts b/elements/lisk-transactions/test/base_transaction.ts index 20ae920d74e..0e207bf8e03 100644 --- a/elements/lisk-transactions/test/base_transaction.ts +++ b/elements/lisk-transactions/test/base_transaction.ts @@ -573,9 +573,11 @@ describe('Base transaction class', () => { invalidTransaction as any, ); - const validFee = invalidTestTransaction.validateFee(); + const feeError = invalidTestTransaction.validateFee(); - expect(validFee).to.be.instanceOf(TransactionError); + expect(feeError) + .to.be.instanceof(TransactionError) + .and.to.have.property('message', 'Invalid fee'); }); });