Skip to content

Commit 8b790ec

Browse files
authored
Pass max_steps flag value to blockifier code (#1760)
1 parent 1329f05 commit 8b790ec

File tree

2 files changed

+51
-4
lines changed

2 files changed

+51
-4
lines changed

vm/rust/src/lib.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ pub extern "C" fn cairoVMCall(
7474
block_info_ptr: *const BlockInfo,
7575
reader_handle: usize,
7676
chain_id: *const c_char,
77-
_max_steps: c_ulonglong, //todo: enforce this
77+
max_steps: c_ulonglong,
7878
) {
7979
let block_info = unsafe { *block_info_ptr };
8080
let call_info = unsafe { *call_info_ptr };
@@ -114,7 +114,7 @@ pub extern "C" fn cairoVMCall(
114114
let mut resources = ExecutionResources::default();
115115
let context = EntryPointExecutionContext::new_invoke(
116116
Arc::new(TransactionContext {
117-
block_context: build_block_context(&mut state, &block_info, chain_id_str),
117+
block_context: build_block_context(&mut state, &block_info, chain_id_str, Some(max_steps)),
118118
tx_info: TransactionInfo::Deprecated(DeprecatedTransactionInfo::default()),
119119
}),
120120
false,
@@ -189,7 +189,7 @@ pub extern "C" fn cairoVMExecute(
189189
let mut state = CachedState::new(reader, GlobalContractCache::new(1));
190190
let txns_and_query_bits = txns_and_query_bits.unwrap();
191191
let mut classes = classes.unwrap();
192-
let block_context: BlockContext = build_block_context(&mut state, &block_info, chain_id_str);
192+
let block_context: BlockContext = build_block_context(&mut state, &block_info, chain_id_str, None);
193193
let charge_fee = skip_charge_fee == 0;
194194
let validate = skip_validate == 0;
195195

@@ -378,6 +378,7 @@ fn build_block_context(
378378
state: &mut dyn State,
379379
block_info: &BlockInfo,
380380
chain_id_str: &str,
381+
max_steps: Option<c_ulonglong>,
381382
) -> BlockContext {
382383
let sequencer_addr = StarkFelt::new(block_info.sequencer_address).unwrap();
383384
let gas_price_wei_felt = StarkFelt::new(block_info.gas_price_wei).unwrap();
@@ -393,6 +394,11 @@ fn build_block_context(
393394
hash: BlockHash(StarkFelt::new(block_info.block_hash_to_be_revealed).unwrap()),
394395
})
395396
}
397+
let mut constants = get_versioned_constants(block_info.version);
398+
if let Some(max_steps) = max_steps {
399+
constants.invoke_tx_max_n_steps = max_steps as u32;
400+
}
401+
396402
pre_process_block(state, old_block_number_and_hash, BlockifierBlockInfo{
397403
block_number: starknet_api::block::BlockNumber(block_info.block_number),
398404
block_timestamp: starknet_api::block::BlockTimestamp(block_info.block_timestamp),
@@ -411,7 +417,7 @@ fn build_block_context(
411417
eth_fee_token_address: ContractAddress::try_from(StarkHash::try_from("0x049d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7").unwrap()).unwrap(),
412418
strk_fee_token_address: ContractAddress::try_from(StarkHash::try_from("0x04718f5a0fc34cc1af16a1cdee98ffb20c31f5cd61d6ab07201858f4287c938d").unwrap()).unwrap(),
413419
},
414-
}, get_versioned_constants(block_info.version)).unwrap()
420+
}, constants).unwrap()
415421
}
416422

417423

vm/vm_test.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,47 @@ func TestV1Call(t *testing.T) {
148148
assert.Equal(t, []*felt.Felt{new(felt.Felt).SetUint64(37)}, ret)
149149
}
150150

151+
func TestCall_MaxSteps(t *testing.T) {
152+
testDB := pebble.NewMemTest(t)
153+
txn, err := testDB.NewTransaction(true)
154+
require.NoError(t, err)
155+
client := feeder.NewTestClient(t, &utils.Mainnet)
156+
gw := adaptfeeder.New(client)
157+
t.Cleanup(func() {
158+
require.NoError(t, txn.Discard())
159+
})
160+
161+
contractAddr := utils.HexToFelt(t, "0xDEADBEEF")
162+
// https://voyager.online/class/0x03297a93c52357144b7da71296d7e8231c3e0959f0a1d37222204f2f7712010e
163+
classHash := utils.HexToFelt(t, "0x3297a93c52357144b7da71296d7e8231c3e0959f0a1d37222204f2f7712010e")
164+
simpleClass, err := gw.Class(context.Background(), classHash)
165+
require.NoError(t, err)
166+
167+
encoder.RegisterType(reflect.TypeOf(core.Cairo0Class{})) //nolint:errcheck
168+
169+
testState := core.NewState(txn)
170+
require.NoError(t, testState.Update(0, &core.StateUpdate{
171+
OldRoot: &felt.Zero,
172+
NewRoot: utils.HexToFelt(t, "0x3d452fbb3c3a32fe85b1a3fbbcdec316d5fc940cefc028ee808ad25a15991c8"),
173+
StateDiff: &core.StateDiff{
174+
DeployedContracts: map[felt.Felt]*felt.Felt{
175+
*contractAddr: classHash,
176+
},
177+
},
178+
}, map[felt.Felt]core.Class{
179+
*classHash: simpleClass,
180+
}))
181+
182+
entryPoint := utils.HexToFelt(t, "0x39e11d48192e4333233c7eb19d10ad67c362bb28580c604d67884c85da39695")
183+
184+
_, err = New(nil).Call(&CallInfo{
185+
ContractAddress: contractAddr,
186+
ClassHash: classHash,
187+
Selector: entryPoint,
188+
}, &BlockInfo{Header: &core.Header{}}, testState, &utils.Mainnet, 0, true)
189+
assert.ErrorContains(t, err, "RunResources has no remaining steps")
190+
}
191+
151192
func TestExecute(t *testing.T) {
152193
network := utils.Goerli2
153194

0 commit comments

Comments
 (0)