Skip to content

feat: implemented decorator-based RPC method dispatcher and parameter handling system #3581

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 43 commits into from
Mar 28, 2025

Conversation

quiet-node
Copy link
Contributor

@quiet-node quiet-node commented Mar 22, 2025

Overview

This PR introduces a fully decorator-based API architecture for JSON-RPC method handling. It provides a centralized solution that ensures a clean, type-safe approach to defining, exposing, validating, and dispatching RPC methods across the Relay package.

Key Components

New Single & Centralized API Entry Point

  • RpcMethodDispatcher is a centralized and robust class that serves as the sole public API entry point for server packages and all consumers. It provides a structured solution for executing RPC methods by autonomously validating requests and routing them to the appropriate operation handler.
  • Streamlined request handling flow, incorporating pre-execution method validation, execution, and error-handling phases.
  • Standardized response format, ensuring that methods either return a valid result or throw a JsonRpcError back to consumers.
// RpcMethodDispatcher used in Relay to create the signle API Entry Point
// relay.ts
public async executeRpcMethod(
    rpcMethodName: string,
    rpcMethodParams: any,
    requestDetails: RequestDetails,
): Promise<any> {
    return this.rpcMethodDispatcher.dispatch(rpcMethodName, rpcMethodParams, requestDetails);
}


// Usage from server or other consumer
// koaJsonRpc.ts in Server package
const result = await relay.executeRpcMethod('eth_getBalance', ['0x123...', 'latest'], requestId);

Decorator-Based Components

  • @rpcMethod annotates methods for RPC exposure and automatic registration.
  • @rpcParamValidationRules defines validation rules for method parameters, ensuring data integrity.
  • @rpcParamLayoutConfig manages parameter arrangement and transformation for correct parameters layout.
  • All decorators function cohesively to establish a declarative, self-documenting API.
// Class implementation with decorators
class EthImpl {
    // decorators usage
    @rpcMethod
    @rpcParamLayoutConfig(RPC_LAYOUT.REQUEST_DETAILS_ONLY)
    chainId(requestDetails: RequestDetails): string {
        // Implementation
    }

    @rpcMethod
    @rpcParamValidationRules({
      0: { type: 'blockHash', required: true },
      1: { type: 'boolean', required: false }
    })
    @rpcParamLayoutConfig(RPC_LAYOUT.custom((params) => [params[0], params[1]]))
    async estimateGas(
        transaction: IContractCallRequest,
        _blockParam: string | null,
        requestDetails: RequestDetails,
    ): Promise<string | JsonRpcError> {
        // Implementation
    }
}

Centralized Method Registry

  • rpcMethodRegistry serves as the single source of truth for all RPC method implementations
  • The method registerRpcMethod():
    • Scans implementation classes for methods decorated with @rpcMethod
    • Preserves validation rules from @rpcParamValidationRules for runtime parameter validation
    • Retains parameter layout configurations from @rpcParamLayoutConfig for correct invocation
    • Maps RPC method names to their implementations in a centralized registry

Parameter Handling

  • Clean separation between validation and parameter structuring
  • Autonomous parameter transformation based on method signature
  • Support for special parameter layouts (request-details-only, custom params layout, etc.)
  • Advanced parameter parsing for complex methods like traceTransaction

Server Package Simplification

  • Eliminated Manual Method Registration:

    • Removed the logAndHandleResponse() and useRpc() pattern previously used to register RPC methods
    • Deleted route files (ethRoutes.ts, debugRoutes.ts, netRoutes.ts, etc.) as methods are now auto-registered in the Relay pacakge
    • Server no longer needs to maintain method mappings or handle routing logic
  • Validator Migration:

    • Migrated the entire Validator service from Server package to Relay
    • Validator now works directly with @rpcParamValidationRules decorators
    • Validation happens automatically during the dispatch process
    • Simplified error handling with consistent response formats

Additional Context

Related issue(s): EPIC #3551

Fixes #3553
Fixes #3575

