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 2 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
20 changes: 18 additions & 2 deletions modules/src/ics02_client/client_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ impl std::str::FromStr for ClientType {
"07-tendermint" => Ok(Self::Tendermint),

#[cfg(any(test, feature = "mocks"))]
"mock" => Ok(Self::Mock),
"9999-mock" => Ok(Self::Mock),
romac marked this conversation as resolved.
Show resolved Hide resolved

_ => Err(error::Kind::UnknownClientType(s.to_string()).into()),
}
Expand All @@ -65,7 +65,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 +85,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_string();
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_string();
let client_type_from_str = ClientType::from_str(type_string).unwrap();
assert_eq!(client_type_from_str, client_type);
}
}
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,
]
);
}
}