@@ -7,7 +7,7 @@ import { Logger } from 'pino';
7
7
import { prepend0x } from '../formatters' ;
8
8
import { MirrorNodeClient } from './clients' ;
9
9
import constants from './constants' ;
10
- import { JsonRpcError , predefined } from './errors/JsonRpcError' ;
10
+ import { predefined } from './errors/JsonRpcError' ;
11
11
import { CommonService } from './services' ;
12
12
import { RequestDetails } from './types' ;
13
13
@@ -69,12 +69,12 @@ export class Precheck {
69
69
this . transactionSize ( parsedTx ) ;
70
70
this . transactionType ( parsedTx ) ;
71
71
this . gasLimit ( parsedTx ) ;
72
- const mirrorAccountInfo = await this . verifyAccount ( parsedTx , requestDetails ) ;
73
- this . nonce ( parsedTx , mirrorAccountInfo . ethereum_nonce ) ;
74
72
this . chainId ( parsedTx ) ;
75
73
this . value ( parsedTx ) ;
76
74
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 ) ;
78
78
await this . receiverAccount ( parsedTx , requestDetails ) ;
79
79
}
80
80
@@ -87,15 +87,6 @@ export class Precheck {
87
87
async verifyAccount ( tx : Transaction , requestDetails : RequestDetails ) : Promise < any > {
88
88
const accountInfo = await this . mirrorNodeClient . getAccount ( tx . from ! , requestDetails ) ;
89
89
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
- }
99
90
throw predefined . RESOURCE_NOT_FOUND ( `address '${ tx . from } '.` ) ;
100
91
}
101
92
@@ -108,32 +99,22 @@ export class Precheck {
108
99
* @param accountInfoNonce - The nonce of the account.
109
100
*/
110
101
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
-
117
102
if ( accountInfoNonce > tx . nonce ) {
118
103
throw predefined . NONCE_TOO_LOW ( tx . nonce , accountInfoNonce ) ;
119
104
}
120
105
}
121
106
122
107
/**
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.
125
113
*/
126
114
chainId ( tx : Transaction ) : void {
127
115
const txChainId = prepend0x ( Number ( tx . chainId ) . toString ( 16 ) ) ;
128
116
const passes = this . isLegacyUnprotectedEtx ( tx ) || txChainId === this . chain ;
129
117
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
- }
137
118
throw predefined . UNSUPPORTED_CHAIN_ID ( txChainId , this . chain ) ;
138
119
}
139
120
}
@@ -181,14 +162,6 @@ export class Precheck {
181
162
}
182
163
}
183
164
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
- }
192
165
throw predefined . GAS_PRICE_TOO_LOW ( txGasPrice , networkGasPrice ) ;
193
166
}
194
167
}
@@ -204,59 +177,15 @@ export class Precheck {
204
177
205
178
/**
206
179
* 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 .
209
182
*/
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 {
216
184
const txGasPrice = BigInt ( tx . gasPrice || tx . maxFeePerGas ! + tx . maxPriorityFeePerGas ! ) ;
217
185
const txTotalValue = tx . value + txGasPrice * tx . gasLimit ;
186
+ const accountBalanceInWeiBars = BigInt ( accountBalance . toString ( ) ) * BigInt ( constants . TINYBAR_TO_WEIBAR_COEF ) ;
218
187
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 ) {
260
189
throw predefined . INSUFFICIENT_ACCOUNT_BALANCE ;
261
190
}
262
191
}
@@ -267,29 +196,11 @@ export class Precheck {
267
196
*/
268
197
gasLimit ( tx : Transaction ) : void {
269
198
const gasLimit = Number ( tx . gasLimit ) ;
270
- const failBaseLog = 'Failed gasLimit precheck for sendRawTransaction(transaction=%s).' ;
271
-
272
199
const intrinsicGasCost = Precheck . transactionIntrinsicGasCost ( tx . data ) ;
273
200
274
201
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
- }
283
202
throw predefined . GAS_LIMIT_TOO_HIGH ( gasLimit , constants . MAX_TRANSACTION_FEE_THRESHOLD ) ;
284
203
} 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
- }
293
204
throw predefined . GAS_LIMIT_TOO_LOW ( gasLimit , intrinsicGasCost ) ;
294
205
}
295
206
}
@@ -356,12 +267,7 @@ export class Precheck {
356
267
transactionType ( tx : Transaction ) {
357
268
// Blob transactions are not supported as per HIP 866
358
269
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 ) ;
365
271
}
366
272
}
367
273
0 commit comments