Skip to content

Commit

Permalink
chore(katana): address! macro for creating ContractAddress instan…
Browse files Browse the repository at this point in the history
…ce (#2475)
  • Loading branch information
kariy authored Sep 25, 2024
1 parent 731ff47 commit 8453b63
Show file tree
Hide file tree
Showing 13 changed files with 234 additions and 266 deletions.
29 changes: 12 additions & 17 deletions bin/saya/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use std::time::Duration;
use cairo_proof_parser::output::extract_output;
use katana_primitives::contract::ContractAddress;
use katana_primitives::state::StateUpdates;
use katana_primitives::{address, felt};
use saya_core::prover::extract::program_input_from_program_output;
use saya_core::prover::{
prove_diff, HttpProverParams, MessageToAppchain, MessageToStarknet, ProgramInput, ProveProgram,
Expand Down Expand Up @@ -43,27 +44,27 @@ async fn test_program_input_from_program_output() -> anyhow::Result<()> {
config_hash: Felt::from_str("104").unwrap(),
message_to_starknet_segment: vec![
MessageToStarknet {
from_address: ContractAddress::from(Felt::from_str("105").unwrap()),
to_address: ContractAddress::from(Felt::from_str("106").unwrap()),
from_address: address!("105"),
to_address: address!("106"),
payload: vec![Felt::from_str("107").unwrap()],
},
MessageToStarknet {
from_address: ContractAddress::from(Felt::from_str("105").unwrap()),
to_address: ContractAddress::from(Felt::from_str("106").unwrap()),
from_address: address!("105"),
to_address: address!("106"),
payload: vec![Felt::from_str("107").unwrap()],
},
],
message_to_appchain_segment: vec![
MessageToAppchain {
from_address: ContractAddress::from(Felt::from_str("108").unwrap()),
to_address: ContractAddress::from(Felt::from_str("109").unwrap()),
from_address: address!("108"),
to_address: address!("109"),
nonce: Felt::from_str("110").unwrap(),
selector: Felt::from_str("111").unwrap(),
payload: vec![Felt::from_str("112").unwrap()],
},
MessageToAppchain {
from_address: ContractAddress::from(Felt::from_str("108").unwrap()),
to_address: ContractAddress::from(Felt::from_str("109").unwrap()),
from_address: address!("108"),
to_address: address!("109"),
nonce: Felt::from_str("110").unwrap(),
selector: Felt::from_str("111").unwrap(),
payload: vec![Felt::from_str("112").unwrap()],
Expand All @@ -72,24 +73,18 @@ async fn test_program_input_from_program_output() -> anyhow::Result<()> {
state_updates: StateUpdates {
nonce_updates: {
let mut map = std::collections::BTreeMap::new();
map.insert(
ContractAddress::from(Felt::from_str("1111").unwrap()),
Felt::from_str("22222").unwrap(),
);
map.insert(address!("1111"), felt!("22222"));
map
},
storage_updates: vec![(
ContractAddress::from(Felt::from_str("333")?),
address!("333"),
vec![(Felt::from_str("4444")?, Felt::from_str("555")?)].into_iter().collect(),
)]
.into_iter()
.collect(),
deployed_contracts: {
let mut map = std::collections::BTreeMap::new();
map.insert(
ContractAddress::from(Felt::from_str("66666").unwrap()),
Felt::from_str("7777").unwrap(),
);
map.insert(address!("66666"), felt!("7777"));
map
},
declared_classes: {
Expand Down
14 changes: 7 additions & 7 deletions crates/katana/executor/src/implementation/blockifier/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ mod tests {
DEFAULT_OZ_ACCOUNT_CONTRACT_CASM,
};
use katana_primitives::utils::class::{parse_compiled_class, parse_sierra_class};
use katana_primitives::Felt;
use katana_primitives::{address, Felt};
use katana_provider::providers::in_memory::InMemoryProvider;
use katana_provider::traits::contract::ContractClassWriter;
use katana_provider::traits::state::{StateFactoryProvider, StateProvider, StateWriter};
Expand All @@ -256,7 +256,7 @@ mod tests {
}

fn state_provider() -> Box<dyn StateProvider> {
let address = ContractAddress::from(felt!("0x67"));
let address = address!("0x67");
let nonce = felt!("0x7");
let storage_key = felt!("0x1");
let storage_value = felt!("0x2");
Expand Down Expand Up @@ -284,7 +284,7 @@ mod tests {
let state = state_provider();
let cached_state = CachedState::new(StateProviderDb::new(state));

let address = ContractAddress::from(felt!("0x67"));
let address = address!("0x67");
let legacy_class_hash = felt!("0x111");
let storage_key = felt!("0x1");

Expand Down Expand Up @@ -319,7 +319,7 @@ mod tests {
let sp = state_provider();

// cache_state native data
let new_address = ContractAddress::from(felt!("0xdead"));
let new_address = address!("0xdead");
let new_storage_key = felt!("0xf00");
let new_storage_value = felt!("0xba");
let new_legacy_class_hash = felt!("0x1234");
Expand Down Expand Up @@ -391,7 +391,7 @@ mod tests {
// assert that can fetch data from the underlyign state provider
let sp: Box<dyn StateProvider> = Box::new(cached_state);

let address = ContractAddress::from(felt!("0x67"));
let address = address!("0x67");
let class_hash = felt!("0x123");
let legacy_class_hash = felt!("0x111");

Expand Down Expand Up @@ -454,7 +454,7 @@ mod tests {
fn fetch_non_existant_data() -> anyhow::Result<()> {
let db = InMemoryProvider::new();

let address = ContractAddress::from(felt!("0x1"));
let address = address!("0x1");
let class_hash = felt!("0x123");
let storage_key = felt!("0x1");

Expand All @@ -463,7 +463,7 @@ mod tests {
// only return None if the contract does not exist. The intended behaviour for
// StateProvider::storage is to return None if the storage key or contract address
// does not exist.
let edge_address = ContractAddress::from(felt!("0x2"));
let edge_address = address!("0x2");
db.set_class_hash_of_contract(edge_address, class_hash)?;

let sp = db.latest()?;
Expand Down
9 changes: 4 additions & 5 deletions crates/katana/executor/tests/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use katana_primitives::genesis::constant::{
DEFAULT_OZ_ACCOUNT_CONTRACT_CLASS_HASH, DEFAULT_PREFUNDED_ACCOUNT_BALANCE, DEFAULT_UDC_ADDRESS,
};
use katana_primitives::transaction::TxWithHash;
use katana_primitives::Felt;
use katana_primitives::{address, Felt};
use katana_provider::traits::state::StateProvider;
use starknet::core::utils::{
get_storage_var_address, get_udc_deployed_address, UdcUniqueSettings, UdcUniqueness,
Expand All @@ -26,11 +26,10 @@ fn test_executor_with_valid_blocks_impl<EF: ExecutorFactory>(
let cfg_env = factory.cfg();

// the contract address of the main account used to send most of the transactions
let main_account: ContractAddress =
felt!("0x6b86e40118f29ebe393a75469b4d926c7a44c2e2681b6d319520b7c1156d114").into();
let main_account =
address!("0x6b86e40118f29ebe393a75469b4d926c7a44c2e2681b6d319520b7c1156d114");
// the contract address of the account deployed using the `DeployAccount` tx
let new_acc: ContractAddress =
felt!("0x3ddfa445a70b927497249f94ff7431fc2e2abc761a34417fd4891beb7c2db85").into();
let new_acc = address!("0x3ddfa445a70b927497249f94ff7431fc2e2abc761a34417fd4891beb7c2db85");

let mut executor = factory.with_state(state);
let mut expected_txs: Vec<TxWithHash> = Vec::with_capacity(3);
Expand Down
11 changes: 5 additions & 6 deletions crates/katana/executor/tests/fixtures/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use katana_primitives::transaction::{
};
use katana_primitives::utils::class::{parse_compiled_class, parse_sierra_class};
use katana_primitives::version::Version;
use katana_primitives::Felt;
use katana_primitives::{address, Felt};
use katana_provider::providers::in_memory::InMemoryProvider;
use katana_provider::traits::block::BlockWriter;
use katana_provider::traits::state::{StateFactoryProvider, StateProvider};
Expand Down Expand Up @@ -88,9 +88,8 @@ pub fn valid_blocks() -> [ExecutableBlock; 3] {
let chain_id = ChainId::parse("KATANA").unwrap();
let sequencer_address = ContractAddress(1u64.into());

let sender_address = ContractAddress(felt!(
"0x06b86e40118f29ebe393a75469b4d926c7a44c2e2681b6d319520b7c1156d114"
));
let sender_address =
address!("0x06b86e40118f29ebe393a75469b4d926c7a44c2e2681b6d319520b7c1156d114");

let gas_prices = GasPrices { eth: 100 * u128::pow(10, 9), strk: 100 * u128::pow(10, 9) };

Expand Down Expand Up @@ -169,9 +168,9 @@ pub fn valid_blocks() -> [ExecutableBlock; 3] {
class_hash: felt!(
"0x5400e90f7e0ae78bd02c77cd75527280470e2fe19c54970dd79dc37a9d3645c"
),
contract_address: ContractAddress(felt!(
contract_address: address!(
"0x3ddfa445a70b927497249f94ff7431fc2e2abc761a34417fd4891beb7c2db85"
)),
),
},
))),
],
Expand Down
20 changes: 17 additions & 3 deletions crates/katana/primitives/src/contract.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::fmt;

use derive_more::Deref;
use starknet::core::utils::normalize_address;

use crate::class::ClassHash;
Expand All @@ -15,16 +14,24 @@ pub type StorageValue = Felt;
pub type Nonce = Felt;

/// Represents a contract address.
#[derive(Default, Clone, Copy, Eq, PartialEq, PartialOrd, Ord, Hash, Debug, Deref)]
#[derive(Default, Clone, Copy, Eq, PartialEq, PartialOrd, Ord, Hash, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct ContractAddress(#[deref] pub Felt);
pub struct ContractAddress(pub Felt);

impl ContractAddress {
pub fn new(address: Felt) -> Self {
ContractAddress(normalize_address(address))
}
}

impl core::ops::Deref for ContractAddress {
type Target = Felt;

fn deref(&self) -> &Self::Target {
&self.0
}
}

impl fmt::Display for ContractAddress {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{:#x}", self.0)
Expand All @@ -43,6 +50,13 @@ impl From<ContractAddress> for Felt {
}
}

#[macro_export]
macro_rules! address {
($value:expr) => {
ContractAddress::new($crate::felt!($value))
};
}

/// Represents a generic contract instance information.
#[derive(Debug, Copy, Clone, Default, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
Expand Down
83 changes: 29 additions & 54 deletions crates/katana/primitives/src/genesis/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -677,6 +677,7 @@ mod tests {
use starknet::macros::felt;

use super::*;
use crate::address;

#[test]
fn deserialize_from_json() {
Expand All @@ -690,7 +691,7 @@ mod tests {
assert_eq!(json.gas_prices.eth, 1111);
assert_eq!(json.gas_prices.strk, 2222);

assert_eq!(json.fee_token.address, Some(ContractAddress::from(felt!("0x55"))));
assert_eq!(json.fee_token.address, Some(address!("0x55")));
assert_eq!(json.fee_token.name, String::from("ETHER"));
assert_eq!(json.fee_token.symbol, String::from("ETH"));
assert_eq!(json.fee_token.class, Some(ClassNameOrHash::Name(String::from("MyErc20"))));
Expand All @@ -702,28 +703,18 @@ mod tests {

assert_eq!(
json.universal_deployer.clone().unwrap().address,
Some(ContractAddress::from(felt!(
"0x041a78e741e5af2fec34b695679bc6891742439f7afb8484ecd7766661ad02bf"
)))
Some(address!("0x041a78e741e5af2fec34b695679bc6891742439f7afb8484ecd7766661ad02bf"))
);
assert_eq!(json.universal_deployer.unwrap().class, None);
assert_eq!(
json.fee_token.storage,
Some(BTreeMap::from([(felt!("0x111"), felt!("0x1")), (felt!("0x222"), felt!("0x2")),]))
);

let acc_1 = ContractAddress::from(felt!(
"0x66efb28ac62686966ae85095ff3a772e014e7fbf56d4c5f6fac5606d4dde23a"
));
let acc_2 = ContractAddress::from(felt!(
"0x6b86e40118f29ebe393a75469b4d926c7a44c2e2681b6d319520b7c1156d114"
));
let acc_3 = ContractAddress::from(felt!(
"0x79156ecb3d8f084001bb498c95e37fa1c4b40dbb35a3ae47b77b1ad535edcb9"
));
let acc_4 = ContractAddress::from(felt!(
"0x053a78e741e5af2fec34b695679bc6891742439f7afb8484ecd7766661ad02bf"
));
let acc_1 = address!("0x66efb28ac62686966ae85095ff3a772e014e7fbf56d4c5f6fac5606d4dde23a");
let acc_2 = address!("0x6b86e40118f29ebe393a75469b4d926c7a44c2e2681b6d319520b7c1156d114");
let acc_3 = address!("0x79156ecb3d8f084001bb498c95e37fa1c4b40dbb35a3ae47b77b1ad535edcb9");
let acc_4 = address!("0x053a78e741e5af2fec34b695679bc6891742439f7afb8484ecd7766661ad02bf");

assert_eq!(json.accounts.len(), 4);

Expand Down Expand Up @@ -766,15 +757,12 @@ mod tests {

assert_eq!(json.contracts.len(), 3);

let contract_1 = ContractAddress::from(felt!(
"0x29873c310fbefde666dc32a1554fea6bb45eecc84f680f8a2b0a8fbb8cb89af"
));
let contract_2 = ContractAddress::from(felt!(
"0xe29882a1fcba1e7e10cad46212257fea5c752a4f9b1b1ec683c503a2cf5c8a"
));
let contract_3 = ContractAddress::from(felt!(
"0x05400e90f7e0ae78bd02c77cd75527280470e2fe19c54970dd79dc37a9d3645c"
));
let contract_1 =
address!("0x29873c310fbefde666dc32a1554fea6bb45eecc84f680f8a2b0a8fbb8cb89af");
let contract_2 =
address!("0xe29882a1fcba1e7e10cad46212257fea5c752a4f9b1b1ec683c503a2cf5c8a");
let contract_3 =
address!("0x05400e90f7e0ae78bd02c77cd75527280470e2fe19c54970dd79dc37a9d3645c");

assert_eq!(
json.contracts[&contract_1].balance,
Expand Down Expand Up @@ -924,7 +912,7 @@ mod tests {
]);

let expected_fee_token = FeeTokenConfig {
address: ContractAddress::from(felt!("0x55")),
address: address!("0x55"),
name: String::from("ETHER"),
symbol: String::from("ETH"),
decimals: 18,
Expand All @@ -935,27 +923,16 @@ mod tests {
])),
};

let acc_1 = ContractAddress::from(felt!(
"0x66efb28ac62686966ae85095ff3a772e014e7fbf56d4c5f6fac5606d4dde23a"
));
let acc_2 = ContractAddress::from(felt!(
"0x6b86e40118f29ebe393a75469b4d926c7a44c2e2681b6d319520b7c1156d114"
));
let acc_3 = ContractAddress::from(felt!(
"0x79156ecb3d8f084001bb498c95e37fa1c4b40dbb35a3ae47b77b1ad535edcb9"
));
let acc_4 = ContractAddress::from(felt!(
"0x053a78e741e5af2fec34b695679bc6891742439f7afb8484ecd7766661ad02bf"
));
let contract_1 = ContractAddress::from(felt!(
"0x29873c310fbefde666dc32a1554fea6bb45eecc84f680f8a2b0a8fbb8cb89af"
));
let contract_2 = ContractAddress::from(felt!(
"0xe29882a1fcba1e7e10cad46212257fea5c752a4f9b1b1ec683c503a2cf5c8a"
));
let contract_3 = ContractAddress::from(felt!(
"0x05400e90f7e0ae78bd02c77cd75527280470e2fe19c54970dd79dc37a9d3645c"
));
let acc_1 = address!("0x66efb28ac62686966ae85095ff3a772e014e7fbf56d4c5f6fac5606d4dde23a");
let acc_2 = address!("0x6b86e40118f29ebe393a75469b4d926c7a44c2e2681b6d319520b7c1156d114");
let acc_3 = address!("0x79156ecb3d8f084001bb498c95e37fa1c4b40dbb35a3ae47b77b1ad535edcb9");
let acc_4 = address!("0x053a78e741e5af2fec34b695679bc6891742439f7afb8484ecd7766661ad02bf");
let contract_1 =
address!("0x29873c310fbefde666dc32a1554fea6bb45eecc84f680f8a2b0a8fbb8cb89af");
let contract_2 =
address!("0xe29882a1fcba1e7e10cad46212257fea5c752a4f9b1b1ec683c503a2cf5c8a");
let contract_3 =
address!("0x05400e90f7e0ae78bd02c77cd75527280470e2fe19c54970dd79dc37a9d3645c");

let expected_allocations = BTreeMap::from([
(
Expand Down Expand Up @@ -1042,15 +1019,15 @@ mod tests {
fee_token: expected_fee_token,
allocations: expected_allocations,
timestamp: 5123512314u64,
sequencer_address: ContractAddress::from(felt!("0x100")),
sequencer_address: address!("0x100"),
state_root: felt!("0x99"),
parent_hash: felt!("0x999"),
gas_prices: GasPrices { eth: 1111, strk: 2222 },
universal_deployer: Some(UniversalDeployerConfig {
class_hash: DEFAULT_LEGACY_UDC_CLASS_HASH,
address: ContractAddress::from(felt!(
address: address!(
"0x041a78e741e5af2fec34b695679bc6891742439f7afb8484ecd7766661ad02bf"
)),
),
storage: Some([(felt!("0x10"), felt!("0x100"))].into()),
}),
};
Expand Down Expand Up @@ -1162,9 +1139,7 @@ mod tests {
};

let allocations = BTreeMap::from([(
ContractAddress::from(felt!(
"0x66efb28ac62686966ae85095ff3a772e014e7fbf56d4c5f6fac5606d4dde23a"
)),
address!("0x66efb28ac62686966ae85095ff3a772e014e7fbf56d4c5f6fac5606d4dde23a"),
GenesisAllocation::Account(GenesisAccountAlloc::Account(GenesisAccount {
public_key: felt!("0x1"),
balance: Some(U256::from_str("0xD3C21BCECCEDA1000000").unwrap()),
Expand All @@ -1182,7 +1157,7 @@ mod tests {
timestamp: 5123512314u64,
state_root: felt!("0x99"),
parent_hash: felt!("0x999"),
sequencer_address: ContractAddress(felt!("0x100")),
sequencer_address: address!("0x100"),
gas_prices: GasPrices { eth: 1111, strk: 2222 },
universal_deployer: Some(UniversalDeployerConfig {
class_hash: DEFAULT_LEGACY_UDC_CLASS_HASH,
Expand Down
Loading

0 comments on commit 8453b63

Please sign in to comment.