Skip to content

Commit

Permalink
fix TracePath::from_str
Browse files Browse the repository at this point in the history
  • Loading branch information
rnbguy committed Apr 24, 2024
1 parent 7408994 commit d34d946
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 44 deletions.
49 changes: 5 additions & 44 deletions ibc-apps/ics20-transfer/types/src/denom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,54 +185,15 @@ impl TracePath {
}
}

impl<'a> TryFrom<Vec<&'a str>> for TracePath {
type Error = TokenTransferError;

fn try_from(v: Vec<&'a str>) -> Result<Self, Self::Error> {
if v.len() % 2 != 0 {
return Err(TokenTransferError::InvalidTraceLength {
len: v.len() as u64,
});
}

let mut trace = vec![];
// this does not take care of the remainder if the length is not even
let id_pairs = v.chunks_exact(2).map(|paths| (paths[0], paths[1]));
for (pos, (port_id, channel_id)) in id_pairs.rev().enumerate() {
let port_id =
PortId::from_str(port_id).map_err(|e| TokenTransferError::InvalidTracePortId {
pos: pos as u64,
validation_error: e,
})?;
let channel_id = ChannelId::from_str(channel_id).map_err(|e| {
TokenTransferError::InvalidTraceChannelId {
pos: pos as u64,
validation_error: e,
}
})?;
trace.push(TracePrefix {
port_id,
channel_id,
});
}

Ok(trace.into())
}
}

impl FromStr for TracePath {
type Err = TokenTransferError;

fn from_str(s: &str) -> Result<Self, Self::Err> {
let parts = {
let parts: Vec<&str> = s.split('/').collect();
if parts.len() == 1 && parts[0].trim().is_empty() {
vec![]
} else {
parts
}
};
parts.try_into()
let (trace_path, remaining_parts) = TracePath::trim(s);
remaining_parts
.is_empty()
.then_some(trace_path)
.ok_or_else(|| TokenTransferError::MalformedTrace(s.to_string()))
}
}

Expand Down
2 changes: 2 additions & 0 deletions ibc-apps/ics20-transfer/types/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ pub enum TokenTransferError {
pos: u64,
validation_error: IdentifierError,
},
/// malformed trace: `{0}`
MalformedTrace(String),
/// trace length must be even but got: `{len}`
InvalidTraceLength { len: u64 },
/// invalid amount error: `{0}`
Expand Down

0 comments on commit d34d946

Please sign in to comment.