fix hex in some responses

This commit is contained in:
Bryan Stitt 2022-07-09 02:33:53 +00:00
parent 58fa7af105
commit 91b9b65db5
4 changed files with 88 additions and 24 deletions

58
Cargo.lock generated

@ -2355,6 +2355,40 @@ dependencies = [
"winapi 0.3.9",
]
[[package]]
name = "num"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "43db66d1170d347f9a065114077f7dccb00c1b9478c89384490a3425279a4606"
dependencies = [
"num-bigint",
"num-complex",
"num-integer",
"num-iter",
"num-rational",
"num-traits",
]
[[package]]
name = "num-bigint"
version = "0.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f93ab6289c7b344a8a9f60f88d80aa20032336fe78da341afc91c8a2341fc75f"
dependencies = [
"autocfg",
"num-integer",
"num-traits",
]
[[package]]
name = "num-complex"
version = "0.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7ae39348c8bc5fbd7f40c727a9925f03517afd2ab27d46702108b6a7e5414c19"
dependencies = [
"num-traits",
]
[[package]]
name = "num-integer"
version = "0.1.45"
@ -2365,6 +2399,29 @@ dependencies = [
"num-traits",
]
[[package]]
name = "num-iter"
version = "0.1.43"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7d03e6c028c5dc5cac6e2dec0efda81fc887605bb3d884578bb6d6bf7514e252"
dependencies = [
"autocfg",
"num-integer",
"num-traits",
]
[[package]]
name = "num-rational"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0638a1c9d0a3c0914158145bc76cff373a75a627e6ecbfb71cbe6f453a5a19b0"
dependencies = [
"autocfg",
"num-bigint",
"num-integer",
"num-traits",
]
[[package]]
name = "num-traits"
version = "0.2.15"
@ -4146,6 +4203,7 @@ dependencies = [
"hashbrown 0.12.1",
"linkedhashmap",
"notify",
"num",
"parking_lot 0.12.1",
"proctitle",
"redis-cell-client",

@ -25,6 +25,7 @@ futures = { version = "0.3.21", features = ["thread-pool"] }
hashbrown = "0.12.1"
linkedhashmap = { path = "../linkedhashmap", features = ["inline-more"] }
notify = "4.0.17"
num = "0.4"
redis-cell-client = { path = "../redis-cell-client" }
# TODO: parking_lot has an "arc_lock" feature that we might want to use
parking_lot = "0.12.1"

@ -693,10 +693,7 @@ impl Web3ProxyApp {
return Err(anyhow::anyhow!("no servers synced"));
}
// TODO: this seems pretty common. make a helper?
let partial_response = format!("{:x}", head_block_number);
let response = JsonRpcForwardedResponse::from_string(partial_response, request.id);
let response = JsonRpcForwardedResponse::from_number(head_block_number, request.id);
Ok(response)
}
@ -758,12 +755,10 @@ impl Web3ProxyApp {
Ok(response)
}
"net_peerCount" => {
let partial_response = serde_json::Value::String(format!(
"{:x}",
self.balanced_rpcs.num_synced_rpcs()
));
let response = JsonRpcForwardedResponse::from_value(partial_response, request.id);
let response = JsonRpcForwardedResponse::from_number(
self.balanced_rpcs.num_synced_rpcs(),
request.id,
);
Ok(response)
}

@ -180,23 +180,14 @@ impl JsonRpcForwardedResponse {
}
}
pub fn try_from_response_result(
result: Result<Box<RawValue>, ProviderError>,
pub fn from_number<T: num::Integer + std::fmt::LowerHex>(
partial_response: T,
id: Box<RawValue>,
) -> anyhow::Result<Self> {
match result {
Ok(response) => Ok(Self::from_response(response, id)),
Err(e) => Self::from_ethers_error(e, id),
}
}
pub fn from_string(partial_response: String, id: Box<RawValue>) -> Self {
trace!("partial_response: {}", partial_response);
// TODO: anyhow result on this
) -> Self {
// TODO: proper escaping. this feels wrong. probably refactor to not need this at all
let partial_response = RawValue::from_string(format!(r#""{}""#, partial_response)).unwrap();
let partial_response = format!("0x{:x}", partial_response);
Self::from_response(partial_response, id)
Self::from_string(partial_response, id)
}
pub fn from_response(partial_response: Box<RawValue>, id: Box<RawValue>) -> Self {
@ -209,6 +200,15 @@ impl JsonRpcForwardedResponse {
}
}
pub fn from_string(partial_response: String, id: Box<RawValue>) -> Self {
trace!("partial_response: {}", partial_response);
// TODO: anyhow result on this
// TODO: proper escaping. this feels wrong. probably refactor to not need this at all
let partial_response = RawValue::from_string(format!(r#""{}""#, partial_response)).unwrap();
Self::from_response(partial_response, id)
}
pub fn from_value(partial_response: serde_json::Value, id: Box<RawValue>) -> Self {
let partial_response =
serde_json::to_string(&partial_response).expect("this should always work");
@ -282,6 +282,16 @@ impl JsonRpcForwardedResponse {
}),
})
}
pub fn try_from_response_result(
result: Result<Box<RawValue>, ProviderError>,
id: Box<RawValue>,
) -> anyhow::Result<Self> {
match result {
Ok(response) => Ok(Self::from_response(response, id)),
Err(e) => Self::from_ethers_error(e, id),
}
}
}
/// JSONRPC Responses can include one or many response objects.