2023-05-31 07:26:11 +03:00
|
|
|
use crate::{errors::Web3ProxyError, jsonrpc::JsonRpcErrorData, rpcs::blockchain::ArcBlock};
|
2023-05-13 21:13:02 +03:00
|
|
|
use derive_more::From;
|
2023-05-31 02:32:34 +03:00
|
|
|
use ethers::{providers::ProviderError, types::U64};
|
|
|
|
use hashbrown::hash_map::DefaultHashBuilder;
|
2023-05-15 20:48:59 +03:00
|
|
|
use quick_cache_ttl::{CacheWithTTL, Weighter};
|
2023-05-13 21:13:02 +03:00
|
|
|
use serde_json::value::RawValue;
|
|
|
|
use std::{
|
|
|
|
borrow::Cow,
|
2023-05-31 02:32:34 +03:00
|
|
|
hash::{BuildHasher, Hash, Hasher},
|
2023-05-13 21:13:02 +03:00
|
|
|
num::NonZeroU32,
|
|
|
|
};
|
|
|
|
|
2023-05-31 02:32:34 +03:00
|
|
|
#[derive(Clone, Debug, Eq, From)]
|
|
|
|
pub struct JsonRpcQueryCacheKey {
|
|
|
|
hash: u64,
|
|
|
|
from_block_num: Option<U64>,
|
|
|
|
to_block_num: Option<U64>,
|
|
|
|
cache_errors: bool,
|
2023-05-13 21:13:02 +03:00
|
|
|
}
|
|
|
|
|
2023-05-31 02:32:34 +03:00
|
|
|
impl JsonRpcQueryCacheKey {
|
|
|
|
pub fn from_block_num(&self) -> Option<U64> {
|
|
|
|
self.from_block_num
|
|
|
|
}
|
|
|
|
pub fn to_block_num(&self) -> Option<U64> {
|
|
|
|
self.to_block_num
|
|
|
|
}
|
|
|
|
pub fn cache_errors(&self) -> bool {
|
|
|
|
self.cache_errors
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PartialEq for JsonRpcQueryCacheKey {
|
|
|
|
fn eq(&self, other: &Self) -> bool {
|
|
|
|
self.hash.eq(&other.hash)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Hash for JsonRpcQueryCacheKey {
|
2023-05-13 21:13:02 +03:00
|
|
|
fn hash<H: Hasher>(&self, state: &mut H) {
|
2023-05-31 02:32:34 +03:00
|
|
|
// TODO: i feel like this hashes twice. oh well
|
|
|
|
self.hash.hash(state);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl JsonRpcQueryCacheKey {
|
|
|
|
pub fn new(
|
|
|
|
from_block: Option<ArcBlock>,
|
|
|
|
to_block: Option<ArcBlock>,
|
|
|
|
method: &str,
|
|
|
|
params: &serde_json::Value,
|
|
|
|
cache_errors: bool,
|
|
|
|
) -> Self {
|
|
|
|
let from_block_num = from_block.as_ref().and_then(|x| x.number);
|
|
|
|
let to_block_num = to_block.as_ref().and_then(|x| x.number);
|
|
|
|
|
|
|
|
let mut hasher = DefaultHashBuilder::default().build_hasher();
|
|
|
|
|
|
|
|
from_block.as_ref().and_then(|x| x.hash).hash(&mut hasher);
|
|
|
|
to_block.as_ref().and_then(|x| x.hash).hash(&mut hasher);
|
2023-05-13 21:13:02 +03:00
|
|
|
|
2023-05-31 02:32:34 +03:00
|
|
|
method.hash(&mut hasher);
|
2023-05-13 21:13:02 +03:00
|
|
|
|
2023-05-31 02:32:34 +03:00
|
|
|
// TODO: make sure preserve_order feature is OFF
|
|
|
|
// TODO: is there a faster way to do this?
|
|
|
|
params.to_string().hash(&mut hasher);
|
|
|
|
|
|
|
|
cache_errors.hash(&mut hasher);
|
|
|
|
|
|
|
|
let hash = hasher.finish();
|
|
|
|
|
|
|
|
Self {
|
|
|
|
hash,
|
|
|
|
from_block_num,
|
|
|
|
to_block_num,
|
|
|
|
cache_errors,
|
|
|
|
}
|
2023-05-13 21:13:02 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-30 01:48:22 +03:00
|
|
|
pub type JsonRpcResponseCache =
|
2023-05-31 02:32:34 +03:00
|
|
|
CacheWithTTL<JsonRpcQueryCacheKey, JsonRpcResponseEnum<Box<RawValue>>, JsonRpcResponseWeigher>;
|
2023-05-13 21:13:02 +03:00
|
|
|
|
|
|
|
#[derive(Clone)]
|
2023-05-30 01:48:22 +03:00
|
|
|
pub struct JsonRpcResponseWeigher;
|
2023-05-13 21:13:02 +03:00
|
|
|
|
2023-05-31 02:32:34 +03:00
|
|
|
/// TODO: we might need one that holds RawValue and one that holds serde_json::Value
|
2023-05-30 01:48:22 +03:00
|
|
|
#[derive(Clone, Debug)]
|
2023-05-31 02:32:34 +03:00
|
|
|
pub enum JsonRpcResponseEnum<R> {
|
2023-05-13 21:13:02 +03:00
|
|
|
Result {
|
2023-05-31 02:32:34 +03:00
|
|
|
value: R,
|
2023-05-30 01:48:22 +03:00
|
|
|
num_bytes: NonZeroU32,
|
2023-05-13 21:13:02 +03:00
|
|
|
},
|
2023-05-31 02:32:34 +03:00
|
|
|
RpcError {
|
|
|
|
error_data: JsonRpcErrorData,
|
2023-05-30 01:48:22 +03:00
|
|
|
num_bytes: NonZeroU32,
|
2023-05-13 21:13:02 +03:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-05-31 02:32:34 +03:00
|
|
|
// TODO: impl for other inner result types?
|
|
|
|
impl<R> JsonRpcResponseEnum<R> {
|
2023-05-13 21:13:02 +03:00
|
|
|
pub fn num_bytes(&self) -> NonZeroU32 {
|
|
|
|
match self {
|
2023-05-31 02:32:34 +03:00
|
|
|
Self::Result { num_bytes, .. } => *num_bytes,
|
|
|
|
Self::RpcError { num_bytes, .. } => *num_bytes,
|
2023-05-13 21:13:02 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-31 02:32:34 +03:00
|
|
|
impl From<serde_json::Value> for JsonRpcResponseEnum<Box<RawValue>> {
|
2023-05-13 21:13:02 +03:00
|
|
|
fn from(value: serde_json::Value) -> Self {
|
|
|
|
let value = RawValue::from_string(value.to_string()).unwrap();
|
|
|
|
|
2023-05-30 01:48:22 +03:00
|
|
|
value.into()
|
2023-05-13 21:13:02 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-31 02:32:34 +03:00
|
|
|
impl From<Box<RawValue>> for JsonRpcResponseEnum<Box<RawValue>> {
|
2023-05-13 21:13:02 +03:00
|
|
|
fn from(value: Box<RawValue>) -> Self {
|
2023-05-30 01:48:22 +03:00
|
|
|
let num_bytes = value.get().len();
|
|
|
|
|
|
|
|
let num_bytes = NonZeroU32::try_from(num_bytes as u32).unwrap();
|
|
|
|
|
|
|
|
Self::Result { value, num_bytes }
|
2023-05-13 21:13:02 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-31 02:32:34 +03:00
|
|
|
impl<R> TryFrom<Web3ProxyError> for JsonRpcResponseEnum<R> {
|
|
|
|
type Error = Web3ProxyError;
|
|
|
|
|
|
|
|
fn try_from(value: Web3ProxyError) -> Result<Self, Self::Error> {
|
|
|
|
match value {
|
|
|
|
Web3ProxyError::EthersProvider(provider_err) => {
|
|
|
|
let err = JsonRpcErrorData::try_from(provider_err)?;
|
|
|
|
|
|
|
|
Ok(err.into())
|
|
|
|
}
|
|
|
|
err => Err(err),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TryFrom<Result<Box<RawValue>, Web3ProxyError>> for JsonRpcResponseEnum<Box<RawValue>> {
|
|
|
|
type Error = Web3ProxyError;
|
|
|
|
|
|
|
|
fn try_from(value: Result<Box<RawValue>, Web3ProxyError>) -> Result<Self, Self::Error> {
|
|
|
|
match value {
|
|
|
|
Ok(x) => Ok(x.into()),
|
|
|
|
Err(err) => {
|
|
|
|
let x: Self = err.try_into()?;
|
|
|
|
|
|
|
|
Ok(x)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<R> From<JsonRpcErrorData> for JsonRpcResponseEnum<R> {
|
2023-05-13 21:13:02 +03:00
|
|
|
fn from(value: JsonRpcErrorData) -> Self {
|
2023-05-30 01:48:22 +03:00
|
|
|
// TODO: wrap the error in a complete response?
|
|
|
|
let num_bytes = serde_json::to_string(&value).unwrap().len();
|
|
|
|
|
|
|
|
let num_bytes = NonZeroU32::try_from(num_bytes as u32).unwrap();
|
|
|
|
|
2023-05-31 02:32:34 +03:00
|
|
|
Self::RpcError {
|
|
|
|
error_data: value,
|
|
|
|
num_bytes,
|
|
|
|
}
|
2023-05-13 21:13:02 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TryFrom<ProviderError> for JsonRpcErrorData {
|
|
|
|
type Error = Web3ProxyError;
|
|
|
|
|
|
|
|
fn try_from(e: ProviderError) -> Result<Self, Self::Error> {
|
|
|
|
// TODO: move turning ClientError into json to a helper function?
|
|
|
|
let code;
|
|
|
|
let message: String;
|
|
|
|
let data;
|
|
|
|
|
|
|
|
match e {
|
|
|
|
ProviderError::JsonRpcClientError(err) => {
|
|
|
|
if let Some(err) = err.as_error_response() {
|
|
|
|
code = err.code;
|
|
|
|
message = err.message.clone();
|
|
|
|
data = err.data.clone();
|
|
|
|
} else if let Some(err) = err.as_serde_error() {
|
|
|
|
// this is not an rpc error. keep it as an error
|
|
|
|
return Err(Web3ProxyError::BadResponse(format!(
|
|
|
|
"bad response: {}",
|
|
|
|
err
|
|
|
|
)));
|
|
|
|
} else {
|
|
|
|
return Err(anyhow::anyhow!("unexpected ethers error! {:?}", err).into());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
e => return Err(e.into()),
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(JsonRpcErrorData {
|
|
|
|
code,
|
|
|
|
message: Cow::Owned(message),
|
|
|
|
data,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-31 02:32:34 +03:00
|
|
|
impl<K, Q> Weighter<K, Q, JsonRpcResponseEnum<Box<RawValue>>> for JsonRpcResponseWeigher {
|
|
|
|
fn weight(&self, _key: &K, _qey: &Q, value: &JsonRpcResponseEnum<Box<RawValue>>) -> NonZeroU32 {
|
2023-05-13 21:13:02 +03:00
|
|
|
value.num_bytes()
|
|
|
|
}
|
|
|
|
}
|
2023-05-30 01:48:22 +03:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2023-05-31 02:32:34 +03:00
|
|
|
use super::{JsonRpcResponseEnum, JsonRpcResponseWeigher};
|
2023-05-30 01:48:22 +03:00
|
|
|
use quick_cache_ttl::CacheWithTTL;
|
2023-05-31 02:32:34 +03:00
|
|
|
use serde_json::value::RawValue;
|
2023-05-30 01:48:22 +03:00
|
|
|
use std::{num::NonZeroU32, time::Duration};
|
|
|
|
|
|
|
|
#[tokio::test(start_paused = true)]
|
|
|
|
async fn test_json_rpc_query_weigher() {
|
|
|
|
let max_item_weight = 200;
|
|
|
|
let weight_capacity = 1_000;
|
|
|
|
|
2023-05-31 02:32:34 +03:00
|
|
|
let test_cache: CacheWithTTL<
|
|
|
|
u32,
|
|
|
|
JsonRpcResponseEnum<Box<RawValue>>,
|
|
|
|
JsonRpcResponseWeigher,
|
|
|
|
> = CacheWithTTL::new_with_weights(
|
|
|
|
"test",
|
|
|
|
5,
|
|
|
|
max_item_weight.try_into().unwrap(),
|
|
|
|
weight_capacity,
|
|
|
|
JsonRpcResponseWeigher,
|
|
|
|
Duration::from_secs(2),
|
|
|
|
)
|
|
|
|
.await;
|
|
|
|
|
|
|
|
let small_data = JsonRpcResponseEnum::Result {
|
2023-05-30 01:48:22 +03:00
|
|
|
value: Default::default(),
|
|
|
|
num_bytes: NonZeroU32::try_from(max_item_weight / 2).unwrap(),
|
|
|
|
};
|
|
|
|
|
2023-05-31 02:32:34 +03:00
|
|
|
let max_sized_data = JsonRpcResponseEnum::Result {
|
2023-05-30 01:48:22 +03:00
|
|
|
value: Default::default(),
|
|
|
|
num_bytes: NonZeroU32::try_from(max_item_weight).unwrap(),
|
|
|
|
};
|
|
|
|
|
2023-05-31 02:32:34 +03:00
|
|
|
let oversized_data = JsonRpcResponseEnum::Result {
|
2023-05-30 01:48:22 +03:00
|
|
|
value: Default::default(),
|
|
|
|
num_bytes: NonZeroU32::try_from(max_item_weight * 2).unwrap(),
|
|
|
|
};
|
|
|
|
|
2023-05-31 02:32:34 +03:00
|
|
|
test_cache.try_insert(0, small_data).unwrap();
|
2023-05-30 01:48:22 +03:00
|
|
|
|
2023-05-31 02:32:34 +03:00
|
|
|
test_cache.get(&0).unwrap();
|
2023-05-30 01:48:22 +03:00
|
|
|
|
2023-05-31 02:32:34 +03:00
|
|
|
test_cache.try_insert(1, max_sized_data).unwrap();
|
2023-05-30 01:48:22 +03:00
|
|
|
|
2023-05-31 02:32:34 +03:00
|
|
|
test_cache.get(&0).unwrap();
|
|
|
|
test_cache.get(&1).unwrap();
|
2023-05-30 01:48:22 +03:00
|
|
|
|
2023-05-31 02:32:34 +03:00
|
|
|
test_cache.try_insert(2, oversized_data).unwrap_err();
|
2023-05-30 01:48:22 +03:00
|
|
|
|
2023-05-31 02:32:34 +03:00
|
|
|
test_cache.get(&0).unwrap();
|
|
|
|
test_cache.get(&1).unwrap();
|
|
|
|
assert!(test_cache.get(&2).is_none());
|
2023-05-30 01:48:22 +03:00
|
|
|
}
|
|
|
|
}
|