Skip to content

Commit

Permalink
Pass max_steps flag value to blockifier code (#1760)
Browse files Browse the repository at this point in the history
  • Loading branch information
kirugan authored Mar 25, 2024
1 parent 1329f05 commit 8b790ec
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 4 deletions.
14 changes: 10 additions & 4 deletions vm/rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ pub extern "C" fn cairoVMCall(
block_info_ptr: *const BlockInfo,
reader_handle: usize,
chain_id: *const c_char,
_max_steps: c_ulonglong, //todo: enforce this
max_steps: c_ulonglong,
) {
let block_info = unsafe { *block_info_ptr };
let call_info = unsafe { *call_info_ptr };
Expand Down Expand Up @@ -114,7 +114,7 @@ pub extern "C" fn cairoVMCall(
let mut resources = ExecutionResources::default();
let context = EntryPointExecutionContext::new_invoke(
Arc::new(TransactionContext {
block_context: build_block_context(&mut state, &block_info, chain_id_str),
block_context: build_block_context(&mut state, &block_info, chain_id_str, Some(max_steps)),
tx_info: TransactionInfo::Deprecated(DeprecatedTransactionInfo::default()),
}),
false,
Expand Down Expand Up @@ -189,7 +189,7 @@ pub extern "C" fn cairoVMExecute(
let mut state = CachedState::new(reader, GlobalContractCache::new(1));
let txns_and_query_bits = txns_and_query_bits.unwrap();
let mut classes = classes.unwrap();
let block_context: BlockContext = build_block_context(&mut state, &block_info, chain_id_str);
let block_context: BlockContext = build_block_context(&mut state, &block_info, chain_id_str, None);
let charge_fee = skip_charge_fee == 0;
let validate = skip_validate == 0;

Expand Down Expand Up @@ -378,6 +378,7 @@ fn build_block_context(
state: &mut dyn State,
block_info: &BlockInfo,
chain_id_str: &str,
max_steps: Option<c_ulonglong>,
) -> BlockContext {
let sequencer_addr = StarkFelt::new(block_info.sequencer_address).unwrap();
let gas_price_wei_felt = StarkFelt::new(block_info.gas_price_wei).unwrap();
Expand All @@ -393,6 +394,11 @@ fn build_block_context(
hash: BlockHash(StarkFelt::new(block_info.block_hash_to_be_revealed).unwrap()),
})
}
let mut constants = get_versioned_constants(block_info.version);
if let Some(max_steps) = max_steps {
constants.invoke_tx_max_n_steps = max_steps as u32;
}

pre_process_block(state, old_block_number_and_hash, BlockifierBlockInfo{
block_number: starknet_api::block::BlockNumber(block_info.block_number),
block_timestamp: starknet_api::block::BlockTimestamp(block_info.block_timestamp),
Expand All @@ -411,7 +417,7 @@ fn build_block_context(
eth_fee_token_address: ContractAddress::try_from(StarkHash::try_from("0x049d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7").unwrap()).unwrap(),
strk_fee_token_address: ContractAddress::try_from(StarkHash::try_from("0x04718f5a0fc34cc1af16a1cdee98ffb20c31f5cd61d6ab07201858f4287c938d").unwrap()).unwrap(),
},
}, get_versioned_constants(block_info.version)).unwrap()
}, constants).unwrap()
}


Expand Down
41 changes: 41 additions & 0 deletions vm/vm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,47 @@ func TestV1Call(t *testing.T) {
assert.Equal(t, []*felt.Felt{new(felt.Felt).SetUint64(37)}, ret)
}

func TestCall_MaxSteps(t *testing.T) {
testDB := pebble.NewMemTest(t)
txn, err := testDB.NewTransaction(true)
require.NoError(t, err)
client := feeder.NewTestClient(t, &utils.Mainnet)
gw := adaptfeeder.New(client)
t.Cleanup(func() {
require.NoError(t, txn.Discard())
})

contractAddr := utils.HexToFelt(t, "0xDEADBEEF")
// https://voyager.online/class/0x03297a93c52357144b7da71296d7e8231c3e0959f0a1d37222204f2f7712010e
classHash := utils.HexToFelt(t, "0x3297a93c52357144b7da71296d7e8231c3e0959f0a1d37222204f2f7712010e")
simpleClass, err := gw.Class(context.Background(), classHash)
require.NoError(t, err)

encoder.RegisterType(reflect.TypeOf(core.Cairo0Class{})) //nolint:errcheck

testState := core.NewState(txn)
require.NoError(t, testState.Update(0, &core.StateUpdate{
OldRoot: &felt.Zero,
NewRoot: utils.HexToFelt(t, "0x3d452fbb3c3a32fe85b1a3fbbcdec316d5fc940cefc028ee808ad25a15991c8"),
StateDiff: &core.StateDiff{
DeployedContracts: map[felt.Felt]*felt.Felt{
*contractAddr: classHash,
},
},
}, map[felt.Felt]core.Class{
*classHash: simpleClass,
}))

entryPoint := utils.HexToFelt(t, "0x39e11d48192e4333233c7eb19d10ad67c362bb28580c604d67884c85da39695")

_, err = New(nil).Call(&CallInfo{
ContractAddress: contractAddr,
ClassHash: classHash,
Selector: entryPoint,
}, &BlockInfo{Header: &core.Header{}}, testState, &utils.Mainnet, 0, true)
assert.ErrorContains(t, err, "RunResources has no remaining steps")
}

func TestExecute(t *testing.T) {
network := utils.Goerli2

Expand Down

0 comments on commit 8b790ec

Please sign in to comment.