mirror of
https://github.com/hl-archive-node/nanoreth.git
synced 2025-12-06 10:59:55 +00:00
feat(rpc): Add method removal functionality for RPC transports (#10497)
Co-authored-by: Matthias Seitz <matthias.seitz@outlook.de>
This commit is contained in:
@ -1709,6 +1709,62 @@ impl TransportRpcModules {
|
||||
self.merge_ipc(other)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Removes the method with the given name from the configured http methods.
|
||||
///
|
||||
/// Returns `true` if the method was found and removed, `false` otherwise.
|
||||
///
|
||||
/// Be aware that a subscription consist of two methods, `subscribe` and `unsubscribe` and
|
||||
/// it's the caller responsibility to remove both `subscribe` and `unsubscribe` methods for
|
||||
/// subscriptions.
|
||||
pub fn remove_http_method(&mut self, method_name: &'static str) -> bool {
|
||||
if let Some(http_module) = &mut self.http {
|
||||
http_module.remove_method(method_name).is_some()
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
/// Removes the method with the given name from the configured ws methods.
|
||||
///
|
||||
/// Returns `true` if the method was found and removed, `false` otherwise.
|
||||
///
|
||||
/// Be aware that a subscription consist of two methods, `subscribe` and `unsubscribe` and
|
||||
/// it's the caller responsibility to remove both `subscribe` and `unsubscribe` methods for
|
||||
/// subscriptions.
|
||||
pub fn remove_ws_method(&mut self, method_name: &'static str) -> bool {
|
||||
if let Some(ws_module) = &mut self.ws {
|
||||
ws_module.remove_method(method_name).is_some()
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
/// Removes the method with the given name from the configured ipc methods.
|
||||
///
|
||||
/// Returns `true` if the method was found and removed, `false` otherwise.
|
||||
///
|
||||
/// Be aware that a subscription consist of two methods, `subscribe` and `unsubscribe` and
|
||||
/// it's the caller responsibility to remove both `subscribe` and `unsubscribe` methods for
|
||||
/// subscriptions.
|
||||
pub fn remove_ipc_method(&mut self, method_name: &'static str) -> bool {
|
||||
if let Some(ipc_module) = &mut self.ipc {
|
||||
ipc_module.remove_method(method_name).is_some()
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
/// Removes the method with the given name from all configured transports.
|
||||
///
|
||||
/// Returns `true` if the method was found and removed, `false` otherwise.
|
||||
pub fn remove_method_from_configured(&mut self, method_name: &'static str) -> bool {
|
||||
let http_removed = self.remove_http_method(method_name);
|
||||
let ws_removed = self.remove_ws_method(method_name);
|
||||
let ipc_removed = self.remove_ipc_method(method_name);
|
||||
|
||||
http_removed || ws_removed || ipc_removed
|
||||
}
|
||||
}
|
||||
|
||||
/// A handle to the spawned servers.
|
||||
@ -1957,4 +2013,82 @@ mod tests {
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
mod remove_methods {
|
||||
use super::*;
|
||||
|
||||
fn create_test_module() -> RpcModule<()> {
|
||||
let mut module = RpcModule::new(());
|
||||
module.register_method("anything", |_, _, _| "succeed").unwrap();
|
||||
module
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_remove_http_method() {
|
||||
let mut modules =
|
||||
TransportRpcModules { http: Some(create_test_module()), ..Default::default() };
|
||||
// Remove a method that exists
|
||||
assert!(modules.remove_http_method("anything"));
|
||||
|
||||
// Remove a method that does not exist
|
||||
assert!(!modules.remove_http_method("non_existent_method"));
|
||||
|
||||
// Verify that the method was removed
|
||||
assert!(modules.http.as_ref().unwrap().method("anything").is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_remove_ws_method() {
|
||||
let mut modules =
|
||||
TransportRpcModules { ws: Some(create_test_module()), ..Default::default() };
|
||||
|
||||
// Remove a method that exists
|
||||
assert!(modules.remove_ws_method("anything"));
|
||||
|
||||
// Remove a method that does not exist
|
||||
assert!(!modules.remove_ws_method("non_existent_method"));
|
||||
|
||||
// Verify that the method was removed
|
||||
assert!(modules.ws.as_ref().unwrap().method("anything").is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_remove_ipc_method() {
|
||||
let mut modules =
|
||||
TransportRpcModules { ipc: Some(create_test_module()), ..Default::default() };
|
||||
|
||||
// Remove a method that exists
|
||||
assert!(modules.remove_ipc_method("anything"));
|
||||
|
||||
// Remove a method that does not exist
|
||||
assert!(!modules.remove_ipc_method("non_existent_method"));
|
||||
|
||||
// Verify that the method was removed
|
||||
assert!(modules.ipc.as_ref().unwrap().method("anything").is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_remove_method_from_configured() {
|
||||
let mut modules = TransportRpcModules {
|
||||
http: Some(create_test_module()),
|
||||
ws: Some(create_test_module()),
|
||||
ipc: Some(create_test_module()),
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
// Remove a method that exists
|
||||
assert!(modules.remove_method_from_configured("anything"));
|
||||
|
||||
// Remove a method that was just removed (it does not exist anymore)
|
||||
assert!(!modules.remove_method_from_configured("anything"));
|
||||
|
||||
// Remove a method that does not exist
|
||||
assert!(!modules.remove_method_from_configured("non_existent_method"));
|
||||
|
||||
// Verify that the method was removed from all transports
|
||||
assert!(modules.http.as_ref().unwrap().method("anything").is_none());
|
||||
assert!(modules.ws.as_ref().unwrap().method("anything").is_none());
|
||||
assert!(modules.ipc.as_ref().unwrap().method("anything").is_none());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user