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

Update required attributes - Closes #975 #983

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 21 additions & 5 deletions packages/lisk-transactions/src/transactions/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ export interface TransactionResponse {
}

export interface Attributes {
readonly [attribute: string]: string[];
readonly [entity: string]: {
readonly [property: string]: ReadonlyArray<string>;
};
}

export enum MultisignatureStatus {
Expand All @@ -63,6 +65,16 @@ export enum MultisignatureStatus {
UNKNOWN = 2,
}

export interface EntityMap {
readonly [name: string]: ReadonlyArray<unknown> | undefined;
}

export interface RequiredState {
shuse2 marked this conversation as resolved.
Show resolved Hide resolved
readonly sender: Account;
}

export const ENTITY_ACCOUNT = 'account';

export abstract class BaseTransaction {
public readonly amount: BigNum;
public readonly fee: BigNum;
Expand Down Expand Up @@ -254,11 +266,15 @@ export abstract class BaseTransaction {

public getRequiredAttributes(): Attributes {
return {
ACCOUNTS: [getAddressFromPublicKey(this.senderPublicKey)],
[ENTITY_ACCOUNT]: {
address: [getAddressFromPublicKey(this.senderPublicKey)],
},
};
}

public verify(sender: Account): TransactionResponse {
public abstract processRequiredState(state: EntityMap): RequiredState;

public verify({ sender }: RequiredState): TransactionResponse {
const errors: TransactionError[] = [];

// Check senderPublicKey
Expand Down Expand Up @@ -377,7 +393,7 @@ export abstract class BaseTransaction {
};
}

public apply(sender: Account): TransactionResponse {
public apply({ sender }: RequiredState): TransactionResponse {
const updatedBalance = new BigNum(sender.balance).sub(this.fee);
const updatedAccount = { ...sender, balance: updatedBalance.toString() };
const errors = updatedBalance.gte(0)
Expand All @@ -400,7 +416,7 @@ export abstract class BaseTransaction {
};
}

public undo(sender: Account): TransactionResponse {
public undo({ sender }: RequiredState): TransactionResponse {
const updatedBalance = new BigNum(sender.balance).add(this.fee);
const updatedAccount = { ...sender, balance: updatedBalance.toString() };
const errors = updatedBalance.lte(MAX_TRANSACTION_AMOUNT)
Expand Down
13 changes: 13 additions & 0 deletions packages/lisk-transactions/test/helpers/test_transaction_class.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import {
BaseTransaction,
EntityMap,
RequiredState,
TransactionResponse,
} from '../../src/transactions/base';
import { TransactionJSON, Status } from '../../src/transaction_types';
Expand All @@ -13,6 +15,17 @@ export class TestTransaction extends BaseTransaction {
return Buffer.alloc(0);
}

public processRequiredState(_: EntityMap): RequiredState {
return {
sender: {
address: '123L',
balance: '10000000',
publicKey:
'0eb0a6d7b862dc35c856c02c47fde3b4f60f2f3571a888b9a8ca7540c6793243',
},
};
}

public verifyAgainstOtherTransactions(
transactions: ReadonlyArray<TransactionJSON>,
): TransactionResponse {
Expand Down
92 changes: 51 additions & 41 deletions packages/lisk-transactions/test/transactions/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -554,14 +554,14 @@ describe('Base transaction class', () => {
).to.have.been.calledWithExactly(validTestTransaction.senderPublicKey);
});

it('should return an object with property `ACCOUNTS` containing an array with address of sender', async () => {
const expectedAddressArray = ['18278674964748191682L'];
it('should return an object with property `account` containing an array with address of sender', async () => {
const expectedAddressArray = { address: ['18278674964748191682L'] };
const requiredAttributes: any = validTestTransaction.getRequiredAttributes();
expect(requiredAttributes)
.to.be.an('object')
.and.to.have.property('ACCOUNTS');
.and.to.have.property('account');

expect(requiredAttributes['ACCOUNTS']).to.be.eql(expectedAddressArray);
expect(requiredAttributes['account']).to.be.eql(expectedAddressArray);
});
});

Expand All @@ -570,7 +570,7 @@ describe('Base transaction class', () => {
const verifyBalanceStub = sandbox
.stub(utils, 'verifyBalance')
.returns(true);
validTestTransaction.verify(defaultSenderAccount);
validTestTransaction.verify({ sender: defaultSenderAccount });

expect(verifyBalanceStub).to.be.calledWithExactly(
defaultSenderAccount,
Expand All @@ -582,7 +582,9 @@ describe('Base transaction class', () => {
const verifySignatureStub = sandbox
.stub(utils, 'verifySignature')
.returns(true);
validSecondSignatureTestTransaction.verify(defaultSecondSignatureAccount);
validSecondSignatureTestTransaction.verify({
sender: defaultSecondSignatureAccount,
});

expect(verifySignatureStub).to.be.calledWithExactly(
defaultSecondSignatureAccount.secondPublicKey,
Expand All @@ -604,7 +606,9 @@ describe('Base transaction class', () => {
'hex',
),
);
validMultisignatureTestTransaction.verify(defaultMultisignatureAccount);
validMultisignatureTestTransaction.verify({
sender: defaultMultisignatureAccount,
});

expect(getBasicBytesStub).to.be.calledOnce;
});
Expand All @@ -618,7 +622,9 @@ describe('Base transaction class', () => {
'hex',
),
);
validMultisignatureTestTransaction.verify(defaultMultisignatureAccount);
validMultisignatureTestTransaction.verify({
sender: defaultMultisignatureAccount,
});

expect(verifyMultisignaturesStub).to.be.calledWithExactly(
defaultMultisignatureAccount.multisignatures,
Expand All @@ -633,23 +639,23 @@ describe('Base transaction class', () => {
});

it('should set isMultisignature to true for multisignature account', async () => {
validTestTransaction.verify(defaultMultisignatureAccount);
validTestTransaction.verify({ sender: defaultMultisignatureAccount });
expect(validTestTransaction.isMultisignature).to.eql(
MultisignatureStatus.TRUE,
);
});

it('should set isMultisignature to false for non-multisignature account', async () => {
validTestTransaction.verify(defaultSenderAccount);
validTestTransaction.verify({ sender: defaultSenderAccount });
expect(validTestTransaction.isMultisignature).to.eql(
MultisignatureStatus.FALSE,
);
});

it('should return a successful transaction response with valid transaction', async () => {
const { id, status, errors } = validTestTransaction.verify(
defaultSenderAccount,
);
const { id, status, errors } = validTestTransaction.verify({
sender: defaultSenderAccount,
});

expect(id).to.be.eql(validTestTransaction.id);
expect(errors).to.be.eql([]);
Expand All @@ -661,9 +667,9 @@ describe('Base transaction class', () => {
...defaultSenderAccount,
publicKey: defaultSenderAccount.publicKey.replace('0', '1'),
};
const { id, status, errors } = validTestTransaction.verify(
invalidPublicKeyAccount,
);
const { id, status, errors } = validTestTransaction.verify({
sender: invalidPublicKeyAccount,
});

expect(id).to.be.eql(validTestTransaction.id);
expect((errors as ReadonlyArray<TransactionError>)[0])
Expand All @@ -677,9 +683,9 @@ describe('Base transaction class', () => {
...defaultSenderAccount,
address: defaultSenderAccount.address.replace('1', '0'),
};
const { id, status, errors } = validTestTransaction.verify(
invalidAddressAccount,
);
const { id, status, errors } = validTestTransaction.verify({
sender: invalidAddressAccount,
});

expect(id).to.be.eql(validTestTransaction.id);
expect((errors as ReadonlyArray<TransactionError>)[0])
Expand All @@ -694,7 +700,7 @@ describe('Base transaction class', () => {
...invalidSecondPublicKeySenderAccount
} = defaultSecondSignatureAccount;
const { id, status, errors } = validSecondSignatureTestTransaction.verify(
invalidSecondPublicKeySenderAccount,
{ sender: invalidSecondPublicKeySenderAccount },
);

expect(id).to.be.eql(validSecondSignatureTestTransaction.id);
Expand All @@ -712,9 +718,9 @@ describe('Base transaction class', () => {
...defaultSenderAccount,
balance: '0',
};
const { id, status, errors } = validTestTransaction.verify(
insufficientBalanceAccount,
);
const { id, status, errors } = validTestTransaction.verify({
sender: insufficientBalanceAccount,
});

expect(id).to.be.eql(validTestTransaction.id);
expect((errors as ReadonlyArray<TransactionError>)[0])
Expand All @@ -740,9 +746,9 @@ describe('Base transaction class', () => {
id,
status,
errors,
} = invalidSecondSignatureTestTransaction.verify(
defaultSecondSignatureAccount,
);
} = invalidSecondSignatureTestTransaction.verify({
sender: defaultSecondSignatureAccount,
});

expect(id).to.be.eql(invalidSecondSignatureTestTransaction.id);
expect((errors as ReadonlyArray<TransactionError>)[0])
Expand All @@ -763,7 +769,7 @@ describe('Base transaction class', () => {
invalidSignSignatureTransaction,
);
const { id, status, errors } = invalidSignSignatureTestTransaction.verify(
defaultSecondSignatureAccount,
{ sender: defaultSecondSignatureAccount },
);

expect(id).to.be.eql(invalidSignSignatureTestTransaction.id);
Expand All @@ -782,7 +788,7 @@ describe('Base transaction class', () => {
secondPublicKey: defaultTransaction.senderPublicKey.replace('1', '0'),
};
const { id, status, errors } = validSecondSignatureTestTransaction.verify(
invalidSecondPublicKeyAccount,
{ sender: invalidSecondPublicKeyAccount },
);

expect(id).to.be.eql(validSecondSignatureTestTransaction.id);
Expand All @@ -807,9 +813,9 @@ describe('Base transaction class', () => {
const invalidSignaturesTransaction = new TestTransaction(
multisignatureTransaction,
);
const { id, status, errors } = invalidSignaturesTransaction.verify(
defaultMultisignatureAccount,
);
const { id, status, errors } = invalidSignaturesTransaction.verify({
sender: defaultMultisignatureAccount,
});

expect(id).to.be.eql(invalidSignaturesTransaction.id);
(errors as ReadonlyArray<TransactionError>).forEach((error, i) =>
Expand Down Expand Up @@ -844,9 +850,9 @@ describe('Base transaction class', () => {

describe('#apply', () => {
it('should return a successful transaction response with an updated sender account', async () => {
const { id, status, state, errors } = validTestTransaction.apply(
defaultSenderAccount,
);
const { id, status, state, errors } = validTestTransaction.apply({
sender: defaultSenderAccount,
});
expect(id).to.be.eql(validTestTransaction.id);
expect(status).to.eql(Status.OK);
expect(state)
Expand All @@ -858,8 +864,10 @@ describe('Base transaction class', () => {

it('should return a failed transaction response with insufficient account balance', async () => {
const { id, status, state, errors } = validTestTransaction.apply({
...defaultSenderAccount,
balance: '0',
sender: {
...defaultSenderAccount,
balance: '0',
},
});

expect(id).to.be.eql(validTestTransaction.id);
Expand All @@ -881,9 +889,9 @@ describe('Base transaction class', () => {

describe('#undo', () => {
it('should return a successful transaction response with an updated sender account', async () => {
const { id, status, state, errors } = validTestTransaction.undo(
defaultSenderAccount,
);
const { id, status, state, errors } = validTestTransaction.undo({
sender: defaultSenderAccount,
});
expect(id).to.be.eql(validTestTransaction.id);
expect(status).to.eql(Status.OK);
expect(state)
Expand All @@ -895,8 +903,10 @@ describe('Base transaction class', () => {

it('should return a failed transaction response with account balance exceeding max amount', async () => {
const { id, status, state, errors } = validTestTransaction.undo({
...defaultSenderAccount,
balance: MAX_TRANSACTION_AMOUNT.toString(),
sender: {
...defaultSenderAccount,
balance: MAX_TRANSACTION_AMOUNT.toString(),
},
});
expect(id).to.be.eql(validTestTransaction.id);
expect(status).to.eql(Status.FAIL);
Expand Down