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

chore(starknet_api): use lazylock to define query version base #2278

Merged
merged 1 commit into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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));
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down
6 changes: 2 additions & 4 deletions crates/blockifier/src/transaction/transactions_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -40,7 +39,7 @@ use starknet_api::transaction::{
EventKey,
L2ToL1Payload,
TransactionVersion,
QUERY_VERSION_BASE_BIT,
QUERY_VERSION_BASE,
};
use starknet_api::{
calldata,
Expand Down Expand Up @@ -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);
Expand Down
11 changes: 7 additions & 4 deletions crates/starknet_api/src/transaction.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::sync::LazyLock;

use num_bigint::BigUint;
use serde::{Deserialize, Serialize};
use starknet_types_core::felt::Felt;
Expand Down Expand Up @@ -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<Felt> = ...
pub const QUERY_VERSION_BASE_BIT: u32 = 128;
pub static QUERY_VERSION_BASE: LazyLock<Felt> = LazyLock::new(|| {
const QUERY_VERSION_BASE_BIT: u32 = 128;
Felt::TWO.pow(QUERY_VERSION_BASE_BIT)
});

pub trait TransactionHasher {
fn calculate_transaction_hash(
Expand Down Expand Up @@ -820,7 +823,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),
Expand Down
Loading