From 111d38eb75b614d7b4556024d4d7247ea4f3316a Mon Sep 17 00:00:00 2001 From: Arni Hod Date: Tue, 13 Feb 2024 09:52:11 +0200 Subject: [PATCH] test(execution): get_sequencer_address in test_validate_accounts_tx --- .../cairo0/account_faulty.cairo | 31 ++- .../compiled/account_faulty_compiled.json | 211 +++++++++++++----- .../blockifier/src/transaction/test_utils.rs | 1 + .../src/transaction/transactions_test.rs | 22 +- 4 files changed, 201 insertions(+), 64 deletions(-) diff --git a/crates/blockifier/feature_contracts/cairo0/account_faulty.cairo b/crates/blockifier/feature_contracts/cairo0/account_faulty.cairo index 5580090935..56adb828d1 100644 --- a/crates/blockifier/feature_contracts/cairo0/account_faulty.cairo +++ b/crates/blockifier/feature_contracts/cairo0/account_faulty.cairo @@ -5,7 +5,12 @@ from starkware.cairo.common.alloc import alloc from starkware.cairo.common.bool import FALSE, TRUE from starkware.cairo.common.cairo_builtins import HashBuiltin -from starkware.starknet.common.syscalls import TxInfo, call_contract, get_tx_info +from starkware.starknet.common.syscalls import ( + TxInfo, + call_contract, + get_sequencer_address, + get_tx_info +) from starkware.starknet.common.messages import send_message_to_l1 // Validate Scenarios. @@ -16,6 +21,8 @@ const VALID = 0; const INVALID = 1; // Make a contract call. const CALL_CONTRACT = 2; +// Use get_sequencer_address syscall. +const GET_SEQUENCER_ADDRESS = 7; // get_selector_from_name('foo'). const FOO_ENTRY_POINT_SELECTOR = ( @@ -84,16 +91,20 @@ func faulty_validate{syscall_ptr: felt*}() { assert 0 = 1; return (); } + if (scenario == CALL_CONTRACT) { + let contract_address = tx_info.signature[1]; + let (calldata: felt*) = alloc(); + call_contract( + contract_address=contract_address, + function_selector=FOO_ENTRY_POINT_SELECTOR, + calldata_size=0, + calldata=calldata, + ); + return (); + } - assert scenario = CALL_CONTRACT; - let contract_address = tx_info.signature[1]; - let (calldata: felt*) = alloc(); - call_contract( - contract_address=contract_address, - function_selector=FOO_ENTRY_POINT_SELECTOR, - calldata_size=0, - calldata=calldata, - ); + assert scenario = GET_SEQUENCER_ADDRESS; + let sequencer_address = get_sequencer_address(); return (); } diff --git a/crates/blockifier/feature_contracts/cairo0/compiled/account_faulty_compiled.json b/crates/blockifier/feature_contracts/cairo0/compiled/account_faulty_compiled.json index cdfe6c5f9f..72ded25e63 100644 --- a/crates/blockifier/feature_contracts/cairo0/compiled/account_faulty_compiled.json +++ b/crates/blockifier/feature_contracts/cairo0/compiled/account_faulty_compiled.json @@ -97,29 +97,29 @@ "entry_points_by_type": { "CONSTRUCTOR": [ { - "offset": 167, + "offset": 174, "selector": "0x28ffe4ff0f226a9107253e17a904099aa4f63a02a5621de0576e5aa71bc5194" } ], "EXTERNAL": [ { - "offset": 124, + "offset": 131, "selector": "0x15d40a3d6ca2ac30f4031e42be28da9b056fef9bb7357ac5e85627ee876e5ad" }, { - "offset": 87, + "offset": 94, "selector": "0x162da33a4585851fe8d3af3c2a9c60b557814e221e0d4f30ff0b2189d9c7775" }, { - "offset": 225, + "offset": 243, "selector": "0x1b1a0649752af1b28b3dc29a1556eee781e4a4c3a1f7f53f90fa834de098c4d" }, { - "offset": 35, + "offset": 42, "selector": "0x289da278a8dc833409cabfdad1581e8e7d40e42dcaed693fa4008dcdb4963b3" }, { - "offset": 59, + "offset": 66, "selector": "0x36fcbf06cd96843058359e1a75928beacfac10727dab22a3972f0af8aa92895" } ], @@ -149,6 +149,13 @@ "0x480280067ff98000", "0x208b7fff7fff7ffe", "0x480680017fff8000", + "0x47657453657175656e63657241646472657373", + "0x400280007ffd7fff", + "0x482680017ffd8000", + "0x2", + "0x480280017ffd8000", + "0x208b7fff7fff7ffe", + "0x480680017fff8000", "0x4765745478496e666f", "0x400280007ffd7fff", "0x482680017ffd8000", @@ -340,13 +347,15 @@ "0x48127ff87fff8000", "0x208b7fff7fff7ffe", "0x480080047ffa8000", - "0x480680017fff8000", - "0x2", - "0x400080007ffe7fff", + "0x480080007fff8000", + "0x482480017fff8000", + "0x800000000000010ffffffffffffffffffffffffffffffffffffffffffffffff", + "0x20680017fff7fff", + "0x10", "0x1104800180018000", - "0x800000000000010ffffffffffffffffffffffffffffffffffffffffffffff2f", - "0x480080047ff58000", - "0x48127ff37fff8000", + "0x800000000000010ffffffffffffffffffffffffffffffffffffffffffffff26", + "0x480080047ff48000", + "0x48127ff27fff8000", "0x480080017ffe8000", "0x480680017fff8000", "0x1b1a0649752af1b28b3dc29a1556eee781e4a4c3a1f7f53f90fa834de098c4d", @@ -354,9 +363,18 @@ "0x0", "0x48127ffa7fff8000", "0x1104800180018000", - "0x800000000000010ffffffffffffffffffffffffffffffffffffffffffffff28", + "0x800000000000010ffffffffffffffffffffffffffffffffffffffffffffff1f", "0x48127ffd7fff8000", "0x208b7fff7fff7ffe", + "0x480080047ff78000", + "0x480680017fff8000", + "0x7", + "0x400080007ffe7fff", + "0x48127ff47fff8000", + "0x1104800180018000", + "0x800000000000010ffffffffffffffffffffffffffffffffffffffffffffff22", + "0x48127ffe7fff8000", + "0x208b7fff7fff7ffe", "0x208b7fff7fff7ffe", "0x402b7ffd7ffc7ffd", "0x1104800180018000", @@ -408,6 +426,24 @@ } ], "18": [ + { + "accessible_scopes": [ + "starkware.starknet.common.syscalls", + "starkware.starknet.common.syscalls.get_sequencer_address" + ], + "code": "syscall_handler.get_sequencer_address(segments=segments, syscall_ptr=ids.syscall_ptr)", + "flow_tracking_data": { + "ap_tracking": { + "group": 2, + "offset": 1 + }, + "reference_ids": { + "starkware.starknet.common.syscalls.get_sequencer_address.syscall_ptr": 1 + } + } + } + ], + "25": [ { "accessible_scopes": [ "starkware.starknet.common.syscalls", @@ -416,16 +452,16 @@ "code": "syscall_handler.get_tx_info(segments=segments, syscall_ptr=ids.syscall_ptr)", "flow_tracking_data": { "ap_tracking": { - "group": 2, + "group": 3, "offset": 1 }, "reference_ids": { - "starkware.starknet.common.syscalls.get_tx_info.syscall_ptr": 1 + "starkware.starknet.common.syscalls.get_tx_info.syscall_ptr": 2 } } } ], - "28": [ + "35": [ { "accessible_scopes": [ "starkware.starknet.common.messages", @@ -434,16 +470,16 @@ "code": "syscall_handler.send_message_to_l1(segments=segments, syscall_ptr=ids.syscall_ptr)", "flow_tracking_data": { "ap_tracking": { - "group": 3, + "group": 4, "offset": 1 }, "reference_ids": { - "starkware.starknet.common.messages.send_message_to_l1.syscall_ptr": 2 + "starkware.starknet.common.messages.send_message_to_l1.syscall_ptr": 3 } } } ], - "42": [ + "49": [ { "accessible_scopes": [ "__main__", @@ -454,14 +490,14 @@ "code": "memory[ap] = segments.add()", "flow_tracking_data": { "ap_tracking": { - "group": 7, + "group": 8, "offset": 0 }, "reference_ids": {} } } ], - "68": [ + "75": [ { "accessible_scopes": [ "__main__", @@ -472,14 +508,14 @@ "code": "memory[ap] = segments.add()", "flow_tracking_data": { "ap_tracking": { - "group": 11, + "group": 12, "offset": 0 }, "reference_ids": {} } } ], - "103": [ + "110": [ { "accessible_scopes": [ "__main__", @@ -490,14 +526,14 @@ "code": "memory[ap] = segments.add()", "flow_tracking_data": { "ap_tracking": { - "group": 15, + "group": 16, "offset": 0 }, "reference_ids": {} } } ], - "144": [ + "151": [ { "accessible_scopes": [ "__main__", @@ -508,14 +544,14 @@ "code": "memory[ap] = segments.add()", "flow_tracking_data": { "ap_tracking": { - "group": 17, + "group": 18, "offset": 25 }, "reference_ids": {} } } ], - "176": [ + "183": [ { "accessible_scopes": [ "__main__", @@ -526,14 +562,14 @@ "code": "memory[ap] = segments.add()", "flow_tracking_data": { "ap_tracking": { - "group": 21, + "group": 22, "offset": 0 }, "reference_ids": {} } } ], - "228": [ + "246": [ { "accessible_scopes": [ "__main__", @@ -544,7 +580,7 @@ "code": "memory[ap] = segments.add()", "flow_tracking_data": { "ap_tracking": { - "group": 24, + "group": 25, "offset": 2 }, "reference_ids": {} @@ -565,6 +601,10 @@ "type": "const", "value": 766151770395363889994273252081996607712327869204808632459022800692259163213 }, + "__main__.GET_SEQUENCER_ADDRESS": { + "type": "const", + "value": 7 + }, "__main__.HashBuiltin": { "destination": "starkware.cairo.common.cairo_builtins.HashBuiltin", "type": "alias" @@ -589,7 +629,7 @@ "decorators": [ "external" ], - "pc": 114, + "pc": 121, "type": "function" }, "__main__.__execute__.Args": { @@ -646,7 +686,7 @@ "decorators": [ "external" ], - "pc": 77, + "pc": 84, "type": "function" }, "__main__.__validate__.Args": { @@ -695,7 +735,7 @@ "decorators": [ "external" ], - "pc": 31, + "pc": 38, "type": "function" }, "__main__.__validate_declare__.Args": { @@ -732,7 +772,7 @@ "decorators": [ "external" ], - "pc": 51, + "pc": 58, "type": "function" }, "__main__.__validate_deploy__.Args": { @@ -785,7 +825,7 @@ "decorators": [ "constructor" ], - "pc": 153, + "pc": 160, "type": "function" }, "__main__.constructor.Args": { @@ -828,7 +868,7 @@ }, "__main__.faulty_validate": { "decorators": [], - "pc": 185, + "pc": 192, "type": "function" }, "__main__.faulty_validate.Args": { @@ -860,7 +900,7 @@ "decorators": [ "external" ], - "pc": 224, + "pc": 242, "type": "function" }, "__main__.foo.Args": { @@ -883,6 +923,10 @@ "type": "const", "value": 0 }, + "__main__.get_sequencer_address": { + "destination": "starkware.starknet.common.syscalls.get_sequencer_address", + "type": "alias" + }, "__main__.get_tx_info": { "destination": "starkware.starknet.common.syscalls.get_tx_info", "type": "alias" @@ -895,7 +939,7 @@ "decorators": [ "external" ], - "pc": 124, + "pc": 131, "type": "function" }, "__wrappers__.__execute__.Args": { @@ -930,7 +974,7 @@ "decorators": [ "external" ], - "pc": 87, + "pc": 94, "type": "function" }, "__wrappers__.__validate__.Args": { @@ -965,7 +1009,7 @@ "decorators": [ "external" ], - "pc": 35, + "pc": 42, "type": "function" }, "__wrappers__.__validate_declare__.Args": { @@ -1000,7 +1044,7 @@ "decorators": [ "external" ], - "pc": 59, + "pc": 66, "type": "function" }, "__wrappers__.__validate_deploy__.Args": { @@ -1035,7 +1079,7 @@ "decorators": [ "constructor" ], - "pc": 167, + "pc": 174, "type": "function" }, "__wrappers__.constructor.Args": { @@ -1070,7 +1114,7 @@ "decorators": [ "external" ], - "pc": 225, + "pc": 243, "type": "function" }, "__wrappers__.foo.Args": { @@ -1374,7 +1418,7 @@ }, "starkware.starknet.common.messages.send_message_to_l1": { "decorators": [], - "pc": 22, + "pc": 29, "type": "function" }, "starkware.starknet.common.messages.send_message_to_l1.Args": { @@ -1421,18 +1465,18 @@ "references": [ { "ap_tracking_data": { - "group": 3, + "group": 4, "offset": 0 }, - "pc": 22, + "pc": 29, "value": "[cast(fp + (-6), felt**)]" }, { "ap_tracking_data": { - "group": 3, + "group": 4, "offset": 1 }, - "pc": 28, + "pc": 35, "value": "cast([fp + (-6)] + 4, felt*)" } ], @@ -2187,11 +2231,64 @@ ], "type": "reference" }, - "starkware.starknet.common.syscalls.get_tx_info": { + "starkware.starknet.common.syscalls.get_sequencer_address": { "decorators": [], "pc": 15, "type": "function" }, + "starkware.starknet.common.syscalls.get_sequencer_address.Args": { + "full_name": "starkware.starknet.common.syscalls.get_sequencer_address.Args", + "members": {}, + "size": 0, + "type": "struct" + }, + "starkware.starknet.common.syscalls.get_sequencer_address.ImplicitArgs": { + "full_name": "starkware.starknet.common.syscalls.get_sequencer_address.ImplicitArgs", + "members": { + "syscall_ptr": { + "cairo_type": "felt*", + "offset": 0 + } + }, + "size": 1, + "type": "struct" + }, + "starkware.starknet.common.syscalls.get_sequencer_address.Return": { + "cairo_type": "(sequencer_address: felt)", + "type": "type_definition" + }, + "starkware.starknet.common.syscalls.get_sequencer_address.SIZEOF_LOCALS": { + "type": "const", + "value": 0 + }, + "starkware.starknet.common.syscalls.get_sequencer_address.syscall_ptr": { + "cairo_type": "felt*", + "full_name": "starkware.starknet.common.syscalls.get_sequencer_address.syscall_ptr", + "references": [ + { + "ap_tracking_data": { + "group": 2, + "offset": 0 + }, + "pc": 15, + "value": "[cast(fp + (-3), felt**)]" + }, + { + "ap_tracking_data": { + "group": 2, + "offset": 1 + }, + "pc": 18, + "value": "cast([fp + (-3)] + 2, felt*)" + } + ], + "type": "reference" + }, + "starkware.starknet.common.syscalls.get_tx_info": { + "decorators": [], + "pc": 22, + "type": "function" + }, "starkware.starknet.common.syscalls.get_tx_info.Args": { "full_name": "starkware.starknet.common.syscalls.get_tx_info.Args", "members": {}, @@ -2223,18 +2320,18 @@ "references": [ { "ap_tracking_data": { - "group": 2, + "group": 3, "offset": 0 }, - "pc": 15, + "pc": 22, "value": "[cast(fp + (-3), felt**)]" }, { "ap_tracking_data": { - "group": 2, + "group": 3, "offset": 1 }, - "pc": 18, + "pc": 25, "value": "cast([fp + (-3)] + 2, felt*)" } ], @@ -2267,6 +2364,14 @@ "offset": 0 }, "pc": 22, + "value": "[cast(fp + (-3), felt**)]" + }, + { + "ap_tracking_data": { + "group": 4, + "offset": 0 + }, + "pc": 29, "value": "[cast(fp + (-6), felt**)]" } ] diff --git a/crates/blockifier/src/transaction/test_utils.rs b/crates/blockifier/src/transaction/test_utils.rs index 42eb5daebc..8cd6fcf4b1 100644 --- a/crates/blockifier/src/transaction/test_utils.rs +++ b/crates/blockifier/src/transaction/test_utils.rs @@ -43,6 +43,7 @@ pub const INVALID: u64 = 1; pub const CALL_CONTRACT: u64 = 2; pub const GET_BLOCK_HASH: u64 = 3; pub const GET_EXECUTION_INFO: u64 = 4; +pub const GET_SEQUENCER_ADDRESS: u64 = 7; macro_rules! impl_from_versioned_tx { ($(($specified_tx_type:ty, $enum_variant:ident)),*) => { diff --git a/crates/blockifier/src/transaction/transactions_test.rs b/crates/blockifier/src/transaction/transactions_test.rs index 4f494f06a2..cf0452792b 100644 --- a/crates/blockifier/src/transaction/transactions_test.rs +++ b/crates/blockifier/src/transaction/transactions_test.rs @@ -74,7 +74,7 @@ use crate::transaction::objects::{ use crate::transaction::test_utils::{ account_invoke_tx, calculate_class_info_for_testing, create_account_tx_for_validate_test, create_account_tx_test_state, l1_resource_bounds, FaultyAccountTxCreatorArgs, CALL_CONTRACT, - GET_BLOCK_HASH, GET_EXECUTION_INFO, INVALID, VALID, + GET_BLOCK_HASH, GET_EXECUTION_INFO, GET_SEQUENCER_ADDRESS, INVALID, VALID, }; use crate::transaction::transaction_types::TransactionType; use crate::transaction::transactions::{ExecutableTransaction, L1HandlerTransaction}; @@ -1436,6 +1436,23 @@ fn test_validate_accounts_tx( validate_constructor, ); } + if let CairoVersion::Cairo0 = cairo_version { + // Trying to use the syscall get_sequencer_address (forbidden). + let account_tx = create_account_tx_for_validate_test( + &mut NonceManager::default(), + FaultyAccountTxCreatorArgs { + scenario: GET_SEQUENCER_ADDRESS, + contract_address_salt: salt_manager.next_salt(), + ..default_args + }, + ); + let error = account_tx.execute(state, block_context, true, true).unwrap_err(); + check_transaction_execution_error_for_custom_hint!( + &error, + "Unauthorized syscall get_sequencer_address in execution mode Validate.", + validate_constructor, + ); + } // Positive flows. @@ -1465,6 +1482,9 @@ fn test_validate_accounts_tx( account_tx.execute(state, block_context, true, true).unwrap(); } + // TODO(Aner, 14/4/24): Cover positive flows of Cairo 0 syscalls: get_block_number and + // get_block_timestamp. + if let CairoVersion::Cairo1 = cairo_version { let account_tx = create_account_tx_for_validate_test( // Calling the syscall get_execution_info and get block_info which was modified for