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

Update transaction test using updated protocol-spec - Closes #5368, #5335 and #5209 #5383

Merged
merged 5 commits into from
Jun 4, 2020
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
37 changes: 35 additions & 2 deletions elements/lisk-transactions/src/12_multisignature_transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,17 @@ const setMemberAccounts = async (
}
};

const hasDuplicateKey = (keys: Buffer[]): boolean => {
const temp: { [key: string]: boolean | undefined } = {};
for (const key of keys) {
if (temp[key.toString('base64')]) {
return true;
}
temp[key.toString('base64')] = true;
}
return false;
};

export interface MultiSignatureAsset {
mandatoryKeys: Array<Readonly<Buffer>>;
optionalKeys: Array<Readonly<Buffer>>;
Expand Down Expand Up @@ -232,6 +243,26 @@ export class MultisignatureTransaction extends BaseTransaction {

const { mandatoryKeys, optionalKeys, numberOfSignatures } = this.asset;

if (hasDuplicateKey(mandatoryKeys as Buffer[])) {
errors.push(
new TransactionError(
'MandatoryKeys contains duplicate public keys',
this.id,
'.asset.mandatoryKeys',
),
);
}

if (hasDuplicateKey(optionalKeys as Buffer[])) {
errors.push(
new TransactionError(
'OptionalKeys contains duplicate public keys',
this.id,
'.asset.optionalKeys',
),
);
}

// Check if key count is less than number of required signatures
if (mandatoryKeys.length + optionalKeys.length < numberOfSignatures) {
errors.push(
Expand Down Expand Up @@ -276,8 +307,10 @@ export class MultisignatureTransaction extends BaseTransaction {
}

// Check if keys are repeated between mandatory and optional key sets
const repeatedKeys = mandatoryKeys.filter(value =>
optionalKeys.includes(value),
const repeatedKeys = mandatoryKeys.filter(
value =>
optionalKeys.find(optional => optional.equals(value as Buffer)) !==
undefined,
);
if (repeatedKeys.length > 0) {
errors.push(
Expand Down
13 changes: 8 additions & 5 deletions elements/lisk-transactions/src/13_vote_transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ const voteAssetSchema = {
properties: {
votes: {
type: 'array',
minItems: 1,
maxItems: 20,
items: {
type: 'object',
required: ['delegateAddress', 'amount'],
Expand Down Expand Up @@ -80,9 +82,9 @@ export class VoteTransaction extends BaseTransaction {
const errors = [];
let upvoteCount = 0;
let downvoteCount = 0;
const addressSet = new Set();
const addressSet: { [addressStr: string]: boolean } = {};
for (const vote of this.asset.votes) {
addressSet.add(vote.delegateAddress);
addressSet[vote.delegateAddress.toString('base64')] = true;
if (vote.amount > BigInt(0)) {
upvoteCount += 1;
} else if (vote.amount < BigInt(0)) {
Expand Down Expand Up @@ -129,7 +131,7 @@ export class VoteTransaction extends BaseTransaction {
),
);
}
if (addressSet.size !== this.asset.votes.length) {
if (Object.keys(addressSet).length !== this.asset.votes.length) {
errors.push(
new TransactionError(
'Delegate address must be unique',
Expand Down Expand Up @@ -167,7 +169,7 @@ export class VoteTransaction extends BaseTransaction {
vote.delegateAddress,
);

if (!votedDelegate.asset.delegate.username) {
if (votedDelegate.asset.delegate.username === '') {
errors.push(
new TransactionError(
'Voted delegate is not registered',
Expand Down Expand Up @@ -206,7 +208,8 @@ export class VoteTransaction extends BaseTransaction {
// Delete entry when amount becomes 0
if (sender.asset.sentVotes[originalUpvoteIndex].amount === BigInt(0)) {
sender.asset.sentVotes = sender.asset.sentVotes.filter(
senderVote => senderVote.delegateAddress !== vote.delegateAddress,
senderVote =>
!senderVote.delegateAddress.equals(vote.delegateAddress),
);
}
// Create unlocking object
Expand Down
11 changes: 6 additions & 5 deletions elements/lisk-transactions/src/14_unlock_transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ const unlockAssetSchema = {
properties: {
unlockObjects: {
type: 'array',
minItems: 1,
maxItems: 20,
items: {
type: 'object',
required: ['delegateAddress', 'amount', 'unvoteHeight'],
Expand Down Expand Up @@ -71,10 +73,9 @@ const getWaitingPeriod = (
unlockObject: Unlock,
): number => {
const currentHeight = lastBlockHeight + 1;
const waitTime =
sender.address === delegateAccount.address
? WAIT_TIME_SELF_VOTE
: WAIT_TIME_VOTE;
const waitTime = sender.address.equals(delegateAccount.address)
? WAIT_TIME_SELF_VOTE
: WAIT_TIME_VOTE;

return waitTime - (currentHeight - unlockObject.unvoteHeight);
};
Expand Down Expand Up @@ -178,7 +179,7 @@ export class UnlockTransaction extends BaseTransaction {
const unlockIndex = sender.asset.unlocking.findIndex(
obj =>
obj.amount === unlock.amount &&
obj.delegateAddress === unlock.delegateAddress &&
obj.delegateAddress.equals(unlock.delegateAddress) &&
obj.unvoteHeight === unlock.unvoteHeight,
);
if (unlockIndex < 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,7 @@ import {
MAX_PUNISHABLE_BLOCK_HEIGHT_DIFFERENCE,
} from './constants';
import { TransactionError } from './errors';
import {
getPunishmentPeriod,
validateSignature,
} from './utils';
import { getPunishmentPeriod, validateSignature } from './utils';
import { BlockHeader, BaseTransactionInput, AccountAsset } from './types';

const signingBlockHeaderSchema = {
Expand Down Expand Up @@ -124,8 +121,9 @@ export class ProofOfMisbehaviorTransaction extends BaseTransaction {
const errors = [];

if (
this.asset.header1.generatorPublicKey !==
this.asset.header2.generatorPublicKey
!this.asset.header1.generatorPublicKey.equals(
this.asset.header2.generatorPublicKey,
)
) {
errors.push(
new TransactionError(
Expand Down
19 changes: 12 additions & 7 deletions elements/lisk-transactions/src/base_transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,20 +137,20 @@ export abstract class BaseTransaction {
}
/* End Getters */

public getSigningBytes(): Buffer {
public getBytes(): Buffer {
const transactionBytes = codec.encode(BaseTransaction.BASE_SCHEMA, ({
...this,
asset: this.getAssetBytes(),
signatures: [],
asset: this._getAssetBytes(),
} as unknown) as GenericObject);

return transactionBytes;
}

public getBytes(): Buffer {
public getSigningBytes(): Buffer {
const transactionBytes = codec.encode(BaseTransaction.BASE_SCHEMA, ({
...this,
asset: this.getAssetBytes(),
asset: this._getAssetBytes(),
signatures: [],
} as unknown) as GenericObject);

return transactionBytes;
Expand All @@ -162,6 +162,11 @@ export abstract class BaseTransaction {
return createResponse(this.id, errors);
}

const assetErrors = this.validateAsset();
if (assetErrors.length > 0) {
errors.push(...assetErrors);
}

if (this.type !== (this.constructor as typeof BaseTransaction).TYPE) {
errors.push(
new TransactionError(
Expand Down Expand Up @@ -196,7 +201,7 @@ export abstract class BaseTransaction {
const senderPublicKeyError = verifySenderPublicKey(
this.id,
sender,
this.senderPublicKeyStr,
this.senderPublicKey,
);
if (senderPublicKeyError) {
errors.push(senderPublicKeyError);
Expand Down Expand Up @@ -391,7 +396,7 @@ export abstract class BaseTransaction {
return [];
}

protected getAssetBytes(): Buffer {
private _getAssetBytes(): Buffer {
const assetSchema = (this.constructor as typeof BaseTransaction)
.ASSET_SCHEMA;
return codec.encode(
Expand Down
11 changes: 7 additions & 4 deletions elements/lisk-transactions/src/cast_votes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,13 @@ const validateInputs = ({
}
};

const convertVotes = (votes: ReadonlyArray<RawAssetVote>): ReadonlyArray<Vote> => votes.map(vote => ({
delegateAddress: hexToBuffer(vote.delegateAddress),
amount: BigInt(vote.amount),
}));
const convertVotes = (
votes: ReadonlyArray<RawAssetVote>,
): ReadonlyArray<Vote> =>
votes.map(vote => ({
delegateAddress: hexToBuffer(vote.delegateAddress),
amount: BigInt(vote.amount),
}));

export const castVotes = (inputs: CastVoteInputs): Partial<TransactionJSON> => {
validateInputs(inputs);
Expand Down
2 changes: 0 additions & 2 deletions elements/lisk-transactions/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ import { unlockToken } from './unlock_token';
import {
convertBeddowsToLSK,
convertLSKToBeddows,
getId,
prependMinusToPublicKeys,
prependPlusToPublicKeys,
validateSenderIdAndPublicKey,
Expand All @@ -51,7 +50,6 @@ import {

const exposedUtils = {
convertBeddowsToLSK,
getId,
convertLSKToBeddows,
prependMinusToPublicKeys,
prependPlusToPublicKeys,
Expand Down
13 changes: 6 additions & 7 deletions elements/lisk-transactions/src/register_delegate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ import { validateNetworkIdentifier } from '@liskhq/lisk-validator';
import { DelegateTransaction } from './10_delegate_transaction';
import { USERNAME_MAX_LENGTH } from './constants';
import { TransactionJSON } from './types';
import { createBaseTransaction, baseTransactionToJSON, convertKeysToBuffer } from './utils';
import {
createBaseTransaction,
baseTransactionToJSON,
convertKeysToBuffer,
} from './utils';

export interface RegisterDelegateInputs {
readonly passphrase?: string;
Expand Down Expand Up @@ -57,12 +61,7 @@ export const registerDelegate = (
inputs: RegisterDelegateInputs,
): Partial<TransactionJSON> => {
validateInputs(inputs);
const {
username,
passphrase,
passphrases,
senderPublicKey,
} = inputs;
const { username, passphrase, passphrases, senderPublicKey } = inputs;
const networkIdentifier = hexToBuffer(inputs.networkIdentifier);

const transaction = {
Expand Down
15 changes: 10 additions & 5 deletions elements/lisk-transactions/src/register_multisignature_account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,12 @@ import {
MIN_NUMBER_OF_SIGNATURES,
} from './constants';
import { TransactionJSON } from './types';
import { createBaseTransaction, findRepeatedKeys, convertKeysToBuffer, baseTransactionToJSON } from './utils';
import {
createBaseTransaction,
findRepeatedKeys,
convertKeysToBuffer,
baseTransactionToJSON,
} from './utils';

export interface RegisterMultisignatureInputs {
readonly senderPassphrase: string;
Expand Down Expand Up @@ -89,16 +94,16 @@ const validateInputs = ({
) {
throw new Error(
`Please provide a valid numberOfSignatures. numberOfSignatures (${numberOfSignatures.toString()}) is bigger than the count of optional (${
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
optionalPublicKeys.length
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
optionalPublicKeys.length
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
}) and mandatory (${mandatoryPublicKeys.length}) keys.`,
);
}

if (
mandatoryPublicKeys.length + optionalPublicKeys.length >
MAX_NUMBER_OF_KEYS ||
MAX_NUMBER_OF_KEYS ||
mandatoryPublicKeys.length + optionalPublicKeys.length < MIN_NUMBER_OF_KEYS
) {
throw new Error(
Expand Down
Loading