Skip to content

Commit

Permalink
Merge pull request #372 from synapseweb3/bug/fix-capacity-balance
Browse files Browse the repository at this point in the history
bug: fix RecvPacket tx capacity balance issue
  • Loading branch information
ashuralyk authored Nov 7, 2023
2 parents d50d44d + a461cc3 commit 70c525d
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 16 deletions.
4 changes: 2 additions & 2 deletions crates/relayer/src/chain/ckb4ibc/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ pub trait MsgToTxConverter {
fn require_useless_write_ack_packet(
&self,
block_number_gap: u64,
) -> Option<(IbcPacket, CellInput)>;
) -> Option<(IbcPacket, CellInput, u64)>;
}

pub struct Converter<'a> {
Expand Down Expand Up @@ -304,7 +304,7 @@ impl<'a> MsgToTxConverter for Converter<'a> {
fn require_useless_write_ack_packet(
&self,
block_number_gap: u64,
) -> Option<(IbcPacket, CellInput)> {
) -> Option<(IbcPacket, CellInput, u64)> {
if let Some(cmd) = self.write_ack_cmd.as_ref() {
let (tx, rx) = crossbeam_channel::bounded(1);
cmd.send((tx, block_number_gap))
Expand Down
7 changes: 4 additions & 3 deletions crates/relayer/src/chain/ckb4ibc/message/packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ pub fn convert_recv_packet_to_tx<C: MsgToTxConverter>(
ack: None,
});

let (channel_input, input_capacity) =
let (channel_input, mut input_capacity) =
converter.get_ibc_channel_input(&channel_id, &msg.packet.destination_port)?;
let channel_lock = get_channel_lock_script(converter, channel_args.to_args());
let packet_lock = get_packet_lock_script(converter, packet_args.to_args());
Expand All @@ -116,7 +116,7 @@ pub fn convert_recv_packet_to_tx<C: MsgToTxConverter>(

// fetch useless packet cell as input to save capacity
let useless_write_ack_packet = converter.require_useless_write_ack_packet(15); // TODO: make block number gap setup in config
if let Some((packet, input)) = useless_write_ack_packet {
if let Some((packet, input, capacity)) = useless_write_ack_packet {
tracing::info!(
"use useless WriteAck({}) to save CKB capacity",
packet.packet.sequence,
Expand All @@ -125,7 +125,8 @@ pub fn convert_recv_packet_to_tx<C: MsgToTxConverter>(
write_ack_witness = write_ack_packet.witness;
packet_tx = packet_tx
.cell_dep(converter.get_packet_contract_outpoint().clone())
.input(input.clone());
.input(input);
input_capacity += capacity;
}

let packet_tx = packet_tx
Expand Down
28 changes: 17 additions & 11 deletions crates/relayer/src/chain/ckb4ibc/monitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ pub enum IbcProtocolType {
Packet,
}

pub type WriteAckMonitorSender = (Sender<Option<(IbcPacket, CellInput)>>, u64);
pub type UselessWriteAckCell = (IbcPacket, CellInput, u64);
pub type WriteAckMonitorSender = (Sender<Option<UselessWriteAckCell>>, u64);
pub type WriteAckMonitorCmd = Sender<WriteAckMonitorSender>;

// TODO: add cell emitter here
Expand All @@ -70,7 +71,7 @@ pub struct Ckb4IbcEventMonitor {
counterparty_client_type_rx: WatchReceiver<Option<ClientType>>,
counterparty_client_type: ClientType,
fetch_cursors: HashMap<IbcProtocolType, JsonBytes>,
useless_write_ack_packets: BTreeMap<u64, (IbcPacket, CellInput)>,
useless_write_ack_packets: BTreeMap<u64, UselessWriteAckCell>,
}

impl Ckb4IbcEventMonitor {
Expand Down Expand Up @@ -132,11 +133,11 @@ impl Ckb4IbcEventMonitor {
.await
.map_err(|err| Error::others(err.detail().to_string()))?;
if block_number + block_number_gap < tip_block_number {
let (packet, input) = self
let useless_packet = self
.useless_write_ack_packets
.remove(&block_number)
.unwrap();
resposne.send(Some((packet, input))).unwrap();
resposne.send(Some(useless_packet)).unwrap();
return Ok(());
}
}
Expand Down Expand Up @@ -203,7 +204,7 @@ impl Ckb4IbcEventMonitor {
events: vec![],
});
}
let ((ibc_connection_cell, tx_hash), (block_number, _)) =
let ((ibc_connection_cell, tx_hash), (block_number, _, _)) =
connections.into_iter().next().unwrap();
if self.cache_set.read().unwrap().has(&tx_hash) {
return Ok(EventBatch {
Expand Down Expand Up @@ -319,7 +320,7 @@ impl Ckb4IbcEventMonitor {
self.cache_set.write().unwrap().insert(tx.clone());
true
})
.map(|((channel, tx), (block_number, _))| match channel.channel_end.state {
.map(|((channel, tx), (block_number, _, _))| match channel.channel_end.state {
State::Init => {
let connection_id = channel.channel_end.connection_hops[0].clone();
info!(
Expand Down Expand Up @@ -411,7 +412,7 @@ impl Ckb4IbcEventMonitor {
true
})
.map(
|(((packet, _content), tx), (block_number, cell_input))| match packet.status {
|(((packet, _), tx), (block_number, cell_input, capacity))| match packet.status {
PacketStatus::Send => {
info!(
"🫡 {} received SendPacket({}) event, from {}/{} to {}/{}",
Expand Down Expand Up @@ -440,7 +441,8 @@ impl Ckb4IbcEventMonitor {
packet.packet.destination_channel_id,
packet.packet.destination_port_id,
);
useless_packets.insert(block_number, (packet.clone(), cell_input));
useless_packets
.insert(block_number, (packet.clone(), cell_input, capacity));
IbcEventWithHeight {
event: IbcEvent::WriteAcknowledgement(WriteAcknowledgement {
ack: packet
Expand Down Expand Up @@ -472,7 +474,7 @@ impl Ckb4IbcEventMonitor {
extractor: &F,
limit: u32,
ibc_protocol: IbcProtocolType,
) -> Result<Vec<((T, H256), (u64, CellInput))>>
) -> Result<Vec<((T, H256), (u64, CellInput, u64))>>
where
F: Fn(TransactionView) -> Result<(T, H256)>,
{
Expand All @@ -490,9 +492,13 @@ impl Ckb4IbcEventMonitor {
let cell_input = CellInput::new_builder()
.previous_output(cell.out_point.clone().into())
.build();
(cell.block_number.into(), cell_input)
(
cell.block_number.into(),
cell_input,
cell.output.capacity.into(),
)
})
.collect::<Vec<(u64, CellInput)>>();
.collect::<Vec<(u64, CellInput, u64)>>();
let ibc_response = cells
.objects
.iter()
Expand Down

0 comments on commit 70c525d

Please sign in to comment.