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

refactor(katana): remove ContractInfoProvider #1943

Merged
merged 2 commits into from
May 9, 2024
Merged
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
12 changes: 1 addition & 11 deletions crates/katana/storage/provider/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use katana_primitives::block::{
SealedBlockWithStatus,
};
use katana_primitives::class::{ClassHash, CompiledClass, CompiledClassHash, FlattenedSierraClass};
use katana_primitives::contract::{ContractAddress, GenericContractInfo, StorageKey, StorageValue};
use katana_primitives::contract::{ContractAddress, StorageKey, StorageValue};
use katana_primitives::env::BlockEnv;
use katana_primitives::receipt::Receipt;
use katana_primitives::state::{StateUpdates, StateUpdatesWithDeclaredClasses};
Expand All @@ -24,7 +24,6 @@ pub mod providers;
pub mod traits;

use crate::traits::block::{BlockHashProvider, BlockNumberProvider, BlockProvider, HeaderProvider};
use crate::traits::contract::ContractInfoProvider;
use crate::traits::state::{StateFactoryProvider, StateProvider};
use crate::traits::state_update::StateUpdateProvider;
use crate::traits::transaction::{ReceiptProvider, TransactionProvider, TransactionsProviderExt};
Expand Down Expand Up @@ -298,15 +297,6 @@ where
}
}

impl<Db> ContractInfoProvider for BlockchainProvider<Db>
where
Db: ContractInfoProvider,
{
fn contract(&self, address: ContractAddress) -> ProviderResult<Option<GenericContractInfo>> {
self.provider.contract(address)
}
}

impl<Db> StateRootProvider for BlockchainProvider<Db>
where
Db: StateRootProvider,
Expand Down
31 changes: 16 additions & 15 deletions crates/katana/storage/provider/src/providers/fork/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ use futures::stream::Stream;
use futures::{Future, FutureExt};
use katana_primitives::block::BlockHashOrNumber;
use katana_primitives::class::{ClassHash, CompiledClass, CompiledClassHash, FlattenedSierraClass};
use katana_primitives::contract::{
ContractAddress, GenericContractInfo, Nonce, StorageKey, StorageValue,
};
use katana_primitives::contract::{ContractAddress, Nonce, StorageKey, StorageValue};
use katana_primitives::conversion::rpc::{
compiled_class_hash_from_flattened_sierra_class, flattened_sierra_to_compiled_class,
legacy_rpc_to_compiled_class,
Expand All @@ -27,7 +25,7 @@ use tracing::{error, trace};

use crate::error::ProviderError;
use crate::providers::in_memory::cache::CacheStateDb;
use crate::traits::contract::{ContractClassProvider, ContractInfoProvider};
use crate::traits::contract::ContractClassProvider;
use crate::traits::state::StateProvider;
use crate::ProviderResult;

Expand Down Expand Up @@ -344,13 +342,6 @@ impl SharedStateProvider {
}
}

impl ContractInfoProvider for SharedStateProvider {
fn contract(&self, address: ContractAddress) -> ProviderResult<Option<GenericContractInfo>> {
let info = self.0.contract_state.read().get(&address).cloned();
Ok(info)
}
}

