From 9b35102a1dde6ce3f2a03ffb261dabd53c29e492 Mon Sep 17 00:00:00 2001 From: yukang Date: Thu, 20 Jun 2024 17:49:24 +0800 Subject: [PATCH] add more tests for tlc amounts --- src/ckb/channel.rs | 40 ++++++++----------- .../08-add-tlc-from-NODE1.bru | 4 +- .../09-add-tlc-from-NODE3.bru | 1 + ...u => 10-add-tlc-from-NODE1-amount-err.bru} | 15 ++++--- ... => 10-add-tlc-from-NODE1-balance-err.bru} | 6 +-- .../14-remove-tlc-from-NODE3.bru | 5 ++- .../18-remove-tlc-from-NODE3.bru | 8 ++-- 7 files changed, 38 insertions(+), 41 deletions(-) rename tests/bruno/e2e/open-use-close-a-channel/{19-remove-tlc-error-from-NODE1.bru => 10-add-tlc-from-NODE1-amount-err.bru} (67%) rename tests/bruno/e2e/open-use-close-a-channel/{10-add-tlc-from-NODE1.bru => 10-add-tlc-from-NODE1-balance-err.bru} (85%) diff --git a/src/ckb/channel.rs b/src/ckb/channel.rs index ed352b5f..e53d0111 100644 --- a/src/ckb/channel.rs +++ b/src/ckb/channel.rs @@ -536,8 +536,6 @@ impl ChannelActor { ) -> Result { warn!("handle add tlc command : {:?}", &command); state.check_state_for_tlc_update()?; - state.check_add_tlc_amount(command.amount)?; - let tlc = state.create_outbounding_tlc(command); state.insert_tlc(tlc)?; @@ -1873,24 +1871,31 @@ impl ChannelActorState { ))); } }; + if tlc.amount == 0 { + return Err(ProcessingChannelError::InvalidParameter( + "Expect the amount of tlc is larger than zero".to_string(), + )); + } if tlc.is_offered() { let sent_tlc_value = self.get_sent_tlc_balance(); - debug_assert!(self.to_local_amount > sent_tlc_value); - // TODO: handle transaction fee here. + debug_assert!(self.to_local_amount >= sent_tlc_value); if sent_tlc_value + tlc.amount > self.to_local_amount { return Err(ProcessingChannelError::InvalidParameter(format!( "Adding tlc {:?} with amount {} exceeds local balance {}", - tlc.id, tlc.amount, self.to_local_amount + tlc.id, + tlc.amount, + self.to_local_amount - sent_tlc_value ))); } } else { let received_tlc_value = self.get_received_tlc_balance(); - debug_assert!(self.to_remote_amount > received_tlc_value); - // TODO: handle transaction fee here. + debug_assert!(self.to_remote_amount >= received_tlc_value); if received_tlc_value + tlc.amount > self.to_remote_amount { return Err(ProcessingChannelError::InvalidParameter(format!( "Adding tlc {:?} with amount {} exceeds remote balance {}", - tlc.id, tlc.amount, self.to_remote_amount + tlc.id, + tlc.amount, + self.to_remote_amount - received_tlc_value ))); } } @@ -1946,7 +1951,8 @@ impl ChannelActorState { } Some((current_remove_reason, current_removed_at)) => { return Err(ProcessingChannelError::InvalidParameter( - format!("Illegally removing the same tlc: {:?} was previously removed at {:?} for {:?}, and trying to remove it again at {:?} for {:?}", tlc_id, current_removed_at, reason, removed_at, current_remove_reason))); + format!("Illegally removing the same tlc: {:?} was previously removed at {:?} for {:?}, and trying to remove it again at {:?} for {:?}", + tlc_id, current_removed_at, reason, removed_at, current_remove_reason))); } None => { debug!( @@ -2334,21 +2340,7 @@ impl ChannelActorState { } } - fn check_add_tlc_amount(&self, amount: u128) -> ProcessingChannelResult { - debug!( - "check_add_tlc_amount: {} available_amount: {}", - amount, self.to_local_amount - ); - if amount > self.to_local_amount { - return Err(ProcessingChannelError::InvalidParameter(format!( - "Local balance is not enough to add tlc with amount {}, you can add at most {}", - amount, self.to_local_amount - ))); - } - Ok(()) - } - - pub fn create_outbounding_tlc(&mut self, command: AddTlcCommand) -> TLC { + pub fn create_outbounding_tlc(&self, command: AddTlcCommand) -> TLC { // TODO: we are filling the user command with a new id here. // The advantage of this is that we don't need to burden the users to // provide a next id for each tlc. The disadvantage is that users may diff --git a/tests/bruno/e2e/open-use-close-a-channel/08-add-tlc-from-NODE1.bru b/tests/bruno/e2e/open-use-close-a-channel/08-add-tlc-from-NODE1.bru index e83b92ca..9bdff254 100644 --- a/tests/bruno/e2e/open-use-close-a-channel/08-add-tlc-from-NODE1.bru +++ b/tests/bruno/e2e/open-use-close-a-channel/08-add-tlc-from-NODE1.bru @@ -39,5 +39,7 @@ assert { script:post-response { // Sleep for sometime to make sure current operation finishes before next request starts. await new Promise(r => setTimeout(r, 100)); - // bru.setVar("TLC_ID2", res.body.result.tlc_id); + // Node 1 only holds 62 CKB as reserved amount, there is no extra amount for TLC, so the request will fail. + // Set the TLC ID into a wrong one, than step 14 will fail as expected. + bru.setVar("TLC_ID2", "0x3200"); } diff --git a/tests/bruno/e2e/open-use-close-a-channel/09-add-tlc-from-NODE3.bru b/tests/bruno/e2e/open-use-close-a-channel/09-add-tlc-from-NODE3.bru index 55a524a7..094a3eb9 100644 --- a/tests/bruno/e2e/open-use-close-a-channel/09-add-tlc-from-NODE3.bru +++ b/tests/bruno/e2e/open-use-close-a-channel/09-add-tlc-from-NODE3.bru @@ -37,6 +37,7 @@ assert { } script:post-response { + console.log("step 9 response: ", res.body); // Sleep for sometime to make sure current operation finishes before next request starts. await new Promise(r => setTimeout(r, 100)); bru.setVar("TLC_ID3", res.body.result.tlc_id); diff --git a/tests/bruno/e2e/open-use-close-a-channel/19-remove-tlc-error-from-NODE1.bru b/tests/bruno/e2e/open-use-close-a-channel/10-add-tlc-from-NODE1-amount-err.bru similarity index 67% rename from tests/bruno/e2e/open-use-close-a-channel/19-remove-tlc-error-from-NODE1.bru rename to tests/bruno/e2e/open-use-close-a-channel/10-add-tlc-from-NODE1-amount-err.bru index b7f087ff..1dc88d01 100644 --- a/tests/bruno/e2e/open-use-close-a-channel/19-remove-tlc-error-from-NODE1.bru +++ b/tests/bruno/e2e/open-use-close-a-channel/10-add-tlc-from-NODE1-amount-err.bru @@ -1,7 +1,7 @@ meta { - name: remove tlc from NODE1 + name: add tlc from NODE1 type: http - seq: 19 + seq: 10 } post { @@ -19,14 +19,13 @@ body:json { { "id": "42", "jsonrpc": "2.0", - "method": "remove_tlc", + "method": "add_tlc", "params": [ { "channel_id": "{{CHANNEL_ID}}", - "tlc_id": "{{TLC_ID5}}", - "reason": { - "payment_preimage": "0x0000000000000000000000000000000000000000000000000000000000000042" - } + "amount": "0x0", + "payment_hash": "0xcb7bce98a778f130d34da522623d7e56705bddfe0dc4781bd2331211134a19a5", + "expiry": 40 } ] } @@ -38,7 +37,7 @@ assert { } script:post-response { + console.log("step 10 response: ", res.body); // Sleep for sometime to make sure current operation finishes before next request starts. await new Promise(r => setTimeout(r, 100)); - console.log(res.body); } diff --git a/tests/bruno/e2e/open-use-close-a-channel/10-add-tlc-from-NODE1.bru b/tests/bruno/e2e/open-use-close-a-channel/10-add-tlc-from-NODE1-balance-err.bru similarity index 85% rename from tests/bruno/e2e/open-use-close-a-channel/10-add-tlc-from-NODE1.bru rename to tests/bruno/e2e/open-use-close-a-channel/10-add-tlc-from-NODE1-balance-err.bru index d587ba7f..9f557b11 100644 --- a/tests/bruno/e2e/open-use-close-a-channel/10-add-tlc-from-NODE1.bru +++ b/tests/bruno/e2e/open-use-close-a-channel/10-add-tlc-from-NODE1-balance-err.bru @@ -32,12 +32,12 @@ body:json { } assert { - res.body.error: isUndefined - res.body.result.tlc_id: isDefined + res.body.error: isDefined + res.body.result: isUndefined } script:post-response { + console.log("step 10 response: ", res.body); // Sleep for sometime to make sure current operation finishes before next request starts. await new Promise(r => setTimeout(r, 100)); - bru.setVar("TLC_ID4", res.body.result.tlc_id); } diff --git a/tests/bruno/e2e/open-use-close-a-channel/14-remove-tlc-from-NODE3.bru b/tests/bruno/e2e/open-use-close-a-channel/14-remove-tlc-from-NODE3.bru index 7b169f58..dd358ac0 100644 --- a/tests/bruno/e2e/open-use-close-a-channel/14-remove-tlc-from-NODE3.bru +++ b/tests/bruno/e2e/open-use-close-a-channel/14-remove-tlc-from-NODE3.bru @@ -33,11 +33,12 @@ body:json { } assert { - res.body.error: isUndefined - res.body.result: isNull + res.body.error: isDefined + res.body.result: isUndefined } script:post-response { + console.log("step 14: ", res.body); // Sleep for sometime to make sure current operation finishes before next request starts. await new Promise(r => setTimeout(r, 100)); } diff --git a/tests/bruno/e2e/open-use-close-a-channel/18-remove-tlc-from-NODE3.bru b/tests/bruno/e2e/open-use-close-a-channel/18-remove-tlc-from-NODE3.bru index 4306509b..d51641c1 100644 --- a/tests/bruno/e2e/open-use-close-a-channel/18-remove-tlc-from-NODE3.bru +++ b/tests/bruno/e2e/open-use-close-a-channel/18-remove-tlc-from-NODE3.bru @@ -23,7 +23,7 @@ body:json { "params": [ { "channel_id": "{{CHANNEL_ID}}", - "tlc_id": "{{TLC_ID4}}", + "tlc_id": "{{TLC_ID3}}", "reason": { "error_code": "0x2a" } @@ -33,11 +33,13 @@ body:json { } assert { - res.body.error: isUndefined - res.body.result: isNull + res.body.error: isDefined + res.body.result: isUndefined } script:post-response { + console.log("step 18: ", res.body); + // This request is remove a TLC which already removed in previous step. // Sleep for sometime to make sure current operation finishes before next request starts. await new Promise(r => setTimeout(r, 100)); }