Skip to content

Commit 217125d

Browse files
committed
chore: rework precheck methods
Signed-off-by: Logan Nguyen <[email protected]> chore: rename signerAccountInfo Signed-off-by: Logan Nguyen <[email protected]> chore: removed unused requestDetails in precheck.ts Signed-off-by: Logan Nguyen <[email protected]> fix: fixed precheck balance test Signed-off-by: Logan Nguyen <[email protected]>
1 parent 9d990a4 commit 217125d

File tree

2 files changed

+55
-163
lines changed

2 files changed

+55
-163
lines changed

packages/relay/src/lib/precheck.ts

Lines changed: 15 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { Logger } from 'pino';
77
import { prepend0x } from '../formatters';
88
import { MirrorNodeClient } from './clients';
99
import constants from './constants';
10-
import { JsonRpcError, predefined } from './errors/JsonRpcError';
10+
import { predefined } from './errors/JsonRpcError';
1111
import { CommonService } from './services';
1212
import { RequestDetails } from './types';
1313

@@ -69,12 +69,12 @@ export class Precheck {
6969
this.transactionSize(parsedTx);
7070
this.transactionType(parsedTx);
7171
this.gasLimit(parsedTx);
72-
const mirrorAccountInfo = await this.verifyAccount(parsedTx, requestDetails);
73-
this.nonce(parsedTx, mirrorAccountInfo.ethereum_nonce);
7472
this.chainId(parsedTx);
7573
this.value(parsedTx);
7674
this.gasPrice(parsedTx, networkGasPriceInWeiBars);
77-
this.balance(parsedTx, mirrorAccountInfo);
75+
const signerAccountInfo = await this.verifyAccount(parsedTx, requestDetails);
76+
this.nonce(parsedTx, signerAccountInfo.ethereum_nonce);
77+
this.balance(parsedTx, signerAccountInfo.balance.balance);
7878
await this.receiverAccount(parsedTx, requestDetails);
7979
}
8080

