Skip to content

Commit

Permalink
Merge pull request #39 from datachainlab/update_lcp
Browse files Browse the repository at this point in the history
Update lcp to 0.2.4
  • Loading branch information
toshihiko-okubo authored Jan 18, 2024
2 parents b5bdac9 + dec610e commit 2e1daa7
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 54 deletions.
14 changes: 7 additions & 7 deletions Cargo.lock

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

6 changes: 3 additions & 3 deletions light-client/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
[package]
name = "parlia-elc"
version = "0.2.0"
version = "0.3.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
light-client = { git = "https://github.com/datachainlab/lcp", rev = "v0.2.2", default-features = false, features=["ibc"] }
light-client = { git = "https://github.com/datachainlab/lcp", rev = "v0.2.4", default-features = false, features=["ibc"] }

rlp = { version = "0.5.2", default-features = false }
prost = { version = "0.11", default-features = false }
Expand All @@ -28,7 +28,7 @@ milagro_bls = { git = "https://github.com/datachainlab/milagro_bls", rev = "bc2b
hex-literal = "0.4.1"

[dev-dependencies]
store = { git = "https://github.com/datachainlab/lcp.git", rev = "v0.2.2", default-features = false }
store = { git = "https://github.com/datachainlab/lcp.git", rev = "v0.2.4", default-features = false }
time = { version = "0.3", default-features = false, features = ["macros", "parsing"] }

[features]
Expand Down
88 changes: 44 additions & 44 deletions light-client/src/client.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
use alloc::string::{String, ToString};
use alloc::vec::Vec;

use light_client::commitments::TrustingPeriodContext;
use light_client::commitments::{
EmittedState, TrustingPeriodContext, UpdateClientMessage, VerifyMembershipMessage,
};
use light_client::{
commitments::{
gen_state_id_from_any, CommitmentContext, CommitmentPrefix, StateCommitment, StateID,
UpdateClientCommitment,
},
commitments::{gen_state_id_from_any, CommitmentPrefix, StateID, ValidationContext},
types::{Any, ClientId, Height},
CreateClientResult, Error as LightClientError, HostClientReader, LightClient,
StateVerificationResult, UpdateClientResult,
UpdateClientResult, VerifyMembershipResult, VerifyNonMembershipResult,
};
use patricia_merkle_trie::keccak::keccak_256;

Expand Down Expand Up @@ -50,21 +49,21 @@ impl LightClient for ParliaLightClient {
let client_state = ClientState::try_from(any_client_state.clone())?;
let consensus_state = ConsensusState::try_from(any_consensus_state)?;

let new_state_id = gen_state_id(client_state.clone(), consensus_state.clone())?;
let post_state_id = gen_state_id(client_state.clone(), consensus_state.clone())?;

let height = client_state.latest_height;
let timestamp = consensus_state.timestamp;

Ok(CreateClientResult {
height,
commitment: UpdateClientCommitment {
message: UpdateClientMessage {
prev_state_id: None,
new_state_id,
new_state: Some(any_client_state),
post_state_id,
emitted_states: vec![EmittedState(height, any_client_state)],
prev_height: None,
new_height: height,
post_height: height,
timestamp,
context: CommitmentContext::Empty,
context: ValidationContext::Empty,
}
.into(),
prove: false,
Expand Down Expand Up @@ -104,20 +103,20 @@ impl LightClient for ParliaLightClient {
let trusting_period = client_state.trusting_period;
let max_clock_drift = client_state.max_clock_drift;
let prev_state_id = gen_state_id(client_state, trusted_consensus_state)?;
let new_state_id = gen_state_id(new_client_state.clone(), new_consensus_state.clone())?;
let post_state_id = gen_state_id(new_client_state.clone(), new_consensus_state.clone())?;

Ok(UpdateClientResult {
new_any_client_state: new_client_state.try_into()?,
new_any_consensus_state: new_consensus_state.try_into()?,
height,
commitment: UpdateClientCommitment {
message: UpdateClientMessage {
prev_state_id: Some(prev_state_id),
new_state_id,
new_state: None,
post_state_id,
emitted_states: Default::default(),
prev_height: Some(trusted_height),
new_height: height,
post_height: height,
timestamp,
context: CommitmentContext::TrustingPeriod(TrustingPeriodContext::new(
context: ValidationContext::TrustingPeriod(TrustingPeriodContext::new(
trusting_period,
max_clock_drift,
timestamp,
Expand All @@ -138,7 +137,7 @@ impl LightClient for ParliaLightClient {
value: Vec<u8>,
proof_height: Height,
proof: Vec<u8>,
) -> Result<StateVerificationResult, LightClientError> {
) -> Result<VerifyMembershipResult, LightClientError> {
let value = keccak_256(&value);
let state_id = self.verify_commitment(
ctx,
Expand All @@ -150,8 +149,8 @@ impl LightClient for ParliaLightClient {
proof,
)?;

Ok(StateVerificationResult {
state_commitment: StateCommitment::new(
Ok(VerifyMembershipResult {
message: VerifyMembershipMessage::new(
prefix,
path,
Some(value),
Expand All @@ -170,11 +169,11 @@ impl LightClient for ParliaLightClient {
path: String,
proof_height: Height,
proof: Vec<u8>,
) -> Result<StateVerificationResult, LightClientError> {
) -> Result<VerifyNonMembershipResult, LightClientError> {
let state_id =
self.verify_commitment(ctx, client_id, &prefix, &path, None, &proof_height, proof)?;
Ok(StateVerificationResult {
state_commitment: StateCommitment::new(prefix, path, None, proof_height, state_id)
Ok(VerifyNonMembershipResult {
message: VerifyMembershipMessage::new(prefix, path, None, proof_height, state_id)
.into(),
})
}
Expand Down Expand Up @@ -268,9 +267,9 @@ mod test {
use hex_literal::hex;
use light_client::types::{Any, ClientId, Height, Time};

use light_client::commitments::{Commitment, CommitmentContext, TrustingPeriodContext};
use light_client::commitments::{Message, TrustingPeriodContext, ValidationContext};
use light_client::{
ClientReader, HostClientReader, HostContext, LightClient, StateVerificationResult,
ClientReader, HostClientReader, HostContext, LightClient, VerifyMembershipResult,
};

use patricia_merkle_trie::keccak::keccak_256;
Expand Down Expand Up @@ -325,6 +324,8 @@ mod test {
}
}

impl HostClientReader for MockClientReader {}

impl store::KVStore for MockClientReader {
fn set(&mut self, _key: Vec<u8>, _value: Vec<u8>) {
todo!()
Expand All @@ -339,8 +340,6 @@ mod test {
}
}

impl HostClientReader for MockClientReader {}

impl ClientReader for MockClientReader {
fn client_state(&self, client_id: &ClientId) -> Result<Any, light_client::Error> {
let cs = self
Expand Down Expand Up @@ -386,18 +385,19 @@ mod test {
.create_client(&ctx, any_client_state.clone(), any_consensus_state.clone())
.unwrap();
assert_eq!(result.height.revision_height(), 32132891);
match result.commitment {
Commitment::UpdateClient(data) => {
assert_eq!(data.new_height, result.height);
match result.message {
Message::UpdateClient(data) => {
assert_eq!(data.post_height, result.height);

let cs = ConsensusState::try_from(any_consensus_state).unwrap();
assert_eq!(data.timestamp.as_unix_timestamp_secs(), 1695891806);
assert_eq!(
data.timestamp.as_unix_timestamp_secs(),
cs.timestamp.as_unix_timestamp_secs()
);
assert_eq!(data.new_state.unwrap(), any_client_state);
assert!(!data.new_state_id.to_vec().is_empty());
assert_eq!(data.emitted_states[0].0, result.height);
assert_eq!(data.emitted_states[0].1, any_client_state);
assert!(!data.post_state_id.to_vec().is_empty());
assert!(data.prev_height.is_none());
assert!(data.prev_state_id.is_none());
}
Expand Down Expand Up @@ -503,19 +503,19 @@ mod test {
new_consensus_state.previous_validators_hash,
new_previous_validator_hash
);
match &data.commitment {
Commitment::UpdateClient(data) => {
assert_eq!(data.new_height, header.height());
assert_eq!(data.new_state, None);
assert!(!data.new_state_id.to_vec().is_empty());
match &data.message {
Message::UpdateClient(data) => {
assert_eq!(data.post_height, header.height());
assert_eq!(data.emitted_states, vec![]);
assert!(!data.post_state_id.to_vec().is_empty());
assert_eq!(
data.prev_height,
Some(new_height(0, header.trusted_height().revision_height()))
);
assert!(data.prev_state_id.is_some());
assert_eq!(data.timestamp, header.timestamp().unwrap());
match &data.context {
CommitmentContext::TrustingPeriod(actual) => {
ValidationContext::TrustingPeriod(actual) => {
let expected = TrustingPeriodContext::new(
cs.trusting_period,
cs.max_clock_drift,
Expand All @@ -527,7 +527,7 @@ mod test {
_ => unreachable!("invalid commitment context {:?}", data.context),
}
}
_ => unreachable!("invalid commitment {:?}", data.commitment),
_ => unreachable!("invalid commitment {:?}", data.message),
}
}
Err(e) => unreachable!("error {:?}", e),
Expand Down Expand Up @@ -645,13 +645,13 @@ mod test {
false,
)
.unwrap();
match result.state_commitment {
Commitment::State(data) => {
match result.message {
Message::VerifyMembership(data) => {
assert_eq!(data.path, path);
assert_eq!(data.height, proof_height);
assert_eq!(data.value, Some(keccak_256(value.as_slice())));
}
_ => unreachable!("invalid state commitment {:?}", result.state_commitment),
_ => unreachable!("invalid state commitment {:?}", result.message),
};
}

Expand Down Expand Up @@ -715,7 +715,7 @@ mod test {
state_root: Hash,
latest_height: Height,
frozen: bool,
) -> Result<StateVerificationResult, light_client::Error> {
) -> Result<VerifyMembershipResult, light_client::Error> {
let client = ParliaLightClient::default();
let client_id = ClientId::new(client.client_type().as_str(), 0).unwrap();
let mut mock_consensus_state = BTreeMap::new();
Expand Down

0 comments on commit 2e1daa7

Please sign in to comment.