Skip to content

Commit

Permalink
Merge pull request #94 from contrun/fix-balance-calculation
Browse files Browse the repository at this point in the history
Fix get sent/received tlc value
  • Loading branch information
quake authored Jun 26, 2024
2 parents ee4701b + 14fefe8 commit b989b73
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 @@ -1655,11 +1655,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 @@ -1913,8 +1917,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 @@ -1923,8 +1930,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 @@ -2202,18 +2212,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 b989b73

Please sign in to comment.