From dc66aa6b72895a5d3edc28979335a5e360738046 Mon Sep 17 00:00:00 2001 From: yukang Date: Thu, 23 May 2024 08:52:17 +0800 Subject: [PATCH 01/28] use local dev rust-sdk --- Cargo.lock | 25 ++++++++++++------------- Cargo.toml | 3 ++- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c4b6693b..37ad5536 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -908,8 +908,7 @@ dependencies = [ [[package]] name = "ckb-sdk" version = "3.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8487deb2c0a77bbc4d33c812aa8f3f93c8786730c37a516eebfc4246602413ca" +source = "git+https://github.com/chenyukang/ckb-sdk-rust.git?branch=v3.1.0-with-pcn-udt#548bae29c205afcf238a5806806ddb106855f95a" dependencies = [ "anyhow", "bech32 0.8.1", @@ -1414,9 +1413,9 @@ checksum = "a47c1c47d2f5964e29c61246e81db715514cd532db6b5116a25ea3c03d6780a2" [[package]] name = "encoding_rs" -version = "0.8.33" +version = "0.8.34" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7268b386296a025e474d5140678f75d6de9493ae55a5d709eeb9dd08149945e1" +checksum = "b45de904aa0b010bce2ab45264d0631681847fa7b6f2eaa7dab7619943bc4f59" dependencies = [ "cfg-if 1.0.0", ] @@ -2625,9 +2624,9 @@ dependencies = [ [[package]] name = "parking_lot" -version = "0.12.1" +version = "0.12.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3742b2c103b9f06bc9fff0a37ff4912935851bee6d36f3c02bcc755bcfec228f" +checksum = "7e4af0ca4f6caed20e900d564c242b8e5d4903fdacf31d3daf527b66fe6f42fb" dependencies = [ "lock_api", "parking_lot_core", @@ -2983,9 +2982,9 @@ checksum = "c08c74e62047bb2de4ff487b251e4a92e24f48745648451635cec7d591162d9f" [[package]] name = "reqwest" -version = "0.11.26" +version = "0.11.27" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "78bf93c4af7a8bb7d879d51cebe797356ff10ae8516ace542b5182d9dcac10b2" +checksum = "dd67538700a17451e7cba03ac727fb961abb7607553461627b97de0b89cf4a62" dependencies = [ "base64 0.21.7", "bytes", @@ -3243,11 +3242,11 @@ dependencies = [ [[package]] name = "security-framework" -version = "2.9.2" +version = "2.11.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "05b64fb303737d99b81884b2c63433e9ae28abebe5eb5045dcdd175dc2ecf4de" +checksum = "c627723fd09706bacdb5cf41499e95098555af3c3c29d014dc3c458ef6be11c0" dependencies = [ - "bitflags 1.3.2", + "bitflags 2.5.0", "core-foundation", "core-foundation-sys", "libc", @@ -3256,9 +3255,9 @@ dependencies = [ [[package]] name = "security-framework-sys" -version = "2.9.1" +version = "2.11.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e932934257d3b408ed8f30db49d85ea163bfe74961f017f405b025af298f0c7a" +checksum = "317936bbbd05227752583946b9e66d7ce3b489f84e11a94a510b4437fef407d7" dependencies = [ "core-foundation-sys", "libc", diff --git a/Cargo.toml b/Cargo.toml index f3cf1b3c..6eb353b5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -38,7 +38,8 @@ tokio = { version = "1", features = [ "signal", ] } home = "0.5.9" -ckb-sdk = "3.1.0" +#ckb-sdk = "3.1.0" +ckb-sdk = { git = "https://github.com/chenyukang/ckb-sdk-rust.git", branch = "v3.1.0-with-pcn-udt" } thiserror = "1.0.58" anyhow = "1.0.81" env_logger = "0.11.3" From 715071a4c2bfa9467fc5b49061deb1a924d51234 Mon Sep 17 00:00:00 2001 From: yukang Date: Thu, 23 May 2024 16:33:42 +0800 Subject: [PATCH 02/28] add udt support for funding tx --- src/ckb_chain/funding/funding_tx.rs | 49 ++++++++++++++++++++++++----- 1 file changed, 42 insertions(+), 7 deletions(-) diff --git a/src/ckb_chain/funding/funding_tx.rs b/src/ckb_chain/funding/funding_tx.rs index 3cb491e4..da18f808 100644 --- a/src/ckb_chain/funding/funding_tx.rs +++ b/src/ckb_chain/funding/funding_tx.rs @@ -1,14 +1,15 @@ -use std::collections::HashMap; +use std::collections::{HashMap, HashSet}; use super::super::FundingError; use crate::ckb::serde_utils::EntityHex; +use anyhow::anyhow; use ckb_sdk::{ constants::SIGHASH_TYPE_HASH, traits::{ - CellCollector, CellDepResolver, DefaultCellCollector, DefaultCellDepResolver, - DefaultHeaderDepResolver, DefaultTransactionDependencyProvider, HeaderDepResolver, - SecpCkbRawKeySigner, TransactionDependencyProvider, + CellCollector, CellDepResolver, CellQueryOptions, DefaultCellCollector, + DefaultCellDepResolver, DefaultHeaderDepResolver, DefaultTransactionDependencyProvider, + HeaderDepResolver, SecpCkbRawKeySigner, TransactionDependencyProvider, ValueRangeOption, }, tx_builder::{unlock_tx, CapacityBalancer, TxBuilder, TxBuilderError}, unlock::{ScriptUnlocker, SecpSighashUnlocker}, @@ -16,9 +17,10 @@ use ckb_sdk::{ }; use ckb_types::{ core::{BlockView, Capacity, TransactionView}, - packed::{self, Script, Transaction}, + packed::{self, CellInput, Script, Transaction}, prelude::*, }; +use log::warn; use molecule::{ bytes::{BufMut as _, BytesMut}, prelude::*, @@ -96,11 +98,41 @@ struct FundingTxBuilder { impl TxBuilder for FundingTxBuilder { fn build_base( &self, - _cell_collector: &mut dyn CellCollector, - _cell_dep_resolver: &dyn CellDepResolver, + cell_collector: &mut dyn CellCollector, + cell_dep_resolver: &dyn CellDepResolver, _header_dep_resolver: &dyn HeaderDepResolver, _tx_dep_provider: &dyn TransactionDependencyProvider, ) -> Result { + // Build inputs + let mut inputs = vec![]; + let mut cell_deps = HashSet::new(); + if let Some(ref udt_info) = self.request.udt_info { + let udt_type_script = udt_info.type_script.clone(); + let owner = self.context.funding_source_lock_script.clone(); + let owner_query = { + let mut query = CellQueryOptions::new_lock(udt_type_script.clone()); + query.secondary_script_len_range = Some(ValueRangeOption::new_exact(0)); + query.data_len_range = Some(ValueRangeOption::new_exact(0)); + query + }; + + let (owner_cells, _) = cell_collector.collect_live_cells(&owner_query, true)?; + if owner_cells.is_empty() { + return Err(TxBuilderError::Other(anyhow!("owner cell not found"))); + } + inputs = vec![CellInput::new(owner_cells[0].out_point.clone(), 0)]; + + let owner_cell_dep = cell_dep_resolver + .resolve(&owner) + .ok_or_else(|| TxBuilderError::ResolveCellDepFailed(owner.clone()))?; + let udt_cell_dep = cell_dep_resolver + .resolve(&udt_type_script) + .ok_or_else(|| TxBuilderError::ResolveCellDepFailed(udt_type_script.clone()))?; + #[allow(clippy::mutable_key_type)] + cell_deps.insert(owner_cell_dep); + cell_deps.insert(udt_cell_dep); + } + let funding_cell = self .build_funding_cell() .map_err(|err| TxBuilderError::Other(err.into()))?; @@ -121,8 +153,10 @@ impl TxBuilder for FundingTxBuilder { None => packed::Transaction::default().as_advanced_builder(), }; let tx = builder + .set_inputs(inputs) .set_outputs(outputs) .set_outputs_data(outputs_data) + .set_cell_deps(cell_deps.into_iter().collect()) .build(); Ok(tx) @@ -175,6 +209,7 @@ impl FundingTxBuilder { .capacity(Capacity::shannons(ckb_amount).pack()) .lock(self.context.funding_cell_lock_script.clone()) .build(); + warn!("yukang debug ckb_output: {:?}", ckb_output); Ok((ckb_output, packed::Bytes::default())) } } From d09775e41e2a4d0d17bb0c495a9bbf4166bc5b9f Mon Sep 17 00:00:00 2001 From: yukang Date: Sat, 25 May 2024 09:52:50 +0800 Subject: [PATCH 03/28] fix funding_tx --- Cargo.lock | 24 +-- src/ckb/channel.rs | 203 +++++++++++++----- src/ckb/network.rs | 36 +++- src/ckb_chain/funding/funding_tx.rs | 122 ++++++++--- .../bruno/e2e/udt/01-node1-connect-node2.bru | 35 +++ .../bruno/e2e/udt/02-node2-connect-node3.bru | 35 +++ .../udt/03-node1-node2-open-channel-wrong.bru | 41 ++++ .../e2e/udt/04-node2-accept-channel-wrong.bru | 40 ++++ ...ode1-node2-open-channel-script-invalid.bru | 41 ++++ ...06-node2-accept-channel-script-invalid.bru | 46 ++++ .../07-node1-node2-open-channel-normal.bru | 41 ++++ .../udt/08-node2-accept-channel-normal.bru | 41 ++++ 12 files changed, 608 insertions(+), 97 deletions(-) create mode 100644 tests/bruno/e2e/udt/01-node1-connect-node2.bru create mode 100644 tests/bruno/e2e/udt/02-node2-connect-node3.bru create mode 100644 tests/bruno/e2e/udt/03-node1-node2-open-channel-wrong.bru create mode 100644 tests/bruno/e2e/udt/04-node2-accept-channel-wrong.bru create mode 100644 tests/bruno/e2e/udt/05-node1-node2-open-channel-script-invalid.bru create mode 100644 tests/bruno/e2e/udt/06-node2-accept-channel-script-invalid.bru create mode 100644 tests/bruno/e2e/udt/07-node1-node2-open-channel-normal.bru create mode 100644 tests/bruno/e2e/udt/08-node2-accept-channel-normal.bru diff --git a/Cargo.lock b/Cargo.lock index 37ad5536..9446c522 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -908,7 +908,7 @@ dependencies = [ [[package]] name = "ckb-sdk" version = "3.1.0" -source = "git+https://github.com/chenyukang/ckb-sdk-rust.git?branch=v3.1.0-with-pcn-udt#548bae29c205afcf238a5806806ddb106855f95a" +source = "git+https://github.com/chenyukang/ckb-sdk-rust.git?branch=v3.1.0-with-pcn-udt#4b712fa35652bcd8b1f9e832cf0ad85681d0c6f5" dependencies = [ "anyhow", "bech32 0.8.1", @@ -1413,9 +1413,9 @@ checksum = "a47c1c47d2f5964e29c61246e81db715514cd532db6b5116a25ea3c03d6780a2" [[package]] name = "encoding_rs" -version = "0.8.34" +version = "0.8.33" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b45de904aa0b010bce2ab45264d0631681847fa7b6f2eaa7dab7619943bc4f59" +checksum = "7268b386296a025e474d5140678f75d6de9493ae55a5d709eeb9dd08149945e1" dependencies = [ "cfg-if 1.0.0", ] @@ -2624,9 +2624,9 @@ dependencies = [ [[package]] name = "parking_lot" -version = "0.12.2" +version = "0.12.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7e4af0ca4f6caed20e900d564c242b8e5d4903fdacf31d3daf527b66fe6f42fb" +checksum = "3742b2c103b9f06bc9fff0a37ff4912935851bee6d36f3c02bcc755bcfec228f" dependencies = [ "lock_api", "parking_lot_core", @@ -2982,9 +2982,9 @@ checksum = "c08c74e62047bb2de4ff487b251e4a92e24f48745648451635cec7d591162d9f" [[package]] name = "reqwest" -version = "0.11.27" +version = "0.11.26" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dd67538700a17451e7cba03ac727fb961abb7607553461627b97de0b89cf4a62" +checksum = "78bf93c4af7a8bb7d879d51cebe797356ff10ae8516ace542b5182d9dcac10b2" dependencies = [ "base64 0.21.7", "bytes", @@ -3242,11 +3242,11 @@ dependencies = [ [[package]] name = "security-framework" -version = "2.11.0" +version = "2.9.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c627723fd09706bacdb5cf41499e95098555af3c3c29d014dc3c458ef6be11c0" +checksum = "05b64fb303737d99b81884b2c63433e9ae28abebe5eb5045dcdd175dc2ecf4de" dependencies = [ - "bitflags 2.5.0", + "bitflags 1.3.2", "core-foundation", "core-foundation-sys", "libc", @@ -3255,9 +3255,9 @@ dependencies = [ [[package]] name = "security-framework-sys" -version = "2.11.0" +version = "2.9.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "317936bbbd05227752583946b9e66d7ce3b489f84e11a94a510b4437fef407d7" +checksum = "e932934257d3b408ed8f30db49d85ea163bfe74961f017f405b025af298f0c7a" dependencies = [ "core-foundation-sys", "libc", diff --git a/src/ckb/channel.rs b/src/ckb/channel.rs index 8e9dc553..28feb513 100644 --- a/src/ckb/channel.rs +++ b/src/ckb/channel.rs @@ -2,8 +2,8 @@ use bitflags::bitflags; use ckb_hash::{blake2b_256, new_blake2b}; use ckb_sdk::Since; use ckb_types::{ - core::{TransactionBuilder, TransactionView}, - packed::{Bytes, CellInput, CellOutput, OutPoint, Script, Transaction}, + core::{Capacity, TransactionBuilder, TransactionView}, + packed::{self, Bytes, CellInput, CellOutput, OutPoint, Script, Transaction}, prelude::{AsTransactionBuilder, IntoTransactionView, Pack, Unpack}, }; @@ -35,7 +35,7 @@ use crate::{ ckb::types::Shutdown, ckb_chain::{ contracts::{get_cell_deps_by_contracts, get_script_by_contract, Contract}, - FundingRequest, + FundingRequest, FundingUdtInfo, }, NetworkServiceEvent, }; @@ -146,7 +146,7 @@ pub enum ChannelInitializationParameter { /// To open a new channel to another peer, the funding amount, /// the temporary channel id a unique channel seed to generate /// channel secrets must be given. - OpenChannel(u128, [u8; 32], oneshot::Sender), + OpenChannel(u128, [u8; 32], Option