Skip to content

Commit d189f39

Browse files
authored
Merge pull request #29 from flashbots/health-check
Add health check endpoint
2 parents 00c87f0 + 9bb0d31 commit d189f39

File tree

1 file changed

+49
-1
lines changed

1 file changed

+49
-1
lines changed

src/proxy.rs

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,12 @@ where
5858
}
5959

6060
fn call(&mut self, req: HttpRequest<HttpBody>) -> Self::Future {
61+
match req.uri().path() {
62+
"/healthz" => return Box::pin(async { Ok(Self::Response::new(HttpBody::from("OK"))) }),
63+
"/metrics" => {}
64+
_ => {}
65+
};
66+
6167
let target_url = self.target_url.clone();
6268
let client = self.client.clone();
6369
let mut inner = self.inner.clone();
@@ -86,7 +92,10 @@ where
8692
message = "received json rpc request for",
8793
method = method.method
8894
);
89-
if PROXY_METHODS.iter().any(|&m| method.method.starts_with(m)) {
95+
if MULTIPLEX_METHODS
96+
.iter()
97+
.any(|&m| method.method.starts_with(m))
98+
{
9099
// let rpc server handle engine rpc requests
91100
let res = inner.call(req).await.map_err(|e| e.into())?;
92101
Ok(res)
@@ -110,6 +119,7 @@ where
110119
mod tests {
111120
use std::net::SocketAddr;
112121

122+
use http_body_util::BodyExt;
113123
use jsonrpsee::{
114124
core::{client::ClientT, ClientError},
115125
http_client::HttpClient,
@@ -130,6 +140,8 @@ mod tests {
130140
proxy_success().await;
131141
proxy_failure().await;
132142
does_not_proxy_engine_method().await;
143+
does_not_proxy_eth_send_raw_transaction_method().await;
144+
health_check().await;
133145
}
134146

135147
async fn proxy_success() {
@@ -154,6 +166,37 @@ mod tests {
154166
assert_eq!(response.unwrap(), "engine response");
155167
}
156168

169+
async fn does_not_proxy_eth_send_raw_transaction_method() {
170+
let response = send_request("eth_sendRawTransaction").await;
171+
assert!(response.is_ok());
172+
assert_eq!(response.unwrap(), "raw transaction response");
173+
}
174+
175+
async fn health_check() {
176+
let proxy_server = spawn_proxy_server().await;
177+
// Create a new HTTP client
178+
let client: Client<HttpConnector, HttpBody> =
179+
Client::builder(TokioExecutor::new()).build_http();
180+
181+
// Test the health check endpoint
182+
let health_check_url = format!("http://{ADDR}:{PORT}/healthz");
183+
let health_response = client.get(health_check_url.parse::<Uri>().unwrap()).await;
184+
assert!(health_response.is_ok());
185+
let b = health_response
186+
.unwrap()
187+
.into_body()
188+
.collect()
189+
.await
190+
.unwrap()
191+
.to_bytes();
192+
// Convert the collected bytes to a string
193+
let body_string = String::from_utf8(b.to_vec()).unwrap();
194+
assert_eq!(body_string, "OK");
195+
196+
proxy_server.stop().unwrap();
197+
proxy_server.stopped().await;
198+
}
199+
157200
async fn send_request(method: &str) -> Result<String, ClientError> {
158201
let server = spawn_server().await;
159202
let proxy_server = spawn_proxy_server().await;
@@ -208,6 +251,11 @@ mod tests {
208251
module
209252
.register_method("engine_method", |_, _, _| "engine response")
210253
.unwrap();
254+
module
255+
.register_method("eth_sendRawTransaction", |_, _, _| {
256+
"raw transaction response"
257+
})
258+
.unwrap();
211259
module
212260
.register_method("non_existent_method", |_, _, _| "no proxy response")
213261
.unwrap();

0 commit comments

Comments
 (0)