From af13da510e70639e30cc2cc3eaedc3d2e6e33eb9 Mon Sep 17 00:00:00 2001 From: Anca Zamfir Date: Wed, 23 Dec 2020 15:33:51 +0100 Subject: [PATCH] Migration to recent stargate (#449) * Initial migration changes * More changes, add some of the ack CLIs * cleanup * fix packet ack * clippy and fmt fixes * Fixed connOpenTry tests; ignored update client tests * Fixed ack msg tests * Added next identifier generation in mock context * Fixed ICS26 tests * Fix for relayer tests * Timeout packet, add Uninit for conn and chan, extract clientId * Fix option order in packet Tx clis * Tx result parsing (not generalized yet) * add extern paths back * WIP General event parsing out of tx response * change packet envelope to packet * Make the code compile, return clientID in relayer and CLI * event parsing and matching for conn open init * Change tm dependency, fix update client CLI * log fix and move to cosmos v0.40.0-rc5 (no changes) * Finished connection CLIs * Event parsing and fixes for channel open CLIs * add more event attributes, some refactor of the packet CLIs and packet relaying * cleanup CLI output for channel, add more fields to events * Fix relayer connection loop for dynamic IDs * Fix relayer channel loop for dynamic IDs, started on config cleanup * Cleanup relayer config * update Changelog * fix channel verification for packets * Fix changelog typo Co-authored-by: Adi Seredinschi --- CHANGELOG.md | 11 +- Cargo.toml | 2 +- .../adr-004-relayer-domain-decomposition.md | 6 +- docs/spec/relayer/Packets.md | 4 +- relayer-cli/Cargo.toml | 9 +- relayer-cli/src/commands/query.rs | 12 + relayer-cli/src/commands/query/client.rs | 16 +- relayer-cli/src/commands/query/packet.rs | 279 +++++++- relayer-cli/src/commands/tx.rs | 8 +- relayer-cli/src/commands/tx/channel.rs | 9 +- relayer-cli/src/commands/tx/client.rs | 64 +- relayer-cli/src/commands/tx/connection.rs | 9 +- relayer-cli/src/commands/tx/packet.rs | 132 +++- relayer-cli/src/commands/v0.rs | 2 - relayer-cli/tests/integration.rs | 1 - relayer/Cargo.toml | 10 +- relayer/src/chain.rs | 125 ++-- relayer/src/chain/cosmos.rs | 262 ++++--- relayer/src/chain/handle.rs | 71 +- relayer/src/chain/handle/prod.rs | 59 +- relayer/src/chain/mock.rs | 38 +- relayer/src/chain/runtime.rs | 113 +-- relayer/src/channel.rs | 449 ++++++------ relayer/src/config.rs | 38 +- relayer/src/connection.rs | 445 ++++++------ relayer/src/error.rs | 46 +- relayer/src/foreign_client.rs | 254 +++---- relayer/src/light_client/mock.rs | 2 +- relayer/src/light_client/tendermint.rs | 2 +- relayer/src/link.rs | 649 +++++++++++++----- relayer/src/relay.rs | 26 +- .../config/fixtures/relayer_conf_example.toml | 29 +- .../tests/config/fixtures/simple_config.toml | 16 +- 33 files changed, 1957 insertions(+), 1241 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index eccbacc942..50b5a403ad 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,12 +3,16 @@ ## Unreleased Changes ### FEATURES +- Update to tendermint-rs version `0.17` ([#451]) +- Update to cosmos-sdk IBC proto version `v0.40.0-rc5` ([#451]) + - [relayer] - - Implement relaying for recv_packet ([#379]) + - Implement packet relaying ([#379]) - [relayer-cli] - Packet CLIs for recv_packet ([#443]) - + - Packet CLIs for acknowledging packets ([#468]) + ### IMPROVEMENTS - [relayer] - Mock chain (implementing IBC handlers) and integration against CLI ([#158]) @@ -20,6 +24,9 @@ [#381]: https://github.com/informalsystems/ibc-rs/issues/381 [#443]: https://github.com/informalsystems/ibc-rs/issues/443 [#447]: https://github.com/informalsystems/ibc-rs/issues/447 +[#451]: https://github.com/informalsystems/ibc-rs/issues/451 +[#468]: https://github.com/informalsystems/ibc-rs/issues/468 + ## v0.0.5 *December 2, 2020* diff --git a/Cargo.toml b/Cargo.toml index 549ac13f08..f1e5c375e1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,4 +17,4 @@ tendermint = { git = "https://github.com/informalsystems/tendermint tendermint-rpc = { git = "https://github.com/informalsystems/tendermint-rs", branch = "romac/skip-verif" } tendermint-proto = { git = "https://github.com/informalsystems/tendermint-rs", branch = "romac/skip-verif" } tendermint-light-client = { git = "https://github.com/informalsystems/tendermint-rs", branch = "romac/skip-verif" } -tendermint-testgen = { git = "https://github.com/informalsystems/tendermint-rs", branch = "romac/skip-verif" } \ No newline at end of file +tendermint-testgen = { git = "https://github.com/informalsystems/tendermint-rs", branch = "romac/skip-verif" } diff --git a/docs/architecture/adr-004-relayer-domain-decomposition.md b/docs/architecture/adr-004-relayer-domain-decomposition.md index cdd7c1403f..28cf70513f 100644 --- a/docs/architecture/adr-004-relayer-domain-decomposition.md +++ b/docs/architecture/adr-004-relayer-domain-decomposition.md @@ -251,13 +251,11 @@ fn main() -> Result<(), Box> { let src_foreign_client_on_dst = ForeignClient::new( &src_chain_handle, - &dst_chain_handle, - ForeignClientConfig::default())?; + &dst_chain_handle)?; let dst_foreign_client_on_src = ForeignClient::new( &src_chain_handle, - &dst_chain_handle, - ForeignClientConfig::default())?; + &dst_chain_handle)?; let connection = Connection::new( &src_chain_handle, diff --git a/docs/spec/relayer/Packets.md b/docs/spec/relayer/Packets.md index b4374ac4c1..b9e24633ef 100644 --- a/docs/spec/relayer/Packets.md +++ b/docs/spec/relayer/Packets.md @@ -127,7 +127,7 @@ func CreateDatagram(ev WriteAcknowledgementEvent, // Stage 1 // Verify if acknowledment is committed to chain A and it is still pending - packetAck, packetAckCommitmentProof, error = + packetAck, PacketStateProof, error = GetPacketAcknowledgement(chainA, ev.port, ev.channel, ev.sequence, proofHeight) if error != nil { return (nil, error) } @@ -191,6 +191,6 @@ func CreateDatagram(ev WriteAcknowledgementEvent, data: ev.data } - return (PacketAcknowledgement { packet, ev.acknowledgement, packetAckCommitmentProof, proofHeight }, nil) + return (PacketAcknowledgement { packet, ev.acknowledgement, PacketStateProof, proofHeight }, nil) } ``` diff --git a/relayer-cli/Cargo.toml b/relayer-cli/Cargo.toml index 1d9796d2ad..2288c1a96e 100644 --- a/relayer-cli/Cargo.toml +++ b/relayer-cli/Cargo.toml @@ -20,6 +20,7 @@ tracing-subscriber = "0.2.3" futures = "0.3.5" toml = "0.5.6" serde_derive = "1.0.116" +serde_json = "1" sled = "0.34.4" prost = "0.6.1" prost-types = { version = "0.6.1" } @@ -27,17 +28,17 @@ hex = "0.4" crossbeam-channel = "0.5.0" [dependencies.tendermint-proto] -version = "=0.17.0-rc3" +version = "=0.17.0" [dependencies.tendermint] -version = "=0.17.0-rc3" +version = "=0.17.0" [dependencies.tendermint-rpc] -version = "=0.17.0-rc3" +version = "=0.17.0" features = ["http-client", "websocket-client"] [dependencies.tendermint-light-client] -version = "=0.17.0-rc3" +version = "=0.17.0" [dependencies.abscissa_core] version = "0.5.2" diff --git a/relayer-cli/src/commands/query.rs b/relayer-cli/src/commands/query.rs index 357dc6a900..e6147b4ba7 100644 --- a/relayer-cli/src/commands/query.rs +++ b/relayer-cli/src/commands/query.rs @@ -69,4 +69,16 @@ pub enum QueryPacketCmds { /// The `query unreceived packets` subcommand #[options(help = "query unreceived packets")] UnreceivedPackets(packet::QueryUnreceivedPacketsCmd), + + /// The `query packet commitments` subcommand + #[options(help = "query packet acknowledgements")] + Acks(packet::QueryPacketAcknowledgementsCmd), + + /// The `query packet commitment` subcommand + #[options(help = "query packet acknowledgment")] + Ack(packet::QueryPacketAcknowledgmentCmd), + + /// The `query unreceived packets` subcommand + #[options(help = "query un-acknowledged packets")] + UnreceivedAcks(packet::QueryUnreceivedAcknowledgementCmd), } diff --git a/relayer-cli/src/commands/query/client.rs b/relayer-cli/src/commands/query/client.rs index 0eaff5c1f9..9afb2fcd13 100644 --- a/relayer-cli/src/commands/query/client.rs +++ b/relayer-cli/src/commands/query/client.rs @@ -53,7 +53,7 @@ impl QueryClientStateCmd { let opts = QueryClientStateOptions { client_id, height: self.height.unwrap_or(0_u64), - proof: self.proof.unwrap_or(true), + proof: self.proof.unwrap_or(false), }; Ok((chain_config, opts)) } @@ -120,8 +120,8 @@ pub struct QueryClientConsensusCmd { #[derive(Debug)] struct QueryClientConsensusOptions { client_id: ClientId, - version_number: u64, - version_height: u64, + revision_number: u64, + revision_height: u64, height: u64, proof: bool, } @@ -135,11 +135,11 @@ impl QueryClientConsensusCmd { validate_common_options(&self.chain_id, &self.client_id, config)?; match (self.consensus_epoch, self.consensus_height) { - (Some(version_number), Some(version_height)) => { + (Some(revision_number), Some(revision_height)) => { let opts = QueryClientConsensusOptions { client_id, - version_number, - version_height, + revision_number, + revision_height, height: self.height.unwrap_or(0_u64), proof: self.proof.unwrap_or(true), }; @@ -179,8 +179,8 @@ impl Runnable for QueryClientConsensusCmd { .query( ClientConsensusState { client_id: opts.client_id, - epoch: opts.version_number, - height: opts.version_height, + epoch: opts.revision_number, + height: opts.revision_height, }, height, opts.proof, diff --git a/relayer-cli/src/commands/query/packet.rs b/relayer-cli/src/commands/query/packet.rs index 26bdd2d925..960219e59b 100644 --- a/relayer-cli/src/commands/query/packet.rs +++ b/relayer-cli/src/commands/query/packet.rs @@ -4,7 +4,8 @@ use tokio::runtime::Runtime as TokioRuntime; use abscissa_core::{Command, Options, Runnable}; use ibc_proto::ibc::core::channel::v1::{ - PacketAckCommitment, QueryPacketCommitmentsRequest, QueryUnreceivedPacketsRequest, + PacketState, QueryPacketAcknowledgementsRequest, QueryPacketCommitmentsRequest, + QueryUnreceivedAcksRequest, QueryUnreceivedPacketsRequest, }; use ibc::ics24_host::identifier::{ChannelId, PortId}; @@ -15,6 +16,7 @@ use relayer::config::{ChainConfig, Config}; use crate::error::{Error, Kind}; use crate::prelude::*; +use ibc::ics04_channel::packet::{PacketMsgType, Sequence}; #[derive(Clone, Command, Debug, Options)] pub struct QueryPacketCommitmentsCmd { @@ -75,7 +77,7 @@ impl Runnable for QueryPacketCommitmentsCmd { pagination: None, }; - let res: Result<(Vec, Height), Error> = chain + let res: Result<(Vec, Height), Error> = chain .query_packet_commitments(grpc_request) .map_err(|e| Kind::Query.context(e).into()); @@ -113,7 +115,7 @@ impl QueryPacketCommitmentCmd { fn validate_options( &self, config: &Config, - ) -> Result<(ChainConfig, QueryPacketOptions, u64), String> { + ) -> Result<(ChainConfig, QueryPacketOptions, Sequence), String> { let dest_chain_config = config .chains .iter() @@ -126,7 +128,7 @@ impl QueryPacketCommitmentCmd { height: self.height.unwrap_or(0_u64), }; - Ok((dest_chain_config.clone(), opts, self.sequence)) + Ok((dest_chain_config.clone(), opts, self.sequence.into())) } } @@ -144,11 +146,12 @@ impl Runnable for QueryPacketCommitmentCmd { status_info!("Options", "{:?}", opts); // run without proof: - // cargo run --bin relayer -- -c relayer/tests/config/fixtures/simple_config.toml query packet commitments ibc-0 transfer ibconexfer --height 3 + // cargo run --bin relayer -- -c relayer/tests/config/fixtures/simple_config.toml query packet commitment ibc-0 transfer ibconexfer 3 --height 3 let rt = Arc::new(Mutex::new(TokioRuntime::new().unwrap())); let chain = CosmosSDKChain::bootstrap(chain_config, rt).unwrap(); - let res = chain.proven_packet_commitment( + let res = chain.build_packet_proofs( + PacketMsgType::Recv, &opts.port_id, &opts.channel_id, sequence, @@ -157,12 +160,12 @@ impl Runnable for QueryPacketCommitmentCmd { match res { Ok(cs) => status_info!( - "Result for packet commitments query at height", + "Result for packet commitment query at height", "{:?} {:#?}", cs.0, cs.1 ), - Err(e) => status_info!("Error encountered on packet commitments query:", "{}", e), + Err(e) => status_info!("Error encountered on packet commitment query:", "{}", e), } } } @@ -267,7 +270,265 @@ impl Runnable for QueryUnreceivedPacketsCmd { match res { Ok(cs) => status_info!("Result for unreceived packets query", "{:?}", cs), - Err(e) => status_info!("Error encountered on packet commitments query:", "{}", e), + Err(e) => status_info!("Error encountered on unreceived packets query:", "{}", e), + } + } +} + +#[derive(Clone, Command, Debug, Options)] +pub struct QueryPacketAcknowledgementsCmd { + #[options(free, help = "identifier of the chain to query")] + chain_id: String, + + #[options(free, help = "identifier of the port to query")] + port_id: PortId, + + #[options(free, help = "identifier of the channel to query")] + channel_id: ChannelId, + + #[options(help = "height of the state to query", short = "h")] + height: Option, +} + +impl QueryPacketAcknowledgementsCmd { + fn validate_options( + &self, + config: &Config, + ) -> Result<(ChainConfig, QueryPacketOptions), String> { + let dest_chain_config = config + .chains + .iter() + .find(|c| c.id == self.chain_id.parse().unwrap()) + .ok_or_else(|| "missing destination chain configuration".to_string())?; + + let opts = QueryPacketOptions { + port_id: self.port_id.clone(), + channel_id: self.channel_id.clone(), + height: self.height.unwrap_or(0_u64), + }; + + Ok((dest_chain_config.clone(), opts)) + } +} + +// cargo run --bin relayer -- -c relayer/tests/config/fixtures/simple_config.toml query packet acknowledgements ibc-0 transfer ibconexfer --height 3 +impl Runnable for QueryPacketAcknowledgementsCmd { + fn run(&self) { + let config = app_config(); + + let (chain_config, opts) = match self.validate_options(&config) { + Err(err) => { + status_err!("invalid options: {}", err); + return; + } + Ok(result) => result, + }; + status_info!("Options", "{:?}", opts); + + let rt = Arc::new(Mutex::new(TokioRuntime::new().unwrap())); + let chain = CosmosSDKChain::bootstrap(chain_config, rt).unwrap(); + + let grpc_request = QueryPacketAcknowledgementsRequest { + port_id: opts.port_id.to_string(), + channel_id: opts.channel_id.to_string(), + pagination: None, + }; + + let res: Result<(Vec, Height), Error> = chain + .query_packet_acknowledgements(grpc_request) + .map_err(|e| Kind::Query.context(e).into()); + + match res { + Ok(cs) => status_info!( + "Result for packet acknowledgement query at height", + "{:?} {:#?}", + cs.0, + cs.1 + ), + Err(e) => status_info!( + "Error encountered on packet acknowledgement query:", + "{}", + e + ), + } + } +} + +#[derive(Clone, Command, Debug, Options)] +pub struct QueryPacketAcknowledgmentCmd { + #[options(free, help = "identifier of the chain to query")] + chain_id: String, + + #[options(free, help = "identifier of the port to query")] + port_id: PortId, + + #[options(free, help = "identifier of the channel to query")] + channel_id: ChannelId, + + #[options(free, help = "sequence of packet to query")] + sequence: u64, + + #[options(help = "height of the state to query", short = "h")] + height: Option, +} + +impl QueryPacketAcknowledgmentCmd { + fn validate_options( + &self, + config: &Config, + ) -> Result<(ChainConfig, QueryPacketOptions, Sequence), String> { + let dest_chain_config = config + .chains + .iter() + .find(|c| c.id == self.chain_id.parse().unwrap()) + .ok_or_else(|| "missing destination chain configuration".to_string())?; + + let opts = QueryPacketOptions { + port_id: self.port_id.clone(), + channel_id: self.channel_id.clone(), + height: self.height.unwrap_or(0_u64), + }; + + Ok((dest_chain_config.clone(), opts, self.sequence.into())) + } +} + +impl Runnable for QueryPacketAcknowledgmentCmd { + fn run(&self) { + let config = app_config(); + + let (chain_config, opts, sequence) = match self.validate_options(&config) { + Err(err) => { + status_err!("invalid options: {}", err); + return; + } + Ok(result) => result, + }; + status_info!("Options", "{:?}", opts); + + // run without proof: + // cargo run --bin relayer -- -c relayer/tests/config/fixtures/simple_config.toml query packet acknowledgment ibc-0 transfer ibconexfer --height 3 + let rt = Arc::new(Mutex::new(TokioRuntime::new().unwrap())); + let chain = CosmosSDKChain::bootstrap(chain_config, rt).unwrap(); + + let res = chain.build_packet_proofs( + PacketMsgType::Ack, + &opts.port_id, + &opts.channel_id, + sequence, + Height::new(0, opts.height), + ); + + match res { + Ok(cs) => status_info!( + "Result for packet acknowledgment query at height", + "{:?} {:#?}", + cs.0, + cs.1 + ), + Err(e) => status_info!("Error encountered on packet acknowledgment query:", "{}", e), + } + } +} + +/// This command does the following: +/// 1. queries the source chain to get the counterparty channel and port identifiers (needed in 3) +/// 2. queries the source chain for all packet commitmments/ sequences for a given port and channel +/// 3. queries the destination chain for the unreceived sequences out of the list obtained in 2. +#[derive(Clone, Command, Debug, Options)] +pub struct QueryUnreceivedAcknowledgementCmd { + #[options(free, help = "identifier of the chain to query the unreceived acks")] + dst_chain_id: String, + + #[options( + free, + help = "identifier of the chain where received sequences are queried" + )] + src_chain_id: String, + + #[options(free, help = "identifier of the port to query on source chain")] + port_id: PortId, + + #[options(free, help = "identifier of the channel to query on source chain")] + channel_id: ChannelId, +} + +impl QueryUnreceivedAcknowledgementCmd { + fn validate_options( + &self, + config: &Config, + ) -> Result<(ChainConfig, ChainConfig, QueryPacketOptions), String> { + let src_chain_config = config + .chains + .iter() + .find(|c| c.id == self.src_chain_id.parse().unwrap()) + .ok_or_else(|| "missing destination chain configuration".to_string())?; + + let dst_chain_config = config + .chains + .iter() + .find(|c| c.id == self.dst_chain_id.parse().unwrap()) + .ok_or_else(|| "missing destination chain configuration".to_string())?; + + let opts = QueryPacketOptions { + port_id: self.port_id.clone(), + channel_id: self.channel_id.clone(), + height: 0_u64, + }; + + Ok((dst_chain_config.clone(), src_chain_config.clone(), opts)) + } +} + +impl Runnable for QueryUnreceivedAcknowledgementCmd { + fn run(&self) { + let config = app_config(); + + let (dst_chain_config, src_chain_config, opts) = match self.validate_options(&config) { + Err(err) => { + status_err!("invalid options: {}", err); + return; + } + Ok(result) => result, + }; + status_info!("Options", "{:?}", opts); + + let rt = Arc::new(Mutex::new(TokioRuntime::new().unwrap())); + let src_chain = CosmosSDKChain::bootstrap(src_chain_config, rt.clone()).unwrap(); + let dst_chain = CosmosSDKChain::bootstrap(dst_chain_config, rt).unwrap(); + + // get the channel information from source chain + let channel = src_chain + .query_channel(&opts.port_id, &opts.channel_id, Height::zero()) + .unwrap(); + + // get the packet commitments on source chain + let acks_request = QueryPacketAcknowledgementsRequest { + port_id: opts.port_id.to_string(), + channel_id: opts.channel_id.to_string(), + pagination: None, + }; + + // extract the sequences + let sequences: Vec = src_chain + .query_packet_acknowledgements(acks_request) + .unwrap() + .0 + .into_iter() + .map(|v| v.sequence) + .collect(); + + let request = QueryUnreceivedAcksRequest { + port_id: channel.counterparty().port_id().to_string(), + channel_id: channel.counterparty().channel_id().unwrap().to_string(), + packet_ack_sequences: sequences, + }; + + let res = dst_chain.query_unreceived_acknowledgements(request); + + match res { + Ok(cs) => status_info!("Result for unreceived acks query", "{:?}", cs), + Err(e) => status_info!("Error encountered on unreceived acks query:", "{}", e), } } } diff --git a/relayer-cli/src/commands/tx.rs b/relayer-cli/src/commands/tx.rs index 1cd9c589a9..d8b57ad962 100644 --- a/relayer-cli/src/commands/tx.rs +++ b/relayer-cli/src/commands/tx.rs @@ -26,11 +26,11 @@ pub enum TxRawCommands { #[options(help = "get usage information")] Help(Help), - /// The `tx raw client-create` subcommand submits a MsgCreateClient in a transaction to a chain + /// The `tx raw create-client` subcommand submits a MsgCreateClient in a transaction to a chain #[options(help = "tx raw create-client")] CreateClient(TxCreateClientCmd), - /// The `tx raw client-update` subcommand submits a MsgUpdateClient in a transaction to a chain + /// The `tx raw update-client` subcommand submits a MsgUpdateClient in a transaction to a chain #[options(help = "tx raw update-client")] UpdateClient(TxUpdateClientCmd), @@ -69,4 +69,8 @@ pub enum TxRawCommands { /// The `tx raw packet-recv` subcommand #[options(help = "tx raw packet-recv")] PacketRecv(packet::TxRawPacketRecvCmd), + + /// The `tx raw packet-ack` subcommand + #[options(help = "tx raw packet-ack")] + PacketAck(packet::TxRawPacketAckCmd), } diff --git a/relayer-cli/src/commands/tx/channel.rs b/relayer-cli/src/commands/tx/channel.rs index 0eccd99a9f..18429f1254 100644 --- a/relayer-cli/src/commands/tx/channel.rs +++ b/relayer-cli/src/commands/tx/channel.rs @@ -1,6 +1,7 @@ use crate::prelude::*; use abscissa_core::{Command, Options, Runnable}; +use ibc::events::IBCEvent; use ibc::ics04_channel::channel::Order; use ibc::ics24_host::identifier::{ChannelId, ClientId, ConnectionId, PortId}; @@ -85,19 +86,19 @@ macro_rules! chan_open_cmd { ), }; - status_info!("Message ", "{}: {:#?}", $dbg_string, opts); + status_info!("Message ", "{}: {:?}", $dbg_string, opts); let (src_chain, _) = ChainRuntime::::spawn(src_chain_config.clone()).unwrap(); let (dst_chain, _) = ChainRuntime::::spawn(dst_chain_config.clone()).unwrap(); - let res: Result, Error> = + let res: Result = $func(dst_chain, src_chain, &opts).map_err(|e| Kind::Tx.context(e).into()); match res { - Ok(receipt) => status_ok!("Result: ", "{:?} - {:?}", $dbg_string, receipt), - Err(e) => status_err!("Failed with Error: {:?} - {:?}", $dbg_string, e), + Ok(receipt) => status_ok!("Ok: ", serde_json::to_string(&receipt).unwrap()), + Err(e) => status_err!("Error: {}", e), } } } diff --git a/relayer-cli/src/commands/tx/client.rs b/relayer-cli/src/commands/tx/client.rs index 0e324434cf..6e56a3a2ac 100644 --- a/relayer-cli/src/commands/tx/client.rs +++ b/relayer-cli/src/commands/tx/client.rs @@ -1,5 +1,6 @@ use abscissa_core::{Command, Options, Runnable}; +use ibc::events::IBCEvent; use ibc::ics24_host::identifier::ClientId; use crate::application::app_config; @@ -8,7 +9,7 @@ use crate::prelude::*; use relayer::chain::runtime::ChainRuntime; use relayer::chain::CosmosSDKChain; use relayer::config::ChainConfig; -use relayer::foreign_client::{build_update_client_and_send, ForeignClient, ForeignClientConfig}; +use relayer::foreign_client::{build_create_client_and_send, build_update_client_and_send}; #[derive(Clone, Command, Debug, Options)] pub struct TxCreateClientCmd { @@ -17,43 +18,34 @@ pub struct TxCreateClientCmd { #[options(free, help = "identifier of the source chain")] src_chain_id: String, - - #[options( - free, - help = "identifier of the client to be created on destination chain" - )] - dst_client_id: ClientId, } impl Runnable for TxCreateClientCmd { fn run(&self) { - let (dst_chain_config, src_chain_config, opts) = match validate_common_options( - &self.dst_chain_id, - &self.src_chain_id, - &self.dst_client_id, - ) { - Ok(result) => result, - Err(err) => { - status_err!("invalid options: {}", err); - return; - } - }; + let (dst_chain_config, src_chain_config) = + match validate_common_options(&self.dst_chain_id, &self.src_chain_id) { + Ok(result) => result, + Err(err) => { + status_err!("invalid options: {}", err); + return; + } + }; status_info!( "Message CreateClient", - "id: {:?}, for chain: {:?}, on chain: {:?}", - opts.client_id(), + "for source chain: {:?}, on destination chain: {:?}", src_chain_config.id, - opts.chain_id() + dst_chain_config.id ); let (src_chain, _) = ChainRuntime::::spawn(src_chain_config).unwrap(); let (dst_chain, _) = ChainRuntime::::spawn(dst_chain_config).unwrap(); - let res = ForeignClient::new(dst_chain, src_chain, opts).map_err(|e| Kind::Tx.context(e)); + let res: Result = build_create_client_and_send(dst_chain, src_chain) + .map_err(|e| Kind::Tx.context(e).into()); match res { - Ok(receipt) => status_ok!("Success", "client created: {:?}", receipt), + Ok(receipt) => status_ok!("Ok: ", serde_json::to_string(&receipt).unwrap()), Err(e) => status_err!("client create failed: {:?}", e), } } @@ -76,10 +68,9 @@ pub struct TxUpdateClientCmd { impl Runnable for TxUpdateClientCmd { fn run(&self) { - let opts = - validate_common_options(&self.dst_chain_id, &self.src_chain_id, &self.dst_client_id); + let opts = validate_common_options(&self.dst_chain_id, &self.src_chain_id); - let (dst_chain_config, src_chain_config, opts) = match opts { + let (dst_chain_config, src_chain_config) = match opts { Ok(result) => result, Err(err) => { status_err!("invalid options: {}", err); @@ -90,21 +81,21 @@ impl Runnable for TxUpdateClientCmd { status_info!( "Message UpdateClient", "id: {:?}, for chain: {:?}, on chain: {:?}", - opts.client_id(), + self.dst_client_id, src_chain_config.id, - opts.chain_id() + dst_chain_config.id ); let (src_chain, _) = ChainRuntime::::spawn(src_chain_config).unwrap(); let (dst_chain, _) = ChainRuntime::::spawn(dst_chain_config).unwrap(); - let res: Result, Error> = - build_update_client_and_send(dst_chain, src_chain, &opts) + let res: Result = + build_update_client_and_send(dst_chain, src_chain, &self.dst_client_id) .map_err(|e| Kind::Tx.context(e).into()); match res { - Ok(receipt) => status_ok!("Success client updated: {:?}", &receipt[0]), - Err(e) => status_err!("client update failed: {}", e), + Ok(receipt) => status_ok!("Ok: ", serde_json::to_string(&receipt).unwrap()), + Err(e) => status_err!("Error: {}", e), } } } @@ -112,8 +103,7 @@ impl Runnable for TxUpdateClientCmd { fn validate_common_options( dst_chain_id: &str, src_chain_id: &str, - dst_client_id: &ClientId, -) -> Result<(ChainConfig, ChainConfig, ForeignClientConfig), String> { +) -> Result<(ChainConfig, ChainConfig), String> { let config = app_config(); // Validate parameters @@ -138,9 +128,5 @@ fn validate_common_options( .find(|c| c.id == src_chain_id) .ok_or_else(|| "missing source chain configuration".to_string())?; - Ok(( - dst_chain_config.clone(), - src_chain_config.clone(), - ForeignClientConfig::new(&dst_chain_config.id, &dst_client_id), - )) + Ok((dst_chain_config.clone(), src_chain_config.clone())) } diff --git a/relayer-cli/src/commands/tx/connection.rs b/relayer-cli/src/commands/tx/connection.rs index 40a8f0301e..ad40179881 100644 --- a/relayer-cli/src/commands/tx/connection.rs +++ b/relayer-cli/src/commands/tx/connection.rs @@ -2,6 +2,7 @@ use crate::prelude::*; use abscissa_core::{Command, Options, Runnable}; +use ibc::events::IBCEvent; use ibc::ics24_host::identifier::{ClientId, ConnectionId}; use relayer::connection::{ @@ -74,19 +75,19 @@ macro_rules! conn_open_cmd { ), }; - status_info!("Message ", "{}: {:#?}", $dbg_string, opts); + status_info!("Message ", "{}: {:?}", $dbg_string, opts); let (src_chain, _) = ChainRuntime::::spawn(src_chain_config.clone()).unwrap(); let (dst_chain, _) = ChainRuntime::::spawn(dst_chain_config.clone()).unwrap(); - let res: Result, Error> = + let res: Result = $func(dst_chain, src_chain, &opts).map_err(|e| Kind::Tx.context(e).into()); match res { - Ok(receipt) => status_ok!("Result: ", "{:?} - {:?}", $dbg_string, receipt[0]), - Err(e) => status_err!("Failed with Error: {:?} - {:?}", $dbg_string, e), + Ok(receipt) => status_ok!("Ok: ", serde_json::to_string(&receipt).unwrap()), + Err(e) => status_err!("Error: {}", e), } } } diff --git a/relayer-cli/src/commands/tx/packet.rs b/relayer-cli/src/commands/tx/packet.rs index cb09180ac1..6baa5039cb 100644 --- a/relayer-cli/src/commands/tx/packet.rs +++ b/relayer-cli/src/commands/tx/packet.rs @@ -4,56 +4,156 @@ use abscissa_core::{Command, Options, Runnable}; use relayer::config::Config; use crate::error::{Error, Kind}; +use ibc::events::IBCEvent; use ibc::ics24_host::identifier::{ChannelId, ClientId, PortId}; use relayer::chain::runtime::ChainRuntime; use relayer::chain::CosmosSDKChain; -use relayer::link::{build_and_send_recv_packet_messages, PacketOptions}; +use relayer::link::{ + build_and_send_ack_packet_messages, build_and_send_recv_packet_messages, PacketOptions, +}; #[derive(Clone, Command, Debug, Options)] pub struct TxRawPacketRecvCmd { + #[options(free, help = "identifier of the source chain")] + src_chain_id: String, + #[options(free, help = "identifier of the destination chain")] dest_chain_id: String, + #[options(free, help = "identifier of the source client")] + src_client_id: ClientId, + #[options(free, help = "identifier of the destination client")] dest_client_id: ClientId, - #[options(free, help = "identifier of the source chain")] - src_chain_id: String, - #[options(free, help = "identifier of the source port")] src_port_id: PortId, + #[options(free, help = "identifier of the destination port")] + dst_port_id: PortId, + #[options(free, help = "identifier of the source channel")] src_channel_id: ChannelId, + + #[options(free, help = "identifier of the destination channel")] + dst_channel_id: ChannelId, } impl TxRawPacketRecvCmd { fn validate_options(&self, config: &Config) -> Result { + let src_chain_config = config + .chains + .iter() + .find(|c| c.id == self.src_chain_id.parse().unwrap()) + .ok_or_else(|| "missing src chain configuration".to_string())?; + let dest_chain_config = config .chains .iter() .find(|c| c.id == self.dest_chain_id.parse().unwrap()) .ok_or_else(|| "missing destination chain configuration".to_string())?; + let opts = PacketOptions { + packet_src_chain_config: src_chain_config.clone(), + packet_src_client_id: self.src_client_id.clone(), + packet_src_port_id: self.src_port_id.clone(), + packet_src_channel_id: self.src_channel_id.clone(), + packet_dst_chain_config: dest_chain_config.clone(), + packet_dst_client_id: self.dest_client_id.clone(), + packet_dst_port_id: self.dst_port_id.clone(), + packet_dst_channel_id: self.dst_channel_id.clone(), + }; + + Ok(opts) + } +} + +impl Runnable for TxRawPacketRecvCmd { + fn run(&self) { + let config = app_config(); + + let opts = match self.validate_options(&config) { + Err(err) => { + status_err!("invalid options: {}", err); + return; + } + Ok(result) => result, + }; + status_info!("Message", "{:?}", opts); + + let (src_chain, _) = + ChainRuntime::::spawn(opts.packet_src_chain_config.clone()).unwrap(); + let (dst_chain, _) = + ChainRuntime::::spawn(opts.packet_dst_chain_config.clone()).unwrap(); + + let res: Result, Error> = + build_and_send_recv_packet_messages(src_chain, dst_chain, &opts) + .map_err(|e| Kind::Tx.context(e).into()); + + match res { + Ok(ev) => status_info!("packet recv, result: ", "{:#?}", ev), + Err(e) => status_info!("packet recv failed, error: ", "{}", e), + } + } +} + +#[derive(Clone, Command, Debug, Options)] +pub struct TxRawPacketAckCmd { + #[options(free, help = "identifier of the source chain")] + src_chain_id: String, + + #[options(free, help = "identifier of the destination chain")] + dest_chain_id: String, + + #[options(free, help = "identifier of the source client")] + src_client_id: ClientId, + + #[options(free, help = "identifier of the destination client")] + dest_client_id: ClientId, + + #[options(free, help = "identifier of the source port")] + src_port_id: PortId, + + #[options(free, help = "identifier of the destination port")] + dst_port_id: PortId, + + #[options(free, help = "identifier of the source channel")] + src_channel_id: ChannelId, + + #[options(free, help = "identifier of the destination channel")] + dst_channel_id: ChannelId, +} + +impl TxRawPacketAckCmd { + fn validate_options(&self, config: &Config) -> Result { let src_chain_config = config .chains .iter() .find(|c| c.id == self.src_chain_id.parse().unwrap()) .ok_or_else(|| "missing src chain configuration".to_string())?; + let dest_chain_config = config + .chains + .iter() + .find(|c| c.id == self.dest_chain_id.parse().unwrap()) + .ok_or_else(|| "missing destination chain configuration".to_string())?; + let opts = PacketOptions { - dst_chain_config: dest_chain_config.clone(), - src_chain_config: src_chain_config.clone(), - dst_client_id: self.dest_client_id.clone(), - src_port_id: self.src_port_id.clone(), - src_channel_id: self.src_channel_id.clone(), + packet_src_chain_config: src_chain_config.clone(), + packet_src_client_id: self.src_client_id.clone(), + packet_src_port_id: self.src_port_id.clone(), + packet_src_channel_id: self.src_channel_id.clone(), + packet_dst_chain_config: dest_chain_config.clone(), + packet_dst_client_id: self.dest_client_id.clone(), + packet_dst_port_id: self.dst_port_id.clone(), + packet_dst_channel_id: self.dst_channel_id.clone(), }; Ok(opts) } } -impl Runnable for TxRawPacketRecvCmd { +impl Runnable for TxRawPacketAckCmd { fn run(&self) { let config = app_config(); @@ -67,17 +167,17 @@ impl Runnable for TxRawPacketRecvCmd { status_info!("Message", "{:?}", opts); let (src_chain, _) = - ChainRuntime::::spawn(opts.src_chain_config.clone()).unwrap(); + ChainRuntime::::spawn(opts.packet_src_chain_config.clone()).unwrap(); let (dst_chain, _) = - ChainRuntime::::spawn(opts.dst_chain_config.clone()).unwrap(); + ChainRuntime::::spawn(opts.packet_dst_chain_config.clone()).unwrap(); - let res: Result, Error> = - build_and_send_recv_packet_messages(dst_chain, src_chain, &opts) + let res: Result, Error> = + build_and_send_ack_packet_messages(src_chain, dst_chain, &opts) .map_err(|e| Kind::Tx.context(e).into()); match res { - Ok(receipt) => status_info!("packet recv, result: ", "{:#?}", receipt), - Err(e) => status_info!("packet recv failed, error: ", "{}", e), + Ok(ev) => status_info!("packet ack, result: ", "{:#?}", ev), + Err(e) => status_info!("packet ack failed, error: ", "{}", e), } } } diff --git a/relayer-cli/src/commands/v0.rs b/relayer-cli/src/commands/v0.rs index 4b69711683..4c9b9449b5 100644 --- a/relayer-cli/src/commands/v0.rs +++ b/relayer-cli/src/commands/v0.rs @@ -33,7 +33,6 @@ impl Runnable for V0Cmd { pub fn v0_task(config: &Config) -> Result<(), BoxError> { // Relay for a single channel, first on the first connection in configuration - // TODO - change the config to use typed Ids and same with ConnectionConfig, ClientConfig, ChannelConfig, etc let conn = &config .connections .clone() @@ -64,7 +63,6 @@ pub fn v0_task(config: &Config) -> Result<(), BoxError> { Ok(channel_relay( src_chain_handle, dst_chain_handle, - connection_cfg, channel_cfg, )?) } diff --git a/relayer-cli/tests/integration.rs b/relayer-cli/tests/integration.rs index 3631430f71..3b7354af65 100644 --- a/relayer-cli/tests/integration.rs +++ b/relayer-cli/tests/integration.rs @@ -33,7 +33,6 @@ fn simd_config() -> Config { account_prefix: "cosmos".to_string(), key_name: "testkey".to_string(), store_prefix: "ibc".to_string(), - client_ids: vec!["ethbridge".to_string()], gas: Some(200000), max_msg_num: None, max_tx_size: None, diff --git a/relayer/Cargo.toml b/relayer/Cargo.toml index 99f9dfa205..4d0ac1bdda 100644 --- a/relayer/Cargo.toml +++ b/relayer/Cargo.toml @@ -42,19 +42,19 @@ dirs-next = "2.0.0" dyn-clone = "1.0.3" [dependencies.tendermint] -version = "=0.17.0-rc3" +version = "=0.17.0" [dependencies.tendermint-rpc] -version = "=0.17.0-rc3" +version = "=0.17.0" features = ["http-client", "websocket-client"] [dependencies.tendermint-light-client] -version = "=0.17.0-rc3" +version = "=0.17.0" [dependencies.tendermint-proto] -version = "=0.17.0-rc3" +version = "=0.17.0" [dev-dependencies] serial_test = "0.5.0" ibc = { path = "../modules", features = [ "mocks" ] } -tendermint-testgen = { version = "0.17.0-rc3" } \ No newline at end of file +tendermint-testgen = { version = "0.17.0" } # Needed for generating (synthetic) light blocks. diff --git a/relayer/src/chain.rs b/relayer/src/chain.rs index 313df4bd9f..592ccf3dc2 100644 --- a/relayer/src/chain.rs +++ b/relayer/src/chain.rs @@ -23,16 +23,18 @@ use tendermint::account::Id as AccountId; use tendermint::block::Height; use ibc_proto::ibc::core::channel::v1::{ - PacketAckCommitment, QueryPacketCommitmentsRequest, QueryUnreceivedPacketsRequest, + PacketState, QueryPacketAcknowledgementsRequest, QueryPacketCommitmentsRequest, + QueryUnreceivedAcksRequest, QueryUnreceivedPacketsRequest, }; +use ibc_proto::ibc::core::commitment::v1::MerkleProof; +use ibc::events::IBCEvent; use ibc::ics02_client::header::Header; use ibc::ics02_client::state::{ClientState, ConsensusState}; use ibc::ics03_connection::connection::ConnectionEnd; -use ibc::ics03_connection::version::get_compatible_versions; +use ibc::ics03_connection::version::{get_compatible_versions, Version}; use ibc::ics04_channel::channel::{ChannelEnd, QueryPacketEventDataRequest}; -use ibc::ics23_commitment::commitment::{CommitmentPrefix, CommitmentProof}; -use ibc::ics23_commitment::merkle::MerkleProof; +use ibc::ics23_commitment::commitment::{CommitmentPrefix, CommitmentProofBytes}; use ibc::ics24_host::identifier::{ChainId, ChannelId, ClientId, ConnectionId, PortId}; use ibc::ics24_host::Path; use ibc::proofs::{ConsensusProof, Proofs}; @@ -44,14 +46,14 @@ use crate::error::{Error, Kind}; use crate::event::monitor::EventBatch; use crate::keyring::store::{KeyEntry, KeyRing}; use crate::light_client::LightClient; -use ibc::events::IBCEvent; +use ibc::ics04_channel::packet::{PacketMsgType, Sequence}; /// Generic query response type /// TODO - will slowly move to GRPC protobuf specs for queries #[derive(Clone, Debug, PartialEq)] pub struct QueryResponse { pub value: Vec, - pub proof: MerkleProof, + pub proof: Option, pub height: Height, } @@ -108,7 +110,7 @@ pub trait Chain: Sized { fn query(&self, data: Path, height: ICSHeight, prove: bool) -> Result; /// Sends one or more transactions with `msgs` to chain. - fn send_msgs(&mut self, proto_msgs: Vec) -> Result, Error>; + fn send_msgs(&mut self, proto_msgs: Vec) -> Result, Error>; fn get_signer(&mut self) -> Result; @@ -141,7 +143,7 @@ pub trait Chain: Sized { fn query_commitment_prefix(&self) -> Result; - fn query_compatible_versions(&self) -> Result, Error> { + fn query_compatible_versions(&self) -> Result, Error> { // TODO - do a real chain query Ok(get_compatible_versions()) } @@ -188,7 +190,11 @@ pub trait Chain: Sized { let connection_end = ConnectionEnd::decode_vec(&res.value).map_err(|e| Kind::Query.context(e))?; - Ok((connection_end, res.proof)) + Ok(( + connection_end, + res.proof + .ok_or_else(|| Kind::Query.context("empty proof".to_string()))?, + )) } fn proven_client_consensus( @@ -214,7 +220,11 @@ pub trait Chain: Sized { let channel_end = ChannelEnd::decode_vec(&res.value).map_err(|e| Kind::Query.context(e))?; - Ok((channel_end, res.proof)) + Ok(( + channel_end, + res.proof + .ok_or_else(|| Kind::Query.context("empty proof".to_string()))?, + )) } /// Builds the required proofs and the client state for connection handshake messages. @@ -234,7 +244,7 @@ pub trait Chain: Sized { // Collect all proofs as required let connection_proof = - CommitmentProof::from(self.proven_connection(&connection_id, query_height)?.1); + CommitmentProofBytes::from(self.proven_connection(&connection_id, query_height)?.1); let mut client_state = None; let mut client_proof = None; @@ -245,7 +255,7 @@ pub trait Chain: Sized { let (client_state_value, client_state_proof) = self.proven_client_state(&client_id, query_height)?; - client_proof = Some(CommitmentProof::from(client_state_proof)); + client_proof = Some(CommitmentProofBytes::from(client_state_proof)); let consensus_state_proof = self .proven_client_consensus( @@ -257,15 +267,11 @@ pub trait Chain: Sized { consensus_proof = Option::from( ConsensusProof::new( - CommitmentProof::from(consensus_state_proof), + CommitmentProofBytes::from(consensus_state_proof), client_state_value.latest_height(), ) .map_err(|e| { - Kind::ConnOpenTry( - connection_id.clone(), - "failed to build consensus proof".to_string(), - ) - .context(e) + Kind::ConnOpenTry("failed to build consensus proof".to_string()).context(e) })?, ); @@ -306,42 +312,81 @@ pub trait Chain: Sized { // Collect all proofs as required let channel_proof = - CommitmentProof::from(self.proven_channel(port_id, channel_id, query_height)?.1); + CommitmentProofBytes::from(self.proven_channel(port_id, channel_id, query_height)?.1); Ok(Proofs::new(channel_proof, None, None, height).map_err(|_| Kind::MalformedProof)?) } - fn proven_packet_commitment( + fn query_packet_commitments( + &self, + request: QueryPacketCommitmentsRequest, + ) -> Result<(Vec, ICSHeight), Error>; + + fn query_unreceived_packets( &self, + request: QueryUnreceivedPacketsRequest, + ) -> Result, Error>; + + fn query_packet_acknowledgements( + &self, + request: QueryPacketAcknowledgementsRequest, + ) -> Result<(Vec, ICSHeight), Error>; + + fn query_unreceived_acknowledgements( + &self, + request: QueryUnreceivedAcksRequest, + ) -> Result, Error>; + + fn build_packet_proofs( + &self, + packet_type: PacketMsgType, port_id: &PortId, channel_id: &ChannelId, - sequence: u64, + sequence: Sequence, height: ICSHeight, - ) -> Result<(Vec, MerkleProof), Error> { - let res = self - .query( - Path::Commitments { + ) -> Result<(Vec, Proofs), Error> { + let data: Path; + match packet_type { + PacketMsgType::Recv => { + data = Path::Commitments { port_id: port_id.clone(), channel_id: channel_id.clone(), sequence, - }, - height, - true, - ) + } + } + PacketMsgType::Ack => { + data = Path::Acks { + port_id: port_id.clone(), + channel_id: channel_id.clone(), + sequence, + } + } + PacketMsgType::Timeout => { + data = Path::Receipts { + port_id: port_id.clone(), + channel_id: channel_id.clone(), + sequence, + } + } + } + + let res = self + .query(data, height, true) .map_err(|e| Kind::Query.context(e))?; - Ok((res.value, res.proof)) + let proofs = Proofs::new( + CommitmentProofBytes::from( + res.proof + .ok_or_else(|| Kind::Query.context("empty proof".to_string()))?, + ), + None, + None, + height.increment(), + ) + .map_err(|_| Kind::MalformedProof)?; + + Ok((res.value, proofs)) } - fn query_packet_commitments( - &self, - request: QueryPacketCommitmentsRequest, - ) -> Result<(Vec, ICSHeight), Error>; - - fn query_unreceived_packets( - &self, - request: QueryUnreceivedPacketsRequest, - ) -> Result, Error>; - fn query_txs(&self, request: QueryPacketEventDataRequest) -> Result, Error>; } diff --git a/relayer/src/chain/cosmos.rs b/relayer/src/chain/cosmos.rs index 4dba252983..aef18dc56f 100644 --- a/relayer/src/chain/cosmos.rs +++ b/relayer/src/chain/cosmos.rs @@ -8,8 +8,6 @@ use std::{ time::Duration, }; -use subtle_encoding::base64; - use anomaly::fail; use bitcoin::hashes::hex::ToHex; @@ -19,7 +17,6 @@ use prost_types::Any; use tokio::runtime::Runtime as TokioRuntime; use tonic::codegen::http::Uri; -use tendermint_proto::crypto::ProofOps; use tendermint_proto::Protobuf; use tendermint_rpc::query::Query; @@ -30,7 +27,7 @@ use tendermint::block::Height; use tendermint::consensus::Params; use tendermint_light_client::types::LightBlock as TMLightBlock; -use tendermint_rpc::{Client, HttpClient, Order}; +use tendermint_rpc::{endpoint::broadcast::tx_commit::Response, Client, HttpClient, Order}; use ibc_proto::cosmos::base::v1beta1::Coin; @@ -39,20 +36,21 @@ use ibc_proto::cosmos::tx::v1beta1::{AuthInfo, Fee, ModeInfo, SignDoc, SignerInf // Support for GRPC use ibc_proto::cosmos::auth::v1beta1::{BaseAccount, QueryAccountRequest}; use ibc_proto::ibc::core::channel::v1::{ - PacketAckCommitment, QueryPacketCommitmentsRequest, QueryUnreceivedPacketsRequest, + PacketState, QueryPacketAcknowledgementsRequest, QueryPacketCommitmentsRequest, + QueryUnreceivedAcksRequest, QueryUnreceivedPacketsRequest, }; +use ibc_proto::ibc::core::commitment::v1::MerkleProof; use ibc::downcast; -use ibc::events::{IBCEvent, IBCEventType}; +use ibc::events::{from_tx_response_event, IBCEvent}; use ibc::ics02_client::client_def::{AnyClientState, AnyConsensusState}; use ibc::ics04_channel::channel::QueryPacketEventDataRequest; -use ibc::ics04_channel::events::{PacketEnvelope, SendPacket}; use ibc::ics07_tendermint::client_state::ClientState; use ibc::ics07_tendermint::consensus_state::ConsensusState as TMConsensusState; use ibc::ics07_tendermint::header::Header as TMHeader; use ibc::ics23_commitment::commitment::CommitmentPrefix; -use ibc::ics23_commitment::merkle::MerkleProof; +use ibc::ics23_commitment::merkle::convert_tm_to_ics_merkle_proof; use ibc::ics24_host::identifier::{ChainId, ClientId}; use ibc::ics24_host::Path::ClientConsensusState as ClientConsensusPath; use ibc::ics24_host::Path::ClientState as ClientStatePath; @@ -69,6 +67,7 @@ use crate::event::monitor::{EventBatch, EventMonitor}; use crate::keyring::store::{KeyEntry, KeyRing, KeyRingOperations, StoreBackend}; use crate::light_client::tendermint::LightClient as TMLightClient; use crate::light_client::LightClient; +use ibc::ics04_channel::packet::Sequence; // TODO size this properly const DEFAULT_MAX_GAS: u64 = 300000; @@ -134,7 +133,7 @@ impl CosmosSDKChain { Ok(self.rt.lock().map_err(|_| Kind::PoisonedMutex)?.block_on(f)) } - fn send_tx(&self, proto_msgs: Vec) -> Result { + fn send_tx(&self, proto_msgs: Vec) -> Result, Error> { let key = self .keybase() .get_key() @@ -223,7 +222,9 @@ impl CosmosSDKChain { .block_on(broadcast_tx_commit(self, txraw_buf))? .map_err(|e| Kind::Rpc.context(e))?; - Ok(response) + let res = tx_result_to_event(response)?; + + Ok(res) } fn gas(&self) -> u64 { @@ -304,7 +305,7 @@ impl Chain for CosmosSDKChain { let path = TendermintABCIPath::from_str(IBC_QUERY_PATH).unwrap(); let height = - Height::try_from(height.version_height).map_err(|e| Kind::InvalidHeight.context(e))?; + Height::try_from(height.revision_height).map_err(|e| Kind::InvalidHeight.context(e))?; if !data.is_provable() & prove { return Err(Kind::Store @@ -322,9 +323,9 @@ impl Chain for CosmosSDKChain { } /// Send one or more transactions that include all the specified messages - fn send_msgs(&mut self, proto_msgs: Vec) -> Result, Error> { + fn send_msgs(&mut self, proto_msgs: Vec) -> Result, Error> { if proto_msgs.is_empty() { - return Ok(vec!["No messages to send".to_string()]); + return Ok(vec![IBCEvent::Empty("No messages to send".to_string())]); } let mut res = vec![]; @@ -338,17 +339,18 @@ impl Chain for CosmosSDKChain { n += 1; size += buf.len(); if n >= self.max_msg_num() || size >= self.max_tx_size() { - let result = self.send_tx(msg_batch)?; - res.append(&mut vec![result]); + let mut result = self.send_tx(msg_batch)?; + res.append(&mut result); n = 0; size = 0; msg_batch = vec![]; } } if !msg_batch.is_empty() { - let result = self.send_tx(msg_batch)?; - res.append(&mut vec![result]); + let mut result = self.send_tx(msg_batch)?; + res.append(&mut result); } + Ok(res) } @@ -368,8 +370,8 @@ impl Chain for CosmosSDKChain { } Ok(ICSHeight { - version_number: ChainId::chain_version(status.node_info.network.as_str()), - version_height: u64::from(status.sync_info.latest_block_height), + revision_number: ChainId::chain_version(status.node_info.network.as_str()), + revision_height: u64::from(status.sync_info.latest_block_height), }) } @@ -411,7 +413,11 @@ impl Chain for CosmosSDKChain { let client_state = downcast!(client_state => AnyClientState::Tendermint) .ok_or_else(|| Kind::Query.context("unexpected client state type"))?; - Ok((client_state, res.proof)) + Ok(( + client_state, + res.proof + .ok_or_else(|| Kind::Query.context("empty proof".to_string()))?, + )) } fn proven_client_consensus( @@ -424,8 +430,8 @@ impl Chain for CosmosSDKChain { .query( ClientConsensusPath { client_id: client_id.clone(), - epoch: consensus_height.version_number, - height: consensus_height.version_height, + epoch: consensus_height.revision_number, + height: consensus_height.revision_height, }, height, true, @@ -438,12 +444,16 @@ impl Chain for CosmosSDKChain { let consensus_state = downcast!(consensus_state => AnyConsensusState::Tendermint) .ok_or_else(|| Kind::Query.context("unexpected client consensus type"))?; - Ok((consensus_state, res.proof)) + Ok(( + consensus_state, + res.proof + .ok_or_else(|| Kind::Query.context("empty proof".to_string()))?, + )) } fn build_client_state(&self, height: ICSHeight) -> Result { // Build the client state. - let client_state = ibc::ics07_tendermint::client_state::ClientState::new( + Ok(ibc::ics07_tendermint::client_state::ClientState::new( self.id().to_string(), self.config.trust_threshold, self.config.trusting_period, @@ -451,14 +461,11 @@ impl Chain for CosmosSDKChain { Duration::from_millis(3000), // TODO - get it from src config when avail height, ICSHeight::zero(), - self.query_consensus_params()?, - "upgrade/upgradedClient".to_string(), + vec!["upgrade".to_string(), "upgradedIBCState".to_string()], false, false, ) - .map_err(|e| Kind::BuildClientStateFailure.context(e))?; - - Ok(client_state) + .map_err(|e| Kind::BuildClientStateFailure.context(e))?) } fn build_consensus_state( @@ -509,13 +516,11 @@ impl Chain for CosmosSDKChain { Ok(key) } /// Queries the packet commitment hashes associated with a channel. - /// TODO - move the chain trait - /// Note: the result Vec has an awkward name but fixed in a future IBC proto variant - /// It will move to Vec + /// TODO - move to the chain trait fn query_packet_commitments( &self, request: QueryPacketCommitmentsRequest, - ) -> Result<(Vec, ICSHeight), Error> { + ) -> Result<(Vec, ICSHeight), Error> { let grpc_addr = Uri::from_str(&self.config().grpc_addr).map_err(|e| Kind::Grpc.context(e))?; let mut client = self @@ -566,6 +571,62 @@ impl Chain for CosmosSDKChain { Ok(response.sequences) } + /// Queries the packet acknowledgment hashes associated with a channel. + /// TODO - move to the chain trait + fn query_packet_acknowledgements( + &self, + request: QueryPacketAcknowledgementsRequest, + ) -> Result<(Vec, ICSHeight), Error> { + let grpc_addr = + Uri::from_str(&self.config().grpc_addr).map_err(|e| Kind::Grpc.context(e))?; + let mut client = self + .block_on( + ibc_proto::ibc::core::channel::v1::query_client::QueryClient::connect(grpc_addr), + )? + .map_err(|e| Kind::Grpc.context(e))?; + + let request = tonic::Request::new(request); + + let response = self + .block_on(client.packet_acknowledgements(request))? + .map_err(|e| Kind::Grpc.context(e))? + .into_inner(); + + let pc = response.acknowledgements; + + let height = response + .height + .ok_or_else(|| Kind::Grpc.context("missing height in response"))? + .try_into() + .map_err(|_| Kind::Grpc.context("invalid height in response"))?; + + Ok((pc, height)) + } + + /// Queries the packet commitment hashes associated with a channel. + /// TODO - move the chain trait + fn query_unreceived_acknowledgements( + &self, + request: QueryUnreceivedAcksRequest, + ) -> Result, Error> { + let grpc_addr = + Uri::from_str(&self.config().grpc_addr).map_err(|e| Kind::Grpc.context(e))?; + let mut client = self + .block_on( + ibc_proto::ibc::core::channel::v1::query_client::QueryClient::connect(grpc_addr), + )? + .map_err(|e| Kind::Grpc.context(e))?; + + let request = tonic::Request::new(request); + + let response = self + .block_on(client.unreceived_acks(request))? + .map_err(|e| Kind::Grpc.context(e))? + .into_inner(); + + Ok(response.sequences) + } + /// Queries the packet data for all packets with sequences included in the request. /// Note - there is no way to format the query such that it asks for Tx-es with either /// sequence (the query conditions can only be AND-ed) @@ -588,7 +649,7 @@ impl Chain for CosmosSDKChain { .unwrap() .unwrap(); // todo - let mut events = packet_from_tx_search_response(&request, seq, &response)? + let mut events = packet_from_tx_search_response(&request, *seq, &response)? .map_or(vec![], |v| vec![v]); result.append(&mut events); } @@ -596,14 +657,14 @@ impl Chain for CosmosSDKChain { } } -fn packet_query(request: &QueryPacketEventDataRequest, seq: &u64) -> Result { +fn packet_query(request: &QueryPacketEventDataRequest, seq: &Sequence) -> Result { Ok(tendermint_rpc::query::Query::eq( format!("{}.packet_src_channel", request.event_id.as_str()), - request.channel_id.clone(), + request.source_channel_id.to_string(), ) .and_eq( format!("{}.packet_src_port", request.event_id.as_str()), - request.port_id.clone(), + request.source_port_id.to_string(), ) .and_eq( format!("{}.packet_sequence", request.event_id.as_str()), @@ -617,83 +678,44 @@ fn packet_query(request: &QueryPacketEventDataRequest, seq: &u64) -> Result Result, Error> { for r in response.txs.iter() { let height = r.height; - if height.value() > request.height.version_height { + if height.value() > request.height.revision_height { continue; } - let mut envelope = PacketEnvelope { - height: r.height, - packet_src_port: Default::default(), - packet_src_channel: Default::default(), - packet_dst_port: Default::default(), - packet_dst_channel: Default::default(), - packet_sequence: 0, - packet_timeout_height: Default::default(), - packet_timeout_stamp: 0, // todo - decoding - }; + for e in r.clone().tx_result.events.iter() { if e.type_str != request.event_id.as_str() { continue; } - for a in e.attributes.iter() { - let key = String::from_utf8(base64::decode(a.key.to_string().as_bytes()).unwrap()) - .unwrap(); - let value = - String::from_utf8(base64::decode(a.value.to_string().as_bytes()).unwrap()) - .unwrap(); - match key.as_str() { - "packet_src_port" => envelope.packet_src_port = value.parse().unwrap(), - "packet_src_channel" => envelope.packet_src_channel = value.parse().unwrap(), - "packet_dst_port" => envelope.packet_dst_port = value.parse().unwrap(), - "packet_dst_channel" => envelope.packet_dst_channel = value.parse().unwrap(), - "packet_sequence" => envelope.packet_sequence = value.parse::().unwrap(), - "packet_timeout_height" => { - let to: Vec<&str> = value.split('-').collect(); - envelope.packet_timeout_height = ibc_proto::ibc::core::client::v1::Height { - version_number: to[0].parse::().unwrap(), - version_height: to[1].parse::().unwrap(), - } - .try_into() - .unwrap(); - } - _ => {} - }; + + let res = from_tx_response_event(e.clone()); + if res.is_none() { + continue; + } + let event = res.unwrap(); + let packet = match event.clone() { + IBCEvent::SendPacketChannel(send_ev) => Some(send_ev.packet), + IBCEvent::WriteAcknowledgementChannel(ack_ev) => Some(ack_ev.packet), + _ => None, + }; + + if packet.is_none() { + continue; } - if envelope.packet_src_port.as_str() != request.port_id.as_str() - || envelope.packet_src_channel.as_str() != request.channel_id.as_str() - || envelope.packet_sequence != *seq + let packet = packet.unwrap(); + if packet.source_port != request.source_port_id + || packet.source_channel != request.source_channel_id + || packet.sequence != seq { continue; } - match request.event_id { - IBCEventType::SendPacket => { - let mut data = vec![]; - for a in e.attributes.iter() { - let key = String::from_utf8( - base64::decode(a.key.to_string().as_bytes()).unwrap(), - ) - .unwrap(); - let value = String::from_utf8( - base64::decode(a.value.to_string().as_bytes()).unwrap(), - ) - .unwrap(); - match key.as_str() { - "packet_data" => data = Vec::from(value.as_bytes()), - _ => continue, - }; - } - return Ok(Some(IBCEvent::SendPacketChannel(SendPacket { - envelope, - data, - }))); - } - _ => continue, - } + + return Ok(Some(event)); } } Ok(None) @@ -724,26 +746,17 @@ async fn abci_query( // Fail with response log. return Err(Kind::Rpc.context(response.log.to_string()).into()); } - if response.value.is_empty() { - // Fail due to empty response value (nothing to decode). - return Err(Kind::EmptyResponseValue.into()); - } + if prove && response.proof.is_none() { // Fail due to empty proof return Err(Kind::EmptyResponseProof.into()); } - let raw_proof_ops = response - .proof - .map(ProofOps::try_from) - .transpose() - .map_err(|e| Kind::MalformedProof.context(e))?; + let raw_proof_ops = response.proof; let response = QueryResponse { value: response.value, - proof: MerkleProof { - proof: raw_proof_ops, - }, + proof: convert_tm_to_ics_merkle_proof(raw_proof_ops).unwrap(), height: response.height, }; @@ -789,3 +802,30 @@ async fn query_account(chain: &CosmosSDKChain, address: String) -> Result Result, anomaly::Error> { + let mut result = vec![]; + + let response: Response = serde_json::from_str(raw_res.as_str()).unwrap(); + + // Verify the return codes from check_tx and deliver_tx + if response.check_tx.code.is_err() { + return Ok(vec![IBCEvent::ChainError(format!( + "check_tx reports error: log={:?}", + response.check_tx.log + ))]); + } + if response.deliver_tx.code.is_err() { + return Ok(vec![IBCEvent::ChainError(format!( + "deliver_tx reports error: log={:?}", + response.deliver_tx.log + ))]); + } + + for event in response.deliver_tx.events { + if let Some(ibc_ev) = from_tx_response_event(event) { + result.append(&mut vec![ibc_ev]) + } + } + Ok(result) +} diff --git a/relayer/src/chain/handle.rs b/relayer/src/chain/handle.rs index f0948a6d35..39e5cdb459 100644 --- a/relayer/src/chain/handle.rs +++ b/relayer/src/chain/handle.rs @@ -1,3 +1,4 @@ +use std::fmt::Debug; use std::sync::Arc; use crossbeam_channel as channel; @@ -5,22 +6,22 @@ use crossbeam_channel as channel; use dyn_clone::DynClone; use ibc_proto::ibc::core::channel::v1::{ - PacketAckCommitment, QueryPacketCommitmentsRequest, QueryUnreceivedPacketsRequest, + PacketState, QueryPacketAcknowledgementsRequest, QueryPacketCommitmentsRequest, + QueryUnreceivedAcksRequest, QueryUnreceivedPacketsRequest, }; +use ibc_proto::ibc::core::commitment::v1::MerkleProof; +use ibc::ics24_host::{identifier::ChainId, identifier::ClientId, Path}; use ibc::{ events::IBCEvent, ics02_client::client_def::{AnyClientState, AnyConsensusState, AnyHeader}, ics03_connection::connection::ConnectionEnd, + ics03_connection::version::Version, ics04_channel::channel::{ChannelEnd, QueryPacketEventDataRequest}, ics24_host::identifier::{ChannelId, ConnectionId, PortId}, proofs::Proofs, }; use ibc::{ics23_commitment::commitment::CommitmentPrefix, Height}; -use ibc::{ - ics23_commitment::merkle::MerkleProof, - ics24_host::{identifier::ChainId, identifier::ClientId, Path}, -}; // FIXME: the handle should not depend on tendermint-specific types use tendermint::account::Id as AccountId; @@ -33,8 +34,8 @@ use crate::{error::Error, event::monitor::EventBatch}; mod prod; +use ibc::ics04_channel::packet::{PacketMsgType, Sequence}; pub use prod::ProdChainHandle; -use std::fmt::Debug; pub type Subscription = channel::Receiver>; @@ -65,23 +66,15 @@ pub enum ChainRequest { SendMsgs { proto_msgs: Vec, - reply_to: ReplyTo>, + reply_to: ReplyTo>, }, - // GetHeader { - // height: Height, - // reply_to: ReplyTo, - // }, GetMinimalSet { from: Height, to: Height, reply_to: ReplyTo>, }, - // Submit { - // transaction: EncodedTransaction, - // reply_to: ReplyTo<()>, - // }, Signer { reply_to: ReplyTo, }, @@ -99,10 +92,6 @@ pub enum ChainRequest { reply_to: ReplyTo, }, - // CreatePacket { - // event: IBCEvent, - // reply_to: ReplyTo, - // }, BuildHeader { trusted_height: Height, target_height: Height, @@ -138,7 +127,7 @@ pub enum ChainRequest { }, QueryCompatibleVersions { - reply_to: ReplyTo>, + reply_to: ReplyTo>, }, QueryConnection { @@ -180,17 +169,18 @@ pub enum ChainRequest { reply_to: ReplyTo, }, - ProvenPacketCommitment { + BuildPacketProofs { + packet_type: PacketMsgType, port_id: PortId, channel_id: ChannelId, - sequence: u64, + sequence: Sequence, height: Height, - reply_to: ReplyTo<(Vec, MerkleProof)>, + reply_to: ReplyTo<(Vec, Proofs)>, }, QueryPacketCommitments { request: QueryPacketCommitmentsRequest, - reply_to: ReplyTo<(Vec, Height)>, + reply_to: ReplyTo<(Vec, Height)>, }, QueryUnreceivedPackets { @@ -198,6 +188,16 @@ pub enum ChainRequest { reply_to: ReplyTo>, }, + QueryPacketAcknowledgement { + request: QueryPacketAcknowledgementsRequest, + reply_to: ReplyTo<(Vec, Height)>, + }, + + QueryUnreceivedAcknowledgement { + request: QueryUnreceivedAcksRequest, + reply_to: ReplyTo>, + }, + QueryPacketEventData { request: QueryPacketEventDataRequest, reply_to: ReplyTo>, @@ -215,7 +215,7 @@ pub trait ChainHandle: DynClone + Send + Sync + Debug { fn subscribe(&self, chain_id: ChainId) -> Result; /// Send a transaction with `msgs` to chain. - fn send_msgs(&self, proto_msgs: Vec) -> Result, Error>; + fn send_msgs(&self, proto_msgs: Vec) -> Result, Error>; fn get_minimal_set(&self, from: Height, to: Height) -> Result, Error>; @@ -235,7 +235,7 @@ pub trait ChainHandle: DynClone + Send + Sync + Debug { fn query_commitment_prefix(&self) -> Result; - fn query_compatible_versions(&self) -> Result, Error>; + fn query_compatible_versions(&self) -> Result, Error>; fn query_connection( &self, @@ -296,23 +296,34 @@ pub trait ChainHandle: DynClone + Send + Sync + Debug { height: Height, ) -> Result; - fn proven_packet_commitment( + fn build_packet_proofs( &self, + packet_type: PacketMsgType, port_id: &PortId, channel_id: &ChannelId, - sequence: u64, + sequence: Sequence, height: Height, - ) -> Result<(Vec, MerkleProof), Error>; + ) -> Result<(Vec, Proofs), Error>; fn query_packet_commitments( &self, request: QueryPacketCommitmentsRequest, - ) -> Result<(Vec, Height), Error>; + ) -> Result<(Vec, Height), Error>; fn query_unreceived_packets( &self, request: QueryUnreceivedPacketsRequest, ) -> Result, Error>; + fn query_packet_acknowledgements( + &self, + request: QueryPacketAcknowledgementsRequest, + ) -> Result<(Vec, Height), Error>; + + fn query_unreceived_acknowledgement( + &self, + request: QueryUnreceivedAcksRequest, + ) -> Result, Error>; + fn query_txs(&self, request: QueryPacketEventDataRequest) -> Result, Error>; } diff --git a/relayer/src/chain/handle/prod.rs b/relayer/src/chain/handle/prod.rs index 0f96536116..f1f120d260 100644 --- a/relayer/src/chain/handle/prod.rs +++ b/relayer/src/chain/handle/prod.rs @@ -3,22 +3,24 @@ use std::fmt::Debug; use crossbeam_channel as channel; use ibc_proto::ibc::core::channel::v1::{ - PacketAckCommitment, QueryPacketCommitmentsRequest, QueryUnreceivedPacketsRequest, + PacketState, QueryPacketAcknowledgementsRequest, QueryPacketCommitmentsRequest, + QueryUnreceivedAcksRequest, QueryUnreceivedPacketsRequest, }; use ibc::{ events::IBCEvent, ics02_client::client_def::{AnyClientState, AnyConsensusState, AnyHeader}, ics03_connection::connection::ConnectionEnd, + ics03_connection::version::Version, ics04_channel::channel::{ChannelEnd, QueryPacketEventDataRequest}, ics23_commitment::commitment::CommitmentPrefix, - ics23_commitment::merkle::MerkleProof, ics24_host::identifier::ChainId, ics24_host::identifier::ChannelId, ics24_host::identifier::{ClientId, ConnectionId, PortId}, proofs::Proofs, Height, }; +use ibc_proto::ibc::core::commitment::v1::MerkleProof; // FIXME: the handle should not depend on tendermint-specific types use tendermint::account::Id as AccountId; @@ -31,6 +33,7 @@ use crate::{ error::{Error, Kind}, keyring::store::KeyEntry, }; +use ibc::ics04_channel::packet::{PacketMsgType, Sequence}; #[derive(Debug, Clone)] pub struct ProdChainHandle { @@ -88,17 +91,13 @@ impl ChainHandle for ProdChainHandle { self.send(|reply_to| ChainRequest::Subscribe { reply_to }) } - fn send_msgs(&self, proto_msgs: Vec) -> Result, Error> { + fn send_msgs(&self, proto_msgs: Vec) -> Result, Error> { self.send(|reply_to| ChainRequest::SendMsgs { proto_msgs, reply_to, }) } - // fn get_header(&self, height: Height) -> Result { - // self.send(|reply_to| HandleInput::GetHeader { height, reply_to }) - // } - fn get_minimal_set(&self, from: Height, to: Height) -> Result, Error> { self.send(|reply_to| ChainRequest::GetMinimalSet { from, to, reply_to }) } @@ -118,17 +117,6 @@ impl ChainHandle for ProdChainHandle { }) } - // fn submit(&self, transaction: EncodedTransaction) -> Result<(), Error> { - // self.send(|reply_to| HandleInput::Submit { - // transaction, - // reply_to, - // }) - // } - - // fn create_packet(&self, event: IBCEvent) -> Result { - // self.send(|reply_to| HandleInput::CreatePacket { event, reply_to }) - // } - fn query_latest_height(&self) -> Result { self.send(|reply_to| ChainRequest::QueryLatestHeight { reply_to }) } @@ -145,18 +133,11 @@ impl ChainHandle for ProdChainHandle { }) } - // fn query_channel( - // &self, - // port_id: &PortId, - // channel_id: &ChannelId, - // height: ICSHeight, - // ) -> Result; - fn query_commitment_prefix(&self) -> Result { self.send(|reply_to| ChainRequest::QueryCommitmentPrefix { reply_to }) } - fn query_compatible_versions(&self) -> Result, Error> { + fn query_compatible_versions(&self) -> Result, Error> { self.send(|reply_to| ChainRequest::QueryCompatibleVersions { reply_to }) } @@ -276,14 +257,16 @@ impl ChainHandle for ProdChainHandle { }) } - fn proven_packet_commitment( + fn build_packet_proofs( &self, + packet_type: PacketMsgType, port_id: &PortId, channel_id: &ChannelId, - sequence: u64, + sequence: Sequence, height: Height, - ) -> Result<(Vec, MerkleProof), Error> { - self.send(|reply_to| ChainRequest::ProvenPacketCommitment { + ) -> Result<(Vec, Proofs), Error> { + self.send(|reply_to| ChainRequest::BuildPacketProofs { + packet_type, port_id: port_id.clone(), channel_id: channel_id.clone(), sequence, @@ -295,7 +278,7 @@ impl ChainHandle for ProdChainHandle { fn query_packet_commitments( &self, request: QueryPacketCommitmentsRequest, - ) -> Result<(Vec, Height), Error> { + ) -> Result<(Vec, Height), Error> { self.send(|reply_to| ChainRequest::QueryPacketCommitments { request, reply_to }) } @@ -306,6 +289,20 @@ impl ChainHandle for ProdChainHandle { self.send(|reply_to| ChainRequest::QueryUnreceivedPackets { request, reply_to }) } + fn query_packet_acknowledgements( + &self, + request: QueryPacketAcknowledgementsRequest, + ) -> Result<(Vec, Height), Error> { + self.send(|reply_to| ChainRequest::QueryPacketAcknowledgement { request, reply_to }) + } + + fn query_unreceived_acknowledgement( + &self, + request: QueryUnreceivedAcksRequest, + ) -> Result, Error> { + self.send(|reply_to| ChainRequest::QueryUnreceivedAcknowledgement { request, reply_to }) + } + fn query_txs(&self, request: QueryPacketEventDataRequest) -> Result, Error> { self.send(|reply_to| ChainRequest::QueryPacketEventData { request, reply_to }) } diff --git a/relayer/src/chain/mock.rs b/relayer/src/chain/mock.rs index 3f4ee30bdc..00bb753feb 100644 --- a/relayer/src/chain/mock.rs +++ b/relayer/src/chain/mock.rs @@ -11,8 +11,10 @@ use tendermint::account::Id; use tendermint_testgen::light_block::TMLightBlock; use ibc_proto::ibc::core::channel::v1::{ - PacketAckCommitment, QueryPacketCommitmentsRequest, QueryUnreceivedPacketsRequest, + PacketState, QueryPacketAcknowledgementsRequest, QueryPacketCommitmentsRequest, + QueryUnreceivedAcksRequest, QueryUnreceivedPacketsRequest, }; +use ibc_proto::ibc::core::commitment::v1::MerkleProof; use ibc::downcast; use ibc::events::IBCEvent; @@ -23,12 +25,11 @@ use ibc::ics07_tendermint::consensus_state::ConsensusState as TendermintConsensu use ibc::ics07_tendermint::header::Header as TendermintHeader; use ibc::ics18_relayer::context::ICS18Context; use ibc::ics23_commitment::commitment::CommitmentPrefix; -use ibc::ics23_commitment::merkle::MerkleProof; use ibc::ics24_host::identifier::{ChainId, ClientId}; use ibc::ics24_host::Path; use ibc::mock::context::MockContext; use ibc::mock::host::HostType; -use ibc::test_utils::{default_consensus_params, get_dummy_account_id}; +use ibc::test_utils::get_dummy_account_id; use ibc::Height; use crate::chain::{Chain, QueryResponse}; @@ -100,13 +101,14 @@ impl Chain for MockChain { unimplemented!() } - fn send_msgs(&mut self, proto_msgs: Vec) -> Result, Error> { + fn send_msgs(&mut self, proto_msgs: Vec) -> Result, Error> { // Use the ICS18Context interface to submit the set of messages. - Ok(vec![self - .context + self.context .send(proto_msgs) - .map(|_| "OK".to_string()) // TODO: establish success return codes. - .map_err(|e| Kind::Rpc.context(e))?]) + .map_err(|e| Kind::Rpc.context(e))?; + + // TODO FIX tests with this + Ok(vec![]) } fn get_signer(&mut self) -> Result { @@ -126,8 +128,7 @@ impl Chain for MockChain { Duration::from_millis(3000), height, Height::zero(), - default_consensus_params(), - "upgrade/upgradedClient".to_string(), + vec!["upgrade/upgradedClient".to_string()], false, false, ) @@ -202,7 +203,7 @@ impl Chain for MockChain { fn query_packet_commitments( &self, _request: QueryPacketCommitmentsRequest, - ) -> Result<(Vec, Height), Error> { + ) -> Result<(Vec, Height), Error> { unimplemented!() } @@ -213,6 +214,20 @@ impl Chain for MockChain { unimplemented!() } + fn query_packet_acknowledgements( + &self, + _request: QueryPacketAcknowledgementsRequest, + ) -> Result<(Vec, Height), Error> { + unimplemented!() + } + + fn query_unreceived_acknowledgements( + &self, + _request: QueryUnreceivedAcksRequest, + ) -> Result, Error> { + unimplemented!() + } + fn query_txs(&self, _request: QueryPacketEventDataRequest) -> Result, Error> { unimplemented!() } @@ -237,7 +252,6 @@ pub mod test_utils { account_prefix: "".to_string(), key_name: "".to_string(), store_prefix: "".to_string(), - client_ids: vec![], gas: None, max_msg_num: None, max_tx_size: None, diff --git a/relayer/src/chain/runtime.rs b/relayer/src/chain/runtime.rs index a66da54352..fc894df4f3 100644 --- a/relayer/src/chain/runtime.rs +++ b/relayer/src/chain/runtime.rs @@ -8,8 +8,10 @@ use crossbeam_channel as channel; use tokio::runtime::Runtime as TokioRuntime; use ibc_proto::ibc::core::channel::v1::{ - PacketAckCommitment, QueryPacketCommitmentsRequest, QueryUnreceivedPacketsRequest, + PacketState, QueryPacketAcknowledgementsRequest, QueryPacketCommitmentsRequest, + QueryUnreceivedAcksRequest, QueryUnreceivedPacketsRequest, }; +use ibc_proto::ibc::core::commitment::v1::MerkleProof; use ibc::{ events::IBCEvent, @@ -19,8 +21,9 @@ use ibc::{ state::{ClientState, ConsensusState}, }, ics03_connection::connection::ConnectionEnd, + ics03_connection::version::Version, ics04_channel::channel::{ChannelEnd, QueryPacketEventDataRequest}, - ics23_commitment::{commitment::CommitmentPrefix, merkle::MerkleProof}, + ics23_commitment::commitment::CommitmentPrefix, ics24_host::identifier::ChannelId, ics24_host::identifier::PortId, ics24_host::identifier::{ClientId, ConnectionId}, @@ -32,11 +35,6 @@ use ibc::{ // FIXME: the handle should not depend on tendermint-specific types use tendermint::account::Id as AccountId; -use super::{ - handle::{ChainHandle, ChainRequest, ProdChainHandle, ReplyTo, Subscription}, - Chain, QueryResponse, -}; - use crate::{ config::ChainConfig, connection::ConnectionMsgType, @@ -46,6 +44,12 @@ use crate::{ light_client::LightClient, }; +use super::{ + handle::{ChainHandle, ChainRequest, ProdChainHandle, ReplyTo, Subscription}, + Chain, QueryResponse, +}; +use ibc::ics04_channel::packet::{PacketMsgType, Sequence}; + pub struct Threads { pub light_client: Option>, pub chain_runtime: thread::JoinHandle<()>, @@ -185,18 +189,6 @@ impl ChainRuntime { self.get_minimal_set(from, to, reply_to)? } - // Ok(ChainRequest::GetHeader { height, reply_to }) => { - // self.get_header(height, reply_to)? - // } - // - // Ok(ChainRequest::Submit { transaction, reply_to, }) => { - // self.submit(transaction, reply_to)? - // }, - // - // Ok(ChainRequest::CreatePacket { event, reply_to }) => { - // self.create_packet(event, reply_to)? - // } - Ok(ChainRequest::Signer { reply_to }) => { self.get_signer(reply_to)? } @@ -212,15 +204,19 @@ impl ChainRuntime { Ok(ChainRequest::BuildHeader { trusted_height, target_height, reply_to }) => { self.build_header(trusted_height, target_height, reply_to)? } + Ok(ChainRequest::BuildClientState { height, reply_to }) => { self.build_client_state(height, reply_to)? } + Ok(ChainRequest::BuildConsensusState { height, reply_to }) => { self.build_consensus_state(height, reply_to)? } + Ok(ChainRequest::BuildConnectionProofsAndClientState { message_type, connection_id, client_id, height, reply_to }) => { self.build_connection_proofs_and_client_state(message_type, connection_id, client_id, height, reply_to)? }, + Ok(ChainRequest::BuildChannelProofs { port_id, channel_id, height, reply_to }) => { self.build_channel_proofs(port_id, channel_id, height, reply_to)? }, @@ -228,6 +224,7 @@ impl ChainRuntime { Ok(ChainRequest::QueryLatestHeight { reply_to }) => { self.query_latest_height(reply_to)? } + Ok(ChainRequest::QueryClientState { client_id, height, reply_to }) => { self.query_client_state(client_id, height, reply_to)? }, @@ -260,8 +257,8 @@ impl ChainRuntime { self.proven_client_consensus(client_id, consensus_height, height, reply_to)? }, - Ok(ChainRequest::ProvenPacketCommitment { port_id, channel_id, sequence, height, reply_to }) => { - self.proven_packet_commitment(port_id, channel_id, sequence, height, reply_to)? + Ok(ChainRequest::BuildPacketProofs { packet_type, port_id, channel_id, sequence, height, reply_to }) => { + self.build_packet_proofs(packet_type, port_id, channel_id, sequence, height, reply_to)? }, Ok(ChainRequest::QueryPacketCommitments { request, reply_to }) => { @@ -272,6 +269,14 @@ impl ChainRuntime { self.query_unreceived_packets(request, reply_to)? }, + Ok(ChainRequest::QueryPacketAcknowledgement { request, reply_to }) => { + self.query_packet_acknowledgements(request, reply_to)? + }, + + Ok(ChainRequest::QueryUnreceivedAcknowledgement { request, reply_to }) => { + self.query_unreceived_acknowledgement(request, reply_to)? + }, + Ok(ChainRequest::QueryPacketEventData { request, reply_to }) => { self.query_txs(request, reply_to)? }, @@ -327,7 +332,7 @@ impl ChainRuntime { fn send_msgs( &mut self, proto_msgs: Vec, - reply_to: ReplyTo>, + reply_to: ReplyTo>, ) -> Result<(), Error> { let result = self.chain.send_msgs(proto_msgs); @@ -348,17 +353,6 @@ impl ChainRuntime { Ok(()) } - // fn get_header(&self, height: Height, reply_to: ReplyTo) -> Result<(), Error> { - // let light_block = self.light_client.verify_to_target(height); - // let header: Result = todo!(); // light_block.map(|lb| lb.signed_header().wrap_any()); - - // reply_to - // .send(header) - // .map_err(|e| Kind::Channel.context(e))?; - - // Ok(()) - // } - fn get_minimal_set( &self, _from: Height, @@ -368,14 +362,6 @@ impl ChainRuntime { todo!() } - // fn submit(&self, transaction: EncodedTransaction, reply_to: ReplyTo<()>) -> Result<(), Error> { - // todo!() - // } - - // fn create_packet(&self, event: IBCEvent, reply_to: ReplyTo) -> Result<(), Error> { - // todo!() - // } - fn get_signer(&mut self, reply_to: ReplyTo) -> Result<(), Error> { let result = self.chain.get_signer(); @@ -527,7 +513,7 @@ impl ChainRuntime { Ok(()) } - fn query_compatible_versions(&self, reply_to: ReplyTo>) -> Result<(), Error> { + fn query_compatible_versions(&self, reply_to: ReplyTo>) -> Result<(), Error> { let versions = self.chain.query_compatible_versions(); reply_to @@ -638,17 +624,18 @@ impl ChainRuntime { Ok(()) } - fn proven_packet_commitment( + fn build_packet_proofs( &self, + packet_type: PacketMsgType, port_id: PortId, channel_id: ChannelId, - sequence: u64, + sequence: Sequence, height: Height, - reply_to: ReplyTo<(Vec, MerkleProof)>, + reply_to: ReplyTo<(Vec, Proofs)>, ) -> Result<(), Error> { - let result = self - .chain - .proven_packet_commitment(&port_id, &channel_id, sequence, height); + let result = + self.chain + .build_packet_proofs(packet_type, &port_id, &channel_id, sequence, height); reply_to .send(result) @@ -660,7 +647,7 @@ impl ChainRuntime { fn query_packet_commitments( &self, request: QueryPacketCommitmentsRequest, - reply_to: ReplyTo<(Vec, Height)>, + reply_to: ReplyTo<(Vec, Height)>, ) -> Result<(), Error> { let result = self.chain.query_packet_commitments(request); @@ -685,6 +672,34 @@ impl ChainRuntime { Ok(()) } + fn query_packet_acknowledgements( + &self, + request: QueryPacketAcknowledgementsRequest, + reply_to: ReplyTo<(Vec, Height)>, + ) -> Result<(), Error> { + let result = self.chain.query_packet_acknowledgements(request); + + reply_to + .send(result) + .map_err(|e| Kind::Channel.context(e))?; + + Ok(()) + } + + fn query_unreceived_acknowledgement( + &self, + request: QueryUnreceivedAcksRequest, + reply_to: ReplyTo>, + ) -> Result<(), Error> { + let result = self.chain.query_unreceived_acknowledgements(request); + + reply_to + .send(result) + .map_err(|e| Kind::Channel.context(e))?; + + Ok(()) + } + fn query_txs( &self, request: QueryPacketEventDataRequest, diff --git a/relayer/src/channel.rs b/relayer/src/channel.rs index 4d59dfcdb9..e1a2a3cb95 100644 --- a/relayer/src/channel.rs +++ b/relayer/src/channel.rs @@ -1,15 +1,15 @@ -use std::str::FromStr; use std::time::SystemTime; use prost_types::Any; use thiserror::Error; -use tracing::info; +use tracing::{debug, error, info}; use ibc_proto::ibc::core::channel::v1::MsgChannelOpenAck as RawMsgChannelOpenAck; use ibc_proto::ibc::core::channel::v1::MsgChannelOpenConfirm as RawMsgChannelOpenConfirm; use ibc_proto::ibc::core::channel::v1::MsgChannelOpenInit as RawMsgChannelOpenInit; use ibc_proto::ibc::core::channel::v1::MsgChannelOpenTry as RawMsgChannelOpenTry; +use ibc::events::IBCEvent; use ibc::ics04_channel::channel::{ChannelEnd, Counterparty, Order, State}; use ibc::ics04_channel::msgs::chan_open_ack::MsgChannelOpenAck; use ibc::ics04_channel::msgs::chan_open_confirm::MsgChannelOpenConfirm; @@ -77,6 +77,14 @@ impl ChannelConfigSide { pub fn channel_id(&self) -> &ChannelId { &self.channel_id } + + pub fn set_client_id(&mut self, id: &ClientId) { + self.client_id = id.clone() + } + + pub fn set_connection_id(&mut self, id: &ConnectionId) { + self.connection_id = id.clone() + } } #[derive(Clone, Debug)] @@ -122,37 +130,18 @@ impl ChannelConfig { pub fn new(conn: &ConnectionConfig, path: &RelayPath) -> Result { let a_config = ChannelConfigSide { chain_id: conn.a_end().chain_id().clone(), - connection_id: conn.a_end().connection_id().clone(), - client_id: conn.a_end().client_id().clone(), - port_id: PortId::from_str(path.a_port.clone().ok_or("Port id not specified")?.as_str()) - .map_err(|e| format!("Invalid port id ({:?})", e))?, - channel_id: ChannelId::from_str( - path.a_channel - .clone() - .ok_or("Channel id not specified")? - .as_str(), - ) - .map_err(|e| format!("Invalid channel id ({:?})", e))?, + connection_id: ConnectionId::default(), + client_id: ClientId::default(), + port_id: path.a_port.clone(), + channel_id: ChannelId::default(), }; let b_config = ChannelConfigSide { chain_id: conn.b_end().chain_id().clone(), - connection_id: conn.b_end().connection_id().clone(), - client_id: conn.b_end().client_id().clone(), - port_id: PortId::from_str( - path.b_port - .clone() - .ok_or("Counterparty port id not specified")? - .as_str(), - ) - .map_err(|e| format!("Invalid counterparty port id ({:?})", e))?, - channel_id: ChannelId::from_str( - path.b_channel - .clone() - .ok_or("Counterparty channel id not specified")? - .as_str(), - ) - .map_err(|e| format!("Invalid counterparty channel id ({:?})", e))?, + connection_id: ConnectionId::default(), + client_id: ClientId::default(), + port_id: path.b_port.clone(), + channel_id: ChannelId::default(), }; Ok(ChannelConfig { @@ -163,31 +152,24 @@ impl ChannelConfig { } } -// temp fix for queries -fn get_channel( - chain: Box, - port: &PortId, - id: &ChannelId, -) -> Result, ChannelError> { - match chain.query_channel(port, id, Height::zero()) { - Err(e) => match e.kind() { - Kind::EmptyResponseValue => Ok(None), - _ => Err(ChannelError::Failed(format!( - "error retrieving channel {:?}", - e - ))), - }, - Ok(chan) => Ok(Some(chan)), - } -} - impl Channel { /// Creates a new channel on top of the existing connection. If the channel is not already /// set-up on both sides of the connection, this functions also fulfils the channel handshake. - pub fn new(connection: Connection, config: ChannelConfig) -> Result { - let channel = Channel { config, connection }; + pub fn new(connection: Connection, mut config: ChannelConfig) -> Result { + config + .a_config + .set_client_id(connection.config.a_config.client_id()); + config + .b_config + .set_client_id(connection.config.b_config.client_id()); + config + .a_config + .set_connection_id(connection.config.a_config.connection_id()); + config + .b_config + .set_connection_id(connection.config.b_config.connection_id()); + let mut channel = Channel { config, connection }; channel.handshake()?; - Ok(channel) } @@ -197,134 +179,115 @@ impl Channel { } /// Executes the channel handshake protocol (ICS004) - fn handshake(&self) -> Result<(), ChannelError> { + fn handshake(&mut self) -> Result<(), ChannelError> { let done = '\u{1F973}'; - let flipped = self.config.flipped(); + let a_chain = self.connection.chain_a(); + let b_chain = self.connection.chain_b(); + + let mut flipped = self.config.flipped(); + // Try chanOpenInit on a_chain + let now = SystemTime::now(); let mut counter = 0; + while counter < MAX_ITER { + counter += 1; + match build_chan_init_and_send(a_chain.clone(), b_chain.clone(), &flipped) { + Err(e) => { + error!("Failed ChanInit {:?}: {}", self.config.a_end(), e); + continue; + } + Ok(result) => { + self.config.a_config.channel_id = extract_channel_id(&result)?.clone(); + info!("{} {} => {:?}\n", done, a_chain.id(), result); + break; + } + } + } + debug!("elapsed time {:?}", now.elapsed().unwrap().as_secs()); + let now = SystemTime::now(); - let a_chain = self.connection.chain_a(); - let b_chain = self.connection.chain_b(); + // Try chanOpenTry on b_chain + counter = 0; + while counter < MAX_ITER { + counter += 1; + match build_chan_try_and_send(b_chain.clone(), a_chain.clone(), &self.config) { + Err(e) => { + error!("Failed ChanTry {:?}: {}", self.config.b_end(), e); + continue; + } + Ok(result) => { + self.config.b_config.channel_id = extract_channel_id(&result)?.clone(); + info!("{} {} => {:?}\n", done, b_chain.id(), result); + break; + } + } + } + debug!("elapsed time {:?}", now.elapsed().unwrap().as_secs()); + flipped = self.config.flipped(); + counter = 0; while counter < MAX_ITER { counter += 1; let now = SystemTime::now(); // Continue loop if query error - let a_channel = get_channel( - a_chain.clone(), + let a_channel = a_chain.query_channel( &self.config.a_end().port_id, &self.config.a_end().channel_id, + Height::zero(), ); if a_channel.is_err() { continue; } - let b_channel = get_channel( - b_chain.clone(), + let b_channel = b_chain.query_channel( &self.config.b_end().port_id, &self.config.b_end().channel_id, + Height::zero(), ); if b_channel.is_err() { continue; } - match (a_channel?, b_channel?) { - (None, None) => { - // Init to src - match build_chan_init_and_send(a_chain.clone(), b_chain.clone(), &flipped) { - Err(e) => info!("{:?} Failed ChanInit {:?}", e, self.config.a_end()), - Ok(_) => info!("{} ChanInit {:?}", done, self.config.a_end()), + match ( + a_channel.unwrap().state().clone(), + b_channel.unwrap().state().clone(), + ) { + (State::Init, State::TryOpen) | (State::TryOpen, State::TryOpen) => { + // Ack to src + match build_chan_ack_and_send(a_chain.clone(), b_chain.clone(), &flipped) { + Err(e) => error!("Failed ChanAck {:?}: {}", self.config.a_end(), e), + Ok(event) => info!("{} {} => {:?}\n", done, a_chain.id(), event), } } - (Some(a_channel), None) => { - // Try to dest - assert!(a_channel.state_matches(&State::Init)); - match build_chan_try_and_send(b_chain.clone(), a_chain.clone(), &self.config) { - Err(e) => info!("{:?} Failed ChanTry {:?}", e, self.config.b_end()), - Ok(_) => info!("{} ChanTry {:?}", done, self.config.b_end()), + (State::Open, State::TryOpen) => { + // Confirm to dest + match build_chan_confirm_and_send( + b_chain.clone(), + a_chain.clone(), + &self.config, + ) { + Err(e) => error!("Failed ChanConfirm {:?}: {}", self.config.b_end(), e), + Ok(event) => info!("{} {} => {:?}\n", done, b_chain.id(), event), } } - (None, Some(b_channel)) => { - // Try to src - assert!(b_channel.state_matches(&State::Init)); - match build_chan_try_and_send(a_chain.clone(), b_chain.clone(), &flipped) { - Err(e) => info!("{:?} Failed ChanTry {:?}", e, self.config.a_end()), - Ok(_) => info!("{} ChanTry {:?}", done, self.config.a_end()), + (State::TryOpen, State::Open) => { + // Confirm to src + match build_chan_confirm_and_send(a_chain.clone(), b_chain.clone(), &flipped) { + Err(e) => error!("Failed ChanConfirm {:?}: {}", self.config.a_end(), e), + Ok(event) => info!("{} {} => {:?}\n", done, a_chain.id(), event), } } - (Some(a_channel), Some(b_channel)) => { - match (a_channel.state(), b_channel.state()) { - (&State::Init, &State::Init) => { - // Try to dest - // Try to dest - match build_chan_try_and_send( - b_chain.clone(), - a_chain.clone(), - &self.config, - ) { - Err(e) => info!("{:?} Failed ChanTry {:?}", e, self.config.b_end()), - Ok(_) => info!("{} ChanTry {:?}", done, self.config.b_end()), - } - } - (&State::TryOpen, &State::Init) => { - // Ack to dest - match build_chan_ack_and_send( - b_chain.clone(), - a_chain.clone(), - &self.config, - ) { - Err(e) => info!("{:?} Failed ChanAck {:?}", e, self.config.b_end()), - Ok(_) => info!("{} ChanAck {:?}", done, self.config.b_end()), - } - } - (&State::Init, &State::TryOpen) | (&State::TryOpen, &State::TryOpen) => { - // Ack to src - match build_chan_ack_and_send( - a_chain.clone(), - b_chain.clone(), - &flipped, - ) { - Err(e) => info!("{:?} Failed ChanAck {:?}", e, self.config.a_end()), - Ok(_) => info!("{} ChanAck {:?}", done, self.config.a_end()), - } - } - (&State::Open, &State::TryOpen) => { - // Confirm to dest - match build_chan_confirm_and_send( - b_chain.clone(), - a_chain.clone(), - &self.config, - ) { - Err(e) => { - info!("{:?} Failed ChanConfirm {:?}", e, self.config.b_end()) - } - Ok(_) => info!("{} ChanConfirm {:?}", done, self.config.b_end()), - } - } - (&State::TryOpen, &State::Open) => { - // Confirm to src - match build_chan_confirm_and_send( - a_chain.clone(), - b_chain.clone(), - &flipped, - ) { - Err(e) => info!("{:?} ChanConfirm {:?}", e, flipped), - Ok(_) => info!("{} ChanConfirm {:?}", done, flipped), - } - } - (&State::Open, &State::Open) => { - info!( - "{} {} {} Channel handshake finished for {:#?}", - done, done, done, self.config - ); - return Ok(()); - } - _ => {} // TODO channel close - } + (State::Open, State::Open) => { + info!( + "{} {} {} Channel handshake finished for {:#?}\n", + done, done, done, self.config + ); + return Ok(()); } + _ => {} // TODO channel close } - info!("elapsed time {:?}\n", now.elapsed().unwrap().as_secs()); + debug!("elapsed time {:?}\n", now.elapsed().unwrap().as_secs()); } Err(ChannelError::Failed(format!( @@ -334,6 +297,18 @@ impl Channel { } } +fn extract_channel_id(event: &IBCEvent) -> Result<&ChannelId, ChannelError> { + match event { + IBCEvent::OpenInitChannel(ev) => Ok(ev.channel_id()), + IBCEvent::OpenTryChannel(ev) => Ok(ev.channel_id()), + IBCEvent::OpenAckChannel(ev) => Ok(ev.channel_id()), + IBCEvent::OpenConfirmChannel(ev) => Ok(ev.channel_id()), + _ => Err(ChannelError::Failed( + "cannot extract channel_id from result".to_string(), + )), + } +} + /// Enumeration of proof carrying ICS4 message, helper for relayer. #[derive(Clone, Debug, PartialEq, Eq)] pub enum ChannelMsgType { @@ -347,30 +322,11 @@ pub fn build_chan_init( _src_chain: Box, opts: &ChannelConfig, ) -> Result, Error> { - // Check that the destination chain will accept the message, i.e. it does not have the channel - if dst_chain - .query_channel( - opts.dst().port_id(), - opts.dst().channel_id(), - Height::default(), - ) - .is_ok() - { - return Err(Kind::ChanOpenInit( - opts.dst().channel_id().clone(), - "channel already exist".into(), - ) - .into()); - } - let signer = dst_chain .get_signer() .map_err(|e| Kind::KeyBase.context(e))?; - let counterparty = Counterparty::new( - opts.src().port_id().clone(), - Some(opts.src().channel_id().clone()), - ); + let counterparty = Counterparty::new(opts.src().port_id().clone(), None); let channel = ChannelEnd::new( State::Init, @@ -383,7 +339,6 @@ pub fn build_chan_init( // Build the domain type message let new_msg = MsgChannelOpenInit { port_id: opts.dst().port_id().clone(), - channel_id: opts.dst().channel_id().clone(), channel, signer, }; @@ -395,9 +350,26 @@ pub fn build_chan_init_and_send( dst_chain: Box, src_chain: Box, opts: &ChannelConfig, -) -> Result, Error> { +) -> Result { let dst_msgs = build_chan_init(dst_chain.clone(), src_chain, &opts)?; - Ok(dst_chain.send_msgs(dst_msgs)?) + + let events = dst_chain.send_msgs(dst_msgs)?; + + // Find the relevant event for channel init + let result = events + .iter() + .find(|&event| { + matches!(event, IBCEvent::OpenInitChannel(_)) + || matches!(event, IBCEvent::ChainError(_)) + }) + .cloned() + .ok_or_else(|| Kind::ChanOpenInit("no chan init event was in the response".to_string()))?; + + match result { + IBCEvent::OpenInitChannel(_) => Ok(result), + IBCEvent::ChainError(e) => Err(Kind::ChanOpenInit(e).into()), + _ => panic!("internal error"), + } } fn check_destination_channel_state( @@ -420,7 +392,7 @@ fn check_destination_channel_state( if good_state && good_connection_hops && good_channel_ids { Ok(()) } else { - Err(Kind::ChanOpenTry( + Err(Kind::ChanOpen( channel_id, "channel already exist in an incompatible state".into(), ) @@ -445,12 +417,12 @@ fn validated_expected_channel( // The highest expected state, depends on the message type: let highest_state = match msg_type { - ChannelMsgType::OpenTry => State::Init, ChannelMsgType::OpenAck => State::TryOpen, ChannelMsgType::OpenConfirm => State::TryOpen, + _ => State::Uninitialized, }; - let dest_expected_channel = ChannelEnd::new( + let dst_expected_channel = ChannelEnd::new( highest_state, opts.ordering, counterparty, @@ -459,37 +431,29 @@ fn validated_expected_channel( ); // Retrieve existing channel if any - let dest_channel = dst_chain.query_channel( + let dst_channel = dst_chain.query_channel( &opts.dst().port_id(), &opts.dst().channel_id(), Height::default(), - ); + )?; // Check if a connection is expected to exist on destination chain - if msg_type == ChannelMsgType::OpenTry { - // TODO - check typed Err, or make query_channel return Option - // It is ok if there is no channel for Try Tx - if dest_channel.is_err() { - return Ok(dest_expected_channel); - } - } else { - // A channel must exist on destination chain for Ack and Confirm Tx-es to succeed - if dest_channel.is_err() { - return Err(Kind::ChanOpenTry( - opts.src().channel_id().clone(), - "missing channel on source chain".to_string(), - ) - .into()); - } + // A channel must exist on destination chain for Ack and Confirm Tx-es to succeed + if dst_channel.state_matches(&State::Uninitialized) { + return Err(Kind::ChanOpen( + opts.src().channel_id().clone(), + "missing channel on source chain".to_string(), + ) + .into()); } check_destination_channel_state( opts.dst().channel_id().clone(), - dest_channel?, - dest_expected_channel.clone(), + dst_channel, + dst_expected_channel.clone(), )?; - Ok(dest_expected_channel) + Ok(dst_expected_channel) } pub fn build_chan_try( @@ -497,37 +461,16 @@ pub fn build_chan_try( src_chain: Box, opts: &ChannelConfig, ) -> Result, Error> { - // Check that the destination chain will accept the message, i.e. it does not have the channel - let _dest_expected_channel = validated_expected_channel( - dst_chain.clone(), - src_chain.clone(), - ChannelMsgType::OpenTry, - opts, - ) - .map_err(|e| { - Kind::ChanOpenTry( - opts.src().channel_id().clone(), - "try options inconsistent with existing channel on destination chain".to_string(), - ) - .context(e) - })?; - let src_channel = src_chain .query_channel( &opts.src().port_id(), &opts.src().channel_id(), Height::default(), ) - .map_err(|e| { - Kind::ChanOpenTry( - opts.dst().channel_id().clone(), - "channel does not exist on source".into(), - ) - .context(e) - })?; + .map_err(|e| Kind::ChanOpenTry("channel does not exist on source".into()).context(e))?; // Retrieve the connection - let dest_connection = + let dst_connection = dst_chain.query_connection(&opts.dst().connection_id().clone(), Height::default())?; let ics_target_height = src_chain.query_latest_height()?; @@ -536,7 +479,7 @@ pub fn build_chan_try( let mut msgs = build_update_client( dst_chain.clone(), src_chain.clone(), - &dest_connection.client_id(), + &dst_connection.client_id(), ics_target_height, )?; @@ -546,7 +489,7 @@ pub fn build_chan_try( ); let channel = ChannelEnd::new( - State::Init, + State::TryOpen, opts.ordering, counterparty, vec![opts.dst().connection_id().clone()], @@ -561,8 +504,7 @@ pub fn build_chan_try( // Build the domain type message let new_msg = MsgChannelOpenTry { port_id: opts.dst().port_id().clone(), - channel_id: opts.dst().channel_id().clone(), - counterparty_chosen_channel_id: src_channel.counterparty().channel_id, + previous_channel_id: src_channel.counterparty().channel_id, channel, counterparty_version: src_chain.module_version(&opts.src().port_id())?, proofs: src_chain.build_channel_proofs( @@ -584,9 +526,21 @@ pub fn build_chan_try_and_send( dst_chain: Box, src_chain: Box, opts: &ChannelConfig, -) -> Result, Error> { +) -> Result { let dst_msgs = build_chan_try(dst_chain.clone(), src_chain, &opts)?; - Ok(dst_chain.send_msgs(dst_msgs)?) + + let events = dst_chain.send_msgs(dst_msgs)?; + + // Find the relevant event for channel try + events + .iter() + .find(|&event| { + matches!(event, IBCEvent::OpenTryChannel(_)) || matches!(event, IBCEvent::ChainError(_)) + }) + .cloned() + .ok_or_else(|| { + Kind::ChanOpenTry("no chan try event was in the response".to_string()).into() + }) } pub fn build_chan_ack( @@ -595,7 +549,7 @@ pub fn build_chan_ack( opts: &ChannelConfig, ) -> Result, Error> { // Check that the destination chain will accept the message - let _dest_expected_channel = validated_expected_channel( + let _dst_expected_channel = validated_expected_channel( dst_chain.clone(), src_chain.clone(), ChannelMsgType::OpenAck, @@ -624,7 +578,7 @@ pub fn build_chan_ack( })?; // Retrieve the connection - let dest_connection = + let dst_connection = dst_chain.query_connection(&opts.dst().connection_id().clone(), Height::default())?; let ics_target_height = src_chain.query_latest_height()?; @@ -633,7 +587,7 @@ pub fn build_chan_ack( let mut msgs = build_update_client( dst_chain.clone(), src_chain.clone(), - &dest_connection.client_id(), + &dst_connection.client_id(), ics_target_height, )?; @@ -667,9 +621,25 @@ pub fn build_chan_ack_and_send( dst_chain: Box, src_chain: Box, opts: &ChannelConfig, -) -> Result, Error> { +) -> Result { let dst_msgs = build_chan_ack(dst_chain.clone(), src_chain, &opts)?; - Ok(dst_chain.send_msgs(dst_msgs)?) + + let events = dst_chain.send_msgs(dst_msgs)?; + + // Find the relevant event for channel ack + events + .iter() + .find(|&event| { + matches!(event, IBCEvent::OpenAckChannel(_)) || matches!(event, IBCEvent::ChainError(_)) + }) + .cloned() + .ok_or_else(|| { + Kind::ChanOpenAck( + opts.dst().channel_id().clone(), + "no chan ack event was in the response".to_string(), + ) + .into() + }) } pub fn build_chan_confirm( @@ -678,7 +648,7 @@ pub fn build_chan_confirm( opts: &ChannelConfig, ) -> Result, Error> { // Check that the destination chain will accept the message - let _dest_expected_channel = validated_expected_channel( + let _dst_expected_channel = validated_expected_channel( dst_chain.clone(), src_chain.clone(), ChannelMsgType::OpenConfirm, @@ -707,7 +677,7 @@ pub fn build_chan_confirm( })?; // Retrieve the connection - let dest_connection = + let dst_connection = dst_chain.query_connection(&opts.dst().connection_id().clone(), Height::default())?; let ics_target_height = src_chain.query_latest_height()?; @@ -716,7 +686,7 @@ pub fn build_chan_confirm( let mut msgs = build_update_client( dst_chain.clone(), src_chain.clone(), - &dest_connection.client_id(), + &dst_connection.client_id(), ics_target_height, )?; @@ -748,7 +718,24 @@ pub fn build_chan_confirm_and_send( dst_chain: Box, src_chain: Box, opts: &ChannelConfig, -) -> Result, Error> { +) -> Result { let dst_msgs = build_chan_confirm(dst_chain.clone(), src_chain, &opts)?; - Ok(dst_chain.send_msgs(dst_msgs)?) + + let events = dst_chain.send_msgs(dst_msgs)?; + + // Find the relevant event for channel confirm + events + .iter() + .find(|&event| { + matches!(event, IBCEvent::OpenConfirmChannel(_)) + || matches!(event, IBCEvent::ChainError(_)) + }) + .cloned() + .ok_or_else(|| { + Kind::ChanOpenConfirm( + opts.dst().channel_id().clone(), + "no chan confirm event was in the response".to_string(), + ) + .into() + }) } diff --git a/relayer/src/config.rs b/relayer/src/config.rs index a3484e14da..d8d68acdf1 100644 --- a/relayer/src/config.rs +++ b/relayer/src/config.rs @@ -11,7 +11,7 @@ use std::{ use serde_derive::{Deserialize, Serialize}; -use ibc::ics24_host::identifier::ChainId; +use ibc::ics24_host::identifier::{ChainId, PortId}; use tendermint::{net, Hash}; use tendermint_light_client::types::{Height, PeerId, TrustThreshold}; @@ -89,7 +89,6 @@ pub struct ChainConfig { pub account_prefix: String, pub key_name: String, pub store_prefix: String, - pub client_ids: Vec, pub gas: Option, pub max_msg_num: Option, pub max_tx_size: Option, @@ -131,40 +130,15 @@ impl ChainConfig { #[derive(Clone, Debug, Deserialize, Serialize)] pub struct Connection { - pub a_end: Option, // use any source - pub b_end: Option, // use any destination - pub paths: Option>, // use any port, direction bidirectional -} - -#[derive(Clone, Debug, Deserialize, Serialize)] -pub struct ConnectionEnd { - pub chain_id: String, - pub client_id: String, - pub connection_id: Option, // use all connections to this client -} - -#[derive(Clone, Debug, Deserialize, Serialize)] -pub enum Direction { - #[serde(rename = "unidirectional")] - Unidirectional, - #[serde(rename = "bidirectional")] - Bidirectional, -} - -impl Default for Direction { - fn default() -> Self { - Self::Bidirectional - } + pub a_chain: ChainId, + pub b_chain: ChainId, + pub paths: Option>, } #[derive(Clone, Debug, Deserialize, Serialize)] pub struct RelayPath { - pub a_port: Option, // default from any source port - pub b_port: Option, // default from any dest port - pub a_channel: Option, // default from any source port - pub b_channel: Option, // default from any dest port - #[serde(default)] - pub direction: Direction, // default bidirectional + pub a_port: PortId, + pub b_port: PortId, } #[derive(Clone, Debug, Deserialize, Serialize)] diff --git a/relayer/src/connection.rs b/relayer/src/connection.rs index c809e16793..674fe88990 100644 --- a/relayer/src/connection.rs +++ b/relayer/src/connection.rs @@ -1,14 +1,14 @@ use prost_types::Any; -use std::str::FromStr; use std::time::SystemTime; use thiserror::Error; -use tracing::info; +use tracing::{debug, error, info}; use ibc_proto::ibc::core::connection::v1::MsgConnectionOpenAck as RawMsgConnectionOpenAck; use ibc_proto::ibc::core::connection::v1::MsgConnectionOpenConfirm as RawMsgConnectionOpenConfirm; use ibc_proto::ibc::core::connection::v1::MsgConnectionOpenInit as RawMsgConnectionOpenInit; use ibc_proto::ibc::core::connection::v1::MsgConnectionOpenTry as RawMsgConnectionOpenTry; +use ibc::events::IBCEvent; use ibc::ics02_client::height::Height; use ibc::ics03_connection::connection::{ConnectionEnd, Counterparty, State}; use ibc::ics03_connection::msgs::conn_open_ack::MsgConnectionOpenAck; @@ -36,7 +36,7 @@ pub enum ConnectionError { #[derive(Clone, Debug)] pub struct Connection { - config: ConnectionConfig, + pub config: ConnectionConfig, a_client: ForeignClient, b_client: ForeignClient, } @@ -72,6 +72,10 @@ impl ConnectionSideConfig { pub fn client_id(&self) -> &ClientId { &self.client_id } + + pub fn set_connection_id(&mut self, id: &ConnectionId) { + self.connection_id = id.clone() + } } #[derive(Clone, Debug)] @@ -107,71 +111,28 @@ impl ConnectionConfig { impl ConnectionConfig { pub fn new(conn: &config::Connection) -> Result { - let a_conn_endpoint = conn - .a_end - .clone() - .ok_or("Connection source endpoint not specified")?; - let b_conn_endpoint = conn - .b_end - .clone() - .ok_or("Connection destination endpoint not specified")?; - let a_config = ConnectionSideConfig { - chain_id: ChainId::from_str(a_conn_endpoint.chain_id.as_str()) - .map_err(|e| format!("Invalid chain id ({:?})", e))?, - connection_id: ConnectionId::from_str( - a_conn_endpoint - .connection_id - .ok_or("Connection id not specified")? - .as_str(), - ) - .map_err(|e| format!("Invalid connection id ({:?})", e))?, - client_id: ClientId::from_str(a_conn_endpoint.client_id.as_str()) - .map_err(|e| format!("Invalid client id ({:?})", e))?, + chain_id: conn.a_chain.clone(), + connection_id: ConnectionId::default(), + client_id: ClientId::default(), }; let b_config = ConnectionSideConfig { - chain_id: ChainId::from_str(b_conn_endpoint.chain_id.as_str()) - .map_err(|e| format!("Invalid counterparty chain id ({:?})", e))?, - connection_id: ConnectionId::from_str( - b_conn_endpoint - .connection_id - .ok_or("Counterparty connection id not specified")? - .as_str(), - ) - .map_err(|e| format!("Invalid counterparty connection id ({:?})", e))?, - client_id: ClientId::from_str(b_conn_endpoint.client_id.as_str()) - .map_err(|e| format!("Invalid counterparty client id ({:?})", e))?, + chain_id: conn.b_chain.clone(), + connection_id: ConnectionId::default(), + client_id: ClientId::default(), }; Ok(ConnectionConfig { a_config, b_config }) } } -// temp fix for queries -fn get_connection( - chain: Box, - id: &ConnectionId, -) -> Result, ConnectionError> { - match chain.query_connection(id, Height::zero()) { - Err(e) => match e.kind() { - Kind::EmptyResponseValue => Ok(None), - _ => Err(ConnectionError::Failed(format!( - "error retrieving connection {:?}", - e - ))), - }, - Ok(conn) => Ok(Some(conn)), - } -} - impl Connection { /// Create a new connection, ensuring that the handshake has succeeded and the two connection /// ends exist on each side. pub fn new( a_client: ForeignClient, b_client: ForeignClient, - config: ConnectionConfig, ) -> Result { // Validate that the two clients serve the same two chains if a_client.src_chain().id().ne(&b_client.dst_chain().id()) { @@ -180,7 +141,8 @@ impl Connection { a_client.src_chain().id(), b_client.dst_chain().id() ))); - } else if a_client.dst_chain().id().ne(&b_client.src_chain().id()) { + } + if a_client.dst_chain().id().ne(&b_client.src_chain().id()) { return Err(ConnectionError::ConstructorFailed(format!( "the destination chain of client a ({}) does not not match the source chain of client b ({})", a_client.dst_chain().id(), @@ -188,8 +150,19 @@ impl Connection { ))); } - let c = Connection { - config, + let mut c = Connection { + config: ConnectionConfig { + a_config: ConnectionSideConfig::new( + a_client.dst_chain().id(), + Default::default(), + a_client.id().clone(), + ), + b_config: ConnectionSideConfig::new( + b_client.dst_chain().id(), + Default::default(), + b_client.id().clone(), + ), + }, a_client, b_client, }; @@ -209,132 +182,115 @@ impl Connection { } /// Executes a connection handshake protocol (ICS 003) for this connection object - fn handshake(&self) -> Result<(), ConnectionError> { + fn handshake(&mut self) -> Result<(), ConnectionError> { let done = '\u{1F942}'; // surprise emoji let a_chain = self.chain_a(); let b_chain = self.chain_b(); - let flipped = self.config.flipped(); + let mut flipped = self.config.flipped(); + // Try connOpenInit on a_chain + let now = SystemTime::now(); let mut counter = 0; + while counter < MAX_ITER { + counter += 1; + match build_conn_init_and_send(a_chain.clone(), b_chain.clone(), &flipped) { + Err(e) => { + error!("Failed ConnInit {:?}: {}", self.config.a_end(), e); + continue; + } + Ok(result) => { + self.config + .a_config + .set_connection_id(extract_connection_id(&result)?); + info!("{} {} => {:?}\n", done, a_chain.id(), result); + break; + } + } + } + debug!("elapsed time {:?}", now.elapsed().unwrap().as_secs()); + let now = SystemTime::now(); + + // Try connOpenTry on b_chain + counter = 0; + while counter < MAX_ITER { + counter += 1; + match build_conn_try_and_send(b_chain.clone(), a_chain.clone(), &self.config) { + Err(e) => { + error!("Failed ConnTry {:?}: {}", self.config.b_end(), e); + continue; + } + Ok(result) => { + self.config + .b_config + .set_connection_id(extract_connection_id(&result)?); + info!("{} {} => {:?}\n", done, b_chain.id(), result); + break; + } + } + } + debug!("elapsed time {:?}", now.elapsed().unwrap().as_secs()); + + flipped = self.config.flipped(); + counter = 0; while counter < MAX_ITER { counter += 1; let now = SystemTime::now(); // Continue loop if query error - let a_connection = get_connection(a_chain.clone(), &self.config.a_end().connection_id); + let a_connection = + a_chain.query_connection(&self.config.a_end().connection_id, Height::zero()); if a_connection.is_err() { continue; } - let b_connection = get_connection(b_chain.clone(), &self.config.b_end().connection_id); + let b_connection = + b_chain.query_connection(&self.config.b_end().connection_id, Height::zero()); if b_connection.is_err() { continue; } - match (a_connection?, b_connection?) { - (None, None) => { - // Init to src - match build_conn_init_and_send(a_chain.clone(), b_chain.clone(), &flipped) { - Err(e) => info!("{:?} Failed ConnInit {:?}", e, self.config.a_end()), - Ok(_) => info!("{} ConnInit {:?}", done, self.config.a_end()), + match ( + a_connection.unwrap().state().clone(), + b_connection.unwrap().state().clone(), + ) { + (State::Init, State::TryOpen) | (State::TryOpen, State::TryOpen) => { + // Ack to src + match build_conn_ack_and_send(a_chain.clone(), b_chain.clone(), &flipped) { + Err(e) => { + error!("Failed ConnAck {:?}: {}", self.config.a_end(), e); + } + Ok(event) => info!("{} {} => {:?}\n", done, a_chain.id(), event), } } - (Some(a_connection), None) => { - assert!(a_connection.state_matches(&State::Init)); - match build_conn_try_and_send(b_chain.clone(), a_chain.clone(), &self.config) { - Err(e) => info!("{:?} Failed ConnTry {:?}", e, self.config.b_end()), - Ok(_) => info!("{} ConnTry {:?}", done, self.config.b_end()), + (State::Open, State::TryOpen) => { + // Confirm to dest + match build_conn_confirm_and_send( + b_chain.clone(), + a_chain.clone(), + &self.config, + ) { + Err(e) => error!("Failed ConnConfirm {:?}: {}", self.config.b_end(), e), + Ok(event) => info!("{} {} => {:?}\n", done, b_chain.id(), event), } } - (None, Some(b_connection)) => { - assert!(b_connection.state_matches(&State::Init)); - match build_conn_try_and_send(a_chain.clone(), b_chain.clone(), &flipped) { - Err(e) => info!("{:?} Failed ConnTry {:?}", e, self.config.a_end()), - Ok(_) => info!("{} ConnTry {:?}", done, self.config.a_end()), + (State::TryOpen, State::Open) => { + // Confirm to src + match build_conn_confirm_and_send(a_chain.clone(), b_chain.clone(), &flipped) { + Err(e) => error!("Failed ConnConfirm {:?}: {}", self.config.a_end(), e), + Ok(event) => info!("{} {} => {:?}\n", done, a_chain.id(), event), } } - (Some(a_connection), Some(b_connection)) => { - match (a_connection.state(), b_connection.state()) { - (&State::Init, &State::Init) => { - // Try to dest - match build_conn_try_and_send( - b_chain.clone(), - a_chain.clone(), - &self.config, - ) { - Err(e) => { - info!("{:?} Failed ConnTry {:?}", e, self.config.b_end()) - } - Ok(_) => info!("{} ConnTry {:?}", done, self.config.b_end()), - } - } - (&State::TryOpen, &State::Init) => { - // Ack to dest - match build_conn_ack_and_send( - b_chain.clone(), - a_chain.clone(), - &self.config, - ) { - Err(e) => { - info!("{:?} Failed ConnAck {:?}", e, self.config.b_end()) - } - Ok(_) => info!("{} ConnAck {:?}", done, self.config.b_end()), - } - } - (&State::Init, &State::TryOpen) | (&State::TryOpen, &State::TryOpen) => { - // Ack to src - match build_conn_ack_and_send( - a_chain.clone(), - b_chain.clone(), - &flipped, - ) { - Err(e) => { - info!("{:?} Failed ConnAck {:?}", e, self.config.a_end()) - } - Ok(_) => info!("{} ConnAck {:?}", done, self.config.a_end()), - } - } - (&State::Open, &State::TryOpen) => { - // Confirm to dest - match build_conn_confirm_and_send( - b_chain.clone(), - a_chain.clone(), - &self.config, - ) { - Err(e) => { - info!("{:?} Failed ConnConfirm {:?}", e, self.config.b_end()) - } - Ok(_) => { - info!("{} ConnConfirm {:?}", done, self.config.b_end()) - } - } - } - (&State::TryOpen, &State::Open) => { - // Confirm to src - match build_conn_confirm_and_send( - a_chain.clone(), - b_chain.clone(), - &flipped, - ) { - Err(e) => info!("{:?} ConnConfirm {:?}", e, self.config.a_end()), - Ok(_) => { - info!("{} ConnConfirm {:?}", done, self.config.a_end()) - } - } - } - (&State::Open, &State::Open) => { - info!( - "{} {} {} Connection handshake finished for [{:#?}]", - done, done, done, self.config - ); - return Ok(()); - } - _ => {} - } + (State::Open, State::Open) => { + info!( + "{} {} {} Connection handshake finished for [{:#?}]\n", + done, done, done, self.config + ); + return Ok(()); } + _ => {} } - info!("elapsed time {:?}\n", now.elapsed().unwrap().as_secs()); + debug!("elapsed time {:?}", now.elapsed().unwrap().as_secs()); } Err(ConnectionError::Failed(format!( @@ -344,6 +300,18 @@ impl Connection { } } +fn extract_connection_id(event: &IBCEvent) -> Result<&ConnectionId, ConnectionError> { + match event { + IBCEvent::OpenInitConnection(ev) => Ok(ev.connection_id()), + IBCEvent::OpenTryConnection(ev) => Ok(ev.connection_id()), + IBCEvent::OpenAckConnection(ev) => Ok(ev.connection_id()), + IBCEvent::OpenConfirmConnection(ev) => Ok(ev.connection_id()), + _ => Err(ConnectionError::Failed( + "cannot extract connection_id from result".to_string(), + )), + } +} + /// Enumeration of proof carrying ICS3 message, helper for relayer. #[derive(Clone, Debug, PartialEq, Eq)] pub enum ConnectionMsgType { @@ -357,18 +325,6 @@ pub fn build_conn_init( src_chain: Box, opts: &ConnectionConfig, ) -> Result, Error> { - // Check that the destination chain will accept the message, i.e. it does not have the connection - if dst_chain - .query_connection(&opts.dst().connection_id(), ICSHeight::default()) - .is_ok() - { - return Err(Kind::ConnOpenInit( - opts.dst().connection_id().clone(), - "connection already exist".into(), - ) - .into()); - } - // Get signer let signer = dst_chain .get_signer() @@ -376,18 +332,14 @@ pub fn build_conn_init( let prefix = src_chain.query_commitment_prefix()?; - let counterparty = Counterparty::new( - opts.src().client_id().clone(), - Some(opts.src().connection_id().clone()), - prefix, - ); + let counterparty = Counterparty::new(opts.src().client_id().clone(), None, prefix); // Build the domain type message let new_msg = MsgConnectionOpenInit { client_id: opts.dst().client_id().clone(), - connection_id: opts.dst().connection_id().clone(), counterparty, version: dst_chain.query_compatible_versions()?[0].clone(), + delay_period: 0, signer, }; @@ -398,9 +350,27 @@ pub fn build_conn_init_and_send( dst_chain: Box, src_chain: Box, opts: &ConnectionConfig, -) -> Result, Error> { +) -> Result { let dst_msgs = build_conn_init(dst_chain.clone(), src_chain, opts)?; - Ok(dst_chain.send_msgs(dst_msgs)?) + + let events = dst_chain.send_msgs(dst_msgs)?; + + // Find the relevant event for connection init + let result = events + .iter() + .find(|&event| { + matches!(event, IBCEvent::OpenInitConnection(_)) + || matches!(event, IBCEvent::ChainError(_)) + }) + .cloned() + .ok_or_else(|| Kind::ConnOpenInit("no conn init event was in the response".to_string()))?; + + // TODO - make chainError an actual error + match result { + IBCEvent::OpenInitConnection(_) => Ok(result), + IBCEvent::ChainError(e) => Err(Kind::ConnOpenInit(e).into()), + _ => panic!("internal error"), + } } fn check_destination_connection_state( @@ -424,7 +394,7 @@ fn check_destination_connection_state( if good_state && good_client_ids && good_connection_ids { Ok(()) } else { - Err(Kind::ConnOpenTry( + Err(Kind::ConnOpen( connection_id, "connection already exist in an incompatible state".into(), ) @@ -450,9 +420,9 @@ fn validated_expected_connection( // The highest expected state, depends on the message type: let highest_state = match msg_type { - ConnectionMsgType::OpenTry => State::Init, ConnectionMsgType::OpenAck => State::TryOpen, ConnectionMsgType::OpenConfirm => State::TryOpen, + _ => State::Uninitialized, }; let dst_expected_connection = ConnectionEnd::new( @@ -460,34 +430,26 @@ fn validated_expected_connection( opts.dst().client_id().clone(), counterparty, src_chain.query_compatible_versions()?, - ) - .unwrap(); + 0, + ); // Retrieve existing connection if any let dst_connection = - dst_chain.query_connection(&opts.dst().connection_id().clone(), ICSHeight::default()); + dst_chain.query_connection(&opts.dst().connection_id().clone(), ICSHeight::default())?; // Check if a connection is expected to exist on destination chain - if msg_type == ConnectionMsgType::OpenTry { - // TODO - check typed Err, or make query_connection return Option - // It is ok if there is no connection for Try Tx - if dst_connection.is_err() { - return Ok(dst_expected_connection); - } - } else { - // A connection must exist on destination chain for Ack and Confirm Tx-es to succeed - if dst_connection.is_err() { - return Err(Kind::ConnOpenTry( - opts.src().connection_id().clone(), - "missing connection on source chain".to_string(), - ) - .into()); - } + // A connection must exist on destination chain for Ack and Confirm Tx-es to succeed + if dst_connection.state_matches(&State::Uninitialized) { + return Err(Kind::ConnOpen( + opts.src().connection_id().clone(), + "missing connection on source chain".to_string(), + ) + .into()); } check_destination_connection_state( opts.dst().connection_id().clone(), - dst_connection?, + dst_connection, dst_expected_connection.clone(), )?; @@ -500,34 +462,16 @@ pub fn build_conn_try( src_chain: Box, opts: &ConnectionConfig, ) -> Result, Error> { - let dst_expected_connection = validated_expected_connection( - dst_chain.clone(), - src_chain.clone(), - ConnectionMsgType::OpenTry, - opts, - ) - .map_err(|e| { - Kind::ConnOpenTry( - opts.dst().connection_id().clone(), - "try options inconsistent with existing connection on destination chain".to_string(), - ) - .context(e) - })?; - let src_connection = src_chain .query_connection(&opts.src().connection_id().clone(), ICSHeight::default()) .map_err(|e| { - Kind::ConnOpenTry( - opts.src().connection_id().clone(), - "missing connection on source chain".to_string(), - ) - .context(e) + Kind::ConnOpenTry("missing connection on source chain".to_string()).context(e) })?; // TODO - check that the src connection is consistent with the try options // Build add send the message(s) for updating client on source - // TODO - add check if it is required + // TODO - add check if update client is required let src_client_target_height = dst_chain.query_latest_height()?; let client_msgs = build_update_client( src_chain.clone(), @@ -565,14 +509,20 @@ pub fn build_conn_try( .get_signer() .map_err(|e| Kind::KeyBase.context(e))?; + let counterparty = Counterparty::new( + opts.src().client_id().clone(), + Option::from(opts.src().connection_id().clone()), + src_chain.query_commitment_prefix()?, + ); + let new_msg = MsgConnectionOpenTry { - connection_id: opts.dst().connection_id().clone(), client_id: opts.dst().client_id().clone(), client_state, - counterparty_chosen_connection_id: src_connection.counterparty().connection_id().cloned(), - counterparty: dst_expected_connection.counterparty(), + previous_connection_id: None, + counterparty, counterparty_versions, proofs, + delay_period: 0, signer, }; @@ -587,9 +537,22 @@ pub fn build_conn_try_and_send( dst_chain: Box, src_chain: Box, opts: &ConnectionConfig, -) -> Result, Error> { +) -> Result { let dst_msgs = build_conn_try(dst_chain.clone(), src_chain, &opts)?; - Ok(dst_chain.send_msgs(dst_msgs)?) + + let events = dst_chain.send_msgs(dst_msgs)?; + + // Find the relevant event for connection try transaction + events + .iter() + .find(|&event| { + matches!(event, IBCEvent::OpenTryConnection(_)) + || matches!(event, IBCEvent::ChainError(_)) + }) + .cloned() + .ok_or_else(|| { + Kind::ConnOpenTry("no conn try event was in the response".to_string()).into() + }) } /// Attempts to build a MsgConnOpenAck. @@ -679,9 +642,26 @@ pub fn build_conn_ack_and_send( dst_chain: Box, src_chain: Box, opts: &ConnectionConfig, -) -> Result, Error> { +) -> Result { let dst_msgs = build_conn_ack(dst_chain.clone(), src_chain, opts)?; - Ok(dst_chain.send_msgs(dst_msgs)?) + + let events = dst_chain.send_msgs(dst_msgs)?; + + // Find the relevant event for connection ack + events + .iter() + .find(|&event| { + matches!(event, IBCEvent::OpenAckConnection(_)) + || matches!(event, IBCEvent::ChainError(_)) + }) + .cloned() + .ok_or_else(|| { + Kind::ConnOpenAck( + opts.dst().connection_id().clone(), + "no conn ack event was in the response".to_string(), + ) + .into() + }) } /// Attempts to build a MsgConnOpenConfirm. @@ -756,7 +736,24 @@ pub fn build_conn_confirm_and_send( dst_chain: Box, src_chain: Box, opts: &ConnectionConfig, -) -> Result, Error> { +) -> Result { let dst_msgs = build_conn_confirm(dst_chain.clone(), src_chain, &opts)?; - Ok(dst_chain.send_msgs(dst_msgs)?) + + let events = dst_chain.send_msgs(dst_msgs)?; + + // Find the relevant event for connection confirm + events + .iter() + .find(|&event| { + matches!(event, IBCEvent::OpenConfirmConnection(_)) + || matches!(event, IBCEvent::ChainError(_)) + }) + .cloned() + .ok_or_else(|| { + Kind::ConnOpenConfirm( + opts.dst().connection_id().clone(), + "no conn confirm event was in the response".to_string(), + ) + .into() + }) } diff --git a/relayer/src/error.rs b/relayer/src/error.rs index 8384d201e4..2e973f1583 100644 --- a/relayer/src/error.rs +++ b/relayer/src/error.rs @@ -1,7 +1,7 @@ //! This module defines the various errors that be raised in the relayer. use anomaly::{BoxError, Context}; -use ibc::ics24_host::identifier::{ChannelId, ClientId, ConnectionId}; +use ibc::ics24_host::identifier::{ChannelId, ConnectionId}; use thiserror::Error; /// An error that can be raised by the relayer. @@ -67,16 +67,20 @@ pub enum Kind { BuildClientStateFailure, /// Create client failure - #[error("Failed to create client {0}: {1}")] - CreateClient(ClientId, String), + #[error("Failed to create client {0}")] + CreateClient(String), + + /// Common failures to all connection messages + #[error("Failed to build conn open message {0}: {1}")] + ConnOpen(ConnectionId, String), /// Connection open init failure - #[error("Failed to build conn open init {0}: {1}")] - ConnOpenInit(ConnectionId, String), + #[error("Failed to build conn open init {0}")] + ConnOpenInit(String), /// Connection open try failure - #[error("Failed to build conn open try {0}: {1}")] - ConnOpenTry(ConnectionId, String), + #[error("Failed to build conn open try {0}")] + ConnOpenTry(String), /// Connection open ack failure #[error("Failed to build conn open ack {0}: {1}")] @@ -86,13 +90,17 @@ pub enum Kind { #[error("Failed to build conn open confirm {0}: {1}")] ConnOpenConfirm(ConnectionId, String), + /// Common failures to all channel messages + #[error("Failed to build chan open msg {0}: {1}")] + ChanOpen(ChannelId, String), + /// Channel open init failure - #[error("Failed to build channel open init {0}: {1}")] - ChanOpenInit(ChannelId, String), + #[error("Failed to build channel open init {0}")] + ChanOpenInit(String), /// Channel open try failure - #[error("Failed to build channel open try {0}: {1}")] - ChanOpenTry(ChannelId, String), + #[error("Failed to build channel open try {0}")] + ChanOpenTry(String), /// Channel open ack failure #[error("Failed to build channel open ack {0}: {1}")] @@ -103,8 +111,20 @@ pub enum Kind { ChanOpenConfirm(ChannelId, String), /// Packet recv failure - #[error("Failed to build packet recv {0}: {1}")] - PacketRecv(ChannelId, String), + #[error("Failed to build packet {0}: {1}")] + Packet(ChannelId, String), + + /// Packet recv failure + #[error("Failed to build recv packet {0}: {1}")] + RecvPacket(ChannelId, String), + + /// Packet acknowledgement failure + #[error("Failed to build acknowledge packet {0}: {1}")] + AckPacket(ChannelId, String), + + /// Packet timeout failure + #[error("Failed to build timeout packet {0}: {1}")] + TimeoutPacket(ChannelId, String), /// A message transaction failure #[error("Message transaction failure: {0}")] diff --git a/relayer/src/foreign_client.rs b/relayer/src/foreign_client.rs index 951829aa04..cbef36be1a 100644 --- a/relayer/src/foreign_client.rs +++ b/relayer/src/foreign_client.rs @@ -1,13 +1,14 @@ use prost_types::Any; use thiserror::Error; -use tracing::info; +use tracing::{error, info}; +use ibc::events::IBCEvent; use ibc::ics02_client::header::Header; use ibc::ics02_client::msgs::create_client::MsgCreateAnyClient; use ibc::ics02_client::msgs::update_client::MsgUpdateAnyClient; use ibc::ics02_client::state::ClientState; use ibc::ics02_client::state::ConsensusState; -use ibc::ics24_host::identifier::{ChainId, ClientId}; +use ibc::ics24_host::identifier::ClientId; use ibc::tx_msg::Msg; use ibc::Height; use ibc_proto::ibc::core::client::v1::MsgCreateClient as RawMsgCreateClient; @@ -25,36 +26,11 @@ pub enum ForeignClientError { ClientUpdate(String), } -#[derive(Clone, Debug)] -pub struct ForeignClientConfig { - /// The identifier of the chain which hosts this client, or destination chain - chain: ChainId, // TODO: This field seems useless, consider removing - - /// The client's identifier - id: ClientId, -} - -impl ForeignClientConfig { - pub fn new(chain: &ChainId, id: &ClientId) -> ForeignClientConfig { - Self { - chain: chain.clone(), - id: id.clone(), - } - } - - pub fn chain_id(&self) -> &ChainId { - &self.chain - } - - pub fn client_id(&self) -> &ClientId { - &self.id - } -} - #[derive(Clone, Debug)] pub struct ForeignClient { - /// The configuration of this client. - config: ForeignClientConfig, + /// The identifier of this client. The host chain determines this id upon client creation, + /// so it may be missing (`None). + id: ClientId, /// A handle to the chain hosting this client, i.e., destination chain. dst_chain: Box, @@ -72,19 +48,18 @@ impl ForeignClient { pub fn new( dst_chain: Box, src_chain: Box, - config: ForeignClientConfig, ) -> Result { - // Sanity check: the configuration should be consistent with the other parameters - if config.chain_id().ne(&dst_chain.id()) { + // Sanity check + if src_chain.id().eq(&dst_chain.id()) { return Err(ForeignClientError::ClientCreate(format!( - "host chain id ({}) in config does not not match host chain in argument ({})", - config.chain_id(), - dst_chain.id() + "the source ({}) and destination ({}) chains must be different", + src_chain.id(), + dst_chain.id(), ))); } - let client = ForeignClient { - config, + let mut client = ForeignClient { + id: ClientId::default(), dst_chain: dst_chain.clone(), src_chain: src_chain.clone(), }; @@ -94,30 +69,27 @@ impl ForeignClient { Ok(client) } - fn create(&self) -> Result<(), ForeignClientError> { + pub fn id(&self) -> &ClientId { + &self.id + } + + /// Sends the client creation transaction & subsequently sets the id of this ForeignClient + fn create(&mut self) -> Result<(), ForeignClientError> { let done = '\u{1F36D}'; - // Query the client state on source chain. - let client_state = self - .dst_chain - .query_client_state(&self.config.id, Height::default()); - - // If an error is returned, the client does not already exist - if client_state.is_err() { - build_create_client_and_send( - self.dst_chain.clone(), - self.src_chain.clone(), - &self.config, - ) - .map_err(|e| { - ForeignClientError::ClientCreate(format!("Create client failed ({:?})", e)) - })?; + match build_create_client_and_send(self.dst_chain.clone(), self.src_chain.clone()) { + Err(e) => { + error!("Failed CreateClient {:?}: {}", self.dst_chain.id(), e); + return Err(ForeignClientError::ClientCreate(format!( + "Create client failed ({:?})", + e + ))); + } + Ok(event) => { + self.id = extract_client_id(&event)?.clone(); + info!("{} {} => {:?}\n", done, self.dst_chain.id(), event); + } } - info!( - "{} Client on {:?} is created {:?}\n", - done, self.config.chain, self.config.id - ); - Ok(()) } @@ -131,30 +103,14 @@ impl ForeignClient { self.src_chain.clone() } - /// Creates a message for updating this client with the latest header of its source chain. - pub fn prepare_update(&self) -> Result, ForeignClientError> { - let target_height = self.src_chain.query_latest_height().map_err(|e| { - ForeignClientError::ClientUpdate(format!("error querying latest height {:?}", e)) - })?; - let msg = build_update_client( - self.dst_chain.clone(), - self.src_chain.clone(), - self.config.client_id(), - target_height, - ) - .map_err(|e| { - ForeignClientError::ClientUpdate(format!("build_update_client error {:?}", e)) - })?; - - Ok(msg) - } - /// Attempts to update a client using header from the latest height of its source chain. pub fn update(&self) -> Result<(), ForeignClientError> { + let client_id = self.id.clone(); + let res = build_update_client_and_send( self.dst_chain.clone(), self.src_chain.clone(), - &self.config, + &client_id, ) .map_err(|e| { ForeignClientError::ClientUpdate(format!("build_create_client_and_send {:?}", e)) @@ -162,13 +118,25 @@ impl ForeignClient { println!( "Client id {:?} on {:?} updated with return message {:?}\n", - self.config.id, self.config.chain, res + client_id, + self.dst_chain.id(), + res ); Ok(()) } } +pub fn extract_client_id(event: &IBCEvent) -> Result<&ClientId, ForeignClientError> { + match event { + IBCEvent::CreateClient(ev) => Ok(ev.client_id()), + IBCEvent::UpdateClient(ev) => Ok(ev.client_id()), + _ => Err(ForeignClientError::ClientCreate( + "cannot extract client_id from result".to_string(), + )), + } +} + /// Lower-level interface for preparing a message to create a client. /// /// ## Note @@ -176,18 +144,7 @@ impl ForeignClient { pub fn build_create_client( dst_chain: Box, src_chain: Box, - dst_client_id: &ClientId, ) -> Result { - // Verify that the client has not been created already, i.e the destination chain does not - // have a state for this client. - let client_state = dst_chain.query_client_state(&dst_client_id, Height::default()); - if client_state.is_ok() { - return Err(Into::::into(Kind::CreateClient( - dst_client_id.clone(), - "client already exists".into(), - ))); - } - // Get signer let signer = dst_chain .get_signer() @@ -196,7 +153,6 @@ pub fn build_create_client( // Build client create message with the data from source chain at latest height. let latest_height = src_chain.query_latest_height()?; Ok(MsgCreateAnyClient::new( - dst_client_id.clone(), src_chain.build_client_state(latest_height)?.wrap_any(), src_chain.build_consensus_state(latest_height)?.wrap_any(), signer, @@ -207,17 +163,19 @@ pub fn build_create_client( } /// Lower-level interface for creating a client. +/// Returns the identifier of the newly created client. /// /// ## Note /// Methods in `ForeignClient` (see `new`) should be preferred over this. pub fn build_create_client_and_send( dst_chain: Box, src_chain: Box, - opts: &ForeignClientConfig, -) -> Result, Error> { - let new_msg = build_create_client(dst_chain.clone(), src_chain, opts.client_id())?; +) -> Result { + let new_msg = build_create_client(dst_chain.clone(), src_chain)?; - Ok(dst_chain.send_msgs(vec![new_msg.to_any::()])?) + let res = dst_chain.send_msgs(vec![new_msg.to_any::()])?; + assert!(!res.is_empty()); + Ok(res[0].clone()) } /// Lower-level interface to create the message for updating a client to height `target_height`. @@ -256,21 +214,30 @@ pub fn build_update_client( pub fn build_update_client_and_send( dst_chain: Box, src_chain: Box, - opts: &ForeignClientConfig, -) -> Result, Error> { + dst_client_id: &ClientId, +) -> Result { let new_msgs = build_update_client( dst_chain.clone(), src_chain.clone(), - opts.client_id(), + dst_client_id, src_chain.query_latest_height()?, )?; - Ok(dst_chain.send_msgs(new_msgs)?) + let mut events = dst_chain.send_msgs(new_msgs)?; + assert!(!events.is_empty()); + Ok(events.pop().unwrap()) } /// Tests the integration of crates `relayer` plus `relayer-cli` against crate `ibc`. These tests /// exercise various client methods (create, update, ForeignClient::new) using locally-running /// instances of chains built using `MockChain`. +/// +/// ## Why are all these tests ignored? +/// We ignore these tests as of #451, because the mock chain is not yet capable of producing +/// transaction responses of correct types (the types should be similar to `tx_commit::Response`). +/// Another problem is that `build_create_client_and_send` contains a Cosmos-specific method +/// for parsing transaction response. Once this parsing stage is general enough for the Mock chain, +/// these tests should require minimal changes to pass. #[cfg(test)] mod test { use std::str::FromStr; @@ -282,64 +249,44 @@ mod test { use crate::chain::mock::MockChain; use crate::chain::runtime::ChainRuntime; use crate::foreign_client::{ - build_create_client_and_send, build_update_client_and_send, ForeignClient, - ForeignClientConfig, + build_create_client_and_send, build_update_client_and_send, extract_client_id, + ForeignClient, }; /// Basic test for the `build_create_client_and_send` method. #[test] + #[ignore = "cannot parse client creation against mock chain (#451), temp. disabled"] fn create_client_and_send_method() { - let a_client_id = ClientId::from_str("client_on_a_forb").unwrap(); - let b_client_id = ClientId::from_str("client_on_b_fora").unwrap(); let a_cfg = get_basic_chain_config("chain_a"); let b_cfg = get_basic_chain_config("chain_b"); - let a_opts = ForeignClientConfig::new(&a_cfg.id, &a_client_id); - let b_opts = ForeignClientConfig::new(&b_cfg.id, &b_client_id); let (a_chain, _) = ChainRuntime::::spawn(a_cfg).unwrap(); let (b_chain, _) = ChainRuntime::::spawn(b_cfg).unwrap(); // Create the client on chain a - let res = build_create_client_and_send(a_chain.clone(), b_chain.clone(), &a_opts); + let res = build_create_client_and_send(a_chain.clone(), b_chain.clone()); assert!( res.is_ok(), "build_create_client_and_send failed (chain a) with error {:?}", res ); - // Double client creation should be forbidden. - let res = build_create_client_and_send(a_chain.clone(), b_chain.clone(), &a_opts); - assert!( - res.is_err(), - "build_create_client_and_send double client creation should have failed!", - ); - // Create the client on chain b - let res = build_create_client_and_send(b_chain.clone(), a_chain.clone(), &b_opts); + let res = build_create_client_and_send(b_chain.clone(), a_chain.clone()); assert!( res.is_ok(), "build_create_client_and_send failed (chain b) with error {:?}", res ); - - // Test double creation for chain b - let res = build_create_client_and_send(b_chain, a_chain, &b_opts); - assert!( - res.is_err(), - "build_create_client_and_send failed (chain b) with error {:?}", - res - ); } /// Basic test for the `build_update_client_and_send` & `build_create_client_and_send` methods. #[test] + #[ignore = "cannot parse client creation against mock chain (#451), temp. disabled"] fn update_client_and_send_method() { let a_cfg = get_basic_chain_config("chain_a"); let b_cfg = get_basic_chain_config("chain_b"); let a_client_id = ClientId::from_str("client_on_a_forb").unwrap(); - let a_opts = ForeignClientConfig::new(&a_cfg.id, &a_client_id); - let b_client_id = ClientId::from_str("client_on_b_fora").unwrap(); - let b_opts = ForeignClientConfig::new(&b_cfg.id, &b_client_id); // The number of ping-pong iterations let num_iterations = 3; @@ -348,7 +295,7 @@ mod test { let (b_chain, _) = ChainRuntime::::spawn(b_cfg).unwrap(); // This action should fail because no client exists (yet) - let res = build_update_client_and_send(a_chain.clone(), b_chain.clone(), &a_opts); + let res = build_update_client_and_send(a_chain.clone(), b_chain.clone(), &a_client_id); assert!( res.is_err(), "build_update_client_and_send was supposed to fail (no client existed)" @@ -358,32 +305,38 @@ mod test { let b_height_start = b_chain.query_latest_height().unwrap(); // Create a client on chain a - let res = build_create_client_and_send(a_chain.clone(), b_chain.clone(), &a_opts); + let res = build_create_client_and_send(a_chain.clone(), b_chain.clone()); assert!( res.is_ok(), "build_create_client_and_send failed (chain a) with error {:?}", res ); + let a_client_id = extract_client_id(&res.unwrap()).unwrap().clone(); // This should fail because the client on chain a already has the latest headers. Chain b, // the source chain for the client on a, is at the same height where it was when the client // was created, so an update should fail here. - let res = build_update_client_and_send(a_chain.clone(), b_chain.clone(), &a_opts); - + let res = build_update_client_and_send(a_chain.clone(), b_chain.clone(), &a_client_id); assert!( res.is_err(), "build_update_client_and_send was supposed to fail", ); + + // Remember b's height. let b_height_last = b_chain.query_latest_height().unwrap(); assert_eq!(b_height_last, b_height_start); // Create a client on chain b - let res = build_create_client_and_send(b_chain.clone(), a_chain.clone(), &b_opts); + let res = build_create_client_and_send(b_chain.clone(), a_chain.clone()); assert!( res.is_ok(), "build_create_client_and_send failed (chain b) with error {:?}", res ); + + // Remember the id of the client we created on chain b + let b_client_id = extract_client_id(&res.unwrap()).unwrap().clone(); + // Chain b should have advanced let mut b_height_last = b_chain.query_latest_height().unwrap(); assert_eq!(b_height_last, b_height_start.increment()); @@ -393,7 +346,7 @@ mod test { // Now we can update both clients -- a ping pong, similar to ICS18 `client_update_ping_pong` for _i in 1..num_iterations { - let res = build_update_client_and_send(a_chain.clone(), b_chain.clone(), &a_opts); + let res = build_update_client_and_send(a_chain.clone(), b_chain.clone(), &a_client_id); assert!( res.is_ok(), "build_update_client_and_send failed (chain a) with error: {:?}", @@ -407,7 +360,7 @@ mod test { ); // And also update the client on chain b. - let res = build_update_client_and_send(b_chain.clone(), a_chain.clone(), &b_opts); + let res = build_update_client_and_send(b_chain.clone(), a_chain.clone(), &b_client_id); assert!( res.is_ok(), "build_update_client_and_send failed (chain b) with error: {:?}", @@ -424,41 +377,43 @@ mod test { /// Tests for `ForeignClient::new()`. #[test] + #[ignore = "cannot parse client creation against mock chain (#451), temp. disabled"] fn foreign_client_create() { let a_cfg = get_basic_chain_config("chain_a"); let b_cfg = get_basic_chain_config("chain_b"); - let a_client_id = ClientId::from_str("client_on_a_forb").unwrap(); - let a_opts = ForeignClientConfig::new(&a_cfg.id, &a_client_id); - let b_client_id = ClientId::from_str("client_on_b_fora").unwrap(); - let b_opts = ForeignClientConfig::new(&b_cfg.id, &b_client_id); let (a_chain, _) = ChainRuntime::::spawn(a_cfg).unwrap(); let (b_chain, _) = ChainRuntime::::spawn(b_cfg).unwrap(); // Instantiate the foreign clients on the two chains. - let client_on_a = ForeignClient::new(a_chain.clone(), b_chain.clone(), a_opts); + let res_client_on_a = ForeignClient::new(a_chain.clone(), b_chain.clone()); assert!( - client_on_a.is_ok(), + res_client_on_a.is_ok(), "Client creation (on chain a) failed with error: {:?}", - client_on_a + res_client_on_a ); - let client_on_b = ForeignClient::new(b_chain.clone(), a_chain.clone(), b_opts); + let client_on_a = res_client_on_a.unwrap(); + let a_client = client_on_a.id; + + let res_client_on_b = ForeignClient::new(b_chain.clone(), a_chain.clone()); assert!( - client_on_b.is_ok(), + res_client_on_b.is_ok(), "Client creation (on chain a) failed with error: {:?}", - client_on_b + res_client_on_b ); + let client_on_b = res_client_on_b.unwrap(); + let b_client = client_on_b.id; // Now that the clients exists, we should be able to query its state - let b_client_state = b_chain.query_client_state(&b_client_id, Height::default()); + let b_client_state = b_chain.query_client_state(&b_client, Height::default()); assert!( b_client_state.is_ok(), "Client query (on chain b) failed with error: {:?}", b_client_state ); - let a_client_state = a_chain.query_client_state(&a_client_id, Height::default()); + let a_client_state = a_chain.query_client_state(&a_client, Height::default()); assert!( a_client_state.is_ok(), "Client query (on chain a) failed with error: {:?}", @@ -468,19 +423,18 @@ mod test { /// Tests for `ForeignClient::update()`. #[test] + #[ignore = "cannot parse client creation against mock chain (#451), temp. disabled"] fn foreign_client_update() { let a_cfg = get_basic_chain_config("chain_a"); let b_cfg = get_basic_chain_config("chain_b"); - let a_client_id = ClientId::from_str("client_on_a_forb").unwrap(); - let a_opts = ForeignClientConfig::new(&a_cfg.id, &a_client_id); - let b_client_id = ClientId::from_str("client_on_b_fora").unwrap(); - let b_opts = ForeignClientConfig::new(&b_cfg.id, &b_client_id); + let mut _a_client_id = ClientId::from_str("client_on_a_forb").unwrap(); + let mut _b_client_id = ClientId::from_str("client_on_b_fora").unwrap(); let (a_chain, _) = ChainRuntime::::spawn(a_cfg).unwrap(); let (b_chain, _) = ChainRuntime::::spawn(b_cfg).unwrap(); // Instantiate the foreign clients on the two chains. - let client_on_a_res = ForeignClient::new(a_chain.clone(), b_chain.clone(), a_opts); + let client_on_a_res = ForeignClient::new(a_chain.clone(), b_chain.clone()); assert!( client_on_a_res.is_ok(), "Client creation (on chain a) failed with error: {:?}", @@ -488,7 +442,7 @@ mod test { ); let client_on_a = client_on_a_res.unwrap(); - let client_on_b_res = ForeignClient::new(b_chain.clone(), a_chain.clone(), b_opts); + let client_on_b_res = ForeignClient::new(b_chain.clone(), a_chain.clone()); assert!( client_on_b_res.is_ok(), "Client creation (on chain a) failed with error: {:?}", diff --git a/relayer/src/light_client/mock.rs b/relayer/src/light_client/mock.rs index 1f716ad2aa..ee4ca9c456 100644 --- a/relayer/src/light_client/mock.rs +++ b/relayer/src/light_client/mock.rs @@ -19,7 +19,7 @@ impl LightClient { /// Returns a LightBlock at the requested height `h`. fn light_block(&self, h: Height) -> ::LightBlock { - HostBlock::generate_tm_block(self.chain_id.clone(), h.version_height) + HostBlock::generate_tm_block(self.chain_id.clone(), h.revision_height) } } diff --git a/relayer/src/light_client/tendermint.rs b/relayer/src/light_client/tendermint.rs index 73cbf49d7f..6ce555a64b 100644 --- a/relayer/src/light_client/tendermint.rs +++ b/relayer/src/light_client/tendermint.rs @@ -30,7 +30,7 @@ impl super::LightClient for LightClient { } fn verify_to_target(&self, height: ibc::Height) -> Result { - let height = TMHeight::try_from(height.version_height) + let height = TMHeight::try_from(height.revision_height) .map_err(|e| error::Kind::InvalidHeight.context(e))?; self.handle diff --git a/relayer/src/link.rs b/relayer/src/link.rs index a0bf3cb641..2f5bb644c2 100644 --- a/relayer/src/link.rs +++ b/relayer/src/link.rs @@ -3,17 +3,20 @@ use thiserror::Error; use tracing::{error, info}; use ibc_proto::ibc::core::channel::v1::{ - MsgRecvPacket as RawMsgRecvPacket, QueryPacketCommitmentsRequest, QueryUnreceivedPacketsRequest, + MsgAcknowledgement as RawMsgAck, MsgRecvPacket as RawMsgRecvPacket, + MsgTimeout as RawMsgTimeout, QueryPacketAcknowledgementsRequest, QueryPacketCommitmentsRequest, + QueryUnreceivedAcksRequest, QueryUnreceivedPacketsRequest, }; use ibc::{ downcast, events::{IBCEvent, IBCEventType}, ics04_channel::channel::{QueryPacketEventDataRequest, State}, - ics04_channel::events::SendPacket, + ics04_channel::events::{SendPacket, WriteAcknowledgement}, + ics04_channel::msgs::acknowledgement::MsgAcknowledgement, ics04_channel::msgs::recv_packet::MsgRecvPacket, + ics04_channel::msgs::timeout::MsgTimeout, ics04_channel::packet::Packet, - ics23_commitment::commitment::CommitmentProof, ics24_host::identifier::{ChainId, ChannelId, ClientId, PortId}, tx_msg::Msg, Height, @@ -25,6 +28,7 @@ use crate::config::ChainConfig; use crate::connection::ConnectionError; use crate::error::{Error, Kind}; use crate::foreign_client::build_update_client; +use ibc::ics04_channel::packet::{PacketMsgType, Sequence}; #[derive(Debug, Error)] pub enum LinkError { @@ -60,9 +64,14 @@ fn send_update_client_and_msgs( let mut msgs_to_send = build_update_client(dst_chain.clone(), src_chain, client_id, update_height)?; msgs_to_send.append(msgs); - info!("sending {:#?} messages", msgs_to_send.len()); + info!( + "sending {:?} messages to {}, update client at height {:?}", + msgs_to_send.len(), + dst_chain.id(), + update_height, + ); let res = dst_chain.send_msgs(msgs_to_send)?; - info!("result {:#?}", res); + info!("result {:?}\n", res); } Ok(()) } @@ -90,14 +99,26 @@ impl Link { IBCEvent::SendPacketChannel(send_packet_ev) => { self.channel.config.a_config.chain_id().clone() == from_chain.id() && self.channel.config.a_config.channel_id().clone() - == send_packet_ev.envelope.packet_src_channel + == send_packet_ev.packet.source_channel && self.channel.config.a_config.port_id().clone() - == send_packet_ev.envelope.packet_src_port + == send_packet_ev.packet.source_port || self.channel.config.b_config.chain_id().clone() == from_chain.id() && self.channel.config.b_config.channel_id().clone() - == send_packet_ev.envelope.packet_src_channel + == send_packet_ev.packet.source_channel && self.channel.config.b_config.port_id().clone() - == send_packet_ev.envelope.packet_src_port + == send_packet_ev.packet.source_port + } + IBCEvent::WriteAcknowledgementChannel(write_ack_ev) => { + self.channel.config.a_config.chain_id().clone() == from_chain.id() + && self.channel.config.a_config.channel_id().clone() + == write_ack_ev.packet.destination_channel + && self.channel.config.a_config.port_id().clone() + == write_ack_ev.packet.destination_port + || self.channel.config.b_config.chain_id().clone() == from_chain.id() + && self.channel.config.b_config.channel_id().clone() + == write_ack_ev.packet.destination_channel + && self.channel.config.b_config.port_id().clone() + == write_ack_ev.packet.destination_port } _ => false, } @@ -112,6 +133,7 @@ impl Link { let mut prev_height = Height::zero(); let mut prev_msgs = vec![]; + let dst_height = dst_chain.query_latest_height()?; // Iterate through the IBC Events, build the message for each and collect all at same height. // Send a multi message transaction with these, prepending the client update for batch in src_subscription.try_iter().collect::>().iter() { @@ -119,12 +141,13 @@ impl Link { if !self.must_process_event(src_chain.clone(), event) { continue; } - let packet_msg = handle_packet_event(dst_chain.clone(), src_chain.clone(), event)?; + let (packet_msg, _) = + handle_packet_event(dst_chain.clone(), dst_height, src_chain.clone(), event)?; // TODO add ICS height to IBC event let event_height = Height { - version_height: u64::from(event.height()), - version_number: ChainId::chain_version(src_chain.id().to_string().as_str()), + revision_height: u64::from(event.height()), + revision_number: ChainId::chain_version(src_chain.id().to_string().as_str()), }; if prev_height == Height::zero() { prev_height = event_height; @@ -171,233 +194,541 @@ impl Link { fn handle_packet_event( dst_chain: Box, + dst_height: Height, src_chain: Box, event: &IBCEvent, -) -> Result, Error> { +) -> Result<(Option, Option), Error> { match event { IBCEvent::SendPacketChannel(send_packet_ev) => { - info!("received event {:#?}", send_packet_ev.envelope); - let msg = build_packet_recv_msg_from_send_event(dst_chain, src_chain, &send_packet_ev) - .unwrap(); - Ok(Some(msg.to_any::())) + info!("{} => event {}", src_chain.id(), send_packet_ev); + Ok(build_recv_or_timeout_from_send_packet_event( + dst_chain, + dst_height, + src_chain, + &send_packet_ev, + )?) + } + IBCEvent::WriteAcknowledgementChannel(write_ack_ev) => { + info!("{} => event {}", src_chain.id(), write_ack_ev); + Ok(( + Some(build_ack_from_recv_event( + dst_chain, + src_chain, + &write_ack_ev, + )?), + None, + )) } - _ => Ok(None), + _ => Ok((None, None)), } } -fn build_packet_recv_msg_from_send_event( +fn build_recv_packet( dst_chain: Box, src_chain: Box, - event: &SendPacket, -) -> Result { - let packet = Packet { - sequence: event.envelope.clone().packet_sequence.into(), - source_port: event.envelope.clone().packet_src_port, - source_channel: event.envelope.clone().packet_src_channel, - destination_port: event.envelope.clone().packet_dst_port, - destination_channel: event.envelope.clone().packet_dst_channel, - timeout_height: event.envelope.clone().packet_timeout_height, - timeout_timestamp: event.envelope.clone().packet_timeout_stamp, - data: event.clone().data, - }; + packet: &Packet, + height: Height, +) -> Result { + // Get signer + let signer = dst_chain + .get_signer() + .map_err(|e| Kind::KeyBase.context(e))?; - // TODO - change event types to return ICS height - let event_height = Height::new( - ChainId::chain_version(src_chain.id().to_string().as_str()), - u64::from(event.envelope.height), + let (_, proofs) = src_chain + .build_packet_proofs( + PacketMsgType::Recv, + &packet.source_port, + &packet.source_channel, + packet.sequence, + height, + ) + .map_err(|e| Kind::MalformedProof.context(e))?; + + let msg = MsgRecvPacket::new(packet.clone(), proofs.clone(), signer).map_err(|e| { + Kind::RecvPacket( + packet.source_channel.clone(), + "error while building the recv packet".to_string(), + ) + .context(e) + })?; + + info!( + "built recv_packet msg {}, proofs at height {:?}", + msg.packet, + proofs.height() ); + Ok(msg.to_any::()) +} + +fn build_ack_packet( + dst_chain: Box, + src_chain: Box, + event: &WriteAcknowledgement, + height: Height, +) -> Result { // Get signer let signer = dst_chain .get_signer() .map_err(|e| Kind::KeyBase.context(e))?; - let (_, proof) = src_chain - .proven_packet_commitment( - &event.envelope.packet_src_port, - &event.envelope.packet_src_channel, - event.envelope.packet_sequence, - event_height, + let packet = event.packet.clone(); + let (_, proofs) = src_chain + .build_packet_proofs( + PacketMsgType::Ack, + &packet.destination_port, + &packet.destination_channel, + packet.sequence, + height, ) .map_err(|e| Kind::MalformedProof.context(e))?; - let msg = MsgRecvPacket::new( - packet.clone(), - CommitmentProof::from(proof), - event_height.increment(), - signer, - ) - .map_err(|e| { - Kind::PacketRecv( - packet.source_channel, - "error while building the recv_packet".to_string(), + let msg = MsgAcknowledgement::new(packet.clone(), event.ack.clone(), proofs.clone(), signer) + .map_err(|e| { + Kind::AckPacket( + packet.destination_channel.clone(), + "error while building the ack packet".to_string(), + ) + .context(e) + })?; + + info!( + "built acknowledgment msg {}, proofs at height {:?}", + msg.packet, + proofs.height() + ); + + Ok(msg.to_any::()) +} + +fn build_timeout_packet( + dst_chain: Box, + src_chain: Box, + packet: &Packet, + height: Height, +) -> Result { + // Get signer + let signer = dst_chain + .get_signer() + .map_err(|e| Kind::KeyBase.context(e))?; + + let (_, proofs) = src_chain + .build_packet_proofs( + PacketMsgType::Timeout, + &packet.destination_port, + &packet.destination_channel, + packet.sequence, + height, ) - .context(e) - })?; + .map_err(|e| Kind::MalformedProof.context(e))?; + + println!("timeout for packet {:#?}", packet); + + let msg = + MsgTimeout::new(packet.clone(), packet.sequence, proofs.clone(), signer).map_err(|e| { + Kind::TimeoutPacket( + packet.source_channel.clone(), + "error while building the timeout packet".to_string(), + ) + .context(e) + })?; + + info!( + "built timeout msg {}, proofs at height {:?}", + msg.packet, + proofs.height() + ); + + Ok(msg.to_any::()) +} + +fn build_recv_or_timeout_from_send_packet_event( + dst_chain: Box, + dst_height: Height, + src_chain: Box, + event: &SendPacket, +) -> Result<(Option, Option), Error> { + let packet = event.packet.clone(); + + // TODO - change event types to return ICS height + let event_height = Height::new( + ChainId::chain_version(src_chain.id().to_string().as_str()), + u64::from(event.height), + ); - Ok(msg) + if packet.timeout_height != Height::zero() && packet.timeout_height < dst_height { + Ok(( + None, + Some(build_timeout_packet( + src_chain, + dst_chain.clone(), + &event.packet, + dst_height, + )?), + )) + // } else if packet.timeout_timestamp != 0 && packet.timeout_timestamp < dst_chain.query_time() { + // TODO - add query to get the current chain time + } else { + Ok(( + Some(build_recv_packet( + dst_chain, + src_chain, + &event.packet, + event_height, + )?), + None, + )) + } } -fn build_packet_recv_msgs( +fn build_ack_from_recv_event( dst_chain: Box, src_chain: Box, + event: &WriteAcknowledgement, +) -> Result { + // TODO - change event types to return ICS height + let event_height = Height::new( + ChainId::chain_version(src_chain.id().to_string().as_str()), + u64::from(event.height), + ); + + build_ack_packet(dst_chain, src_chain, &event, event_height) +} + +/// From each sequence it builds either a MsgRecvPacket or a MsgTimeout message +/// MsgTimeout-s are sent to the source chain while MsgRecvPacket-s are sent to +/// the destination chain +fn build_recv_packet_and_timeout_msgs( + packet_src_chain: Box, + src_query_height: Height, + packet_dst_chain: Box, + dst_query_height: Height, src_channel_id: &ChannelId, src_port: &PortId, - src_height: Height, - sequences: &[u64], + sequences: Vec, +) -> Result<(Vec, Vec), Error> { + if sequences.is_empty() { + return Ok((vec![], vec![])); + } + + let mut events = packet_src_chain.query_txs(QueryPacketEventDataRequest { + event_id: IBCEventType::SendPacket, + source_port_id: src_port.clone(), + source_channel_id: src_channel_id.clone(), + sequences, + height: src_query_height, + })?; + + let mut packet_sequences = vec![]; + for event in events.iter() { + let send_event = downcast!(event => IBCEvent::SendPacketChannel) + .ok_or_else(|| Kind::Query.context("unexpected query tx response"))?; + + packet_sequences.append(&mut vec![send_event.packet.sequence]); + } + info!("received from query_txs {:?}", packet_sequences); + + let (mut recv_msgs, mut timeout_msgs) = (vec![], vec![]); + for event in events.iter_mut() { + event.set_height(src_query_height); + + let (recv, timeout) = handle_packet_event( + packet_dst_chain.clone(), + dst_query_height, + packet_src_chain.clone(), + event, + )?; + if let Some(recv) = recv { + recv_msgs.append(&mut vec![recv]); + } + if let Some(timeout) = timeout { + timeout_msgs.append(&mut vec![timeout]); + } + } + Ok((recv_msgs, timeout_msgs)) +} + +fn build_packet_ack_msgs( + packet_src_chain: Box, + packet_dst_chain: Box, + dst_query_height: Height, + src_channel_id: &ChannelId, + src_port: &PortId, + sequences: &[Sequence], ) -> Result, Error> { if sequences.is_empty() { return Ok(vec![]); } - // Set the height of the queries at height - 1 - let query_height = src_height - .decrement() - .map_err(|e| Kind::InvalidHeight.context(e))?; - let mut events = src_chain.query_txs(QueryPacketEventDataRequest { - event_id: IBCEventType::SendPacket, - port_id: src_port.to_string(), - channel_id: src_channel_id.to_string(), + let mut events = packet_dst_chain.query_txs(QueryPacketEventDataRequest { + event_id: IBCEventType::WriteAck, + source_port_id: src_port.clone(), + source_channel_id: src_channel_id.clone(), sequences: Vec::from(sequences), - height: query_height, + height: dst_query_height, })?; let mut packet_sequences = vec![]; for event in events.iter() { - let send_event = downcast!(event => IBCEvent::SendPacketChannel) + let write_ack_event = downcast!(event => IBCEvent::WriteAcknowledgementChannel) .ok_or_else(|| Kind::Query.context("unexpected query tx response"))?; - packet_sequences.append(&mut vec![send_event.envelope.packet_sequence]); + packet_sequences.append(&mut vec![write_ack_event.packet.sequence]); } info!("received from query_txs {:?}", packet_sequences); let mut msgs = vec![]; for event in events.iter_mut() { - event.set_height(query_height); - if let Some(new_msg) = handle_packet_event(dst_chain.clone(), src_chain.clone(), event)? { + event.set_height(dst_query_height); + if let (Some(new_msg), _) = handle_packet_event( + packet_src_chain.clone(), + packet_src_chain.query_latest_height()?, + packet_dst_chain.clone(), + event, + )? { msgs.append(&mut vec![new_msg]); } } Ok(msgs) } -#[derive(Clone, Debug)] -pub struct PacketOptions { - pub dst_chain_config: ChainConfig, - pub src_chain_config: ChainConfig, - pub dst_client_id: ClientId, - pub src_port_id: PortId, - pub src_channel_id: ChannelId, -} - fn target_height_and_sequences_of_recv_packets( - dst_chain: Box, - src_chain: Box, + packet_src_chain: Box, // where timeout and acknowledgment is sent + packet_dst_chain: Box, // where packet_recv is sent opts: &PacketOptions, -) -> Result<(Vec, Height), Error> { - let src_channel = src_chain - .query_channel(&opts.src_port_id, &opts.src_channel_id, Height::default()) - .map_err(|e| { - Kind::PacketRecv( - opts.src_channel_id.clone(), - "source channel does not exist on source".into(), - ) - .context(e) - })?; - - let dst_channel_id = src_channel.counterparty().channel_id.ok_or_else(|| { - Kind::PacketRecv( - opts.src_channel_id.clone(), - "missing counterparty channel id".into(), - ) - })?; +) -> Result<(Vec, Height), Error> { + // Query packet commitments on packet's source chain (sent but not acknowledged) + let pc_request = QueryPacketCommitmentsRequest { + port_id: opts.packet_src_port_id.to_string(), + channel_id: opts.packet_src_channel_id.to_string(), + pagination: None, + }; + let (packet_commitments, query_height) = + packet_src_chain.query_packet_commitments(pc_request)?; + if packet_commitments.is_empty() { + return Ok((vec![], Height::zero())); + } + let commit_sequences = packet_commitments.iter().map(|p| p.sequence).collect(); + info!( + "packets that still have commitments on source {}: {:?}", + packet_src_chain.id(), + commit_sequences + ); - let dst_channel = dst_chain - .query_channel( - &src_channel.counterparty().port_id, - &dst_channel_id, - Height::default(), - ) - .map_err(|e| { - Kind::PacketRecv( - dst_channel_id.clone(), - "channel does not exist on destination chain".into(), - ) - .context(e) - })?; + // Get the packets that have not been received on destination chain + let request = QueryUnreceivedPacketsRequest { + port_id: opts.packet_dst_port_id.to_string(), + channel_id: opts.packet_dst_channel_id.to_string(), + packet_commitment_sequences: commit_sequences, + }; + let sequences_to_send = packet_dst_chain + .query_unreceived_packets(request)? + .into_iter() + .map(From::from) + .collect(); + info!( + "packets to send out of the ones with commitments on source {:?}", + sequences_to_send + ); - if dst_channel.state().clone() != State::Open { - return Err(Kind::PacketRecv( - dst_channel_id, - "channel on destination not in open state".into(), - ) - .into()); - } + Ok((sequences_to_send, query_height)) +} - let pc_request = QueryPacketCommitmentsRequest { - port_id: src_channel.counterparty().port_id.to_string(), - channel_id: opts.src_channel_id.to_string(), +fn target_height_and_sequences_of_ack_packets( + packet_src_chain: Box, + packet_dst_chain: Box, + opts: &PacketOptions, +) -> Result<(Vec, Height), Error> { + // Get the sequences of packets that have been acknowledged on destination + let pc_request = QueryPacketAcknowledgementsRequest { + port_id: opts.packet_dst_port_id.to_string(), + channel_id: opts.packet_dst_channel_id.to_string(), pagination: None, }; + let (acks_on_destination, query_height) = + packet_dst_chain.query_packet_acknowledgements(pc_request)?; - let (packet_commitments, query_height) = src_chain.query_packet_commitments(pc_request)?; - - if packet_commitments.is_empty() { + if acks_on_destination.is_empty() { return Ok((vec![], Height::zero())); } - let mut src_sequences = vec![]; - for pc in packet_commitments.iter() { - src_sequences.append(&mut vec![pc.sequence]); - } + let acked_sequences = acks_on_destination.iter().map(|p| p.sequence).collect(); info!( - "packets that still have commitments on source {:?}", - src_sequences + "packets that have acknowledgments on destination {} {:?}", + packet_dst_chain.id(), + acked_sequences ); - let request = QueryUnreceivedPacketsRequest { - port_id: src_channel.counterparty().port_id.to_string(), - channel_id: dst_channel_id.to_string(), - packet_commitment_sequences: src_sequences, + let request = QueryUnreceivedAcksRequest { + port_id: opts.packet_src_port_id.to_string(), + channel_id: opts.packet_src_channel_id.to_string(), + packet_ack_sequences: acked_sequences, }; - let packets_to_send = dst_chain.query_unreceived_packets(request)?; - + let acks_to_send = packet_src_chain + .query_unreceived_acknowledgement(request)? + .into_iter() + .map(From::from) + .collect(); info!( - "packets to send out of the ones with commitments on source {:?}", - packets_to_send + "acks to send out to {} of the ones with acknowledgments on destination {}: {:?}", + packet_src_chain.id(), + packet_dst_chain.id(), + acks_to_send ); - Ok((packets_to_send, query_height)) + Ok((acks_to_send, query_height)) +} + +fn verify_channel_state( + chain: Box, + port_id: &PortId, + channel_id: &ChannelId, +) -> Result<(), Error> { + // Check the packet's channel on source chain is Open + let channel = chain + .query_channel(port_id, channel_id, Height::default()) + .map_err(|e| { + Kind::Packet( + channel_id.clone(), + format!("channel does not exist on {}", chain.id()), + ) + .context(e) + })?; + + if !channel.state_matches(&State::Open) { + return Err(Kind::AckPacket( + channel_id.clone(), + format!("channel on chain {} not in open state", chain.id()), + ) + .into()); + } + Ok(()) } pub fn build_and_send_recv_packet_messages( - dst_chain: Box, - src_chain: Box, + packet_src_chain: Box, // the chain that sourced the packet and where recv proofs are collected + packet_dst_chain: Box, // the chain where recv is sent and from where ack data and proofs are collected opts: &PacketOptions, -) -> Result, Error> { - let (sequences, height) = - target_height_and_sequences_of_recv_packets(dst_chain.clone(), src_chain.clone(), opts)?; +) -> Result, Error> { + let (sequences, src_query_height) = target_height_and_sequences_of_recv_packets( + packet_src_chain.clone(), + packet_dst_chain.clone(), + opts, + )?; if sequences.is_empty() { - return Ok(vec!["No sent packets on source chain".to_string()]); + return Ok(vec![]); + } + + let dst_query_height = packet_dst_chain.query_latest_height()?; + + let (mut recv_msgs, mut timeout_msgs) = build_recv_packet_and_timeout_msgs( + packet_src_chain.clone(), + src_query_height, + packet_dst_chain.clone(), + dst_query_height, + &opts.packet_src_channel_id, + &opts.packet_src_port_id, + sequences, + )?; + + let mut result = vec![]; + + if !recv_msgs.is_empty() { + // Check the channel on destination chain is Open + verify_channel_state( + packet_dst_chain.clone(), + &opts.packet_dst_port_id, + &opts.packet_dst_channel_id, + )?; + + // Prepend client update and send all recv_packet messages + let mut dst_msgs = build_update_client( + packet_dst_chain.clone(), + packet_src_chain.clone(), + &opts.packet_dst_client_id.clone(), + src_query_height.increment(), + )?; + dst_msgs.append(&mut recv_msgs); + result.append(&mut packet_dst_chain.send_msgs(dst_msgs)?); + } + + if !timeout_msgs.is_empty() { + // Check the channel on source chain is Open + verify_channel_state( + packet_src_chain.clone(), + &opts.packet_src_port_id, + &opts.packet_src_channel_id, + )?; + + // Prepend client update and send all timeout messages + let mut src_msgs = build_update_client( + packet_src_chain.clone(), + packet_dst_chain.clone(), + &opts.packet_src_client_id.clone(), + dst_query_height.increment(), + )?; + src_msgs.append(&mut timeout_msgs); + result.append(&mut packet_src_chain.send_msgs(src_msgs)?); } + Ok(result) +} - let mut msgs = build_update_client( - dst_chain.clone(), - src_chain.clone(), - &opts.dst_client_id.clone(), - height, +pub fn build_and_send_ack_packet_messages( + packet_src_chain: Box, // the chain that sourced the packet and where ack is sent + packet_dst_chain: Box, // the chain from where ack data and proofs are collected + opts: &PacketOptions, +) -> Result, Error> { + let (sequences, dst_query_height) = target_height_and_sequences_of_ack_packets( + packet_src_chain.clone(), + packet_dst_chain.clone(), + opts, )?; + if sequences.is_empty() { + return Ok(vec![]); + } - let mut packet_msgs = build_packet_recv_msgs( - dst_chain.clone(), - src_chain, - &opts.src_channel_id, - &opts.src_port_id, - height, - &sequences, + let mut ack_msgs = build_packet_ack_msgs( + packet_src_chain.clone(), + packet_dst_chain.clone(), + dst_query_height, + &opts.packet_src_channel_id, + &opts.packet_src_port_id, + sequences.as_slice(), )?; - msgs.append(&mut packet_msgs); - Ok(dst_chain.send_msgs(msgs)?) + let mut result = vec![]; + + if !ack_msgs.is_empty() { + // Check the channel on source chain is Open + verify_channel_state( + packet_src_chain.clone(), + &opts.packet_src_port_id, + &opts.packet_src_channel_id, + )?; + + let mut src_msgs = build_update_client( + packet_src_chain.clone(), + packet_dst_chain.clone(), + &opts.packet_src_client_id.clone(), + dst_query_height.increment(), + )?; + + src_msgs.append(&mut ack_msgs); + result.append(&mut packet_src_chain.send_msgs(src_msgs)?); + } + + Ok(result) +} + +#[derive(Clone, Debug)] +pub struct PacketOptions { + pub packet_src_chain_config: ChainConfig, + pub packet_src_client_id: ClientId, + pub packet_src_port_id: PortId, + pub packet_src_channel_id: ChannelId, + pub packet_dst_chain_config: ChainConfig, + pub packet_dst_client_id: ClientId, + pub packet_dst_port_id: PortId, + pub packet_dst_channel_id: ChannelId, } diff --git a/relayer/src/relay.rs b/relayer/src/relay.rs index 45fe952a77..d0a4819c37 100644 --- a/relayer/src/relay.rs +++ b/relayer/src/relay.rs @@ -1,9 +1,10 @@ use anomaly::BoxError; +use tracing::info; use crate::chain::handle::ChainHandle; use crate::channel::{Channel, ChannelConfig}; -use crate::connection::{Connection, ConnectionConfig}; -use crate::foreign_client::{ForeignClient, ForeignClientConfig}; +use crate::connection::Connection; +use crate::foreign_client::ForeignClient; use crate::link::Link; pub(crate) const MAX_ITER: u32 = 10; @@ -11,25 +12,16 @@ pub(crate) const MAX_ITER: u32 = 10; pub fn channel_relay( a_chain_handle: Box, b_chain_handle: Box, - conn_cfg: ConnectionConfig, chan_cfg: ChannelConfig, ) -> Result<(), BoxError> { - // Instantiate the foreign client on the source chain. - let client_on_a = ForeignClient::new( - a_chain_handle.clone(), - b_chain_handle.clone(), - ForeignClientConfig::new(conn_cfg.a_end().chain_id(), conn_cfg.a_end().client_id()), - )?; - - // Instantiate the foreign client on the destination chain. - let client_on_b = ForeignClient::new( - b_chain_handle.clone(), - a_chain_handle.clone(), - ForeignClientConfig::new(conn_cfg.b_end().chain_id(), conn_cfg.b_end().client_id()), - )?; + info!("\nChannel Relay Loop\n"); + + // Instantiate the foreign client on the two chains + let client_on_a = ForeignClient::new(a_chain_handle.clone(), b_chain_handle.clone())?; + let client_on_b = ForeignClient::new(b_chain_handle.clone(), a_chain_handle.clone())?; // Setup the connection between the two chains - let connection = Connection::new(client_on_a, client_on_b, conn_cfg)?; + let connection = Connection::new(client_on_a, client_on_b)?; // Setup the channel over the connection let channel = Channel::new(connection, chan_cfg)?; diff --git a/relayer/tests/config/fixtures/relayer_conf_example.toml b/relayer/tests/config/fixtures/relayer_conf_example.toml index 2e4e540d6a..220e178f5a 100644 --- a/relayer/tests/config/fixtures/relayer_conf_example.toml +++ b/relayer/tests/config/fixtures/relayer_conf_example.toml @@ -9,10 +9,7 @@ grpc_addr = 'tcp://localhost:9090' account_prefix = 'cosmos' key_name = 'testkey' store_prefix = 'ibc' -client_ids = [ - 'clA1', - 'clA2', -] + gas = 200000 max_msg_num = 4 max_tx_size = 1048576 @@ -30,7 +27,6 @@ grpc_addr = 'tcp://localhost:9090' account_prefix = 'cosmos' key_name = 'testkey' store_prefix = 'ibc' -client_ids = ['clB1'] clock_drift = '5s' trusting_period = '14days' @@ -39,30 +35,11 @@ numerator = '1' denominator = '3' [[connections]] -[connections.a_end] -chain_id = 'chain_A' -client_id = 'clB1' -connection_id = 'connAtoB' - -[connections.b_end] -chain_id = 'chain_B' -client_id = 'clA1' -connection_id = 'connBtoA' +a_chain = 'chain_A' +b_chain = 'chain_B' [[connections.paths]] a_port = 'transfer' b_port = 'transfer' -direction = 'unidirectional' -[[connections.paths]] -a_port = 'transfer' -b_port = 'transfer' -direction = 'bidirectional' - -[[connections.paths]] -a_port = 'transfer' -b_port = 'transfer' -a_channel = 'chan3onA' -b_channel = 'chan3onB' -direction = 'bidirectional' diff --git a/relayer/tests/config/fixtures/simple_config.toml b/relayer/tests/config/fixtures/simple_config.toml index 4a9594ec66..bc8f90de41 100644 --- a/relayer/tests/config/fixtures/simple_config.toml +++ b/relayer/tests/config/fixtures/simple_config.toml @@ -13,7 +13,6 @@ strategy = "naive" account_prefix = "cosmos" key_name = "testkey" store_prefix = "ibc" - client_ids = ["ethbridge"] gas = 200000 gas_adjustement = 1.3 gas_price = "0.025stake" @@ -29,7 +28,6 @@ strategy = "naive" account_prefix = "cosmos" key_name = "testkey" store_prefix = "ibc" - client_ids = ["ibconeclient"] gas = 200000 gas_adjustement = 1.3 gas_price = "0.025stake" @@ -39,13 +37,9 @@ strategy = "naive" peer_id = "DC0C0ADEADBEEFC0FFEEFACADEBADFADAD0BEFEE" [[connections]] +a_chain = "ibc1" +b_chain = "ibc0" -[connections.a_end] - chain_id = "ibc1" - client_id = "ibconeclient" - connection_id = "ibconeconnection" - -[connections.b_end] - chain_id = "ibc0" - client_id = "ibczeroclientt" - connection_id = "ibczeroconnection" +[[connections.paths]] +a_port = 'transfer' +b_port = 'transfer'