Skip to content

Commit

Permalink
feat(ethereum-lc): implement misbehaviour check on header
Browse files Browse the repository at this point in the history
Signed-off-by: aeryz <abdullaheryz@protonmail.com>
  • Loading branch information
aeryz committed Sep 8, 2023
1 parent a175834 commit b8d65dc
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 10 deletions.
4 changes: 2 additions & 2 deletions lib/ics-008-wasm-client/src/ibc_client.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use core::fmt::Debug;
use core::fmt::{Debug, Display};

use cosmwasm_std::{Binary, Deps, DepsMut, Env, MessageInfo};
use unionlabs::{
Expand All @@ -22,7 +22,7 @@ pub enum StorageState {
pub trait IbcClient {
type Error: From<TryFromProtoBytesError<TryFromProtoErrorOf<Self::Header>>>
+ From<Error>
+ ToString;
+ Display;
type CustomQuery: cosmwasm_std::CustomQuery;
// TODO(aeryz): see #583
type Header: TryFromProto;
Expand Down
34 changes: 26 additions & 8 deletions light-clients/ethereum-light-client/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,14 +284,8 @@ impl IbcClient for EthereumLightClient {
deps: Deps<Self::CustomQuery>,
header: Self::Header,
) -> Result<ContractResult, Self::Error> {
let height = Height::new(
0,
header
.consensus_update
.attested_header
.execution
.block_number,
);
let height = Height::new(0, header.consensus_update.attested_header.beacon.slot);

if let Some(consensus_state) =
read_consensus_state::<CustomQuery, ConsensusState>(deps, &height)?
{
Expand All @@ -308,6 +302,28 @@ impl IbcClient for EthereumLightClient {
if consensus_state.data.storage_root != storage_root {
return Err(Error::StorageRootMismatch);
}

if consensus_state.data.slot != header.consensus_update.attested_header.beacon.slot {
return Err(Error::SlotCannotBeModified);
}

// Next sync committee for a consensus height can be set if it is not being set
// previously, but it cannot be changed or unset after being set.
match (
consensus_state.data.next_sync_committee,
header.consensus_update.next_sync_committee,
) {
(Some(_), None) => return Err(Error::NextSyncCommitteeCannotBeModified),
(Some(lhs), Some(rhs)) => {
if lhs != rhs.aggregate_pubkey {
return Err(Error::NextSyncCommitteeCannotBeModified);
}
}
_ => {}
}

// NOTE(aeryz): we don't check the timestamp here since it is calculated based on the
// client state and the slot number during update.
}

// TODO(#605): Do we need to check whether this header's timestamp is between
Expand Down Expand Up @@ -718,6 +734,8 @@ mod test {
];

for update in updates {
EthereumLightClient::check_for_misbehaviour_on_header(deps.as_ref(), update.clone())
.unwrap();
EthereumLightClient::verify_header(deps.as_ref(), mock_env(), update.clone()).unwrap();
EthereumLightClient::update_state(deps.as_mut(), mock_env(), update.clone()).unwrap();
// Consensus state is saved to the updated height.
Expand Down
6 changes: 6 additions & 0 deletions light-clients/ethereum-light-client/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,12 @@ pub enum Error {

#[error("Wasm client error: {0}")]
Wasm(String),

#[error("Next sync committee can't be changed after being set.")]
NextSyncCommitteeCannotBeModified,

#[error("The slot number that is saved previously to the consensus state cannot be changed.")]
SlotCannotBeModified,
}

impl Error {
Expand Down

0 comments on commit b8d65dc

Please sign in to comment.