Skip to content

Commit 82f48c8

Browse files
committed
feat: import LockService as constructor params instead of passing down
Signed-off-by: Logan Nguyen <[email protected]>
1 parent 71d9e2b commit 82f48c8

File tree

10 files changed

+43
-62
lines changed

10 files changed

+43
-62
lines changed

packages/relay/src/lib/clients/sdkClient.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,14 @@ export class SDKClient {
6868
* @param logger - The logger instance for logging information, warnings, and errors.
6969
* @param eventEmitter - The eventEmitter used for emitting and handling events within the class.
7070
* @param hbarLimitService - The HbarLimitService that tracks hbar expenses and limits.
71+
* @param lockService - Service for managing access control locks.
7172
*/
7273
constructor(
7374
hederaNetwork: string,
7475
logger: Logger,
7576
private readonly eventEmitter: EventEmitter<TypedEvents>,
7677
hbarLimitService: HbarLimitService,
78+
public readonly lockService: LockService,
7779
) {
7880
const client =
7981
hederaNetwork in constants.CHAIN_IDS
@@ -135,7 +137,6 @@ export class SDKClient {
135137
* @param {string} originalCallerAddress - The address of the original caller making the request.
136138
* @param {number} networkGasPriceInWeiBars - The predefined gas price of the network in weibar.
137139
* @param {number} currentNetworkExchangeRateInCents - The exchange rate in cents of the current network.
138-
* @param {LockService} lockService - The service for managing transaction locks.
139140
* @param {string | null} lockSessionKey - The session key for the acquired lock, null if no lock was acquired.
140141
* @returns {Promise<{ txResponse: TransactionResponse; fileId: FileId | null }>}
141142
* @throws {SDKClientError} Throws an error if no file ID is created or if the preemptive fee check fails.
@@ -147,7 +148,6 @@ export class SDKClient {
147148
originalCallerAddress: string,
148149
networkGasPriceInWeiBars: number,
149150
currentNetworkExchangeRateInCents: number,
150-
lockService: LockService,
151151
lockSessionKey: string | null,
152152
): Promise<{ txResponse: TransactionResponse; fileId: FileId | null }> {
153153
const jumboTxEnabled = ConfigService.get('JUMBO_TX_ENABLED');
@@ -194,7 +194,6 @@ export class SDKClient {
194194
requestDetails,
195195
true,
196196
originalCallerAddress,
197-
lockService,
198197
lockSessionKey,
199198
),
200199
};
@@ -272,7 +271,6 @@ export class SDKClient {
272271
* @param requestDetails - The request details for logging and tracking.
273272
* @param shouldThrowHbarLimit - Flag to indicate whether to check HBAR limits.
274273
* @param originalCallerAddress - The address of the original caller making the request.
275-
* @param lockService - The service for managing transaction locks.
276274
* @param estimatedTxFee - The optional total estimated transaction fee.
277275
* @param lockSessionKey - The session key for the acquired lock, null if no lock was acquired.
278276
* @returns - A promise that resolves to the transaction response.
@@ -284,7 +282,6 @@ export class SDKClient {
284282
requestDetails: RequestDetails,
285283
shouldThrowHbarLimit: boolean,
286284
originalCallerAddress: string,
287-
lockService?: LockService,
288285
lockSessionKey?: string | null,
289286
estimatedTxFee?: number,
290287
): Promise<TransactionResponse> {
@@ -343,9 +340,9 @@ export class SDKClient {
343340
}
344341
return transactionResponse;
345342
} finally {
346-
// Eventually release the transaction mutex lock if it was acquired by the sender
347-
if (lockService && lockSessionKey) {
348-
await lockService.releaseLock(originalCallerAddress, lockSessionKey);
343+
// Eventually release the transaction lock if it was acquired by the sender using lockSessionKey
344+
if (lockSessionKey) {
345+
await this.lockService.releaseLock(originalCallerAddress, lockSessionKey);
349346
}
350347

351348
if (transactionId?.length) {

packages/relay/src/lib/eth.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -108,12 +108,6 @@ export class EthImpl implements Eth {
108108
*/
109109
private readonly transactionService: ITransactionService;
110110

111-
/**
112-
* The service responsible for managing locks to ensure proper resource synchronization.
113-
* @private
114-
*/
115-
private readonly lockService: LockService;
116-
117111
/**
118112
* Constructs an instance of the service responsible for handling Ethereum JSON-RPC methods
119113
* using Hedera Hashgraph as the underlying network.
@@ -123,13 +117,15 @@ export class EthImpl implements Eth {
123117
* @param {Logger} logger - Logger instance for logging system messages.
124118
* @param {string} chain - The chain identifier for the current blockchain environment.
125119
* @param {CacheService} cacheService - Service for managing cached data.
120+
* @param {LockService} lockService - Service for managing access control locks.
126121
*/
127122
constructor(
128123
hapiService: HAPIService,
129124
mirrorNodeClient: MirrorNodeClient,
130125
logger: Logger,
131126
chain: string,
132127
public readonly cacheService: CacheService,
128+
public readonly lockService: LockService,
133129
) {
134130
this.chain = chain;
135131
this.logger = logger;
@@ -141,7 +137,6 @@ export class EthImpl implements Eth {
141137
this.contractService = new ContractService(cacheService, this.common, hapiService, logger, mirrorNodeClient);
142138
this.accountService = new AccountService(cacheService, this.common, logger, mirrorNodeClient);
143139
this.blockService = new BlockService(cacheService, chain, this.common, mirrorNodeClient, logger);
144-
this.lockService = new LockService(logger);
145140

146141
this.transactionService = new TransactionService(
147142
cacheService,

packages/relay/src/lib/relay.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import {
3232
RpcNamespaceRegistry,
3333
} from './types';
3434
import { Web3Impl } from './web3';
35+
import { LockService } from './services';
3536

3637
export class Relay {
3738
/**
@@ -153,8 +154,8 @@ export class Relay {
153154
register,
154155
duration,
155156
);
156-
157-
const hapiService = new HAPIService(logger, register, hbarLimitService);
157+
const lockService = new LockService(logger);
158+
const hapiService = new HAPIService(logger, register, hbarLimitService, lockService);
158159

159160
this.operatorAccountId = hapiService.getOperatorAccountId();
160161

@@ -181,6 +182,7 @@ export class Relay {
181182
logger.child({ name: 'relay-eth' }),
182183
chainId,
183184
this.cacheService,
185+
lockService,
184186
);
185187

186188
(this.ethImpl as EthImpl).eventEmitter.on('eth_execution', (args: IEthExecutionEventPayload) => {

packages/relay/src/lib/services/ethService/transactionService/TransactionService.ts

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,6 @@ export class TransactionService implements ITransactionService {
281281
transactionBuffer,
282282
parsedTx,
283283
networkGasPriceInWeiBars,
284-
this.lockService,
285284
lockSessionKey,
286285
requestDetails,
287286
);
@@ -296,7 +295,6 @@ export class TransactionService implements ITransactionService {
296295
transactionBuffer,
297296
parsedTx,
298297
networkGasPriceInWeiBars,
299-
this.lockService,
300298
lockSessionKey,
301299
requestDetails,
302300
);
@@ -495,7 +493,6 @@ export class TransactionService implements ITransactionService {
495493
* @param {Buffer} transactionBuffer - The raw transaction data as a buffer.
496494
* @param {EthersTransaction} parsedTx - The parsed Ethereum transaction object.
497495
* @param {number} networkGasPriceInWeiBars - The current network gas price in wei bars.
498-
* @param {LockService} lockService - The service for managing locks.
499496
* @param {string | null} lockSessionKey - The session key for the acquired lock, null if no lock was acquired.
500497
* @param {RequestDetails} requestDetails - Details of the request for logging and tracking purposes.
501498
* @returns {Promise<string | JsonRpcError>} A promise that resolves to the transaction hash if successful, or a JsonRpcError if an error occurs.
@@ -504,7 +501,6 @@ export class TransactionService implements ITransactionService {
504501
transactionBuffer: Buffer,
505502
parsedTx: EthersTransaction,
506503
networkGasPriceInWeiBars: number,
507-
lockService: LockService,
508504
lockSessionKey: string | null,
509505
requestDetails: RequestDetails,
510506
): Promise<string | JsonRpcError> {
@@ -520,7 +516,6 @@ export class TransactionService implements ITransactionService {
520516
transactionBuffer,
521517
originalCallerAddress,
522518
networkGasPriceInWeiBars,
523-
lockService,
524519
lockSessionKey,
525520
requestDetails,
526521
);
@@ -668,7 +663,6 @@ export class TransactionService implements ITransactionService {
668663
* @param transactionBuffer The raw transaction buffer
669664
* @param originalCallerAddress The address of the original caller
670665
* @param networkGasPriceInWeiBars The current network gas price in wei bars
671-
* @param lockService The service for managing locks
672666
* @param lockSessionKey The session key for the acquired lock, null if no lock was acquired
673667
* @param requestDetails The request details for logging and tracking
674668
* @returns {Promise<{txSubmitted: boolean, submittedTransactionId: string, error: any}>} A promise that resolves to an object containing transaction submission details
@@ -677,7 +671,6 @@ export class TransactionService implements ITransactionService {
677671
transactionBuffer: Buffer,
678672
originalCallerAddress: string,
679673
networkGasPriceInWeiBars: number,
680-
lockService: LockService,
681674
lockSessionKey: string | null,
682675
requestDetails: RequestDetails,
683676
): Promise<{
@@ -698,7 +691,6 @@ export class TransactionService implements ITransactionService {
698691
originalCallerAddress,
699692
networkGasPriceInWeiBars,
700693
await this.getCurrentNetworkExchangeRateInCents(requestDetails),
701-
lockService,
702694
lockSessionKey,
703695
);
704696

packages/relay/src/lib/services/hapiService/hapiService.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,14 @@ export default class HAPIService {
9494
* @param logger - The logger instance used for logging.
9595
* @param register - The registry instance for metrics and other services.
9696
* @param hbarLimitService - An HBAR Rate Limit service that tracks hbar expenses and limits.
97+
* @param lockService - Service for managing access control locks.
9798
*/
98-
constructor(logger: Logger, register: Registry, hbarLimitService: HbarLimitService) {
99+
constructor(
100+
logger: Logger,
101+
register: Registry,
102+
hbarLimitService: HbarLimitService,
103+
public readonly lockService: LockService,
104+
) {
99105
this.logger = logger;
100106
this.hbarLimitService = hbarLimitService;
101107
this.eventEmitter = new EventEmitter<TypedEvents>();
@@ -191,6 +197,7 @@ export default class HAPIService {
191197
this.logger.child({ name: `consensus-node` }),
192198
this.eventEmitter,
193199
this.hbarLimitService,
200+
this.lockService,
194201
);
195202
}
196203

@@ -243,7 +250,6 @@ export default class HAPIService {
243250
originalCallerAddress: string,
244251
networkGasPriceInWeiBars: number,
245252
currentNetworkExchangeRateInCents: number,
246-
lockService: LockService,
247253
lockSessionKey: string | null,
248254
): Promise<{ txResponse: TransactionResponse; fileId: FileId | null }> {
249255
return this.getSDKClient().submitEthereumTransaction(
@@ -253,7 +259,6 @@ export default class HAPIService {
253259
originalCallerAddress,
254260
networkGasPriceInWeiBars,
255261
currentNetworkExchangeRateInCents,
256-
lockService,
257262
lockSessionKey,
258263
);
259264
}

packages/relay/tests/lib/eth/eth-helpers.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { CommonService } from '../../../src/lib/services';
1616
import { CacheService } from '../../../src/lib/services/cacheService/cacheService';
1717
import HAPIService from '../../../src/lib/services/hapiService/hapiService';
1818
import { HbarLimitService } from '../../../src/lib/services/hbarLimitService';
19+
import { LockService } from '../../../src/lib/services/lockService/LockService';
1920

2021
export function contractResultsByNumberByIndexURL(number: number, index: number): string {
2122
return `contracts/results?block.number=${number}&transaction.index=${index}&limit=100&order=asc`;
@@ -59,11 +60,13 @@ export function generateEthTestEnv(fixedFeeHistory = false) {
5960
duration,
6061
);
6162

62-
const hapiServiceInstance = new HAPIService(logger, registry, hbarLimitService);
63+
const lockService = new LockService(logger);
64+
65+
const hapiServiceInstance = new HAPIService(logger, registry, hbarLimitService, lockService);
6366

6467
const commonService = new CommonService(mirrorNodeInstance, logger, cacheService);
6568

66-
const ethImpl = new EthImpl(hapiServiceInstance, mirrorNodeInstance, logger, '0x12a', cacheService);
69+
const ethImpl = new EthImpl(hapiServiceInstance, mirrorNodeInstance, logger, '0x12a', cacheService, lockService);
6770

6871
return {
6972
cacheService,

packages/relay/tests/lib/eth/eth_sendRawTransaction.spec.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ import { generateEthTestEnv } from './eth-helpers';
3636
use(chaiAsPromised);
3737

3838
let sdkClientStub: sinon.SinonStubbedInstance<SDKClient>;
39-
let getSdkClientStub: sinon.SinonStub;
4039

4140
describe('@ethSendRawTransaction eth_sendRawTransaction spec', async function () {
4241
this.timeout(10000);
@@ -61,11 +60,9 @@ describe('@ethSendRawTransaction eth_sendRawTransaction spec', async function ()
6160
await cacheService.clear();
6261
restMock.reset();
6362
sdkClientStub = sinon.createStubInstance(SDKClient);
64-
getSdkClientStub = sinon.stub(hapiServiceInstance, 'getSDKClient').returns(sdkClientStub);
6563
restMock.onGet('network/fees').reply(200, JSON.stringify(DEFAULT_NETWORK_FEES));
6664
});
6765
this.afterEach(() => {
68-
getSdkClientStub.restore();
6966
restMock.resetHandlers();
7067
});
7168

packages/relay/tests/lib/openrpc.spec.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import { NetImpl } from '../../src/lib/net';
2727
import { CacheService } from '../../src/lib/services/cacheService/cacheService';
2828
import ClientService from '../../src/lib/services/hapiService/hapiService';
2929
import { HbarLimitService } from '../../src/lib/services/hbarLimitService';
30+
import { LockService } from '../../src/lib/services/lockService/LockService';
3031
import { RequestDetails } from '../../src/lib/types';
3132
import { Web3Impl } from '../../src/lib/web3';
3233
import {
@@ -125,10 +126,11 @@ describe('Open RPC Specification', function () {
125126
duration,
126127
);
127128

128-
clientServiceInstance = new ClientService(logger, registry, hbarLimitService);
129+
const lockService = new LockService(logger);
130+
131+
clientServiceInstance = new ClientService(logger, registry, hbarLimitService, lockService);
129132
sdkClientStub = sinon.createStubInstance(SDKClient);
130-
sinon.stub(clientServiceInstance, 'getSDKClient').returns(sdkClientStub);
131-
ethImpl = new EthImpl(clientServiceInstance, mirrorNodeInstance, logger, '0x12a', cacheService);
133+
ethImpl = new EthImpl(clientServiceInstance, mirrorNodeInstance, logger, '0x12a', cacheService, lockService);
132134
ns = { eth: ethImpl, net: new NetImpl(), web3: new Web3Impl() };
133135

134136
// mocked data

0 commit comments

Comments
 (0)