Skip to content

Commit

Permalink
tendermint-lc: remove some validations
Browse files Browse the repository at this point in the history
Signed-off-by: Jun Kimura <[email protected]>
  • Loading branch information
bluele committed Feb 6, 2024
1 parent efbbf26 commit d4f7410
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 143 deletions.
32 changes: 1 addition & 31 deletions modules/tendermint-lc/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ use light_client::{
LightClientRegistry, UpdateClientResult, VerifyMembershipResult,
};
use light_client::{MisbehaviourData, UpdateStateData, VerifyNonMembershipResult};
#[allow(unused_imports)]
use log::*;

#[derive(Default)]
Expand Down Expand Up @@ -239,37 +240,6 @@ impl TendermintLightClient {
.into());
}

// Read consensus state from the host chain store.
let latest_consensus_state: ConsensusState = ctx
.consensus_state(&client_id, &client_state.latest_height().into())
.map_err(|_| {
Error::ics02(ICS02Error::ConsensusStateNotFound {
client_id: client_id.clone().into(),
height: client_state.latest_height(),
})
})?
.try_into()?;

debug!("latest consensus state: {:?}", latest_consensus_state);

let now = ctx.host_timestamp();
let duration = now
.duration_since(latest_consensus_state.timestamp().into_tm_time().unwrap())
.map_err(|_| {
Error::ics02(ICS02Error::InvalidConsensusStateTimestamp {
time1: latest_consensus_state.timestamp(),
time2: now.into(),
})
})?;

if client_state.expired(duration) {
return Err(Error::ics02(ICS02Error::HeaderNotWithinTrustPeriod {
latest_time: latest_consensus_state.timestamp(),
update_time: header.timestamp(),
})
.into());
}

let height = header.height().into();
let header_timestamp: Time = header.timestamp().into();

Expand Down
112 changes: 0 additions & 112 deletions modules/tendermint-lc/src/verifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,25 +29,6 @@ pub(crate) fn check_header_and_update_state(
client_id: ClientId,
header: Any,
) -> Result<UpdatedState, ClientError> {
fn maybe_consensus_state(
ctx: &dyn ValidationContext,
client_cons_state_path: &ClientConsensusStatePath,
) -> Result<Option<Box<dyn ConsensusState>>, ClientError> {
match ctx.consensus_state(client_cons_state_path) {
Ok(cs) => Ok(Some(cs)),
Err(e) => match e {
ContextError::ClientError(ClientError::ConsensusStateNotFound {
client_id: _,
height: _,
}) => Ok(None),
ContextError::ClientError(e) => Err(e),
_ => Err(ClientError::Other {
description: e.to_string(),
}),
},
}
}

let client_state = downcast_tm_client_state(client_state)?.clone();
let header = TmHeader::try_from(header)?;

Expand All @@ -61,28 +42,6 @@ pub(crate) fn check_header_and_update_state(
});
}

// Check if a consensus state is already installed; if so it should
// match the untrusted header.
let header_consensus_state = TmConsensusState::from(header.clone());
let client_cons_state_path = ClientConsensusStatePath::new(&client_id, &header.height());
let existing_consensus_state = match maybe_consensus_state(ctx, &client_cons_state_path)? {
Some(cs) => {
let cs = downcast_tm_consensus_state(cs.as_ref())?;
// If this consensus state matches, skip verification
// (optimization)
if cs == header_consensus_state {
// Header is already installed and matches the incoming
// header (already verified)
return Ok(UpdatedState {
client_state: client_state.into_box(),
consensus_state: cs.into_box(),
});
}
Some(cs)
}
None => None,
};

let trusted_client_cons_state_path =
ClientConsensusStatePath::new(&client_id, &header.trusted_height);
let trusted_consensus_state = downcast_tm_consensus_state(
Expand Down Expand Up @@ -137,77 +96,6 @@ pub(crate) fn check_header_and_update_state(
Verdict::Invalid(detail) => Err(Error::VerificationError { detail }),
}?;

// If the header has verified, but its corresponding consensus state
// differs from the existing consensus state for that height, freeze the
// client and return the installed consensus state.
if let Some(cs) = existing_consensus_state {
if cs != header_consensus_state {
return Ok(UpdatedState {
client_state: client_state.with_frozen_height(header.height()).into_box(),
consensus_state: cs.into_box(),
});
}
}

// Monotonicity checks for timestamps for in-the-middle updates
// (cs-new, cs-next, cs-latest)
if header.height() < client_state.latest_height() {
let maybe_next_cs = ctx
.next_consensus_state(&client_id, &header.height())
.map_err(|e| match e {
ContextError::ClientError(e) => e,
_ => ClientError::Other {
description: e.to_string(),
},
})?
.as_ref()
.map(|cs| downcast_tm_consensus_state(cs.as_ref()))
.transpose()?;

if let Some(next_cs) = maybe_next_cs {
// New (untrusted) header timestamp cannot occur after next
// consensus state's height
if header.signed_header.header().time > next_cs.timestamp {
return Err(ClientError::ClientSpecific {
description: Error::HeaderTimestampTooHigh {
actual: header.signed_header.header().time.to_string(),
max: next_cs.timestamp.to_string(),
}
.to_string(),
});
}
}
}

// (cs-trusted, cs-prev, cs-new)
if header.trusted_height < header.height() {
let maybe_prev_cs = ctx
.prev_consensus_state(&client_id, &header.height())
.map_err(|e| match e {
ContextError::ClientError(e) => e,
_ => ClientError::Other {
description: e.to_string(),
},
})?
.as_ref()
.map(|cs| downcast_tm_consensus_state(cs.as_ref()))
.transpose()?;

if let Some(prev_cs) = maybe_prev_cs {
// New (untrusted) header timestamp cannot occur before the
// previous consensus state's height
if header.signed_header.header().time < prev_cs.timestamp {
return Err(ClientError::ClientSpecific {
description: Error::HeaderTimestampTooLow {
actual: header.signed_header.header().time.to_string(),
min: prev_cs.timestamp.to_string(),
}
.to_string(),
});
}
}
}

Ok(UpdatedState {
client_state: client_state.with_header(header.clone())?.into_box(),
consensus_state: TmConsensusState::from(header).into_box(),
Expand Down

0 comments on commit d4f7410

Please sign in to comment.