Skip to content

Commit

Permalink
chore: implement From<BigUint> for address type (#2488)
Browse files Browse the repository at this point in the history
  • Loading branch information
kariy authored Sep 30, 2024
1 parent 25092b8 commit afb86a9
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 11 deletions.
7 changes: 2 additions & 5 deletions crates/katana/controller/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
};
Expand Down Expand Up @@ -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)
};
Expand Down
13 changes: 13 additions & 0 deletions crates/katana/primitives/src/contract.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::fmt;

use num_bigint::BigUint;
use starknet::core::utils::normalize_address;

use crate::class::ClassHash;
Expand Down Expand Up @@ -50,6 +51,18 @@ impl From<ContractAddress> 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<BigUint> 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) => {
Expand Down
9 changes: 5 additions & 4 deletions crates/katana/primitives/src/da/encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ pub fn decode_state_updates(value: &[BigUint]) -> Result<StateUpdates, EncodingE

for _ in 0..total_updates {
let address = iter.next().ok_or(EncodingError::MissingAddress)?;
let address: ContractAddress = Felt::from(address).into();
let address = ContractAddress::from(address);

let metadata = iter.next().ok_or(EncodingError::MissingMetadata)?;
let metadata = Metadata::decode(metadata)?;
Expand Down Expand Up @@ -308,6 +308,7 @@ mod tests {
use starknet::macros::felt;

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

macro_rules! biguint {
($s:expr) => {
Expand Down Expand Up @@ -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));

Expand Down
3 changes: 2 additions & 1 deletion crates/katana/storage/provider/src/providers/fork/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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");
Expand Down
3 changes: 2 additions & 1 deletion crates/katana/storage/provider/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -49,7 +50,7 @@ fn initialize_test_provider<P: BlockWriter>(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 = {
Expand Down

1 comment on commit afb86a9

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 1.30.

Benchmark suite Current: afb86a9 Previous: 5eb722a Ratio
Invoke.ERC20.transfer/Blockifier.Cold 5468516 ns/iter (± 336815) 3918328 ns/iter (± 46996) 1.40

This comment was automatically generated by workflow using github-action-benchmark.

CC: @kariy @glihm @tarrencev

Please sign in to comment.