All files / src queue_checkers.ts

97.37% Statements 37/38
37.5% Branches 3/8
100% Functions 15/15
97.14% Lines 34/35

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 925x                     5x     161x 53853x             53853x 2400x         51453x     5x       104x     22333x     5x     43x   36207x     5x     8x 8x 2022x     8x     5x     136x 3981x   136x     5x     10x 10x 10x 2062x 2062x   10x     5x     8x 8x 2022x     8x    
import { getAddressFromPublicKey } from '@liskhq/lisk-cryptography';
 
import { Transaction } from './transaction_pool';
 
export type TransactionFilterableKeys =
	| 'id'
	| 'recipientId'
	| 'senderPublicKey'
	| 'senderId'
	| 'type';
 
export const checkTransactionPropertyForValues = (
	values: ReadonlyArray<string | number>,
	propertyName: TransactionFilterableKeys,
): ((transaction: Transaction) => boolean) => (transaction: Transaction) => {
	Iif (propertyName === 'recipientId') {
		return transaction.asset.recipientId &&
			typeof transaction.asset.recipientId === 'string'
			? values.includes(transaction.asset.recipientId)
			: false;
	}
 
	if (propertyName === 'senderId') {
		return values.includes(
			getAddressFromPublicKey(transaction.senderPublicKey),
		);
	}
 
	return values.includes(transaction[propertyName]);
};
 
export const returnTrueUntilLimit = (
	limit: number,
): ((transaction: Transaction) => boolean) => {
	// tslint:disable-next-line:no-let
	let current = 0;
 
	// tslint:disable-next-line:increment-decrement
	return _ => current++ < limit;
};
 
export const checkTransactionForExpiry = (): ((
	transaction: Transaction,
) => boolean) => {
	const timeNow = new Date();
 
	return (transaction: Transaction) => transaction.isExpired(timeNow);
};
 
export const checkTransactionForSenderPublicKey = (
	transactions: ReadonlyArray<Transaction>,
): ((transaction: Transaction) => boolean) => {
	const senderProperty: TransactionFilterableKeys = 'senderPublicKey';
	const senderPublicKeys = transactions.map(
		transaction => transaction[senderProperty],
	);
 
	return checkTransactionPropertyForValues(senderPublicKeys, senderProperty);
};
 
export const checkTransactionForId = (
	transactions: ReadonlyArray<Transaction>,
): ((transaction: Transaction) => boolean) => {
	const idProperty: TransactionFilterableKeys = 'id';
	const ids = transactions.map(transaction => transaction.id);
 
	return checkTransactionPropertyForValues(ids, idProperty);
};
 
export const checkTransactionForSenderIdWithRecipientIds = (
	transactions: ReadonlyArray<Transaction>,
): ((transaction: Transaction) => boolean) => {
	const recipientProperty: TransactionFilterableKeys = 'recipientId';
	const senderIdProperty: TransactionFilterableKeys = 'senderId';
	const recipients = transactions
		.map(transaction => transaction.asset[recipientProperty])
		.filter(id => id !== undefined) as ReadonlyArray<string>;
 
	return checkTransactionPropertyForValues(recipients, senderIdProperty);
};
 
export const checkTransactionForTypes = (
	transactions: ReadonlyArray<Transaction>,
): ((transaction: Transaction) => boolean) => {
	const typeProperty: TransactionFilterableKeys = 'type';
	const types: ReadonlyArray<number> = transactions.map(
		(transaction: Transaction) => transaction[typeProperty],
	);
 
	return checkTransactionPropertyForValues(types, typeProperty);
};