Skip to content

Commit 881682d

Browse files
authored
Merge pull request #116 from datachainlab/fix-context-timestamp
Fix to always keep the current timestamp in `Context` Signed-off-by: Jun Kimura <jun.kimura@datachain.jp>
2 parents 1db32ff + 77349df commit 881682d

File tree

10 files changed

+38
-50
lines changed

10 files changed

+38
-50
lines changed

enclave-modules/ecall-handler/src/light_client/aggregate_messages.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ pub fn aggregate_messages<R: LightClientResolver, S: KVStore, K: Signer>(
1313
ctx: &mut Context<R, S, K>,
1414
input: AggregateMessagesInput,
1515
) -> Result<LightClientResponse, Error> {
16-
ctx.set_timestamp(input.current_timestamp);
17-
1816
if input.messages.len() < 2 {
1917
return Err(Error::invalid_argument(
2018
"messages and signatures must have at least 2 elements".into(),

enclave-modules/ecall-handler/src/light_client/init_client.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ pub fn init_client<R: LightClientResolver, S: KVStore, K: Signer>(
1313
ctx: &mut Context<R, S, K>,
1414
input: InitClientInput,
1515
) -> Result<LightClientResponse, Error> {
16-
ctx.set_timestamp(input.current_timestamp);
17-
1816
let lc = match ctx.get_light_client(&input.any_client_state.type_url) {
1917
Some(lc) => lc,
2018
None => {

enclave-modules/ecall-handler/src/light_client/router.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,12 @@ pub fn dispatch<E: Env>(
2121
let sealed_ek = cctx
2222
.sealed_ek
2323
.ok_or(Error::sealed_enclave_key_not_found())?;
24-
let mut ctx =
25-
Context::new(env.get_lc_registry(), env.new_store(cctx.tx_id), &sealed_ek);
24+
let mut ctx = Context::new(
25+
env.get_lc_registry(),
26+
env.new_store(cctx.tx_id),
27+
&sealed_ek,
28+
cctx.current_timestamp,
29+
);
2630
match cmd {
2731
InitClient(input) => init_client(&mut ctx, input)?,
2832
UpdateClient(input) => update_client(&mut ctx, input)?,
@@ -33,8 +37,12 @@ pub fn dispatch<E: Env>(
3337
}
3438
LightClientCommand::Query(cmd) => {
3539
use LightClientQueryCommand::*;
36-
let mut ctx =
37-
Context::new(env.get_lc_registry(), env.new_store(cctx.tx_id), &NopSigner);
40+
let mut ctx = Context::new(
41+
env.get_lc_registry(),
42+
env.new_store(cctx.tx_id),
43+
&NopSigner,
44+
cctx.current_timestamp,
45+
);
3846
match cmd {
3947
QueryClient(input) => query_client(&mut ctx, input)?,
4048
}

enclave-modules/ecall-handler/src/light_client/update_client.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ pub fn update_client<R: LightClientResolver, S: KVStore, K: Signer>(
1212
ctx: &mut Context<R, S, K>,
1313
input: UpdateClientInput,
1414
) -> Result<LightClientResponse, Error> {
15-
ctx.set_timestamp(input.current_timestamp);
16-
1715
let lc = get_light_client_by_client_id(ctx, &input.client_id)?;
1816
let ek = ctx.get_enclave_key();
1917
match lc.update_client(ctx, input.client_id.clone(), input.any_header)? {

modules/context/src/context.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,19 @@ pub struct Context<'k, R: LightClientResolver, S: KVStore, K: Signer> {
1111
lc_registry: R,
1212
store: S,
1313
ek: &'k K,
14-
current_timestamp: Option<Time>,
14+
current_timestamp: Time,
1515
}
1616

1717
impl<'k, R: LightClientResolver, S: KVStore, K: Signer> Context<'k, R, S, K> {
18-
pub fn new(lc_registry: R, store: S, ek: &'k K) -> Self {
18+
pub fn new(lc_registry: R, store: S, ek: &'k K, current_timestamp: Time) -> Self {
1919
Self {
2020
lc_registry,
2121
store,
2222
ek,
23-
current_timestamp: None,
23+
current_timestamp,
2424
}
2525
}
2626

27-
pub fn set_timestamp(&mut self, timestamp: Time) {
28-
self.current_timestamp = Some(timestamp)
29-
}
30-
3127
pub fn get_enclave_key(&self) -> &'k dyn Signer {
3228
self.ek
3329
}
@@ -49,7 +45,7 @@ impl<'k, R: LightClientResolver, S: KVStore, K: Signer> KVStore for Context<'k,
4945

5046
impl<'k, R: LightClientResolver, S: KVStore, K: Signer> HostContext for Context<'k, R, S, K> {
5147
fn host_timestamp(&self) -> Time {
52-
self.current_timestamp.unwrap()
48+
self.current_timestamp
5349
}
5450
}
5551

modules/ecall-commands/src/light_client.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::{prelude::*, EnclaveKeySelector};
22
use commitments::CommitmentProof;
33
use crypto::Address;
4-
use lcp_types::{Any, ClientId, Height, Time};
4+
use lcp_types::{Any, ClientId, Height};
55
use serde::{Deserialize, Serialize};
66

77
#[derive(Serialize, Deserialize, Debug)]
@@ -44,7 +44,6 @@ pub struct InitClientInput {
4444
pub client_id: String,
4545
pub any_client_state: Any,
4646
pub any_consensus_state: Any,
47-
pub current_timestamp: Time,
4847
pub signer: Address,
4948
}
5049

@@ -53,7 +52,6 @@ pub struct UpdateClientInput {
5352
pub client_id: ClientId,
5453
pub any_header: Any,
5554
pub include_state: bool,
56-
pub current_timestamp: Time,
5755
pub signer: Address,
5856
}
5957

@@ -62,7 +60,6 @@ pub struct AggregateMessagesInput {
6260
pub signer: Address,
6361
pub messages: Vec<Vec<u8>>,
6462
pub signatures: Vec<Vec<u8>>,
65-
pub current_timestamp: Time,
6663
}
6764

6865
#[derive(Serialize, Deserialize, Debug)]

modules/ecall-commands/src/msgs.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use lcp_types::proto::lcp::service::elc::v1::{
99
MsgVerifyNonMembership, MsgVerifyNonMembershipResponse,
1010
QueryClientRequest as MsgQueryClientRequest, QueryClientResponse as MsgQueryClientResponse,
1111
};
12-
use lcp_types::{ClientId, Time};
12+
use lcp_types::ClientId;
1313

1414
impl TryFrom<MsgCreateClient> for InitClientInput {
1515
type Error = Error;
@@ -26,7 +26,6 @@ impl TryFrom<MsgCreateClient> for InitClientInput {
2626
client_id: msg.client_id,
2727
any_client_state,
2828
any_consensus_state,
29-
current_timestamp: Time::now(),
3029
signer: Address::try_from(msg.signer.as_slice())?,
3130
})
3231
}
@@ -44,7 +43,6 @@ impl TryFrom<MsgUpdateClient> for UpdateClientInput {
4443
client_id,
4544
any_header,
4645
include_state: msg.include_state,
47-
current_timestamp: Time::now(),
4846
signer: Address::try_from(msg.signer.as_slice())?,
4947
})
5048
}
@@ -58,7 +56,6 @@ impl TryFrom<MsgAggregateMessages> for AggregateMessagesInput {
5856
signer,
5957
messages: msg.messages,
6058
signatures: msg.signatures,
61-
current_timestamp: Time::now(),
6259
})
6360
}
6461
}

modules/lcp-client/src/client_def.rs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -656,9 +656,7 @@ mod tests {
656656
timestamp: Time::unix_epoch(),
657657
};
658658

659-
let mut ctx = Context::new(registry.clone(), ibc_store.clone(), &ek);
660-
ctx.set_timestamp(Time::now());
661-
659+
let mut ctx = Context::new(registry.clone(), ibc_store.clone(), &ek, Time::now());
662660
let client_id = ClientId::from_str(&format!("{}-0", LCPClient.client_type())).unwrap();
663661

664662
let res = LCPClient.initialise(
@@ -673,8 +671,7 @@ mod tests {
673671

674672
// 2. register enclave key to the LCP client
675673
{
676-
let mut ctx = Context::new(registry.clone(), ibc_store.clone(), &ek);
677-
ctx.set_timestamp(Time::now());
674+
let mut ctx = Context::new(registry.clone(), ibc_store.clone(), &ek, Time::now());
678675
let report = generate_dummy_signed_avr(&ek.get_pubkey());
679676
let operator_signature = op_key
680677
.sign(compute_eip712_register_enclave_key(report.avr.as_str()).as_slice())
@@ -692,8 +689,7 @@ mod tests {
692689
let header = MockHeader::new(ICS02Height::new(0, 1).unwrap());
693690
let client_state = mock_lc::ClientState::from(MockClientState::new(header));
694691
let consensus_state = mock_lc::ConsensusState::from(MockConsensusState::new(header));
695-
let mut ctx = Context::new(registry.clone(), lcp_store.clone(), &ek);
696-
ctx.set_timestamp(Time::now());
692+
let mut ctx = Context::new(registry.clone(), lcp_store.clone(), &ek, Time::now());
697693

698694
let res = MockLightClient.create_client(
699695
&ctx,
@@ -721,8 +717,7 @@ mod tests {
721717
let proof1 = {
722718
let header = MockHeader::new(ICS02Height::new(0, 2).unwrap());
723719

724-
let mut ctx = Context::new(registry.clone(), lcp_store.clone(), &ek);
725-
ctx.set_timestamp(Time::now());
720+
let mut ctx = Context::new(registry.clone(), lcp_store.clone(), &ek, Time::now());
726721
let res = MockLightClient.update_client(
727722
&ctx,
728723
upstream_client_id.clone(),
@@ -758,18 +753,20 @@ mod tests {
758753
proxy_message: proof1.message().unwrap(),
759754
signatures: vec![proof1.signature],
760755
});
761-
let mut ctx = Context::new(registry.clone(), ibc_store.clone(), &ek);
762-
ctx.set_timestamp((Time::now() + Duration::from_secs(60)).unwrap());
756+
let mut ctx = Context::new(
757+
registry.clone(),
758+
ibc_store.clone(),
759+
&ek,
760+
(Time::now() + Duration::from_secs(60)).unwrap(),
761+
);
763762

764763
let res = LCPClient.update_client(&mut ctx, lcp_client_id.clone(), header);
765764
assert!(res.is_ok(), "res={:?}", res);
766765
}
767766

768767
// 6. on the upstream side, updates the Light Client state with a misbehaviour
769768
let misbehaviour_proof = {
770-
let mut ctx = Context::new(registry.clone(), lcp_store, &ek);
771-
ctx.set_timestamp(Time::now());
772-
769+
let ctx = Context::new(registry.clone(), lcp_store, &ek, Time::now());
773770
let mock_misbehaviour = MockMisbehaviour {
774771
client_id: upstream_client_id.clone().into(),
775772
header1: MockHeader::new(ICS02Height::new(0, 3).unwrap()),
@@ -797,9 +794,12 @@ mod tests {
797794
proxy_message: misbehaviour_proof.message().unwrap(),
798795
signatures: vec![misbehaviour_proof.signature],
799796
});
800-
let mut ctx = Context::new(registry, ibc_store, &ek);
801-
ctx.set_timestamp((Time::now() + Duration::from_secs(60)).unwrap());
802-
797+
let mut ctx = Context::new(
798+
registry,
799+
ibc_store,
800+
&ek,
801+
(Time::now() + Duration::from_secs(60)).unwrap(),
802+
);
803803
let res = LCPClient.update_client(&mut ctx, lcp_client_id, header);
804804
assert!(res.is_ok(), "res={:?}", res);
805805
}

tests/integration/src/config.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ fn make_tm_config(cfg: TestChainConfig) -> ChainConfig {
5757
max_msg_num: Default::default(),
5858
max_tx_size: Default::default(),
5959
max_block_time: Default::default(),
60-
clock_drift: Duration::from_secs(5),
60+
clock_drift: Duration::from_secs(30),
6161
trusting_period: Some(Duration::from_secs(14 * 24 * 3600)),
6262
trust_threshold: Default::default(),
6363
gas_price: config::GasPrice::new(0.001, "stake".to_string()),

tests/integration/src/lib.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ mod tests {
3535
};
3636
use keymanager::EnclaveKeyManager;
3737
use lcp_proto::protobuf::Protobuf;
38-
use lcp_types::{ClientId, Height, Time};
38+
use lcp_types::{ClientId, Height};
3939
use log::*;
40+
use std::str::FromStr;
4041
use std::sync::{Arc, RwLock};
41-
use std::{ops::Add, str::FromStr, time::Duration};
4242
use store::{host::HostStore, memory::MemStore};
4343
use tempfile::TempDir;
4444
use tokio::runtime::Runtime as TokioRuntime;
@@ -239,7 +239,6 @@ mod tests {
239239
client_id: client_id.clone(),
240240
any_client_state: client_state,
241241
any_consensus_state: consensus_state,
242-
current_timestamp: Time::now(),
243242
signer,
244243
})?;
245244
assert!(!res.proof.is_proven());
@@ -254,7 +253,6 @@ mod tests {
254253
let res = enclave.update_client(UpdateClientInput {
255254
client_id: client_id.clone(),
256255
any_header: target_header,
257-
current_timestamp: Time::now(),
258256
include_state: true,
259257
signer,
260258
})?;
@@ -298,7 +296,6 @@ mod tests {
298296
let res = enclave.update_client(UpdateClientInput {
299297
client_id: client_id.clone(),
300298
any_header: target_header,
301-
current_timestamp: Time::now().add(Duration::from_secs(10))?, // for gaiad's clock drift
302299
include_state: false,
303300
signer,
304301
})?;
@@ -316,7 +313,6 @@ mod tests {
316313
messages,
317314
signatures,
318315
signer,
319-
current_timestamp: Time::now().add(Duration::from_secs(10))?,
320316
})?;
321317
let msg: UpdateStateProxyMessage = res.0.message().unwrap().try_into()?;
322318
assert!(msg.prev_height == Some(Height::from(last_height)));

0 commit comments

Comments
 (0)