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

Fix to always keep the current timestamp in Context #116

Merged
merged 1 commit into from
Oct 23, 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
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ pub fn aggregate_messages<R: LightClientResolver, S: KVStore, K: Signer>(
ctx: &mut Context<R, S, K>,
input: AggregateMessagesInput,
) -> Result<LightClientResponse, Error> {
ctx.set_timestamp(input.current_timestamp);

if input.messages.len() < 2 {
return Err(Error::invalid_argument(
"messages and signatures must have at least 2 elements".into(),
Expand Down
2 changes: 0 additions & 2 deletions enclave-modules/ecall-handler/src/light_client/init_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ pub fn init_client<R: LightClientResolver, S: KVStore, K: Signer>(
ctx: &mut Context<R, S, K>,
input: InitClientInput,
) -> Result<LightClientResponse, Error> {
ctx.set_timestamp(input.current_timestamp);

let lc = match ctx.get_light_client(&input.any_client_state.type_url) {
Some(lc) => lc,
None => {
Expand Down
16 changes: 12 additions & 4 deletions enclave-modules/ecall-handler/src/light_client/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,12 @@ pub fn dispatch<E: Env>(
let sealed_ek = cctx
.sealed_ek
.ok_or(Error::sealed_enclave_key_not_found())?;
let mut ctx =
Context::new(env.get_lc_registry(), env.new_store(cctx.tx_id), &sealed_ek);
let mut ctx = Context::new(
env.get_lc_registry(),
env.new_store(cctx.tx_id),
&sealed_ek,
cctx.current_timestamp,
);
match cmd {
InitClient(input) => init_client(&mut ctx, input)?,
UpdateClient(input) => update_client(&mut ctx, input)?,
Expand All @@ -33,8 +37,12 @@ pub fn dispatch<E: Env>(
}
LightClientCommand::Query(cmd) => {
use LightClientQueryCommand::*;
let mut ctx =
Context::new(env.get_lc_registry(), env.new_store(cctx.tx_id), &NopSigner);
let mut ctx = Context::new(
env.get_lc_registry(),
env.new_store(cctx.tx_id),
&NopSigner,
cctx.current_timestamp,
);
match cmd {
QueryClient(input) => query_client(&mut ctx, input)?,
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ pub fn update_client<R: LightClientResolver, S: KVStore, K: Signer>(
ctx: &mut Context<R, S, K>,
input: UpdateClientInput,
) -> Result<LightClientResponse, Error> {
ctx.set_timestamp(input.current_timestamp);

let lc = get_light_client_by_client_id(ctx, &input.client_id)?;
let ek = ctx.get_enclave_key();
match lc.update_client(ctx, input.client_id.clone(), input.any_header)? {
Expand Down
12 changes: 4 additions & 8 deletions modules/context/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,19 @@ pub struct Context<'k, R: LightClientResolver, S: KVStore, K: Signer> {
lc_registry: R,
store: S,
ek: &'k K,
current_timestamp: Option<Time>,
current_timestamp: Time,
}

impl<'k, R: LightClientResolver, S: KVStore, K: Signer> Context<'k, R, S, K> {
pub fn new(lc_registry: R, store: S, ek: &'k K) -> Self {
pub fn new(lc_registry: R, store: S, ek: &'k K, current_timestamp: Time) -> Self {
Self {
lc_registry,
store,
ek,
current_timestamp: None,
current_timestamp,
}
}

pub fn set_timestamp(&mut self, timestamp: Time) {
self.current_timestamp = Some(timestamp)
}

pub fn get_enclave_key(&self) -> &'k dyn Signer {
self.ek
}
Expand All @@ -49,7 +45,7 @@ impl<'k, R: LightClientResolver, S: KVStore, K: Signer> KVStore for Context<'k,

impl<'k, R: LightClientResolver, S: KVStore, K: Signer> HostContext for Context<'k, R, S, K> {
fn host_timestamp(&self) -> Time {
self.current_timestamp.unwrap()
self.current_timestamp
}
}

Expand Down
5 changes: 1 addition & 4 deletions modules/ecall-commands/src/light_client.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{prelude::*, EnclaveKeySelector};
use commitments::CommitmentProof;
use crypto::Address;
use lcp_types::{Any, ClientId, Height, Time};
use lcp_types::{Any, ClientId, Height};
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Debug)]
Expand Down Expand Up @@ -44,7 +44,6 @@ pub struct InitClientInput {
pub client_id: String,
pub any_client_state: Any,
pub any_consensus_state: Any,
pub current_timestamp: Time,
pub signer: Address,
}

Expand All @@ -53,7 +52,6 @@ pub struct UpdateClientInput {
pub client_id: ClientId,
pub any_header: Any,
pub include_state: bool,
pub current_timestamp: Time,
pub signer: Address,
}

Expand All @@ -62,7 +60,6 @@ pub struct AggregateMessagesInput {
pub signer: Address,
pub messages: Vec<Vec<u8>>,
pub signatures: Vec<Vec<u8>>,
pub current_timestamp: Time,
}

#[derive(Serialize, Deserialize, Debug)]
Expand Down
5 changes: 1 addition & 4 deletions modules/ecall-commands/src/msgs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use lcp_types::proto::lcp::service::elc::v1::{
MsgVerifyNonMembership, MsgVerifyNonMembershipResponse,
QueryClientRequest as MsgQueryClientRequest, QueryClientResponse as MsgQueryClientResponse,
};
use lcp_types::{ClientId, Time};
use lcp_types::ClientId;

impl TryFrom<MsgCreateClient> for InitClientInput {
type Error = Error;
Expand All @@ -26,7 +26,6 @@ impl TryFrom<MsgCreateClient> for InitClientInput {
client_id: msg.client_id,
any_client_state,
any_consensus_state,
current_timestamp: Time::now(),
signer: Address::try_from(msg.signer.as_slice())?,
})
}
Expand All @@ -44,7 +43,6 @@ impl TryFrom<MsgUpdateClient> for UpdateClientInput {
client_id,
any_header,
include_state: msg.include_state,
current_timestamp: Time::now(),
signer: Address::try_from(msg.signer.as_slice())?,
})
}
Expand All @@ -58,7 +56,6 @@ impl TryFrom<MsgAggregateMessages> for AggregateMessagesInput {
signer,
messages: msg.messages,
signatures: msg.signatures,
current_timestamp: Time::now(),
})
}
}
Expand Down
34 changes: 17 additions & 17 deletions modules/lcp-client/src/client_def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -656,9 +656,7 @@ mod tests {
timestamp: Time::unix_epoch(),
};

let mut ctx = Context::new(registry.clone(), ibc_store.clone(), &ek);
ctx.set_timestamp(Time::now());

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

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

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

let res = MockLightClient.create_client(
&ctx,
Expand Down Expand Up @@ -721,8 +717,7 @@ mod tests {
let proof1 = {
let header = MockHeader::new(ICS02Height::new(0, 2).unwrap());

let mut ctx = Context::new(registry.clone(), lcp_store.clone(), &ek);
ctx.set_timestamp(Time::now());
let mut ctx = Context::new(registry.clone(), lcp_store.clone(), &ek, Time::now());
let res = MockLightClient.update_client(
&ctx,
upstream_client_id.clone(),
Expand Down Expand Up @@ -758,18 +753,20 @@ mod tests {
proxy_message: proof1.message().unwrap(),
signatures: vec![proof1.signature],
});
let mut ctx = Context::new(registry.clone(), ibc_store.clone(), &ek);
ctx.set_timestamp((Time::now() + Duration::from_secs(60)).unwrap());
let mut ctx = Context::new(
registry.clone(),
ibc_store.clone(),
&ek,
(Time::now() + Duration::from_secs(60)).unwrap(),
);

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

// 6. on the upstream side, updates the Light Client state with a misbehaviour
let misbehaviour_proof = {
let mut ctx = Context::new(registry.clone(), lcp_store, &ek);
ctx.set_timestamp(Time::now());

let ctx = Context::new(registry.clone(), lcp_store, &ek, Time::now());
let mock_misbehaviour = MockMisbehaviour {
client_id: upstream_client_id.clone().into(),
header1: MockHeader::new(ICS02Height::new(0, 3).unwrap()),
Expand Down Expand Up @@ -797,9 +794,12 @@ mod tests {
proxy_message: misbehaviour_proof.message().unwrap(),
signatures: vec![misbehaviour_proof.signature],
});
let mut ctx = Context::new(registry, ibc_store, &ek);
ctx.set_timestamp((Time::now() + Duration::from_secs(60)).unwrap());

let mut ctx = Context::new(
registry,
ibc_store,
&ek,
(Time::now() + Duration::from_secs(60)).unwrap(),
);
let res = LCPClient.update_client(&mut ctx, lcp_client_id, header);
assert!(res.is_ok(), "res={:?}", res);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ fn make_tm_config(cfg: TestChainConfig) -> ChainConfig {
max_msg_num: Default::default(),
max_tx_size: Default::default(),
max_block_time: Default::default(),
clock_drift: Duration::from_secs(5),
clock_drift: Duration::from_secs(30),
trusting_period: Some(Duration::from_secs(14 * 24 * 3600)),
trust_threshold: Default::default(),
gas_price: config::GasPrice::new(0.001, "stake".to_string()),
Expand Down
8 changes: 2 additions & 6 deletions tests/integration/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ mod tests {
};
use keymanager::EnclaveKeyManager;
use lcp_proto::protobuf::Protobuf;
use lcp_types::{ClientId, Height, Time};
use lcp_types::{ClientId, Height};
use log::*;
use std::str::FromStr;
use std::sync::{Arc, RwLock};
use std::{ops::Add, str::FromStr, time::Duration};
use store::{host::HostStore, memory::MemStore};
use tempfile::TempDir;
use tokio::runtime::Runtime as TokioRuntime;
Expand Down Expand Up @@ -239,7 +239,6 @@ mod tests {
client_id: client_id.clone(),
any_client_state: client_state,
any_consensus_state: consensus_state,
current_timestamp: Time::now(),
signer,
})?;
assert!(!res.proof.is_proven());
Expand All @@ -254,7 +253,6 @@ mod tests {
let res = enclave.update_client(UpdateClientInput {
client_id: client_id.clone(),
any_header: target_header,
current_timestamp: Time::now(),
include_state: true,
signer,
})?;
Expand Down Expand Up @@ -298,7 +296,6 @@ mod tests {
let res = enclave.update_client(UpdateClientInput {
client_id: client_id.clone(),
any_header: target_header,
current_timestamp: Time::now().add(Duration::from_secs(10))?, // for gaiad's clock drift
include_state: false,
signer,
})?;
Expand All @@ -316,7 +313,6 @@ mod tests {
messages,
signatures,
signer,
current_timestamp: Time::now().add(Duration::from_secs(10))?,
})?;
let msg: UpdateStateProxyMessage = res.0.message().unwrap().try_into()?;
assert!(msg.prev_height == Some(Height::from(last_height)));
Expand Down
Loading