Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Deref to newtypes, add casts to double newtypes #170

Merged
merged 1 commit into from
Jan 17, 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
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
Loading