Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: buggy ptr inget_tx_info syscall inside validate_declare for account contracts #441

Merged
merged 6 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions crates/bin/prove_block/tests/prove_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ const DEFAULT_COMPILED_OS: &[u8] = include_bytes!("../../../../build/os_latest.j
#[case::peekable_peek_is_none(174156)]
#[case::no_more_storage_reads_available(161884)]
#[case::no_more_storage_reads_available(174027)]
#[case::memory_addresses_must_be_relocatable(202083)]
#[case::memory_invalid_signature(216914)]
#[case::diff_assert_values(218624)]
#[case::could_nt_compute_operand_op1(204337)]
#[ignore = "Requires a running Pathfinder node"]
#[tokio::test(flavor = "multi_thread")]
async fn test_prove_selected_blocks(#[case] block_number: u64) {
Expand Down
7 changes: 4 additions & 3 deletions crates/starknet-os/src/hints/execute_transactions.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::collections::HashMap;

use cairo_vm::hint_processor::builtin_hint_processor::hint_utils::{
get_integer_from_var_name, get_ptr_from_var_name, get_relocatable_from_var_name, insert_value_from_var_name,
get_integer_from_var_name, get_ptr_from_var_name, insert_value_from_var_name,
};
use cairo_vm::hint_processor::builtin_hint_processor::sha256_utils::sha256_finalize;
use cairo_vm::hint_processor::hint_processor_definition::HintReference;
Expand Down Expand Up @@ -38,8 +38,9 @@ where
{
let execution_helper: ExecutionHelperWrapper<PCS> = exec_scopes.get(vars::scopes::EXECUTION_HELPER)?;
let execution_context_ptr =
get_relocatable_from_var_name(vars::ids::VALIDATE_DECLARE_EXECUTION_CONTEXT, vm, ids_data, ap_tracking)?;
let deprecated_tx_info_ptr = (execution_context_ptr + ExecutionContext::deprecated_tx_info_offset())?;
get_ptr_from_var_name(vars::ids::VALIDATE_DECLARE_EXECUTION_CONTEXT, vm, ids_data, ap_tracking)?;
let deprecated_tx_info_ptr =
vm.get_relocatable((execution_context_ptr + ExecutionContext::deprecated_tx_info_offset())?)?;

execution_helper.start_tx(Some(deprecated_tx_info_ptr)).await;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ from starkware.starknet.common.syscalls import (
)

@external
func __validate_declare__(class_hash: felt) {
func __validate_declare__{syscall_ptr: felt*}(class_hash: felt) {
let (tx_info) = get_tx_info();
with_attr error_message("assertion failed") {
assert tx_info.signature_len = 0;
}
return ();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,33 +86,51 @@
"CONSTRUCTOR": [],
"EXTERNAL": [
{
"offset": 100,
"offset": 108,
"selector": "0x15d40a3d6ca2ac30f4031e42be28da9b056fef9bb7357ac5e85627ee876e5ad"
},
{
"offset": 61,
"offset": 69,
"selector": "0x162da33a4585851fe8d3af3c2a9c60b557814e221e0d4f30ff0b2189d9c7775"
},
{
"offset": 20,
"offset": 27,
"selector": "0x289da278a8dc833409cabfdad1581e8e7d40e42dcaed693fa4008dcdb4963b3"
},
{
"offset": 43,
"offset": 51,
"selector": "0x36fcbf06cd96843058359e1a75928beacfac10727dab22a3972f0af8aa92895"
}
],
"L1_HANDLER": []
},
"program": {
"attributes": [
{
"accessible_scopes": [
"__main__",
"__main__",
"__main__.__validate_declare__"
],
"end_pc": 25,
"flow_tracking_data": {
"ap_tracking": {
"group": 2,
"offset": 6
},
"reference_ids": {}
},
"name": "error_message",
"start_pc": 22,
"value": "assertion failed"
},
{
"accessible_scopes": [
"__main__",
"__main__",
"__main__.__validate_deploy__"
],
"end_pc": 41,
"end_pc": 49,
"flow_tracking_data": {
"ap_tracking": {
"group": 4,
Expand All @@ -121,7 +139,7 @@
"reference_ids": {}
},
"name": "error_message",
"start_pc": 38,
"start_pc": 46,
"value": "assertion failed"
}
],
Expand Down Expand Up @@ -150,16 +168,24 @@
"0x2",
"0x480280017ffd8000",
"0x208b7fff7fff7ffe",
"0x480a7ffc7fff8000",
"0x1104800180018000",
"0x800000000000010fffffffffffffffffffffffffffffffffffffffffffffff9",
"0x480680017fff8000",
"0x0",
"0x400080037ffe7fff",
"0x48127ffd7fff8000",
"0x208b7fff7fff7ffe",
"0x482680017ffd8000",
"0x1",
"0x402a7ffd7ffc7fff",
"0x480280007ffb8000",
"0x480280007ffd8000",
"0x1104800180018000",
"0x800000000000010fffffffffffffffffffffffffffffffffffffffffffffffc",
"0x800000000000010fffffffffffffffffffffffffffffffffffffffffffffff4",
"0x40780017fff7fff",
"0x1",
"0x480280007ffb8000",
"0x48127ffe7fff8000",
"0x480280017ffb8000",
"0x480280027ffb8000",
"0x480680017fff8000",
Expand All @@ -168,7 +194,7 @@
"0x208b7fff7fff7ffe",
"0x480a7ffb7fff8000",
"0x1104800180018000",
"0x800000000000010ffffffffffffffffffffffffffffffffffffffffffffffe9",
"0x800000000000010ffffffffffffffffffffffffffffffffffffffffffffffe1",
"0x480680017fff8000",
"0x0",
"0x400080037ffe7fff",
Expand Down Expand Up @@ -224,7 +250,7 @@
"0x480a7ffc7fff8000",
"0x480a7ffd7fff8000",
"0x1104800180018000",
"0x800000000000010ffffffffffffffffffffffffffffffffffffffffffffffa5",
"0x800000000000010ffffffffffffffffffffffffffffffffffffffffffffff9d",
"0x48127ffd7fff8000",
"0x480a7ff87fff8000",
"0x480a7ff97fff8000",
Expand Down Expand Up @@ -291,7 +317,7 @@
}
}
],
"26": [
"34": [
{
"accessible_scopes": [
"__main__",
Expand All @@ -303,13 +329,13 @@
"flow_tracking_data": {
"ap_tracking": {
"group": 3,
"offset": 4
"offset": 13
},
"reference_ids": {}
}
}
],
"51": [
"59": [
{
"accessible_scopes": [
"__main__",
Expand All @@ -327,7 +353,7 @@
}
}
],
"76": [
"84": [
{
"accessible_scopes": [
"__main__",
Expand Down Expand Up @@ -360,7 +386,7 @@
"external",
"raw_output"
],
"pc": 87,
"pc": 95,
"type": "function"
},
"__main__.__execute__.Args": {
Expand Down Expand Up @@ -417,7 +443,7 @@
"decorators": [
"external"
],
"pc": 60,
"pc": 68,
"type": "function"
},
"__main__.__validate__.Args": {
Expand Down Expand Up @@ -477,8 +503,13 @@
},
"__main__.__validate_declare__.ImplicitArgs": {
"full_name": "__main__.__validate_declare__.ImplicitArgs",
"members": {},
"size": 0,
"members": {
"syscall_ptr": {
"cairo_type": "felt*",
"offset": 0
}
},
"size": 1,
"type": "struct"
},
"__main__.__validate_declare__.Return": {
Expand All @@ -493,7 +524,7 @@
"decorators": [
"external"
],
"pc": 35,
"pc": 43,
"type": "function"
},
"__main__.__validate_deploy__.Args": {
Expand Down Expand Up @@ -543,7 +574,7 @@
"external",
"raw_output"
],
"pc": 100,
"pc": 108,
"type": "function"
},
"__wrappers__.__execute__.Args": {
Expand Down Expand Up @@ -578,7 +609,7 @@
"decorators": [
"external"
],
"pc": 61,
"pc": 69,
"type": "function"
},
"__wrappers__.__validate__.Args": {
Expand Down Expand Up @@ -613,7 +644,7 @@
"decorators": [
"external"
],
"pc": 20,
"pc": 27,
"type": "function"
},
"__wrappers__.__validate_declare__.Args": {
Expand All @@ -629,7 +660,7 @@
"type": "struct"
},
"__wrappers__.__validate_declare__.Return": {
"cairo_type": "(syscall_ptr: felt, pedersen_ptr: felt, range_check_ptr: felt, size: felt, retdata: felt*)",
"cairo_type": "(syscall_ptr: felt*, pedersen_ptr: felt, range_check_ptr: felt, size: felt, retdata: felt*)",
"type": "type_definition"
},
"__wrappers__.__validate_declare__.SIZEOF_LOCALS": {
Expand All @@ -648,7 +679,7 @@
"decorators": [
"external"
],
"pc": 43,
"pc": 51,
"type": "function"
},
"__wrappers__.__validate_deploy__.Args": {
Expand Down
68 changes: 68 additions & 0 deletions tests/integration/declare_txn_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,11 @@ async fn initial_state_declare_cairo0(
#[from(init_logging)] _logging: (),
) -> StarknetTestState {
let account_with_dummy_validate = load_cairo0_feature_contract("account_with_dummy_validate");
let account_with_tx_info_check = load_cairo0_feature_contract("account_with_tx_info_check");

StarknetStateBuilder::new(&block_context)
.deploy_cairo0_contract(account_with_dummy_validate.0, account_with_dummy_validate.1)
.deploy_cairo0_contract(account_with_tx_info_check.0, account_with_tx_info_check.1)
.set_default_balance(BALANCE, BALANCE)
.build()
.await
Expand Down Expand Up @@ -220,3 +222,69 @@ async fn declare_v1_cairo0_account(
.await
.expect("OS run failed");
}

#[rstest]
// We need to use the multi_thread runtime to use task::block_in_place for sync -> async calls.
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
async fn declare_cairo0_with_tx_info(
#[future] initial_state_declare_cairo0: StarknetTestState,
block_context: BlockContext,
max_fee: Fee,
) {
let tx_version = TransactionVersion::THREE;

let mut nonce_manager = NonceManager::default();

let initial_state = initial_state_declare_cairo0.await;
let sender_address = initial_state.deployed_cairo0_contracts.get("account_with_tx_info_check").unwrap().address;

// We want to declare a fresh (never-before-declared) contract, so we don't want to reuse
// anything from the test fixtures, and we need to do it "by hand". The transaction will
// error if the class trie already contains the class we are trying to deploy.
let (_, sierra_class, casm_class) = load_cairo1_feature_contract("test_contract");

// We also need to write the class and compiled class facts so that the FFC will contain them
// during block re-execution.
let mut ffc = initial_state.clone_ffc::<PoseidonHash>();
let (contract_class_hash, compiled_class_hash) =
write_class_facts(sierra_class.clone().into(), casm_class.clone(), &mut ffc).await.unwrap();

let contract_class = casm_class.to_blockifier_contract_class().unwrap();
let class_hash = starknet_api::core::ClassHash::from(contract_class_hash);
let compiled_class_hash = CompiledClassHash::from(compiled_class_hash);

let sierra_program_len = sierra_class.sierra_program.len();
let generic_sierra_class = GenericSierraContractClass::from(sierra_class);
let flattened_sierra_class = generic_sierra_class.to_starknet_core_contract_class().unwrap();

let class_hash_component_hashes =
HashMap::from([(class_hash, ContractClassComponentHashes::from(flattened_sierra_class))]);

let class_info = ClassInfo::new(&contract_class.into(), sierra_program_len, 0).unwrap();

let declare_tx = blockifier::test_utils::declare::declare_tx(
declare_tx_args! {
max_fee,
sender_address,
version: tx_version,
nonce: nonce_manager.next(sender_address),
class_hash: class_hash,
compiled_class_hash,
resource_bounds: default_testing_resource_bounds(),
},
class_info,
);

let txs = vec![declare_tx].into_iter().map(Into::into).collect();
let _result = execute_txs_and_run_os(
crate::common::DEFAULT_COMPILED_OS,
initial_state.cached_state,
block_context,
txs,
initial_state.cairo0_compiled_classes,
initial_state.cairo1_compiled_classes,
class_hash_component_hashes,
)
.await
.expect("OS run failed");
}
2 changes: 1 addition & 1 deletion tests/integration/deploy_txn_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ async fn deploy_via_invoke_no_calldata_cairo1_account(
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
async fn deploy_cairo0_check_get_info_call(block_context: BlockContext, max_fee: Fee) {
let account_with_tx_info_check = load_cairo0_feature_contract("account_with_tx_info_check");
let class_hash = class_hash!("0x6c8903651a5f89ffc304621a7d8106a0324cc28aca04934fcbbb4398d5c8bc8");
let class_hash = class_hash!("0x479f265ce303d47bbfb7b17995017e1e77bd2c131a6651ed484f19e0dede22d");

let ctor_calldata = Calldata::default();
let deployed_contract_address = calculate_contract_address(
Expand Down
Loading