From 4abde83a83cb162c91156f3441e3ad9349434e72 Mon Sep 17 00:00:00 2001 From: Gilad Chase Date: Tue, 16 Jan 2024 15:17:14 +0200 Subject: [PATCH] Add `Deref` to newtypes, add casts to double newtypes - QOL - Nobody should have to handle `PatriciaKey` directly, it's an implementation detail. --- src/core.rs | 34 ++++++++++++++++++++++++++++-- src/state.rs | 19 ++++++++++++++++- src/transaction.rs | 52 ++++++++++++++++++++++++++++++++++++++++++---- 3 files changed, 98 insertions(+), 7 deletions(-) diff --git a/src/core.rs b/src/core.rs index 561a3f54..f356371d 100644 --- a/src/core.rs +++ b/src/core.rs @@ -34,10 +34,27 @@ impl ChainId { // this is a special address that is not used for contracts. pub const BLOCK_HASH_TABLE_ADDRESS: ContractAddress = ContractAddress(PatriciaKey(StarkHash::ONE)); #[derive( - Debug, Default, Copy, Clone, Eq, PartialEq, Hash, Deserialize, Serialize, PartialOrd, Ord, + Debug, + Default, + Copy, + Clone, + Eq, + PartialEq, + Hash, + Deserialize, + Serialize, + PartialOrd, + Ord, + derive_more::Deref, )] pub struct ContractAddress(pub PatriciaKey); +impl From for StarkFelt { + fn from(contract_address: ContractAddress) -> StarkFelt { + **contract_address + } +} + impl From for ContractAddress { fn from(val: u128) -> Self { ContractAddress(PatriciaKey::from(val)) @@ -102,6 +119,7 @@ pub fn calculate_contract_address( PartialOrd, Ord, Display, + derive_more::Deref, )] pub struct ClassHash(pub StarkHash); @@ -177,7 +195,19 @@ pub struct GlobalRoot(pub StarkHash); /// A key for nodes of a Patricia tree. // Invariant: key is in range. -#[derive(Copy, Clone, Eq, PartialEq, Default, Hash, Deserialize, Serialize, PartialOrd, Ord)] +#[derive( + Copy, + Clone, + Eq, + PartialEq, + Default, + Hash, + Deserialize, + Serialize, + PartialOrd, + Ord, + derive_more:: Deref, +)] pub struct PatriciaKey(StarkHash); // 2**251 diff --git a/src/state.rs b/src/state.rs index 133b5807..d1131e8f 100644 --- a/src/state.rs +++ b/src/state.rs @@ -127,10 +127,27 @@ impl StateNumber { /// A storage key in a contract. #[derive( - Debug, Default, Clone, Copy, Eq, PartialEq, Hash, Deserialize, Serialize, PartialOrd, Ord, + Debug, + Default, + Clone, + Copy, + Eq, + PartialEq, + Hash, + Deserialize, + Serialize, + PartialOrd, + Ord, + derive_more::Deref, )] pub struct StorageKey(pub PatriciaKey); +impl From for StarkFelt { + fn from(storage_key: StorageKey) -> StarkFelt { + **storage_key + } +} + impl TryFrom for StorageKey { type Error = StarknetApiError; diff --git a/src/transaction.rs b/src/transaction.rs index 83db2a1e..2800eb67 100644 --- a/src/transaction.rs +++ b/src/transaction.rs @@ -392,7 +392,18 @@ pub enum TransactionExecutionStatus { /// A fee. #[derive( - Debug, Copy, Clone, Default, Eq, PartialEq, Hash, Deserialize, Serialize, PartialOrd, Ord, + Debug, + Copy, + Clone, + Default, + Eq, + PartialEq, + Hash, + Deserialize, + Serialize, + PartialOrd, + Ord, + derive_more::Deref, )] #[serde(from = "PrefixedBytesAsHex<16_usize>", into = "PrefixedBytesAsHex<16_usize>")] pub struct Fee(pub u128); @@ -417,7 +428,18 @@ impl From for StarkFelt { /// The hash of a [Transaction](`crate::transaction::Transaction`). #[derive( - Debug, Default, Copy, Clone, Eq, PartialEq, Hash, Deserialize, Serialize, PartialOrd, Ord, + Debug, + Default, + Copy, + Clone, + Eq, + PartialEq, + Hash, + Deserialize, + Serialize, + PartialOrd, + Ord, + derive_more::Deref, )] pub struct TransactionHash(pub StarkHash); @@ -439,7 +461,18 @@ pub struct TransactionSignature(pub Vec); /// A transaction version. #[derive( - Debug, Copy, Clone, Default, Eq, PartialEq, Hash, Deserialize, Serialize, PartialOrd, Ord, + Debug, + Copy, + Clone, + Default, + Eq, + PartialEq, + Hash, + Deserialize, + Serialize, + PartialOrd, + Ord, + derive_more::Deref, )] pub struct TransactionVersion(pub StarkFelt); @@ -528,7 +561,18 @@ pub struct EventIndexInTransactionOutput(pub usize); /// Transaction fee tip. #[derive( - Clone, Copy, Debug, Default, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize, + Clone, + Copy, + Debug, + Default, + Deserialize, + Eq, + Hash, + Ord, + PartialEq, + PartialOrd, + Serialize, + derive_more::Deref, )] #[serde(from = "PrefixedBytesAsHex<8_usize>", into = "PrefixedBytesAsHex<8_usize>")] pub struct Tip(pub u64);