From ed27bf0e37bdf83843122b6a983009c5904c036e Mon Sep 17 00:00:00 2001 From: Arnon Hod Date: Tue, 26 Nov 2024 13:31:53 +0200 Subject: [PATCH] chore(starknet_api): use lazylock to define query version base (#2278) --- .../deprecated_syscalls/deprecated_syscalls_test.rs | 5 ++--- .../syscalls/syscall_tests/get_execution_info.rs | 6 ++---- .../blockifier/src/transaction/transactions_test.rs | 6 ++---- crates/starknet_api/src/transaction.rs | 11 +++++++---- 4 files changed, 13 insertions(+), 15 deletions(-) diff --git a/crates/blockifier/src/execution/deprecated_syscalls/deprecated_syscalls_test.rs b/crates/blockifier/src/execution/deprecated_syscalls/deprecated_syscalls_test.rs index 3581c7f7ad..33aad71bbe 100644 --- a/crates/blockifier/src/execution/deprecated_syscalls/deprecated_syscalls_test.rs +++ b/crates/blockifier/src/execution/deprecated_syscalls/deprecated_syscalls_test.rs @@ -2,7 +2,6 @@ use std::collections::{HashMap, HashSet}; use cairo_vm::types::builtin_name::BuiltinName; use cairo_vm::vm::runners::cairo_runner::ExecutionResources; -use num_traits::Pow; use pretty_assertions::assert_eq; use rstest::rstest; use starknet_api::abi::abi_utils::selector_from_name; @@ -15,7 +14,7 @@ use starknet_api::transaction::{ EventKey, TransactionHash, TransactionVersion, - QUERY_VERSION_BASE_BIT, + QUERY_VERSION_BASE, }; use starknet_api::{calldata, felt, nonce, storage_key}; use starknet_types_core::felt::Felt; @@ -465,7 +464,7 @@ fn test_tx_info(#[values(false, true)] only_query: bool) { let mut state = test_state(&ChainInfo::create_for_testing(), Fee(0), &[(test_contract, 1)]); let mut version = felt!(1_u8); if only_query { - let simulate_version_base = Pow::pow(felt!(2_u8), QUERY_VERSION_BASE_BIT); + let simulate_version_base = *QUERY_VERSION_BASE; version += simulate_version_base; } let tx_hash = TransactionHash(felt!(1991_u16)); diff --git a/crates/blockifier/src/execution/syscalls/syscall_tests/get_execution_info.rs b/crates/blockifier/src/execution/syscalls/syscall_tests/get_execution_info.rs index ae4c09b15b..834ca9c530 100644 --- a/crates/blockifier/src/execution/syscalls/syscall_tests/get_execution_info.rs +++ b/crates/blockifier/src/execution/syscalls/syscall_tests/get_execution_info.rs @@ -1,5 +1,3 @@ -use cairo_vm::Felt252; -use num_traits::Pow; use starknet_api::abi::abi_utils::selector_from_name; use starknet_api::block::GasPrice; use starknet_api::core::ChainId; @@ -15,7 +13,7 @@ use starknet_api::transaction::fields::{ Tip, ValidResourceBounds, }; -use starknet_api::transaction::{TransactionHash, TransactionVersion, QUERY_VERSION_BASE_BIT}; +use starknet_api::transaction::{TransactionHash, TransactionVersion, QUERY_VERSION_BASE}; use starknet_api::{felt, nonce}; use starknet_types_core::felt::Felt; use test_case::test_case; @@ -197,7 +195,7 @@ fn test_get_execution_info( }; if only_query { - let simulate_version_base = Pow::pow(Felt252::from(2_u8), QUERY_VERSION_BASE_BIT); + let simulate_version_base = *QUERY_VERSION_BASE; let query_version = simulate_version_base + version.0; version = TransactionVersion(query_version); } diff --git a/crates/blockifier/src/transaction/transactions_test.rs b/crates/blockifier/src/transaction/transactions_test.rs index 5539a10e36..1ed376ab9f 100644 --- a/crates/blockifier/src/transaction/transactions_test.rs +++ b/crates/blockifier/src/transaction/transactions_test.rs @@ -5,7 +5,6 @@ use assert_matches::assert_matches; use cairo_vm::types::builtin_name::BuiltinName; use cairo_vm::vm::runners::cairo_runner::ExecutionResources; use num_bigint::BigUint; -use num_traits::Pow; use pretty_assertions::assert_eq; use rstest::{fixture, rstest}; use starknet_api::abi::abi_utils::{ @@ -40,7 +39,7 @@ use starknet_api::transaction::{ EventKey, L2ToL1Payload, TransactionVersion, - QUERY_VERSION_BASE_BIT, + QUERY_VERSION_BASE, }; use starknet_api::{ calldata, @@ -2159,8 +2158,7 @@ fn test_only_query_flag( ); let mut version = Felt::from(3_u8); if only_query { - let query_version_base = Felt::TWO.pow(QUERY_VERSION_BASE_BIT); - version += query_version_base; + version += *QUERY_VERSION_BASE; } let sender_address = account.get_instance_address(0); let test_contract_address = test_contract.get_instance_address(0); diff --git a/crates/starknet_api/src/transaction.rs b/crates/starknet_api/src/transaction.rs index ac6300cc39..975137d04d 100644 --- a/crates/starknet_api/src/transaction.rs +++ b/crates/starknet_api/src/transaction.rs @@ -1,3 +1,5 @@ +use std::sync::LazyLock; + use num_bigint::BigUint; use serde::{Deserialize, Serialize}; use starknet_types_core::felt::Felt; @@ -47,9 +49,10 @@ mod transaction_test; pub mod constants; pub mod fields; -// TODO(Noa, 14/11/2023): Replace QUERY_VERSION_BASE_BIT with a lazy calculation. -// pub static QUERY_VERSION_BASE: Lazy = ... -pub const QUERY_VERSION_BASE_BIT: u32 = 128; +pub static QUERY_VERSION_BASE: LazyLock = LazyLock::new(|| { + const QUERY_VERSION_BASE_BIT: u32 = 128; + Felt::TWO.pow(QUERY_VERSION_BASE_BIT) +}); pub trait TransactionHasher { fn calculate_transaction_hash( @@ -834,7 +837,7 @@ pub fn signed_tx_version( transaction_options: &TransactionOptions, ) -> TransactionVersion { // If only_query is true, set the 128-th bit. - let query_only_bit = Felt::TWO.pow(QUERY_VERSION_BASE_BIT); + let query_only_bit = *QUERY_VERSION_BASE; assert_eq!( tx_version.0.to_biguint() & query_only_bit.to_biguint(), BigUint::from(0_u8),