Skip to content

Commit

Permalink
test(katana-executor): improve tests and add test for state (#1649)
Browse files Browse the repository at this point in the history
- add tests for each executor's CachedState
- improve test in executor.rs
- fix sir not enough gas error
- fix test in simulate.rs
  • Loading branch information
kariy committed Mar 18, 2024
1 parent d18335f commit 29067c8
Show file tree
Hide file tree
Showing 8 changed files with 908 additions and 137 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.

1 change: 1 addition & 0 deletions crates/katana/executor/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ sir = { package = "starknet_in_rust", git = "https://github.com/dojoengine/stark
starknet-types-core = { version = "0.0.9", optional = true }

[dev-dependencies]
anyhow.workspace = true
cairo-vm.workspace = true
katana-provider.workspace = true
katana-rpc-types.workspace = true
Expand Down
321 changes: 314 additions & 7 deletions crates/katana/executor/src/implementation/blockifier/state.rs

Large diffs are not rendered by default.

6 changes: 4 additions & 2 deletions crates/katana/executor/src/implementation/sir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,8 @@ impl<'a> TransactionExecutor for StarknetVMProcessor<'a> {

let tx_ = TxWithHash::from(&tx);

let gas = 0;
// TODO: this should be based on the transaction gas cost (refer to blockifier)
let gas = u128::MAX;
let res = utils::transact(tx, &mut state.0.write().inner, block_context, gas, flags)?;

let receipt = res.receipt(tx_.as_ref());
Expand All @@ -180,7 +181,8 @@ impl<'a> TransactionExecutor for StarknetVMProcessor<'a> {

let block_context = &self.block_context;

let gas = 0;
// TODO: this should be based on the transaction gas cost (refer to blockifier)
let gas = u128::MAX;
let res = utils::transact(tx, &mut state, block_context, gas, &flags)?;

Ok(Box::new(res))
Expand Down
349 changes: 341 additions & 8 deletions crates/katana/executor/src/implementation/sir/state.rs

Large diffs are not rendered by default.

287 changes: 210 additions & 77 deletions crates/katana/executor/tests/executor.rs

Large diffs are not rendered by default.

14 changes: 7 additions & 7 deletions crates/katana/executor/tests/fixtures/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,11 @@ pub fn valid_blocks() -> [ExecutableBlock; 3] {
felt!("0x49d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7"),
felt!("0x83afd3f4caedc6eebf44246fe54e38c95e3179a5ec9ea81740eca5b482d12e"),
felt!("0x3"),
felt!("0x77880e2192169bc7107d213ebe643452e1e3e8f40bcc2ebba420b77b1522bd1"),
felt!("0x9999999999999"),
felt!("0x3ddfa445a70b927497249f94ff7431fc2e2abc761a34417fd4891beb7c2db85"),
felt!("0x9999999999999999"),
felt!("0x0"),
],
max_fee: 954300000000000,
max_fee: 4367000000000000,
signature: vec![],
nonce: FieldElement::ZERO,
}))),
Expand All @@ -136,7 +136,7 @@ pub fn valid_blocks() -> [ExecutableBlock; 3] {
sierra_class: Some(sierra),
transaction: DeclareTx::V2(DeclareTxV2 {
nonce: FieldElement::ONE,
max_fee: 666300000000000,
max_fee: 982300000000000,
chain_id,
signature: vec![],
sender_address,
Expand All @@ -163,7 +163,7 @@ pub fn valid_blocks() -> [ExecutableBlock; 3] {
ExecutableTxWithHash::new(ExecutableTx::DeployAccount(DeployAccountTx::V1(
DeployAccountTxV1 {
chain_id,
max_fee: 883800000000000,
max_fee: 1443900000000000,
signature: vec![],
nonce: 0u64.into(),
contract_address_salt: felt!(
Expand All @@ -176,7 +176,7 @@ pub fn valid_blocks() -> [ExecutableBlock; 3] {
"0x5400e90f7e0ae78bd02c77cd75527280470e2fe19c54970dd79dc37a9d3645c"
),
contract_address: ContractAddress(felt!(
"0x77880e2192169bc7107d213ebe643452e1e3e8f40bcc2ebba420b77b1522bd1"
"0x3ddfa445a70b927497249f94ff7431fc2e2abc761a34417fd4891beb7c2db85"
)),
},
))),
Expand Down Expand Up @@ -210,7 +210,7 @@ pub fn valid_blocks() -> [ExecutableBlock; 3] {
felt!("0x12"),
felt!("0x1b39"),
felt!("0x0"),
felt!("0x6b86e40118f29ebe393a75469b4d926c7a44c2e2681b6d319520b7c1156d114"),
sender_address.into(),
],
max_fee: 2700700000000000,
signature: vec![],
Expand Down
66 changes: 30 additions & 36 deletions crates/katana/executor/tests/simulate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,34 @@ fn simulate_tx<EF: ExecutorFactory>(
) {
}

#[allow(unused)]
fn test_simulate_tx_impl<EF: ExecutorFactory>(
executor_factory: EF,
block_env: BlockEnv,
state_provider: Box<dyn StateProvider>,
tx: ExecutableTxWithHash,
flags: SimulationFlag,
) {
let mut executor = executor_factory.with_state_and_block_env(state_provider, block_env);

// TODO: assert that the tx execution didn't fail
let _ = executor.simulate(tx, flags).expect("must simulate");

// check that the underlying state is not modified
let ExecutionOutput { states, transactions } =
executor.take_execution_output().expect("must take output");

assert!(transactions.is_empty(), "simulated tx should not be stored");

assert!(states.state_updates.nonce_updates.is_empty(), "no state updates");
assert!(states.state_updates.storage_updates.is_empty(), "no state updates");
assert!(states.state_updates.contract_updates.is_empty(), "no state updates");
assert!(states.state_updates.declared_classes.is_empty(), "no state updates");

assert!(states.declared_sierra_classes.is_empty(), "no new classes should be declared");
assert!(states.declared_compiled_classes.is_empty(), "no new classes should be declared");
}

#[cfg(feature = "blockifier")]
mod blockifier {
use fixtures::blockifier::factory;
Expand All @@ -47,23 +75,7 @@ mod blockifier {
#[case] tx: ExecutableTxWithHash,
#[case] flags: SimulationFlag,
) {
let mut executor = executor_factory.with_state_and_block_env(state_provider, block_env);

let _ = executor.simulate(tx, flags).expect("must simulate");

// check that the underlying state is not modified
let ExecutionOutput { states, transactions } =
executor.take_execution_output().expect("must take output");

assert!(transactions.is_empty(), "simulated tx should not be stored");

assert!(states.state_updates.nonce_updates.is_empty(), "no state updates");
assert!(states.state_updates.storage_updates.is_empty(), "no state updates");
assert!(states.state_updates.contract_updates.is_empty(), "no state updates");
assert!(states.state_updates.declared_classes.is_empty(), "no state updates");

assert!(states.declared_sierra_classes.is_empty(), "no new classes should be declared");
assert!(states.declared_compiled_classes.is_empty(), "no new classes should be declared");
test_simulate_tx_impl(executor_factory, block_env, state_provider, tx, flags);
}
}

Expand All @@ -74,8 +86,6 @@ mod sir {

use super::*;

// TODO: unignore once wrong validate retdata issue is resolved https://github.com/dojoengine/dojo/issues/1592
#[ignore]
#[apply(simulate_tx)]
fn test_simulate_tx(
#[with(factory::default())] executor_factory: NativeExecutorFactory,
Expand All @@ -84,22 +94,6 @@ mod sir {
#[case] tx: ExecutableTxWithHash,
#[case] flags: SimulationFlag,
) {
let mut executor = executor_factory.with_state_and_block_env(state_provider, block_env);

let _ = executor.simulate(tx, flags).expect("must simulate");

// check that the underlying state is not modified
let ExecutionOutput { states, transactions } =
executor.take_execution_output().expect("must take output");

assert!(transactions.is_empty(), "simulated tx should not be stored");

assert!(states.state_updates.nonce_updates.is_empty(), "no state updates");
assert!(states.state_updates.storage_updates.is_empty(), "no state updates");
assert!(states.state_updates.contract_updates.is_empty(), "no state updates");
assert!(states.state_updates.declared_classes.is_empty(), "no state updates");

assert!(states.declared_sierra_classes.is_empty(), "no new classes should be declared");
assert!(states.declared_compiled_classes.is_empty(), "no new classes should be declared");
test_simulate_tx_impl(executor_factory, block_env, state_provider, tx, flags);
}
}

0 comments on commit 29067c8

Please sign in to comment.