RawValues
This commit is contained in:
parent
1669c15a32
commit
e5e7d37f20
@ -18,7 +18,7 @@ tokio = { version = "1.18.0", features = ["full"] }
|
|||||||
parking_lot = "0.12.0"
|
parking_lot = "0.12.0"
|
||||||
regex = "1.5.5"
|
regex = "1.5.5"
|
||||||
reqwest = { version = "0.11.10", features = ["json", "rustls"] }
|
reqwest = { version = "0.11.10", features = ["json", "rustls"] }
|
||||||
serde = "1.0.136"
|
serde = { version = "1.0.136", features = [] }
|
||||||
serde_json = { version = "1.0.79", default-features = false, features = ["alloc"] }
|
serde_json = { version = "1.0.79", default-features = false, features = ["alloc"] }
|
||||||
tracing = "0.1.34"
|
tracing = "0.1.34"
|
||||||
tracing-subscriber = "0.3.11"
|
tracing-subscriber = "0.3.11"
|
||||||
|
42
src/main.rs
42
src/main.rs
@ -6,7 +6,9 @@ use futures::future;
|
|||||||
use futures::stream::FuturesUnordered;
|
use futures::stream::FuturesUnordered;
|
||||||
use futures::StreamExt;
|
use futures::StreamExt;
|
||||||
use governor::clock::{Clock, QuantaClock};
|
use governor::clock::{Clock, QuantaClock};
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
use serde_json::json;
|
use serde_json::json;
|
||||||
|
use serde_json::value::RawValue;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
@ -27,6 +29,21 @@ static APP_USER_AGENT: &str = concat!(
|
|||||||
env!("CARGO_PKG_VERSION"),
|
env!("CARGO_PKG_VERSION"),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
#[derive(Clone, Deserialize)]
|
||||||
|
struct JsonRpcRequest {
|
||||||
|
jsonrpc: Box<RawValue>,
|
||||||
|
id: Box<RawValue>,
|
||||||
|
method: String,
|
||||||
|
params: Box<RawValue>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Serialize)]
|
||||||
|
struct JsonRpcForwardedResponse {
|
||||||
|
jsonrpc: Box<RawValue>,
|
||||||
|
id: Box<RawValue>,
|
||||||
|
result: Box<RawValue>,
|
||||||
|
}
|
||||||
|
|
||||||
/// The application
|
/// The application
|
||||||
// TODO: this debug impl is way too verbose. make something smaller
|
// TODO: this debug impl is way too verbose. make something smaller
|
||||||
struct Web3ProxyApp {
|
struct Web3ProxyApp {
|
||||||
@ -175,13 +192,9 @@ impl Web3ProxyApp {
|
|||||||
/// TODO: dry this up
|
/// TODO: dry this up
|
||||||
async fn proxy_web3_rpc(
|
async fn proxy_web3_rpc(
|
||||||
self: Arc<Web3ProxyApp>,
|
self: Arc<Web3ProxyApp>,
|
||||||
json_body: serde_json::Value,
|
json_body: JsonRpcRequest,
|
||||||
) -> anyhow::Result<impl warp::Reply> {
|
) -> anyhow::Result<impl warp::Reply> {
|
||||||
let eth_send_raw_transaction =
|
if self.private_rpcs.is_some() && json_body.method == "eth_sendRawTransaction" {
|
||||||
serde_json::Value::String("eth_sendRawTransaction".to_string());
|
|
||||||
|
|
||||||
if self.private_rpcs.is_some() && json_body.get("method") == Some(ð_send_raw_transaction)
|
|
||||||
{
|
|
||||||
let private_rpcs = self.private_rpcs.clone().unwrap();
|
let private_rpcs = self.private_rpcs.clone().unwrap();
|
||||||
|
|
||||||
// there are private rpcs configured and the request is eth_sendSignedTransaction. send to all private rpcs
|
// there are private rpcs configured and the request is eth_sendSignedTransaction. send to all private rpcs
|
||||||
@ -199,7 +212,7 @@ impl Web3ProxyApp {
|
|||||||
let connections = private_rpcs.clone_connections();
|
let connections = private_rpcs.clone_connections();
|
||||||
|
|
||||||
// check incoming_id before sending any requests
|
// check incoming_id before sending any requests
|
||||||
let incoming_id = json_body.as_object().unwrap().get("id").unwrap();
|
let incoming_id = &*json_body.id;
|
||||||
|
|
||||||
tokio::spawn(async move {
|
tokio::spawn(async move {
|
||||||
clone
|
clone
|
||||||
@ -253,7 +266,7 @@ impl Web3ProxyApp {
|
|||||||
let mut earliest_not_until = None;
|
let mut earliest_not_until = None;
|
||||||
|
|
||||||
// check incoming_id before sending any requests
|
// check incoming_id before sending any requests
|
||||||
let incoming_id = json_body.as_object().unwrap().get("id").unwrap();
|
let incoming_id = &*json_body.id;
|
||||||
|
|
||||||
for balanced_rpcs in self.balanced_rpc_tiers.iter() {
|
for balanced_rpcs in self.balanced_rpc_tiers.iter() {
|
||||||
// TODO: what allowed lag?
|
// TODO: what allowed lag?
|
||||||
@ -358,20 +371,13 @@ impl Web3ProxyApp {
|
|||||||
&self,
|
&self,
|
||||||
rpc_servers: Vec<String>,
|
rpc_servers: Vec<String>,
|
||||||
connections: Arc<Web3ConnectionMap>,
|
connections: Arc<Web3ConnectionMap>,
|
||||||
json_request_body: serde_json::Value,
|
json_request_body: JsonRpcRequest,
|
||||||
// TODO: better type for this
|
// TODO: better type for this
|
||||||
tx: mpsc::UnboundedSender<anyhow::Result<serde_json::Value>>,
|
tx: mpsc::UnboundedSender<anyhow::Result<serde_json::Value>>,
|
||||||
) -> anyhow::Result<()> {
|
) -> anyhow::Result<()> {
|
||||||
// {"jsonrpc":"2.0","method":"eth_syncing","params":[],"id":1}
|
// {"jsonrpc":"2.0","method":"eth_syncing","params":[],"id":1}
|
||||||
let method = json_request_body
|
let method = json_request_body.method.clone();
|
||||||
.get("method")
|
let params = json_request_body.params;
|
||||||
.and_then(|x| x.as_str())
|
|
||||||
.ok_or_else(|| anyhow::anyhow!("bad id"))?
|
|
||||||
.to_string();
|
|
||||||
let params = json_request_body
|
|
||||||
.get("params")
|
|
||||||
.ok_or_else(|| anyhow::anyhow!("no params"))?
|
|
||||||
.to_owned();
|
|
||||||
|
|
||||||
if rpc_servers.len() == 1 {
|
if rpc_servers.len() == 1 {
|
||||||
let rpc = rpc_servers.first().unwrap();
|
let rpc = rpc_servers.first().unwrap();
|
||||||
|
@ -39,7 +39,7 @@ impl Web3Provider {
|
|||||||
pub async fn request(
|
pub async fn request(
|
||||||
&self,
|
&self,
|
||||||
method: &str,
|
method: &str,
|
||||||
params: serde_json::Value,
|
params: Box<serde_json::value::RawValue>,
|
||||||
) -> Result<serde_json::Value, ethers::prelude::ProviderError> {
|
) -> Result<serde_json::Value, ethers::prelude::ProviderError> {
|
||||||
match self {
|
match self {
|
||||||
Self::Http(provider) => provider.request(method, params).await,
|
Self::Http(provider) => provider.request(method, params).await,
|
||||||
|
Loading…
Reference in New Issue
Block a user