Skip to content

Commit

Permalink
test(batcher): set const block info for batcher test
Browse files Browse the repository at this point in the history
  • Loading branch information
ArniStarkware committed Dec 3, 2024
1 parent 616d561 commit 548bb28
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 28 deletions.
26 changes: 25 additions & 1 deletion crates/blockifier/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,15 @@ use std::path::PathBuf;
use cairo_vm::types::builtin_name::BuiltinName;
use cairo_vm::vm::runners::cairo_runner::ExecutionResources;
use starknet_api::abi::abi_utils::{get_fee_token_var_address, selector_from_name};
use starknet_api::block::{BlockHash, BlockHashAndNumber, BlockNumber, GasPrice, NonzeroGasPrice};
use starknet_api::block::{
BlockHash,
BlockHashAndNumber,
BlockNumber,
GasPrice,
GasPriceVector,
GasPrices,
NonzeroGasPrice,
};
use starknet_api::core::{ClassHash, ContractAddress};
use starknet_api::execution_resources::{GasAmount, GasVector};
use starknet_api::hash::StarkHash;
Expand Down Expand Up @@ -149,9 +157,25 @@ pub const DEFAULT_ETH_L1_DATA_GAS_PRICE: NonzeroGasPrice =
NonzeroGasPrice::new_unchecked(GasPrice(u128::pow(10, 6))); // Given in units of Wei.
pub const DEFAULT_STRK_L1_DATA_GAS_PRICE: NonzeroGasPrice =
NonzeroGasPrice::new_unchecked(GasPrice(u128::pow(10, 9))); // Given in units of STRK.
pub const DEFAULT_ETH_L2_GAS_PRICE: NonzeroGasPrice =
NonzeroGasPrice::new_unchecked(GasPrice(25 * u128::pow(10, 5)));
// TODO(Arni): Fix to: 25 * u128::pow(10, 5) when the gas price is given in STRK.
pub const DEFAULT_STRK_L2_GAS_PRICE: NonzeroGasPrice =
NonzeroGasPrice::new_unchecked(GasPrice(u128::pow(10, 9)));

pub const DEFAULT_GAS_PRICES: GasPrices = GasPrices {
eth_gas_prices: GasPriceVector {
l1_gas_price: DEFAULT_ETH_L1_GAS_PRICE,
l2_gas_price: DEFAULT_ETH_L2_GAS_PRICE,
l1_data_gas_price: DEFAULT_ETH_L1_DATA_GAS_PRICE,
},
strk_gas_prices: GasPriceVector {
l1_gas_price: DEFAULT_STRK_L1_GAS_PRICE,
l2_gas_price: DEFAULT_STRK_L2_GAS_PRICE,
l1_data_gas_price: DEFAULT_STRK_L1_DATA_GAS_PRICE,
},
};

// Deprecated transactions:
pub const MAX_FEE: Fee = DEFAULT_L1_GAS_AMOUNT.nonzero_saturating_mul(DEFAULT_ETH_L1_GAS_PRICE);

