Skip to content

Commit

Permalink
Add Deref to newtypes, add casts to double newtypes
Browse files Browse the repository at this point in the history
- QOL
- Nobody should have to handle `PatriciaKey` directly, it's an
implementation detail.
  • Loading branch information
Gilad Chase committed Jan 16, 2024
1 parent 10a5b78 commit 4abde83
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 7 deletions.
34 changes: 32 additions & 2 deletions src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ContractAddress> for StarkFelt {
fn from(contract_address: ContractAddress) -> StarkFelt {
**contract_address
}
}

impl From<u128> for ContractAddress {
fn from(val: u128) -> Self {
ContractAddress(PatriciaKey::from(val))
Expand Down Expand Up @@ -102,6 +119,7 @@ pub fn calculate_contract_address(
PartialOrd,
Ord,
Display,
derive_more::Deref,
)]
pub struct ClassHash(pub StarkHash);

Expand Down Expand Up @@ -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
Expand Down
19 changes: 18 additions & 1 deletion src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<StorageKey> for StarkFelt {
fn from(storage_key: StorageKey) -> StarkFelt {
**storage_key
}
}

impl TryFrom<StarkHash> for StorageKey {
type Error = StarknetApiError;

Expand Down
52 changes: 48 additions & 4 deletions src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -417,7 +428,18 @@ impl From<Fee> 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);

Expand All @@ -439,7 +461,18 @@ pub struct TransactionSignature(pub Vec<StarkFelt>);

/// 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);

Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 4abde83

Please sign in to comment.