Skip to content

Commit

Permalink
Refactors precheck; adds JSDoc; removes unecessary let etc.
Browse files Browse the repository at this point in the history
Signed-off-by: Konstantina Blazhukova <konstantina.blajukova@gmail.com>

Removes console.log

Signed-off-by: Konstantina Blazhukova <konstantina.blajukova@gmail.com>

fix: comments

Signed-off-by: georgi-l95 <glazarov95@gmail.com>

fix: comments

Signed-off-by: georgi-l95 <glazarov95@gmail.com>
  • Loading branch information
konstantinabl authored and georgi-l95 committed Apr 3, 2024
1 parent 996e82e commit d185259
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 23 deletions.
87 changes: 65 additions & 22 deletions packages/relay/src/lib/precheck.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,32 +26,52 @@ import constants from './constants';
import { ethers, Transaction } from 'ethers';
import { formatRequestIdMessage, prepend0x } from '../formatters';

/**
* Precheck class for handling various prechecks before sending a raw transaction.
*/
export class Precheck {
private mirrorNodeClient: MirrorNodeClient;
private readonly mirrorNodeClient: MirrorNodeClient;
private readonly chain: string;
private readonly logger: Logger;

/**
* Creates an instance of Precheck.
* @param {MirrorNodeClient} mirrorNodeClient - The MirrorNodeClient instance.
* @param {Logger} logger - The logger instance.
* @param {string} chainId - The chain ID.
*/
constructor(mirrorNodeClient: MirrorNodeClient, logger: Logger, chainId: string) {
this.mirrorNodeClient = mirrorNodeClient;
this.logger = logger;
this.chain = chainId;
}

/**
* Parses the transaction if needed.
* @param {string | Transaction} transaction - The transaction to parse.
* @returns {Transaction} The parsed transaction.
*/
public static parseTxIfNeeded(transaction: string | Transaction): Transaction {
return typeof transaction === 'string' ? Transaction.from(transaction) : transaction;
}

value(tx: Transaction) {
/**
* Checks if the value of the transaction is valid.
* @param {Transaction} tx - The transaction.
*/
value(tx: Transaction): void {
if (tx.data === EthImpl.emptyHex && tx.value < constants.TINYBAR_TO_WEIBAR_COEF) {
throw predefined.VALUE_TOO_LOW;
}
}

/**
* @param transaction
* @param gasPrice
* Sends a raw transaction after performing various prechecks.
* @param {ethers.Transaction} parsedTx - The parsed transaction.
* @param {number} gasPrice - The gas price.
* @param {string} [requestId] - The request ID.
*/
async sendRawTransactionCheck(parsedTx: ethers.Transaction, gasPrice: number, requestId?: string) {
async sendRawTransactionCheck(parsedTx: ethers.Transaction, gasPrice: number, requestId?: string): Promise<void> {
this.gasLimit(parsedTx, requestId);
const mirrorAccountInfo = await this.verifyAccount(parsedTx, requestId);
await this.nonce(parsedTx, mirrorAccountInfo.ethereum_nonce, requestId);
Expand All @@ -61,7 +81,13 @@ export class Precheck {
await this.balance(parsedTx, mirrorAccountInfo, requestId);
}

async verifyAccount(tx: Transaction, requestId?: string) {
/**
* Verifies the account.
* @param {Transaction} tx - The transaction.
* @param {string} [requestId] - The request ID.
* @returns {Promise<any>} A Promise.
*/
async verifyAccount(tx: Transaction, requestId?: string): Promise<any> {
const requestIdPrefix = formatRequestIdMessage(requestId);
// verify account
const accountInfo = await this.mirrorNodeClient.getAccount(tx.from!, requestId);
Expand All @@ -80,9 +106,12 @@ export class Precheck {
}

/**
* @param tx
* Checks the nonce of the transaction.
* @param {Transaction} tx - The transaction.
* @param {number} accountInfoNonce - The nonce of the account.
* @param {string} [requestId] - The request ID.
*/
async nonce(tx: Transaction, accountInfoNonce: number, requestId?: string) {
nonce(tx: Transaction, accountInfoNonce: number, requestId?: string): void {
const requestIdPrefix = formatRequestIdMessage(requestId);
this.logger.trace(
`${requestIdPrefix} Nonce precheck for sendRawTransaction(tx.nonce=${tx.nonce}, accountInfoNonce=${accountInfoNonce})`,
Expand All @@ -95,9 +124,11 @@ export class Precheck {
}

/**
* @param tx
* Checks the chain ID of the transaction.
* @param {Transaction} tx - The transaction.
* @param {string} [requestId] - The request ID.
*/
chainId(tx: Transaction, requestId?: string) {
chainId(tx: Transaction, requestId?: string): void {
const requestIdPrefix = formatRequestIdMessage(requestId);
const txChainId = prepend0x(Number(tx.chainId).toString(16));
const passes = this.isLegacyUnprotectedEtx(tx) || txChainId === this.chain;
Expand All @@ -123,10 +154,12 @@ export class Precheck {
}

/**
* @param tx
* @param gasPrice
* Checks the gas price of the transaction.
* @param {Transaction} tx - The transaction.
* @param {number} gasPrice - The gas price.
* @param {string} [requestId] - The request ID.
*/
gasPrice(tx: Transaction, gasPrice: number, requestId?: string) {
gasPrice(tx: Transaction, gasPrice: number, requestId?: string): void {
const requestIdPrefix = formatRequestIdMessage(requestId);
const minGasPrice = BigInt(gasPrice);
const txGasPrice = tx.gasPrice || tx.maxFeePerGas! + tx.maxPriorityFeePerGas!;
Expand All @@ -153,10 +186,12 @@ export class Precheck {
}

/**
* @param tx
* @param callerName
* Checks the balance of the sender account.
* @param {Transaction} tx - The transaction.
* @param {any} account - The account information.
* @param {string} [requestId] - The request ID.
*/
async balance(tx: Transaction, account: any, requestId?: string) {
balance(tx: Transaction, account: any, requestId?: string): void {
const requestIdPrefix = formatRequestIdMessage(requestId);
const result = {
passes: false,
Expand Down Expand Up @@ -205,9 +240,11 @@ export class Precheck {
}

/**
* @param tx
* Checks the gas limit of the transaction.
* @param {Transaction} tx - The transaction.
* @param {string} [requestId] - The request ID.
*/
gasLimit(tx: Transaction, requestId?: string) {
gasLimit(tx: Transaction, requestId?: string): void {
const requestIdPrefix = formatRequestIdMessage(requestId);
const gasLimit = Number(tx.gasLimit);
const failBaseLog = 'Failed gasLimit precheck for sendRawTransaction(transaction=%s).';
Expand Down Expand Up @@ -237,11 +274,12 @@ export class Precheck {
* Calculates the intrinsic gas cost based on the number of bytes in the data field.
* Using a loop that goes through every two characters in the string it counts the zero and non-zero bytes.
* Every two characters that are packed together and are both zero counts towards zero bytes.
* @param data
* @param {string} data - The data with the bytes to be calculated
* @returns {number} The intrinsic gas cost.
* @private
*/
private static transactionIntrinsicGasCost(data: string) {
let trimmedData = data.replace('0x', '');
private static transactionIntrinsicGasCost(data: string): number {
const trimmedData = data.replace('0x', '');

let zeros = 0;
let nonZeros = 0;
Expand All @@ -261,7 +299,8 @@ export class Precheck {

/**
* Converts hex string to bytes array
* @param hex the hex string you want to convert
* @param {string} hex - The hex string you want to convert.
* @returns {Uint8Array} The bytes array.
*/
hexToBytes(hex: string): Uint8Array {
if (hex === '') {
Expand All @@ -277,6 +316,10 @@ export class Precheck {
return Uint8Array.from(Buffer.from(hex, 'hex'));
}

/**
* Checks the size of the transaction.
* @param {string} transaction - The transaction to check.
*/
checkSize(transaction: string): void {
const transactionToBytes: Uint8Array = this.hexToBytes(transaction);
const transactionSize: number = transactionToBytes.length;
Expand Down
1 change: 0 additions & 1 deletion packages/relay/tests/lib/precheck.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,6 @@ describe('Precheck', async function () {
await precheck.verifyAccount(parsedTx);
expectedError();
} catch (e: any) {
console.log(e);
expect(e).to.exist;
expect(e.code).to.eq(-32001);
expect(e.name).to.eq('Resource not found');
Expand Down

0 comments on commit d185259

Please sign in to comment.