Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix get sent/received tlc value #94

Merged
merged 1 commit into from
Jun 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading