Skip to content

Commit 5b59734

Browse files
acuaricakonstantinabl
authored andcommitted
Add validator refactor PoC draft
Signed-off-by: Luis Mastrangelo <[email protected]>
1 parent 688545a commit 5b59734

File tree

16 files changed

+213
-437
lines changed

16 files changed

+213
-437
lines changed

packages/relay/src/lib/debug.ts

Lines changed: 7 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,9 @@ 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: ['tracerType', 'tracerConfig', 'tracerConfigWrapper'], required: false },
110+
2: { type: 'tracerConfig', required: false },
110111
})
111112
@rpcParamLayoutConfig(RPC_LAYOUT.custom((params) => [params[0], params[1]]))
112113
@cache(CacheService.getInstance(CACHE_LEVEL.L1))
@@ -161,8 +162,8 @@ export class DebugImpl implements Debug {
161162
*/
162163
@rpcMethod
163164
@rpcParamValidationRules({
164-
0: { type: ParamType.BLOCK_NUMBER, required: true },
165-
1: { type: ParamType.TRACER_CONFIG_WRAPPER, required: false },
165+
0: { type: 'blockNumber', required: true },
166+
1: { type: 'tracerConfigWrapper', required: false },
166167
})
167168
@rpcParamLayoutConfig(RPC_LAYOUT.custom((params) => [params[0], params[1]]))
168169
@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 & 36 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 {
@@ -35,6 +35,7 @@ import {
3535
RequestDetails,
3636
} from './types';
3737
import { ParamType } from './types/validation';
38+
import { rpcParamValidationRules } from './validators';
3839

3940
/**
4041
* Implementation of the "eth_" methods from the Ethereum JSON-RPC API.
@@ -175,9 +176,9 @@ export class EthImpl implements Eth {
175176
*/
176177
@rpcMethod
177178
@rpcParamValidationRules({
178-
0: { type: ParamType.HEX, required: true },
179-
1: { type: ParamType.BLOCK_NUMBER, required: true },
180-
2: { type: ParamType.ARRAY, required: false },
179+
0: { type: 'hex', required: true },
180+
1: { type: 'blockNumber', required: true },
181+
2: { type: 'array', required: false },
181182
})
182183
@rpcParamLayoutConfig(RPC_LAYOUT.custom((params) => [Number(params[0]), params[1], params[2]]))
183184
@cache(CacheService.getInstance(CACHE_LEVEL.L1), {
@@ -248,8 +249,8 @@ export class EthImpl implements Eth {
248249
*/
249250
@rpcMethod
250251
@rpcParamValidationRules({
251-
0: { type: ParamType.TRANSACTION, required: true },
252-
1: { type: ParamType.BLOCK_NUMBER, required: false },
252+
0: { type: 'transaction', required: true },
253+
1: { type: 'blockNumber', required: false },
253254
})
254255
@rpcParamLayoutConfig(RPC_LAYOUT.custom((params) => [params[0], params[1]]))
255256
async estimateGas(
@@ -322,7 +323,7 @@ export class EthImpl implements Eth {
322323
*/
323324
@rpcMethod
324325
@rpcParamValidationRules({
325-
0: { type: ParamType.FILTER, required: true },
326+
0: { type: 'filter', required: true },
326327
})
327328
async newFilter(params: INewFilterParams, requestDetails: RequestDetails): Promise<string> {
328329
const requestIdPrefix = requestDetails.formattedRequestId;
@@ -344,7 +345,7 @@ export class EthImpl implements Eth {
344345
*/
345346
@rpcMethod
346347
@rpcParamValidationRules({
347-
0: { type: ParamType.HEX, required: true },
348+
0: { type: 'hex', required: true },
348349
})
349350
async getFilterLogs(filterId: string, requestDetails: RequestDetails): Promise<Log[]> {
350351
if (this.logger.isLevelEnabled('trace')) {
@@ -365,7 +366,7 @@ export class EthImpl implements Eth {
365366
*/
366367
@rpcMethod
367368
@rpcParamValidationRules({
368-
0: { type: ParamType.HEX, required: true },
369+
0: { type: 'hex', required: true },
369370
})
370371
async getFilterChanges(filterId: string, requestDetails: RequestDetails): Promise<string[] | Log[]> {
371372
if (this.logger.isLevelEnabled('trace')) {
@@ -404,7 +405,7 @@ export class EthImpl implements Eth {
404405
*/
405406
@rpcMethod
406407
@rpcParamValidationRules({
407-
0: { type: ParamType.HEX, required: true },
408+
0: { type: 'hex', required: true },
408409
})
409410
async uninstallFilter(filterId: string, requestDetails: RequestDetails): Promise<boolean> {
410411
if (this.logger.isLevelEnabled('trace')) {
@@ -677,9 +678,9 @@ export class EthImpl implements Eth {
677678
*/
678679
@rpcMethod
679680
@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 },
681+
0: { type: 'address', required: true },
682+
1: { type: 'hex64', required: true },
683+
2: { type: ['blockNumber', 'blockHash'], required: false },
683684
})
684685
@rpcParamLayoutConfig(RPC_LAYOUT.custom((params) => [params[0], params[1], params[2]]))
685686
@cache(CacheService.getInstance(CACHE_LEVEL.L1), {
@@ -708,8 +709,8 @@ export class EthImpl implements Eth {
708709
*/
709710
@rpcMethod
710711
@rpcParamValidationRules({
711-
0: { type: ParamType.ADDRESS, required: true },
712-
1: { type: ParamType.BLOCK_NUMBER_OR_HASH, required: true },
712+
0: { type: 'address', required: true },
713+
1: { type: ['blockNumber', 'blockHash'], required: true },
713714
})
714715
@cache(CacheService.getInstance(CACHE_LEVEL.L1), {
715716
skipParams: [{ index: '1', value: constants.NON_CACHABLE_BLOCK_PARAMS }],
@@ -736,8 +737,8 @@ export class EthImpl implements Eth {
736737
*/
737738
@rpcMethod
738739
@rpcParamValidationRules({
739-
0: { type: ParamType.ADDRESS, required: true },
740-
1: { type: ParamType.BLOCK_NUMBER_OR_HASH, required: true },
740+
0: { type: 'address', required: true },
741+
1: { type: ['blockNumber', 'blockHash'], required: true },
741742
})
742743
@cache(CacheService.getInstance(CACHE_LEVEL.L1), {
743744
skipParams: [{ index: '1', value: constants.NON_CACHABLE_BLOCK_PARAMS }],
@@ -763,8 +764,8 @@ export class EthImpl implements Eth {
763764
*/
764765
@rpcMethod
765766
@rpcParamValidationRules({
766-
0: { type: ParamType.BLOCK_HASH, required: true },
767-
1: { type: ParamType.BOOLEAN, required: true },
767+
0: { type: 'blockHash', required: true },
768+
1: { type: 'boolean', required: true },
768769
})
769770
@cache(CacheService.getInstance(CACHE_LEVEL.L1))
770771
async getBlockByHash(hash: string, showDetails: boolean, requestDetails: RequestDetails): Promise<Block | null> {
@@ -783,7 +784,7 @@ export class EthImpl implements Eth {
783784
*/
784785
@rpcMethod
785786
@rpcParamValidationRules({
786-
0: { type: ParamType.BLOCK_HASH, required: true },
787+
0: { type: 'blockHash', required: true },
787788
})
788789
@cache(CacheService.getInstance(CACHE_LEVEL.L1))
789790
async getBlockTransactionCountByHash(hash: string, requestDetails: RequestDetails): Promise<string | null> {
@@ -802,7 +803,7 @@ export class EthImpl implements Eth {
802803
*/
803804
@rpcMethod
804805
@rpcParamValidationRules({
805-
0: { type: ParamType.BLOCK_NUMBER, required: true },
806+
0: { type: 'blockNumber', required: true },
806807
})
807808
@cache(CacheService.getInstance(CACHE_LEVEL.L1), {
808809
skipParams: [{ index: '0', value: constants.NON_CACHABLE_BLOCK_PARAMS }],
@@ -827,8 +828,8 @@ export class EthImpl implements Eth {
827828
*/
828829
@rpcMethod
829830
@rpcParamValidationRules({
830-
0: { type: ParamType.BLOCK_HASH, required: true },
831-
1: { type: ParamType.HEX, required: true },
831+
0: { type: 'blockHash', required: true },
832+
1: { type: 'hex', required: true },
832833
})
833834
@cache(CacheService.getInstance(CACHE_LEVEL.L1))
834835
async getTransactionByBlockHashAndIndex(
@@ -852,8 +853,8 @@ export class EthImpl implements Eth {
852853
*/
853854
@rpcMethod
854855
@rpcParamValidationRules({
855-
0: { type: ParamType.BLOCK_NUMBER, required: true },
856-
1: { type: ParamType.HEX, required: true },
856+
0: { type: 'blockNumber', required: true },
857+
1: { type: 'hex', required: true },
857858
})
858859
@cache(CacheService.getInstance(CACHE_LEVEL.L1), {
859860
skipParams: [{ index: '0', value: constants.NON_CACHABLE_BLOCK_PARAMS }],
@@ -883,8 +884,8 @@ export class EthImpl implements Eth {
883884
*/
884885
@rpcMethod
885886
@rpcParamValidationRules({
886-
0: { type: ParamType.BLOCK_NUMBER, required: true },
887-
1: { type: ParamType.BOOLEAN, required: true },
887+
0: { type: 'blockNumber', required: true },
888+
1: { type: 'boolean', required: true },
888889
})
889890
@cache(CacheService.getInstance(CACHE_LEVEL.L1), {
890891
skipParams: [{ index: '0', value: constants.NON_CACHABLE_BLOCK_PARAMS }],
@@ -913,8 +914,8 @@ export class EthImpl implements Eth {
913914
*/
914915
@rpcMethod
915916
@rpcParamValidationRules({
916-
0: { type: ParamType.ADDRESS, required: true },
917-
1: { type: ParamType.BLOCK_NUMBER_OR_HASH, required: true },
917+
0: { type: 'address', required: true },
918+
1: { type: ['blockNumber', 'blockHash'], required: true },
918919
})
919920
@cache(CacheService.getInstance(CACHE_LEVEL.L1), {
920921
skipParams: [{ index: '1', value: constants.NON_CACHABLE_BLOCK_PARAMS }],
@@ -939,7 +940,7 @@ export class EthImpl implements Eth {
939940
*/
940941
@rpcMethod
941942
@rpcParamValidationRules({
942-
0: { type: ParamType.HEX, required: true },
943+
0: { type: 'hex', required: true },
943944
})
944945
async sendRawTransaction(transaction: string, requestDetails: RequestDetails): Promise<string | JsonRpcError> {
945946
return await this.transactionService.sendRawTransaction(transaction, requestDetails);
@@ -958,8 +959,8 @@ export class EthImpl implements Eth {
958959
*/
959960
@rpcMethod
960961
@rpcParamValidationRules({
961-
0: { type: ParamType.TRANSACTION, required: true },
962-
1: { type: ParamType.BLOCK_PARAMS, required: true },
962+
0: { type: 'transaction', required: true },
963+
1: { type: 'blockParams', required: true },
963964
})
964965
@cache(CacheService.getInstance(CACHE_LEVEL.L1), {
965966
skipParams: [{ index: '1', value: constants.NON_CACHABLE_BLOCK_PARAMS }],
@@ -1001,7 +1002,7 @@ export class EthImpl implements Eth {
10011002
*/
10021003
@rpcMethod
10031004
@rpcParamValidationRules({
1004-
0: { type: ParamType.TRANSACTION_HASH, required: true },
1005+
0: { type: 'transactionHash', required: true },
10051006
})
10061007
@cache(CacheService.getInstance(CACHE_LEVEL.L1))
10071008
async getTransactionByHash(hash: string, requestDetails: RequestDetails): Promise<Transaction | null> {
@@ -1019,7 +1020,7 @@ export class EthImpl implements Eth {
10191020
*/
10201021
@rpcMethod
10211022
@rpcParamValidationRules({
1022-
0: { type: ParamType.TRANSACTION_HASH, required: true },
1023+
0: { type: 'transactionHash', required: true },
10231024
})
10241025
@cache(CacheService.getInstance(CACHE_LEVEL.L1))
10251026
async getTransactionReceipt(hash: string, requestDetails: RequestDetails): Promise<any> {
@@ -1058,7 +1059,7 @@ export class EthImpl implements Eth {
10581059
*/
10591060
@rpcMethod
10601061
@rpcParamValidationRules({
1061-
0: { type: ParamType.FILTER, required: true },
1062+
0: { type: 'filter', required: true },
10621063
})
10631064
@cache(CacheService.getInstance(CACHE_LEVEL.L1), {
10641065
skipNamedParams: [
@@ -1106,7 +1107,7 @@ export class EthImpl implements Eth {
11061107
*/
11071108
@rpcMethod
11081109
@rpcParamValidationRules({
1109-
0: { type: ParamType.BLOCK_NUMBER_OR_HASH, required: true },
1110+
0: { type: ['blockNumber', 'blockHash'], required: true },
11101111
})
11111112
@cache(CacheService.getInstance(CACHE_LEVEL.L1), {
11121113
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)