2020-11-27 12:57:44 +00:00
|
|
|
use crate::{JsonRpcClient, ProviderError};
|
|
|
|
|
|
|
|
use async_trait::async_trait;
|
2020-11-30 09:33:06 +00:00
|
|
|
use serde::{de::DeserializeOwned, Serialize};
|
2020-11-27 12:57:44 +00:00
|
|
|
use serde_json::Value;
|
|
|
|
use std::{
|
|
|
|
borrow::Borrow,
|
|
|
|
collections::VecDeque,
|
|
|
|
sync::{Arc, Mutex},
|
|
|
|
};
|
|
|
|
use thiserror::Error;
|
|
|
|
|
2022-08-22 23:32:47 +00:00
|
|
|
/// Helper type that can be used to pass through the `params` value.
|
|
|
|
/// This is necessary because the wrapper provider is supposed to skip the `params` if it's of
|
|
|
|
/// size 0, see `crate::transports::common::Request`
|
|
|
|
#[derive(Debug)]
|
|
|
|
enum MockParams {
|
|
|
|
Value(Value),
|
2022-08-22 23:33:00 +00:00
|
|
|
Zst,
|
2022-08-22 23:32:47 +00:00
|
|
|
}
|
|
|
|
|
2020-11-27 12:57:44 +00:00
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
/// Mock transport used in test environments.
|
|
|
|
pub struct MockProvider {
|
2022-08-22 23:32:47 +00:00
|
|
|
requests: Arc<Mutex<VecDeque<(String, MockParams)>>>,
|
2020-11-27 12:57:44 +00:00
|
|
|
responses: Arc<Mutex<VecDeque<Value>>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for MockProvider {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self::new()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-23 09:56:44 +00:00
|
|
|
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
|
|
|
|
#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
|
2020-11-27 12:57:44 +00:00
|
|
|
impl JsonRpcClient for MockProvider {
|
|
|
|
type Error = MockError;
|
|
|
|
|
2022-08-22 23:32:47 +00:00
|
|
|
/// Pushes the `(method, params)` to the back of the `requests` queue,
|
2020-11-27 12:57:44 +00:00
|
|
|
/// pops the responses from the back of the `responses` queue
|
2020-11-30 09:33:06 +00:00
|
|
|
async fn request<T: Serialize + Send + Sync, R: DeserializeOwned>(
|
2020-11-27 12:57:44 +00:00
|
|
|
&self,
|
|
|
|
method: &str,
|
2022-08-22 23:32:47 +00:00
|
|
|
params: T,
|
2020-11-27 12:57:44 +00:00
|
|
|
) -> Result<R, MockError> {
|
2022-08-22 23:32:47 +00:00
|
|
|
let params = if std::mem::size_of::<T>() == 0 {
|
|
|
|
MockParams::Zst
|
|
|
|
} else {
|
|
|
|
MockParams::Value(serde_json::to_value(params)?)
|
|
|
|
};
|
|
|
|
self.requests.lock().unwrap().push_back((method.to_owned(), params));
|
2020-11-27 12:57:44 +00:00
|
|
|
let mut data = self.responses.lock().unwrap();
|
|
|
|
let element = data.pop_back().ok_or(MockError::EmptyResponses)?;
|
|
|
|
let res: R = serde_json::from_value(element)?;
|
|
|
|
|
|
|
|
Ok(res)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl MockProvider {
|
|
|
|
/// Checks that the provided request was submitted by the client
|
|
|
|
pub fn assert_request<T: Serialize + Send + Sync>(
|
|
|
|
&self,
|
|
|
|
method: &str,
|
|
|
|
data: T,
|
|
|
|
) -> Result<(), MockError> {
|
2021-10-29 12:29:35 +00:00
|
|
|
let (m, inp) = self.requests.lock().unwrap().pop_front().ok_or(MockError::EmptyRequests)?;
|
2020-11-27 12:57:44 +00:00
|
|
|
assert_eq!(m, method);
|
2022-08-22 23:32:47 +00:00
|
|
|
assert!(!matches!(inp, MockParams::Value(serde_json::Value::Null)));
|
|
|
|
if std::mem::size_of::<T>() == 0 {
|
|
|
|
assert!(matches!(inp, MockParams::Zst));
|
|
|
|
} else if let MockParams::Value(inp) = inp {
|
|
|
|
assert_eq!(serde_json::to_value(data).expect("could not serialize data"), inp);
|
|
|
|
} else {
|
|
|
|
unreachable!("Zero sized types must be denoted with MockParams::Zst")
|
|
|
|
}
|
|
|
|
|
2020-11-27 12:57:44 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Instantiates a mock transport
|
|
|
|
pub fn new() -> Self {
|
|
|
|
Self {
|
|
|
|
requests: Arc::new(Mutex::new(VecDeque::new())),
|
|
|
|
responses: Arc::new(Mutex::new(VecDeque::new())),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Pushes the data to the responses
|
|
|
|
pub fn push<T: Serialize + Send + Sync, K: Borrow<T>>(&self, data: K) -> Result<(), MockError> {
|
|
|
|
let value = serde_json::to_value(data.borrow())?;
|
|
|
|
self.responses.lock().unwrap().push_back(value);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Error, Debug)]
|
|
|
|
/// Errors for the `MockProvider`
|
|
|
|
pub enum MockError {
|
|
|
|
#[error(transparent)]
|
|
|
|
SerdeJson(#[from] serde_json::Error),
|
|
|
|
|
|
|
|
#[error("empty responses array, please push some requests")]
|
|
|
|
EmptyRequests,
|
|
|
|
|
|
|
|
#[error("empty responses array, please push some responses")]
|
|
|
|
EmptyResponses,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<MockError> for ProviderError {
|
|
|
|
fn from(src: MockError) -> Self {
|
|
|
|
ProviderError::JsonRpcClientError(Box::new(src))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
2021-08-23 09:56:44 +00:00
|
|
|
#[cfg(not(target_arch = "wasm32"))]
|
2020-11-27 12:57:44 +00:00
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
use crate::Middleware;
|
|
|
|
use ethers_core::types::U64;
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn pushes_request_and_response() {
|
|
|
|
let mock = MockProvider::new();
|
|
|
|
mock.push(U64::from(12)).unwrap();
|
|
|
|
let block: U64 = mock.request("eth_blockNumber", ()).await.unwrap();
|
|
|
|
mock.assert_request("eth_blockNumber", ()).unwrap();
|
|
|
|
assert_eq!(block.as_u64(), 12);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn empty_responses() {
|
|
|
|
let mock = MockProvider::new();
|
|
|
|
// tries to get a response without pushing a response
|
2021-10-29 12:29:35 +00:00
|
|
|
let err = mock.request::<_, ()>("eth_blockNumber", ()).await.unwrap_err();
|
2020-11-27 12:57:44 +00:00
|
|
|
match err {
|
|
|
|
MockError::EmptyResponses => {}
|
|
|
|
_ => panic!("expected empty responses"),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn empty_requests() {
|
|
|
|
let mock = MockProvider::new();
|
|
|
|
// tries to assert a request without making one
|
|
|
|
let err = mock.assert_request("eth_blockNumber", ()).unwrap_err();
|
|
|
|
match err {
|
|
|
|
MockError::EmptyRequests => {}
|
|
|
|
_ => panic!("expected empty request"),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn composes_with_provider() {
|
|
|
|
let (provider, mock) = crate::Provider::mocked();
|
|
|
|
|
|
|
|
mock.push(U64::from(12)).unwrap();
|
|
|
|
let block = provider.get_block_number().await.unwrap();
|
|
|
|
assert_eq!(block.as_u64(), 12);
|
|
|
|
}
|
|
|
|
}
|