Skip to content

Commit

Permalink
add more tests for tlc amounts
Browse files Browse the repository at this point in the history
  • Loading branch information
chenyukang committed Jun 22, 2024
1 parent 7a76f94 commit 9b35102
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 41 deletions.
40 changes: 16 additions & 24 deletions src/ckb/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -536,8 +536,6 @@ impl<S> ChannelActor<S> {
) -> Result<u64, ProcessingChannelError> {
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)?;

Expand Down Expand Up @@ -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
)));
}
}
Expand Down Expand Up @@ -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!(
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
meta {
name: remove tlc from NODE1
name: add tlc from NODE1
type: http
seq: 19
seq: 10
}

post {
Expand All @@ -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
}
]
}
Expand All @@ -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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ body:json {
"params": [
{
"channel_id": "{{CHANNEL_ID}}",
"tlc_id": "{{TLC_ID4}}",
"tlc_id": "{{TLC_ID3}}",
"reason": {
"error_code": "0x2a"
}
Expand All @@ -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));
}

0 comments on commit 9b35102

Please sign in to comment.