diff --git a/crates/relayer/src/chain/ckb4ibc/message.rs b/crates/relayer/src/chain/ckb4ibc/message.rs index 436556c8f..40946f5ce 100644 --- a/crates/relayer/src/chain/ckb4ibc/message.rs +++ b/crates/relayer/src/chain/ckb4ibc/message.rs @@ -1,19 +1,23 @@ mod chan; mod client; mod conn; +mod packet; use std::{cell::Ref, collections::HashMap}; -use chan::*; -use conn::*; - use crate::{config::ckb4ibc::ChainConfig, error::Error, keyring::Secp256k1KeyPair}; use ckb_ics_axon::{ handler::{IbcChannel, IbcConnections}, message::Envelope, }; -use ckb_types::core::TransactionView; -use ckb_types::packed::{Byte32, CellInput, OutPoint}; +use ckb_types::{ + core::{Capacity, DepType, TransactionBuilder, TransactionView}, + packed::{ + Byte32, Bytes as PackedBytes, BytesOpt, CellDep, CellInput, CellOutput, OutPoint, Script, + WitnessArgs, + }, + prelude::{Builder, Entity, Pack}, +}; use ibc_proto::google::protobuf::Any; use ibc_relayer_types::{ core::ics02_client::msgs::{ @@ -32,18 +36,17 @@ use ibc_relayer_types::{ ics03_connection::connection::IdentifiedConnectionEnd, ics04_channel::{ msgs::{ - acknowledgement::MsgAcknowledgement, - acknowledgement::TYPE_URL as ACK_TYPE_URL, - chan_close_init::MsgChannelCloseInit, - chan_close_init::TYPE_URL as CHAN_CLOSE_INIT_TYPE_URL, - chan_open_ack::MsgChannelOpenAck, - chan_open_ack::TYPE_URL as CHAN_OPEN_ACK_TYPE_URL, - chan_open_confirm::MsgChannelOpenConfirm, - chan_open_confirm::TYPE_URL as CHAN_OPEN_CONFIRM_TYPE_URL, - chan_open_init::MsgChannelOpenInit, - chan_open_init::TYPE_URL as CHAN_OPEN_INIT_TYPE_URL, - chan_open_try::MsgChannelOpenTry, - chan_open_try::TYPE_URL as CHAN_OPEN_TRY_TYPE_URL, + acknowledgement::{MsgAcknowledgement, TYPE_URL as ACK_PACKET_TYPE_URL}, + chan_close_confirm::{ + MsgChannelCloseConfirm, TYPE_URL as CHANN_CLOSE_CONFIRM_TYPE_URL, + }, + chan_close_init::{MsgChannelCloseInit, TYPE_URL as CHAN_CLOSE_INIT_TYPE_URL}, + chan_open_ack::{MsgChannelOpenAck, TYPE_URL as CHAN_OPEN_ACK_TYPE_URL}, + chan_open_confirm::{ + MsgChannelOpenConfirm, TYPE_URL as CHAN_OPEN_CONFIRM_TYPE_URL, + }, + chan_open_init::{MsgChannelOpenInit, TYPE_URL as CHAN_OPEN_INIT_TYPE_URL}, + chan_open_try::{MsgChannelOpenTry, TYPE_URL as CHAN_OPEN_TRY_TYPE_URL}, recv_packet::{MsgRecvPacket, TYPE_URL as RECV_PACKET_TYPE_URL}, }, packet::Sequence, @@ -55,9 +58,12 @@ use ibc_relayer_types::{ tx_msg::Msg, }; -use self::client::{convert_create_client, convert_update_client}; - use super::utils::{generate_connection_id, get_script_hash}; +use client::{convert_create_client, convert_update_client}; + +use chan::*; +use conn::*; +use packet::*; macro_rules! convert { ($msg:ident, $conval:ident, $msgty:ty, $conv:ident) => {{ @@ -318,9 +324,82 @@ pub fn convert_msg_to_ckb_tx( MsgChannelCloseInit, convert_chan_close_init_to_tx ), + CHANN_CLOSE_CONFIRM_TYPE_URL => convert!( + msg, + converter, + MsgChannelCloseConfirm, + convert_chan_close_confirm_to_tx + ), // packet RECV_PACKET_TYPE_URL => convert!(msg, converter, MsgRecvPacket, convert_recv_packet_to_tx), - ACK_TYPE_URL => convert!(msg, converter, MsgAcknowledgement, convert_ack_packet_to_tx), - _ => todo!(), + ACK_PACKET_TYPE_URL => { + convert!(msg, converter, MsgAcknowledgement, convert_ack_packet_to_tx) + } + _ => Err(Error::other(format!( + "cannot convert ibc_msg: {}", + msg.type_url + ))), + } +} + +pub struct TxBuilder { + builder: TransactionBuilder, +} + +impl From for TxBuilder { + fn from(builder: TransactionBuilder) -> TxBuilder { + TxBuilder { builder } + } +} + +impl Default for TxBuilder { + fn default() -> Self { + TransactionView::new_advanced_builder().into() + } +} + +impl TxBuilder { + pub fn cell_dep(self, outpoint: OutPoint) -> Self { + self.builder + .cell_dep( + CellDep::new_builder() + .dep_type(DepType::Code.into()) + .out_point(outpoint) + .build(), + ) + .into() + } + + pub fn input(self, input: CellInput) -> Self { + self.builder.input(input).into() + } + + pub fn output(self, lock: Script, capacity: Capacity, data: PackedBytes) -> Self { + self.builder + .output( + CellOutput::new_builder() + .lock(lock) + .capacity(capacity.pack()) + .build(), + ) + .output_data(data) + .into() + } + + pub fn witness(self, input_type: BytesOpt, output_type: BytesOpt) -> Self { + self.builder + .witness( + WitnessArgs::new_builder() + .input_type(input_type) + .output_type(output_type) + .build() + .as_bytes() + .pack(), + ) + .into() + } + + pub fn build(self) -> TransactionView { + self.builder.build() } } diff --git a/crates/relayer/src/chain/ckb4ibc/message/chan.rs b/crates/relayer/src/chain/ckb4ibc/message/chan.rs index 93996fec7..d539f32b3 100644 --- a/crates/relayer/src/chain/ckb4ibc/message/chan.rs +++ b/crates/relayer/src/chain/ckb4ibc/message/chan.rs @@ -1,45 +1,87 @@ -use std::str::FromStr; - -use super::{CkbTxInfo, MsgToTxConverter}; -use crate::chain::ckb4ibc::utils::{ - convert_port_id_to_array, convert_proof, extract_client_id_by_connection_id, - generate_channel_id, get_channel_capacity, get_channel_idx, get_client_id_from_channel, - get_client_outpoint, get_connection_capacity, get_connection_lock_script, get_encoded_object, - get_packet_capacity, -}; -use crate::error::Error; -use ckb_ics_axon::consts::{CHANNEL_CELL_CAPACITY, CONNECTION_CELL_CAPACITY, PACKET_CELL_CAPACITY}; - +use ckb_ics_axon::consts::{CHANNEL_CELL_CAPACITY, CONNECTION_CELL_CAPACITY}; use ckb_ics_axon::handler::IbcChannel; -use ckb_ics_axon::handler::IbcPacket; -use ckb_ics_axon::handler::PacketStatus; use ckb_ics_axon::message::Envelope; -use ckb_ics_axon::message::MsgAckPacket as CkbMsgAckPacket; use ckb_ics_axon::message::MsgChannelOpenAck as CkbMsgChannelOpenAck; use ckb_ics_axon::message::MsgChannelOpenConfirm as CkbMsgChannelOpenConfirm; use ckb_ics_axon::message::MsgChannelOpenInit as CkbMsgChannelOpenInit; use ckb_ics_axon::message::MsgChannelOpenTry as CkbMsgChannelOpenTry; -use ckb_ics_axon::message::MsgRecvPacket as CkbMsgRecvPacket; use ckb_ics_axon::message::MsgType; -use ckb_ics_axon::object::Packet as CkbPacket; use ckb_ics_axon::object::{ChannelCounterparty, Ordering as CkbOrdering, State as CkbState}; -use ckb_ics_axon::{ChannelArgs, PacketArgs}; -use ckb_types::core::{DepType, ScriptHashType, TransactionView}; -use ckb_types::packed::CellDep; -use ckb_types::packed::{CellOutput, Script, WitnessArgs}; -use ckb_types::prelude::{Builder, Entity, Pack}; +use ckb_ics_axon::ChannelArgs; +use ckb_types::packed::BytesOpt; use ibc_relayer_types::core::ics04_channel::channel::{ChannelEnd, Order, State}; use ibc_relayer_types::core::ics04_channel::events::{OpenAck, OpenConfirm, OpenInit, OpenTry}; -use ibc_relayer_types::core::ics04_channel::msgs::acknowledgement::MsgAcknowledgement; -use ibc_relayer_types::core::ics04_channel::msgs::recv_packet::MsgRecvPacket; use ibc_relayer_types::core::ics04_channel::msgs::{ - chan_close_init::MsgChannelCloseInit, chan_open_ack::MsgChannelOpenAck, - chan_open_confirm::MsgChannelOpenConfirm, chan_open_init::MsgChannelOpenInit, - chan_open_try::MsgChannelOpenTry, + chan_close_confirm::MsgChannelCloseConfirm, chan_close_init::MsgChannelCloseInit, + chan_open_ack::MsgChannelOpenAck, chan_open_confirm::MsgChannelOpenConfirm, + chan_open_init::MsgChannelOpenInit, chan_open_try::MsgChannelOpenTry, }; -use ibc_relayer_types::core::ics04_channel::packet::Packet; use ibc_relayer_types::core::ics24_host::identifier::{ChannelId, PortId}; use ibc_relayer_types::events::IbcEvent; +use std::str::FromStr; + +use super::{CkbTxInfo, MsgToTxConverter, TxBuilder}; +use crate::chain::ckb4ibc::utils::{ + convert_port_id_to_array, convert_proof, extract_client_id_by_connection_id, + generate_channel_id, get_channel_capacity, get_channel_idx, get_channel_lock_script, + get_client_id_from_channel, get_client_outpoint, get_connection_capacity, + get_connection_lock_script, get_encoded_object, +}; +use crate::error::Error; + +fn convert_channel_end( + channel_end: ChannelEnd, + port_id: PortId, + channel_number: u16, +) -> Result { + let state = match channel_end.state { + State::Uninitialized => CkbState::Unknown, + State::Init => CkbState::Init, + State::TryOpen => CkbState::OpenTry, + State::Open => CkbState::Open, + State::Closed => CkbState::Closed, + }; + let order = match channel_end.ordering { + Order::None => CkbOrdering::Unknown, + Order::Unordered => CkbOrdering::Unordered, + Order::Ordered => CkbOrdering::Ordered, + }; + let port_id = port_id.to_string(); + let connection_hops = channel_end + .connection_hops + .into_iter() + .map(|v| v.to_string()) + .collect::>(); + + if connection_hops.is_empty() { + return Err(Error::empty_connection_hops()); + } + + let remote_port_id = channel_end.remote.port_id.to_string(); + let remote_channel_id = { + if let Some(id) = channel_end.remote.channel_id { + id.to_string() + } else { + String::from("") + } + }; + + let counterparty = ChannelCounterparty { + port_id: remote_port_id, + channel_id: remote_channel_id, + }; + + let result = IbcChannel { + number: channel_number, + port_id, + state, + order, + sequence: Default::default(), + counterparty, + connection_hops, + }; + Ok(result) +} pub fn convert_chan_open_init_to_tx( msg: MsgChannelOpenInit, @@ -53,7 +95,7 @@ pub fn convert_chan_open_init_to_tx( let ibc_channel_end = convert_channel_end(msg.channel.clone(), msg.port_id.clone(), next_channel_num)?; - let ibc_channel_end_encoded = get_encoded_object(ibc_channel_end); + let ibc_channel = get_encoded_object(ibc_channel_end); let envelope = Envelope { msg_type: MsgType::MsgChannelOpenInit, @@ -68,69 +110,27 @@ pub fn convert_chan_open_init_to_tx( port_id: convert_port_id_to_array(&msg.port_id)?, }; - let old_connection_encoded = get_encoded_object(old_connection_cell); - let new_connection_encoded = get_encoded_object(new_connection_cell); - - let client_outpoint = get_client_outpoint(converter, &client_id)?; - let ibc_connection_input = converter.get_ibc_connections_input(&client_id)?; - let connection_lock = get_connection_lock_script(converter.get_config(), Some(client_id))?; - - let packed_tx = TransactionView::new_advanced_builder() - .cell_dep( - CellDep::new_builder() - .dep_type(DepType::Code.into()) - .out_point(client_outpoint) - .build(), - ) - .cell_dep( - CellDep::new_builder() - .dep_type(DepType::Code.into()) - .out_point(converter.get_conn_contract_outpoint()) - .build(), - ) - .cell_dep( - CellDep::new_builder() - .dep_type(DepType::Code.into()) - .out_point(converter.get_chan_contract_outpoint()) - .build(), - ) - .input(ibc_connection_input) + let old_connection = get_encoded_object(old_connection_cell); + let new_connection = get_encoded_object(new_connection_cell); + let connection_lock = + get_connection_lock_script(converter.get_config(), Some(client_id.clone()))?; + let channel_lock = get_channel_lock_script(converter, channel_args.to_args()); + + let packed_tx = TxBuilder::default() + .cell_dep(get_client_outpoint(converter, &client_id)?) + .cell_dep(converter.get_conn_contract_outpoint()) + .cell_dep(converter.get_chan_contract_outpoint()) + .input(converter.get_ibc_connections_input(&client_id)?) .output( - CellOutput::new_builder() - .lock(connection_lock) - .capacity(get_connection_capacity().pack()) - .build(), - ) - .output( - CellOutput::new_builder() - .lock( - Script::new_builder() - .code_hash(converter.get_channel_code_hash()) - .args(channel_args.to_args().pack()) - .hash_type(ScriptHashType::Type.into()) - .build(), - ) - .capacity(get_channel_capacity().pack()) - .build(), - ) - .output_data(new_connection_encoded.data) - .output_data(ibc_channel_end_encoded.data) - .witness( - WitnessArgs::new_builder() - .input_type(old_connection_encoded.witness) - .output_type(new_connection_encoded.witness) - .build() - .as_bytes() - .pack(), - ) - .witness( - WitnessArgs::new_builder() - .output_type(ibc_channel_end_encoded.witness) - .build() - .as_bytes() - .pack(), + connection_lock, + get_connection_capacity(), + new_connection.data, ) + .output(channel_lock, get_channel_capacity(), ibc_channel.data) + .witness(old_connection.witness, new_connection.witness) + .witness(BytesOpt::default(), ibc_channel.witness) .build(); + let event = IbcEvent::OpenInitChannel(OpenInit { port_id: msg.port_id, channel_id: Some(generate_channel_id(next_channel_num)), @@ -138,6 +138,7 @@ pub fn convert_chan_open_init_to_tx( counterparty_port_id: msg.channel.remote.port_id, counterparty_channel_id: msg.channel.remote.channel_id, }); + Ok(CkbTxInfo { unsigned_tx: Some(packed_tx), envelope, @@ -158,11 +159,11 @@ pub fn convert_chan_open_try_to_tx( let ibc_channel_end = convert_channel_end(msg.channel.clone(), msg.port_id.clone(), next_channel_num)?; - let ibc_channel_end_encoded = get_encoded_object(ibc_channel_end); + let ibc_channel = get_encoded_object(ibc_channel_end); let (client_cell_type_args, client_id) = get_client_id_from_channel(&msg.channel, converter)?; - let old_connection_encoded = get_encoded_object(old_connection_cell); - let new_connection_encoded = get_encoded_object(new_connection_cell); + let old_connection = get_encoded_object(old_connection_cell); + let new_connection = get_encoded_object(new_connection_cell); let envelope = Envelope { msg_type: MsgType::MsgChannelOpenTry, @@ -178,60 +179,24 @@ pub fn convert_chan_open_try_to_tx( channel_id: next_channel_num, port_id: convert_port_id_to_array(&msg.port_id)?, }; - let ibc_connection_input = converter.get_ibc_connections_input(&client_id)?; - let client_outpoint = get_client_outpoint(converter, &client_id)?; - let connection_lock = get_connection_lock_script(converter.get_config(), Some(client_id))?; - - let packed_tx = TransactionView::new_advanced_builder() - .cell_dep( - CellDep::new_builder() - .dep_type(DepType::Code.into()) - .out_point(client_outpoint) - .build(), - ) - .cell_dep( - CellDep::new_builder() - .dep_type(DepType::Code.into()) - .out_point(converter.get_conn_contract_outpoint()) - .build(), - ) - .input(ibc_connection_input) - .output( - CellOutput::new_builder() - .lock(connection_lock) - .capacity(get_connection_capacity().pack()) - .build(), - ) + let connection_lock = + get_connection_lock_script(converter.get_config(), Some(client_id.clone()))?; + let channel_lock = get_channel_lock_script(converter, channel_args.to_args()); + + let packed_tx = TxBuilder::default() + .cell_dep(get_client_outpoint(converter, &client_id)?) + .cell_dep(converter.get_conn_contract_outpoint()) + .input(converter.get_ibc_connections_input(&client_id)?) .output( - CellOutput::new_builder() - .lock( - Script::new_builder() - .code_hash(converter.get_channel_code_hash()) - .args(channel_args.to_args().pack()) - .hash_type(ScriptHashType::Type.into()) - .build(), - ) - .capacity(get_channel_capacity().pack()) - .build(), - ) - .output_data(new_connection_encoded.data) - .output_data(ibc_channel_end_encoded.data) - .witness( - WitnessArgs::new_builder() - .input_type(old_connection_encoded.witness) - .output_type(new_connection_encoded.witness) - .build() - .as_bytes() - .pack(), - ) - .witness( - WitnessArgs::new_builder() - .output_type(ibc_channel_end_encoded.witness) - .build() - .as_bytes() - .pack(), + connection_lock, + get_connection_capacity(), + new_connection.data, ) + .output(channel_lock, get_channel_capacity(), ibc_channel.data) + .witness(old_connection.witness, new_connection.witness) + .witness(BytesOpt::default(), ibc_channel.witness) .build(); + let event = IbcEvent::OpenTryChannel(OpenTry { port_id: msg.port_id, channel_id: Some(generate_channel_id(next_channel_num)), @@ -239,6 +204,7 @@ pub fn convert_chan_open_try_to_tx( counterparty_port_id: msg.channel.remote.port_id, counterparty_channel_id: msg.channel.remote.channel_id, }); + Ok(CkbTxInfo { unsigned_tx: Some(packed_tx), envelope, @@ -276,49 +242,17 @@ pub fn convert_chan_open_ack_to_tx( port_id: convert_port_id_to_array(&msg.port_id)?, }; - let old_channel_encoded = get_encoded_object(old_channel); - let new_channel_encoded = get_encoded_object(new_channel); + let channel_lock = get_channel_lock_script(converter, channel_args.to_args()); + let old_channel = get_encoded_object(old_channel); + let new_channel = get_encoded_object(new_channel); - let packed_tx = TransactionView::new_advanced_builder() - .cell_dep( - CellDep::new_builder() - .out_point(get_client_outpoint(converter, &client_id)?) - .build(), - ) - .cell_dep( - CellDep::new_builder() - .dep_type(DepType::Code.into()) - .out_point(converter.get_conn_contract_outpoint()) - .build(), - ) - .cell_dep( - CellDep::new_builder() - .dep_type(DepType::Code.into()) - .out_point(converter.get_chan_contract_outpoint()) - .build(), - ) + let packed_tx = TxBuilder::default() + .cell_dep(get_client_outpoint(converter, &client_id)?) + .cell_dep(converter.get_conn_contract_outpoint()) + .cell_dep(converter.get_chan_contract_outpoint()) .input(converter.get_ibc_channel_input(&msg.channel_id, &msg.port_id)) - .output( - CellOutput::new_builder() - .lock( - Script::new_builder() - .code_hash(converter.get_channel_code_hash()) - .args(channel_args.to_args().pack()) - .hash_type(ScriptHashType::Type.into()) - .build(), - ) - .capacity(get_channel_capacity().pack()) - .build(), - ) - .output_data(new_channel_encoded.data) - .witness( - WitnessArgs::new_builder() - .input_type(old_channel_encoded.witness) - .output_type(new_channel_encoded.witness) - .build() - .as_bytes() - .pack(), - ) + .output(channel_lock, get_channel_capacity(), new_channel.data) + .witness(old_channel.witness, new_channel.witness) .build(); let event = IbcEvent::OpenAckChannel(OpenAck { @@ -328,6 +262,7 @@ pub fn convert_chan_open_ack_to_tx( counterparty_port_id, counterparty_channel_id: Some(msg.counterparty_channel_id), }); + Ok(CkbTxInfo { unsigned_tx: Some(packed_tx), envelope, @@ -360,51 +295,25 @@ pub fn convert_chan_open_confirm_to_tx( let connection_id = old_channel.connection_hops[0].clone(); let (client_cell_type_args, client_id) = extract_client_id_by_connection_id(&connection_id, converter)?; - let lock_args = ChannelArgs { + let channel_args = ChannelArgs { client_id: client_cell_type_args, open: true, channel_id: get_channel_idx(&msg.channel_id)?, port_id: convert_port_id_to_array(&msg.port_id)?, }; - let old_channel_encoded = get_encoded_object(old_channel); - let new_channel_encoded = get_encoded_object(new_channel); + let channel_lock = get_channel_lock_script(converter, channel_args.to_args()); + let old_channel = get_encoded_object(old_channel); + let new_channel = get_encoded_object(new_channel); - let packed_tx = TransactionView::new_advanced_builder() - .cell_dep( - CellDep::new_builder() - .out_point(get_client_outpoint(converter, &client_id)?) - .build(), - ) - .cell_dep( - CellDep::new_builder() - .dep_type(DepType::Code.into()) - .out_point(converter.get_chan_contract_outpoint()) - .build(), - ) + let packed_tx = TxBuilder::default() + .cell_dep(get_client_outpoint(converter, &client_id)?) + .cell_dep(converter.get_chan_contract_outpoint()) .input(converter.get_ibc_channel_input(&msg.channel_id, &msg.port_id)) - .output( - CellOutput::new_builder() - .lock( - Script::new_builder() - .code_hash(converter.get_channel_code_hash()) - .hash_type(ScriptHashType::Type.into()) - .args(lock_args.to_args().pack()) - .build(), - ) - .capacity(get_channel_capacity().pack()) - .build(), - ) - .output_data(new_channel_encoded.data) - .witness( - WitnessArgs::new_builder() - .input_type(old_channel_encoded.witness) - .output_type(new_channel_encoded.witness) - .build() - .as_bytes() - .pack(), - ) + .output(channel_lock, get_channel_capacity(), new_channel.data) + .witness(old_channel.witness, new_channel.witness) .build(); + let event = IbcEvent::OpenConfirmChannel(OpenConfirm { port_id: msg.port_id, channel_id: Some(msg.channel_id), @@ -412,6 +321,7 @@ pub fn convert_chan_open_confirm_to_tx( counterparty_port_id, counterparty_channel_id: Some(counterparty_channel_id), }); + Ok(CkbTxInfo { unsigned_tx: Some(packed_tx), envelope, @@ -427,297 +337,9 @@ pub fn convert_chan_close_init_to_tx( todo!() } -pub fn convert_ack_packet_to_tx( - msg: MsgAcknowledgement, - converter: &C, -) -> Result { - let channel_id = msg.packet.source_channel.clone(); - let old_channel_end = converter.get_ibc_channel(&channel_id); - let mut new_channel_end = old_channel_end.clone(); - new_channel_end.sequence.next_sequence_acks += 1; - let old_channel_end_encoded = get_encoded_object(old_channel_end); - let new_channel_end_encoded = get_encoded_object(new_channel_end.clone()); - - let ckb_msg = CkbMsgAckPacket { - proofs: convert_proof(msg.proofs)?, - acknowledgement: msg.acknowledgement.as_ref().to_vec(), - }; - let envelope = Envelope { - msg_type: MsgType::MsgAckPacket, - content: rlp::encode(&ckb_msg).to_vec(), - }; - let port_id = msg.packet.source_port.clone(); - - let channel_input = converter.get_ibc_channel_input(&channel_id, &msg.packet.source_port); - let sequence = msg.packet.sequence; - let packet = convert_ibc_packet(msg.packet); - let seq = packet.sequence; - let new_ibc_packet = IbcPacket { - packet, - tx_hash: None, - status: PacketStatus::Ack, - }; - let new_ibc_packet_encoded = get_encoded_object(new_ibc_packet); - let old_ibc_packet_input = - converter.get_packet_cell_input(channel_id.clone(), port_id.clone(), sequence); - let channel_idx = get_channel_idx(&channel_id)?; - let port_id_in_args: [u8; 32] = port_id.as_bytes().try_into().unwrap(); - - let (client_cell_type_args, client_id) = - extract_client_id_by_connection_id(&new_channel_end.connection_hops[0], converter)?; - let channel_args = ChannelArgs { - client_id: client_cell_type_args, - open: true, - channel_id: channel_idx, - port_id: port_id_in_args, - }; - - let packed_tx = TransactionView::new_advanced_builder() - .cell_dep( - CellDep::new_builder() - .out_point(get_client_outpoint(converter, &client_id)?) - .build(), - ) - .cell_dep( - CellDep::new_builder() - .dep_type(DepType::Code.into()) - .out_point(converter.get_chan_contract_outpoint()) - .build(), - ) - .input(channel_input) - .input(old_ibc_packet_input) - .output( - CellOutput::new_builder() - .lock( - Script::new_builder() - .code_hash(converter.get_channel_code_hash()) - .hash_type(ScriptHashType::Type.into()) - .args(channel_args.to_args().pack()) - .hash_type(ScriptHashType::Type.into()) - .build(), - ) - .capacity(get_channel_capacity().pack()) - .build(), - ) - .output_data(new_channel_end_encoded.data) - .output( - CellOutput::new_builder() - .lock( - Script::new_builder() - .code_hash(converter.get_packet_code_hash()) - .args( - PacketArgs { - channel_id: channel_idx, - port_id: port_id_in_args, - sequence: seq, - } - .to_args() - .pack(), - ) // todo - .build(), - ) - .capacity(get_packet_capacity().pack()) - .build(), - ) - .output_data(new_ibc_packet_encoded.data) - .witness( - WitnessArgs::new_builder() - .input_type(old_channel_end_encoded.witness) - .output_type(new_channel_end_encoded.witness) - .build() - .as_bytes() - .pack(), - ) - .witness( - WitnessArgs::new_builder() - .output_type(new_ibc_packet_encoded.witness) - .build() - .as_bytes() - .pack(), - ) - .build(); - Ok(CkbTxInfo { - unsigned_tx: Some(packed_tx), - envelope, - input_capacity: CHANNEL_CELL_CAPACITY + PACKET_CELL_CAPACITY, - event: None, - }) -} - -pub fn convert_recv_packet_to_tx( - msg: MsgRecvPacket, - converter: &C, +pub fn convert_chan_close_confirm_to_tx( + _msg: MsgChannelCloseConfirm, + _converter: &C, ) -> Result { - let channel_id = msg.packet.destination_channel.clone(); - let old_channel_end = converter.get_ibc_channel(&channel_id); - let mut new_channel_end = old_channel_end.clone(); - new_channel_end.sequence.next_sequence_recvs += 1; - - let old_channel_end_encoded = get_encoded_object(old_channel_end); - let new_channel_end_encoded = get_encoded_object(new_channel_end.clone()); - - let ckb_msg = CkbMsgRecvPacket { - proofs: convert_proof(msg.proofs)?, - }; - let envelope = Envelope { - msg_type: MsgType::MsgRecvPacket, - content: rlp::encode(&ckb_msg).to_vec(), - }; - let port_id = msg.packet.destination_port.clone(); - - let channel_input = converter.get_ibc_channel_input(&channel_id, &msg.packet.source_port); - let packet = convert_ibc_packet(msg.packet); - let seq = packet.sequence; - let ibc_packet = IbcPacket { - packet, - tx_hash: None, - status: PacketStatus::Recv, - }; - let ibc_packet_encoded = get_encoded_object(ibc_packet); - let channel_idx = get_channel_idx(&channel_id)?; - let port_id_in_args: [u8; 32] = port_id.as_str().as_bytes().try_into().unwrap(); - - let (client_cell_type_args, client_id) = - extract_client_id_by_connection_id(&new_channel_end.connection_hops[0], converter)?; - let channel_args = ChannelArgs { - client_id: client_cell_type_args, - open: true, - channel_id: channel_idx, - port_id: port_id_in_args, - }; - - let packed_tx = TransactionView::new_advanced_builder() - .cell_dep( - CellDep::new_builder() - .out_point(get_client_outpoint(converter, &client_id)?) - .build(), - ) - .input(channel_input) - .output( - CellOutput::new_builder() - .lock( - Script::new_builder() - .code_hash(converter.get_channel_code_hash()) - .hash_type(ScriptHashType::Type.into()) - .args(channel_args.to_args().pack()) - .build(), - ) - .capacity(get_channel_capacity().pack()) - .build(), - ) - .output_data(new_channel_end_encoded.data) - .output( - CellOutput::new_builder() - .lock( - Script::new_builder() - .code_hash(converter.get_packet_code_hash()) - .args( - PacketArgs { - channel_id: channel_idx, - port_id: port_id_in_args, - sequence: seq, - } - .to_args() - .pack(), - ) // todo - .build(), - ) - .capacity(get_packet_capacity().pack()) - .build(), - ) - .output_data(ibc_packet_encoded.data) - .witness( - WitnessArgs::new_builder() - .input_type(old_channel_end_encoded.witness) - .output_type(new_channel_end_encoded.witness) - .build() - .as_bytes() - .pack(), - ) - .witness( - WitnessArgs::new_builder() - .output_type(ibc_packet_encoded.witness) - .build() - .as_bytes() - .pack(), - ) - .build(); - Ok(CkbTxInfo { - unsigned_tx: Some(packed_tx), - envelope, - input_capacity: PACKET_CELL_CAPACITY, - event: None, - }) -} - -pub fn convert_channel_end( - channel_end: ChannelEnd, - port_id: PortId, - channel_number: u16, -) -> Result { - let state = match channel_end.state { - State::Uninitialized => CkbState::Unknown, - State::Init => CkbState::Init, - State::TryOpen => CkbState::OpenTry, - State::Open => CkbState::Open, - State::Closed => CkbState::Closed, - }; - let order = match channel_end.ordering { - Order::None => CkbOrdering::Unknown, - Order::Unordered => CkbOrdering::Unordered, - Order::Ordered => CkbOrdering::Ordered, - }; - let port_id = port_id.to_string(); - let connection_hops = channel_end - .connection_hops - .into_iter() - .map(|v| v.to_string()) - .collect::>(); - - if connection_hops.is_empty() { - return Err(Error::empty_connection_hops()); - } - - let remote_port_id = channel_end.remote.port_id.to_string(); - let remote_channel_id = { - if let Some(id) = channel_end.remote.channel_id { - id.to_string() - } else { - String::from("") - } - }; - - let counterparty = ChannelCounterparty { - port_id: remote_port_id, - channel_id: remote_channel_id, - }; - - let result = IbcChannel { - number: channel_number, - port_id, - state, - order, - sequence: Default::default(), - counterparty, - connection_hops, - }; - Ok(result) -} - -pub fn convert_ibc_packet(packet: Packet) -> CkbPacket { - let seq: u64 = packet.sequence.into(); - let source_port_id = packet.source_port.to_string(); - let source_channel_id = packet.source_channel.to_string(); - let destination_port_id = packet.destination_port.to_string(); - let destination_channel_id = packet.destination_channel.to_string(); - CkbPacket { - sequence: seq as u16, - source_port_id, - source_channel_id, - destination_port_id, - destination_channel_id, - data: packet.data, - timeout_height: packet.timeout_height.commitment_revision_height(), - timeout_timestamp: packet.timeout_timestamp.nanoseconds() / 100000, // use second as unit - } + todo!() } diff --git a/crates/relayer/src/chain/ckb4ibc/message/conn.rs b/crates/relayer/src/chain/ckb4ibc/message/conn.rs index 61c63d899..7c094963d 100644 --- a/crates/relayer/src/chain/ckb4ibc/message/conn.rs +++ b/crates/relayer/src/chain/ckb4ibc/message/conn.rs @@ -15,11 +15,6 @@ use ckb_ics_axon::{ }, object::{ConnectionCounterparty, ConnectionEnd as CkbConnectionEnd, State}, }; -use ckb_types::{ - core::{DepType, TransactionView}, - packed::{CellDep, CellOutput, WitnessArgs}, - prelude::{Builder, Entity, Pack}, -}; use ibc_relayer_types::{ core::ics03_connection::{ events::{Attributes, OpenAck, OpenConfirm, OpenInit, OpenTry}, @@ -31,7 +26,7 @@ use ibc_relayer_types::{ events::IbcEvent, }; -use super::{CkbTxInfo, MsgToTxConverter}; +use super::{CkbTxInfo, MsgToTxConverter, TxBuilder}; pub fn convert_conn_open_init_to_tx( msg: MsgConnectionOpenInit, @@ -64,47 +59,30 @@ pub fn convert_conn_open_init_to_tx( content: rlp::encode(&CkbMsgConnectionOpenInit {}).to_vec(), }; - let old_connection_encoded = get_encoded_object(old_ibc_connection_cell); - let new_connection_encoded = get_encoded_object(new_ibc_connection_cell); + let old_connection = get_encoded_object(old_ibc_connection_cell); + let new_connection = get_encoded_object(new_ibc_connection_cell); let connection_lock = get_connection_lock_script(converter.get_config(), Some(client_id.clone()))?; - let packed_tx = TransactionView::new_advanced_builder() - .cell_dep( - CellDep::new_builder() - .dep_type(DepType::Code.into()) - .out_point(get_client_outpoint(converter, &client_id)?) - .build(), - ) - .cell_dep( - CellDep::new_builder() - .dep_type(DepType::Code.into()) - .out_point(converter.get_conn_contract_outpoint()) - .build(), - ) + let packed_tx = TxBuilder::default() + .cell_dep(get_client_outpoint(converter, &client_id)?) + .cell_dep(converter.get_conn_contract_outpoint()) .input(converter.get_ibc_connections_input(&client_id)?) .output( - CellOutput::new_builder() - .lock(connection_lock) - .capacity(get_connection_capacity().pack()) - .build(), - ) - .output_data(new_connection_encoded.data) - .witness( - WitnessArgs::new_builder() - .input_type(old_connection_encoded.witness) - .output_type(new_connection_encoded.witness) - .build() - .as_bytes() - .pack(), + connection_lock, + get_connection_capacity(), + new_connection.data, ) + .witness(old_connection.witness, new_connection.witness) .build(); + let event = IbcEvent::OpenInitConnection(OpenInit(Attributes { connection_id: Some(generate_connection_id(this_conn_idx, &client_id)), client_id: msg.client_id, counterparty_connection_id: None, counterparty_client_id: msg.counterparty.client_id().clone(), })); + Ok(CkbTxInfo { unsigned_tx: Some(packed_tx), envelope, @@ -118,7 +96,6 @@ pub fn convert_conn_open_try_to_tx( converter: &C, ) -> Result { let client_id = msg.client_id.to_string(); - let remote_client_id = msg.counterparty.client_id().to_string(); let remote_conn_id = msg.counterparty.connection_id.clone().unwrap(); @@ -151,47 +128,30 @@ pub fn convert_conn_open_try_to_tx( .to_vec(), }; - let old_connection_encoded = get_encoded_object(old_ibc_connection_cell); - let new_connection_encoded = get_encoded_object(new_ibc_connection_cell); + let old_connection = get_encoded_object(old_ibc_connection_cell); + let new_connection = get_encoded_object(new_ibc_connection_cell); let connection_lock = get_connection_lock_script(converter.get_config(), Some(client_id.clone()))?; - let packed_tx = TransactionView::new_advanced_builder() - .cell_dep( - CellDep::new_builder() - .dep_type(DepType::Code.into()) - .out_point(get_client_outpoint(converter, &client_id)?) - .build(), - ) - .cell_dep( - CellDep::new_builder() - .dep_type(DepType::Code.into()) - .out_point(converter.get_conn_contract_outpoint()) - .build(), - ) + let packed_tx = TxBuilder::default() + .cell_dep(get_client_outpoint(converter, &client_id)?) + .cell_dep(converter.get_conn_contract_outpoint()) .input(converter.get_ibc_connections_input(&client_id)?) .output( - CellOutput::new_builder() - .lock(connection_lock) - .capacity(get_connection_capacity().pack()) - .build(), - ) - .output_data(new_connection_encoded.data) - .witness( - WitnessArgs::new_builder() - .input_type(old_connection_encoded.witness) - .output_type(new_connection_encoded.witness) - .build() - .as_bytes() - .pack(), + connection_lock, + get_connection_capacity(), + new_connection.data, ) + .witness(old_connection.witness, new_connection.witness) .build(); + let event = IbcEvent::OpenTryConnection(OpenTry(Attributes { connection_id: Some(generate_connection_id(this_conn_idx, &client_id)), client_id: msg.client_id, counterparty_connection_id: msg.counterparty.connection_id.clone(), counterparty_client_id: msg.counterparty.client_id().clone(), })); + Ok(CkbTxInfo { unsigned_tx: Some(packed_tx), envelope, @@ -225,47 +185,30 @@ pub fn convert_conn_open_ack_to_tx( let counterparty_client_id = connection_end.counterparty.client_id.clone(); let client_id = connection_end.client_id.clone(); - let old_connection_encoded = get_encoded_object(old_ibc_connection_cell); - let new_connection_encoded = get_encoded_object(new_ibc_connection_cell); + let old_connection = get_encoded_object(old_ibc_connection_cell); + let new_connection = get_encoded_object(new_ibc_connection_cell); let connection_lock = get_connection_lock_script(converter.get_config(), Some(client_id.clone()))?; - let packed_tx = TransactionView::new_advanced_builder() - .cell_dep( - CellDep::new_builder() - .dep_type(DepType::Code.into()) - .out_point(get_client_outpoint(converter, &client_id)?) - .build(), - ) - .cell_dep( - CellDep::new_builder() - .dep_type(DepType::Code.into()) - .out_point(converter.get_conn_contract_outpoint()) - .build(), - ) + let packed_tx = TxBuilder::default() + .cell_dep(get_client_outpoint(converter, &client_id)?) + .cell_dep(converter.get_conn_contract_outpoint()) .input(converter.get_ibc_connections_input(&client_id)?) .output( - CellOutput::new_builder() - .lock(connection_lock) - .capacity(get_connection_capacity().pack()) - .build(), - ) - .output_data(new_connection_encoded.data) - .witness( - WitnessArgs::new_builder() - .input_type(old_connection_encoded.witness) - .output_type(new_connection_encoded.witness) - .build() - .as_bytes() - .pack(), + connection_lock, + get_connection_capacity(), + new_connection.data, ) + .witness(old_connection.witness, new_connection.witness) .build(); + let event = IbcEvent::OpenAckConnection(OpenAck(Attributes { connection_id: Some(msg.connection_id), client_id: client_id.parse().unwrap(), counterparty_connection_id: Some(msg.counterparty_connection_id), counterparty_client_id: counterparty_client_id.parse().unwrap(), })); + Ok(CkbTxInfo { unsigned_tx: Some(packed_tx), envelope, @@ -303,47 +246,30 @@ pub fn convert_conn_open_confirm_to_tx( .map(|v| v.parse().unwrap()); let client_id = connection_end.client_id.clone(); - let old_connection_encoded = get_encoded_object(old_ibc_connection_cell); - let new_connection_encoded = get_encoded_object(new_ibc_connection_cell); + let old_connection = get_encoded_object(old_ibc_connection_cell); + let new_connection = get_encoded_object(new_ibc_connection_cell); let connection_lock = get_connection_lock_script(converter.get_config(), Some(client_id.clone()))?; - let packed_tx = TransactionView::new_advanced_builder() - .cell_dep( - CellDep::new_builder() - .dep_type(DepType::Code.into()) - .out_point(get_client_outpoint(converter, &client_id)?) - .build(), - ) - .cell_dep( - CellDep::new_builder() - .dep_type(DepType::Code.into()) - .out_point(converter.get_conn_contract_outpoint()) - .build(), - ) + let packed_tx = TxBuilder::default() + .cell_dep(get_client_outpoint(converter, &client_id)?) + .cell_dep(converter.get_conn_contract_outpoint()) .input(converter.get_ibc_connections_input(&client_id)?) .output( - CellOutput::new_builder() - .lock(connection_lock) - .capacity(get_connection_capacity().pack()) - .build(), - ) - .output_data(new_connection_encoded.data) - .witness( - WitnessArgs::new_builder() - .input_type(old_connection_encoded.witness) - .output_type(new_connection_encoded.witness) - .build() - .as_slice() - .pack(), + connection_lock, + get_connection_capacity(), + new_connection.data, ) + .witness(old_connection.witness, new_connection.witness) .build(); + let event = IbcEvent::OpenConfirmConnection(OpenConfirm(Attributes { connection_id: Some(msg.connection_id), client_id: client_id.parse().unwrap(), counterparty_connection_id, counterparty_client_id: counterparty_client_id.parse().unwrap(), })); + Ok(CkbTxInfo { unsigned_tx: Some(packed_tx), envelope, diff --git a/crates/relayer/src/chain/ckb4ibc/message/packet.rs b/crates/relayer/src/chain/ckb4ibc/message/packet.rs new file mode 100644 index 000000000..f1c5d9561 --- /dev/null +++ b/crates/relayer/src/chain/ckb4ibc/message/packet.rs @@ -0,0 +1,178 @@ +use ckb_ics_axon::consts::{CHANNEL_CELL_CAPACITY, PACKET_CELL_CAPACITY}; +use ckb_ics_axon::handler::IbcPacket; +use ckb_ics_axon::handler::PacketStatus; +use ckb_ics_axon::message::Envelope; +use ckb_ics_axon::message::MsgAckPacket as CkbMsgAckPacket; +use ckb_ics_axon::message::MsgRecvPacket as CkbMsgRecvPacket; +use ckb_ics_axon::message::MsgType; +use ckb_ics_axon::object::Packet as CkbPacket; +use ckb_ics_axon::{ChannelArgs, PacketArgs}; +use ckb_types::packed::BytesOpt; +use ibc_relayer_types::core::ics04_channel::msgs::acknowledgement::MsgAcknowledgement; +use ibc_relayer_types::core::ics04_channel::msgs::recv_packet::MsgRecvPacket; +use ibc_relayer_types::core::ics04_channel::packet::Packet; + +use super::{CkbTxInfo, MsgToTxConverter, TxBuilder}; +use crate::chain::ckb4ibc::utils::{ + convert_proof, extract_client_id_by_connection_id, get_channel_capacity, get_channel_idx, + get_channel_lock_script, get_client_outpoint, get_encoded_object, get_packet_capacity, + get_packet_lock_script, +}; +use crate::error::Error; + +fn convert_ibc_packet(packet: Packet) -> CkbPacket { + let seq: u64 = packet.sequence.into(); + let source_port_id = packet.source_port.to_string(); + let source_channel_id = packet.source_channel.to_string(); + let destination_port_id = packet.destination_port.to_string(); + let destination_channel_id = packet.destination_channel.to_string(); + CkbPacket { + sequence: seq as u16, + source_port_id, + source_channel_id, + destination_port_id, + destination_channel_id, + data: packet.data, + timeout_height: packet.timeout_height.commitment_revision_height(), + timeout_timestamp: packet.timeout_timestamp.nanoseconds() / 100000, // use second as unit + } +} + +pub fn convert_ack_packet_to_tx( + msg: MsgAcknowledgement, + converter: &C, +) -> Result { + let channel_id = msg.packet.source_channel.clone(); + let old_channel_end = converter.get_ibc_channel(&channel_id); + let mut new_channel_end = old_channel_end.clone(); + new_channel_end.sequence.next_sequence_acks += 1; + let old_channel = get_encoded_object(old_channel_end); + let new_channel = get_encoded_object(new_channel_end.clone()); + + let ckb_msg = CkbMsgAckPacket { + proofs: convert_proof(msg.proofs)?, + acknowledgement: msg.acknowledgement.as_ref().to_vec(), + }; + let envelope = Envelope { + msg_type: MsgType::MsgAckPacket, + content: rlp::encode(&ckb_msg).to_vec(), + }; + let port_id = msg.packet.source_port.clone(); + + let channel_input = converter.get_ibc_channel_input(&channel_id, &msg.packet.source_port); + let sequence = msg.packet.sequence; + let packet = convert_ibc_packet(msg.packet); + let seq = packet.sequence; + let new_ibc_packet = IbcPacket { + packet, + tx_hash: None, + status: PacketStatus::Ack, + }; + let new_packet = get_encoded_object(new_ibc_packet); + let old_ibc_packet_input = + converter.get_packet_cell_input(channel_id.clone(), port_id.clone(), sequence); + let channel_idx = get_channel_idx(&channel_id)?; + let port_id_in_args: [u8; 32] = port_id.as_bytes().try_into().unwrap(); + + let (client_cell_type_args, client_id) = + extract_client_id_by_connection_id(&new_channel_end.connection_hops[0], converter)?; + let channel_args = ChannelArgs { + client_id: client_cell_type_args, + open: true, + channel_id: channel_idx, + port_id: port_id_in_args, + }; + let packet_args = PacketArgs { + channel_id: channel_idx, + port_id: port_id_in_args, + sequence: seq, + }; + + let channel_lock = get_channel_lock_script(converter, channel_args.to_args()); + let packet_lock = get_packet_lock_script(converter, packet_args.to_args()); + + let packed_tx = TxBuilder::default() + .cell_dep(get_client_outpoint(converter, &client_id)?) + .cell_dep(converter.get_chan_contract_outpoint()) + .input(channel_input) + .input(old_ibc_packet_input) + .output(channel_lock, get_channel_capacity(), new_channel.data) + .output(packet_lock, get_packet_capacity(), new_packet.data) + .witness(old_channel.witness, new_channel.witness) + .witness(BytesOpt::default(), new_packet.witness) + .build(); + + Ok(CkbTxInfo { + unsigned_tx: Some(packed_tx), + envelope, + input_capacity: CHANNEL_CELL_CAPACITY + PACKET_CELL_CAPACITY, + event: None, + }) +} + +pub fn convert_recv_packet_to_tx( + msg: MsgRecvPacket, + converter: &C, +) -> Result { + let channel_id = msg.packet.destination_channel.clone(); + let old_channel_end = converter.get_ibc_channel(&channel_id); + let mut new_channel_end = old_channel_end.clone(); + new_channel_end.sequence.next_sequence_recvs += 1; + + let old_channel = get_encoded_object(old_channel_end); + let new_channel = get_encoded_object(new_channel_end.clone()); + + let ckb_msg = CkbMsgRecvPacket { + proofs: convert_proof(msg.proofs)?, + }; + let envelope = Envelope { + msg_type: MsgType::MsgRecvPacket, + content: rlp::encode(&ckb_msg).to_vec(), + }; + let port_id = msg.packet.destination_port.clone(); + + let channel_input = converter.get_ibc_channel_input(&channel_id, &msg.packet.source_port); + let packet = convert_ibc_packet(msg.packet); + let seq = packet.sequence; + let ibc_packet = IbcPacket { + packet, + tx_hash: None, + status: PacketStatus::Recv, + }; + let packet = get_encoded_object(ibc_packet); + let channel_idx = get_channel_idx(&channel_id)?; + let port_id_in_args: [u8; 32] = port_id.as_str().as_bytes().try_into().unwrap(); + + let (client_cell_type_args, client_id) = + extract_client_id_by_connection_id(&new_channel_end.connection_hops[0], converter)?; + let channel_args = ChannelArgs { + client_id: client_cell_type_args, + open: true, + channel_id: channel_idx, + port_id: port_id_in_args, + }; + let packet_args = PacketArgs { + channel_id: channel_idx, + port_id: port_id_in_args, + sequence: seq, + }; + + let channel_lock = get_channel_lock_script(converter, channel_args.to_args()); + let packet_lock = get_packet_lock_script(converter, packet_args.to_args()); + + let packed_tx = TxBuilder::default() + .cell_dep(get_client_outpoint(converter, &client_id)?) + .input(channel_input) + .output(channel_lock, get_channel_capacity(), new_channel.data) + .output(packet_lock, get_packet_capacity(), packet.data) + .witness(old_channel.witness, new_channel.witness) + .witness(BytesOpt::default(), packet.witness) + .build(); + + Ok(CkbTxInfo { + unsigned_tx: Some(packed_tx), + envelope, + input_capacity: PACKET_CELL_CAPACITY, + event: None, + }) +} diff --git a/crates/relayer/src/chain/ckb4ibc/monitor.rs b/crates/relayer/src/chain/ckb4ibc/monitor.rs index 59a4f4444..d5e54d220 100644 --- a/crates/relayer/src/chain/ckb4ibc/monitor.rs +++ b/crates/relayer/src/chain/ckb4ibc/monitor.rs @@ -28,6 +28,7 @@ use ibc_relayer_types::core::ics24_host::identifier::{ChannelId, ClientId, Conne use ibc_relayer_types::events::IbcEvent; use ibc_relayer_types::timestamp::Timestamp; use tokio::runtime::Runtime as TokioRuntime; +use tracing::error; use crate::chain::ckb::prelude::CkbReader; use crate::chain::ckb::rpc_client::RpcClient; @@ -101,15 +102,9 @@ impl Ckb4IbcEventMonitor { } let result = async { tokio::select! { - Ok(batch) = self.fetch_channel_events() => { - batch - }, - Ok(batch) = self.fetch_connection_events() => { - batch - }, - Ok(batch) = self.fetch_packet_events() => { - batch - } + batch = self.fetch_channel_events() => batch, + batch = self.fetch_connection_events() => batch, + batch = self.fetch_packet_events() => batch, } } .await; @@ -379,8 +374,11 @@ impl Ckb4IbcEventMonitor { Ok(result) } - fn process_batch(&mut self, batch: EventBatch) { - self.event_bus.broadcast(Arc::new(Ok(batch))); + fn process_batch(&mut self, batch: Result) { + match batch { + Ok(events) => self.event_bus.broadcast(Arc::new(Ok(events))), + Err(error) => error!("{error}"), + } } } diff --git a/crates/relayer/src/chain/ckb4ibc/utils.rs b/crates/relayer/src/chain/ckb4ibc/utils.rs index f086e3ba9..c0289d114 100644 --- a/crates/relayer/src/chain/ckb4ibc/utils.rs +++ b/crates/relayer/src/chain/ckb4ibc/utils.rs @@ -128,6 +128,7 @@ pub fn get_connection_lock_script( config: &ChainConfig, client_id: Option, ) -> Result { + // fetch specific (concrete client cell) or all (prefix search) let mut client_cell_type_args = vec![]; if let Some(client_id) = client_id { let client_type = config.lc_client_type(&client_id)?; @@ -135,13 +136,28 @@ pub fn get_connection_lock_script( } let script = Script::new_builder() .code_hash(get_script_hash(&config.connection_type_args)) - // fetch specific (concrete client cell) or all (prefix search) .args(client_cell_type_args.pack()) .hash_type(ScriptHashType::Type.into()) .build(); Ok(script) } +pub fn get_channel_lock_script(converter: &impl MsgToTxConverter, args: Vec) -> Script { + Script::new_builder() + .code_hash(converter.get_channel_code_hash()) + .args(args.pack()) + .hash_type(ScriptHashType::Type.into()) + .build() +} + +pub fn get_packet_lock_script(converter: &impl MsgToTxConverter, args: Vec) -> Script { + Script::new_builder() + .code_hash(converter.get_packet_code_hash()) + .args(args.pack()) + .hash_type(ScriptHashType::Type.into()) + .build() +} + pub fn get_search_key(script: Script) -> SearchKey { SearchKey { script: script.into(), @@ -152,17 +168,14 @@ pub fn get_search_key(script: Script) -> SearchKey { } } -#[inline] pub fn get_connection_capacity() -> Capacity { Capacity::bytes(CONNECTION_CELL_CAPACITY as usize).unwrap() } -#[inline] pub fn get_channel_capacity() -> Capacity { Capacity::bytes(CHANNEL_CELL_CAPACITY as usize).unwrap() } -#[inline] pub fn get_packet_capacity() -> Capacity { Capacity::bytes(PACKET_CELL_CAPACITY as usize).unwrap() }