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

Commit

Permalink
Merge pull request #3945 from LiskHQ/3941-custom-tx-fails-fee-validation
Browse files Browse the repository at this point in the history
Custom transaction fails to validate if `FEE` member is not defined - Closes #3941
  • Loading branch information
shuse2 authored Jul 12, 2019
2 parents d1a4662 + ab11e7a commit b363aeb
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 9 deletions.
17 changes: 11 additions & 6 deletions elements/lisk-transactions/src/base_transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ export abstract class BaseTransaction {
public receivedAt?: Date;

public static TYPE: number;
public static FEE: string;
public static FEE = '0';

protected _id?: string;
protected _senderId?: string;
Expand Down Expand Up @@ -143,13 +143,22 @@ export abstract class BaseTransaction {
const tx = (typeof rawTransaction === 'object' && rawTransaction !== null
? rawTransaction
: {}) as Partial<TransactionJSON>;

this.amount = new BigNum(
isValidNumber(tx.amount) ? (tx.amount as string | number) : '0',
);

this.fee = new BigNum(
isValidNumber(tx.fee) ? (tx.fee as string | number) : '0',
isValidNumber(tx.fee)
? (tx.fee as string | number)
: (this.constructor as typeof BaseTransaction).FEE,
);

this.type =
typeof tx.type === 'number'
? tx.type
: (this.constructor as typeof BaseTransaction).TYPE;

this._id = tx.id;
this.recipientId = tx.recipientId || '';
this.recipientPublicKey = tx.recipientPublicKey || undefined;
Expand All @@ -166,10 +175,6 @@ export abstract class BaseTransaction {
this.signatures = (tx.signatures as string[]) || [];
this._signSignature = tx.signSignature;
this.timestamp = typeof tx.timestamp === 'number' ? tx.timestamp : 0;
this.type =
typeof tx.type === 'number'
? tx.type
: (this.constructor as typeof BaseTransaction).TYPE;

// Additional data not related to the protocol
this.confirmations = tx.confirmations;
Expand Down
20 changes: 19 additions & 1 deletion elements/lisk-transactions/test/base_transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
addTransactionFields,
MockStateStore as store,
TestTransaction,
TestTransactionBasicImpl,
} from './helpers';
import {
validAccount as defaultSenderAccount,
Expand All @@ -48,6 +49,7 @@ describe('Base transaction class', () => {

let validTestTransaction: BaseTransaction;
let transactionWithDefaultValues: BaseTransaction;
let transactionWithBasicImpl: BaseTransaction;
let validSecondSignatureTestTransaction: BaseTransaction;
let validMultisignatureTestTransaction: BaseTransaction;
let storeAccountGetStub: sinon.SinonStub;
Expand All @@ -56,6 +58,7 @@ describe('Base transaction class', () => {
beforeEach(async () => {
validTestTransaction = new TestTransaction(defaultTransaction);
transactionWithDefaultValues = new TestTransaction({});
transactionWithBasicImpl = new TestTransactionBasicImpl({});
validSecondSignatureTestTransaction = new TestTransaction(
defaultSecondSignatureTransaction,
);
Expand All @@ -79,7 +82,7 @@ describe('Base transaction class', () => {

it('should set default values', async () => {
expect(transactionWithDefaultValues.amount.toString()).to.be.eql('0');
expect(transactionWithDefaultValues.fee.toString()).to.be.eql('0');
expect(transactionWithDefaultValues.fee.toString()).to.be.eql('10000000');
expect(transactionWithDefaultValues.recipientId).to.be.eql('');
expect(transactionWithDefaultValues.recipientPublicKey).to.be.undefined;
expect(transactionWithDefaultValues.timestamp).to.be.eql(0);
Expand Down Expand Up @@ -116,6 +119,12 @@ describe('Base transaction class', () => {
.and.be.instanceof(BigNum);
});

it('should have default fee if fee param is invalid', async () => {
const transactionWithInvalidFee = new TestTransaction({ fee: 'invalid' });

expect(transactionWithInvalidFee.fee.toString()).to.be.eql('10000000');
});

it('should have id string', async () => {
expect(validTestTransaction)
.to.have.property('id')
Expand Down Expand Up @@ -501,6 +510,15 @@ describe('Base transaction class', () => {
expect(status).to.eql(Status.OK);
});

it('should return a successful transaction response with a valid transaction with basic impl', async () => {
transactionWithBasicImpl.sign('passphrase');
const { id, status, errors } = transactionWithBasicImpl.validate();

expect(id).to.be.eql(transactionWithBasicImpl.id);
expect(errors).to.be.empty;
expect(status).to.eql(Status.OK);
});

it('should call getBasicBytes', async () => {
const getBasicBytesStub = sandbox
.stub(validTestTransaction as any, 'getBasicBytes')
Expand Down
12 changes: 10 additions & 2 deletions elements/lisk-transactions/test/helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@
*
*/
import { addTransactionFields } from './add_transaction_fields';
import { TestTransaction } from './test_transaction_class';
import {
TestTransaction,
TestTransactionBasicImpl,
} from './test_transaction_class';
import { MockStateStore } from './state_store';
export { addTransactionFields, MockStateStore, TestTransaction };
export {
addTransactionFields,
MockStateStore,
TestTransaction,
TestTransactionBasicImpl,
};
28 changes: 28 additions & 0 deletions elements/lisk-transactions/test/helpers/test_transaction_class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,31 @@ export class TestTransaction extends BaseTransaction {
return { data: raw };
}
}

export class TestTransactionBasicImpl extends BaseTransaction {
public static TYPE = 1;

public validateAsset() {
return [];
}

public applyAsset() {
return [];
}

public undoAsset() {
return [];
}

public verifyAgainstTransactions(
transactions: ReadonlyArray<TransactionJSON>,
): ReadonlyArray<TransactionError> {
transactions.forEach(() => true);

return [];
}

public assetFromSync(raw: any) {
return { data: raw };
}
}

0 comments on commit b363aeb

Please sign in to comment.