Make RpcAddOns::launch_add_ons_with More Composable

by ADMIN 52 views

Describe the Feature

The current implementation of RpcAddOns::launch_add_ons_with in the Reth project always spawns two RPC servers. However, there are use cases where only the regular RPC server needs to be spawned, skipping the additional step. To address this, we can refactor the launch_add_ons_with function into multiple composable functions, allowing for more flexibility and customization.

Current Implementation

The current implementation of launch_add_ons_with is as follows:

// https://github.com/paradigmxyz/reth/blob/d69c42402fa9f9e9c24d73d59e4deeb82f1cdeb1/crates/node/builder/src/rpc.rs#L442-L455
fn launch_add_ons_with(
    &self,
    _config: &Config,
    _builder: &mut Builder,
    _rpc: &mut Rpc,
    _auth: &mut Auth,
) {
    // Spawn the regular RPC server
    self.spawn_rpc_server(_config, _builder, _rpc);

    // Spawn the authentication server
    self.spawn_auth_server(_config, _builder, _auth);
}

However, there are cases where only the regular RPC server needs to be spawned, as shown in the following code snippet:

// https://github.com/paradigmxyz/reth/blob/d69c42402fa9f9e9c24d73d59e4deeb82f1cdeb1/crates/node/builder/src/rpc.rs#L458-L458
fn launch_add_ons_with(
    &self,
    _config: &Config,
    _builder: &mut Builder,
    _rpc: &mut Rpc,
    _auth: &mut Auth,
) {
    // Spawn the regular RPC server
    self.spawn_rpc_server(_config, _builder, _rpc);
}

Refactoring the Function

To make launch_add_ons_with more composable, we can refactor it into multiple functions, each responsible for launching a specific RPC server. This will allow us to customize the behavior of the function and launch only the necessary servers.

// Define a function to launch the regular RPC server
fn launch_rpc_server(
    &self,
    _config: &Config,
    _builder: &mut Builder,
    _rpc: &mut Rpc,
) {
    // Spawn the regular RPC server
    self.spawn_rpc_server(_config, _builder, _rpc);
}

// Define a function to launch the authentication server
fn launch_auth_server(
    &self,
    _config: &Config,
    _builder: &mut Builder,
    _auth: &mut Auth,
) {
    // Spawn the authentication server
    self.spawn_auth_server(_config, _builder, _auth);
}

// Refactor launch_add_ons_with to compose the new functions
fn launch_add_ons_with(
    &self,
    _config: &Config,
    _builder: &mut Builder,
    _rpc: &mut Rpc,
    _auth: &mut Auth,
) {
    // Launch the regular RPC server
    self.launch_rpc_server(_config, _builder, _rpc);

    // Launch the authentication server
    self.launch_auth_server(_config _builder, _auth);
}

Working Around the Tricky Part

The tricky part is the step where we need to handle the case where only the regular RPC server needs to be spawned. We can work around this by introducing another function that accepts a closure instead of a simple if-else statement.

// Define a function to launch the RPC server with a closure
fn launch_rpc_server_with_closure(
    &self,
    _config: &Config,
    _builder: &mut Builder,
    _rpc: &mut Rpc,
    closure: impl FnOnce() + 'static,
) {
    // Spawn the regular RPC server
    self.spawn_rpc_server(_config, _builder, _rpc);

    // Execute the closure
    closure();
}

// Refactor launch_add_ons_with to use the new function
fn launch_add_ons_with(
    &self,
    _config: &Config,
    _builder: &mut Builder,
    _rpc: &mut Rpc,
    _auth: &mut Auth,
) {
    // Launch the regular RPC server with a closure
    self.launch_rpc_server_with_closure(_config, _builder, _rpc, || {
        // Do nothing
    });
}

Conclusion

Q: What is the current implementation of launch_add_ons_with?

A: The current implementation of launch_add_ons_with in the Reth project always spawns two RPC servers. However, there are use cases where only the regular RPC server needs to be spawned, skipping the additional step.

Q: Why do we need to refactor launch_add_ons_with?

A: We need to refactor launch_add_ons_with to make it more composable, allowing for more flexibility and customization. This will enable us to launch only the necessary RPC servers, making the code more efficient and scalable.

Q: What are the benefits of refactoring launch_add_ons_with?

A: The benefits of refactoring launch_add_ons_with include:

  • More flexibility and customization
  • Ability to launch only the necessary RPC servers
  • Improved code maintainability and understandability
  • Increased scalability and efficiency

Q: How do we refactor launch_add_ons_with into multiple composable functions?

A: We can refactor launch_add_ons_with into multiple functions, each responsible for launching a specific RPC server. This will allow us to customize the behavior of the function and launch only the necessary servers.

Q: What is the tricky part in refactoring launch_add_ons_with?

A: The tricky part is the step where we need to handle the case where only the regular RPC server needs to be spawned. We can work around this by introducing another function that accepts a closure instead of a simple if-else statement.

Q: How do we work around the tricky part?

A: We can work around the tricky part by introducing a new function that accepts a closure instead of a simple if-else statement. This will allow us to handle the case where only the regular RPC server needs to be spawned.

Q: What is the new function that accepts a closure?

A: The new function that accepts a closure is launch_rpc_server_with_closure. This function takes a closure as an argument and executes it after spawning the regular RPC server.

Q: How do we refactor launch_add_ons_with to use the new function?

A: We can refactor launch_add_ons_with to use the new function by calling launch_rpc_server_with_closure and passing a closure that does nothing.

Q: What are the advantages of using the new function?

A: The advantages of using the new function include:

  • Ability to handle the case where only the regular RPC server needs to be spawned
  • Improved code maintainability and understandability
  • Increased scalability and efficiency

Q: What is the conclusion of refactoring launch_add_ons_with?

A: In conclusion, refactoring launch_add_ons_with into multiple composable functions allows for more flexibility and customization. By introducing new functions and working around the tricky part, we can make the code more maintainable and easier to understand. This change will enable to launch only the necessary RPC servers, making the code more efficient and scalable.