Skip to content

Commit

Permalink
refactor: remove all references to eth_storage, now it is just storage
Browse files Browse the repository at this point in the history
  • Loading branch information
dinhani-cw committed Feb 22, 2024
1 parent cc858c2 commit 2eb0541
Show file tree
Hide file tree
Showing 9 changed files with 20 additions and 26 deletions.
10 changes: 5 additions & 5 deletions src/eth/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use crate::eth::primitives::Hash;
use crate::eth::primitives::LogMined;
use crate::eth::primitives::StoragePointInTime;
use crate::eth::primitives::TransactionInput;
use crate::eth::storage::EthStorageError;
use crate::eth::storage::StorageError;
use crate::eth::storage::StratusStorage;
use crate::eth::BlockMiner;

Expand Down Expand Up @@ -60,13 +60,13 @@ pub struct EthExecutor {

impl EthExecutor {
/// Creates a new executor.
pub fn new(evms: NonEmpty<Box<dyn Evm>>, eth_storage: Arc<StratusStorage>) -> Self {
pub fn new(evms: NonEmpty<Box<dyn Evm>>, storage: Arc<StratusStorage>) -> Self {
let evm_tx = spawn_background_evms(evms);

Self {
evm_tx,
miner: Mutex::new(BlockMiner::new(Arc::clone(&eth_storage))),
storage: eth_storage,
miner: Mutex::new(BlockMiner::new(Arc::clone(&storage))),
storage,
block_notifier: broadcast::channel(NOTIFIER_CAPACITY).0,
log_notifier: broadcast::channel(NOTIFIER_CAPACITY).0,
}
Expand Down Expand Up @@ -217,7 +217,7 @@ impl EthExecutor {
let block = miner_lock.mine_with_one_transaction(transaction.clone(), execution.clone()).await?;
match self.storage.commit_to_perm(block.clone()).await {
Ok(()) => {}
Err(EthStorageError::Conflict(conflicts)) => {
Err(StorageError::Conflict(conflicts)) => {
tracing::warn!(?conflicts, "storage conflict detected when saving block");
continue;
}
Expand Down
4 changes: 2 additions & 2 deletions src/eth/rpc/rpc_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ use crate::eth::EthExecutor;
// -----------------------------------------------------------------------------

/// Starts JSON-RPC server.
pub async fn serve_rpc(executor: EthExecutor, eth_storage: Arc<StratusStorage>, config: StratusConfig) -> anyhow::Result<()> {
pub async fn serve_rpc(executor: EthExecutor, storage: Arc<StratusStorage>, config: StratusConfig) -> anyhow::Result<()> {
// configure subscriptions
let subs = Arc::new(RpcSubscriptions::default());
let subscriptions_cleaner_handle = Arc::clone(&subs).spawn_subscriptions_cleaner();
Expand All @@ -60,7 +60,7 @@ pub async fn serve_rpc(executor: EthExecutor, eth_storage: Arc<StratusStorage>,

// services
executor,
storage: eth_storage,
storage,

// subscriptions
subs,
Expand Down
5 changes: 0 additions & 5 deletions src/eth/storage/eth_storage.rs

This file was deleted.

6 changes: 3 additions & 3 deletions src/eth/storage/inmemory/inmemory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ use crate::eth::primitives::StoragePointInTime;
use crate::eth::primitives::TransactionMined;
use crate::eth::storage::inmemory::InMemoryAccount;
use crate::eth::storage::inmemory::InMemoryHistory;
use crate::eth::storage::EthStorageError;
use crate::eth::storage::PermanentStorage;
use crate::eth::storage::StorageError;
use crate::eth::storage::TemporaryStorage;

/// In-memory implementation using maps.
Expand Down Expand Up @@ -208,7 +208,7 @@ impl PermanentStorage for InMemoryStorage {
Ok(logs)
}

async fn save_block(&self, block: Block) -> anyhow::Result<(), EthStorageError> {
async fn save_block(&self, block: Block) -> anyhow::Result<(), StorageError> {
let mut state = self.lock_write().await;

// keep track of current block if we need to rollback
Expand All @@ -231,7 +231,7 @@ impl PermanentStorage for InMemoryStorage {
PermanentStorage::reset_at(self, current_block).await?;

// inform error
return Err(EthStorageError::Conflict(conflicts));
return Err(StorageError::Conflict(conflicts));
}

// save transaction
Expand Down
3 changes: 1 addition & 2 deletions src/eth/storage/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
//! Ethereum / EVM storage.
mod eth_storage;
mod inmemory;
mod permanent_storage;
mod postgres;
Expand All @@ -10,6 +9,6 @@ mod temporary_storage;

pub use inmemory::InMemoryStorage;
pub use permanent_storage::PermanentStorage;
pub use storage_error::EthStorageError;
pub use storage_error::StorageError;
pub use stratus_storage::StratusStorage;
pub use temporary_storage::TemporaryStorage;
4 changes: 2 additions & 2 deletions src/eth/storage/permanent_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use crate::eth::primitives::Slot;
use crate::eth::primitives::SlotIndex;
use crate::eth::primitives::StoragePointInTime;
use crate::eth::primitives::TransactionMined;
use crate::eth::storage::EthStorageError;
use crate::eth::storage::StorageError;

/// Permanent (committed) storage operations
#[async_trait]
Expand Down Expand Up @@ -44,7 +44,7 @@ pub trait PermanentStorage: Send + Sync {
async fn read_logs(&self, filter: &LogFilter) -> anyhow::Result<Vec<LogMined>>;

/// Persists atomically all changes from a block.
async fn save_block(&self, block: Block) -> anyhow::Result<(), EthStorageError>;
async fn save_block(&self, block: Block) -> anyhow::Result<(), StorageError>;

/// Persists initial accounts (test accounts or genesis accounts).
async fn save_accounts(&self, accounts: Vec<Account>) -> anyhow::Result<()>;
Expand Down
8 changes: 4 additions & 4 deletions src/eth/storage/postgres/postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ use crate::eth::primitives::TransactionMined;
use crate::eth::storage::postgres::types::PostgresLog;
use crate::eth::storage::postgres::types::PostgresTopic;
use crate::eth::storage::postgres::types::PostgresTransaction;
use crate::eth::storage::EthStorageError;
use crate::eth::storage::PermanentStorage;
use crate::eth::storage::StorageError;
use crate::infra::postgres::Postgres;

#[async_trait]
Expand Down Expand Up @@ -427,7 +427,7 @@ impl PermanentStorage for Postgres {
// byte arrays for numbers. Implementing the trait sqlx::Encode for the eth primitives would make
// this much easier to work with (note: I tried implementing Encode for both Hash and Nonce and
// neither worked for some reason I was not able to determine at this time)
async fn save_block(&self, block: Block) -> anyhow::Result<(), EthStorageError> {
async fn save_block(&self, block: Block) -> anyhow::Result<(), StorageError> {
let mut tx = self.connection_pool.begin().await.context("failed to init save_block transaction")?;

tracing::debug!(block = ?block, "saving block");
Expand Down Expand Up @@ -521,7 +521,7 @@ impl PermanentStorage for Postgres {
// A successful insert/update with no conflicts will have one affected row
if account_result.rows_affected() != 1 {
tx.rollback().await.context("failed to rollback transaction")?;
let error: EthStorageError = EthStorageError::Conflict(ExecutionConflicts(nonempty![ExecutionConflict::Account {
let error: StorageError = StorageError::Conflict(ExecutionConflicts(nonempty![ExecutionConflict::Account {
address: change.address,
expected_balance: original_balance,
expected_nonce: original_nonce,
Expand Down Expand Up @@ -576,7 +576,7 @@ impl PermanentStorage for Postgres {
// A successful insert/update with no conflicts will have one affected row
if slot_result.rows_affected() != 1 {
tx.rollback().await.context("failed to rollback transaction")?;
let error: EthStorageError = EthStorageError::Conflict(ExecutionConflicts(nonempty![ExecutionConflict::PgSlot {
let error: StorageError = StorageError::Conflict(ExecutionConflicts(nonempty![ExecutionConflict::PgSlot {
address: change.address,
slot: idx,
expected: original_value,
Expand Down
2 changes: 1 addition & 1 deletion src/eth/storage/storage_error.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::eth::primitives::ExecutionConflicts;

#[derive(Debug, thiserror::Error, derive_new::new)]
pub enum EthStorageError {
pub enum StorageError {
/// Generic error interacting with the storage.
#[error("Storage error: {0}")]
Generic(#[from] anyhow::Error),
Expand Down
4 changes: 2 additions & 2 deletions src/eth/storage/stratus_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ use crate::eth::primitives::Slot;
use crate::eth::primitives::SlotIndex;
use crate::eth::primitives::StoragePointInTime;
use crate::eth::primitives::TransactionMined;
use crate::eth::storage::EthStorageError;
use crate::eth::storage::PermanentStorage;
use crate::eth::storage::StorageError;
use crate::eth::storage::TemporaryStorage;
use crate::infra::metrics;

Expand Down Expand Up @@ -143,7 +143,7 @@ impl StratusStorage {
}

/// Commits changes to permanent storage and prepares temporary storage to a new block to be produced.
pub async fn commit_to_perm(&self, block: Block) -> anyhow::Result<(), EthStorageError> {
pub async fn commit_to_perm(&self, block: Block) -> anyhow::Result<(), StorageError> {
let start = Instant::now();

let result = self.perm.save_block(block).await;
Expand Down

0 comments on commit 2eb0541

Please sign in to comment.