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

Commit

Permalink
♻️ Allow generic type usage for base transaction asset
Browse files Browse the repository at this point in the history
  • Loading branch information
ManuGowda committed May 28, 2020
1 parent 15a9ea1 commit d6c8702
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 34 deletions.
2 changes: 1 addition & 1 deletion elements/lisk-transactions/src/10_delegate_transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export const delegateRegistrationAssetSchema = {
},
};

export class DelegateTransaction extends BaseTransaction {
export class DelegateTransaction extends BaseTransaction<DelegateAsset> {
public static TYPE = 10;
public static NAME_FEE = BigInt(DELEGATE_NAME_FEE);
public static ASSET_SCHEMA = delegateRegistrationAssetSchema;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,9 @@ import {
signData,
bufferToHex,
} from '@liskhq/lisk-cryptography';
import { validator } from '@liskhq/lisk-validator';

import { BaseTransaction, StateStore } from './base_transaction';
import { convertToAssetError, TransactionError } from './errors';
import { TransactionError } from './errors';
import { createResponse, TransactionResponse } from './response';
import {
buildPublicKeyPassphraseDict,
Expand Down Expand Up @@ -81,7 +80,9 @@ export interface MultiSignatureAsset {
readonly numberOfSignatures: number;
}

export class MultisignatureTransaction extends BaseTransaction {
export class MultisignatureTransaction extends BaseTransaction<
MultiSignatureAsset
> {
public static TYPE = 12;
public static ASSET_SCHEMA = multisigRegAssetSchema;
public readonly asset: MultiSignatureAsset;
Expand Down
18 changes: 9 additions & 9 deletions elements/lisk-transactions/src/13_vote_transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ const TEN_UNIT = BigInt(10) * BigInt(10) ** BigInt(8);
const MAX_VOTE = 10;
const MAX_UNLOCKING = 20;

export class VoteTransaction extends BaseTransaction {
export class VoteTransaction extends BaseTransaction<VoteAsset> {
public static TYPE = 13;
public static ASSET_SCHEMA = voteAssetSchema;
public readonly asset: VoteAsset;
Expand Down Expand Up @@ -178,8 +178,8 @@ export class VoteTransaction extends BaseTransaction {
continue;
}
if (vote.amount < BigInt(0)) {
const originalUpvoteIndex = sender.votes.findIndex(
senderVote => senderVote.delegateAddress.equals(vote.delegateAddress),
const originalUpvoteIndex = sender.votes.findIndex(senderVote =>
senderVote.delegateAddress.equals(vote.delegateAddress),
);
if (originalUpvoteIndex < 0) {
errors.push(
Expand Down Expand Up @@ -238,9 +238,9 @@ export class VoteTransaction extends BaseTransaction {
originalUpvoteIndex > -1
? sender.votes[originalUpvoteIndex]
: {
delegateAddress: vote.delegateAddress,
amount: BigInt(0),
};
delegateAddress: vote.delegateAddress,
amount: BigInt(0),
};
upvote.amount += vote.amount;
// Special case for postgres because maximum is int64 for bigint in postgres
if (upvote.amount > BigInt(MAX_INT64)) {
Expand Down Expand Up @@ -308,9 +308,9 @@ export class VoteTransaction extends BaseTransaction {
originalUpvoteIndex > -1
? sender.votes[originalUpvoteIndex]
: {
delegateAddress: vote.delegateAddress,
amount: BigInt(0),
};
delegateAddress: vote.delegateAddress,
amount: BigInt(0),
};
// Add back the vote
upvote.amount += vote.amount * BigInt(-1);
sender.votes[index] = upvote;
Expand Down
2 changes: 1 addition & 1 deletion elements/lisk-transactions/src/14_unlock_transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ const getWaitingPeriod = (
return waitTime - (currentHeight - unlockObject.unvoteHeight);
};

export class UnlockTransaction extends BaseTransaction {
export class UnlockTransaction extends BaseTransaction<UnlockAsset> {
public static TYPE = 14;
public static ASSET_SCHEMA = unlockAssetSchema;
public readonly asset: UnlockAsset;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,18 @@
*/

import { codec, GenericObject } from '@liskhq/lisk-codec';
import { getAddressFromPublicKey, bufferToHex } from '@liskhq/lisk-cryptography';
import {
getAddressFromPublicKey,
bufferToHex,
} from '@liskhq/lisk-cryptography';

import { BaseTransaction, StateStore } from './base_transaction';
import {
MAX_POM_HEIGHTS,
MAX_PUNISHABLE_BLOCK_HEIGHT_DIFFERENCE,
} from './constants';
import { TransactionError } from './errors';
import {
getPunishmentPeriod,
validateSignature,
} from './utils';
import { getPunishmentPeriod, validateSignature } from './utils';
import { BlockHeader } from './types';

const proofOfMisbehaviorAssetSchema = {
Expand All @@ -47,7 +47,22 @@ const proofOfMisbehaviorAssetSchema = {
const blockHeaderSchema = {
$id: 'lisk/block-header',
type: 'object',
required: ['height', 'version', 'timestamp', 'previousBlockId', 'generatorPublicKey', 'numberOfTransactions', 'payloadLength', 'transactionRoot', 'maxHeightPreviouslyForged', 'maxHeightPrevoted', 'totalAmount', 'totalFee', 'reward', 'seedReveal'],
required: [
'height',
'version',
'timestamp',
'previousBlockId',
'generatorPublicKey',
'numberOfTransactions',
'payloadLength',
'transactionRoot',
'maxHeightPreviouslyForged',
'maxHeightPrevoted',
'totalAmount',
'totalFee',
'reward',
'seedReveal',
],
properties: {
height: {
dataType: 'uint32',
Expand Down Expand Up @@ -110,7 +125,9 @@ export interface ProofOfMisbehaviorAsset {
reward: bigint;
}

export class ProofOfMisbehaviorTransaction extends BaseTransaction {
export class ProofOfMisbehaviorTransaction extends BaseTransaction<
ProofOfMisbehaviorAsset
> {
public static TYPE = 15;
public static ASSET_SCHEMA = proofOfMisbehaviorAssetSchema;
public readonly asset: ProofOfMisbehaviorAsset;
Expand Down Expand Up @@ -401,6 +418,9 @@ export class ProofOfMisbehaviorTransaction extends BaseTransaction {
// eslint-disable-next-line class-methods-use-this
private _getBlockHeaderBytes(header: BlockHeader): Buffer {
const { blockSignature, ...restOfHeader } = header;
return codec.encode(blockHeaderSchema, restOfHeader as unknown as GenericObject);
return codec.encode(
blockHeaderSchema,
(restOfHeader as unknown) as GenericObject,
);
}
}
10 changes: 7 additions & 3 deletions elements/lisk-transactions/src/8_transfer_transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export const transferAssetSchema = {
},
};

export class TransferTransaction extends BaseTransaction {
export class TransferTransaction extends BaseTransaction<TransferAsset> {
public static TYPE = 8;
public static ASSET_SCHEMA = transferAssetSchema;
public readonly asset: TransferAsset;
Expand All @@ -67,7 +67,9 @@ export class TransferTransaction extends BaseTransaction {

sender.balance -= this.asset.amount;
store.account.set(sender.address, sender);
const recipient = await store.account.getOrDefault(this.asset.recipientAddress);
const recipient = await store.account.getOrDefault(
this.asset.recipientAddress,
);

recipient.balance += this.asset.amount;

Expand Down Expand Up @@ -117,7 +119,9 @@ export class TransferTransaction extends BaseTransaction {

sender.balance = updatedSenderBalance;
store.account.set(sender.address, sender);
const recipient = await store.account.getOrDefault(this.asset.recipientAddress);
const recipient = await store.account.getOrDefault(
this.asset.recipientAddress,
);
recipient.balance -= this.asset.amount;

store.account.set(recipient.address, recipient);
Expand Down
13 changes: 8 additions & 5 deletions elements/lisk-transactions/src/base_transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export interface StateStore {
export const ENTITY_ACCOUNT = 'account';
export const ENTITY_TRANSACTION = 'transaction';

export abstract class BaseTransaction {
export abstract class BaseTransaction<T> {
public static TYPE: number;
// Minimum remaining balance requirement for any account to perform a transaction
public static MIN_REMAINING_BALANCE = BigInt('5000000'); // 0.05 LSK
Expand All @@ -74,7 +74,7 @@ export abstract class BaseTransaction {

public readonly id: Buffer;
public readonly type: number;
public asset: object;
public asset: T;
public nonce: bigint;
public fee: bigint;
public senderPublicKey: Buffer;
Expand All @@ -86,7 +86,7 @@ export abstract class BaseTransaction {
private readonly _senderPublicKey: string;
private readonly _senderId: Buffer;

public constructor(transaction: BaseTransaction) {
public constructor(transaction: BaseTransaction<T>) {
this.id = transaction.id;
this.type = transaction.type;
this.asset = transaction.asset;
Expand Down Expand Up @@ -384,7 +384,10 @@ export abstract class BaseTransaction {
protected getAssetBytes(): Buffer {
const assetSchema = (this.constructor as typeof BaseTransaction)
.ASSET_SCHEMA;
return codec.encode(assetSchema as Schema, this.asset as GenericObject);
return codec.encode(
assetSchema as Schema,
(this.asset as unknown) as GenericObject,
);
}

private _validateSchema(): ReadonlyArray<TransactionError> {
Expand All @@ -396,7 +399,7 @@ export abstract class BaseTransaction {

const assetSchemaErrors = validator.validate(
BaseTransaction.ASSET_SCHEMA,
this.asset,
(this.asset as unknown) as GenericObject,
);
const assetErrors = convertToTransactionError(
this.id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@
import * as cryptography from '@liskhq/lisk-cryptography';

import { DelegateTransaction } from './10_delegate_transaction';
import { MultisignatureTransaction } from './12_multisignature_transaction';
import {
MultisignatureTransaction,
MultiSignatureAsset,
} from './12_multisignature_transaction';
import { VoteTransaction } from './13_vote_transaction';
import { UnlockTransaction } from './14_unlock_transaction';
import { TransferTransaction } from './8_transfer_transaction';
Expand All @@ -33,7 +36,7 @@ const transactionMap: { readonly [key: number]: any } = {
};

const sanitizeSignaturesArray = (
tx: BaseTransaction,
tx: BaseTransaction<MultiSignatureAsset>,
keys: MultisigKeys,
): void => {
let numberOfSignatures = keys.mandatoryKeys.length + keys.optionalKeys.length;
Expand All @@ -60,7 +63,7 @@ export const signMultiSignatureTransaction = (options: {
readonly passphrase: string;
readonly networkIdentifier: string;
readonly keys: MultisigKeys;
}): BaseTransaction => {
}): BaseTransaction<MultiSignatureAsset> => {
const { transaction, passphrase, networkIdentifier } = options;
if (transaction.type === undefined || transaction.type === null) {
throw new Error('Transaction type is required.');
Expand All @@ -81,7 +84,7 @@ export const signMultiSignatureTransaction = (options: {
const tx = new TransactionClass({
...transaction,
networkIdentifier,
}) as BaseTransaction;
}) as BaseTransaction<MultiSignatureAsset>;

const { publicKey } = cryptography.getPrivateAndPublicKeyFromPassphrase(
passphrase,
Expand Down

0 comments on commit d6c8702

Please sign in to comment.