diff --git a/Cargo.lock b/Cargo.lock index 8ebe746..31f0847 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -157,7 +157,7 @@ dependencies = [ [[package]] name = "axon-tools" version = "0.1.1" -source = "git+https://github.com/axonweb3/axon.git?rev=06340ba4#06340ba42a752f9f31d38dab2dd40fa50ab2e239" +source = "git+https://github.com/axonweb3/axon.git?rev=f889d38#f889d38949d6201c448ac72ebaec4d4fb0e5ba51" dependencies = [ "bit-vec", "blst", @@ -532,7 +532,7 @@ dependencies = [ [[package]] name = "ckb-ics-axon" version = "0.1.0" -source = "git+https://github.com/synapseweb3/ckb-ics.git?rev=b026840b9599ba#b026840b9599ba277465efa2d3d4e2b33292b2e1" +source = "git+https://github.com/synapseweb3/ckb-ics.git?rev=4ca20ad#4ca20ade6a479303a207084406248266bcbf80ec" dependencies = [ "axon-tools", "axon-types", @@ -1241,7 +1241,6 @@ version = "0.1.0" dependencies = [ "anyhow", "async-stream", - "axon-types", "bytes", "ckb-fixed-hash", "ckb-ics-axon", diff --git a/Cargo.toml b/Cargo.toml index 0962cc8..6964745 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" @@ -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" diff --git a/contracts/ibc-sudt-transfer b/contracts/ibc-sudt-transfer index 24e66b6..3a57660 100644 Binary files a/contracts/ibc-sudt-transfer and b/contracts/ibc-sudt-transfer differ diff --git a/contracts/ics-channel b/contracts/ics-channel old mode 100644 new mode 100755 index 18d5a3c..0c142be Binary files a/contracts/ics-channel and b/contracts/ics-channel differ diff --git a/contracts/ics-packet b/contracts/ics-packet old mode 100644 new mode 100755 index 3a3cfa1..e6f84ff Binary files a/contracts/ics-packet and b/contracts/ics-packet differ diff --git a/contracts/version b/contracts/version index 5309e40..22dec9a 100644 --- a/contracts/version +++ b/contracts/version @@ -1 +1 @@ -Built at 59b5b8450b2db9b448d92e0df90df15376ab4ff4 (complete-axon-client) +Built at 8c363775ac066ce95dc713f5b80e53cd3a949e61 (write-commitment) diff --git a/src/config.rs b/src/config.rs index c0cecd0..23597e1 100644 --- a/src/config.rs +++ b/src/config.rs @@ -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, @@ -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() @@ -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) } @@ -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, diff --git a/src/json.rs b/src/json.rs index 58fa204..b7cd7b6 100644 --- a/src/json.rs +++ b/src/json.rs @@ -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; @@ -16,7 +16,6 @@ pub struct JsonIbcPacket { pub packet: Packet, #[serde(with = "JsonPacketStatus")] pub status: PacketStatus, - pub tx_hash: Option, #[serde_as(as = "Option")] pub ack: Option>, } @@ -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(), } } @@ -46,7 +44,6 @@ impl From for IbcPacket { Self { packet: value.packet, status: value.status, - tx_hash: value.tx_hash.map(|v| <[u8; 32]>::from(v).into()), ack: value.ack, } } @@ -56,7 +53,7 @@ impl From 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, @@ -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, @@ -92,7 +89,6 @@ enum JsonState { OpenTry, Open, Closed, - Frozen, } #[derive(Serialize, Deserialize)] @@ -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, + pub next_sequence_sends: u64, + pub next_sequence_recvs: u64, + pub next_sequence_acks: u64, + pub received_sequences: Vec, } #[derive(Serialize, Deserialize)] @@ -146,13 +142,14 @@ 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, + pub commitments: Vec<(H256, H256)>, } impl From<&Envelope> for JsonEnvelope { @@ -160,6 +157,11 @@ impl From<&Envelope> for JsonEnvelope { 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(), } } } @@ -169,6 +171,11 @@ impl From 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(), } } } diff --git a/src/transaction.rs b/src/transaction.rs index d2ad522..5f31aa2 100644 --- a/src/transaction.rs +++ b/src/transaction.rs @@ -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, @@ -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(), @@ -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() @@ -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(), }; @@ -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), }; @@ -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) @@ -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(), }; @@ -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(), }; @@ -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) @@ -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(), }; diff --git a/tests/json.rs b/tests/json.rs index 4ffda48..1b4a620 100644 --- a/tests/json.rs +++ b/tests/json.rs @@ -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); } diff --git a/tests/sudt-transfer.rs b/tests/sudt-transfer.rs index 0d59ace..b5afe15 100644 --- a/tests/sudt-transfer.rs +++ b/tests/sudt-transfer.rs @@ -1,5 +1,4 @@ use anyhow::Result; -use axon_types::metadata::Metadata; use bytes::Bytes; use ckb_ics_axon::{ handler::{IbcChannel, IbcPacket, PacketStatus}, @@ -78,7 +77,7 @@ fn test_send_packet() -> Result<()> { } .encode_to_vec(); - let axon_metadata_data = Metadata::new_builder().build().as_bytes(); + let axon_metadata_data = Bytes::from_static(b"metadata"); let axon_metadata_cell = context.deploy_cell(axon_metadata_data); let axon_metadata_type_script = get_type_script(&context, &axon_metadata_cell); let axon_metadata_cell_dep = simple_dep(axon_metadata_cell); @@ -174,7 +173,7 @@ fn test_write_ack_packet() -> Result<()> { Bytes::new(), ); - let axon_metadata_data = Metadata::new_builder().build().as_bytes(); + let axon_metadata_data = Bytes::from_static(b"metadata"); let axon_metadata_cell = context.deploy_cell(axon_metadata_data); let axon_metadata_type_script = context .get_cell(&axon_metadata_cell) @@ -275,7 +274,6 @@ fn test_write_ack_packet() -> Result<()> { source_port_id, ..Default::default() }, - tx_hash: None, status: PacketStatus::Recv, ack: None, }; @@ -299,6 +297,8 @@ fn test_write_ack_packet() -> Result<()> { msg_type: MsgType::MsgRecvPacket, // Invalid mock envelope content. content: vec![], + // Mock empty commitments. + commitments: vec![], }, packet, }; @@ -415,7 +415,6 @@ fn test_consume_ack_packet() -> Result<()> { data, ..Default::default() }, - tx_hash: None, status: PacketStatus::Ack, ack: Some(vec![if success { 1 } else { 0 }]), }; @@ -444,6 +443,8 @@ fn test_consume_ack_packet() -> Result<()> { msg_type: MsgType::MsgAckPacket, // Invalid mock envelope content. content: vec![], + // Mock empty commitments. + commitments: vec![], }, packet, }; diff --git a/tests/transaction.rs b/tests/transaction.rs index c38e5f7..7cedf73 100644 --- a/tests/transaction.rs +++ b/tests/transaction.rs @@ -1,5 +1,4 @@ use anyhow::Result; -use axon_types::metadata::Metadata; use bytes::Bytes; use ckb_ics_axon::{ handler::{IbcChannel, IbcPacket, PacketStatus}, @@ -39,7 +38,7 @@ fn test_send_packet() -> Result<()> { Bytes::new(), ); - let axon_metadata_data = Metadata::new_builder().build().as_bytes(); + let axon_metadata_data = Bytes::from_static(b"metadata"); let axon_metadata_cell = context.deploy_cell(axon_metadata_data); let axon_metadata_type_script = get_type_script(&context, &axon_metadata_cell); let axon_metadata_cell_dep = packed::CellDep::new_builder() @@ -132,7 +131,7 @@ fn test_write_ack_packet() -> Result<()> { Bytes::new(), ); - let axon_metadata_data = Metadata::new_builder().build().as_bytes(); + let axon_metadata_data = Bytes::from_static(b"metadata"); let axon_metadata_cell = context.deploy_cell(axon_metadata_data); let axon_metadata_type_script = context .get_cell(&axon_metadata_cell) @@ -198,7 +197,6 @@ fn test_write_ack_packet() -> Result<()> { packet: Packet { ..Default::default() }, - tx_hash: None, status: PacketStatus::Recv, ack: None, }; @@ -222,6 +220,8 @@ fn test_write_ack_packet() -> Result<()> { msg_type: MsgType::MsgRecvPacket, // Invalid mock envelope content. content: vec![], + // Mock empty commitments. + commitments: vec![], }, packet, }; @@ -297,7 +297,6 @@ fn test_consume_ack_packet() -> Result<()> { packet: Packet { ..Default::default() }, - tx_hash: None, status: PacketStatus::Ack, ack: Some(vec![1]), }; @@ -326,6 +325,8 @@ fn test_consume_ack_packet() -> Result<()> { msg_type: MsgType::MsgAckPacket, // Invalid mock envelope content. content: vec![], + // Mock empty commitments. + commitments: vec![], }, packet, }; @@ -365,7 +366,7 @@ fn test_channel_close_init() -> Result<()> { Bytes::new(), ); - let axon_metadata_data = Metadata::new_builder().build().as_bytes(); + let axon_metadata_data = Bytes::from_static(b"metadata"); let axon_metadata_cell = context.deploy_cell(axon_metadata_data); let axon_metadata_type_script = context .get_cell(&axon_metadata_cell)