Skip to content

Commit d6ad9ab

Browse files
committed
Update LOAD_NEXT_TX hint
1 parent 525a223 commit d6ad9ab

File tree

3 files changed

+54
-41
lines changed

3 files changed

+54
-41
lines changed

crates/starknet-os/src/hints/execution.rs

+53-19
Original file line numberDiff line numberDiff line change
@@ -40,26 +40,37 @@ use crate::starkware_utils::commitment_tree::update_tree::{DecodeNodeCase, TreeU
4040
use crate::utils::{custom_hint_error, execute_coroutine, get_constant};
4141

4242
pub const LOAD_NEXT_TX: &str = indoc! {r#"
43-
tx = next(transactions)
44-
assert tx.tx_type.name in ('INVOKE_FUNCTION', 'L1_HANDLER', 'DEPLOY_ACCOUNT', 'DECLARE'), (
45-
f"Unexpected transaction type: {tx.type.name}."
46-
)
43+
from src.starkware.starknet.core.os.transaction_hash.transaction_hash import (
44+
create_resource_bounds_list,
45+
)
46+
tx = next(transactions)
47+
assert tx.tx_type.name in ('INVOKE_FUNCTION', 'L1_HANDLER', 'DEPLOY_ACCOUNT', 'DECLARE'), (
48+
f"Unexpected transaction type: {tx.type.name}."
49+
)
4750
48-
tx_type_bytes = tx.tx_type.name.encode("ascii")
49-
ids.tx_type = int.from_bytes(tx_type_bytes, "big")
50-
execution_helper.os_logger.enter_tx(
51-
tx=tx,
52-
n_steps=current_step,
53-
builtin_ptrs=ids.builtin_ptrs,
54-
range_check_ptr=ids.range_check_ptr,
55-
)
51+
tx_type_bytes = tx.tx_type.name.encode("ascii")
52+
ids.tx_type = int.from_bytes(tx_type_bytes, "big")
53+
execution_helper.os_logger.enter_tx(
54+
tx=tx,
55+
n_steps=current_step,
56+
builtin_ptrs=ids.builtin_ptrs,
57+
range_check_ptr=ids.range_check_ptr,
58+
)
59+
60+
# Prepare a short callable to save code duplication.
61+
exit_tx = lambda: execution_helper.os_logger.exit_tx(
62+
n_steps=current_step,
63+
builtin_ptrs=ids.builtin_ptrs,
64+
range_check_ptr=ids.range_check_ptr,
65+
)
5666
57-
# Prepare a short callable to save code duplication.
58-
exit_tx = lambda: execution_helper.os_logger.exit_tx(
59-
n_steps=current_step,
60-
builtin_ptrs=ids.builtin_ptrs,
61-
range_check_ptr=ids.range_check_ptr,
62-
)"#
67+
# Guess the resource bounds.
68+
if tx.tx_type.name == 'L1_HANDLER' or tx.version < 3:
69+
ids.resource_bounds = 0
70+
ids.n_resource_bounds = 0
71+
else:
72+
ids.resource_bounds = segments.gen_arg(create_resource_bounds_list(tx.resource_bounds))
73+
ids.n_resource_bounds = len(tx.resource_bounds)"#
6374
};
6475
pub fn load_next_tx(
6576
vm: &mut VirtualMachine,
@@ -82,8 +93,31 @@ pub fn load_next_tx(
8293
vm,
8394
ids_data,
8495
ap_tracking,
85-
)
96+
)?;
97+
8698
// TODO: add logger
99+
100+
// Guess the resource bounds
101+
if tx.r#type == "L1_HANDLER" {
102+
insert_value_from_var_name(vars::ids::RESOURCE_BOUNDS, 0, vm, ids_data, ap_tracking)?;
103+
insert_value_from_var_name(vars::ids::N_RESOURCE_BOUNDS, 0, vm, ids_data, ap_tracking)?;
104+
} else {
105+
let resource_bounds_base = vm.add_memory_segment();
106+
let resource_bounds = tx.resource_bounds.ok_or_else(|| {
107+
HintError::CustomHint("Transaction should have resource bounds".to_owned().into_boxed_str())
108+
})?;
109+
let resource_bounds_list = create_resource_bounds_list(&resource_bounds);
110+
let resource_bounds_list_data = resource_bounds_list.iter().map(|felt| {
111+
MaybeRelocatable::Int(*felt)
112+
})
113+
.collect::<Vec<_>>();
114+
vm.load_data(resource_bounds_base, &resource_bounds_list_data[..])?;
115+
116+
insert_value_from_var_name(vars::ids::RESOURCE_BOUNDS, resource_bounds_base, vm, ids_data, ap_tracking)?;
117+
insert_value_from_var_name(vars::ids::N_RESOURCE_BOUNDS, resource_bounds_list_data.len(), vm, ids_data, ap_tracking)?;
118+
}
119+
120+
Ok(())
87121
}
88122

89123
pub const EXIT_TX: &str = "exit_tx()";

crates/starknet-os/src/hints/orphaned.rs

-22
Original file line numberDiff line numberDiff line change
@@ -38,28 +38,6 @@ pub const HINT_7: &str = indoc! {r#"memory[ap] = to_felt_or_relocatable(0 if tx.
3838
// ?
3939
pub const HINT_8: &str = indoc! {r#"memory[fp + 19] = to_felt_or_relocatable(os_input.full_output)"#};
4040

41-
// this hint grew into "HINT_19"
42-
pub const HINT_9: &str = indoc! {r#"tx = next(transactions)
43-
assert tx.tx_type.name in ('INVOKE_FUNCTION', 'L1_HANDLER', 'DEPLOY_ACCOUNT', 'DECLARE'), (
44-
f"Unexpected transaction type: {tx.type.name}."
45-
)
46-
47-
tx_type_bytes = tx.tx_type.name.encode("ascii")
48-
ids.tx_type = int.from_bytes(tx_type_bytes, "big")
49-
execution_helper.os_logger.enter_tx(
50-
tx=tx,
51-
n_steps=current_step,
52-
builtin_ptrs=ids.builtin_ptrs,
53-
range_check_ptr=ids.range_check_ptr,
54-
)
55-
56-
# Prepare a short callable to save code duplication.
57-
exit_tx = lambda: execution_helper.os_logger.exit_tx(
58-
n_steps=current_step,
59-
builtin_ptrs=ids.builtin_ptrs,
60-
range_check_ptr=ids.range_check_ptr,
61-
)"#};
62-
6341
// this hint looks like it grew into "HINT_22", but it also moved to a different fn (?) or at least
6442
// different part of the same fn
6543
pub const HINT_10: &str = indoc! {r#"from starkware.starknet.core.os.contract_class.compiled_class_hash import (

crates/starknet-os/src/hints/vars.rs

+1
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ pub mod ids {
102102
pub const N_BUILTINS: &str = "n_builtins";
103103
pub const N_ELMS: &str = "n_elms";
104104
pub const N_COMPILED_CLASS_FACTS: &str = "n_compiled_class_facts";
105+
pub const N_RESOURCE_BOUNDS: &str = "n_resource_bounds";
105106
pub const N_SELECTED_BUILTINS: &str = "n_selected_builtins";
106107
pub const N_TXS: &str = "n_txs";
107108
pub const N_UPDATES: &str = "n_updates";

0 commit comments

Comments
 (0)