Expand Down
34 changes: 18 additions & 16 deletions crates/blockifier/src/test_utils/struct_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,26 +153,28 @@ pub trait BlockInfoExt {

impl BlockInfoExt for BlockInfo {
fn create_for_testing() -> Self {
let gas_prices = validated_gas_prices(
DEFAULT_ETH_L1_GAS_PRICE,
DEFAULT_STRK_L1_GAS_PRICE,
DEFAULT_ETH_L1_DATA_GAS_PRICE,
DEFAULT_STRK_L1_DATA_GAS_PRICE,
NonzeroGasPrice::new(
VersionedConstants::latest_constants()
.convert_l1_to_l2_gas_price_round_up(DEFAULT_ETH_L1_GAS_PRICE.into()),
)
.unwrap(),
NonzeroGasPrice::new(
VersionedConstants::latest_constants()
.convert_l1_to_l2_gas_price_round_up(DEFAULT_STRK_L1_GAS_PRICE.into()),
)
.unwrap(),
);
// TODO(Arni): assert_eq!(gas_prices, crate::test_utils::DEFAULT_GAS_PRICES);
Self {
block_number: BlockNumber(CURRENT_BLOCK_NUMBER),
block_timestamp: BlockTimestamp(CURRENT_BLOCK_TIMESTAMP),
sequencer_address: contract_address!(TEST_SEQUENCER_ADDRESS),
gas_prices: validated_gas_prices(
DEFAULT_ETH_L1_GAS_PRICE,
DEFAULT_STRK_L1_GAS_PRICE,
DEFAULT_ETH_L1_DATA_GAS_PRICE,
DEFAULT_STRK_L1_DATA_GAS_PRICE,
NonzeroGasPrice::new(
VersionedConstants::latest_constants()
.convert_l1_to_l2_gas_price_round_up(DEFAULT_ETH_L1_GAS_PRICE.into()),
)
.unwrap(),
NonzeroGasPrice::new(
VersionedConstants::latest_constants()
.convert_l1_to_l2_gas_price_round_up(DEFAULT_STRK_L1_GAS_PRICE.into()),
)
.unwrap(),
),
gas_prices,
use_kzg_da: false,
}
}
Expand Down
4 changes: 4 additions & 0 deletions crates/starknet_api/src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,10 @@ impl PatriciaKey {
pub fn key(&self) -> &StarkHash {
&self.0
}

pub const fn new_unchecked(key: StarkHash) -> Self {
PatriciaKey(key)
}
}

impl From<u128> for PatriciaKey {
Expand Down
26 changes: 15 additions & 11 deletions crates/starknet_batcher/src/batcher_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ use std::sync::Arc;
use assert_matches::assert_matches;
use async_trait::async_trait;
use blockifier::abi::constants;
use blockifier::test_utils::struct_impls::BlockInfoExt;
use blockifier::test_utils::{CURRENT_BLOCK_TIMESTAMP, DEFAULT_GAS_PRICES};
use chrono::Utc;
use futures::future::BoxFuture;
use futures::FutureExt;
use mockall::automock;
use mockall::predicate::{always, eq};
use rstest::rstest;
use starknet_api::block::{BlockInfo, BlockNumber};
use starknet_api::core::{ContractAddress, Nonce, StateDiffCommitment};
use starknet_api::block::{BlockInfo, BlockNumber, BlockTimestamp};
use starknet_api::core::{ContractAddress, Nonce, PatriciaKey, StateDiffCommitment};
use starknet_api::executable_transaction::Transaction;
use starknet_api::hash::PoseidonHash;
use starknet_api::hash::{PoseidonHash, StarkHash};
use starknet_api::state::ThinStateDiff;
use starknet_api::transaction::TransactionHash;
use starknet_api::{contract_address, felt, nonce, tx_hash};
Expand Down Expand Up @@ -62,10 +62,14 @@ const INITIAL_HEIGHT: BlockNumber = BlockNumber(3);
const STREAMING_CHUNK_SIZE: usize = 3;
const BLOCK_GENERATION_TIMEOUT: tokio::time::Duration = tokio::time::Duration::from_secs(1);
const PROPOSAL_ID: ProposalId = ProposalId(0);

fn initial_block_info() -> BlockInfo {
BlockInfo { block_number: INITIAL_HEIGHT, ..BlockInfo::create_for_testing() }
}
const INITIAL_BLOCK_INFO: BlockInfo = BlockInfo {
block_number: INITIAL_HEIGHT,
block_timestamp: BlockTimestamp(CURRENT_BLOCK_TIMESTAMP),
// TODO: Use test sequencer address.
sequencer_address: ContractAddress(PatriciaKey::new_unchecked(StarkHash::ONE)),
gas_prices: DEFAULT_GAS_PRICES,
use_kzg_da: false,
};

fn proposal_commitment() -> ProposalCommitment {
ProposalCommitment {
Expand Down Expand Up @@ -268,7 +272,7 @@ async fn validate_block_full_flow() {
proposal_id: PROPOSAL_ID,
deadline: deadline(),
retrospective_block_hash: None,
block_info: initial_block_info(),
block_info: INITIAL_BLOCK_INFO,
};
batcher.validate_block(validate_block_input).await.unwrap();

Expand Down Expand Up @@ -392,7 +396,7 @@ async fn send_finish_to_an_invalid_proposal() {
proposal_id: PROPOSAL_ID,
deadline: deadline(),
retrospective_block_hash: None,
block_info: initial_block_info(),
block_info: INITIAL_BLOCK_INFO,
};
batcher.validate_block(validate_block_input).await.unwrap();

Expand Down Expand Up @@ -429,7 +433,7 @@ async fn propose_block_full_flow() {
proposal_id: PROPOSAL_ID,
retrospective_block_hash: None,
deadline: chrono::Utc::now() + chrono::Duration::seconds(1),
block_info: initial_block_info(),
block_info: INITIAL_BLOCK_INFO,
})
.await
.unwrap();
Expand Down

0 comments on commit 548bb28

Please sign in to comment.