Skip to content

Commit

Permalink
add misbehaviour test for lcp-client
Browse files Browse the repository at this point in the history
Signed-off-by: Jun Kimura <[email protected]>
  • Loading branch information
bluele committed Feb 2, 2024
1 parent 239949c commit 3c63abc
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 5 deletions.
51 changes: 48 additions & 3 deletions modules/lcp-client/src/client_def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,7 @@ mod tests {
use ibc::{
mock::{
client_state::MockClientState, consensus_state::MockConsensusState, header::MockHeader,
misbehaviour::Misbehaviour as MockMisbehaviour,
},
Height as ICS02Height,
};
Expand Down Expand Up @@ -407,7 +408,7 @@ mod tests {
let proof1 = {
let header = MockHeader::new(ICS02Height::new(0, 2).unwrap());

let mut ctx = Context::new(registry.clone(), lcp_store, &ek);
let mut ctx = Context::new(registry.clone(), lcp_store.clone(), &ek);
ctx.set_timestamp(Time::now());
let res = mock_client.update_client(
&ctx,
Expand Down Expand Up @@ -437,7 +438,7 @@ mod tests {

ctx.store_any_client_state(upstream_client_id.clone(), client_state)
.unwrap();
ctx.store_any_consensus_state(upstream_client_id, height, consensus_state)
ctx.store_any_consensus_state(upstream_client_id.clone(), height, consensus_state)
.unwrap();
res.unwrap()
};
Expand All @@ -449,7 +450,51 @@ mod tests {
signer: proof1.signer,
signature: proof1.signature,
});
let mut ctx = Context::new(registry.clone(), ibc_store, &ek);
let mut ctx = Context::new(registry.clone(), ibc_store.clone(), &ek);
ctx.set_timestamp((Time::now() + Duration::from_secs(60)).unwrap());

let res = lcp_client.update_state(&mut ctx, lcp_client_id.clone(), header);
assert!(res.is_ok(), "res={:?}", res);
}

// 6. on the upstream side, updates the Light Client state with a misbehaviour
let misbehaviour_proof = {
let mut ctx = Context::new(registry.clone(), lcp_store, &ek);
ctx.set_timestamp(Time::now());

let mock_misbehaviour = MockMisbehaviour {
client_id: upstream_client_id.clone().into(),
header1: MockHeader::new(ICS02Height::new(0, 3).unwrap()),
header2: MockHeader::new(ICS02Height::new(0, 3).unwrap()),
};
let res = mock_client
.update_client(
&ctx,
upstream_client_id,
mock_lc::Misbehaviour::from(mock_misbehaviour).into(),
)
.unwrap();
let data = match res {
UpdateClientResult::SubmitMisbehaviour(data) => data,
_ => unreachable!(),
};
let res = prove_commitment(
ctx.get_enclave_key(),
ctx.get_enclave_key().pubkey().unwrap().as_address(),
data.message.into(),
);
assert!(res.is_ok(), "res={:?}", res);
res.unwrap()
};

// 7. on the downstream side, updates LCP Light Client's state with the message from the ELC
{
let header = ClientMessage::Misbehaviour(MisbehaviourMessage {
elc_message: misbehaviour_proof.message().unwrap().try_into().unwrap(),
signer: misbehaviour_proof.signer,
signature: misbehaviour_proof.signature,
});
let mut ctx = Context::new(registry, ibc_store, &ek);
ctx.set_timestamp((Time::now() + Duration::from_secs(60)).unwrap());

let res = lcp_client.update_state(&mut ctx, lcp_client_id, header);
Expand Down
3 changes: 2 additions & 1 deletion modules/mock-lc/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,8 @@ impl MockLightClient {
client_id: ClientId,
misbehaviour: Misbehaviour,
) -> Result<SubmitMisbehaviourData, LightClientError> {
assert_eq!(client_id.as_str(), misbehaviour.client_id.as_str());
// WARNING: misbehaviour of ibc-rs's mock client has a bug where the client_id is set to `07-tendermint-0` when decoding from `Any`.
// assert_eq!(client_id, misbehaviour.client_id());

// Read client state from the host chain store.
let client_state: ClientState = ctx.client_state(&client_id)?.try_into()?;
Expand Down
2 changes: 1 addition & 1 deletion modules/mock-lc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ mod prelude {
}

pub use client::{register_implementations, MockLightClient};
pub use message::Header;
pub use message::{Header, Misbehaviour};
pub use state::{ClientState, ConsensusState};

mod client;
Expand Down

0 comments on commit 3c63abc

Please sign in to comment.