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

refactor: wrap ClientId in ChannelId #1370

Open
wants to merge 1 commit into
base: rano/eureka/1366
Choose a base branch
from
Open
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
35 changes: 11 additions & 24 deletions ibc-eureka-core/ics24-host/types/src/identifiers/channel_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ use derive_more::Into;
use ibc_primitives::prelude::*;

use crate::error::IdentifierError;
use crate::validate::validate_channel_identifier;

use crate::identifiers::ClientId;

const CHANNEL_ID_PREFIX: &str = "channel";

Expand All @@ -24,43 +25,29 @@ const CHANNEL_ID_PREFIX: &str = "channel";
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Into)]
pub struct ChannelId(String);
pub struct ChannelId(ClientId);

impl ChannelId {
/// Builds a new channel identifier. Like client and connection identifiers, channel ids are
/// deterministically formed from two elements: a prefix `prefix`, and a monotonically
/// increasing `counter`, separated by a dash "-".
/// The prefix is currently determined statically (see `ChannelId::prefix()`) so this method
/// accepts a single argument, the `counter`.
///
/// ```
/// # use ibc_eureka_core_host_types::identifiers::ChannelId;
/// let chan_id = ChannelId::new(27);
/// assert_eq!(chan_id.to_string(), "channel-27");
/// ```
pub fn new(identifier: u64) -> Self {
let id = format!("{}-{}", Self::prefix(), identifier);
Self(id)
impl From<ClientId> for ChannelId {
fn from(client_id: ClientId) -> Self {
Self(client_id)
}
}

impl ChannelId {
/// Returns the static prefix to be used across all channel identifiers.
pub fn prefix() -> &'static str {
CHANNEL_ID_PREFIX
}

/// Get this identifier as a borrowed `&str`
pub fn as_str(&self) -> &str {
&self.0
self.0.as_str()
}

/// Get this identifier as a borrowed byte slice
pub fn as_bytes(&self) -> &[u8] {
self.0.as_bytes()
}

pub fn zero() -> Self {
Self::new(0)
}
}

/// This implementation provides a `to_string` method.
Expand All @@ -74,13 +61,13 @@ impl FromStr for ChannelId {
type Err = IdentifierError;

fn from_str(s: &str) -> Result<Self, Self::Err> {
validate_channel_identifier(s).map(|_| Self(s.to_string()))
Ok(Self(s.parse()?))
}
}

impl AsRef<str> for ChannelId {
fn as_ref(&self) -> &str {
&self.0
self.0.as_str()
}
}

Expand Down