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 #3727 from LiskHQ/3726-type_as_static
Browse files Browse the repository at this point in the history
Move the type check from validateAsset to validate as a static field - Closes #3726
  • Loading branch information
shuse2 authored May 28, 2019
2 parents 59c6884 + 6e1cff0 commit ac1f565
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 84 deletions.
14 changes: 1 addition & 13 deletions elements/lisk-transactions/src/0_transfer_transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ import {
verifyBalance,
} from './utils';

const TRANSACTION_TRANSFER_TYPE = 0;

export interface TransferAsset {
readonly data: string;
}
Expand All @@ -49,6 +47,7 @@ export const transferAssetFormatSchema = {

export class TransferTransaction extends BaseTransaction {
public readonly asset: TransferAsset;
public static TYPE = 0;

public constructor(rawTransaction: unknown) {
super(rawTransaction);
Expand Down Expand Up @@ -93,17 +92,6 @@ export class TransferTransaction extends BaseTransaction {
this.id,
validator.errors,
) as TransactionError[];
if (this.type !== TRANSACTION_TRANSFER_TYPE) {
errors.push(
new TransactionError(
'Invalid type',
this.id,
'.type',
this.type,
TRANSACTION_TRANSFER_TYPE,
),
);
}

if (!validateTransferAmount(this.amount.toString())) {
errors.push(
Expand Down
16 changes: 2 additions & 14 deletions elements/lisk-transactions/src/1_second_signature_transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ import { convertToAssetError, TransactionError } from './errors';
import { TransactionJSON } from './transaction_types';
import { getId, validator } from './utils';

const TRANSACTION_SIGNATURE_TYPE = 1;

export interface SecondSignatureAsset {
readonly signature: {
readonly publicKey: string;
Expand All @@ -50,6 +48,8 @@ export const secondSignatureAssetFormatSchema = {

export class SecondSignatureTransaction extends BaseTransaction {
public readonly asset: SecondSignatureAsset;
public static TYPE = 1;

public constructor(rawTransaction: unknown) {
super(rawTransaction);
const tx = (typeof rawTransaction === 'object' && rawTransaction !== null
Expand Down Expand Up @@ -104,18 +104,6 @@ export class SecondSignatureTransaction extends BaseTransaction {
validator.errors,
) as TransactionError[];

if (this.type !== TRANSACTION_SIGNATURE_TYPE) {
errors.push(
new TransactionError(
'Invalid type',
this.id,
'.type',
this.type,
TRANSACTION_SIGNATURE_TYPE,
),
);
}

if (!this.amount.eq(0)) {
errors.push(
new TransactionError(
Expand Down
15 changes: 1 addition & 14 deletions elements/lisk-transactions/src/2_delegate_transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ import { convertToAssetError, TransactionError } from './errors';
import { Account, TransactionJSON } from './transaction_types';
import { validator } from './utils';

const TRANSACTION_DELEGATE_TYPE = 2;

export interface DelegateAsset {
readonly delegate: {
readonly username: string;
Expand Down Expand Up @@ -52,6 +50,7 @@ export const delegateAssetFormatSchema = {
export class DelegateTransaction extends BaseTransaction {
public readonly asset: DelegateAsset;
public readonly containsUniqueData: boolean;
public static TYPE = 2;

public constructor(rawTransaction: unknown) {
super(rawTransaction);
Expand Down Expand Up @@ -110,18 +109,6 @@ export class DelegateTransaction extends BaseTransaction {
validator.errors,
) as TransactionError[];

if (this.type !== TRANSACTION_DELEGATE_TYPE) {
errors.push(
new TransactionError(
'Invalid type',
this.id,
'.type',
this.type,
TRANSACTION_DELEGATE_TYPE,
),
);
}

if (!this.amount.eq(0)) {
errors.push(
new TransactionError(
Expand Down
14 changes: 1 addition & 13 deletions elements/lisk-transactions/src/3_vote_transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ const PREFIX_UNVOTE = '-';
const MAX_VOTE_PER_ACCOUNT = 101;
const MIN_VOTE_PER_TX = 1;
const MAX_VOTE_PER_TX = 33;
const TRANSACTION_VOTE_TYPE = 3;

export interface VoteAsset {
readonly votes: ReadonlyArray<string>;
Expand Down Expand Up @@ -63,6 +62,7 @@ export const voteAssetFormatSchema = {
export class VoteTransaction extends BaseTransaction {
public readonly containsUniqueData: boolean;
public readonly asset: VoteAsset;
public static TYPE = 3;

public constructor(rawTransaction: unknown) {
super(rawTransaction);
Expand Down Expand Up @@ -151,18 +151,6 @@ export class VoteTransaction extends BaseTransaction {
);
}

if (this.type !== TRANSACTION_VOTE_TYPE) {
errors.push(
new TransactionError(
'Invalid type',
this.id,
'.type',
this.type,
TRANSACTION_VOTE_TYPE,
),
);
}

try {
validateAddress(this.recipientId);
} catch (err) {
Expand Down
16 changes: 2 additions & 14 deletions elements/lisk-transactions/src/4_multisignature_transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ import { createResponse, Status, TransactionResponse } from './response';
import { TransactionJSON } from './transaction_types';
import { validateMultisignatures, validateSignature, validator } from './utils';

const TRANSACTION_MULTISIGNATURE_TYPE = 4;

export const multisignatureAssetFormatSchema = {
type: 'object',
required: ['multisignature'],
Expand Down Expand Up @@ -94,8 +92,10 @@ export interface MultiSignatureAsset {

export class MultisignatureTransaction extends BaseTransaction {
public readonly asset: MultiSignatureAsset;
public static TYPE = 4;
protected _multisignatureStatus: MultisignatureStatus =
MultisignatureStatus.PENDING;

public constructor(rawTransaction: unknown) {
super(rawTransaction);
const tx = (typeof rawTransaction === 'object' && rawTransaction !== null
Expand Down Expand Up @@ -159,18 +159,6 @@ export class MultisignatureTransaction extends BaseTransaction {
validator.errors,
) as TransactionError[];

if (this.type !== TRANSACTION_MULTISIGNATURE_TYPE) {
errors.push(
new TransactionError(
'Invalid type',
this.id,
'.type',
this.type,
TRANSACTION_MULTISIGNATURE_TYPE,
),
);
}

if (!this.amount.eq(0)) {
errors.push(
new TransactionError(
Expand Down
19 changes: 3 additions & 16 deletions elements/lisk-transactions/src/5_dapp_transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ import { convertToAssetError, TransactionError } from './errors';
import { TransactionJSON } from './transaction_types';
import { stringEndsWith, validator } from './utils/validation';

const TRANSACTION_DAPP_TYPE = 5;

export interface DappAsset {
readonly dapp: {
readonly category: number;
Expand Down Expand Up @@ -92,6 +90,7 @@ export const dappAssetFormatSchema = {
export class DappTransaction extends BaseTransaction {
public readonly containsUniqueData: boolean;
public readonly asset: DappAsset;
public static TYPE = 5;

public constructor(rawTransaction: unknown) {
super(rawTransaction);
Expand Down Expand Up @@ -213,18 +212,6 @@ export class DappTransaction extends BaseTransaction {
validator.errors,
) as TransactionError[];

if (this.type !== TRANSACTION_DAPP_TYPE) {
errors.push(
new TransactionError(
'Invalid type',
this.id,
'.type',
this.type,
TRANSACTION_DAPP_TYPE,
),
);
}

if (!this.amount.eq(0)) {
errors.push(
new TransactionError(
Expand Down Expand Up @@ -317,7 +304,7 @@ export class DappTransaction extends BaseTransaction {
const errors: TransactionError[] = [];
const nameExists = store.transaction.find(
(transaction: TransactionJSON) =>
transaction.type === TRANSACTION_DAPP_TYPE &&
transaction.type === DappTransaction.TYPE &&
transaction.id !== this.id &&
(transaction.asset as DappAsset).dapp &&
(transaction.asset as DappAsset).dapp.name === this.asset.dapp.name,
Expand All @@ -335,7 +322,7 @@ export class DappTransaction extends BaseTransaction {

const linkExists = store.transaction.find(
(transaction: TransactionJSON) =>
transaction.type === TRANSACTION_DAPP_TYPE &&
transaction.type === DappTransaction.TYPE &&
transaction.id !== this.id &&
(transaction.asset as DappAsset).dapp &&
(transaction.asset as DappAsset).dapp.link === this.asset.dapp.link,
Expand Down
14 changes: 14 additions & 0 deletions elements/lisk-transactions/src/base_transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ export abstract class BaseTransaction {
public readonly fee: BigNum;
public receivedAt?: Date;

public static TYPE: number;

protected _id?: string;
protected _signature?: string;
protected _signSignature?: string;
Expand Down Expand Up @@ -264,6 +266,18 @@ export abstract class BaseTransaction {
errors.push(idError);
}

if (this.type !== (this.constructor as typeof BaseTransaction).TYPE) {
errors.push(
new TransactionError(
`Invalid type`,
this.id,
'.type',
this.type,
(this.constructor as typeof BaseTransaction).TYPE,
),
);
}

return createResponse(this.id, errors);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import { TransactionJSON } from '../../src/transaction_types';
import { TransactionError } from '../../src/errors';

export class TestTransaction extends BaseTransaction {
public static TYPE = 0;

public assetToJSON(): object {
return {};
}
Expand Down

0 comments on commit ac1f565

Please sign in to comment.