diff --git a/Cargo.lock b/Cargo.lock index c7a99e7..883b1e2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -185,7 +185,7 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[package]] name = "commitments" version = "0.1.0" -source = "git+https://github.com/datachainlab/lcp?rev=v0.2.2#c073f89517fcdc4f796c07ad420b0029bbc148db" +source = "git+https://github.com/datachainlab/lcp?rev=v0.2.4#3bd77b94f0ad6012941e5de9cddc62696e6ffc0c" dependencies = [ "crypto", "ethabi", @@ -221,7 +221,7 @@ checksum = "7a81dae078cea95a014a339291cec439d2f232ebe854a9d672b796c6afafa9b7" [[package]] name = "crypto" version = "0.1.0" -source = "git+https://github.com/datachainlab/lcp?rev=v0.2.2#c073f89517fcdc4f796c07ad420b0029bbc148db" +source = "git+https://github.com/datachainlab/lcp?rev=v0.2.4#3bd77b94f0ad6012941e5de9cddc62696e6ffc0c" dependencies = [ "flex-error", "hex", @@ -804,7 +804,7 @@ dependencies = [ [[package]] name = "lcp-proto" version = "0.1.0" -source = "git+https://github.com/datachainlab/lcp?rev=v0.2.2#c073f89517fcdc4f796c07ad420b0029bbc148db" +source = "git+https://github.com/datachainlab/lcp?rev=v0.2.4#3bd77b94f0ad6012941e5de9cddc62696e6ffc0c" dependencies = [ "ibc-proto", "prost", @@ -814,7 +814,7 @@ dependencies = [ [[package]] name = "lcp-types" version = "0.1.0" -source = "git+https://github.com/datachainlab/lcp?rev=v0.2.2#c073f89517fcdc4f796c07ad420b0029bbc148db" +source = "git+https://github.com/datachainlab/lcp?rev=v0.2.4#3bd77b94f0ad6012941e5de9cddc62696e6ffc0c" dependencies = [ "flex-error", "hex", @@ -884,7 +884,7 @@ dependencies = [ [[package]] name = "light-client" version = "0.1.0" -source = "git+https://github.com/datachainlab/lcp?rev=v0.2.2#c073f89517fcdc4f796c07ad420b0029bbc148db" +source = "git+https://github.com/datachainlab/lcp?rev=v0.2.4#3bd77b94f0ad6012941e5de9cddc62696e6ffc0c" dependencies = [ "bincode", "commitments", @@ -1013,7 +1013,7 @@ dependencies = [ [[package]] name = "parlia-elc" -version = "0.2.0" +version = "0.3.0" dependencies = [ "elliptic-curve", "hex-literal", @@ -1474,7 +1474,7 @@ checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f" [[package]] name = "store" version = "0.1.0" -source = "git+https://github.com/datachainlab/lcp?rev=v0.2.2#c073f89517fcdc4f796c07ad420b0029bbc148db" +source = "git+https://github.com/datachainlab/lcp?rev=v0.2.4#3bd77b94f0ad6012941e5de9cddc62696e6ffc0c" dependencies = [ "flex-error", "log", diff --git a/light-client/Cargo.toml b/light-client/Cargo.toml index 6b548ec..5d9d229 100644 --- a/light-client/Cargo.toml +++ b/light-client/Cargo.toml @@ -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 } @@ -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] diff --git a/light-client/src/client.rs b/light-client/src/client.rs index 03a3886..e1cd8b9 100644 --- a/light-client/src/client.rs +++ b/light-client/src/client.rs @@ -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; @@ -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, @@ -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, @@ -138,7 +137,7 @@ impl LightClient for ParliaLightClient { value: Vec, proof_height: Height, proof: Vec, - ) -> Result { + ) -> Result { let value = keccak_256(&value); let state_id = self.verify_commitment( ctx, @@ -150,8 +149,8 @@ impl LightClient for ParliaLightClient { proof, )?; - Ok(StateVerificationResult { - state_commitment: StateCommitment::new( + Ok(VerifyMembershipResult { + message: VerifyMembershipMessage::new( prefix, path, Some(value), @@ -170,11 +169,11 @@ impl LightClient for ParliaLightClient { path: String, proof_height: Height, proof: Vec, - ) -> Result { + ) -> Result { 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(), }) } @@ -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; @@ -325,6 +324,8 @@ mod test { } } + impl HostClientReader for MockClientReader {} + impl store::KVStore for MockClientReader { fn set(&mut self, _key: Vec, _value: Vec) { todo!() @@ -339,8 +340,6 @@ mod test { } } - impl HostClientReader for MockClientReader {} - impl ClientReader for MockClientReader { fn client_state(&self, client_id: &ClientId) -> Result { let cs = self @@ -386,9 +385,9 @@ 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); @@ -396,8 +395,9 @@ mod test { 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()); } @@ -503,11 +503,11 @@ 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())) @@ -515,7 +515,7 @@ mod test { 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, @@ -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), @@ -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), }; } @@ -715,7 +715,7 @@ mod test { state_root: Hash, latest_height: Height, frozen: bool, - ) -> Result { + ) -> Result { let client = ParliaLightClient::default(); let client_id = ClientId::new(client.client_type().as_str(), 0).unwrap(); let mut mock_consensus_state = BTreeMap::new();