Skip to content

Commit

Permalink
feat: initial redis support (only for tests)
Browse files Browse the repository at this point in the history
  • Loading branch information
dinhani-cw committed Jul 29, 2024
1 parent 06cd4ce commit f4877c3
Show file tree
Hide file tree
Showing 8 changed files with 328 additions and 10 deletions.
34 changes: 32 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ tracing-subscriber = { version = "=0.3.18", features = ["env-filter", "json"] }
tracing-serde = "=0.1.3"

# storage
redis = "=0.26.0"
rocksdb = { version = "=0.22.0", features = ["multi-threaded-cf"] }
sqlx = { version = "=0.7.4", features = ["runtime-tokio", "postgres", "bigdecimal", "time"] }

Expand Down
24 changes: 24 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
version: "2"
services:

# ----------------------------------------------------------------------------
# PostgreSQL
# ----------------------------------------------------------------------------
postgres:
image: postgres
environment:
Expand All @@ -20,6 +23,24 @@ services:
profiles:
- manual

# ----------------------------------------------------------------------------
# Redis
# ----------------------------------------------------------------------------
redis:
image: redis:7.2.5
ports:
- "6379:6379"

redis-commander:
image: rediscommander/redis-commander:latest
ports:
- "8081:8081"
environment:
- REDIS_HOSTS=local:redis:6379

# ----------------------------------------------------------------------------
# PromethMeus
# ----------------------------------------------------------------------------
prometheus:
image: prom/prometheus
volumes:
Expand All @@ -32,6 +53,9 @@ services:
- "--web.enable-lifecycle"
- "--log.level=debug"

# ----------------------------------------------------------------------------
# OpenTelemetry
# ----------------------------------------------------------------------------
jaeger:
image: jaegertracing/all-in-one:1.57
ports:
Expand Down
24 changes: 16 additions & 8 deletions src/eth/primitives/block_number.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use std::fmt::Display;
use std::num::TryFromIntError;
use std::ops::Add;
use std::ops::AddAssign;
Expand All @@ -20,7 +19,22 @@ use crate::alias::RevmU256;
use crate::eth::primitives::Hash;
use crate::gen_newtype_from;

#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash, derive_more::Add, derive_more::Sub, serde::Serialize, serde::Deserialize)]
#[derive(
Debug,
derive_more::Display,
Clone,
Copy,
Default,
PartialEq,
Eq,
PartialOrd,
Ord,
Hash,
derive_more::Add,
derive_more::Sub,
serde::Serialize,
serde::Deserialize,
)]
#[serde(transparent)]
pub struct BlockNumber(pub U64);

Expand Down Expand Up @@ -75,12 +89,6 @@ impl BlockNumber {
}
}

impl Display for BlockNumber {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "#{}", self.0)
}
}

impl Dummy<Faker> for BlockNumber {
fn dummy_with_rng<R: ethers_core::rand::prelude::Rng + ?Sized>(_: &Faker, rng: &mut R) -> Self {
rng.next_u64().into()
Expand Down
1 change: 1 addition & 0 deletions src/eth/storage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ mod permanent_storage;
mod postgres_external_rpc;
pub mod rocks;

mod redis;
mod storage_point_in_time;
mod stratus_storage;
mod temporary_storage;
Expand Down
13 changes: 13 additions & 0 deletions src/eth/storage/permanent_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use crate::eth::primitives::LogMined;
use crate::eth::primitives::Slot;
use crate::eth::primitives::SlotIndex;
use crate::eth::primitives::TransactionMined;
use crate::eth::storage::redis::RedisPermanentStorage;
use crate::eth::storage::InMemoryPermanentStorage;
use crate::eth::storage::RocksPermanentStorage;
use crate::eth::storage::StoragePointInTime;
Expand Down Expand Up @@ -79,6 +80,10 @@ pub struct PermanentStorageConfig {
#[arg(long = "perm-storage", env = "PERM_STORAGE")]
pub perm_storage_kind: PermanentStorageKind,

/// Storage connection URL.
#[arg(long = "perm-storage-url", env = "PERM_STORAGE_URL")]
pub perm_storage_url: Option<String>,

/// RocksDB storage path prefix to execute multiple local Stratus instances.
#[arg(long = "rocks-path-prefix", env = "ROCKS_PATH_PREFIX")]
pub rocks_path_prefix: Option<String>,
Expand All @@ -88,6 +93,10 @@ pub struct PermanentStorageConfig {
pub enum PermanentStorageKind {
#[serde(rename = "inmemory")]
InMemory,

#[serde(rename = "redis")]
Redis,

#[serde(rename = "rocks")]
Rocks,
}
Expand All @@ -99,6 +108,9 @@ impl PermanentStorageConfig {

let perm: Box<dyn PermanentStorage> = match self.perm_storage_kind {
PermanentStorageKind::InMemory => Box::<InMemoryPermanentStorage>::default(),

PermanentStorageKind::Redis => Box::new(RedisPermanentStorage::new()?),

PermanentStorageKind::Rocks => {
let prefix = self.rocks_path_prefix.clone();
Box::new(RocksPermanentStorage::new(prefix)?)
Expand All @@ -114,6 +126,7 @@ impl FromStr for PermanentStorageKind {
fn from_str(s: &str) -> anyhow::Result<Self, Self::Err> {
match s {
"inmemory" => Ok(Self::InMemory),
"redis" => Ok(Self::Redis),
"rocks" => Ok(Self::Rocks),
s => Err(anyhow!("unknown permanent storage: {}", s)),
}
Expand Down
3 changes: 3 additions & 0 deletions src/eth/storage/redis/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
mod redis_permanent;

pub use redis_permanent::RedisPermanentStorage;
Loading

0 comments on commit f4877c3

Please sign in to comment.