diff --git a/src/ckb/channel.rs b/src/ckb/channel.rs index 2a8d1206..521abf47 100644 --- a/src/ckb/channel.rs +++ b/src/ckb/channel.rs @@ -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::() } 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::() } fn update_state(&mut self, new_state: ChannelState) { @@ -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!( @@ -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!( @@ -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::() - } else { - self.get_active_received_tlcs(local_commitment) - .map(|tlc| tlc.tlc.amount) - .sum::() - } - } - // 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 {