Skip to content

Commit

Permalink
Add a pseudo root to MockConsensusState (#1219)
Browse files Browse the repository at this point in the history
* fix MockConsensusState root

* cargo fmt

* update CHANGELOG
  • Loading branch information
yito88 authored Jul 21, 2021
1 parent e513919 commit d139f47
Show file tree
Hide file tree
Showing 11 changed files with 46 additions and 29 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
- [ibc]
- Enable `pub` access to verification methods of ICS 03 & 04 ([#1198])
- Add `ics26_routing::handler::decode` function ([#1194])

- Add a pseudo root to `MockConsensusState` ([#1215])

- [ibc-relayer-cli]
- Added `upgrade-clients` CLI ([#763])

Expand Down
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions modules/src/ics02_client/handler/create_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ mod tests {

let msg = MsgCreateAnyClient::new(
MockClientState(MockHeader::new(height)).into(),
MockConsensusState(MockHeader::new(height)).into(),
MockConsensusState::new(MockHeader::new(height)).into(),
signer,
)
.unwrap();
Expand Down Expand Up @@ -138,7 +138,7 @@ mod tests {
..height
}))
.into(),
MockConsensusState(MockHeader::new(Height {
MockConsensusState::new(MockHeader::new(Height {
revision_height: 42,
..height
}))
Expand All @@ -152,7 +152,7 @@ mod tests {
..height
}))
.into(),
MockConsensusState(MockHeader::new(Height {
MockConsensusState::new(MockHeader::new(Height {
revision_height: 42,
..height
}))
Expand All @@ -166,7 +166,7 @@ mod tests {
..height
}))
.into(),
MockConsensusState(MockHeader::new(Height {
MockConsensusState::new(MockHeader::new(Height {
revision_height: 50,
..height
}))
Expand Down
6 changes: 3 additions & 3 deletions modules/src/ics02_client/handler/upgrade_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ mod tests {
let msg = MsgUpgradeAnyClient {
client_id: client_id.clone(),
client_state: MockClientState(MockHeader::new(Height::new(1, 26))).into(),
consensus_state: MockConsensusState(MockHeader::new(Height::new(1, 26))).into(),
consensus_state: MockConsensusState::new(MockHeader::new(Height::new(1, 26))).into(),
proof_upgrade_client: MerkleProof::try_from(c_bytes).unwrap(),
proof_upgrade_consensus_state: MerkleProof::try_from(cs_bytes).unwrap(),
signer,
Expand Down Expand Up @@ -167,7 +167,7 @@ mod tests {
let msg = MsgUpgradeAnyClient {
client_id: ClientId::from_str("nonexistingclient").unwrap(),
client_state: MockClientState(MockHeader::new(Height::new(1, 26))).into(),
consensus_state: MockConsensusState(MockHeader::new(Height::new(1, 26))).into(),
consensus_state: MockConsensusState::new(MockHeader::new(Height::new(1, 26))).into(),
proof_upgrade_client: MerkleProof::try_from(c_bytes).unwrap(),
proof_upgrade_consensus_state: MerkleProof::try_from(cs_bytes).unwrap(),
signer,
Expand Down Expand Up @@ -201,7 +201,7 @@ mod tests {
let msg = MsgUpgradeAnyClient {
client_id,
client_state: MockClientState(MockHeader::new(Height::new(0, 26))).into(),
consensus_state: MockConsensusState(MockHeader::new(Height::new(0, 26))).into(),
consensus_state: MockConsensusState::new(MockHeader::new(Height::new(0, 26))).into(),
proof_upgrade_client: MerkleProof::try_from(c_bytes).unwrap(),
proof_upgrade_consensus_state: MerkleProof::try_from(cs_bytes).unwrap(),
signer,
Expand Down
5 changes: 3 additions & 2 deletions modules/src/ics02_client/msgs/upgrade_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ pub mod test_util {
AnyClientState::Mock(MockClientState(MockHeader::new(height))).into(),
),
consensus_state: Some(
AnyConsensusState::Mock(MockConsensusState(MockHeader::new(height))).into(),
AnyConsensusState::Mock(MockConsensusState::new(MockHeader::new(height))).into(),
),
proof_upgrade_client: get_dummy_proof(),
proof_upgrade_consensus_state: get_dummy_proof(),
Expand Down Expand Up @@ -178,7 +178,8 @@ mod tests {
let height = Height::new(1, 1);

let client_state = AnyClientState::Mock(MockClientState(MockHeader::new(height)));
let consensus_state = AnyConsensusState::Mock(MockConsensusState(MockHeader::new(height)));
let consensus_state =
AnyConsensusState::Mock(MockConsensusState::new(MockHeader::new(height)));

let proof = get_dummy_merkle_proof();

Expand Down
8 changes: 5 additions & 3 deletions modules/src/ics26_routing/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,9 @@ mod tests {

let create_client_msg = MsgCreateAnyClient::new(
AnyClientState::from(MockClientState(MockHeader::new(start_client_height))),
AnyConsensusState::from(MockConsensusState(MockHeader::new(start_client_height))),
AnyConsensusState::Mock(MockConsensusState::new(MockHeader::new(
start_client_height,
))),
default_signer.clone(),
)
.unwrap();
Expand Down Expand Up @@ -434,7 +436,7 @@ mod tests {
msg: Ics26Envelope::Ics2Msg(ClientMsg::UpgradeClient(MsgUpgradeAnyClient::new(
client_id.clone(),
AnyClientState::Mock(MockClientState(MockHeader::new(upgrade_client_height))),
AnyConsensusState::Mock(MockConsensusState(MockHeader::new(
AnyConsensusState::Mock(MockConsensusState::new(MockHeader::new(
upgrade_client_height,
))),
get_dummy_merkle_proof(),
Expand All @@ -450,7 +452,7 @@ mod tests {
AnyClientState::Mock(MockClientState(MockHeader::new(
upgrade_client_height_second,
))),
AnyConsensusState::Mock(MockConsensusState(MockHeader::new(
AnyConsensusState::Mock(MockConsensusState::new(MockHeader::new(
upgrade_client_height_second,
))),
get_dummy_merkle_proof(),
Expand Down
4 changes: 2 additions & 2 deletions modules/src/mock/client_def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ impl ClientDef for MockClient {
"received header height is lower than (or equal to) client latest height".into(),
);
}
Ok((MockClientState(header), MockConsensusState(header)))
Ok((MockClientState(header), MockConsensusState::new(header)))
}

fn verify_client_consensus_state(
Expand Down Expand Up @@ -156,6 +156,6 @@ impl ClientDef for MockClient {
_proof_upgrade_client: MerkleProof,
_proof_upgrade_consensus_state: MerkleProof,
) -> Result<(Self::ClientState, Self::ConsensusState), Box<dyn std::error::Error>> {
Ok((*client_state, *consensus_state))
Ok((*client_state, consensus_state.clone()))
}
}
31 changes: 22 additions & 9 deletions modules/src/mock/client_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,16 +104,26 @@ impl ClientState for MockClientState {

impl From<MockConsensusState> for MockClientState {
fn from(cs: MockConsensusState) -> Self {
Self(cs.0)
Self(cs.header)
}
}

#[derive(Copy, Clone, Debug, PartialEq, Eq, Serialize)]
pub struct MockConsensusState(pub MockHeader);
#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
pub struct MockConsensusState {
pub header: MockHeader,
pub root: CommitmentRoot,
}

impl MockConsensusState {
pub fn new(header: MockHeader) -> Self {
MockConsensusState {
header,
root: CommitmentRoot::from(vec![0]),
}
}

pub fn timestamp(&self) -> Timestamp {
(self.0).timestamp
self.header.timestamp
}
}

Expand All @@ -127,16 +137,19 @@ impl TryFrom<RawMockConsensusState> for MockConsensusState {
.header
.ok_or_else(|| ClientKind::InvalidRawConsensusState.context("missing header"))?;

Ok(Self(MockHeader::try_from(raw_header)?))
Ok(Self {
header: MockHeader::try_from(raw_header)?,
root: CommitmentRoot::from(vec![0]),
})
}
}

impl From<MockConsensusState> for RawMockConsensusState {
fn from(value: MockConsensusState) -> Self {
RawMockConsensusState {
header: Some(ibc_proto::ibc::mock::Header {
height: Some(value.0.height().into()),
timestamp: (value.0).timestamp.as_nanoseconds(),
height: Some(value.header.height().into()),
timestamp: value.header.timestamp.as_nanoseconds(),
}),
}
}
Expand All @@ -154,11 +167,11 @@ impl ConsensusState for MockConsensusState {
}

fn root(&self) -> &CommitmentRoot {
todo!()
&self.root
}

fn validate_basic(&self) -> Result<(), Box<dyn std::error::Error>> {
todo!()
Ok(())
}

fn wrap_any(self) -> AnyConsensusState {
Expand Down
2 changes: 1 addition & 1 deletion modules/src/mock/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ impl MockContext {
// If it's a mock client, create the corresponding mock states.
ClientType::Mock => (
Some(MockClientState(MockHeader::new(client_state_height)).into()),
MockConsensusState(MockHeader::new(cs_height)).into(),
MockConsensusState::new(MockHeader::new(cs_height)).into(),
),
// If it's a Tendermint client, we need TM states.
ClientType::Tendermint => {
Expand Down
2 changes: 1 addition & 1 deletion modules/src/mock/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ impl Header for MockHeader {

impl From<MockHeader> for AnyConsensusState {
fn from(h: MockHeader) -> Self {
AnyConsensusState::Mock(MockConsensusState(h))
AnyConsensusState::Mock(MockConsensusState::new(h))
}
}

Expand Down
2 changes: 1 addition & 1 deletion modules/tests/runner/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ impl IbcTestRunner {
}

pub fn consensus_state(height: u64) -> AnyConsensusState {
AnyConsensusState::Mock(MockConsensusState(Self::mock_header(height)))
AnyConsensusState::Mock(MockConsensusState::new(Self::mock_header(height)))
}

fn signer() -> Signer {
Expand Down

0 comments on commit d139f47

Please sign in to comment.