@@ -30,15 +30,17 @@ import relayConstants from '../../../../packages/relay/src/lib/constants';
3030
3131// Local resources
3232import parentContractJson from '../contracts/Parent.json' ;
33+ import largeContractJson from '../contracts/EstimatePrecompileContract.json' ;
3334import { Utils } from '../helpers/utils' ;
35+ import { predefined } from '@hashgraph/json-rpc-relay' ;
3436
3537describe ( '@ratelimiter Rate Limiters Acceptance Tests' , function ( ) {
3638 this . timeout ( 480 * 1000 ) ; // 480 seconds
3739
3840 const accounts : AliasAccount [ ] = [ ] ;
3941
4042 // @ts -ignore
41- const { mirrorNode, relay, logger, initialBalance } = global ;
43+ const { mirrorNode, relay, logger, initialBalance, metrics } = global ;
4244
4345 // cached entities
4446 let parentContractAddress : string ;
@@ -90,72 +92,121 @@ describe('@ratelimiter Rate Limiters Acceptance Tests', function () {
9092 } ) ;
9193 } ) ;
9294
93- describe ( 'HBAR Limiter Acceptance Tests' , function ( ) {
94- this . timeout ( 480 * 1000 ) ; // 480 seconds
95-
96- this . beforeAll ( async ( ) => {
97- requestId = Utils . generateRequestId ( ) ;
98- const requestIdPrefix = Utils . formatRequestIdMessage ( requestId ) ;
99-
100- logger . info ( `${ requestIdPrefix } Creating accounts` ) ;
101- logger . info ( `${ requestIdPrefix } HBAR_RATE_LIMIT_TINYBAR: ${ process . env . HBAR_RATE_LIMIT_TINYBAR } ` ) ;
102-
103- const initialAccount : AliasAccount = global . accounts [ 0 ] ;
104-
105- const neededAccounts : number = 2 ;
106- accounts . push (
107- ...( await Utils . createMultipleAliasAccounts (
108- mirrorNode ,
109- initialAccount ,
110- neededAccounts ,
111- initialBalance ,
112- requestId ,
113- ) ) ,
114- ) ;
115- global . accounts . push ( ...accounts ) ;
116-
117- const parentContract = await Utils . deployContract (
118- parentContractJson . abi ,
119- parentContractJson . bytecode ,
120- accounts [ 0 ] . wallet ,
121- ) ;
122-
123- parentContractAddress = parentContract . target as string ;
124- global . logger . trace ( `${ requestIdPrefix } Deploy parent contract on address ${ parentContractAddress } ` ) ;
125- } ) ;
126-
127- this . beforeEach ( async ( ) => {
128- requestId = Utils . generateRequestId ( ) ;
129- } ) ;
95+ // The following tests exhaust the hbar limit, so they should only be run against a local relay
96+ if ( global . relayIsLocal ) {
97+ describe ( 'HBAR Limiter Acceptance Tests' , function ( ) {
98+ before ( async ( ) => {
99+ // Restart the relay to reset the limits
100+ await global . restartLocalRelay ( ) ;
101+ } ) ;
130102
131- describe ( 'HBAR Rate Limit Tests' , ( ) => {
132- const defaultGasPrice = Assertions . defaultGasPrice ;
133- const defaultGasLimit = 3_000_000 ;
134-
135- const defaultLondonTransactionData = {
136- value : ONE_TINYBAR ,
137- chainId : Number ( CHAIN_ID ) ,
138- maxPriorityFeePerGas : defaultGasPrice ,
139- maxFeePerGas : defaultGasPrice ,
140- gasLimit : defaultGasLimit ,
141- type : 2 ,
142- } ;
103+ this . timeout ( 480 * 1000 ) ; // 480 seconds
104+
105+ this . beforeAll ( async ( ) => {
106+ requestId = Utils . generateRequestId ( ) ;
107+ const requestIdPrefix = Utils . formatRequestIdMessage ( requestId ) ;
108+
109+ logger . info ( `${ requestIdPrefix } Creating accounts` ) ;
110+ logger . info ( `${ requestIdPrefix } HBAR_RATE_LIMIT_TINYBAR: ${ process . env . HBAR_RATE_LIMIT_TINYBAR } ` ) ;
111+
112+ const initialAccount : AliasAccount = global . accounts [ 0 ] ;
113+
114+ const neededAccounts : number = 2 ;
115+ accounts . push (
116+ ...( await Utils . createMultipleAliasAccounts (
117+ mirrorNode ,
118+ initialAccount ,
119+ neededAccounts ,
120+ initialBalance ,
121+ requestId ,
122+ ) ) ,
123+ ) ;
124+ global . accounts . push ( ...accounts ) ;
125+
126+ const parentContract = await Utils . deployContract (
127+ parentContractJson . abi ,
128+ parentContractJson . bytecode ,
129+ accounts [ 0 ] . wallet ,
130+ ) ;
131+
132+ parentContractAddress = parentContract . target as string ;
133+ global . logger . trace ( `${ requestIdPrefix } Deploy parent contract on address ${ parentContractAddress } ` ) ;
134+ } ) ;
143135
144- it ( 'should execute "eth_sendRawTransaction" without triggering HBAR rate limit exceeded ' , async function ( ) {
145- const gasPrice = await relay . gasPrice ( requestId ) ;
136+ this . beforeEach ( async ( ) => {
137+ requestId = Utils . generateRequestId ( ) ;
138+ } ) ;
146139
147- const transaction = {
148- ...defaultLondonTransactionData ,
149- to : parentContractAddress ,
150- nonce : await relay . getAccountNonce ( accounts [ 1 ] . address , requestId ) ,
151- maxPriorityFeePerGas : gasPrice ,
152- maxFeePerGas : gasPrice ,
140+ describe ( 'HBAR Rate Limit Tests' , ( ) => {
141+ const defaultGasPrice = Assertions . defaultGasPrice ;
142+ const defaultGasLimit = 3_000_000 ;
143+
144+ const defaultLondonTransactionData = {
145+ value : ONE_TINYBAR ,
146+ chainId : Number ( CHAIN_ID ) ,
147+ maxPriorityFeePerGas : defaultGasPrice ,
148+ maxFeePerGas : defaultGasPrice ,
149+ gasLimit : defaultGasLimit ,
150+ type : 2 ,
153151 } ;
154- const signedTx = await accounts [ 1 ] . wallet . signTransaction ( transaction ) ;
155152
156- await expect ( relay . call ( testConstants . ETH_ENDPOINTS . ETH_SEND_RAW_TRANSACTION , [ signedTx ] , requestId ) ) . to . be
157- . fulfilled ;
153+ it ( 'should execute "eth_sendRawTransaction" without triggering HBAR rate limit exceeded' , async function ( ) {
154+ const gasPrice = await relay . gasPrice ( requestId ) ;
155+ const remainingHbarsBefore = Number ( await metrics . get ( testConstants . METRICS . REMAINING_HBAR_LIMIT ) ) ;
156+
157+ const transaction = {
158+ ...defaultLondonTransactionData ,
159+ to : parentContractAddress ,
160+ nonce : await relay . getAccountNonce ( accounts [ 1 ] . address , requestId ) ,
161+ maxPriorityFeePerGas : gasPrice ,
162+ maxFeePerGas : gasPrice ,
163+ } ;
164+ const signedTx = await accounts [ 1 ] . wallet . signTransaction ( transaction ) ;
165+
166+ await expect ( relay . call ( testConstants . ETH_ENDPOINTS . ETH_SEND_RAW_TRANSACTION , [ signedTx ] , requestId ) ) . to . be
167+ . fulfilled ;
168+ const remainingHbarsAfter = Number ( await metrics . get ( testConstants . METRICS . REMAINING_HBAR_LIMIT ) ) ;
169+ expect ( remainingHbarsAfter ) . to . be . eq ( remainingHbarsBefore ) ;
170+ } ) ;
171+
172+ it ( 'should deploy a large contract and decrease remaining HBAR in limiter when transaction data is large' , async function ( ) {
173+ const remainingHbarsBefore = Number ( await metrics . get ( testConstants . METRICS . REMAINING_HBAR_LIMIT ) ) ;
174+ expect ( remainingHbarsBefore ) . to . be . gt ( 0 ) ;
175+
176+ const largeContract = await Utils . deployContract (
177+ largeContractJson . abi ,
178+ largeContractJson . bytecode ,
179+ accounts [ 0 ] . wallet ,
180+ ) ;
181+ await largeContract . waitForDeployment ( ) ;
182+ const remainingHbarsAfter = Number ( await metrics . get ( testConstants . METRICS . REMAINING_HBAR_LIMIT ) ) ;
183+ expect ( largeContract . target ) . to . not . be . null ;
184+ expect ( remainingHbarsAfter ) . to . be . lt ( remainingHbarsBefore ) ;
185+ } ) ;
186+
187+ it ( 'multiple deployments of large contracts should eventually exhaust the remaining hbar limit' , async function ( ) {
188+ const remainingHbarsBefore = Number ( await metrics . get ( testConstants . METRICS . REMAINING_HBAR_LIMIT ) ) ;
189+ expect ( remainingHbarsBefore ) . to . be . gt ( 0 ) ;
190+ try {
191+ for ( let i = 0 ; i < 50 ; i ++ ) {
192+ const largeContract = await Utils . deployContract (
193+ largeContractJson . abi ,
194+ largeContractJson . bytecode ,
195+ accounts [ 0 ] . wallet ,
196+ ) ;
197+ await largeContract . waitForDeployment ( ) ;
198+ expect ( largeContract . target ) . to . not . be . null ;
199+ }
200+
201+ expect ( true ) . to . be . false ;
202+ } catch ( e : any ) {
203+ expect ( e . message ) . to . contain ( predefined . HBAR_RATE_LIMIT_EXCEEDED . message ) ;
204+ }
205+
206+ const remainingHbarsAfter = Number ( await metrics . get ( testConstants . METRICS . REMAINING_HBAR_LIMIT ) ) ;
207+ expect ( remainingHbarsAfter ) . to . be . lte ( 0 ) ;
208+ } ) ;
158209 } ) ;
159210 } ) ;
160- } ) ;
211+ }
161212} ) ;
0 commit comments