diff --git a/src/fiber/channel.rs b/src/fiber/channel.rs index 7680de47..7eea9117 100644 --- a/src/fiber/channel.rs +++ b/src/fiber/channel.rs @@ -4471,10 +4471,18 @@ impl ChannelActorState { ))?; if first_output.lock() != self.get_funding_lock_script() { - error!("Checking if transaction final failed as tx's first output's script is not funding lock: tx: {:?}, first output lock script: {:?}, funding lock script: {:?}", - &tx, first_output.lock(), self.get_funding_lock_script()); - // TODO: return an error here. We panic because we want to move fast. - panic!("Invalid funding transation") + return Err(ProcessingChannelError::InvalidState( + "Invalid funding transation lock script".to_string(), + )); + } + + let current_capacity: u64 = first_output.capacity().unpack(); + + // make sure both parties have paid the reserved ckb amount + if current_capacity <= self.local_reserved_ckb_amount + || current_capacity <= self.remote_reserved_ckb_amount + { + return Ok(false); } if self.funding_udt_type_script.is_some() { @@ -4491,9 +4499,11 @@ impl ChannelActorState { "udt_amount: {}, to_remote_amount: {}, to_local_amount: {}", udt_amount, self.to_remote_amount, self.to_local_amount ); - return Ok(udt_amount == self.to_remote_amount + self.to_local_amount); + debug!("current_capacity: {}, remote_reserved_ckb_amount: {}, local_reserved_ckb_amount: {}", + current_capacity, self.remote_reserved_ckb_amount, self.local_reserved_ckb_amount); + let is_udt_amount_ok = udt_amount == self.to_remote_amount + self.to_local_amount; + return Ok(is_udt_amount_ok); } else { - let current_capacity: u64 = first_output.capacity().unpack(); let is_complete = current_capacity == (self.to_local_amount + self.to_remote_amount diff --git a/tests/bruno/e2e/udt/11-node1-node2-open-channel-no-auto-accept.bru b/tests/bruno/e2e/udt/11-node1-node2-open-channel-no-auto-accept.bru new file mode 100644 index 00000000..b41cea4f --- /dev/null +++ b/tests/bruno/e2e/udt/11-node1-node2-open-channel-no-auto-accept.bru @@ -0,0 +1,48 @@ +meta { + name: Node1 open a channel to Node2, that should not be auto accepted + type: http + seq: 11 +} + +post { + url: {{NODE1_RPC_URL}} + body: json + auth: none +} + +headers { + Content-Type: application/json + Accept: application/json +} + + +body:json { + { + "id": "42", + "jsonrpc": "2.0", + "method": "open_channel", + "params": [ + { + "peer_id": "{{NODE2_PEERID}}", + "funding_amount": "0x200", + "funding_udt_type_script": { + "code_hash": "{{UDT_CODE_HASH}}", + "hash_type": "data1", + "args": "0x32e555f3ff8e135cece1351a6a2971518392c1e30375c1e006ad0ce8eac07947" + } + } + ] + } +} + +assert { + res.body.error: isUndefined + res.body.result.temporary_channel_id: isDefined +} + +script:post-response { + await new Promise(r => setTimeout(r, 1000)); + console.log("N1N2 response: ", res.body); + console.log("N1N2 response: ", res.body.result.temporary_channel_id); + bru.setVar("N1N2_TEMP_CHANNEL_ID", res.body.result.temporary_channel_id); +} diff --git a/tests/bruno/e2e/udt/12-node2-accept-channel.bru b/tests/bruno/e2e/udt/12-node2-accept-channel.bru new file mode 100644 index 00000000..1309c58f --- /dev/null +++ b/tests/bruno/e2e/udt/12-node2-accept-channel.bru @@ -0,0 +1,40 @@ +meta { + name: node2 accept udt channel + type: http + seq: 12 +} + +post { + url: {{NODE2_RPC_URL}} + body: json + auth: none +} + +headers { + Content-Type: application/json + Accept: application/json +} + +body:json { + { + "id": "42", + "jsonrpc": "2.0", + "method": "accept_channel", + "params": [ + { + "temporary_channel_id": "{{N1N2_TEMP_CHANNEL_ID}}", + "funding_amount": "0x0" + } + ] + } +} + +assert { + res.body.error: isUndefined +} + +script:post-response { + // Sleep for sometime to make sure current operation finishes before next request starts. + await new Promise(r => setTimeout(r, 1000)); + console.log("accept channel result: ", res.body); +} diff --git a/tests/bruno/e2e/udt/13-ckb-generate-blocks.bru b/tests/bruno/e2e/udt/13-ckb-generate-blocks.bru new file mode 100644 index 00000000..f1ecc40e --- /dev/null +++ b/tests/bruno/e2e/udt/13-ckb-generate-blocks.bru @@ -0,0 +1,33 @@ +meta { + name: generate a few epochs + type: http + seq: 13 +} + +post { + url: {{CKB_RPC_URL}} + body: json + auth: none +} + +headers { + Content-Type: application/json + Accept: application/json +} + +body:json { + { + "id": 42, + "jsonrpc": "2.0", + "method": "generate_epochs", + "params": ["0x2"] + } +} + +assert { + res.status: eq 200 +} + +script:post-response { + await new Promise(r => setTimeout(r, 5000)); +} diff --git a/tests/bruno/e2e/udt/14-node2-list-channel-expect-two-channel.bru b/tests/bruno/e2e/udt/14-node2-list-channel-expect-two-channel.bru new file mode 100644 index 00000000..b84a84e8 --- /dev/null +++ b/tests/bruno/e2e/udt/14-node2-list-channel-expect-two-channel.bru @@ -0,0 +1,43 @@ +meta { + name: get channel list from node2 + type: http + seq: 14 +} + +post { + url: {{NODE2_RPC_URL}} + body: json + auth: none +} + +headers { + Content-Type: application/json + Accept: application/json +} + +body:json { + { + "id": "42", + "jsonrpc": "2.0", + "method": "list_channels", + "params": [ + { + "peer_id": "{{NODE1_PEERID}}" + } + ] + } +} + +assert { + res.body.error: isUndefined + res.body.result.channels: isDefined +} + +script:post-response { + // Sleep for sometime to make sure current operation finishes before next request starts. + await new Promise(r => setTimeout(r, 2000)); + console.log("accept channel result: ", res.body.result.channels); + if (res.body.result.channels.length != 2) { + throw new Error("Assertion failed: expect there are 2 channels"); + } +} diff --git a/tests/bruno/e2e/udt/11-node1-send-shutdown-channel.bru b/tests/bruno/e2e/udt/15-node1-send-shutdown-channel.bru similarity index 93% rename from tests/bruno/e2e/udt/11-node1-send-shutdown-channel.bru rename to tests/bruno/e2e/udt/15-node1-send-shutdown-channel.bru index c43e197d..ccf05d0c 100644 --- a/tests/bruno/e2e/udt/11-node1-send-shutdown-channel.bru +++ b/tests/bruno/e2e/udt/15-node1-send-shutdown-channel.bru @@ -1,7 +1,7 @@ meta { name: Node1 send shutdown channel1 type: http - seq: 11 + seq: 15 } post { @@ -41,5 +41,5 @@ assert { script:post-response { // Sleep for sometime to make sure current operation finishes before next request starts. - await new Promise(r => setTimeout(r, 100)); + await new Promise(r => setTimeout(r, 1000)); } diff --git a/tests/bruno/e2e/udt/12-node2-send-shutdown-channel-error.bru b/tests/bruno/e2e/udt/16-node2-send-shutdown-channel-error.bru similarity index 98% rename from tests/bruno/e2e/udt/12-node2-send-shutdown-channel-error.bru rename to tests/bruno/e2e/udt/16-node2-send-shutdown-channel-error.bru index 729b7c5f..3923ae0f 100644 --- a/tests/bruno/e2e/udt/12-node2-send-shutdown-channel-error.bru +++ b/tests/bruno/e2e/udt/16-node2-send-shutdown-channel-error.bru @@ -1,7 +1,7 @@ meta { name: Node2 send shutdown channel1 type: http - seq: 12 + seq: 16 } post { diff --git a/tests/deploy/udt-init/src/main.rs b/tests/deploy/udt-init/src/main.rs index e123159c..3ae05f0d 100644 --- a/tests/deploy/udt-init/src/main.rs +++ b/tests/deploy/udt-init/src/main.rs @@ -123,8 +123,6 @@ fn init_or_send_udt( )?; let json_tx = ckb_jsonrpc_types::TransactionView::from(tx_with_groups.get_tx_view().clone()); - println!("tx: {}", serde_json::to_string_pretty(&json_tx).unwrap()); - if apply { let tx_hash = CkbRpcClient::new(network_info.url.as_str()) .send_transaction(json_tx.inner, None) @@ -143,11 +141,10 @@ fn init_or_send_udt( fn generate_blocks(num: u64) -> Result<(), Box> { let network_info = NetworkInfo::devnet(); let rpc_client = CkbRpcClient::new(network_info.url.as_str()); - for i in 0..num { + for _i in 0..num { rpc_client.generate_block()?; // sleep 200ms std::thread::sleep(std::time::Duration::from_millis(200)); - eprintln!("block generated: {}", i); } Ok(()) } @@ -208,7 +205,7 @@ fn genrate_nodes_config() { for udt in UDT_KINDS { let udt_info = UdtInfo { name: udt.to_string(), - auto_accept_amount: Some(0), + auto_accept_amount: Some(1000), script: UdtScript { code_hash: get_code_hash(udt), hash_type: "Data1".to_string(),