Skip to content

Commit 79fe178

Browse files
author
Naohiro Yoshida
committed
refactor
Signed-off-by: Naohiro Yoshida <naohiro.yoshida@datachain.jp>
1 parent bc87d30 commit 79fe178

File tree

4 files changed

+99
-91
lines changed

4 files changed

+99
-91
lines changed

server/src/host/single/handler.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use crate::host::single::config::Config;
44
use crate::host::single::local_kv::LocalKeyValueStore;
55
use crate::host::single::trace::{encode_to_bytes, TracingKeyValueStore};
6-
use crate::transport::Http;
6+
use crate::transport::Transport;
77
use alloy_primitives::B256;
88
use alloy_provider::RootProvider;
99
use alloy_rpc_client::RpcClient;
@@ -21,7 +21,6 @@ use kona_proof::boot::L2_ROLLUP_CONFIG_KEY;
2121
use kona_proof::HintType;
2222
use kona_providers_alloy::{OnlineBeaconClient, OnlineBlobProvider};
2323
use op_alloy_network::{Network, Optimism};
24-
use reqwest::Client;
2524
use std::sync::Arc;
2625
use tokio::sync::RwLock;
2726

@@ -67,7 +66,7 @@ impl DerivationRequest {
6766

6867
fn http_provider<N: Network>(url: &str) -> RootProvider<N> {
6968
let url = url.parse().unwrap();
70-
let http = Http::<Client>::new(url);
69+
let http = Transport::new(url);
7170
RootProvider::new(RpcClient::new(http, true))
7271
}
7372

server/src/transport/hash.rs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
use alloy_json_rpc::{RequestPacket, ResponsePacket};
2+
use alloy_primitives::{keccak256, B256};
3+
use alloy_transport::{TransportError, TransportFut};
4+
use std::task;
5+
use tower::Service;
6+
7+
use crate::transport::Transport;
8+
9+
impl Service<RequestPacket> for Transport {
10+
type Response = ResponsePacket;
11+
type Error = TransportError;
12+
type Future = TransportFut<'static>;
13+
14+
fn poll_ready(&mut self, _cx: &mut task::Context<'_>) -> task::Poll<Result<(), Self::Error>> {
15+
task::Poll::Ready(Ok(()))
16+
}
17+
18+
fn call(&mut self, req: RequestPacket) -> Self::Future {
19+
let hash = match from_request_to_hash(&req) {
20+
Ok(hash) => hash,
21+
Err(err) => {
22+
return Box::pin(async move {
23+
Err(TransportError::deser_err(
24+
err,
25+
"invalid request for hashing",
26+
))
27+
});
28+
}
29+
};
30+
let mut headers = req.headers();
31+
headers.insert("X-Request-Hash", hash.to_string().parse().unwrap());
32+
33+
let this = self.clone();
34+
Box::pin(this.post(req, headers))
35+
}
36+
}
37+
38+
fn from_request_to_hash(req: &RequestPacket) -> Result<B256, serde_json::Error> {
39+
let body = match &req {
40+
RequestPacket::Single(r) => serde_json::to_vec(r)?,
41+
RequestPacket::Batch(_) => {
42+
let mut value = vec![];
43+
if let Some(batch) = req.as_batch() {
44+
for r in batch {
45+
value.extend_from_slice(&r.params_hash().0);
46+
}
47+
}
48+
value
49+
}
50+
};
51+
Ok(keccak256(&body))
52+
}

server/src/transport/mod.rs

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,51 @@
1-
pub mod reqwest_transport;
1+
pub mod hash;
22

3+
use alloy_json_rpc::{RequestPacket, ResponsePacket};
4+
use alloy_transport::{TransportError, TransportErrorKind, TransportResult};
5+
use reqwest::header::HeaderMap;
6+
use reqwest::Client;
7+
use tracing::{debug, trace};
38
use url::Url;
49

510
#[derive(Clone, Debug)]
6-
pub struct Http<T> {
7-
client: T,
11+
pub struct Transport {
12+
client: Client,
813
url: Url,
914
}
15+
16+
impl Transport {
17+
pub fn new(url: Url) -> Self {
18+
Self {
19+
client: Default::default(),
20+
url,
21+
}
22+
}
23+
24+
async fn post(self, req: RequestPacket, headers: HeaderMap) -> TransportResult<ResponsePacket> {
25+
let resp = self
26+
.client
27+
.post(self.url)
28+
.json(&req)
29+
.headers(headers)
30+
.send()
31+
.await
32+
.map_err(TransportErrorKind::custom)?;
33+
let status = resp.status();
34+
35+
debug!(%status, "received response from server");
36+
37+
let body = resp.bytes().await.map_err(TransportErrorKind::custom)?;
38+
39+
trace!(body = %String::from_utf8_lossy(&body), "response body");
40+
41+
if !status.is_success() {
42+
return Err(TransportErrorKind::http_error(
43+
status.as_u16(),
44+
String::from_utf8_lossy(&body).into_owned(),
45+
));
46+
}
47+
48+
serde_json::from_slice(&body)
49+
.map_err(|err| TransportError::deser_err(err, String::from_utf8_lossy(&body)))
50+
}
51+
}

server/src/transport/reqwest_transport.rs

Lines changed: 0 additions & 85 deletions
This file was deleted.

0 commit comments

Comments
 (0)