Skip to content

Commit

Permalink
remove tendermint crate from lcp-types
Browse files Browse the repository at this point in the history
Signed-off-by: Jun Kimura <[email protected]>
  • Loading branch information
bluele committed Oct 20, 2024
1 parent d74c587 commit 1defac6
Show file tree
Hide file tree
Showing 9 changed files with 190 additions and 62 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion enclave/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions modules/mock-lc/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,10 @@ impl LightClient for MockLightClient {
let client_state: ClientState = any_client_state.clone().try_into()?;
let consensus_state: ConsensusState = any_consensus_state.try_into()?;
let height = client_state.latest_height().into();
let timestamp: Time = consensus_state.timestamp().into();
let timestamp: Time = consensus_state
.timestamp()
.try_into()
.map_err(Error::time)?;
Ok(CreateClientResult {
height,
message: UpdateStateProxyMessage {
Expand Down Expand Up @@ -128,7 +131,7 @@ impl MockLightClient {
}

let height = header.height().into();
let header_timestamp = header.timestamp().into();
let header_timestamp = header.timestamp().try_into().map_err(Error::time)?;

let latest_height = client_state.latest_height();

Expand Down
6 changes: 5 additions & 1 deletion modules/mock-lc/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ define_error! {

Commitment
[light_client::commitments::Error]
|_| { "Commitment error" }
|_| { "Commitment error" },

Time
[light_client::types::TimeError]
|_| { "Time error" },
}
}

Expand Down
32 changes: 26 additions & 6 deletions modules/tendermint-lc/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,13 @@ impl LightClient for TendermintLightClient {

let canonical_client_state = canonicalize_state(&client_state);
let height = client_state.latest_height().into();
let timestamp: Time = consensus_state.timestamp.into();
let timestamp = Time::try_from(
consensus_state
.timestamp()
.into_datetime()
.ok_or_else(Error::invalid_timestamp)?,
)
.map_err(Error::time)?;
let state_id = gen_state_id(canonical_client_state, consensus_state)?;

Ok(CreateClientResult {
Expand Down Expand Up @@ -241,7 +247,7 @@ impl TendermintLightClient {
}

let height = header.height().into();
let header_timestamp: Time = header.timestamp().into();
let header_timestamp: Time = header.timestamp().try_into().map_err(Error::time)?;

let trusted_consensus_state: ConsensusState = ctx
.consensus_state(&client_id, &header.trusted_height.into())
Expand Down Expand Up @@ -282,7 +288,10 @@ impl TendermintLightClient {
.clone(),
);

let trusted_state_timestamp: Time = trusted_consensus_state.timestamp().into();
let trusted_state_timestamp: Time = trusted_consensus_state
.timestamp()
.try_into()
.map_err(Error::time)?;
let lc_opts = client_state.as_light_client_options().unwrap();

let prev_state_id =
Expand Down Expand Up @@ -365,13 +374,21 @@ impl TendermintLightClient {
context: TrustingPeriodContext::new(
lc_opts.trusting_period,
lc_opts.clock_drift,
misbehaviour.header1().timestamp().into(),
misbehaviour
.header1()
.timestamp()
.try_into()
.map_err(Error::time)?,
trusted_consensus_state_timestamps[0],
)
.aggregate(TrustingPeriodContext::new(
lc_opts.trusting_period,
lc_opts.clock_drift,
misbehaviour.header2().timestamp().into(),
misbehaviour
.header2()
.timestamp()
.try_into()
.map_err(Error::time)?,
trusted_consensus_state_timestamps[1],
))
.map_err(|e| {
Expand Down Expand Up @@ -405,7 +422,10 @@ impl TendermintLightClient {
})
})?
.try_into()?;
let timestamp = consensus_state.timestamp().into();
let timestamp = consensus_state
.timestamp()
.try_into()
.map_err(Error::time)?;
let prev_state_id = gen_state_id(canonicalize_state(client_state), consensus_state)?;
prev_states.push(PrevState {
height,
Expand Down
9 changes: 8 additions & 1 deletion modules/tendermint-lc/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,14 @@ define_error! {

Commitment
[light_client::commitments::Error]
|_| { "Commitment error" }
|_| { "Commitment error" },

InvalidTimestamp
|_| { "Invalid timestamp" },

Time
[light_client::types::TimeError]
|_| { "Time error" },
}
}

Expand Down
5 changes: 3 additions & 2 deletions modules/types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ sgx_types = { rev = "v1.1.6", git = "https://github.com/apache/incubator-teaclav
prost = { version = "0.11", default-features = false }
ibc = { version = "0.29.0", default-features = false, features = ["serde"], optional = true }
lcp-proto = { path = "../../proto", default-features = false }
tendermint = { version = "0.29", default-features = false }
serde = { version = "1.0.184", default-features = false, features = ["alloc", "derive"] }
serde_with = { version = "2.0.1", default-features = false, features = ["alloc", "macros"] }
serde_json = { version = "1.0", default-features = false, features = ["alloc"] }
flex-error = { version = "0.4.4", default-features = false }
hex = { version = "0.4", default-features = false, features = ["alloc"] }
time = { version = "0.3", default-features = false, features = ["macros", "parsing"] }

[dev-dependencies]
proptest = "1.2.0"
Expand All @@ -22,7 +22,8 @@ ibc = { version = "0.29.0", default-features = false, features = ["serde"] }
[features]
default = ["std"]
std = [
"flex-error/std"
"flex-error/std",
"time/std",
]
ibc = [
"dep:ibc"
Expand Down
10 changes: 7 additions & 3 deletions modules/types/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,15 @@ impl From<lcp_proto::protobuf::Error> for TypeError {
define_error! {
#[derive(Debug, Clone, PartialEq, Eq)]
TimeError {
Tendermint
[tendermint::Error]
InvalidDate
|_| {
"tendermint error"
"invalid date"
},
DurationOutOfRange
|_| { format_args!("duration value out of range") },
ComponentRange
[TraceError<time::error::ComponentRange>]
|e| { format_args!("{}", e) },
TryFromIntError
[TraceError<core::num::TryFromIntError>]
|_| {"TryFromIntError"}
Expand Down
Loading

0 comments on commit 1defac6

Please sign in to comment.