Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: impl reqpool #447

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions reqpool/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
[package]
name = "raiko-reqpool"
version = "0.1.0"
authors = ["Taiko Labs"]
edition = "2021"

[dependencies]
raiko-lib = { workspace = true }
raiko-core = { workspace = true }
raiko-redis-derive = { workspace = true }
chrono = { workspace = true, features = ["serde"] }
serde = { workspace = true }
serde_json = { workspace = true }
serde_with = { workspace = true }
tracing = { workspace = true }
tokio = { workspace = true }
async-trait = { workspace = true }
redis = { workspace = true }
backoff = { workspace = true }
derive-getters = { workspace = true }
proc-macro2 = { workspace = true }
quote = { workspace = true }
syn = { workspace = true }
alloy-primitives = { workspace = true }

[dev-dependencies]
lazy_static = { workspace = true }
10 changes: 10 additions & 0 deletions reqpool/src/config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, Serialize, Deserialize)]
/// The configuration for the redis-backend request pool
pub struct RedisPoolConfig {
/// The URL of the Redis database, e.g. "redis://localhost:6379"
pub redis_url: String,
/// The TTL of the Redis database
pub redis_ttl: u64,
}
16 changes: 16 additions & 0 deletions reqpool/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
mod config;
mod macros;
#[cfg(any(test, feature = "enable-mock"))]
mod mock;
mod redis_pool;
mod request;
mod utils;

// Re-export
pub use config::RedisPoolConfig;
pub use redis_pool::Pool;
pub use request::{
AggregationRequestEntity, AggregationRequestKey, RequestEntity, RequestKey,
SingleProofRequestEntity, SingleProofRequestKey, Status, StatusWithContext,
};
pub use utils::proof_key_to_hack_request_key;
44 changes: 44 additions & 0 deletions reqpool/src/macros.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/// This macro implements the Display trait for a type by using serde_json's pretty printing.
/// If the type cannot be serialized to JSON, it falls back to using Debug formatting.
///
/// # Example
///
/// ```rust
/// use serde::{Serialize, Deserialize};
///
/// #[derive(Debug, Serialize, Deserialize)]
/// struct Person {
/// name: String,
/// age: u32
/// }
///
/// impl_display_using_json_pretty!(Person);
///
/// let person = Person {
/// name: "John".to_string(),
/// age: 30
/// };
///
/// // Will print:
/// // {
/// // "name": "John",
/// // "age": 30
/// // }
/// println!("{}", person);
/// ```
///
/// The type must implement serde's Serialize trait for JSON serialization to work.
/// If serialization fails, it will fall back to using the Debug implementation.
#[macro_export]
macro_rules! impl_display_using_json_pretty {
($type:ty) => {
impl std::fmt::Display for $type {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match serde_json::to_string_pretty(self) {
Ok(s) => write!(f, "{}", s),
Err(_) => write!(f, "{:?}", self),
}
}
}
};
}
145 changes: 145 additions & 0 deletions reqpool/src/mock.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
use lazy_static::lazy_static;
use redis::{RedisError, RedisResult};
use serde::Serialize;
use serde_json::{json, Value};
use std::{
collections::HashMap,
sync::{Arc, Mutex},
};

type SingleStorage = Arc<Mutex<HashMap<Value, Value>>>;
type GlobalStorage = Mutex<HashMap<String, SingleStorage>>;

lazy_static! {
// #{redis_url => single_storage}
//
// We use redis_url to distinguish different redis database for tests, to prevent
// data race problem when running multiple tests.
static ref GLOBAL_STORAGE: GlobalStorage = Mutex::new(HashMap::new());
}

pub struct MockRedisConnection {
storage: SingleStorage,
}

impl MockRedisConnection {
pub(crate) fn new(redis_url: String) -> Self {
let mut global = GLOBAL_STORAGE.lock().unwrap();
Self {
storage: global
.entry(redis_url)
.or_insert_with(|| Arc::new(Mutex::new(HashMap::new())))
.clone(),
}
}

pub fn set_ex<K: Serialize, V: Serialize>(
&mut self,
key: K,
val: V,
_ttl: u64,
) -> RedisResult<()> {
let mut lock = self.storage.lock().unwrap();
lock.insert(json!(key), json!(val));
Ok(())
}

pub fn get<K: Serialize, V: serde::de::DeserializeOwned>(&mut self, key: &K) -> RedisResult<V> {
let lock = self.storage.lock().unwrap();
match lock.get(&json!(key)) {
None => Err(RedisError::from((redis::ErrorKind::TypeError, "not found"))),
Some(v) => serde_json::from_value(v.clone()).map_err(|e| {
RedisError::from((
redis::ErrorKind::TypeError,
"deserialization error",
e.to_string(),
))
}),
}
}

pub fn del<K: Serialize>(&mut self, key: K) -> RedisResult<usize> {
let mut lock = self.storage.lock().unwrap();
if lock.remove(&json!(key)).is_none() {
Ok(0)
} else {
Ok(1)
}
}
}

#[cfg(test)]
mod tests {
use redis::RedisResult;

use crate::{Pool, RedisPoolConfig};

#[test]
fn test_mock_redis_pool() {
let config = RedisPoolConfig {
redis_ttl: 111,
redis_url: "redis://localhost:6379".to_string(),
};
let mut pool = Pool::open(config).unwrap();
let mut conn = pool.conn().expect("mock conn");

let key = "hello".to_string();
let val = "world".to_string();
conn.set_ex(key.clone(), val.clone(), 111)
.expect("mock set_ex");

let actual: RedisResult<String> = conn.get(&key);
assert_eq!(actual, Ok(val));

let _ = conn.del(&key);
let actual: RedisResult<String> = conn.get(&key);
assert!(actual.is_err());
}

#[test]
fn test_mock_multiple_redis_pool() {
let mut pool1 = Pool::open(RedisPoolConfig {
redis_ttl: 111,
redis_url: "redis://localhost:6379".to_string(),
})
.unwrap();
let mut pool2 = Pool::open(RedisPoolConfig {
redis_ttl: 111,
redis_url: "redis://localhost:6380".to_string(),
})
.unwrap();

let mut conn1 = pool1.conn().expect("mock conn");
let mut conn2 = pool2.conn().expect("mock conn");

let key = "hello".to_string();
let world = "world".to_string();

{
conn1
.set_ex(key.clone(), world.clone(), 111)
.expect("mock set_ex");
let actual: RedisResult<String> = conn1.get(&key);
assert_eq!(actual, Ok(world.clone()));
}

{
let actual: RedisResult<String> = conn2.get(&key);
assert!(actual.is_err());
}

{
let meme = "meme".to_string();
conn2
.set_ex(key.clone(), meme.clone(), 111)
.expect("mock set_ex");
let actual: RedisResult<String> = conn2.get(&key);
assert_eq!(actual, Ok(meme));
}

{
let actual: RedisResult<String> = conn1.get(&key);
assert_eq!(actual, Ok(world));
}
}
}
Loading