Skip to content

Commit

Permalink
feat(wip): successfully submit RecvPacket transaction, but cannot lis…
Browse files Browse the repository at this point in the history
…ten it through SDK
  • Loading branch information
liyukun committed Aug 27, 2023
1 parent 0563cbd commit 3364085
Show file tree
Hide file tree
Showing 15 changed files with 51 additions and 61 deletions.
1 change: 1 addition & 0 deletions crates/relayer-cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
unused_qualifications
)]
#![allow(deprecated)]
#![allow(clippy::redundant_closure_call)]

extern crate alloc;

Expand Down
2 changes: 1 addition & 1 deletion crates/relayer/src/chain/ckb/helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ pub trait CellSearcher: CkbReader {
.objects
.into_iter()
.filter_map(|cell| {
if searched_capacity < need_capacity && !cell.output.type_.is_some() {
if searched_capacity < need_capacity && cell.output.type_.is_none() {
searched_capacity += Into::<u64>::into(cell.output.capacity);
Some(cell.into())
} else {
Expand Down
44 changes: 24 additions & 20 deletions crates/relayer/src/chain/ckb4ibc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,28 +294,27 @@ impl Ckb4IbcChain {
.transaction
.unwrap();
let tx = match tx_resp.inner {
ckb_jsonrpc_types::Either::Left(channel) => channel,
ckb_jsonrpc_types::Either::Left(tx) => tx,
ckb_jsonrpc_types::Either::Right(json_bytes) => {
serde_json::from_slice(json_bytes.as_bytes()).unwrap()
}
};
let channel_end = extract_channel_end_from_tx(tx)?;
let input = CellInput::new_builder()
.previous_output(
OutPoint::new_builder()
.tx_hash(tx_hash.pack())
.index(cell.tx_index.pack())
.build(),
)
.previous_output(cell.out_point.clone().into())
.build();
Ok((channel_end, input))
});

let ((channel, ibc_channel_end), cell_input) = self.rt.block_on(channel_future)?;

let mut data = self.channel_input_data.borrow_mut();
data.insert((channel.channel_id.clone(), channel.port_id), cell_input);
let mut cache = self.channel_cache.borrow_mut();
cache.insert(channel.channel_id, ibc_channel_end.clone());
self.channel_input_data
.borrow_mut()
.insert((channel.channel_id.clone(), channel.port_id), cell_input);
self.channel_cache
.borrow_mut()
.insert(channel.channel_id, ibc_channel_end.clone());

Ok((channel.channel_end, ibc_channel_end))
}

Expand Down Expand Up @@ -593,6 +592,7 @@ impl ChainEndpoint for Ckb4IbcChain {
continue;
}
let unsigned_tx = unsigned_tx.unwrap();
let msg_type = envelope.msg_type;
match self.complete_tx_with_secp256k1_change_and_envelope(
unsigned_tx,
input_capacity,
Expand Down Expand Up @@ -623,7 +623,7 @@ impl ChainEndpoint for Ckb4IbcChain {
)
.unwrap();
tx_hashes.push(tx.hash().unpack());
txs.push(tx);
txs.push((tx, msg_type));
events.push(event);
}
Err(err) => {
Expand All @@ -632,21 +632,28 @@ impl ChainEndpoint for Ckb4IbcChain {
}
}
}
let resps = txs.iter().map(|tx| {
let responses = txs.iter().map(|(tx, msg_type)| {
let tx: TransactionView = tx.clone().into();
self.rpc_client
.send_transaction(&tx.inner, None)
.and_then(|tx_hash| {
let confirms = 3;
info!(
"{:?} transaction {} committed to {}, wait {confirms} blocks confirmation",
*msg_type,
hex::encode(&tx_hash),
self.id()
);
wait_ckb_transaction_committed(
&self.rpc_client,
tx_hash,
Duration::from_secs(10),
3,
confirms,
Duration::from_secs(600),
)
})
});
let responses = self.rt.block_on(futures::future::join_all(resps));
let responses = self.rt.block_on(futures::future::join_all(responses));
for (i, response) in responses.iter().enumerate() {
match response {
Ok(height) => {
Expand All @@ -661,12 +668,9 @@ impl ChainEndpoint for Ckb4IbcChain {
}
}
Err(e) => {
let tx: TransactionView = txs[i].clone().into();
let tx: TransactionView = txs[i].0.clone().into();
let json_tx = serde_json::to_string_pretty(&tx).unwrap();
let error = format!(
"{}\n\n======== transaction info ========\n\n{json_tx}\n",
e.to_string()
);
let error = format!("{e}\n\n======== transaction info ========\n\n{json_tx}\n");
return Err(Error::send_tx(error));
}
}
Expand Down
8 changes: 4 additions & 4 deletions crates/relayer/src/chain/ckb4ibc/extractor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use super::utils::{generate_channel_id, generate_connection_id};
pub fn extract_channel_end_from_tx(
tx: TransactionView,
) -> Result<(IdentifiedChannelEnd, CkbIbcChannel), Error> {
let idx = get_object_idx(&tx, ObjectType::ChannelEnd)?;
let idx = get_object_index(&tx, ObjectType::ChannelEnd)?;
let witness = tx.inner.witnesses.get(idx).unwrap();
let witness_args = WitnessArgs::from_slice(witness.as_bytes())
.map_err(|_| Error::ckb_decode_witness_args())?;
Expand All @@ -45,7 +45,7 @@ pub fn extract_channel_end_from_tx(
}

pub fn extract_ibc_connections_from_tx(tx: TransactionView) -> Result<IbcConnections, Error> {
let idx = get_object_idx(&tx, ObjectType::IbcConnections)?;
let idx = get_object_index(&tx, ObjectType::IbcConnections)?;
let witness = tx.inner.witnesses.get(idx).unwrap();
let witness_args = WitnessArgs::from_slice(witness.as_bytes()).unwrap();
let ibc_connection_cells =
Expand All @@ -70,7 +70,7 @@ pub fn extract_connections_from_tx(
}

pub fn extract_ibc_packet_from_tx(tx: TransactionView) -> Result<IbcPacket, Error> {
let idx = get_object_idx(&tx, ObjectType::IbcPacket)?;
let idx = get_object_index(&tx, ObjectType::IbcPacket)?;
let witness = tx.inner.witnesses.get(idx).unwrap();
let witness_args = WitnessArgs::from_slice(witness.as_bytes())
.map_err(|_| Error::ckb_decode_witness_args())?;
Expand Down Expand Up @@ -209,7 +209,7 @@ enum ObjectType {
IbcPacket,
}

fn get_object_idx(tx: &TransactionView, object_type: ObjectType) -> Result<usize, Error> {
fn get_object_index(tx: &TransactionView, object_type: ObjectType) -> Result<usize, Error> {
let msg = tx.inner.witnesses.last().ok_or(Error::ckb_none_witness())?;

let bytes = msg.as_bytes();
Expand Down
1 change: 1 addition & 0 deletions crates/relayer/src/chain/ckb4ibc/message/packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ pub fn convert_recv_packet_to_tx<C: MsgToTxConverter>(

let packed_tx = TxBuilder::default()
.cell_dep(get_client_outpoint(converter, &client_id)?)
.cell_dep(converter.get_chan_contract_outpoint())
.input(channel_input)
// TODO: fetch useless packet cell as input to save capacity
// .input()
Expand Down
28 changes: 6 additions & 22 deletions crates/relayer/src/chain/ckb4ibc/monitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ use ibc_relayer_types::core::ics03_connection::events::{
};
use ibc_relayer_types::core::ics04_channel::channel::State;
use ibc_relayer_types::core::ics04_channel::events::{
AcknowledgePacket, OpenInit as ChannelOpenInit, OpenTry as ChannelOpenTry, ReceivePacket,
SendPacket,
AcknowledgePacket, OpenInit as ChannelOpenInit, OpenTry as ChannelOpenTry, SendPacket,
};
use ibc_relayer_types::core::ics04_channel::packet::{Packet, Sequence};
use ibc_relayer_types::core::ics04_channel::timeout::TimeoutHeight;
Expand Down Expand Up @@ -350,7 +349,10 @@ impl Ckb4IbcEventMonitor {
let events = ibc_packets
.into_iter()
.filter(|((packet, tx), _)| {
if packet.status == PacketStatus::Ack || self.cache_set.read().unwrap().has(tx) {
if packet.status == PacketStatus::Ack
|| packet.status == PacketStatus::Recv
|| self.cache_set.read().unwrap().has(tx)
{
return false;
}
self.cache_set.write().unwrap().insert(tx.clone());
Expand All @@ -375,24 +377,6 @@ impl Ckb4IbcEventMonitor {
tx_hash: tx.into(),
}
}
PacketStatus::Recv => {
info!(
"🫡 {} received RecvPacket({}) event, from {}/{} to {}/{}",
self.config.id,
packet.packet.sequence,
packet.packet.source_channel_id,
packet.packet.source_port_id,
packet.packet.destination_channel_id,
packet.packet.destination_port_id,
);
IbcEventWithHeight {
event: IbcEvent::ReceivePacket(ReceivePacket {
packet: convert_packet(packet),
}),
height: Height::from_noncosmos_height(block_number),
tx_hash: tx.into(),
}
}
PacketStatus::WriteAck => {
info!(
"🫡 {} received WriteAck({}) event, from {}/{} to {}/{}",
Expand All @@ -411,7 +395,7 @@ impl Ckb4IbcEventMonitor {
tx_hash: tx.into(),
}
}
PacketStatus::Ack => unreachable!(),
PacketStatus::Ack | PacketStatus::Recv => unreachable!(),
})
.collect::<Vec<_>>();

Expand Down
Binary file modified tools/ckb4ibc-test/contracts/ics-channel
Binary file not shown.
Binary file modified tools/ckb4ibc-test/contracts/ics-connection
Binary file not shown.
Binary file modified tools/ckb4ibc-test/contracts/ics-packet
Binary file not shown.
8 changes: 4 additions & 4 deletions tools/ckb4ibc-test/src/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ use ckb_types::{h256, H256};
pub const CONNECTION_CODE_HASH: H256 =
h256!("0xcf6e0c0148123081af1deda0ef162d39cfdfe1ea6565d3689009c1f3562a5e82");
pub const CHANNEL_CODE_HASH: H256 =
h256!("0x903e6d9cebe749c92c36c509a797ab422c6d2023d0e77d07de646b6079f7cdbb");
h256!("0xc1e2403733df5650ecb616f094f4aa7e9153d94a72d5599cafe4dce222735d8b");

pub const CONNECTION_TYPE_ARGS: H256 =
h256!("0xf49ce32397c6741998b04d7548c5ed372007424daf67ee5bfadaefec3c865781");
pub const CHANNEL_TYPE_ARGS: H256 =
h256!("0xc0cc3728ad52ed82eb2ea6d418e773b8410faa14caa96714f6d8b653165c0e0b");
h256!("0x3dcef28158396cd414c9560e369a691b8a3ebd2bc077bce08d980f21bc061fff");
pub const PACKET_TYPE_ARGS: H256 =
h256!("0x395dff439222ac7146022147b846f19586a319034ddc0b9038395e2fcf352292");
h256!("0xbac825404a26b9ad2d0421859ae967644b2ce577abbcc459ed09b025337e23c3");
pub const CLIENT_TYPE_ARGS: H256 =
h256!("0xd3d01069e55e5653f80c9018470a4ee7f7b1a408cf191b6272808d9b6a5c1dbf");
h256!("0x6db6f1466d7d06210c050639fd63e58ddb858138124309643bfd41b4c22586e0");
12 changes: 6 additions & 6 deletions tools/ckb4ibc-test/src/tests/packet.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use ckb_types::prelude::Entity;
use ibc_test_framework::prelude::*;
use log::{debug, info};
use log::info;
use tokio::runtime::Runtime;

mod utils;
Expand Down Expand Up @@ -58,7 +58,7 @@ impl BinaryChannelTest for CKB4IbcPacketTest {
);

// 2. trigger SendPacket event on ChainA
debug!("send send_packet transaction to chain_a");
info!("send send_packet transaction to chain_a");
let message = b"ping".to_vec();
let send_packet_tx = generate_send_packet_transaction(
&rt,
Expand All @@ -76,7 +76,7 @@ impl BinaryChannelTest for CKB4IbcPacketTest {
);

// 3. listen RecvPacket event on ChainB
debug!("wait recv_packet being found on chain_b");
info!("wait recv_packet being found on chain_b");
let mut recv_packets =
listen_and_wait_packet_cells(&rt, &chain_b_url, &chain_b_config, |packet| {
packet.is_recv_packet()
Expand All @@ -87,7 +87,7 @@ impl BinaryChannelTest for CKB4IbcPacketTest {
info!("🍻 successfully find recv_packet cell on chain_b");

// 4. trigger WriteAck event on ChainB
debug!("send write_ack transaction to chain_b");
info!("send write_ack transaction to chain_b");
let acknowledgemnt = b"pong".to_vec();
let write_ack_tx = generate_write_ack_transaction(
&rt,
Expand All @@ -104,7 +104,7 @@ impl BinaryChannelTest for CKB4IbcPacketTest {
);

// 5. lisen AckPacket event on ChainA
debug!("wait ack_packet being found on chain_a");
info!("wait ack_packet being found on chain_a");
let mut ack_packets =
listen_and_wait_packet_cells(&rt, &chain_a_url, &chain_a_config, |packet| {
packet.is_ack_packet()
Expand All @@ -115,7 +115,7 @@ impl BinaryChannelTest for CKB4IbcPacketTest {
info!("🍻 successfully find ack_packet cell on chain_a");

// 6. comsune AckPacket cell on ChainA
debug!("send ack_packet consume transaction to chain_a");
info!("send ack_packet consume transaction to chain_a");
let consume_ack_packet_tx = generate_consume_ack_packet_transaction(
&rt,
&chain_a_config,
Expand Down
2 changes: 1 addition & 1 deletion tools/ckb4ibc-test/txs/create_connection.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"id": 4, "jsonrpc": "2.0", "method": "send_transaction", "params": [{"version":"0x0","cell_deps":[{"out_point":{"tx_hash":"0x4e8a574dfb3db9daf11683a6ad415ff4fe20507fde84f066fcae5288e0067dbc","index":"0x1"},"dep_type":"code"},{"out_point":{"tx_hash":"0x4e8a574dfb3db9daf11683a6ad415ff4fe20507fde84f066fcae5288e0067dbc","index":"0x0"},"dep_type":"code"},{"out_point":{"tx_hash":"0x29ed5663501cd171513155f8939ad2c9ffeb92aa4879d39cde987f8eb6274407","index":"0x0"},"dep_type":"dep_group"}],"header_deps":[],"inputs":[{"since":"0x0","previous_output":{"tx_hash":"0x4e8a574dfb3db9daf11683a6ad415ff4fe20507fde84f066fcae5288e0067dbc","index":"0x2"}}],"outputs":[{"capacity":"0x6fc23ac00","lock":{"code_hash":"0xcf6e0c0148123081af1deda0ef162d39cfdfe1ea6565d3689009c1f3562a5e82","hash_type":"type","args":"0x0c638fcf7faac9dbcf181308563e6c335f647813f8e89ea3ed59cd29d07acd4a"},"type":null},{"capacity":"0x16bcc41e90000","lock":{"code_hash":"0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8","hash_type":"type","args":"0x470dcdc5e44064909650113a274b3b36aecb6dc7"},"type":null}],"outputs_data":["0x83795d737ad7359b0f3f87eba317e87c7eacba1ea3e21a7f00764c91ad265d2d","0x3078"],"witnesses":["0x5d000000100000005500000055000000410000008e441ada197d269c95e39a6d56288b6dcc62815362539f349bbd636bf2d8e2dc1d78b8a9e5476709414b5a64955c2ce6e0d328ecf9b52bdd20dabb2c465f8de70004000000c38080c0","0x1900000010000000100000001000000005000000c401c281c0"]}]}
{"id": 4, "jsonrpc": "2.0", "method": "send_transaction", "params": [{"version":"0x0","cell_deps":[{"out_point":{"tx_hash":"0x58197125211ceebe4736050ad1cddf031a0ee669521eca5c6e2d75b7e7773c61","index":"0x1"},"dep_type":"code"},{"out_point":{"tx_hash":"0x58197125211ceebe4736050ad1cddf031a0ee669521eca5c6e2d75b7e7773c61","index":"0x0"},"dep_type":"code"},{"out_point":{"tx_hash":"0x29ed5663501cd171513155f8939ad2c9ffeb92aa4879d39cde987f8eb6274407","index":"0x0"},"dep_type":"dep_group"}],"header_deps":[],"inputs":[{"since":"0x0","previous_output":{"tx_hash":"0x58197125211ceebe4736050ad1cddf031a0ee669521eca5c6e2d75b7e7773c61","index":"0x2"}}],"outputs":[{"capacity":"0x6fc23ac00","lock":{"code_hash":"0xcf6e0c0148123081af1deda0ef162d39cfdfe1ea6565d3689009c1f3562a5e82","hash_type":"type","args":"0x70146c3e95a4dfa70e4bbefa9b6a20a4015aa5a238e9e2931bfbb34e40247acc"},"type":null},{"capacity":"0x16bcc41e90000","lock":{"code_hash":"0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8","hash_type":"type","args":"0x470dcdc5e44064909650113a274b3b36aecb6dc7"},"type":null}],"outputs_data":["0x83795d737ad7359b0f3f87eba317e87c7eacba1ea3e21a7f00764c91ad265d2d","0x3078"],"witnesses":["0x5d00000010000000550000005500000041000000efaee970ec7f2080f13995ce1e6f482be816c54763701ddba051e6f8b4f2fae77d966742e8a12890fbe496f68c0f05008e549776c49fccaf51293dc6fb64c6370104000000c38080c0","0x1900000010000000100000001000000005000000c401c281c0"]}]}
2 changes: 1 addition & 1 deletion tools/ckb4ibc-test/txs/deploy_channel.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion tools/ckb4ibc-test/txs/deploy_connection.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion tools/ckb4ibc-test/txs/deploy_packet_metadata.json

Large diffs are not rendered by default.

0 comments on commit 3364085

Please sign in to comment.