Skip to content

Commit

Permalink
imp: use new() constructor for PortId
Browse files Browse the repository at this point in the history
  • Loading branch information
Farhad-Shabani committed Sep 20, 2024
1 parent 3bb3de2 commit 741dfd4
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 15 deletions.
14 changes: 8 additions & 6 deletions cairo-contracts/packages/core/src/host/identifiers.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -109,12 +117,6 @@ pub impl PortIdIntoByteArray of Into<PortId, ByteArray> {
}
}

pub impl ByteArrayIntoPortId of Into<ByteArray, PortId> {
fn into(self: ByteArray) -> PortId {
PortId { port_id: self }
}
}

#[derive(Clone, Debug, Drop, PartialEq, Serde, starknet::Store)]
pub struct Sequence {
pub sequence: u64,
Expand Down
4 changes: 2 additions & 2 deletions cairo-contracts/packages/core/src/lib.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down
14 changes: 7 additions & 7 deletions cairo-contracts/packages/core/src/router/component.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -30,7 +30,7 @@ pub mod RouterHandlerComponent {
fn get_app_address(
self: @ComponentState<TContractState>, port_id: ByteArray
) -> Option<ContractAddress> {
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)
Expand All @@ -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<TContractState>, port_id: ByteArray) {
self.remove_app_address(port_id.into())
self.remove_app_address(PortIdImpl::new(port_id))
}
}

Expand All @@ -57,11 +57,11 @@ pub mod RouterHandlerComponent {
TContractState, +HasComponent<TContractState>, +Drop<TContractState>
> of RouterInternalTrait<TContractState> {
fn get_app(self: @ComponentState<TContractState>, 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()
}
}

Expand Down

0 comments on commit 741dfd4

Please sign in to comment.