From afb86a95d7d0fbc3e67c8fa613ef3132b88ac161 Mon Sep 17 00:00:00 2001 From: Ammar Arif Date: Mon, 30 Sep 2024 19:07:26 -0400 Subject: [PATCH] chore: implement `From` for address type (#2488) --- crates/katana/controller/src/lib.rs | 7 ++----- crates/katana/primitives/src/contract.rs | 13 +++++++++++++ crates/katana/primitives/src/da/encoding.rs | 9 +++++---- .../storage/provider/src/providers/fork/state.rs | 3 ++- crates/katana/storage/provider/src/test_utils.rs | 3 ++- 5 files changed, 24 insertions(+), 11 deletions(-) diff --git a/crates/katana/controller/src/lib.rs b/crates/katana/controller/src/lib.rs index 4d85a216ac..c3763473f7 100644 --- a/crates/katana/controller/src/lib.rs +++ b/crates/katana/controller/src/lib.rs @@ -47,8 +47,7 @@ fn add_controller_account_inner(genesis: &mut Genesis, user: slot::account::Acco storage: Some(get_contract_storage(credential_id, public_key)?), }; - let address = - ContractAddress::from(Felt::from_bytes_be(&user.contract_address.to_bytes_be())); + let address = ContractAddress::from(user.contract_address); (address, GenesisAllocation::Contract(account)) }; @@ -97,9 +96,7 @@ pub mod json { storage: Some(get_contract_storage(credential_id, public_key)?), }; - let address = ContractAddress::from(Felt::from_bytes_be( - &user.account.contract_address.to_bytes_be(), - )); + let address = ContractAddress::from(user.account.contract_address); (address, account) }; diff --git a/crates/katana/primitives/src/contract.rs b/crates/katana/primitives/src/contract.rs index fe93ec1712..08f2831da9 100644 --- a/crates/katana/primitives/src/contract.rs +++ b/crates/katana/primitives/src/contract.rs @@ -1,5 +1,6 @@ use std::fmt; +use num_bigint::BigUint; use starknet::core::utils::normalize_address; use crate::class::ClassHash; @@ -50,6 +51,18 @@ impl From for Felt { } } +impl From<&BigUint> for ContractAddress { + fn from(biguint: &BigUint) -> Self { + Self::new(Felt::from_bytes_le_slice(&biguint.to_bytes_le())) + } +} + +impl From for ContractAddress { + fn from(biguint: BigUint) -> Self { + Self::new(Felt::from_bytes_le_slice(&biguint.to_bytes_le())) + } +} + #[macro_export] macro_rules! address { ($value:expr) => { diff --git a/crates/katana/primitives/src/da/encoding.rs b/crates/katana/primitives/src/da/encoding.rs index aabf055532..832aebdd1f 100644 --- a/crates/katana/primitives/src/da/encoding.rs +++ b/crates/katana/primitives/src/da/encoding.rs @@ -139,7 +139,7 @@ pub fn decode_state_updates(value: &[BigUint]) -> Result { @@ -352,9 +353,9 @@ mod tests { assert_eq!(state_updates.declared_classes.len(), 1); assert_eq!(state_updates.deployed_contracts.len(), 0); - let address: ContractAddress = - felt!("2019172390095051323869047481075102003731246132997057518965927979101413600827") - .into(); + let address = address!( + "2019172390095051323869047481075102003731246132997057518965927979101413600827" + ); assert_eq!(state_updates.nonce_updates.get(&address), Some(&Felt::ONE)); diff --git a/crates/katana/storage/provider/src/providers/fork/state.rs b/crates/katana/storage/provider/src/providers/fork/state.rs index aa181cd480..77e0605205 100644 --- a/crates/katana/storage/provider/src/providers/fork/state.rs +++ b/crates/katana/storage/provider/src/providers/fork/state.rs @@ -219,6 +219,7 @@ impl ContractClassProvider for ForkedSnapshot { mod tests { use std::collections::BTreeMap; + use katana_primitives::address; use katana_primitives::state::{StateUpdates, StateUpdatesWithDeclaredClasses}; use starknet::macros::felt; @@ -229,7 +230,7 @@ mod tests { fn test_get_nonce() { let backend = create_forked_backend("http://localhost:8080", 1); - let address: ContractAddress = felt!("1").into(); + let address = address!("1"); let class_hash = felt!("11"); let remote_nonce = felt!("111"); let local_nonce = felt!("1111"); diff --git a/crates/katana/storage/provider/src/test_utils.rs b/crates/katana/storage/provider/src/test_utils.rs index 151ad7a560..54d230a957 100644 --- a/crates/katana/storage/provider/src/test_utils.rs +++ b/crates/katana/storage/provider/src/test_utils.rs @@ -2,6 +2,7 @@ use std::sync::Arc; use alloy_primitives::U256; use katana_db::mdbx::{test_utils, DbEnvKind}; +use katana_primitives::address; use katana_primitives::block::{BlockHash, FinalityStatus}; use katana_primitives::class::CompiledClass; use katana_primitives::contract::ContractAddress; @@ -49,7 +50,7 @@ fn initialize_test_provider(provider: &P) { /// - An account with simple `__execute__` function, deployed at address `0x1`. pub fn create_genesis_for_testing() -> Genesis { let class_hash = felt!("0x111"); - let address = ContractAddress::from(felt!("0x1")); + let address = address!("0x1"); // TODO: we should have a genesis builder that can do all of this for us. let class = {