Skip to content

Commit 343a003

Browse files
committed
implement aggregate func for TrustingPeriodContext
Signed-off-by: Jun Kimura <jun.kimura@datachain.jp>
1 parent abbf97c commit 343a003

File tree

1 file changed

+33
-26
lines changed

1 file changed

+33
-26
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,

0 commit comments

Comments
 (0)