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

test(katana): provider test utils #1967

Merged
merged 7 commits into from
May 16, 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
1 change: 1 addition & 0 deletions Cargo.lock

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

5 changes: 5 additions & 0 deletions crates/katana/storage/provider/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,17 @@ futures = { workspace = true, optional = true }
starknet = { workspace = true, optional = true }
tokio = { workspace = true, optional = true }

alloy-primitives = { workspace = true, optional = true }
serde_json = { workspace = true, optional = true }

[features]
default = [ "fork", "in-memory" ]
fork = [ "dep:futures", "dep:starknet", "dep:tokio", "in-memory" ]
in-memory = [ ]
test-utils = [ "dep:alloy-primitives", "dep:serde_json" ]

[dev-dependencies]
alloy-primitives.workspace = true
katana-core.workspace = true
katana-runner.workspace = true
lazy_static.workspace = true
Expand Down
3 changes: 3 additions & 0 deletions crates/katana/storage/provider/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ pub mod error;
pub mod providers;
pub mod traits;

#[cfg(any(test, feature = "test-utils"))]
pub mod test_utils;

use crate::traits::block::{BlockHashProvider, BlockNumberProvider, BlockProvider, HeaderProvider};
use crate::traits::state::{StateFactoryProvider, StateProvider};
use crate::traits::state_update::StateUpdateProvider;
Expand Down
76 changes: 76 additions & 0 deletions crates/katana/storage/provider/src/test_utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
use std::sync::Arc;

use alloy_primitives::U256;
use katana_db::mdbx::{test_utils, DbEnvKind};
use katana_primitives::block::{BlockHash, FinalityStatus};
use katana_primitives::class::CompiledClass;
use katana_primitives::contract::ContractAddress;
use katana_primitives::genesis::allocation::{
DevGenesisAccount, GenesisAccountAlloc, GenesisAllocation,
};
use katana_primitives::genesis::{Genesis, GenesisClass};
use katana_primitives::utils::class::parse_compiled_class_v1;
use starknet::macros::felt;

use crate::providers::db::DbProvider;
use crate::providers::in_memory::InMemoryProvider;
use crate::traits::block::BlockWriter;

/// Creates an in-memory provider with initial states loaded for testing.
pub fn test_in_memory_provider() -> InMemoryProvider {
let provider = InMemoryProvider::new();
initialize_test_provider(&provider);
provider
}

Check warning on line 24 in crates/katana/storage/provider/src/test_utils.rs

View check run for this annotation

Codecov / codecov/patch

crates/katana/storage/provider/src/test_utils.rs#L20-L24

Added lines #L20 - L24 were not covered by tests

/// Creates a persistent storage provider with initial states loaded for testin.
pub fn test_db_provider() -> DbProvider {
let provider = DbProvider::new(test_utils::create_test_db(DbEnvKind::RW));
initialize_test_provider(&provider);
provider
}

Check warning on line 31 in crates/katana/storage/provider/src/test_utils.rs

View check run for this annotation

Codecov / codecov/patch

crates/katana/storage/provider/src/test_utils.rs#L27-L31

Added lines #L27 - L31 were not covered by tests

/// Initializes the provider with a genesis block and states.
fn initialize_test_provider<P: BlockWriter>(provider: &P) {
let genesis = create_genesis_for_testing();

let hash = BlockHash::ZERO;
let status = FinalityStatus::AcceptedOnL2;
let block = genesis.block().seal_with_hash_and_status(hash, status);
let states = genesis.state_updates();

provider
.insert_block_with_states_and_receipts(block, states, Vec::new(), Vec::new())
.expect("Failed to initialize test provider with genesis block and states.");
}

Check warning on line 45 in crates/katana/storage/provider/src/test_utils.rs

View check run for this annotation

Codecov / codecov/patch

crates/katana/storage/provider/src/test_utils.rs#L34-L45

Added lines #L34 - L45 were not covered by tests

/// Creates a genesis config specifically for testing purposes.
/// This includes:
/// - An account with simple `__execute__` function, deployed at address `0x1`.
pub fn create_genesis_for_testing() -> Genesis {
let class_hash = felt!("0x111");
let address = ContractAddress::from(felt!("0x1"));

// TODO: we should have a genesis builder that can do all of this for us.
let class = {
let json = include_str!("../test-data/simple_account.sierra.json");
let json = serde_json::from_str(json).unwrap();
let sierra = parse_compiled_class_v1(json).unwrap();

GenesisClass {
sierra: None,
compiled_class_hash: class_hash,
casm: Arc::new(CompiledClass::Class(sierra)),
}
};

// setup test account
let (_, account) = DevGenesisAccount::new_with_balance(felt!("0x1"), class_hash, U256::MAX);
let account = GenesisAllocation::Account(GenesisAccountAlloc::DevAccount(account));

let mut genesis = Genesis::default();
// insert test account class and contract
genesis.classes.insert(class_hash, class);
genesis.extend_allocations([(address, account)]);
genesis
}

Check warning on line 76 in crates/katana/storage/provider/src/test_utils.rs

View check run for this annotation

Codecov / codecov/patch

crates/katana/storage/provider/src/test_utils.rs#L50-L76

Added lines #L50 - L76 were not covered by tests
Loading