Skip to content
This repository has been archived by the owner on Aug 21, 2024. It is now read-only.

QOL: Add direct cast from PyFelt into ContractAddress + Deref #1345

Merged
merged 1 commit into from
Jan 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 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/native_blockifier/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ crate-type = ["cdylib"]
blockifier = { path = "../blockifier", features = ["testing"] }
cairo-lang-starknet.workspace = true
cairo-vm.workspace = true
derive_more.workspace = true
indexmap.workspace = true
log.workspace = true
num-bigint.workspace = true
Expand Down
2 changes: 1 addition & 1 deletion crates/native_blockifier/src/py_block_executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ pub fn into_block_context(
block_info: BlockInfo {
block_number: BlockNumber(block_info.block_number),
block_timestamp: BlockTimestamp(block_info.block_timestamp),
sequencer_address: ContractAddress::try_from(block_info.sequencer_address.0)?,
sequencer_address: ContractAddress::try_from(block_info.sequencer_address)?,
vm_resource_fee_cost: general_config.cairo_resource_fee_weights.clone(),
gas_prices: GasPrices {
eth_l1_gas_price: block_info.eth_l1_gas_price,
Expand Down
6 changes: 3 additions & 3 deletions crates/native_blockifier/src/py_declare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ impl TryFrom<PyDeclareTransactionV0V1> for DeclareTransactionV0V1 {
signature: TransactionSignature(from_py_felts(tx.signature)),
nonce: Nonce(tx.nonce.0),
class_hash: ClassHash(tx.class_hash.0),
sender_address: ContractAddress::try_from(tx.sender_address.0)?,
sender_address: ContractAddress::try_from(tx.sender_address)?,
})
}
}
Expand All @@ -56,7 +56,7 @@ impl TryFrom<PyDeclareTransactionV2> for DeclareTransactionV2 {
nonce: Nonce(tx.nonce.0),
class_hash: ClassHash(tx.class_hash.0),
compiled_class_hash: CompiledClassHash(tx.compiled_class_hash.0),
sender_address: ContractAddress::try_from(tx.sender_address.0)?,
sender_address: ContractAddress::try_from(tx.sender_address)?,
})
}
}
Expand Down Expand Up @@ -86,7 +86,7 @@ impl TryFrom<PyDeclareTransactionV3> for DeclareTransactionV3 {
nonce: Nonce(tx.nonce.0),
class_hash: ClassHash(tx.class_hash.0),
compiled_class_hash: CompiledClassHash(tx.compiled_class_hash.0),
sender_address: ContractAddress::try_from(tx.sender_address.0)?,
sender_address: ContractAddress::try_from(tx.sender_address)?,
nonce_data_availability_mode: DataAvailabilityMode::from(
tx.nonce_data_availability_mode,
),
Expand Down
3 changes: 1 addition & 2 deletions crates/native_blockifier/src/py_deploy_account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ pub fn py_deploy_account(py_tx: &PyAny) -> NativeBlockifierResult<DeployAccountT
}?;

