Skip to content

Commit

Permalink
test(katana): provider test utils (#1967)
Browse files Browse the repository at this point in the history
# Description

Test utilities for when we need to instantiate the providers with predefined initial states that are useful for testing/benchmarking purposes.

## Related issue

<!--
Please link related issues: Fixes #<issue_number>
More info: https://docs.github.com/en/free-pro-team@latest/github/managing-your-work-on-github/linking-a-pull-request-to-an-issue#linking-a-pull-request-to-an-issue-using-a-keyword
-->

## Tests

<!--
Please refer to the CONTRIBUTING.md file to know more about the testing process. Ensure you've tested at least the package you're modifying if running all the tests consumes too much memory on your system.
-->

- [ ] Yes
- [x] No, because they aren't needed
- [ ] No, because I need help

## Added to documentation?

<!--
If the changes are small, code comments are enough, otherwise, the documentation is needed. It
may be a README.md file added to your module/package, a DojoBook PR or both.
-->

- [ ] README.md
- [ ] [Dojo Book](https://github.com/dojoengine/book)
- [x] No documentation needed

## Checklist

- [x] I've formatted my code (`scripts/prettier.sh`, `scripts/rust_fmt.sh`, `scripts/cairo_fmt.sh`)
- [x] I've linted my code (`scripts/clippy.sh`, `scripts/docs.sh`)
- [x] I've commented my code
- [ ] I've requested a review after addressing the comments
  • Loading branch information
kariy authored May 16, 2024
1 parent 49f0a4e commit 8c6df7b
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 0 deletions.
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
}

/// 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
}

/// 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.");
}

/// 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
}

0 comments on commit 8c6df7b

Please sign in to comment.