Skip to content

Commit

Permalink
Fix get sent/received tlc value
Browse files Browse the repository at this point in the history
  • Loading branch information
contrun committed Jun 20, 2024
1 parent 3c14178 commit 14fefe8
Showing 1 changed file with 14 additions and 16 deletions.
30 changes: 14 additions & 16 deletions src/ckb/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1601,11 +1601,15 @@ impl ChannelActorState {
}

pub fn get_sent_tlc_balance(&self) -> u128 {
self.get_tlc_value_sent_by_local(true)
self.get_active_offered_tlcs(true)
.map(|tlc| tlc.tlc.amount)
.sum::<u128>()
}

pub fn get_received_tlc_balance(&self) -> u128 {
self.get_tlc_value_received_from_remote(false)
self.get_active_received_tlcs(false)
.map(|tlc| tlc.tlc.amount)
.sum::<u128>()
}

fn update_state(&mut self, new_state: ChannelState) {
Expand Down Expand Up @@ -1806,8 +1810,11 @@ impl ChannelActorState {
}
};
if tlc.is_offered() {
// TODO: We should actually also consider all our fulfilled tlcs here.
// Because this is also the amount that we can actually spend.
let sent_tlc_value = self.get_sent_tlc_balance();
debug_assert!(self.to_local_amount > sent_tlc_value);
debug!("Value of local sent tlcs: {}", sent_tlc_value);
debug_assert!(self.to_local_amount >= sent_tlc_value);
// TODO: handle transaction fee here.
if sent_tlc_value + tlc.amount > self.to_local_amount {
return Err(ProcessingChannelError::InvalidParameter(format!(
Expand All @@ -1816,8 +1823,11 @@ impl ChannelActorState {
)));
}
} else {
// TODO: We should actually also consider all their fulfilled tlcs here.
// Because this is also the amount that we can actually spend.
let received_tlc_value = self.get_received_tlc_balance();
debug_assert!(self.to_remote_amount > received_tlc_value);
debug!("Value of remote received tlcs: {}", received_tlc_value);
debug_assert!(self.to_remote_amount >= received_tlc_value);
// TODO: handle transaction fee here.
if received_tlc_value + tlc.amount > self.to_remote_amount {
return Err(ProcessingChannelError::InvalidParameter(format!(
Expand Down Expand Up @@ -2089,18 +2099,6 @@ impl ChannelActorState {
})
}

fn get_tlc_value_sent_by_local(&self, local_commitment: bool) -> u128 {
if local_commitment {
self.get_active_offered_tlcs(local_commitment)
.map(|tlc| tlc.tlc.amount)
.sum::<u128>()
} else {
self.get_active_received_tlcs(local_commitment)
.map(|tlc| tlc.tlc.amount)
.sum::<u128>()
}
}

// The parameter local indicates whether we are interested in the value sent by the local party.
fn get_tlc_value_received_from_remote(&self, local_commitment: bool) -> u128 {
if local_commitment {
Expand Down

0 comments on commit 14fefe8

Please sign in to comment.