let tx_hash = TransactionHash(py_attr::<PyFelt>(py_tx, "hash_value")?.0);
let contract_address =
ContractAddress::try_from(py_attr::<PyFelt>(py_tx, "sender_address")?.0)?;
let contract_address = ContractAddress::try_from(py_attr::<PyFelt>(py_tx, "sender_address")?)?;
Ok(DeployAccountTransaction::new(tx, tx_hash, contract_address))
}
6 changes: 3 additions & 3 deletions crates/native_blockifier/src/py_invoke_function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ impl TryFrom<PyInvokeTransactionV0> for InvokeTransactionV0 {
Ok(Self {
max_fee: Fee(tx.max_fee),
signature: TransactionSignature(from_py_felts(tx.signature)),
contract_address: ContractAddress::try_from(tx.sender_address.0)?,
contract_address: ContractAddress::try_from(tx.sender_address)?,
entry_point_selector: EntryPointSelector(tx.entry_point_selector.0),
calldata: Calldata(Arc::from(from_py_felts(tx.calldata))),
})
Expand All @@ -54,7 +54,7 @@ impl TryFrom<PyInvokeTransactionV1> for InvokeTransactionV1 {
max_fee: Fee(tx.max_fee),
signature: TransactionSignature(from_py_felts(tx.signature)),
nonce: Nonce(tx.nonce.0),
sender_address: ContractAddress::try_from(tx.sender_address.0)?,
sender_address: ContractAddress::try_from(tx.sender_address)?,
calldata: Calldata(Arc::from(from_py_felts(tx.calldata))),
})
}
Expand Down Expand Up @@ -82,7 +82,7 @@ impl TryFrom<PyInvokeTransactionV3> for InvokeTransactionV3 {
tip: Tip(tx.tip),
signature: TransactionSignature(from_py_felts(tx.signature)),
nonce: Nonce(tx.nonce.0),
sender_address: ContractAddress::try_from(tx.sender_address.0)?,
sender_address: ContractAddress::try_from(tx.sender_address)?,
calldata: Calldata(Arc::from(from_py_felts(tx.calldata))),
nonce_data_availability_mode: DataAvailabilityMode::from(
tx.nonce_data_availability_mode,
Expand Down
2 changes: 1 addition & 1 deletion crates/native_blockifier/src/py_l1_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ impl TryFrom<PyL1HandlerTransaction> for starknet_api::transaction::L1HandlerTra
Ok(Self {
version: constants::L1_HANDLER_VERSION,
nonce: Nonce(tx.nonce.0),
contract_address: ContractAddress::try_from(tx.contract_address.0)?,
contract_address: ContractAddress::try_from(tx.contract_address)?,
entry_point_selector: EntryPointSelector(tx.entry_point_selector.0),
calldata: Calldata(Arc::from(from_py_felts(tx.calldata))),
})
Expand Down
6 changes: 3 additions & 3 deletions crates/native_blockifier/src/py_state_diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ impl TryFrom<PyStateDiff> for StateDiff {
fn try_from(state_diff: PyStateDiff) -> NativeBlockifierResult<Self> {
let mut deployed_contracts: IndexMap<ContractAddress, ClassHash> = IndexMap::new();
for (address, class_hash) in state_diff.address_to_class_hash {
let address = ContractAddress::try_from(address.0)?;
let address = ContractAddress::try_from(address)?;
let class_hash = ClassHash(class_hash.0);
deployed_contracts.insert(address, class_hash);
}

let mut storage_diffs = IndexMap::new();
for (address, storage_mapping) in state_diff.storage_updates {
let address = ContractAddress::try_from(address.0)?;
let address = ContractAddress::try_from(address)?;
storage_diffs.insert(address, IndexMap::new());

for (key, value) in storage_mapping {
Expand All @@ -51,7 +51,7 @@ impl TryFrom<PyStateDiff> for StateDiff {

let mut nonces = IndexMap::new();
for (address, nonce) in state_diff.address_to_nonce {
let address = ContractAddress::try_from(address.0)?;
let address = ContractAddress::try_from(address)?;
let nonce = Nonce(nonce.0);
nonces.insert(address, nonce);
}
Expand Down
11 changes: 10 additions & 1 deletion crates/native_blockifier/src/py_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ use pyo3::prelude::*;
use starknet_api::core::{ChainId, ClassHash, CompiledClassHash, ContractAddress, EthAddress};
use starknet_api::hash::StarkFelt;
use starknet_api::state::StorageKey;
use starknet_api::StarknetApiError;

use crate::errors::NativeBlockifierResult;

#[derive(Clone, Copy, Debug, Default, Eq, FromPyObject, Hash, PartialEq)]
#[derive(Clone, Copy, Debug, Default, Eq, FromPyObject, Hash, PartialEq, derive_more::Deref)]
pub struct PyFelt(#[pyo3(from_py_with = "int_to_stark_felt")] pub StarkFelt);

impl IntoPy<PyObject> for PyFelt {
Expand Down Expand Up @@ -64,6 +65,14 @@ impl From<StorageKey> for PyFelt {
}
}

impl TryFrom<PyFelt> for ContractAddress {
type Error = StarknetApiError;

fn try_from(value: PyFelt) -> Result<Self, Self::Error> {
Self::try_from(value.0)
}
}

fn int_to_stark_felt(int: &PyAny) -> PyResult<StarkFelt> {
let biguint: BigUint = int.extract()?;
biguint_to_felt(biguint).map_err(|e| PyValueError::new_err(e.to_string()))
Expand Down
2 changes: 1 addition & 1 deletion crates/native_blockifier/src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ impl Storage for PapyrusStorage {
// i.e.: pointing to a non-zeroed class hash). Rest would be (newly) deployed classes.
let mut replaced_classes = IndexMap::<ContractAddress, ClassHash>::new();
for (address, class_hash) in &py_state_diff.address_to_class_hash {
let address = ContractAddress::try_from(address.0)?;
let address = ContractAddress::try_from(*address)?;
let address_assigned: bool = self
.reader()
.begin_ro_txn()?
Expand Down
Loading