Skip to content

Commit

Permalink
feat!: write commitments
Browse files Browse the repository at this point in the history
  • Loading branch information
blckngm committed Dec 1, 2023
1 parent f3e4414 commit 1c4b361
Show file tree
Hide file tree
Showing 12 changed files with 108 additions and 42 deletions.
5 changes: 2 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ anyhow = "1.0.72"
async-stream = "0.3.5"
bytes = "1.4.0"
ckb-fixed-hash = "0.111.0"
ckb-ics-axon = { git = "https://github.com/synapseweb3/ckb-ics.git", rev = "b026840b9599ba" }
ckb-ics-axon = { git = "https://github.com/synapseweb3/ckb-ics.git", rev = "4ca20ad" }
ckb-jsonrpc-types = "0.111.0"
ckb-sdk = "3.0.0"
ckb-types = "0.111.0"
Expand All @@ -29,7 +29,6 @@ tokio = { version = "1.31.0", features = ["time"] }
tracing = "0.1.37"

[dev-dependencies]
axon-types = { git = "https://github.com/axonweb3/axon-contract", rev = "8106ddc0266" }
ckb-testtool = "0.10.0"
clap = { version = "4.4.6", features = ["derive"] }
hex = "0.4.3"
Expand Down
Binary file modified contracts/ibc-sudt-transfer
Binary file not shown.
Binary file modified contracts/ics-channel
100644 → 100755
Binary file not shown.
Binary file modified contracts/ics-packet
100644 → 100755
Binary file not shown.
2 changes: 1 addition & 1 deletion contracts/version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Built at 59b5b8450b2db9b448d92e0df90df15376ab4ff4 (complete-axon-client)
Built at 8c363775ac066ce95dc713f5b80e53cd3a949e61 (write-commitment)
23 changes: 17 additions & 6 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub struct Config {
pub axon_ibc_handler_address: H160,

pub channel_contract_type_id_args: H256,
pub channel_id: u16,
pub channel_id: u64,

pub packet_contract_type_id_args: H256,

Expand Down Expand Up @@ -58,8 +58,8 @@ impl Config {
hex::encode(self.port_id())
}

pub fn channel_cell_lock_script(&self, open: bool) -> packed::Script {
let channel_args = ChannelArgs {
pub fn channel_args(&self, open: bool) -> ChannelArgs {
ChannelArgs {
metadata_type_id: self
.axon_metadata_type_script()
.calc_script_hash()
Expand All @@ -69,14 +69,25 @@ impl Config {
open,
channel_id: self.channel_id,
port_id: self.module_lock_script().calc_script_hash().unpack().0,
};
}
}

pub fn channel_cell_lock_script(&self, open: bool) -> packed::Script {
packed::Script::new_builder()
.hash_type(ScriptHashType::Type.into())
.code_hash(self.channel_contract_type_script().calc_script_hash())
.args(channel_args.to_args().pack())
.args(self.channel_args(open).to_args().pack())
.build()
}

pub fn packet_args(&self, sequence: u64) -> PacketArgs {
PacketArgs {
channel_id: self.channel_id,
port_id: self.module_lock_script().calc_script_hash().unpack().0,
sequence,
}
}

pub fn packet_contract_type_script(&self) -> packed::Script {
Self::type_id_type_script(&self.packet_contract_type_id_args)
}
Expand All @@ -96,7 +107,7 @@ impl Config {
}

/// Packet cell lock script for certain sequence number.
pub fn packet_cell_lock_script(&self, sequence: u16) -> packed::Script {
pub fn packet_cell_lock_script(&self, sequence: u64) -> packed::Script {
let packet_args = PacketArgs {
channel_id: self.channel_id,
port_id: self.module_lock_script().calc_script_hash().unpack().0,
Expand Down
31 changes: 19 additions & 12 deletions src/json.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use ckb_fixed_hash::H256;
use ckb_ics_axon::{
handler::{IbcChannel, IbcPacket, PacketStatus, Sequence},
message::{Envelope, MsgType},
message::{CommitmentKV, Envelope, MsgType},
object::{ChannelCounterparty, Ordering, Packet, State},
};
use ckb_jsonrpc_types::JsonBytes;
Expand All @@ -16,7 +16,6 @@ pub struct JsonIbcPacket {
pub packet: Packet,
#[serde(with = "JsonPacketStatus")]
pub status: PacketStatus,
pub tx_hash: Option<H256>,
#[serde_as(as = "Option<HexBytes>")]
pub ack: Option<Vec<u8>>,
}
Expand All @@ -35,7 +34,6 @@ impl From<&IbcPacket> for JsonIbcPacket {
Self {
packet: value.packet.clone(),
status: value.status,
tx_hash: value.tx_hash.map(|v| <[u8; 32]>::from(v).into()),
ack: value.ack.clone(),
}
}
Expand All @@ -46,7 +44,6 @@ impl From<JsonIbcPacket> for IbcPacket {
Self {
packet: value.packet,
status: value.status,
tx_hash: value.tx_hash.map(|v| <[u8; 32]>::from(v).into()),
ack: value.ack,
}
}
Expand All @@ -56,7 +53,7 @@ impl From<JsonIbcPacket> for IbcPacket {
#[derive(Serialize, Deserialize)]
#[serde(remote = "Packet", deny_unknown_fields)]
pub struct JsonPacket {
pub sequence: u16,
pub sequence: u64,
pub source_port_id: String,
pub source_channel_id: String,
pub destination_port_id: String,
Expand All @@ -70,7 +67,7 @@ pub struct JsonPacket {
#[derive(Serialize, Deserialize)]
#[serde(remote = "IbcChannel", deny_unknown_fields)]
pub struct JsonIbcChannel {
pub number: u16,
pub number: u64,
pub port_id: String,
#[serde(with = "JsonState")]
pub state: State,
Expand All @@ -92,7 +89,6 @@ enum JsonState {
OpenTry,
Open,
Closed,
Frozen,
}

#[derive(Serialize, Deserialize)]
Expand All @@ -106,10 +102,10 @@ enum JsonOrdering {
#[derive(Serialize, Deserialize)]
#[serde(remote = "Sequence", deny_unknown_fields)]
pub struct JsonSequence {
pub next_sequence_sends: u16,
pub next_sequence_recvs: u16,
pub next_sequence_acks: u16,
pub received_sequences: Vec<u16>,
pub next_sequence_sends: u64,
pub next_sequence_recvs: u64,
pub next_sequence_acks: u64,
pub received_sequences: Vec<u64>,
}

#[derive(Serialize, Deserialize)]
Expand Down Expand Up @@ -146,20 +142,26 @@ enum JsonMsgType {
}

#[serde_as]
#[derive(Serialize, Deserialize)]
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug)]
#[serde(deny_unknown_fields)]
pub struct JsonEnvelope {
#[serde(with = "JsonMsgType")]
pub msg_type: MsgType,
#[serde_as(as = "HexBytes")]
pub content: Vec<u8>,
pub commitments: Vec<(H256, H256)>,
}

impl From<&Envelope> for JsonEnvelope {
fn from(value: &Envelope) -> Self {
Self {
content: value.content.clone(),
msg_type: value.msg_type,
commitments: value
.commitments
.iter()
.map(|kv| (kv.0 .0.into(), kv.1 .0.into()))
.collect(),
}
}
}
Expand All @@ -169,6 +171,11 @@ impl From<JsonEnvelope> for Envelope {
Self {
msg_type: value.msg_type,
content: value.content,
commitments: value
.commitments
.into_iter()
.map(|(k, v)| CommitmentKV(k.0.into(), v.0.into()))
.collect(),
}
}
}
Expand Down
54 changes: 49 additions & 5 deletions src/transaction.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use anyhow::{anyhow, ensure, Context, Result};
use ckb_ics_axon::{
get_channel_id_str,
handler::{IbcPacket, PacketStatus},
handler::{
handle_msg_channel_close_init, handle_msg_send_packet, handle_msg_write_ack_packet,
IbcPacket, PacketStatus,
},
message::{
Envelope, MsgChannelCloseInit, MsgConsumeAckPacket, MsgSendPacket, MsgType,
MsgWriteAckPacket,
Expand Down Expand Up @@ -47,15 +49,15 @@ pub fn assemble_send_packet_partial_transaction(
timeout_height: u64,
timeout_timestamp: u64,
) -> Result<(TransactionBuilder, Envelope)> {
let args = config.channel_args(true);
let packet = IbcPacket {
tx_hash: None,
status: PacketStatus::Send,
packet: Packet {
data,
timeout_height,
timeout_timestamp,
sequence: channel.channel.sequence.next_sequence_sends,
source_channel_id: get_channel_id_str(channel.channel.number),
source_channel_id: args.channel_id_str(),
source_port_id: channel.channel.port_id.clone(),
destination_port_id: channel.channel.counterparty.port_id.clone(),
destination_channel_id: channel.channel.counterparty.channel_id.clone(),
Expand All @@ -70,6 +72,19 @@ pub fn assemble_send_packet_partial_transaction(
.checked_add(1)
.context("sequence overflow")?;

let packet_args = config.packet_args(packet.packet.sequence);
let mut commitments = Vec::new();
handle_msg_send_packet(
channel.channel.clone(),
args,
new_channel_state.clone(),
args,
packet.clone(),
packet_args,
&mut commitments,
)
.map_err(|e| anyhow!("handle_msg_send_packet: {e:?}"))?;

let prev_channel_bytes = rlp::encode(&channel.channel).freeze();
let new_channel_bytes = rlp::encode(&new_channel_state).freeze();
let channel_witness = packed::WitnessArgs::new_builder()
Expand Down Expand Up @@ -101,6 +116,7 @@ pub fn assemble_send_packet_partial_transaction(

let envelope = Envelope {
msg_type: MsgType::MsgSendPacket,
commitments,
content: rlp::encode(&MsgSendPacket {}).to_vec(),
};

Expand Down Expand Up @@ -130,7 +146,6 @@ pub fn assemble_write_ack_partial_transaction(
let ack = IbcPacket {
packet: packet.packet.packet.clone(),
status: PacketStatus::WriteAck,
tx_hash: None,
ack: Some(ack),
};

Expand All @@ -153,6 +168,22 @@ pub fn assemble_write_ack_partial_transaction(
.output_type(Some(packet_bytes.clone()).pack())
.build();

let mut commitments = Vec::new();
let channel_args = config.channel_args(true);
let packet_args = config.packet_args(packet.packet.packet.sequence);
handle_msg_write_ack_packet(
channel.channel.clone(),
channel_args,
new_channel_state,
channel_args,
packet.packet.clone(),
packet_args,
ack,
packet_args,
&mut commitments,
)
.map_err(|e| anyhow!("handle_msg_write_ack_packet: {e:?}"))?;

let tx = TransactionView::new_advanced_builder()
.cell_dep(axon_metadata_cell_dep)
.cell_dep(channel_contract_cell_dep)
Expand All @@ -171,6 +202,7 @@ pub fn assemble_write_ack_partial_transaction(

let envelope = Envelope {
msg_type: MsgType::MsgWriteAckPacket,
commitments,
content: rlp::encode(&MsgWriteAckPacket {}).to_vec(),
};

Expand Down Expand Up @@ -203,6 +235,7 @@ pub fn assemble_consume_ack_packet_partial_transaction(

let envelope = Envelope {
msg_type: MsgType::MsgConsumeAckPacket,
commitments: vec![],
content: rlp::encode(&MsgConsumeAckPacket {}).to_vec(),
};

Expand Down Expand Up @@ -248,6 +281,16 @@ pub fn assemble_channel_close_init_partial_transaction(
.lock(new_channel_script)
.build_exact_capacity(Capacity::bytes(32)?)?;

let mut commitments = Vec::new();
handle_msg_channel_close_init(
channel.channel.clone(),
old_channel_script_args,
new_channel,
new_channel_script_args,
&mut commitments,
)
.map_err(|e| anyhow!("handle_msg_channel_close_init: {e:?}"))?;

let tx = TransactionView::new_advanced_builder()
.cell_dep(axon_metadata_cell_dep)
.cell_dep(channel_contract_cell_dep)
Expand All @@ -259,6 +302,7 @@ pub fn assemble_channel_close_init_partial_transaction(

let envelope = Envelope {
msg_type: MsgType::MsgChannelCloseInit,
commitments,
content: rlp::encode(&MsgChannelCloseInit {}).to_vec(),
};

Expand Down
8 changes: 6 additions & 2 deletions tests/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@ use forcerelay_ckb_sdk::json::JsonEnvelope;
fn test_envelope_serde() {
let e = JsonEnvelope {
content: [3, 4].to_vec(),
commitments: Vec::new(),
msg_type: MsgType::MsgAckPacket,
};
let json = serde_json::to_string(&e).unwrap();
assert_eq!(json, r#"{"msg_type":"MsgAckPacket","content":"0x0304"}"#);
assert_eq!(
json,
r#"{"msg_type":"MsgAckPacket","content":"0x0304","commitments":[]}"#
);
let e1: JsonEnvelope = serde_json::from_str(&json).unwrap();
assert_eq!((e.msg_type, e.content), (e1.msg_type, e1.content));
assert_eq!(e1, e);
}
Loading

0 comments on commit 1c4b361

Please sign in to comment.