Skip to content

Commit f68c902

Browse files
chore: refactor validator service in relay package for clarity and optimization (#3891)
Signed-off-by: Luis Mastrangelo <[email protected]> Signed-off-by: Konstantina Blazhukova <[email protected]> Signed-off-by: konstantinabl <[email protected]> Co-authored-by: Luis Mastrangelo <[email protected]> Co-authored-by: Luis Mastrangelo <[email protected]>
1 parent d838e16 commit f68c902

File tree

22 files changed

+669
-903
lines changed

22 files changed

+669
-903
lines changed

packages/relay/src/lib/debug.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { MirrorNodeClient } from './clients';
99
import { IOpcode } from './clients/models/IOpcode';
1010
import { IOpcodesResponse } from './clients/models/IOpcodesResponse';
1111
import constants, { CallType, TracerType } from './constants';
12-
import { cache, RPC_LAYOUT, rpcMethod, rpcParamLayoutConfig, rpcParamValidationRules } from './decorators';
12+
import { cache, RPC_LAYOUT, rpcMethod, rpcParamLayoutConfig } from './decorators';
1313
import { predefined } from './errors/JsonRpcError';
1414
import { CommonService } from './services';
1515
import { CACHE_LEVEL, CacheService } from './services/cacheService/cacheService';
@@ -20,11 +20,11 @@ import {
2020
ICallTracerConfig,
2121
IOpcodeLoggerConfig,
2222
MirrorNodeContractResult,
23-
ParamType,
2423
RequestDetails,
2524
TraceBlockByNumberTxResult,
2625
TransactionTracerConfig,
2726
} from './types';
27+
import { rpcParamValidationRules } from './validators';
2828

2929
/**
3030
* Represents a DebugService for tracing and debugging transactions.
@@ -105,8 +105,8 @@ export class DebugImpl implements Debug {
105105
*/
106106
@rpcMethod
107107
@rpcParamValidationRules({
108-
0: { type: ParamType.TRANSACTION_HASH_OR_ID, required: true },
109-
1: { type: ParamType.TRACER_CONFIG_WRAPPER, required: false },
108+
0: { type: 'transactionHash', required: true },
109+
1: { type: 'tracerConfigWrapper', required: false },
110110
})
111111
@rpcParamLayoutConfig(RPC_LAYOUT.custom((params) => [params[0], params[1]]))
112112
@cache(CacheService.getInstance(CACHE_LEVEL.L1))
@@ -161,8 +161,8 @@ export class DebugImpl implements Debug {
161161
*/
162162
@rpcMethod
163163
@rpcParamValidationRules({
164-
0: { type: ParamType.BLOCK_NUMBER, required: true },
165-
1: { type: ParamType.TRACER_CONFIG_WRAPPER, required: false },
164+
0: { type: 'blockNumber', required: true },
165+
1: { type: 'tracerConfigWrapper', required: false },
166166
})
167167
@rpcParamLayoutConfig(RPC_LAYOUT.custom((params) => [params[0], params[1]]))
168168
@cache(CacheService.getInstance(CACHE_LEVEL.L1), {
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// SPDX-License-Identifier: Apache-2.0
22

33
export * from './rpcMethod.decorator';
4-
export * from './rpcParamValidationRules.decorator';
54
export * from './rpcParamLayoutConfig.decorator';
65
export * from './cache.decorator';

packages/relay/src/lib/decorators/rpcParamValidationRules.decorator.ts

Lines changed: 0 additions & 34 deletions
This file was deleted.

packages/relay/src/lib/dispatcher/rpcMethodDispatcher.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,12 @@
33
import { Logger } from 'pino';
44

55
import { Utils } from '../../utils';
6-
import { RPC_PARAM_VALIDATION_RULES_KEY } from '../decorators';
76
import { JsonRpcError } from '../errors/JsonRpcError';
87
import { predefined } from '../errors/JsonRpcError';
98
import { MirrorNodeClientError } from '../errors/MirrorNodeClientError';
109
import { SDKClientError } from '../errors/SDKClientError';
1110
import { OperationHandler, RequestDetails, RpcMethodRegistry } from '../types';
12-
import { Validator } from '../validators';
11+
import { RPC_PARAM_VALIDATION_RULES_KEY, validateParams } from '../validators';
1312

1413
/**
1514
* Dispatches JSON-RPC method calls to their appropriate handlers
@@ -105,7 +104,7 @@ export class RpcMethodDispatcher {
105104
} Validating method parameters for ${rpcMethodName}, params: ${JSON.stringify(rpcMethodParams)}`,
106105
);
107106
}
108-
Validator.validateParams(rpcMethodParams, methodParamSchemas);
107+
validateParams(rpcMethodParams, methodParamSchemas);
109108
}
110109

111110
return operationHandler;

packages/relay/src/lib/eth.ts

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { Logger } from 'pino';
66
import { Eth } from '../index';
77
import { MirrorNodeClient } from './clients';
88
import constants from './constants';
9-
import { cache, RPC_LAYOUT, rpcMethod, rpcParamLayoutConfig, rpcParamValidationRules } from './decorators';
9+
import { cache, RPC_LAYOUT, rpcMethod, rpcParamLayoutConfig } from './decorators';
1010
import { JsonRpcError, predefined } from './errors/JsonRpcError';
1111
import { Block, Log, Receipt, Transaction } from './model';
1212
import {
@@ -34,7 +34,7 @@ import {
3434
ITransactionReceipt,
3535
RequestDetails,
3636
} from './types';
37-
import { ParamType } from './types/validation';
37+
import { rpcParamValidationRules } from './validators';
3838

3939
/**
4040
* Implementation of the "eth_" methods from the Ethereum JSON-RPC API.
@@ -175,9 +175,9 @@ export class EthImpl implements Eth {
175175
*/
176176
@rpcMethod
177177
@rpcParamValidationRules({
178-
0: { type: ParamType.HEX, required: true },
179-
1: { type: ParamType.BLOCK_NUMBER, required: true },
180-
2: { type: ParamType.ARRAY, required: false },
178+
0: { type: 'hex', required: true },
179+
1: { type: 'blockNumber', required: true },
180+
2: { type: 'array', required: false },
181181
})
182182
@rpcParamLayoutConfig(RPC_LAYOUT.custom((params) => [Number(params[0]), params[1], params[2]]))
183183
@cache(CacheService.getInstance(CACHE_LEVEL.L1), {
@@ -248,8 +248,8 @@ export class EthImpl implements Eth {
248248
*/
249249
@rpcMethod
250250
@rpcParamValidationRules({
251-
0: { type: ParamType.TRANSACTION, required: true },
252-
1: { type: ParamType.BLOCK_NUMBER, required: false },
251+
0: { type: 'transaction', required: true },
252+
1: { type: 'blockNumber', required: false },
253253
})
254254
@rpcParamLayoutConfig(RPC_LAYOUT.custom((params) => [params[0], params[1]]))
255255
async estimateGas(
@@ -322,7 +322,7 @@ export class EthImpl implements Eth {
322322
*/
323323
@rpcMethod
324324
@rpcParamValidationRules({
325-
0: { type: ParamType.FILTER, required: true },
325+
0: { type: 'filter', required: true },
326326
})
327327
async newFilter(params: INewFilterParams, requestDetails: RequestDetails): Promise<string> {
328328
const requestIdPrefix = requestDetails.formattedRequestId;
@@ -344,7 +344,7 @@ export class EthImpl implements Eth {
344344
*/
345345
@rpcMethod
346346
@rpcParamValidationRules({
347-
0: { type: ParamType.HEX, required: true },
347+
0: { type: 'hex', required: true },
348348
})
349349
async getFilterLogs(filterId: string, requestDetails: RequestDetails): Promise<Log[]> {
350350
if (this.logger.isLevelEnabled('trace')) {
@@ -365,7 +365,7 @@ export class EthImpl implements Eth {
365365
*/
366366
@rpcMethod
367367
@rpcParamValidationRules({
368-
0: { type: ParamType.HEX, required: true },
368+
0: { type: 'hex', required: true },
369369
})
370370
async getFilterChanges(filterId: string, requestDetails: RequestDetails): Promise<string[] | Log[]> {
371371
if (this.logger.isLevelEnabled('trace')) {
@@ -404,7 +404,7 @@ export class EthImpl implements Eth {
404404
*/
405405
@rpcMethod
406406
@rpcParamValidationRules({
407-
0: { type: ParamType.HEX, required: true },
407+
0: { type: 'hex', required: true },
408408
})
409409
async uninstallFilter(filterId: string, requestDetails: RequestDetails): Promise<boolean> {
410410
if (this.logger.isLevelEnabled('trace')) {
@@ -677,9 +677,9 @@ export class EthImpl implements Eth {
677677
*/
678678
@rpcMethod
679679
@rpcParamValidationRules({
680-
0: { type: ParamType.ADDRESS, required: true },
681-
1: { type: ParamType.HEX64, required: true },
682-
2: { type: ParamType.BLOCK_NUMBER_OR_HASH, required: true },
680+
0: { type: 'address', required: true },
681+
1: { type: 'hex64', required: true },
682+
2: { type: ['blockNumber', 'blockHash'], required: true },
683683
})
684684
@rpcParamLayoutConfig(RPC_LAYOUT.custom((params) => [params[0], params[1], params[2]]))
685685
@cache(CacheService.getInstance(CACHE_LEVEL.L1), {
@@ -708,8 +708,8 @@ export class EthImpl implements Eth {
708708
*/
709709
@rpcMethod
710710
@rpcParamValidationRules({
711-
0: { type: ParamType.ADDRESS, required: true },
712-
1: { type: ParamType.BLOCK_NUMBER_OR_HASH, required: true },
711+
0: { type: 'address', required: true },
712+
1: { type: ['blockNumber', 'blockHash'], required: true },
713713
})
714714
@cache(CacheService.getInstance(CACHE_LEVEL.L1), {
715715
skipParams: [{ index: '1', value: constants.NON_CACHABLE_BLOCK_PARAMS }],
@@ -736,8 +736,8 @@ export class EthImpl implements Eth {
736736
*/
737737
@rpcMethod
738738
@rpcParamValidationRules({
739-
0: { type: ParamType.ADDRESS, required: true },
740-
1: { type: ParamType.BLOCK_NUMBER_OR_HASH, required: true },
739+
0: { type: 'address', required: true },
740+
1: { type: ['blockNumber', 'blockHash'], required: true },
741741
})
742742
@cache(CacheService.getInstance(CACHE_LEVEL.L1), {
743743
skipParams: [{ index: '1', value: constants.NON_CACHABLE_BLOCK_PARAMS }],
@@ -763,8 +763,8 @@ export class EthImpl implements Eth {
763763
*/
764764
@rpcMethod
765765
@rpcParamValidationRules({
766-
0: { type: ParamType.BLOCK_HASH, required: true },
767-
1: { type: ParamType.BOOLEAN, required: true },
766+
0: { type: 'blockHash', required: true },
767+
1: { type: 'boolean', required: true },
768768
})
769769
@cache(CacheService.getInstance(CACHE_LEVEL.L1))
770770
async getBlockByHash(hash: string, showDetails: boolean, requestDetails: RequestDetails): Promise<Block | null> {
@@ -783,7 +783,7 @@ export class EthImpl implements Eth {
783783
*/
784784
@rpcMethod
785785
@rpcParamValidationRules({
786-
0: { type: ParamType.BLOCK_HASH, required: true },
786+
0: { type: 'blockHash', required: true },
787787
})
788788
@cache(CacheService.getInstance(CACHE_LEVEL.L1))
789789
async getBlockTransactionCountByHash(hash: string, requestDetails: RequestDetails): Promise<string | null> {
@@ -802,7 +802,7 @@ export class EthImpl implements Eth {
802802
*/
803803
@rpcMethod
804804
@rpcParamValidationRules({
805-
0: { type: ParamType.BLOCK_NUMBER, required: true },
805+
0: { type: 'blockNumber', required: true },
806806
})
807807
@cache(CacheService.getInstance(CACHE_LEVEL.L1), {
808808
skipParams: [{ index: '0', value: constants.NON_CACHABLE_BLOCK_PARAMS }],
@@ -827,8 +827,8 @@ export class EthImpl implements Eth {
827827
*/
828828
@rpcMethod
829829
@rpcParamValidationRules({
830-
0: { type: ParamType.BLOCK_HASH, required: true },
831-
1: { type: ParamType.HEX, required: true },
830+
0: { type: 'blockHash', required: true },
831+
1: { type: 'hex', required: true },
832832
})
833833
@cache(CacheService.getInstance(CACHE_LEVEL.L1))
834834
async getTransactionByBlockHashAndIndex(
@@ -852,8 +852,8 @@ export class EthImpl implements Eth {
852852
*/
853853
@rpcMethod
854854
@rpcParamValidationRules({
855-
0: { type: ParamType.BLOCK_NUMBER, required: true },
856-
1: { type: ParamType.HEX, required: true },
855+
0: { type: 'blockNumber', required: true },
856+
1: { type: 'hex', required: true },
857857
})
858858
@cache(CacheService.getInstance(CACHE_LEVEL.L1), {
859859
skipParams: [{ index: '0', value: constants.NON_CACHABLE_BLOCK_PARAMS }],
@@ -883,8 +883,8 @@ export class EthImpl implements Eth {
883883
*/
884884
@rpcMethod
885885
@rpcParamValidationRules({
886-
0: { type: ParamType.BLOCK_NUMBER, required: true },
887-
1: { type: ParamType.BOOLEAN, required: true },
886+
0: { type: 'blockNumber', required: true },
887+
1: { type: 'boolean', required: true },
888888
})
889889
@cache(CacheService.getInstance(CACHE_LEVEL.L1), {
890890
skipParams: [{ index: '0', value: constants.NON_CACHABLE_BLOCK_PARAMS }],
@@ -913,8 +913,8 @@ export class EthImpl implements Eth {
913913
*/
914914
@rpcMethod
915915
@rpcParamValidationRules({
916-
0: { type: ParamType.ADDRESS, required: true },
917-
1: { type: ParamType.BLOCK_NUMBER_OR_HASH, required: true },
916+
0: { type: 'address', required: true },
917+
1: { type: ['blockNumber', 'blockHash'], required: true },
918918
})
919919
@cache(CacheService.getInstance(CACHE_LEVEL.L1), {
920920
skipParams: [{ index: '1', value: constants.NON_CACHABLE_BLOCK_PARAMS }],
@@ -939,7 +939,7 @@ export class EthImpl implements Eth {
939939
*/
940940
@rpcMethod
941941
@rpcParamValidationRules({
942-
0: { type: ParamType.HEX, required: true },
942+
0: { type: 'hex', required: true },
943943
})
944944
async sendRawTransaction(transaction: string, requestDetails: RequestDetails): Promise<string | JsonRpcError> {
945945
return await this.transactionService.sendRawTransaction(transaction, requestDetails);
@@ -958,8 +958,8 @@ export class EthImpl implements Eth {
958958
*/
959959
@rpcMethod
960960
@rpcParamValidationRules({
961-
0: { type: ParamType.TRANSACTION, required: true },
962-
1: { type: ParamType.BLOCK_PARAMS, required: true },
961+
0: { type: 'transaction', required: true },
962+
1: { type: 'blockParams', required: true },
963963
})
964964
@cache(CacheService.getInstance(CACHE_LEVEL.L1), {
965965
skipParams: [{ index: '1', value: constants.NON_CACHABLE_BLOCK_PARAMS }],
@@ -1001,7 +1001,7 @@ export class EthImpl implements Eth {
10011001
*/
10021002
@rpcMethod
10031003
@rpcParamValidationRules({
1004-
0: { type: ParamType.TRANSACTION_HASH, required: true },
1004+
0: { type: 'transactionHash', required: true },
10051005
})
10061006
@cache(CacheService.getInstance(CACHE_LEVEL.L1))
10071007
async getTransactionByHash(hash: string, requestDetails: RequestDetails): Promise<Transaction | null> {
@@ -1019,7 +1019,7 @@ export class EthImpl implements Eth {
10191019
*/
10201020
@rpcMethod
10211021
@rpcParamValidationRules({
1022-
0: { type: ParamType.TRANSACTION_HASH, required: true },
1022+
0: { type: 'transactionHash', required: true },
10231023
})
10241024
@cache(CacheService.getInstance(CACHE_LEVEL.L1))
10251025
async getTransactionReceipt(hash: string, requestDetails: RequestDetails): Promise<any> {
@@ -1058,7 +1058,7 @@ export class EthImpl implements Eth {
10581058
*/
10591059
@rpcMethod
10601060
@rpcParamValidationRules({
1061-
0: { type: ParamType.FILTER, required: true },
1061+
0: { type: 'filter', required: true },
10621062
})
10631063
@cache(CacheService.getInstance(CACHE_LEVEL.L1), {
10641064
skipNamedParams: [
@@ -1106,7 +1106,7 @@ export class EthImpl implements Eth {
11061106
*/
11071107
@rpcMethod
11081108
@rpcParamValidationRules({
1109-
0: { type: ParamType.BLOCK_NUMBER_OR_HASH, required: true },
1109+
0: { type: ['blockNumber', 'blockHash'], required: true },
11101110
})
11111111
@cache(CacheService.getInstance(CACHE_LEVEL.L1), {
11121112
skipParams: [{ index: '0', value: constants.NON_CACHABLE_BLOCK_PARAMS }],

packages/relay/src/lib/services/registryService/rpcMethodRegistryService.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// SPDX-License-Identifier: Apache-2.0
2-
import { RPC_METHOD_KEY, RPC_PARAM_LAYOUT_KEY, RPC_PARAM_VALIDATION_RULES_KEY } from '../../decorators';
2+
import { RPC_METHOD_KEY, RPC_PARAM_LAYOUT_KEY } from '../../decorators';
33
import { RpcMethodRegistry, RpcNamespaceRegistry } from '../../types';
4+
import { RPC_PARAM_VALIDATION_RULES_KEY } from '../../validators';
45

56
/**
67
* Registers RPC methods from the provided service implementations.

packages/relay/src/lib/types/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,5 @@ export * from './RequestDetails';
1111
export * from './requestParams';
1212
export * from './spendingPlanConfig';
1313
export * from './registry';
14-
export * from './validation';
1514
export * from './debug';
1615
export * from './rateLimiter';

0 commit comments

Comments
 (0)