-
Notifications
You must be signed in to change notification settings - Fork 188
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(katana): provider test utils (#1967)
# 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
Showing
5 changed files
with
86 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
1 change: 1 addition & 0 deletions
1
crates/katana/storage/provider/test-data/simple_account.sierra.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../../../contracts/compiled/account_with_dummy_validate.sierra.json |