Skip to content

Commit

Permalink
Fix Client State Conversion (#39)
Browse files Browse the repository at this point in the history
fix client state conversion
  • Loading branch information
Wizdave97 authored Sep 30, 2022
1 parent 5568c58 commit aa30f84
Show file tree
Hide file tree
Showing 21 changed files with 46 additions and 68 deletions.
36 changes: 18 additions & 18 deletions Cargo.lock

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

4 changes: 2 additions & 2 deletions algorithms/beefy/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ beefy-primitives = { default-features = false, git = "https://github.com/parityt
sp-trie = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.27", default-features = false }

# local
beefy-client-primitives = { package = "primitives", path = "primitives", default-features = false }
beefy-light-client-primitives = { path = "primitives", default-features = false }
light-client-common = { path = "../../light-clients/common", default-features = false }

[dev-dependencies]
Expand Down Expand Up @@ -59,7 +59,7 @@ std = [
"rs_merkle/std",
"mmr-lib/std",
"frame-support/std",
"beefy-client-primitives/std",
"beefy-light-client-primitives/std",
"sp-trie/std",
"light-client-common/std",
]
2 changes: 1 addition & 1 deletion algorithms/beefy/primitives/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "primitives"
name = "beefy-light-client-primitives"
version = "0.1.0"
edition = "2021"
authors = ["Seun Lanlege <seunlanlege@gmail.com>", "David Salami <david.salami@gmail.com>"]
Expand Down
2 changes: 1 addition & 1 deletion algorithms/beefy/prover/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,5 @@ serde_json = { version = "1.0.74" }
pallet-mmr-rpc = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.27" }
sp-trie = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.27" }
hex = { version = "0.4.3" }
beefy-client-primitives = { package = "primitives", path = "../primitives" }
beefy-light-client-primitives = { path = "../primitives" }

2 changes: 1 addition & 1 deletion algorithms/beefy/prover/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
// limitations under the License.

use crate::{error::Error, Crypto};
use beefy_client_primitives::{MerkleHasher, SignatureWithAuthorityIndex};
use beefy_light_client_primitives::{MerkleHasher, SignatureWithAuthorityIndex};
use codec::{Decode, Encode};
use frame_support::sp_runtime::traits::Convert;
use jsonrpsee_core::client::Client;
Expand Down
2 changes: 1 addition & 1 deletion algorithms/beefy/prover/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub mod helpers;
pub mod relay_chain_queries;
pub mod runtime;

use beefy_client_primitives::{
use beefy_light_client_primitives::{
get_leaf_index_for_block_number, ClientState, HostFunctions, MerkleHasher, MmrUpdateProof,
ParachainHeader, PartialMmrLeaf, SignedCommitment,
};
Expand Down
2 changes: 1 addition & 1 deletion algorithms/beefy/prover/src/relay_chain_queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use super::runtime;
use crate::{
error::Error, runtime::api::runtime_types::polkadot_runtime_parachains::paras::ParaLifecycle,
};
use beefy_client_primitives::get_leaf_index_for_block_number;
use beefy_light_client_primitives::get_leaf_index_for_block_number;
use beefy_primitives::{SignedCommitment, VersionedFinalityProof};
use codec::{Decode, Encode};
use pallet_mmr_rpc::{LeafBatchProof, LeafProof};
Expand Down
2 changes: 1 addition & 1 deletion algorithms/beefy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ extern crate alloc;
#[cfg(test)]
mod tests;

use beefy_client_primitives::{
use beefy_light_client_primitives::{
error::BeefyClientError, get_leaf_index_for_block_number, BeefyNextAuthoritySet, ClientState,
HostFunctions, MerkleHasher, MmrUpdateProof, NodesUtils, ParachainsUpdateProof,
SignatureWithAuthorityIndex, HASH_LENGTH,
Expand Down
2 changes: 1 addition & 1 deletion algorithms/beefy/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use beefy_client_primitives::{
use beefy_light_client_primitives::{
error::BeefyClientError, MmrUpdateProof, ParachainsUpdateProof, SignatureWithAuthorityIndex,
SignedCommitment,
};
Expand Down
4 changes: 0 additions & 4 deletions light-clients/ics07-tendermint/src/client_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,10 +258,6 @@ where
self.latest_height()
}

fn is_frozen(&self) -> bool {
todo!()
}

fn frozen_height(&self) -> Option<Height> {
self.frozen_height()
}
Expand Down
15 changes: 4 additions & 11 deletions light-clients/ics10-grandpa/src/client_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,15 +166,6 @@ impl<H> TryFrom<RawClientState> for ClientState<H> {
type Error = Error;

fn try_from(raw: RawClientState) -> Result<Self, Self::Error> {
let frozen_height = {
let height = Height::new(raw.para_id as u64, raw.frozen_height.into());
if height == Height::zero() {
None
} else {
Some(height)
}
};

let current_authorities = raw
.current_authorities
.into_iter()
Expand All @@ -194,7 +185,7 @@ impl<H> TryFrom<RawClientState> for ClientState<H> {
let latest_relay_hash = H256::from(fixed_bytes);

Ok(Self {
frozen_height,
frozen_height: raw.frozen_height.map(|height| Height::new(raw.para_id.into(), height)),
relay_chain,
latest_para_height: raw.latest_para_height,
para_id: raw.para_id,
Expand All @@ -211,7 +202,9 @@ impl<H> From<ClientState<H>> for RawClientState {
RawClientState {
latest_relay_hash: client_state.latest_relay_hash.as_bytes().to_vec(),
current_set_id: client_state.current_set_id,
frozen_height: client_state.frozen_height.unwrap_or_default().revision_height,
frozen_height: client_state
.frozen_height
.map(|frozen_height| frozen_height.revision_height),
relay_chain: client_state.relay_chain as i32,
para_id: client_state.para_id,
latest_para_height: client_state.latest_para_height,
Expand Down
2 changes: 1 addition & 1 deletion light-clients/ics10-grandpa/src/proto/grandpa.proto
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ message ClientState {
uint64 current_set_id = 2;

// Block height when the client was frozen due to a misbehaviour
uint64 frozen_height = 3;
optional uint64 frozen_height = 3;

// Known relay chains
RelayChain relay_chain = 4;
Expand Down
4 changes: 2 additions & 2 deletions light-clients/ics11-beefy/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ std = [
"ibc/std",
"ibc-proto/std",
"beefy-client/std",
"beefy-client-primitives/std",
"beefy-light-client-primitives/std",
"light-client-common/std",
"pallet-mmr-primitives/std",
"beefy-primitives/std",
Expand All @@ -40,7 +40,7 @@ ibc-proto = { git = "https://github.com/ComposableFi/ibc-rs", rev = "9a959faabb8

# beefy deps
beefy-client = { package = "beefy-light-client", path = "../../algorithms/beefy", default-features = false }
beefy-client-primitives = { package = "primitives", path = "../../algorithms/beefy/primitives", default-features = false }
beefy-light-client-primitives = { path = "../../algorithms/beefy/primitives", default-features = false }
light-client-common = { path = "../common", default-features = false }

# substrate deps
Expand Down
4 changes: 2 additions & 2 deletions light-clients/ics11-beefy/src/client_def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use beefy_client_primitives::{
use beefy_light_client_primitives::{
ClientState as LightClientState, ParachainHeader, ParachainsUpdateProof,
};
use codec::{Decode, Encode};
Expand Down Expand Up @@ -60,7 +60,7 @@ pub struct BeefyClient<T>(PhantomData<T>);

impl<H> ClientDef for BeefyClient<H>
where
H: light_client_common::HostFunctions + beefy_client_primitives::HostFunctions,
H: light_client_common::HostFunctions + beefy_light_client_primitives::HostFunctions,
{
type ClientMessage = ClientMessage;
type ClientState = ClientState<H>;
Expand Down
2 changes: 1 addition & 1 deletion light-clients/ics11-beefy/src/client_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use crate::{
};
use alloc::{format, vec, vec::Vec};
use anyhow::anyhow;
use beefy_client_primitives::{
use beefy_light_client_primitives::{
BeefyNextAuthoritySet, Hash, MmrUpdateProof, PartialMmrLeaf, SignatureWithAuthorityIndex,
SignedCommitment,
};
Expand Down
17 changes: 5 additions & 12 deletions light-clients/ics11-beefy/src/client_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ impl<H> ClientState<H> {

impl<H> ibc::core::ics02_client::client_state::ClientState for ClientState<H>
where
H: light_client_common::HostFunctions + beefy_client_primitives::HostFunctions,
H: light_client_common::HostFunctions + beefy_light_client_primitives::HostFunctions,
{
type UpgradeOptions = UpgradeOptions;
type ClientDef = BeefyClient<H>;
Expand Down Expand Up @@ -289,15 +289,6 @@ impl<H> TryFrom<RawClientState> for ClientState<H> {
type Error = Error;

fn try_from(raw: RawClientState) -> Result<Self, Self::Error> {
let frozen_height = {
let height = Height::new(0, raw.frozen_height.into());
if height == Height::zero() {
None
} else {
Some(height)
}
};

let authority_set = raw
.authority
.and_then(|set| {
Expand Down Expand Up @@ -328,7 +319,7 @@ impl<H> TryFrom<RawClientState> for ClientState<H> {
chain_id,
mmr_root_hash,
latest_beefy_height: raw.latest_beefy_height,
frozen_height,
frozen_height: raw.frozen_height.map(|height| Height::new(raw.para_id.into(), height)),
beefy_activation_block: raw.beefy_activation_block,
authority: authority_set,
next_authority_set,
Expand All @@ -345,7 +336,9 @@ impl<H> From<ClientState<H>> for RawClientState {
RawClientState {
mmr_root_hash: client_state.mmr_root_hash.encode(),
latest_beefy_height: client_state.latest_beefy_height,
frozen_height: client_state.frozen_height.unwrap_or_default().revision_height,
frozen_height: client_state
.frozen_height
.map(|frozen_height| frozen_height.revision_height),
beefy_activation_block: client_state.beefy_activation_block,
authority: Some(BeefyAuthoritySet {
id: client_state.authority.id,
Expand Down
2 changes: 1 addition & 1 deletion light-clients/ics11-beefy/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use prost::DecodeError;
#[derive(Debug, derive_more::From, derive_more::Display)]
pub enum Error {
Codec(codec::Error),
Beefy(beefy_client_primitives::error::BeefyClientError),
Beefy(beefy_light_client_primitives::error::BeefyClientError),
TimeStamp(TimestampOverflowError),
ParseTimeStamp(ParseTimestampError),
ValidationError(ValidationError),
Expand Down
2 changes: 1 addition & 1 deletion light-clients/ics11-beefy/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ pub const MOCK_CONSENSUS_STATE_TYPE_URL: &str = "/ibc.mock.ConsensusState";
#[derive(Clone, Default, PartialEq, Debug, Eq)]
pub struct HostFunctionsManager;

impl beefy_client_primitives::HostFunctions for HostFunctionsManager {
impl beefy_light_client_primitives::HostFunctions for HostFunctionsManager {
fn keccak_256(input: &[u8]) -> [u8; 32] {
beefy_prover::Crypto::keccak_256(input)
}
Expand Down
2 changes: 1 addition & 1 deletion light-clients/ics11-beefy/src/proto/beefy.proto
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ message ClientState {
uint32 latest_beefy_height = 2;

// Block height when the client was frozen due to a misbehaviour
uint64 frozen_height = 3;
optional uint64 frozen_height = 3;

/// Known relay chains
RelayChain relay_chain = 4;
Expand Down
2 changes: 1 addition & 1 deletion light-clients/ics11-beefy/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use crate::{
AnyClientMessage, AnyClientState, AnyConsensusState, HostFunctionsManager, MockClientTypes,
},
};
use beefy_client_primitives::{NodesUtils, PartialMmrLeaf};
use beefy_light_client_primitives::{NodesUtils, PartialMmrLeaf};
use beefy_prover::{
helpers::{fetch_timestamp_extrinsic_with_proof, TimeStampExtWithProof},
runtime, ClientWrapper,
Expand Down
4 changes: 0 additions & 4 deletions light-clients/ics13-near/src/client_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,6 @@ impl<H: HostFunctionsTrait> ClientState for NearClientState<H> {
self.head.get_height()
}

fn is_frozen(&self) -> bool {
self.frozen_height().is_some()
}

fn frozen_height(&self) -> Option<Height> {
// TODO: validate this
Some(self.head.get_height())
Expand Down

0 comments on commit aa30f84

Please sign in to comment.