@@ -1966,6 +1966,25 @@ impl TransportRpcModules {
19661966 self . add_or_replace_ipc ( other) ?;
19671967 Ok ( ( ) )
19681968 }
1969+ /// Adds or replaces the given [`Methods`] in the transport modules where the specified
1970+ /// [`RethRpcModule`] is configured.
1971+ pub fn add_or_replace_if_module_configured (
1972+ & mut self ,
1973+ module : RethRpcModule ,
1974+ other : impl Into < Methods > ,
1975+ ) -> Result < ( ) , RegisterMethodError > {
1976+ let other = other. into ( ) ;
1977+ if self . module_config ( ) . contains_http ( & module) {
1978+ self . add_or_replace_http ( other. clone ( ) ) ?;
1979+ }
1980+ if self . module_config ( ) . contains_ws ( & module) {
1981+ self . add_or_replace_ws ( other. clone ( ) ) ?;
1982+ }
1983+ if self . module_config ( ) . contains_ipc ( & module) {
1984+ self . add_or_replace_ipc ( other) ?;
1985+ }
1986+ Ok ( ( ) )
1987+ }
19691988}
19701989
19711990/// Returns the methods installed in the given module that match the given filter.
@@ -2522,4 +2541,56 @@ mod tests {
25222541 assert ! ( modules. ipc. as_ref( ) . unwrap( ) . method( "anything" ) . is_some( ) ) ;
25232542 assert ! ( modules. ws. as_ref( ) . unwrap( ) . method( "anything" ) . is_some( ) ) ;
25242543 }
2544+
2545+ #[ test]
2546+ fn test_add_or_replace_if_module_configured ( ) {
2547+ // Create a config that enables RethRpcModule::Eth for HTTP and WS, but NOT IPC
2548+ let config = TransportRpcModuleConfig :: default ( )
2549+ . with_http ( [ RethRpcModule :: Eth ] )
2550+ . with_ws ( [ RethRpcModule :: Eth ] ) ;
2551+
2552+ // Create HTTP module with an existing method (to test "replace")
2553+ let mut http_module = RpcModule :: new ( ( ) ) ;
2554+ http_module. register_method ( "eth_existing" , |_, _, _| "original" ) . unwrap ( ) ;
2555+
2556+ // Create WS module with the same existing method
2557+ let mut ws_module = RpcModule :: new ( ( ) ) ;
2558+ ws_module. register_method ( "eth_existing" , |_, _, _| "original" ) . unwrap ( ) ;
2559+
2560+ // Create IPC module (empty, to ensure no changes)
2561+ let ipc_module = RpcModule :: new ( ( ) ) ;
2562+
2563+ // Set up TransportRpcModules with the config and modules
2564+ let mut modules = TransportRpcModules {
2565+ config,
2566+ http : Some ( http_module) ,
2567+ ws : Some ( ws_module) ,
2568+ ipc : Some ( ipc_module) ,
2569+ } ;
2570+
2571+ // Create new methods: one to replace an existing method, one to add a new one
2572+ let mut new_module = RpcModule :: new ( ( ) ) ;
2573+ new_module. register_method ( "eth_existing" , |_, _, _| "replaced" ) . unwrap ( ) ; // Replace
2574+ new_module. register_method ( "eth_new" , |_, _, _| "added" ) . unwrap ( ) ; // Add
2575+ let new_methods: Methods = new_module. into ( ) ;
2576+
2577+ // Call the function for RethRpcModule::Eth
2578+ let result = modules. add_or_replace_if_module_configured ( RethRpcModule :: Eth , new_methods) ;
2579+ assert ! ( result. is_ok( ) , "Function should succeed" ) ;
2580+
2581+ // Verify HTTP: existing method still exists (replaced), new method added
2582+ let http = modules. http . as_ref ( ) . unwrap ( ) ;
2583+ assert ! ( http. method( "eth_existing" ) . is_some( ) ) ;
2584+ assert ! ( http. method( "eth_new" ) . is_some( ) ) ;
2585+
2586+ // Verify WS: existing method still exists (replaced), new method added
2587+ let ws = modules. ws . as_ref ( ) . unwrap ( ) ;
2588+ assert ! ( ws. method( "eth_existing" ) . is_some( ) ) ;
2589+ assert ! ( ws. method( "eth_new" ) . is_some( ) ) ;
2590+
2591+ // Verify IPC: no changes (Eth not configured for IPC)
2592+ let ipc = modules. ipc . as_ref ( ) . unwrap ( ) ;
2593+ assert ! ( ipc. method( "eth_existing" ) . is_none( ) ) ;
2594+ assert ! ( ipc. method( "eth_new" ) . is_none( ) ) ;
2595+ }
25252596}
0 commit comments