From 741dfd47a3ecf68b37020c9f344c69d4e7f79cc2 Mon Sep 17 00:00:00 2001 From: Farhad Shabani Date: Thu, 19 Sep 2024 17:23:19 -0700 Subject: [PATCH] imp: use new() constructor for PortId --- .../packages/core/src/host/identifiers.cairo | 14 ++++++++------ cairo-contracts/packages/core/src/lib.cairo | 4 ++-- .../packages/core/src/router/component.cairo | 14 +++++++------- 3 files changed, 17 insertions(+), 15 deletions(-) diff --git a/cairo-contracts/packages/core/src/host/identifiers.cairo b/cairo-contracts/packages/core/src/host/identifiers.cairo index f128560c..13d24ce1 100644 --- a/cairo-contracts/packages/core/src/host/identifiers.cairo +++ b/cairo-contracts/packages/core/src/host/identifiers.cairo @@ -87,6 +87,14 @@ pub struct PortId { #[generate_trait] pub impl PortIdImpl of PortIdTrait { + /// Constructs a new port identifier from a byte array with basic + /// validation. + fn new(port_id: ByteArray) -> PortId { + let port_id = PortId { port_id }; + port_id.validate_basic(); + port_id + } + fn validate(self: @PortId, port_id_hash: felt252) { self.validate_basic(); assert(self.key() == port_id_hash, HostErrors::INVALID_PORT_ID); @@ -109,12 +117,6 @@ pub impl PortIdIntoByteArray of Into { } } -pub impl ByteArrayIntoPortId of Into { - fn into(self: ByteArray) -> PortId { - PortId { port_id: self } - } -} - #[derive(Clone, Debug, Drop, PartialEq, Serde, starknet::Store)] pub struct Sequence { pub sequence: u64, diff --git a/cairo-contracts/packages/core/src/lib.cairo b/cairo-contracts/packages/core/src/lib.cairo index b3e5f0ee..216a163b 100644 --- a/cairo-contracts/packages/core/src/lib.cairo +++ b/cairo-contracts/packages/core/src/lib.cairo @@ -95,8 +95,8 @@ pub mod host { mod prefixes; pub use errors::HostErrors; pub use identifiers::{ - ClientId, ClientIdImpl, ClientIdTrait, ChannelId, ChannelIdTrait, PortId, PortIdTrait, - Sequence, SequenceImpl, SequenceTrait, SequencePartialOrd + ClientId, ClientIdImpl, ClientIdTrait, ChannelId, ChannelIdTrait, PortId, PortIdImpl, + PortIdTrait, Sequence, SequenceImpl, SequenceTrait, SequencePartialOrd }; pub use keys::{channel_end_key, receipt_key, ack_key, next_sequence_recv_key}; diff --git a/cairo-contracts/packages/core/src/router/component.cairo b/cairo-contracts/packages/core/src/router/component.cairo index 8f3c172c..e616fc21 100644 --- a/cairo-contracts/packages/core/src/router/component.cairo +++ b/cairo-contracts/packages/core/src/router/component.cairo @@ -3,7 +3,7 @@ pub mod RouterHandlerComponent { use core::num::traits::Zero; use starknet::ContractAddress; use starknet::storage::Map; - use starknet_ibc_core::host::PortId; + use starknet_ibc_core::host::{PortId, PortIdImpl}; use starknet_ibc_core::router::{RouterErrors, IRouter, ApplicationContract}; use starknet_ibc_utils::ComputeKey; @@ -30,7 +30,7 @@ pub mod RouterHandlerComponent { fn get_app_address( self: @ComponentState, port_id: ByteArray ) -> Option { - let app_address = self.read_app_address(port_id.into()); + let app_address = self.read_app_address(PortIdImpl::new(port_id)); if app_address.is_non_zero() { Option::Some(app_address) @@ -44,11 +44,11 @@ pub mod RouterHandlerComponent { port_id: ByteArray, app_address: ContractAddress ) { - self.write_app_address(port_id.into(), app_address) + self.write_app_address(PortIdImpl::new(port_id), app_address) } fn release_port_id(ref self: ComponentState, port_id: ByteArray) { - self.remove_app_address(port_id.into()) + self.remove_app_address(PortIdImpl::new(port_id)) } } @@ -57,11 +57,11 @@ pub mod RouterHandlerComponent { TContractState, +HasComponent, +Drop > of RouterInternalTrait { fn get_app(self: @ComponentState, port_id: PortId) -> ApplicationContract { - let maybe_app_address = self.get_app_address(port_id.port_id); + let maybe_app_address = self.read_app_address(port_id); - assert(maybe_app_address.is_some(), RouterErrors::UNSUPPORTED_PORT_ID); + assert(maybe_app_address.is_non_zero(), RouterErrors::UNSUPPORTED_PORT_ID); - maybe_app_address.unwrap().into() + maybe_app_address.into() } }