Simplified the sequencer forwarding RPC calls (#14386)

Co-authored-by: Matthias Seitz <matthias.seitz@outlook.de>
This commit is contained in:
Poulav Bhowmick
2025-02-13 17:46:07 +05:30
committed by GitHub
parent 84a375698d
commit 9dd90b5993

View File

@ -8,7 +8,7 @@ use std::sync::{
use alloy_primitives::hex; use alloy_primitives::hex;
use alloy_rpc_types_eth::erc4337::TransactionConditional; use alloy_rpc_types_eth::erc4337::TransactionConditional;
use reqwest::Client; use reqwest::Client;
use serde_json::json; use serde_json::{json, Value};
use tracing::warn; use tracing::warn;
use crate::SequencerClientError; use crate::SequencerClientError;
@ -51,36 +51,49 @@ impl SequencerClient {
self.inner.id.fetch_add(1, atomic::Ordering::SeqCst) self.inner.id.fetch_add(1, atomic::Ordering::SeqCst)
} }
/// Forwards a transaction to the sequencer endpoint. /// Helper function to get body of the request with the given params array.
pub async fn forward_raw_transaction(&self, tx: &[u8]) -> Result<(), SequencerClientError> { fn request_body(&self, method: &str, params: Value) -> serde_json::Result<String> {
let body = serde_json::to_string(&json!({ let request = json!({
"jsonrpc": "2.0", "jsonrpc": "2.0",
"method": "eth_sendRawTransaction", "method": method,
"params": [format!("0x{}", hex::encode(tx))], "params": params,
"id": self.next_request_id() "id": self.next_request_id()
})) });
.map_err(|_| {
warn!(
target = "rpc::eth",
"Failed to serialize transaction for forwarding to sequencer"
);
SequencerClientError::InvalidSequencerTransaction
})?;
serde_json::to_string(&request)
}
/// Sends a POST request to the sequencer endpoint.
async fn post_request(&self, body: String) -> Result<(), reqwest::Error> {
self.http_client() self.http_client()
.post(self.endpoint()) .post(self.endpoint())
.header(reqwest::header::CONTENT_TYPE, "application/json") .header(reqwest::header::CONTENT_TYPE, "application/json")
.body(body) .body(body)
.send() .send()
.await .await?;
.inspect_err(|err| { Ok(())
}
/// Forwards a transaction to the sequencer endpoint.
pub async fn forward_raw_transaction(&self, tx: &[u8]) -> Result<(), SequencerClientError> {
let body = self
.request_body("eth_sendRawTransaction", json!([format!("0x{}", hex::encode(tx))]))
.map_err(|_| {
warn!( warn!(
target = "rpc::eth", target: "rpc::eth",
%err, "Failed to serialize transaction for forwarding to sequencer"
"Failed to forward transaction to sequencer",
); );
SequencerClientError::InvalidSequencerTransaction
})?; })?;
self.post_request(body).await.inspect_err(|err| {
warn!(
target: "rpc::eth",
%err,
"Failed to forward transaction to sequencer",
);
})?;
Ok(()) Ok(())
} }
@ -90,34 +103,23 @@ impl SequencerClient {
tx: &[u8], tx: &[u8],
condition: TransactionConditional, condition: TransactionConditional,
) -> Result<(), SequencerClientError> { ) -> Result<(), SequencerClientError> {
let body = serde_json::to_string(&json!({ let params = json!([format!("0x{}", hex::encode(tx)), condition]);
"jsonrpc": "2.0", let body =
"method": "eth_sendRawTransactionConditional", self.request_body("eth_sendRawTransactionConditional", params).map_err(|_| {
"params": [format!("0x{}", hex::encode(tx)), condition],
"id": self.next_request_id()
}))
.map_err(|_| {
warn!(
target = "rpc::eth",
"Failed to serialize transaction conditional for forwarding to sequencer"
);
SequencerClientError::InvalidSequencerTransaction
})?;
self.http_client()
.post(self.endpoint())
.header(reqwest::header::CONTENT_TYPE, "application/json")
.body(body)
.send()
.await
.inspect_err(|err| {
warn!( warn!(
target = "rpc::eth", target: "rpc::eth",
%err, "Failed to serialize transaction for forwarding to sequencer"
"Failed to forward transaction conditional to sequencer",
); );
SequencerClientError::InvalidSequencerTransaction
})?; })?;
self.post_request(body).await.inspect_err(|err| {
warn!(
target: "rpc::eth",
%err,
"Failed to forward transaction conditional for sequencer",
);
})?;
Ok(()) Ok(())
} }
} }
@ -131,3 +133,32 @@ struct SequencerClientInner {
/// Keeps track of unique request ids /// Keeps track of unique request ids
id: AtomicUsize, id: AtomicUsize,
} }
#[cfg(test)]
mod tests {
use super::*;
use serde_json::json;
#[test]
fn test_body_str() {
let client = SequencerClient::new("http://localhost:8545");
let params = json!(["0x1234", {"block_number":10}]);
let body = client.request_body("eth_getBlockByNumber", params).unwrap();
assert_eq!(
body,
r#"{"id":0,"jsonrpc":"2.0","method":"eth_getBlockByNumber","params":["0x1234",{"block_number":10}]}"#
);
let condition = TransactionConditional::default();
let params = json!([format!("0x{}", hex::encode("abcd")), condition]);
let body = client.request_body("eth_sendRawTransactionConditional", params).unwrap();
assert_eq!(
body,
r#"{"id":1,"jsonrpc":"2.0","method":"eth_sendRawTransactionConditional","params":["0x61626364",{"knownAccounts":{}}]}"#
);
}
}