-
Notifications
You must be signed in to change notification settings - Fork 332
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Decode ClientState from Protobuf's Any type #233
Changes from all commits
632043d
141c657
9130e48
84c8853
869baa3
32f4c7a
df7f232
e0795c6
1c89a50
dc752e3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
pub struct AccAddress(Vec<u8>; | ||
pub struct AccAddress(Vec<u8>); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,30 @@ | ||
use prost::Message; | ||
use serde_derive::{Deserialize, Serialize}; | ||
|
||
use crate::downcast; | ||
use crate::ics02_client::client_type::ClientType; | ||
use crate::ics02_client::error; | ||
use crate::ics02_client::error::{self, Error}; | ||
use crate::ics02_client::header::Header; | ||
use crate::ics02_client::state::{ClientState, ConsensusState}; | ||
use crate::ics03_connection::connection::ConnectionEnd; | ||
use crate::ics07_tendermint as tendermint; | ||
use crate::ics07_tendermint::client_def::TendermintClient; | ||
use crate::ics07_tendermint::client_state::ClientState as TendermintClientState; | ||
use crate::ics23_commitment::commitment::{CommitmentPrefix, CommitmentProof, CommitmentRoot}; | ||
use crate::ics24_host::identifier::{ClientId, ConnectionId}; | ||
use crate::try_from_raw::TryFromRaw; | ||
|
||
use ibc_proto::ibc::tendermint::ClientState as RawTendermintClientState; | ||
|
||
use ::tendermint::block::Height; | ||
|
||
#[cfg(test)] | ||
use crate::mock_client::client_def::MockClient; | ||
#[cfg(test)] | ||
use crate::mock_client::header::MockHeader; | ||
#[cfg(test)] | ||
use crate::mock_client::state::{MockClientState, MockConsensusState}; | ||
use { | ||
crate::mock_client::client_def::MockClient, | ||
crate::mock_client::header::MockHeader, | ||
crate::mock_client::state::{MockClientState, MockConsensusState}, | ||
ibc_proto::ibc::mock::ClientState as RawMockClientState, | ||
}; | ||
|
||
pub trait ClientDef: Clone { | ||
type Header: Header; | ||
|
@@ -63,6 +69,7 @@ pub trait ClientDef: Clone { | |
) -> Result<(), Box<dyn std::error::Error>>; | ||
|
||
/// Verify the client state for this chain that it is stored on the counterparty chain. | ||
#[allow(clippy::too_many_arguments)] | ||
fn verify_client_full_state( | ||
&self, | ||
_client_state: &Self::ClientState, | ||
|
@@ -106,12 +113,39 @@ impl Header for AnyHeader { | |
|
||
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] | ||
pub enum AnyClientState { | ||
Tendermint(crate::ics07_tendermint::client_state::ClientState), | ||
Tendermint(TendermintClientState), | ||
|
||
#[cfg(test)] | ||
Mock(MockClientState), | ||
} | ||
|
||
impl AnyClientState { | ||
pub fn from_any(any: prost_types::Any) -> Result<Self, Error> { | ||
match any.type_url.as_str() { | ||
"ibc.tendermint.ClientState" => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Minor: Maybe we should consider keeping these strings (lines 125 To keep things simpler and get this merged fast, we can also just keep the hardcoded values as they are and just add a todo. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah good point! Those should probably be defined as constants somewhere. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wondering if we should define these There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed! See my reply to Adi’s comment above. |
||
let raw = RawTendermintClientState::decode(any.value.as_ref()) | ||
.map_err(|e| error::Kind::ProtoDecodingFailure.context(e))?; | ||
let client_state = TendermintClientState::try_from(raw) | ||
.map_err(|e| error::Kind::InvalidRawClientState.context(e))?; | ||
|
||
Ok(AnyClientState::Tendermint(client_state)) | ||
} | ||
|
||
#[cfg(test)] | ||
"ibc.mock.ClientState" => { | ||
let raw = RawMockClientState::decode(any.value.as_ref()) | ||
.map_err(|e| error::Kind::ProtoDecodingFailure.context(e))?; | ||
let client_state = MockClientState::try_from(raw) | ||
.map_err(|e| error::Kind::InvalidRawClientState.context(e))?; | ||
|
||
Ok(AnyClientState::Mock(client_state)) | ||
} | ||
|
||
_ => Err(error::Kind::UnknownClientStateType(any.type_url).into()), | ||
} | ||
} | ||
} | ||
|
||
impl ClientState for AnyClientState { | ||
fn chain_id(&self) -> String { | ||
todo!() | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,23 +14,26 @@ | |
//! TODO: Separate the Cosmos-SDK specific functionality from canonical ICS types. Decorators? | ||
|
||
#![allow(clippy::too_many_arguments)] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looking at the code, I think it's safe to remove this now. |
||
|
||
use crate::ics02_client::client_def::AnyClientState; | ||
use crate::ics03_connection::connection::{validate_version, validate_versions, Counterparty}; | ||
use crate::ics03_connection::error::{Error, Kind}; | ||
use crate::ics24_host::identifier::{ClientId, ConnectionId}; | ||
use crate::proofs::{ConsensusProof, Proofs}; | ||
use crate::try_from_raw::TryFromRaw; | ||
use crate::tx_msg::Msg; | ||
use ibc_proto::connection::MsgConnectionOpenAck as RawMsgConnectionOpenAck; | ||
use ibc_proto::connection::MsgConnectionOpenConfirm as RawMsgConnectionOpenConfirm; | ||
use ibc_proto::connection::MsgConnectionOpenInit as RawMsgConnectionOpenInit; | ||
use ibc_proto::connection::MsgConnectionOpenTry as RawMsgConnectionOpenTry; | ||
|
||
use crate::ics02_client::client_def::AnyClientState; | ||
use ibc_proto::ibc::connection::MsgConnectionOpenAck as RawMsgConnectionOpenAck; | ||
use ibc_proto::ibc::connection::MsgConnectionOpenConfirm as RawMsgConnectionOpenConfirm; | ||
use ibc_proto::ibc::connection::MsgConnectionOpenInit as RawMsgConnectionOpenInit; | ||
use ibc_proto::ibc::connection::MsgConnectionOpenTry as RawMsgConnectionOpenTry; | ||
|
||
use tendermint::account::Id as AccountId; | ||
use tendermint::block::Height; | ||
|
||
use serde_derive::{Deserialize, Serialize}; | ||
use std::convert::TryInto; | ||
use std::str::{from_utf8, FromStr}; | ||
use tendermint::account::Id as AccountId; | ||
use tendermint::block::Height; | ||
|
||
/// Message type for the `MsgConnectionOpenInit` message. | ||
pub const TYPE_MSG_CONNECTION_OPEN_INIT: &str = "connection_open_init"; | ||
|
@@ -48,7 +51,7 @@ pub const TYPE_MSG_CONNECTION_OPEN_CONFIRM: &str = "connection_open_confirm"; | |
#[derive(Clone, Debug)] | ||
pub enum ConnectionMsg { | ||
ConnectionOpenInit(MsgConnectionOpenInit), | ||
ConnectionOpenTry(MsgConnectionOpenTry), | ||
ConnectionOpenTry(Box<MsgConnectionOpenTry>), | ||
// ConnectionOpenAck(MsgConnectionOpenAck), | ||
// ConnectionOpenConfirm(MsgConnectionOpenConfirm), | ||
} | ||
|
@@ -214,31 +217,19 @@ impl Msg for MsgConnectionOpenTry { | |
} | ||
} | ||
|
||
pub fn unpack_client_state( | ||
any_client_raw: ::std::option::Option<::prost_types::Any>, | ||
) -> Result<Option<AnyClientState>, anomaly::Error<Kind>> { | ||
match any_client_raw { | ||
None => Ok(None), | ||
Some(_client_raw) => { | ||
// TODO deserialize | ||
//let _cs = client_raw.value.into(); | ||
Ok(None) | ||
} | ||
} | ||
} | ||
|
||
impl TryFromRaw for MsgConnectionOpenTry { | ||
type Error = Error; | ||
type RawType = RawMsgConnectionOpenTry; | ||
type Error = anomaly::Error<Kind>; | ||
|
||
fn try_from(msg: RawMsgConnectionOpenTry) -> Result<Self, Self::Error> { | ||
let proof_height = msg | ||
.proof_height | ||
.ok_or_else(|| Kind::MissingProofHeight)? | ||
.epoch_height; | ||
.epoch_height; // FIXME: This is wrong as it does not take the epoch number into account | ||
let consensus_height = msg | ||
.consensus_height | ||
.ok_or_else(|| Kind::MissingConsensusHeight)? | ||
.epoch_height; | ||
.epoch_height; // FIXME: This is wrong as it does not take the epoch number into account | ||
let consensus_proof_obj = ConsensusProof::new(msg.proof_consensus.into(), consensus_height) | ||
.map_err(|e| Kind::InvalidProof.context(e))?; | ||
|
||
|
@@ -256,7 +247,10 @@ impl TryFromRaw for MsgConnectionOpenTry { | |
.client_id | ||
.parse() | ||
.map_err(|e| Kind::IdentifierError.context(e))?, | ||
client_state: unpack_client_state(msg.client_state) | ||
client_state: msg | ||
.client_state | ||
.map(AnyClientState::from_any) | ||
.transpose() | ||
.map_err(|e| Kind::InvalidProof.context(e))?, | ||
counterparty: msg | ||
.counterparty | ||
|
@@ -354,11 +348,11 @@ impl TryFromRaw for MsgConnectionOpenAck { | |
let proof_height = msg | ||
.proof_height | ||
.ok_or_else(|| Kind::MissingProofHeight)? | ||
.epoch_height; | ||
.epoch_height; // FIXME: This is wrong as it does not take the epoch number into account | ||
let consensus_height = msg | ||
.consensus_height | ||
.ok_or_else(|| Kind::MissingConsensusHeight)? | ||
.epoch_height; | ||
.epoch_height; // FIXME: This is wrong as it does not take the epoch number into account | ||
let consensus_proof_obj = ConsensusProof::new(msg.proof_consensus.into(), consensus_height) | ||
.map_err(|e| Kind::InvalidProof.context(e))?; | ||
|
||
|
@@ -372,7 +366,10 @@ impl TryFromRaw for MsgConnectionOpenAck { | |
.connection_id | ||
.parse() | ||
.map_err(|e| Kind::IdentifierError.context(e))?, | ||
client_state: unpack_client_state(msg.client_state) | ||
client_state: msg | ||
.client_state | ||
.map(AnyClientState::from_any) | ||
.transpose() | ||
.map_err(|e| Kind::InvalidProof.context(e))?, | ||
version: validate_version(msg.version).map_err(|e| Kind::InvalidVersion.context(e))?, | ||
proofs: Proofs::new( | ||
|
@@ -444,7 +441,7 @@ impl TryFromRaw for MsgConnectionOpenConfirm { | |
let proof_height = msg | ||
.proof_height | ||
.ok_or_else(|| Kind::MissingProofHeight)? | ||
.epoch_height; | ||
.epoch_height; // FIXME: This is wrong as it does not take the epoch number into account | ||
Ok(Self { | ||
connection_id: msg | ||
.connection_id | ||
|
@@ -462,14 +459,14 @@ impl TryFromRaw for MsgConnectionOpenConfirm { | |
|
||
#[cfg(test)] | ||
pub mod test_util { | ||
use ibc_proto::connection::Counterparty as RawCounterparty; | ||
use ibc_proto::connection::MsgConnectionOpenAck as RawMsgConnectionOpenAck; | ||
use ibc_proto::connection::MsgConnectionOpenConfirm as RawMsgConnectionOpenConfirm; | ||
use ibc_proto::connection::MsgConnectionOpenInit as RawMsgConnectionOpenInit; | ||
use ibc_proto::connection::MsgConnectionOpenTry as RawMsgConnectionOpenTry; | ||
use ibc_proto::ibc::connection::Counterparty as RawCounterparty; | ||
use ibc_proto::ibc::connection::MsgConnectionOpenAck as RawMsgConnectionOpenAck; | ||
use ibc_proto::ibc::connection::MsgConnectionOpenConfirm as RawMsgConnectionOpenConfirm; | ||
use ibc_proto::ibc::connection::MsgConnectionOpenInit as RawMsgConnectionOpenInit; | ||
use ibc_proto::ibc::connection::MsgConnectionOpenTry as RawMsgConnectionOpenTry; | ||
|
||
use ibc_proto::client::Height; | ||
use ibc_proto::commitment::MerklePrefix; | ||
use ibc_proto::ibc::client::Height; | ||
use ibc_proto::ibc::commitment::MerklePrefix; | ||
|
||
pub fn get_dummy_proof() -> Vec<u8> { | ||
"Y29uc2Vuc3VzU3RhdGUvaWJjb25lY2xpZW50LzIy" | ||
|
@@ -573,12 +570,12 @@ mod tests { | |
MsgConnectionOpenAck, MsgConnectionOpenConfirm, MsgConnectionOpenTry, | ||
}; | ||
use crate::try_from_raw::TryFromRaw; | ||
use ibc_proto::client::Height; | ||
use ibc_proto::connection::Counterparty as RawCounterparty; | ||
use ibc_proto::connection::MsgConnectionOpenAck as RawMsgConnectionOpenAck; | ||
use ibc_proto::connection::MsgConnectionOpenConfirm as RawMsgConnectionOpenConfirm; | ||
use ibc_proto::connection::MsgConnectionOpenInit as RawMsgConnectionOpenInit; | ||
use ibc_proto::connection::MsgConnectionOpenTry as RawMsgConnectionOpenTry; | ||
use ibc_proto::ibc::client::Height; | ||
use ibc_proto::ibc::connection::Counterparty as RawCounterparty; | ||
use ibc_proto::ibc::connection::MsgConnectionOpenAck as RawMsgConnectionOpenAck; | ||
use ibc_proto::ibc::connection::MsgConnectionOpenConfirm as RawMsgConnectionOpenConfirm; | ||
use ibc_proto::ibc::connection::MsgConnectionOpenInit as RawMsgConnectionOpenInit; | ||
use ibc_proto::ibc::connection::MsgConnectionOpenTry as RawMsgConnectionOpenTry; | ||
|
||
#[test] | ||
fn parse_connection_open_init_msg() { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ahh this is such a treat to the eyes.