impl StateProvider for SharedStateProvider {
fn nonce(&self, address: ContractAddress) -> ProviderResult<Option<Nonce>> {
// TEMP:
Expand All @@ -369,8 +360,13 @@ impl StateProvider for SharedStateProvider {
//
// Similar story with `class_hash_of_contract`
//
if let nonce @ Some(_) =
self.contract(address)?.map(|i| i.nonce).filter(|n| n != &Nonce::ZERO)
if let nonce @ Some(_) = self
.0
.contract_state
.read()
.get(&address)
.map(|i| i.nonce)
.filter(|n| n != &Nonce::ZERO)
{
return Ok(nonce);
}
Expand Down Expand Up @@ -432,8 +428,13 @@ impl StateProvider for SharedStateProvider {
address: ContractAddress,
) -> ProviderResult<Option<ClassHash>> {
// See comment at `nonce` for the explanation of this filter.
if let hash @ Some(_) =
self.contract(address)?.map(|i| i.class_hash).filter(|h| h != &ClassHash::ZERO)
if let hash @ Some(_) = self
.0
.contract_state
.read()
.get(&address)
.map(|i| i.class_hash)
.filter(|h| h != &ClassHash::ZERO)
{
return Ok(hash);
}
Expand Down
30 changes: 2 additions & 28 deletions crates/katana/storage/provider/src/providers/fork/state.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
use std::sync::Arc;

use katana_primitives::class::{ClassHash, CompiledClass, CompiledClassHash, FlattenedSierraClass};
use katana_primitives::contract::{
ContractAddress, GenericContractInfo, Nonce, StorageKey, StorageValue,
};
use katana_primitives::contract::{ContractAddress, Nonce, StorageKey, StorageValue};

use super::backend::SharedStateProvider;
use crate::providers::in_memory::cache::CacheStateDb;
use crate::providers::in_memory::state::StateSnapshot;
use crate::traits::contract::{ContractClassProvider, ContractInfoProvider};
use crate::traits::contract::ContractClassProvider;
use crate::traits::state::StateProvider;
use crate::ProviderResult;

Expand All @@ -24,15 +22,6 @@ impl ForkedStateDb {
}
}

impl ContractInfoProvider for ForkedStateDb {
fn contract(&self, address: ContractAddress) -> ProviderResult<Option<GenericContractInfo>> {
if let info @ Some(_) = self.contract_state.read().get(&address).cloned() {
return Ok(info);
}
ContractInfoProvider::contract(&self.db, address)
}
}

impl StateProvider for ForkedStateDb {
fn class_hash_of_contract(
&self,
Expand Down Expand Up @@ -101,12 +90,6 @@ impl ContractClassProvider for CacheStateDb<SharedStateProvider> {

pub(super) struct LatestStateProvider(pub(super) Arc<ForkedStateDb>);

impl ContractInfoProvider for LatestStateProvider {
fn contract(&self, address: ContractAddress) -> ProviderResult<Option<GenericContractInfo>> {
ContractInfoProvider::contract(&self.0, address)
}
}

impl StateProvider for LatestStateProvider {
fn nonce(&self, address: ContractAddress) -> ProviderResult<Option<Nonce>> {
StateProvider::nonce(&self.0, address)
Expand Down Expand Up @@ -145,15 +128,6 @@ impl ContractClassProvider for LatestStateProvider {
}
}

impl ContractInfoProvider for ForkedSnapshot {
fn contract(&self, address: ContractAddress) -> ProviderResult<Option<GenericContractInfo>> {
if let info @ Some(_) = self.inner.contract_state.get(&address).cloned() {
return Ok(info);
}
ContractInfoProvider::contract(&self.inner.db, address)
}
}

impl StateProvider for ForkedSnapshot {
fn nonce(&self, address: ContractAddress) -> ProviderResult<Option<Nonce>> {
if let nonce @ Some(_) = self
Expand Down
28 changes: 6 additions & 22 deletions crates/katana/storage/provider/src/providers/in_memory/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@ use std::sync::Arc;

use katana_primitives::block::BlockNumber;
use katana_primitives::class::{ClassHash, CompiledClass, CompiledClassHash, FlattenedSierraClass};
use katana_primitives::contract::{
ContractAddress, GenericContractInfo, Nonce, StorageKey, StorageValue,
};
use katana_primitives::contract::{ContractAddress, Nonce, StorageKey, StorageValue};

use super::cache::{CacheSnapshotWithoutClasses, CacheStateDb, SharedContractClasses};
use crate::traits::contract::{ContractClassProvider, ContractInfoProvider};
use crate::traits::contract::ContractClassProvider;
use crate::traits::state::StateProvider;
use crate::ProviderResult;

Expand Down Expand Up @@ -117,16 +115,9 @@ impl InMemoryStateDb {
}
}

impl ContractInfoProvider for InMemorySnapshot {
fn contract(&self, address: ContractAddress) -> ProviderResult<Option<GenericContractInfo>> {
let info = self.inner.contract_state.get(&address).cloned();
Ok(info)
}
}

impl StateProvider for InMemorySnapshot {
fn nonce(&self, address: ContractAddress) -> ProviderResult<Option<Nonce>> {
let nonce = ContractInfoProvider::contract(&self, address)?.map(|i| i.nonce);
let nonce = self.inner.contract_state.get(&address).map(|i| i.nonce);
Ok(nonce)
}

Expand All @@ -143,7 +134,7 @@ impl StateProvider for InMemorySnapshot {
&self,
address: ContractAddress,
) -> ProviderResult<Option<ClassHash>> {
let class_hash = ContractInfoProvider::contract(&self, address)?.map(|i| i.class_hash);
let class_hash = self.inner.contract_state.get(&address).map(|i| i.class_hash);
Ok(class_hash)
}
}
Expand Down Expand Up @@ -176,16 +167,9 @@ impl ContractClassProvider for InMemorySnapshot {

pub(super) struct LatestStateProvider(pub(super) Arc<InMemoryStateDb>);

impl ContractInfoProvider for LatestStateProvider {
fn contract(&self, address: ContractAddress) -> ProviderResult<Option<GenericContractInfo>> {
let info = self.0.contract_state.read().get(&address).cloned();
Ok(info)
}
}

impl StateProvider for LatestStateProvider {
fn nonce(&self, address: ContractAddress) -> ProviderResult<Option<Nonce>> {
let nonce = ContractInfoProvider::contract(&self, address)?.map(|i| i.nonce);
let nonce = self.0.contract_state.read().get(&address).map(|i| i.nonce);
Ok(nonce)
}

Expand All @@ -202,7 +186,7 @@ impl StateProvider for LatestStateProvider {
&self,
address: ContractAddress,
) -> ProviderResult<Option<ClassHash>> {
let class_hash = ContractInfoProvider::contract(&self, address)?.map(|i| i.class_hash);
let class_hash = self.0.contract_state.read().get(&address).map(|i| i.class_hash);
Ok(class_hash)
}
}
Expand Down
7 changes: 0 additions & 7 deletions crates/katana/storage/provider/src/traits/contract.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
use katana_primitives::class::{ClassHash, CompiledClass, CompiledClassHash, FlattenedSierraClass};
use katana_primitives::contract::{ContractAddress, GenericContractInfo};

use crate::ProviderResult;

#[auto_impl::auto_impl(&, Box, Arc)]
pub trait ContractInfoProvider: Send + Sync {
/// Returns the contract information given its address.
fn contract(&self, address: ContractAddress) -> ProviderResult<Option<GenericContractInfo>>;
}

/// A provider trait for retrieving contract class related information.
#[auto_impl::auto_impl(&, Box, Arc)]
pub trait ContractClassProvider: Send + Sync {
Expand Down
Loading