|  | 
|  | 1 | +// Based on https://github.com/paritytech/jsonrpsee/blob/master/examples/examples/rpc_middleware_modify_request.rs | 
|  | 2 | + | 
|  | 3 | +use jsonrpsee::core::client::ClientT; | 
|  | 4 | +use jsonrpsee::server::middleware::rpc::{RpcServiceBuilder, RpcServiceT}; | 
|  | 5 | +use jsonrpsee::server::Server; | 
|  | 6 | +use jsonrpsee::types::Request; | 
|  | 7 | +use jsonrpsee::ws_client::WsClientBuilder; | 
|  | 8 | +use jsonrpsee::{rpc_params, RpcModule}; | 
|  | 9 | +use std::borrow::Cow as StdCow; | 
|  | 10 | +use std::net::SocketAddr; | 
|  | 11 | +use tracing_subscriber::EnvFilter; | 
|  | 12 | + | 
|  | 13 | +#[derive(Clone)] | 
|  | 14 | +pub struct ModifyRequestIf<S>(S); | 
|  | 15 | + | 
|  | 16 | +impl<'a, S> RpcServiceT<'a> for ModifyRequestIf<S> | 
|  | 17 | +where | 
|  | 18 | +    S: Send + Sync + RpcServiceT<'a>, | 
|  | 19 | +{ | 
|  | 20 | +    type Future = S::Future; | 
|  | 21 | + | 
|  | 22 | +    fn call(&self, mut req: Request<'a>) -> Self::Future { | 
|  | 23 | +        // Example how to modify the params in the call. | 
|  | 24 | +        // TODO: Change this to intercept eth_sendRawTransaction | 
|  | 25 | +        if req.method == "say_hello" { | 
|  | 26 | +            // It's a bit awkward to create new params in the request | 
|  | 27 | +            // but this shows how to do it. | 
|  | 28 | +            let raw_value = serde_json::value::to_raw_value("myparams").unwrap(); | 
|  | 29 | +            req.params = Some(StdCow::Owned(raw_value)); | 
|  | 30 | +        } | 
|  | 31 | +        // Re-direct all calls that isn't `say_hello` to `say_goodbye` | 
|  | 32 | +        // TODO: Change this to redirect all calls to the underlying RPC | 
|  | 33 | +        // (optionally, via an env variable if you want to forward) | 
|  | 34 | +        // If the node operator does not want to forward | 
|  | 35 | +        // non-eth_sendRawTransaction calls, then reject all other calls | 
|  | 36 | +        else if req.method != "say_hello" { | 
|  | 37 | +            req.method = "say_goodbye".into(); | 
|  | 38 | +        } | 
|  | 39 | + | 
|  | 40 | +        self.0.call(req) | 
|  | 41 | +    } | 
|  | 42 | +} | 
|  | 43 | + | 
|  | 44 | +#[tokio::main] | 
|  | 45 | +async fn main() -> anyhow::Result<()> { | 
|  | 46 | +    tracing_subscriber::fmt() | 
|  | 47 | +        .with_env_filter(EnvFilter::from_default_env()) | 
|  | 48 | +        .try_init() | 
|  | 49 | +        .expect("setting default subscriber failed"); | 
|  | 50 | + | 
|  | 51 | +    let addr = run_server().await?; | 
|  | 52 | +    let url = format!("ws://{}", addr); | 
|  | 53 | + | 
|  | 54 | +    let client = WsClientBuilder::default().build(&url).await?; | 
|  | 55 | +    let _response: String = client.request("say_hello", rpc_params![]).await?; | 
|  | 56 | +    let _response: Result<String, _> = client.request("unknown_method", rpc_params![]).await; | 
|  | 57 | +    let _: String = client.request("say_hello", rpc_params![]).await?; | 
|  | 58 | + | 
|  | 59 | +    Ok(()) | 
|  | 60 | +} | 
|  | 61 | + | 
|  | 62 | +async fn run_server() -> anyhow::Result<SocketAddr> { | 
|  | 63 | +    let rpc_middleware = RpcServiceBuilder::new().layer_fn(ModifyRequestIf); | 
|  | 64 | +    let server = Server::builder() | 
|  | 65 | +        .set_rpc_middleware(rpc_middleware) | 
|  | 66 | +        .build("127.0.0.1:0") | 
|  | 67 | +        .await?; | 
|  | 68 | +    let mut module = RpcModule::new(()); | 
|  | 69 | +    module.register_method("say_hello", |_, _, _| "lo")?; | 
|  | 70 | +    module.register_method("say_goodbye", |_, _, _| "goodbye")?; | 
|  | 71 | +    let addr = server.local_addr()?; | 
|  | 72 | + | 
|  | 73 | +    let handle = server.start(module); | 
|  | 74 | + | 
|  | 75 | +    // In this example we don't care about doing shutdown so let's it run forever. | 
|  | 76 | +    // You may use the `ServerHandle` to shut it down or manage it yourself. | 
|  | 77 | +    tokio::spawn(handle.stopped()); | 
|  | 78 | + | 
|  | 79 | +    Ok(addr) | 
|  | 80 | +} | 
0 commit comments