@@ -87,15 +87,6 @@ export class Precheck {
8787
async verifyAccount(tx: Transaction, requestDetails: RequestDetails): Promise<any> {
8888
const accountInfo = await this.mirrorNodeClient.getAccount(tx.from!, requestDetails);
8989
if (accountInfo == null) {
90-
if (this.logger.isLevelEnabled('trace')) {
91-
this.logger.trace(
92-
`Failed to retrieve address '${
93-
tx.from
94-
}' account details from mirror node on verify account precheck for sendRawTransaction(transaction=${JSON.stringify(
95-
tx,
96-
)})`,
97-
);
98-
}
9990
throw predefined.RESOURCE_NOT_FOUND(`address '${tx.from}'.`);
10091
}
10192

@@ -108,32 +99,22 @@ export class Precheck {
10899
* @param accountInfoNonce - The nonce of the account.
109100
*/
110101
nonce(tx: Transaction, accountInfoNonce: number): void {
111-
if (this.logger.isLevelEnabled('trace')) {
112-
this.logger.trace(
113-
`Nonce precheck for sendRawTransaction(tx.nonce=${tx.nonce}, accountInfoNonce=${accountInfoNonce})`,
114-
);
115-
}
116-
117102
if (accountInfoNonce > tx.nonce) {
118103
throw predefined.NONCE_TOO_LOW(tx.nonce, accountInfoNonce);
119104
}
120105
}
121106

122107
/**
123-
* Checks the chain ID of the transaction.
124-
* @param tx - The transaction.
108+
* Validates that the transaction's chain ID matches the network's chain ID.
109+
* Legacy unprotected transactions (pre-EIP155) are exempt from this check.
110+
*
111+
* @param {Transaction} tx - The transaction to validate.
112+
* @throws {JsonRpcError} If the transaction's chain ID doesn't match the network's chain ID.
125113
*/
126114
chainId(tx: Transaction): void {
127115
const txChainId = prepend0x(Number(tx.chainId).toString(16));
128116
const passes = this.isLegacyUnprotectedEtx(tx) || txChainId === this.chain;
129117
if (!passes) {
130-
if (this.logger.isLevelEnabled('trace')) {
131-
this.logger.trace(
132-
`Failed chainId precheck for sendRawTransaction(transaction=%s, chainId=%s)`,
133-
JSON.stringify(tx),
134-
txChainId,
135-
);
136-
}
137118
throw predefined.UNSUPPORTED_CHAIN_ID(txChainId, this.chain);
138119
}
139120
}
@@ -181,14 +162,6 @@ export class Precheck {
181162
}
182163
}
183164

184-
if (this.logger.isLevelEnabled('trace')) {
185-
this.logger.trace(
186-
`Failed gas price precheck for sendRawTransaction(transaction=%s, gasPrice=%s, requiredGasPrice=%s)`,
187-
JSON.stringify(tx),
188-
txGasPrice,
189-
networkGasPrice,
190-
);
191-
}
192165
throw predefined.GAS_PRICE_TOO_LOW(txGasPrice, networkGasPrice);
193166
}
194167
}
@@ -204,59 +177,15 @@ export class Precheck {
204177

205178
/**
206179
* Checks the balance of the sender account.
207-
* @param tx - The transaction.
208-
* @param account - The account information.
180+
* @param {Transaction} tx - The transaction.
181+
* @param {number} accountBalance - The account balance in tinybars.
209182
*/
210-
balance(tx: Transaction, account: any): void {
211-
const result = {
212-
passes: false,
213-
error: predefined.INSUFFICIENT_ACCOUNT_BALANCE,
214-
};
215-
183+
balance(tx: Transaction, accountBalance: number): void {
216184
const txGasPrice = BigInt(tx.gasPrice || tx.maxFeePerGas! + tx.maxPriorityFeePerGas!);
217185
const txTotalValue = tx.value + txGasPrice * tx.gasLimit;
186+
const accountBalanceInWeiBars = BigInt(accountBalance.toString()) * BigInt(constants.TINYBAR_TO_WEIBAR_COEF);
218187

219-
if (account == null) {
220-
if (this.logger.isLevelEnabled('trace')) {
221-
this.logger.trace(
222-
`Failed to retrieve account details from mirror node on balance precheck for sendRawTransaction(transaction=${JSON.stringify(
223-
tx,
224-
)}, totalValue=${txTotalValue})`,
225-
);
226-
}
227-
throw predefined.RESOURCE_NOT_FOUND(`tx.from '${tx.from}'.`);
228-
}
229-
230-
let tinybars: bigint;
231-
try {
232-
tinybars = BigInt(account.balance.balance.toString()) * BigInt(constants.TINYBAR_TO_WEIBAR_COEF);
233-
result.passes = tinybars >= txTotalValue;
234-
} catch (error: any) {
235-
if (this.logger.isLevelEnabled('trace')) {
236-
this.logger.trace(
237-
`Error on balance precheck for sendRawTransaction(transaction=%s, totalValue=%s, error=%s)`,
238-
JSON.stringify(tx),
239-
txTotalValue,
240-
error.message,
241-
);
242-
}
243-
if (error instanceof JsonRpcError) {
244-
// preserve original error
245-
throw error;
246-
} else {
247-
throw predefined.INTERNAL_ERROR(`balance precheck: ${error.message}`);
248-
}
249-
}
250-
251-
if (!result.passes) {
252-
if (this.logger.isLevelEnabled('trace')) {
253-
this.logger.trace(
254-
`Failed balance precheck for sendRawTransaction(transaction=%s, totalValue=%s, accountTinyBarBalance=%s)`,
255-
JSON.stringify(tx),
256-
txTotalValue,
257-
tinybars,
258-
);
259-
}
188+
if (accountBalanceInWeiBars < txTotalValue) {
260189
throw predefined.INSUFFICIENT_ACCOUNT_BALANCE;
261190
}
262191
}
@@ -267,29 +196,11 @@ export class Precheck {
267196
*/
268197
gasLimit(tx: Transaction): void {
269198
const gasLimit = Number(tx.gasLimit);
270-
const failBaseLog = 'Failed gasLimit precheck for sendRawTransaction(transaction=%s).';
271-
272199
const intrinsicGasCost = Precheck.transactionIntrinsicGasCost(tx.data);
273200

274201
if (gasLimit > constants.MAX_TRANSACTION_FEE_THRESHOLD) {
275-
if (this.logger.isLevelEnabled('trace')) {
276-
this.logger.trace(
277-
`${failBaseLog} Gas Limit was too high: %s, block gas limit: %s`,
278-
JSON.stringify(tx),
279-
gasLimit,
280-
constants.MAX_TRANSACTION_FEE_THRESHOLD,
281-
);
282-
}
283202
throw predefined.GAS_LIMIT_TOO_HIGH(gasLimit, constants.MAX_TRANSACTION_FEE_THRESHOLD);
284203
} else if (gasLimit < intrinsicGasCost) {
285-
if (this.logger.isLevelEnabled('trace')) {
286-
this.logger.trace(
287-
`${failBaseLog} Gas Limit was too low: %s, intrinsic gas cost: %s`,
288-
JSON.stringify(tx),
289-
gasLimit,
290-
intrinsicGasCost,
291-
);
292-
}
293204
throw predefined.GAS_LIMIT_TOO_LOW(gasLimit, intrinsicGasCost);
294205
}
295206
}
@@ -356,12 +267,7 @@ export class Precheck {
356267
transactionType(tx: Transaction) {
357268
// Blob transactions are not supported as per HIP 866
358269
if (tx.type === 3) {
359-
if (this.logger.isLevelEnabled('trace')) {
360-
this.logger.trace(
361-
`Transaction with type=${tx.type} is unsupported for sendRawTransaction(transaction=${JSON.stringify(tx)})`,
362-
);
363-
}
364-
throw predefined.UNSUPPORTED_TRANSACTION_TYPE;
270+
throw predefined.UNSUPPORTED_TRANSACTION_TYPE(tx.type);
365271
}
366272
}
367273

0 commit comments

Comments
 (0)