Skip to content

Commit

Permalink
feat: postgres temporary storage
Browse files Browse the repository at this point in the history
  • Loading branch information
carneiro-cw committed May 3, 2024
1 parent abb3728 commit 14fc1c1
Show file tree
Hide file tree
Showing 55 changed files with 614 additions and 128 deletions.

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

This file was deleted.

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

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

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

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

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

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

23 changes: 22 additions & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ use crate::eth::storage::PostgresExternalRpcStorage;
use crate::eth::storage::PostgresExternalRpcStorageConfig;
use crate::eth::storage::PostgresPermanentStorage;
use crate::eth::storage::PostgresPermanentStorageConfig;
use crate::eth::storage::PostgresTemporaryStorage;
use crate::eth::storage::PostgresTemporaryStorageConfig;
#[cfg(feature = "rocks")]
use crate::eth::storage::RocksPermanentStorage;
#[cfg(feature = "rocks")]
Expand Down Expand Up @@ -562,22 +564,40 @@ pub struct TemporaryStorageConfig {
/// Temporary storage implementation.
#[arg(long = "temp-storage", env = "TEMP_STORAGE")]
pub temp_storage_kind: TemporaryStorageKind,

#[arg(long = "temp-storage-connections", env = "TEMP_STORAGE_CONNECTIONS")]
pub temp_storage_connections: u32,

/// Permamenent storage timeout when opening a connection (in millis).
#[arg(long = "temp-storage-timeout", env = "TEMP_STORAGE_TIMEOUT")]
pub temp_storage_timeout_millis: u64,
}

#[derive(Clone, Debug)]
pub enum TemporaryStorageKind {
InMemory,
#[cfg(feature = "rocks")]
Rocks,
Postgres {
url: String,
},
}

impl TemporaryStorageConfig {
/// Initializes temporary storage implementation.
pub async fn init(&self) -> anyhow::Result<Arc<dyn TemporaryStorage>> {
match self.temp_storage_kind {
match self.temp_storage_kind.clone() {
TemporaryStorageKind::InMemory => Ok(Arc::new(InMemoryTemporaryStorage::default())),
#[cfg(feature = "rocks")]
TemporaryStorageKind::Rocks => Ok(Arc::new(RocksTemporary::new().await?)),
TemporaryStorageKind::Postgres { url } => Ok(Arc::new(
PostgresTemporaryStorage::new(PostgresTemporaryStorageConfig {
url,
connections: self.temp_storage_connections,
acquire_timeout: Duration::from_millis(self.temp_storage_timeout_millis),
})
.await?,
)),
}
}
}
Expand All @@ -590,6 +610,7 @@ impl FromStr for TemporaryStorageKind {
"inmemory" => Ok(Self::InMemory),
#[cfg(feature = "rocks")]
"rocks" => Ok(Self::Rocks),
s if s.starts_with("postgres://") => Ok(Self::Postgres { url: s.to_string() }),
s => Err(anyhow!("unknown temporary storage: {}", s)),
}
}
Expand Down
17 changes: 17 additions & 0 deletions src/eth/primitives/execution_account_changes.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use std::collections::HashMap;

use anyhow::Context;

use super::CodeHash;
use crate::eth::primitives::Account;
use crate::eth::primitives::Address;
Expand Down Expand Up @@ -114,3 +116,18 @@ impl ExecutionAccountChanges {
not(self.new_account)
}
}

impl TryFrom<ExecutionAccountChanges> for Account {
type Error = anyhow::Error;
fn try_from(value: ExecutionAccountChanges) -> Result<Self, Self::Error> {
Ok(Account {
address: value.address,
nonce: value.nonce.take().context("No nonce in changes")?,
balance: value.balance.take().context("No balance in changes")?,
bytecode: value.bytecode.take().context("No bytecode in changes")?,
code_hash: value.code_hash,
static_slot_indexes: value.static_slot_indexes.take().context("No static_slot_indexes in changes")?,
mapping_slot_indexes: value.mapping_slot_indexes.take().context("No mapping_slot_indexes in changes")?,
})
}
}
8 changes: 5 additions & 3 deletions src/eth/storage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ mod external_rpc_storage;
mod inmemory;
mod permanent_storage;
mod postgres_external_rpc;
mod postgres_permanent;
mod postgres;
#[cfg(feature = "rocks")]
pub mod rocks;

Expand All @@ -21,8 +21,10 @@ pub use inmemory::InMemoryTemporaryStorage;
pub use permanent_storage::PermanentStorage;
pub use postgres_external_rpc::PostgresExternalRpcStorage;
pub use postgres_external_rpc::PostgresExternalRpcStorageConfig;
pub use postgres_permanent::PostgresPermanentStorage;
pub use postgres_permanent::PostgresPermanentStorageConfig;
pub use postgres::PostgresPermanentStorage;
pub use postgres::PostgresPermanentStorageConfig;
pub use postgres::PostgresTemporaryStorage;
pub use postgres::PostgresTemporaryStorageConfig;
#[cfg(feature = "rocks")]
pub use rocks::rocks_permanent::RocksPermanentStorage;
#[cfg(feature = "rocks")]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#[allow(clippy::module_inception)]
mod postgres_permanent;
mod types;
mod postgres_temporary;

pub use postgres_permanent::PostgresPermanentStorage;
pub use postgres_permanent::PostgresPermanentStorageConfig;
pub use postgres_temporary::PostgresTemporaryStorage;
pub use postgres_temporary::PostgresTemporaryStorageConfig;
Loading

0 comments on commit 14fc1c1

Please sign in to comment.