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

tendermint-lc: add TrustingPeriodContext to misbehaviour message #103

Merged
merged 2 commits into from
Feb 6, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
59 changes: 33 additions & 26 deletions modules/commitments/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,32 +48,7 @@ impl ValidationContext {
(Self::Empty, Self::TrustingPeriod(ctx)) => Ok(Self::TrustingPeriod(ctx)),
(Self::TrustingPeriod(ctx), Self::Empty) => Ok(Self::TrustingPeriod(ctx)),
(Self::TrustingPeriod(ctx1), Self::TrustingPeriod(ctx2)) => {
if ctx1.trusting_period != ctx2.trusting_period {
return Err(Error::context_aggregation_failed(format!(
"trusting_period mismatch: ctx1={:?} ctx2={:?}",
ctx1.trusting_period, ctx2.trusting_period,
)));
}
if ctx1.clock_drift != ctx2.clock_drift {
return Err(Error::context_aggregation_failed(format!(
"clock_drift mismatch: ctx1={:?} ctx2={:?}",
ctx1.clock_drift, ctx2.clock_drift
)));
}
Ok(Self::TrustingPeriod(TrustingPeriodContext::new(
ctx1.trusting_period,
ctx1.clock_drift,
if ctx1.untrusted_header_timestamp > ctx2.untrusted_header_timestamp {
ctx1.untrusted_header_timestamp
} else {
ctx2.untrusted_header_timestamp
},
if ctx1.trusted_state_timestamp < ctx2.trusted_state_timestamp {
ctx1.trusted_state_timestamp
} else {
ctx2.trusted_state_timestamp
},
)))
Ok(Self::TrustingPeriod(ctx1.aggregate(ctx2)?))
}
}
}
Expand Down Expand Up @@ -210,6 +185,38 @@ impl TrustingPeriodContext {
Ok(())
}

pub fn aggregate(self, other: Self) -> Result<Self, Error> {
if self.trusting_period != other.trusting_period {
return Err(Error::context_aggregation_failed(format!(
"trusting_period mismatch: self={:?} other={:?}",
self.trusting_period, other.trusting_period,
)));
}
if self.clock_drift != other.clock_drift {
return Err(Error::context_aggregation_failed(format!(
"clock_drift mismatch: self={:?} other={:?}",
self.clock_drift, other.clock_drift
)));
}
Ok(Self {
trusting_period: self.trusting_period,
clock_drift: self.clock_drift,
untrusted_header_timestamp: if self.untrusted_header_timestamp
> other.untrusted_header_timestamp
{
self.untrusted_header_timestamp
} else {
other.untrusted_header_timestamp
},
trusted_state_timestamp: if self.trusted_state_timestamp < other.trusted_state_timestamp
{
self.trusted_state_timestamp
} else {
other.trusted_state_timestamp
},
})
}

fn ensure_within_trust_period(
now: Time,
trusted_state_time: Time,
Expand Down
30 changes: 25 additions & 5 deletions modules/tendermint-lc/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ impl TendermintLightClient {
.clone(),
);

let prev_states = self.make_prev_states(
let (prev_states, trusted_consensus_state_timestamps) = self.make_prev_states(
ctx,
&client_id,
&client_state,
Expand All @@ -357,12 +357,29 @@ impl TendermintLightClient {
misbehaviour.header2().trusted_height.into(),
],
)?;

let lc_opts = client_state.as_light_client_options().unwrap();
Ok(MisbehaviourData {
new_any_client_state: new_client_state.into(),
message: MisbehaviourProxyMessage {
prev_states,
context: ValidationContext::Empty,
context: TrustingPeriodContext::new(
lc_opts.trusting_period,
lc_opts.clock_drift,
misbehaviour.header1().timestamp().into(),
trusted_consensus_state_timestamps[0],
)
.aggregate(TrustingPeriodContext::new(
lc_opts.trusting_period,
lc_opts.clock_drift,
misbehaviour.header2().timestamp().into(),
trusted_consensus_state_timestamps[1],
))
.map_err(|e| {
Error::ics02(ICS02Error::ClientSpecific {
description: e.to_string(),
})
})?
.into(),
client_message: Any::from(misbehaviour),
},
})
Expand All @@ -374,8 +391,9 @@ impl TendermintLightClient {
client_id: &ClientId,
client_state: &ClientState,
heights: Vec<Height>,
) -> Result<Vec<PrevState>, LightClientError> {
) -> Result<(Vec<PrevState>, Vec<Time>), LightClientError> {
let mut prev_states = Vec::new();
let mut timestamps = Vec::new();
for height in heights {
let ibc_height = height.try_into().map_err(Error::ics02)?;
let consensus_state: ConsensusState = ctx
Expand All @@ -387,13 +405,15 @@ impl TendermintLightClient {
})
})?
.try_into()?;
let timestamp = consensus_state.timestamp().into();
let prev_state_id = gen_state_id(canonicalize_state(client_state), consensus_state)?;
prev_states.push(PrevState {
height,
state_id: prev_state_id,
});
timestamps.push(timestamp);
}
Ok(prev_states)
Ok((prev_states, timestamps))
}
}

Expand Down
Loading