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

Refactor env utils ci test (DO NOT MERGE) #457

Closed
wants to merge 13 commits into from
Closed
45 changes: 43 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/rpc-replay/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ tokio = { workspace = true }

[dev-dependencies]
rstest = { workspace = true }
serial_test = "3.2.0"
46 changes: 39 additions & 7 deletions crates/rpc-replay/src/block_context.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::env;
use std::num::NonZeroU128;

use blockifier::blockifier::block::{BlockInfo, GasPrices};
Expand All @@ -11,6 +12,16 @@ use starknet_api::{contract_address, felt, patricia_key};

use crate::utils::{felt_to_u128, FeltConversionError};

const DEFAULT_STRK_FEE_TOKEN_ADDRESS: &str = "0x04718f5a0fc34cc1af16a1cdee98ffb20c31f5cd61d6ab07201858f4287c938d";
const DEFAULT_ETH_FEE_TOKEN_ADDRESS: &str = "0x049d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7";

/// Fetches environment variable based on provided key.
/// If key is not found in env, it returns default value provided
#[inline]
fn get_env_var_or_default(key: &str, default: &str) -> String {
env::var(key).unwrap_or(default.to_string())
}

fn felt_to_gas_price(price: &Felt) -> Result<NonZeroU128, FeltConversionError> {
// Inspiration taken from Papyrus:
// https://github.com/starkware-libs/sequencer/blob/7218aa1f7ca3fe21c0a2bede2570820939ffe069/crates/papyrus_execution/src/lib.rs#L363-L371
Expand Down Expand Up @@ -49,16 +60,15 @@ pub fn build_block_context(
use_kzg_da,
};

let strk_fee_token_address = get_env_var_or_default("SNOS_STRK_FEE_TOKEN_ADDRESS", DEFAULT_STRK_FEE_TOKEN_ADDRESS);
let eth_fee_token_address = get_env_var_or_default("SNOS_ETH_FEE_TOKEN_ADDRESS", DEFAULT_ETH_FEE_TOKEN_ADDRESS);

let chain_info = ChainInfo {
chain_id,
// cf. https://docs.starknet.io/tools/important-addresses/
fee_token_addresses: FeeTokenAddresses {
strk_fee_token_address: contract_address!(
"0x04718f5a0fc34cc1af16a1cdee98ffb20c31f5cd61d6ab07201858f4287c938d"
),
eth_fee_token_address: contract_address!(
"0x049d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7"
),
strk_fee_token_address: contract_address!(strk_fee_token_address.as_str()),
eth_fee_token_address: contract_address!(eth_fee_token_address.as_str()),
},
};

Expand All @@ -70,12 +80,34 @@ pub fn build_block_context(

#[cfg(test)]
mod tests {

use starknet::core::types::{Felt, ResourcePrice};
use starknet_api::core::ChainId;

use super::*;

#[test]
fn test_get_env_var_or_default_existing() {
let test_key = "TEST_ENV_VAR_DEFAULT";
let test_value = "actual_value";
let default_value = "default_value";
env::set_var(test_key, test_value);

let result = get_env_var_or_default(test_key, default_value);
assert_eq!(result, test_value);

env::remove_var(test_key);
}

#[test]
fn test_get_env_var_or_default_non_existing() {
let test_key = "NON_EXISTING_VAR_DEFAULT";
let default_value = "default_value";
env::remove_var(test_key); // Ensure it doesn't exist

let result = get_env_var_or_default(test_key, default_value);
assert_eq!(result, default_value);
}

#[test]
fn test_build_block_context_with_zero_gas_prices() {
let chain_id = ChainId::Mainnet;
Expand Down
65 changes: 65 additions & 0 deletions crates/rpc-replay/tests/test_replay_block.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
use std::env;

use blockifier::blockifier::block::GasPrices;
use blockifier::state::cached_state::CachedState;
use blockifier::transaction::objects::FeeType;
use blockifier::transaction::transactions::ExecutableTransaction as _;
use blockifier::versioned_constants::StarknetVersion;
use rpc_client::RpcClient;
use rpc_replay::block_context::build_block_context;
use rpc_replay::rpc_state_reader::AsyncRpcStateReader;
use rpc_replay::transactions::starknet_rs_to_blockifier;
use rstest::rstest;
use serial_test::serial;
use starknet::core::types::{BlockId, BlockWithTxs};
use starknet::providers::Provider;
use starknet_api::core::ChainId;
Expand Down Expand Up @@ -63,3 +67,64 @@ async fn test_replay_block() {
}
}
}

#[rstest]
#[serial]
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
// wrong values: https://docs.starknet.io/tools/important-addresses/
#[case(
"0x0782f0ddca11d9950bc3220e35ac82cf868778edb67a5e58b39838544bc4cd0f",
"0x035c332b8de00874e702b4831c84b22281fb3246f714475496d74e644f35d492"
)]
async fn test_build_block_context_with_wrong_env_fails(
#[case] strk_token_address: String,
#[case] eth_token_address: String,
) {
let strk_token_key = "SNOS_STRK_FEE_TOKEN_ADDRESS";
env::set_var(strk_token_key, &strk_token_address);

let eth_token_key = "SNOS_ETH_FEE_TOKEN_ADDRESS";
env::set_var(eth_token_key, &eth_token_address);

let block_fixture = include_bytes!("./block_with_txs.json");
let block_with_txs: BlockWithTxs = serde_json::from_slice(block_fixture).unwrap();
let block_context = build_block_context(ChainId::Sepolia, &block_with_txs, StarknetVersion::V0_13_1)
.expect("Failed to build block context");

let strk_token_contract_address = block_context.chain_info().fee_token_address(&FeeType::Strk).to_string();
let eth_token_contract_address = block_context.chain_info().fee_token_address(&FeeType::Eth).to_string();

let correct_strk_token_address = "0x04718f5a0fc34cc1af16a1cdee98ffb20c31f5cd61d6ab07201858f4287c938d";
let correct_eth_token_address = "0x049d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7";

assert_ne!(correct_strk_token_address, strk_token_contract_address);
assert_ne!(correct_eth_token_address, eth_token_contract_address);

env::remove_var(strk_token_key);
env::remove_var(eth_token_key);
}

#[rstest]
#[serial]
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
// correct values: https://docs.starknet.io/tools/important-addresses/
async fn test_build_block_context_with_default_env_works() {
let strk_token_key = "SNOS_STRK_FEE_TOKEN_ADDRESS";
let strk_token_address = "0x04718f5a0fc34cc1af16a1cdee98ffb20c31f5cd61d6ab07201858f4287c938d";
env::remove_var(strk_token_key);

let eth_token_key = "SNOS_ETH_FEE_TOKEN_ADDRESS";
let eth_token_address = "0x049d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7";
env::remove_var(eth_token_key);

let block_fixture = include_bytes!("./block_with_txs.json");
let block_with_txs: BlockWithTxs = serde_json::from_slice(block_fixture).unwrap();
let block_context = build_block_context(ChainId::Sepolia, &block_with_txs, StarknetVersion::V0_13_1)
.expect("Failed to build block context");

let strk_token_contract_address = block_context.chain_info().fee_token_address(&FeeType::Strk).to_string();
let eth_token_contract_address = block_context.chain_info().fee_token_address(&FeeType::Eth).to_string();

assert_eq!(strk_token_address, strk_token_contract_address);
assert_eq!(eth_token_address, eth_token_contract_address);
}
Loading