Skip to content

Commit a680eab

Browse files
authored
Merge pull request #103 from datachainlab/fix-tendermint-misbehaviour-context
tendermint-lc: add TrustingPeriodContext to misbehaviour message Signed-off-by: Jun Kimura <jun.kimura@datachain.jp>
2 parents abbf97c + 420be96 commit a680eab

File tree

2 files changed

+58
-31
lines changed

2 files changed

+58
-31
lines changed

modules/commitments/src/context.rs

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -48,32 +48,7 @@ impl ValidationContext {
4848
(Self::Empty, Self::TrustingPeriod(ctx)) => Ok(Self::TrustingPeriod(ctx)),
4949
(Self::TrustingPeriod(ctx), Self::Empty) => Ok(Self::TrustingPeriod(ctx)),
5050
(Self::TrustingPeriod(ctx1), Self::TrustingPeriod(ctx2)) => {
51-
if ctx1.trusting_period != ctx2.trusting_period {
52-
return Err(Error::context_aggregation_failed(format!(
53-
"trusting_period mismatch: ctx1={:?} ctx2={:?}",
54-
ctx1.trusting_period, ctx2.trusting_period,
55-
)));
56-
}
57-
if ctx1.clock_drift != ctx2.clock_drift {
58-
return Err(Error::context_aggregation_failed(format!(
59-
"clock_drift mismatch: ctx1={:?} ctx2={:?}",
60-
ctx1.clock_drift, ctx2.clock_drift
61-
)));
62-
}
63-
Ok(Self::TrustingPeriod(TrustingPeriodContext::new(
64-
ctx1.trusting_period,
65-
ctx1.clock_drift,
66-
if ctx1.untrusted_header_timestamp > ctx2.untrusted_header_timestamp {
67-
ctx1.untrusted_header_timestamp
68-
} else {
69-
ctx2.untrusted_header_timestamp
70-
},
71-
if ctx1.trusted_state_timestamp < ctx2.trusted_state_timestamp {
72-
ctx1.trusted_state_timestamp
73-
} else {
74-
ctx2.trusted_state_timestamp
75-
},
76-
)))
51+
Ok(Self::TrustingPeriod(ctx1.aggregate(ctx2)?))
7752
}
7853
}
7954
}
@@ -210,6 +185,38 @@ impl TrustingPeriodContext {
210185
Ok(())
211186
}
212187

188+
pub fn aggregate(self, other: Self) -> Result<Self, Error> {
189+
if self.trusting_period != other.trusting_period {
190+
return Err(Error::context_aggregation_failed(format!(
191+
"trusting_period mismatch: self={:?} other={:?}",
192+
self.trusting_period, other.trusting_period,
193+
)));
194+
}
195+
if self.clock_drift != other.clock_drift {
196+
return Err(Error::context_aggregation_failed(format!(
197+
"clock_drift mismatch: self={:?} other={:?}",
198+
self.clock_drift, other.clock_drift
199+
)));
200+
}
201+
Ok(Self {
202+
trusting_period: self.trusting_period,
203+
clock_drift: self.clock_drift,
204+
untrusted_header_timestamp: if self.untrusted_header_timestamp
205+
> other.untrusted_header_timestamp
206+
{
207+
self.untrusted_header_timestamp
208+
} else {
209+
other.untrusted_header_timestamp
210+
},
211+
trusted_state_timestamp: if self.trusted_state_timestamp < other.trusted_state_timestamp
212+
{
213+
self.trusted_state_timestamp
214+
} else {
215+
other.trusted_state_timestamp
216+
},
217+
})
218+
}
219+
213220
fn ensure_within_trust_period(
214221
now: Time,
215222
trusted_state_time: Time,

modules/tendermint-lc/src/client.rs

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ impl TendermintLightClient {
348348
.clone(),
349349
);
350350

351-
let prev_states = self.make_prev_states(
351+
let (prev_states, trusted_consensus_state_timestamps) = self.make_prev_states(
352352
ctx,
353353
&client_id,
354354
&client_state,
@@ -357,12 +357,29 @@ impl TendermintLightClient {
357357
misbehaviour.header2().trusted_height.into(),
358358
],
359359
)?;
360-
360+
let lc_opts = client_state.as_light_client_options().unwrap();
361361
Ok(MisbehaviourData {
362362
new_any_client_state: new_client_state.into(),
363363
message: MisbehaviourProxyMessage {
364364
prev_states,
365-
context: ValidationContext::Empty,
365+
context: TrustingPeriodContext::new(
366+
lc_opts.trusting_period,
367+
lc_opts.clock_drift,
368+
misbehaviour.header1().timestamp().into(),
369+
trusted_consensus_state_timestamps[0],
370+
)
371+
.aggregate(TrustingPeriodContext::new(
372+
lc_opts.trusting_period,
373+
lc_opts.clock_drift,
374+
misbehaviour.header2().timestamp().into(),
375+
trusted_consensus_state_timestamps[1],
376+
))
377+
.map_err(|e| {
378+
Error::ics02(ICS02Error::ClientSpecific {
379+
description: e.to_string(),
380+
})
381+
})?
382+
.into(),
366383
client_message: Any::from(misbehaviour),
367384
},
368385
})
@@ -374,8 +391,9 @@ impl TendermintLightClient {
374391
client_id: &ClientId,
375392
client_state: &ClientState,
376393
heights: Vec<Height>,
377-
) -> Result<Vec<PrevState>, LightClientError> {
394+
) -> Result<(Vec<PrevState>, Vec<Time>), LightClientError> {
378395
let mut prev_states = Vec::new();
396+
let mut timestamps = Vec::new();
379397
for height in heights {
380398
let ibc_height = height.try_into().map_err(Error::ics02)?;
381399
let consensus_state: ConsensusState = ctx
@@ -387,13 +405,15 @@ impl TendermintLightClient {
387405
})
388406
})?
389407
.try_into()?;
408+
let timestamp = consensus_state.timestamp().into();
390409
let prev_state_id = gen_state_id(canonicalize_state(client_state), consensus_state)?;
391410
prev_states.push(PrevState {
392411
height,
393412
state_id: prev_state_id,
394413
});
414+
timestamps.push(timestamp);
395415
}
396-
Ok(prev_states)
416+
Ok((prev_states, timestamps))
397417
}
398418
}
399419

0 commit comments

Comments
 (0)