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

Fix conversions related to MockHeader and ClientType::Mock #1195

Merged
merged 6 commits into from
Jul 13, 2021
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
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,15 @@
- Add CLI git hash ([#1094])
- Fix unwraps in `packet query` CLIs ([#1114])

### BUG FIXES

- [ibc]
- Fix stack overflow in `MockHeader` implementation ([#1192])
- Align `as_str` and `from_str` behavior in `ClientType` ([#1192])

[#1094]: https://github.com/informalsystems/ibc-rs/issues/1094
[#1114]: https://github.com/informalsystems/ibc-rs/issues/1114
[#1192]: https://github.com/informalsystems/ibc-rs/issues/1192

## v0.6.0
*July 12th, 2021*
Expand Down
35 changes: 28 additions & 7 deletions modules/src/ics02_client/client_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,25 @@ pub enum ClientType {
}

impl ClientType {
const TENDERMINT_STR: &'static str = "07-tendermint";

#[cfg_attr(not(test), allow(dead_code))]
const MOCK_STR: &'static str = "9999-mock";

/// Yields the identifier of this client type as a string
pub fn as_string(&self) -> &'static str {
pub fn as_str(&self) -> &'static str {
match self {
Self::Tendermint => "07-tendermint",
Self::Tendermint => Self::TENDERMINT_STR,

#[cfg(any(test, feature = "mocks"))]
Self::Mock => "9999-mock",
Self::Mock => Self::MOCK_STR,
}
}
}

impl fmt::Display for ClientType {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "ClientType({})", self.as_string())
write!(f, "ClientType({})", self.as_str())
}
}

Expand All @@ -36,10 +41,10 @@ impl std::str::FromStr for ClientType {

fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"07-tendermint" => Ok(Self::Tendermint),
Self::TENDERMINT_STR => Ok(Self::Tendermint),

#[cfg(any(test, feature = "mocks"))]
"mock" => Ok(Self::Mock),
Self::MOCK_STR => Ok(Self::Mock),

_ => Err(error::Kind::UnknownClientType(s.to_string()).into()),
}
Expand All @@ -65,7 +70,7 @@ mod tests {

#[test]
fn parse_mock_client_type() {
let client_type = ClientType::from_str("mock");
let client_type = ClientType::from_str("9999-mock");

match client_type {
Ok(ClientType::Mock) => (),
Expand All @@ -85,4 +90,20 @@ mod tests {
_ => panic!("parse didn't fail"),
}
}

#[test]
fn parse_mock_as_string_result() {
let client_type = ClientType::Mock;
let type_string = client_type.as_str();
let client_type_from_str = ClientType::from_str(type_string).unwrap();
assert_eq!(client_type_from_str, client_type);
}

#[test]
fn parse_tendermint_as_string_result() {
let client_type = ClientType::Tendermint;
let type_string = client_type.as_str();
let client_type_from_str = ClientType::from_str(type_string).unwrap();
assert_eq!(client_type_from_str, client_type);
}
}
4 changes: 2 additions & 2 deletions modules/src/ics24_host/identifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,10 +168,10 @@ impl ClientId {
/// chain, for example, will have the prefix '07-tendermint'.
pub fn prefix(client_type: ClientType) -> &'static str {
match client_type {
ClientType::Tendermint => ClientType::Tendermint.as_string(),
ClientType::Tendermint => ClientType::Tendermint.as_str(),

#[cfg(any(test, feature = "mocks"))]
ClientType::Mock => ClientType::Mock.as_string(),
ClientType::Mock => ClientType::Mock.as_str(),
}
}

Expand Down
29 changes: 26 additions & 3 deletions modules/src/mock/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,18 @@ impl TryFrom<RawMockHeader> for MockHeader {

impl From<MockHeader> for RawMockHeader {
fn from(value: MockHeader) -> Self {
value.into()
RawMockHeader {
height: Some(value.height.into()),
timestamp: value.timestamp.as_nanoseconds(),
}
}
}

impl MockHeader {
pub fn height(&self) -> Height {
self.height
}

pub fn new(height: Height) -> Self {
Self {
height,
Expand All @@ -68,11 +72,11 @@ impl Header for MockHeader {
}

fn height(&self) -> Height {
todo!()
self.height
}

fn wrap_any(self) -> AnyHeader {
todo!()
AnyHeader::Mock(self)
}
}

Expand All @@ -81,3 +85,22 @@ impl From<MockHeader> for AnyConsensusState {
AnyConsensusState::Mock(MockConsensusState(h))
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn encode_any() {
let header = MockHeader::new(Height::new(1, 10));
let bytes = header.wrap_any().encode_vec().unwrap();

assert_eq!(
&bytes,
&[
10, 16, 47, 105, 98, 99, 46, 109, 111, 99, 107, 46, 72, 101, 97, 100, 101, 114, 18,
6, 10, 4, 8, 1, 16, 10,
]
);
}
}