-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from flashbots/proxy
Add proxy layer to forward calls to op-geth
- Loading branch information
Showing
7 changed files
with
174 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
JWT_TOKEN=688f5d737bad920bdfb2fc2f488d6b6209eebda1dae949a8de91398d932c517a | ||
L2_URL=http://localhost:8551 | ||
BUILDER_URL=http://localhost:8552 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
name: Linting | ||
|
||
on: | ||
push: | ||
branches: | ||
- main | ||
pull_request: | ||
|
||
jobs: | ||
build: | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- name: Checkout code | ||
uses: actions/checkout@v2 | ||
|
||
- name: Set up Rust | ||
uses: actions-rs/toolchain@v1 | ||
with: | ||
toolchain: stable | ||
override: true | ||
components: rustfmt | ||
|
||
- name: Build | ||
run: cargo build --verbose | ||
|
||
- name: Lint | ||
run: cargo clippy -- -D warnings | ||
|
||
- name: Format code | ||
run: cargo fmt -- --check |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
use hyper::Response; | ||
use hyper_util::client::legacy::connect::HttpConnector; | ||
use hyper_util::client::legacy::Client; | ||
use hyper_util::rt::TokioExecutor; | ||
use jsonrpsee::core::BoxError; | ||
use jsonrpsee::http_client::{HttpBody, HttpRequest, HttpResponse}; | ||
use std::task::{Context, Poll}; | ||
use std::{future::Future, pin::Pin}; | ||
use tower::{Layer, Service}; | ||
use tracing::error; | ||
|
||
#[derive(Debug, Clone)] | ||
pub struct ProxyLayer { | ||
target_url: String, | ||
} | ||
|
||
impl ProxyLayer { | ||
pub fn new(target_url: String) -> Self { | ||
ProxyLayer { target_url } | ||
} | ||
} | ||
|
||
impl<S> Layer<S> for ProxyLayer { | ||
type Service = ProxyService<S>; | ||
|
||
fn layer(&self, inner: S) -> Self::Service { | ||
ProxyService { | ||
inner, | ||
client: Client::builder(TokioExecutor::new()).build_http(), | ||
target_url: self.target_url.clone(), | ||
} | ||
} | ||
} | ||
|
||
#[derive(Debug, Clone)] | ||
pub struct ProxyService<S> { | ||
inner: S, | ||
client: Client<HttpConnector, HttpBody>, | ||
target_url: String, | ||
} | ||
|
||
impl<S> Service<HttpRequest<HttpBody>> for ProxyService<S> | ||
where | ||
S: Service<HttpRequest<HttpBody>, Response = Response<HttpBody>>, | ||
S::Response: 'static, | ||
S::Error: Into<BoxError> + 'static, | ||
S::Future: Send + 'static, | ||
{ | ||
type Response = HttpResponse<hyper::body::Incoming>; | ||
type Error = BoxError; | ||
type Future = | ||
Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>> + Send + 'static>>; | ||
|
||
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> { | ||
self.inner.poll_ready(cx).map_err(Into::into) | ||
} | ||
|
||
fn call(&mut self, mut req: HttpRequest<HttpBody>) -> Self::Future { | ||
let target_url = self.target_url.clone(); | ||
let client = self.client.clone(); | ||
let fut = async move { | ||
*req.uri_mut() = target_url.parse().unwrap(); | ||
client.request(req).await.map_err(|e| { | ||
error!("Error proxying request: {}", e); | ||
Box::new(e) as Box<dyn std::error::Error + Send + Sync> | ||
}) | ||
}; | ||
Box::pin(fut) | ||
} | ||
} |
Oops, something went wrong.