Skip to content

Commit

Permalink
Enable Event create Grandpa client (#44)
Browse files Browse the repository at this point in the history
* Update client_state, consensus_state

* Pub client_state new function

* Update  consensus_stae  new function

* Update grandpa client implement

* Update struct

* Update ics02_client/client_state

* format code

* Add latest height

* Add Ics10 Header height

* Add Ics10 client_state chain

* Add Ics10 client state frozen_height

* remove println for debug

* public OpenInit inner type, and Add MsgConnectionOpenInit new function

Co-authored-by: DaviRain-Su <davirain.yin@gamil.com>
  • Loading branch information
DaviRain-Su and DaviRain-Su committed Aug 10, 2021
1 parent 078480a commit d5be5af
Show file tree
Hide file tree
Showing 18 changed files with 424 additions and 123 deletions.
15 changes: 13 additions & 2 deletions modules/src/ics02_client/client_consensus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ use crate::mock::client_state::MockConsensusState;
pub const TENDERMINT_CONSENSUS_STATE_TYPE_URL: &str =
"/ibc.lightclients.tendermint.v1.ConsensusState";

pub const GRANDPA_CONSENSUS_STATE_TYPE_URL: &str = "/ibc.lightclients.grandpa.v1.ConsensusState";

pub const MOCK_CONSENSUS_STATE_TYPE_URL: &str = "/ibc.mock.ConsensusState";

pub trait ConsensusState: Clone + std::fmt::Debug + Send + Sync {
Expand Down Expand Up @@ -95,6 +97,11 @@ impl TryFrom<Any> for AnyConsensusState {
.map_err(Error::decode_raw_client_state)?,
)),

GRANDPA_CONSENSUS_STATE_TYPE_URL => Ok(AnyConsensusState::Grandpa(
crate::ics10_grandpa::consensus_state::ConsensusState::decode_vec(&value.value)
.map_err(Error::decode_raw_client_state)?,
)),

#[cfg(any(test, feature = "mocks"))]
MOCK_CONSENSUS_STATE_TYPE_URL => Ok(AnyConsensusState::Mock(
MockConsensusState::decode_vec(&value.value)
Expand All @@ -115,9 +122,13 @@ impl From<AnyConsensusState> for Any {
.encode_vec()
.expect("encoding to `Any` from `AnyConsensusState::Tendermint`"),
},
AnyConsensusState::Grandpa(value) => {
unimplemented!()
AnyConsensusState::Grandpa(value) => Any {
type_url: GRANDPA_CONSENSUS_STATE_TYPE_URL.to_string(),
value: value
.encode_vec()
.expect("encoding to 'Any' from 'AnyConsensusState::Grandpa'"),
},

#[cfg(any(test, feature = "mocks"))]
AnyConsensusState::Mock(value) => Any {
type_url: MOCK_CONSENSUS_STATE_TYPE_URL.to_string(),
Expand Down
165 changes: 155 additions & 10 deletions modules/src/ics02_client/client_def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,21 @@ impl ClientDef for AnyClient {
AnyConsensusState::Tendermint(new_consensus),
))
}
Self::Grandpa(client) => unimplemented!(),
Self::Grandpa(client) => {
let (client_state, header) = downcast!(
client_state => AnyClientState::Grandpa,
header => AnyHeader::Grandpa,
)
.ok_or_else(|| Error::client_args_type_mismatch(ClientType::Grandpa))?;

let (new_state, new_consensus) =
client.check_header_and_update_state(client_state, header)?;

Ok((
AnyClientState::Grandpa(new_state),
AnyConsensusState::Grandpa(new_consensus),
))
}

#[cfg(any(test, feature = "mocks"))]
Self::Mock(client) => {
Expand Down Expand Up @@ -243,7 +257,22 @@ impl ClientDef for AnyClient {
)
}

Self::Grandpa(client) => unimplemented!(),
Self::Grandpa(client) => {
let client_state = downcast!(
client_state => AnyClientState::Grandpa
)
.ok_or_else(|| Error::client_args_type_mismatch(ClientType::Grandpa))?;

client.verify_client_consensus_state(
client_state,
height,
prefix,
proof,
client_id,
consensus_height,
expected_consensus_state,
)
}

#[cfg(any(test, feature = "mocks"))]
Self::Mock(client) => {
Expand Down Expand Up @@ -288,7 +317,19 @@ impl ClientDef for AnyClient {
expected_connection_end,
)
}
Self::Grandpa(client) => unimplemented!(),
Self::Grandpa(client) => {
let client_state = downcast!(client_state => AnyClientState::Grandpa)
.ok_or_else(|| Error::client_args_type_mismatch(ClientType::Grandpa))?;

client.verify_connection_state(
client_state,
height,
prefix,
proof,
connection_id,
expected_connection_end,
)
}

#[cfg(any(test, feature = "mocks"))]
Self::Mock(client) => {
Expand Down Expand Up @@ -332,7 +373,20 @@ impl ClientDef for AnyClient {
expected_channel_end,
)
}
Self::Grandpa(client) => unimplemented!(),
Self::Grandpa(client) => {
let client_state = downcast!(client_state => AnyClientState::Grandpa)
.ok_or_else(|| Error::client_args_type_mismatch(ClientType::Grandpa))?;

client.verify_channel_state(
client_state,
height,
prefix,
proof,
port_id,
channel_id,
expected_channel_end,
)
}

#[cfg(any(test, feature = "mocks"))]
Self::Mock(client) => {
Expand Down Expand Up @@ -379,7 +433,22 @@ impl ClientDef for AnyClient {
client_state_on_counterparty,
)
}
Self::Grandpa(client) => unimplemented!(),
Self::Grandpa(client) => {
let client_state = downcast!(
client_state => AnyClientState::Grandpa
)
.ok_or_else(|| Error::client_args_type_mismatch(ClientType::Grandpa))?;

client.verify_client_full_state(
client_state,
height,
root,
prefix,
client_id,
proof,
client_state_on_counterparty,
)
}

#[cfg(any(test, feature = "mocks"))]
Self::Mock(client) => {
Expand Down Expand Up @@ -427,7 +496,22 @@ impl ClientDef for AnyClient {
commitment,
)
}
Self::Grandpa(client) => unimplemented!(),
Self::Grandpa(client) => {
let client_state = downcast!(
client_state => AnyClientState::Grandpa
)
.ok_or_else(|| Error::client_args_type_mismatch(ClientType::Grandpa))?;

client.verify_packet_data(
client_state,
height,
proof,
port_id,
channel_id,
seq,
commitment,
)
}

#[cfg(any(test, feature = "mocks"))]
Self::Mock(client) => {
Expand Down Expand Up @@ -476,7 +560,22 @@ impl ClientDef for AnyClient {
ack,
)
}
Self::Grandpa(client) => unimplemented!(),
Self::Grandpa(client) => {
let client_state = downcast!(
client_state => AnyClientState::Grandpa
)
.ok_or_else(|| Error::client_args_type_mismatch(ClientType::Grandpa))?;

client.verify_packet_acknowledgement(
client_state,
height,
proof,
port_id,
channel_id,
seq,
ack,
)
}

#[cfg(any(test, feature = "mocks"))]
Self::Mock(client) => {
Expand Down Expand Up @@ -524,7 +623,21 @@ impl ClientDef for AnyClient {
)
}

Self::Grandpa(client) => unimplemented!(),
Self::Grandpa(client) => {
let client_state = downcast!(
client_state => AnyClientState::Grandpa
)
.ok_or_else(|| Error::client_args_type_mismatch(ClientType::Grandpa))?;

client.verify_next_sequence_recv(
client_state,
height,
proof,
port_id,
channel_id,
seq,
)
}

#[cfg(any(test, feature = "mocks"))]
Self::Mock(client) => {
Expand Down Expand Up @@ -569,7 +682,21 @@ impl ClientDef for AnyClient {
seq,
)
}
Self::Grandpa(client) => unimplemented!(),
Self::Grandpa(client) => {
let client_state = downcast!(
client_state => AnyClientState::Grandpa
)
.ok_or_else(|| Error::client_args_type_mismatch(ClientType::Grandpa))?;

client.verify_packet_receipt_absence(
client_state,
height,
proof,
port_id,
channel_id,
seq,
)
}

#[cfg(any(test, feature = "mocks"))]
Self::Mock(client) => {
Expand Down Expand Up @@ -617,7 +744,25 @@ impl ClientDef for AnyClient {
AnyConsensusState::Tendermint(new_consensus),
))
}
Self::Grandpa(client) => unimplemented!(),
Self::Grandpa(client) => {
let (client_state, consensus_state) = downcast!(
client_state => AnyClientState::Grandpa,
consensus_state => AnyConsensusState::Grandpa,
)
.ok_or_else(|| Error::client_args_type_mismatch(ClientType::Grandpa))?;

let (new_state, new_consensus) = client.verify_upgrade_and_update_state(
client_state,
consensus_state,
proof_upgrade_client,
proof_upgrade_consensus_state,
)?;

Ok((
AnyClientState::Grandpa(new_state),
AnyConsensusState::Grandpa(new_consensus),
))
}

#[cfg(any(test, feature = "mocks"))]
Self::Mock(client) => {
Expand Down
21 changes: 15 additions & 6 deletions modules/src/ics02_client/client_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ use crate::mock::client_state::MockClientState;
use crate::Height;

pub const TENDERMINT_CLIENT_STATE_TYPE_URL: &str = "/ibc.lightclients.tendermint.v1.ClientState";
pub const GRANDPA_CLIENT_STATE_TYPE_URL: &str = "/ibc.ligheclients.grandpa.v1.ClientState";

pub const MOCK_CLIENT_STATE_TYPE_URL: &str = "/ibc.mock.ClientState";

#[dyn_clonable::clonable]
Expand All @@ -45,7 +47,6 @@ pub trait ClientState: Clone + std::fmt::Debug + Send + Sync {
#[serde(tag = "type")]
pub enum AnyClientState {
Tendermint(client_state::ClientState),

Grandpa(ics10_grandpa::client_state::ClientState),

#[cfg(any(test, feature = "mocks"))]
Expand All @@ -56,7 +57,6 @@ impl AnyClientState {
pub fn latest_height(&self) -> Height {
match self {
Self::Tendermint(tm_state) => tm_state.latest_height(),

Self::Grandpa(tm_state) => tm_state.latest_height(),

#[cfg(any(test, feature = "mocks"))]
Expand All @@ -77,7 +77,6 @@ impl AnyClientState {
pub fn client_type(&self) -> ClientType {
match self {
Self::Tendermint(state) => state.client_type(),

Self::Grandpa(state) => state.client_type(),

#[cfg(any(test, feature = "mocks"))]
Expand Down Expand Up @@ -120,6 +119,11 @@ impl TryFrom<Any> for AnyClientState {
.map_err(Error::decode_raw_client_state)?,
)),

GRANDPA_CLIENT_STATE_TYPE_URL => Ok(AnyClientState::Grandpa(
crate::ics10_grandpa::client_state::ClientState::decode_vec(&raw.value)
.map_err(Error::decode_raw_client_state)?,
)),

#[cfg(any(test, feature = "mocks"))]
MOCK_CLIENT_STATE_TYPE_URL => Ok(AnyClientState::Mock(
MockClientState::decode_vec(&raw.value).map_err(Error::decode_raw_client_state)?,
Expand All @@ -140,7 +144,12 @@ impl From<AnyClientState> for Any {
.expect("encoding to `Any` from `AnyClientState::Tendermint`"),
},

AnyClientState::Grandpa(value) => unimplemented!(),
AnyClientState::Grandpa(value) => Any {
type_url: GRANDPA_CLIENT_STATE_TYPE_URL.to_string(),
value: value
.encode_vec()
.expect("encoding to `Any` from `AnyClientState::Grandpa`"),
},

#[cfg(any(test, feature = "mocks"))]
AnyClientState::Mock(value) => Any {
Expand All @@ -157,7 +166,7 @@ impl ClientState for AnyClientState {
fn chain_id(&self) -> ChainId {
match self {
AnyClientState::Tendermint(tm_state) => tm_state.chain_id(),
AnyClientState::Grandpa(_tm_state) => unimplemented!(),
AnyClientState::Grandpa( tm_state) => tm_state.chain_id(),

#[cfg(any(test, feature = "mocks"))]
AnyClientState::Mock(mock_state) => mock_state.chain_id(),
Expand All @@ -175,7 +184,7 @@ impl ClientState for AnyClientState {
fn is_frozen(&self) -> bool {
match self {
AnyClientState::Tendermint(tm_state) => tm_state.is_frozen(),
AnyClientState::Grandpa(_tm_state) => unimplemented!(),
AnyClientState::Grandpa( tm_state) => tm_state.is_frozen(),

#[cfg(any(test, feature = "mocks"))]
AnyClientState::Mock(mock_state) => mock_state.is_frozen(),
Expand Down
1 change: 0 additions & 1 deletion modules/src/ics02_client/client_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ impl ClientType {
/// Yields the identifier of this client type as a string
pub fn as_str(&self) -> &'static str {
match self {

Self::Tendermint => Self::TENDERMINT_STR,
Self::Grandpa => Self::GRANDPA_STR,

Expand Down
5 changes: 5 additions & 0 deletions modules/src/ics02_client/error.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::ics02_client::client_type::ClientType;
use crate::ics07_tendermint::error::Error as Ics07Error;
use crate::ics10_grandpa::error::Error as Ics10Error;
use crate::ics23_commitment::error::Error as Ics23Error;
use crate::ics24_host::error::ValidationError;
use crate::ics24_host::identifier::ClientId;
Expand Down Expand Up @@ -157,6 +158,10 @@ define_error! {
[ Ics07Error ]
| _ | { "tendermint error" },

Grandpa
[ Ics10Error ]
| _ | { "grandpa error" },

InvalidPacketTimestamp
[ TraceError<TryFromIntError> ]
| _ | { "invalid packet timeout timestamp value" },
Expand Down
Loading

0 comments on commit d5be5af

Please sign in to comment.