Notes for reviewer:

  • The Validator service was migrated from the Server package to the Relay component by component, ensuring the preservation of validation logic.
  • MirrorNodeClientError and SdkClientError are currently handled at the dispatcher only for timeout errors. A more comprehensive error mapping will be introduced in a follow-up PR for Stage 3.

Acknowledgments

Special kudos to @acuarica for initially proposing this approach and for the excellent work on the POC PR (#3548). His contributions laid the foundation for this implementation and significantly influenced the design of this PR.

@quiet-node quiet-node added the enhancement New feature or request label Mar 22, 2025
@quiet-node quiet-node added this to the 0.68.0 milestone Mar 22, 2025
@quiet-node quiet-node self-assigned this Mar 22, 2025
@quiet-node quiet-node linked an issue Mar 22, 2025 that may be closed by this pull request
Copy link

github-actions bot commented Mar 22, 2025

Test Results

 20 files   -  2  298 suites  +29   35m 4s ⏱️ - 34m 54s
623 tests  -  4  615 ✅ + 5  4 💤 ±0  4 ❌  -  9 
778 runs   - 92  768 ✅  - 81  6 💤 +2  4 ❌  - 13 

For more details on these failures, see this check.

Results for commit edc1a1c. ± Comparison against base commit 7f226e3.

♻️ This comment has been updated with latest results.

@quiet-node quiet-node force-pushed the 3553-implement-decorator-based-dispatcher branch 2 times, most recently from 709baa2 to d26a1ed Compare March 23, 2025 01:00
@quiet-node quiet-node marked this pull request as ready for review March 23, 2025 02:53
@quiet-node quiet-node requested review from a team as code owners March 23, 2025 02:53
@quiet-node quiet-node requested a review from AlfredoG87 March 23, 2025 02:53
@quiet-node quiet-node force-pushed the 3553-implement-decorator-based-dispatcher branch 3 times, most recently from 6d47cdc to 8cc81c4 Compare March 24, 2025 20:57
Copy link
Contributor

@acuarica acuarica left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Initial round of reviews, thanks for the effort! we are on good track.

Copy link
Contributor

@natanasow natanasow left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At first sight, it looks good. Thanks for the crazy amount of work you have already done 🚀.

@quiet-node quiet-node force-pushed the 3553-implement-decorator-based-dispatcher branch 2 times, most recently from 28f241a to 6b1e1db Compare March 26, 2025 17:06
natanasow
natanasow previously approved these changes Mar 26, 2025
…class"

This reverts commit 14b47b6.
Signed-off-by: Logan Nguyen <[email protected]>

fix:

Signed-off-by: Logan Nguyen <[email protected]>
Signed-off-by: Logan Nguyen <[email protected]>

Revert "chore: removed unnecessary index.ts"

This reverts commit 28f241a.

Reapply "chore: removed unnecessary index.ts"

This reverts commit f2e1524e060ce409adced9f350f486ef4de72754.
Signed-off-by: Logan Nguyen <[email protected]>
@quiet-node quiet-node force-pushed the 3553-implement-decorator-based-dispatcher branch from cfe5d1e to 2791180 Compare March 27, 2025 13:49
Signed-off-by: Logan Nguyen <[email protected]>

Revert "feat: registered getBlockReceipts to rpcMethodRegistry"

This reverts commit 2791180.

Reapply "feat: registered getBlockReceipts to rpcMethodRegistry"

This reverts commit 30a2419.
Signed-off-by: Logan Nguyen <[email protected]>
@quiet-node quiet-node force-pushed the 3553-implement-decorator-based-dispatcher branch from 44abf6c to b5af2b7 Compare March 27, 2025 14:50
Signed-off-by: Logan Nguyen <[email protected]>
@quiet-node quiet-node merged commit 92adeae into main Mar 28, 2025
37 of 39 checks passed
@quiet-node quiet-node deleted the 3553-implement-decorator-based-dispatcher branch March 28, 2025 13:50
@acuarica acuarica modified the milestones: 0.68.0, 0.67.0 Apr 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Ensure RequestID is generated and part of the Response Implement Decorator-Based Dispatcher
4 participants