diff --git a/src/fiber/channel.rs b/src/fiber/channel.rs index c03ffbe4..a13965a7 100644 --- a/src/fiber/channel.rs +++ b/src/fiber/channel.rs @@ -532,7 +532,7 @@ where let channel_id = state.get_id(); let tlc_details = state - .remove_tlc_with_reason(TLCId::Offered(remove_tlc.tlc_id), remove_tlc.reason)?; + .remove_tlc_with_reason(TLCId::Sent(remove_tlc.tlc_id), remove_tlc.reason)?; if let ( Some(ref udt_type_script), RemoveTlcReason::RemoveTlcFulfill(RemoveTlcFulfill { payment_preimage }), @@ -1727,32 +1727,32 @@ impl TLCIds { #[derive(Copy, Clone, Debug, PartialEq, Eq, Serialize, Deserialize, PartialOrd, Ord)] pub enum TLCId { - Offered(u64), + Sent(u64), Received(u64), } impl From for u64 { fn from(id: TLCId) -> u64 { match id { - TLCId::Offered(id) => id, + TLCId::Sent(id) => id, TLCId::Received(id) => id, } } } impl TLCId { - pub fn is_offered(&self) -> bool { - matches!(self, TLCId::Offered(_)) + pub fn is_sent(&self) -> bool { + matches!(self, TLCId::Sent(_)) } pub fn is_received(&self) -> bool { - !self.is_offered() + !self.is_sent() } pub fn flip(&self) -> Self { match self { - TLCId::Offered(id) => TLCId::Received(*id), - TLCId::Received(id) => TLCId::Offered(*id), + TLCId::Sent(id) => TLCId::Received(*id), + TLCId::Received(id) => TLCId::Sent(*id), } } @@ -2726,8 +2726,8 @@ impl ChannelActorState { self.to_remote_amount } - pub fn get_offered_tlc_balance(&self) -> u128 { - self.get_active_offered_tlcs(true) + pub fn get_sent_tlc_balance(&self) -> u128 { + self.get_active_sent_tlcs(true) .map(|tlc| tlc.tlc.amount) .sum::() } @@ -3021,7 +3021,7 @@ impl ChannelActorState { self.tlcs .values() .filter(|tlc| { - !tlc.is_offered() && tlc.creation_confirmed_at.is_some() && tlc.removed_at.is_none() + !tlc.is_sent() && tlc.creation_confirmed_at.is_some() && tlc.removed_at.is_none() }) .cloned() .collect() @@ -3069,7 +3069,7 @@ impl ChannelActorState { tlc.removal_confirmed_at = Some(commitment_numbers); match reason { RemoveTlcReason::RemoveTlcFulfill(_) => { - if tlc.is_offered(){ + if tlc.is_sent(){ to_local_amount -= amount; to_remote_amount += amount; } else { @@ -3202,7 +3202,7 @@ impl ChannelActorState { self.tlc_ids.get_next_received() } - pub fn increment_next_offered_tlc_id(&mut self) { + pub fn increment_next_sent_tlc_id(&mut self) { self.tlc_ids.increment_offering(); } @@ -3210,8 +3210,8 @@ impl ChannelActorState { self.tlc_ids.increment_received(); } - pub fn get_offered_tlc(&self, tlc_id: u64) -> Option<&DetailedTLCInfo> { - self.tlcs.get(&TLCId::Offered(tlc_id)) + pub fn get_sent_tlc(&self, tlc_id: u64) -> Option<&DetailedTLCInfo> { + self.tlcs.get(&TLCId::Sent(tlc_id)) } pub fn get_received_tlc(&self, tlc_id: u64) -> Option<&DetailedTLCInfo> { @@ -3239,10 +3239,10 @@ impl ChannelActorState { tlc.id ))); } - if tlc.is_offered() { + if tlc.is_sent() { // 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_offered_tlc_balance(); + let sent_tlc_value = self.get_sent_tlc_balance(); debug!("Value of local sent tlcs: {}", sent_tlc_value); debug_assert!(self.to_local_amount >= sent_tlc_value); // TODO: handle transaction fee here. @@ -3285,8 +3285,8 @@ impl ChannelActorState { removal_confirmed_at: None, }; self.tlcs.insert(tlc.id, detailed_tlc.clone()); - if tlc.is_offered() { - self.increment_next_offered_tlc_id(); + if tlc.is_sent() { + self.increment_next_sent_tlc_id(); } else { self.increment_next_received_tlc_id(); } @@ -3516,7 +3516,7 @@ impl ChannelActorState { } = info; { let am_i_sending_add_tlc_message = { - if tlc.is_offered() { + if tlc.is_sent() { local_commitment } else { !local_commitment @@ -3555,30 +3555,28 @@ impl ChannelActorState { local_commitment: bool, ) -> impl Iterator { self.tlcs.values().filter(move |info| { - Self::should_tlc_be_included_in_commitment_tx(info, local_commitment) - && !info.is_offered() + Self::should_tlc_be_included_in_commitment_tx(info, local_commitment) && !info.is_sent() }) } - pub fn get_active_offered_tlcs( + pub fn get_active_sent_tlcs( &self, local_commitment: bool, ) -> impl Iterator { self.tlcs.values().filter(move |info| { - Self::should_tlc_be_included_in_commitment_tx(info, local_commitment) - && info.is_offered() + Self::should_tlc_be_included_in_commitment_tx(info, local_commitment) && info.is_sent() }) } // Get the pubkeys for the tlc. Tlc pubkeys are the pubkeys held by each party // while this tlc was created (pubkeys are derived from the commitment number // when this tlc was created). The pubkeys returned here are sorted. - // The offerer who offered this tlc will have the first pubkey, and the receiver + // The offerer who sent this tlc will have the first pubkey, and the receiver // will have the second pubkey. // This tlc must have valid local_committed_at and remote_committed_at fields. pub fn get_tlc_pubkeys(&self, tlc: &DetailedTLCInfo, local: bool) -> (Pubkey, Pubkey) { debug!("Getting tlc pubkeys for tlc: {:?}", tlc); - let is_offered = tlc.tlc.is_offered(); + let is_sent = tlc.tlc.is_sent(); let CommitmentNumbers { local: local_commitment_number, remote: remote_commitment_number, @@ -3596,7 +3594,7 @@ impl ChannelActorState { &self.get_remote_commitment_point(local_commitment_number), ); - if is_offered { + if is_sent { (local_pubkey, remote_pubkey) } else { (remote_pubkey, local_pubkey) @@ -3613,11 +3611,11 @@ impl ChannelActorState { }) } - pub fn get_active_offered_tlc_with_pubkeys( + pub fn get_active_sent_tlc_with_pubkeys( &self, local: bool, ) -> impl Iterator { - self.get_active_offered_tlcs(local).map(move |tlc| { + self.get_active_sent_tlcs(local).map(move |tlc| { let (k1, k2) = self.get_tlc_pubkeys(tlc, local); (tlc, k1, k2) }) @@ -3627,24 +3625,24 @@ impl ChannelActorState { // Build a sorted array of TLC so that both party can generate the same commitment transaction. debug!("All tlcs: {:?}", self.tlcs); let tlcs = { - let (mut received_tlcs, mut offered_tlcs) = ( + let (mut received_tlcs, mut sent_tlcs) = ( self.get_active_received_tlc_with_pubkeys(local) .map(|(tlc, local, remote)| (tlc.clone(), local, remote)) .collect::>(), - self.get_active_offered_tlc_with_pubkeys(local) + self.get_active_sent_tlc_with_pubkeys(local) .map(|(tlc, local, remote)| (tlc.clone(), local, remote)) .collect::>(), ); debug!("Received tlcs: {:?}", &received_tlcs); - debug!("Offered tlcs: {:?}", &offered_tlcs); + debug!("Sent tlcs: {:?}", &sent_tlcs); let (mut a, mut b) = if local { - (received_tlcs, offered_tlcs) + (received_tlcs, sent_tlcs) } else { - for (tlc, _, _) in received_tlcs.iter_mut().chain(offered_tlcs.iter_mut()) { + for (tlc, _, _) in received_tlcs.iter_mut().chain(sent_tlcs.iter_mut()) { // Need to flip these fields for the counterparty. tlc.tlc.flip_mut(); } - (offered_tlcs, received_tlcs) + (sent_tlcs, received_tlcs) }; a.sort_by(|x, y| u64::from(x.0.tlc.id).cmp(&u64::from(y.0.tlc.id))); b.sort_by(|x, y| u64::from(x.0.tlc.id).cmp(&u64::from(y.0.tlc.id))); @@ -3718,7 +3716,7 @@ impl ChannelActorState { } if let Some(add_amount) = add_tlc_amount { - let active_tls_number = self.get_active_offered_tlcs(true).count() + let active_tls_number = self.get_active_sent_tlcs(true).count() + self.get_active_received_tlcs(true).count(); if active_tls_number as u64 + 1 > self.max_num_of_accept_tlcs { @@ -3730,7 +3728,7 @@ impl ChannelActorState { if self .get_active_received_tlcs(true) - .chain(self.get_active_offered_tlcs(true)) + .chain(self.get_active_sent_tlcs(true)) .fold(0_u128, |sum, tlc| sum + tlc.tlc.amount) + add_amount > self.max_tlc_value_in_flight @@ -3753,8 +3751,8 @@ impl ChannelActorState { // Is this what we want? let id = self.get_next_offering_tlc_id(); assert!( - self.get_offered_tlc(id).is_none(), - "Must not have the same id in pending offered tlcs" + self.get_sent_tlc(id).is_none(), + "Must not have the same id in pending sent tlcs" ); let preimage = command.preimage.unwrap_or(get_random_preimage()); @@ -3763,7 +3761,7 @@ impl ChannelActorState { .unwrap_or_else(|| command.hash_algorithm.hash(preimage).into()); TLC { - id: TLCId::Offered(id), + id: TLCId::Sent(id), amount: command.amount, payment_hash, lock_time: command.expiry, @@ -4543,7 +4541,7 @@ impl ChannelActorState { // resend AddTlc, RemoveTlc and CommitmentSigned messages if needed let mut need_resend_commitment_signed = false; for info in self.tlcs.values() { - if info.is_offered() { + if info.is_sent() { if info.created_at.get_local() >= acutal_local_commitment_number && info.creation_confirmed_at.is_none() { @@ -5040,13 +5038,13 @@ impl ChannelActorState { .get_active_received_tlcs(local) .map(|tlc| tlc.tlc.amount) .sum::(); - let offered_tlc_value = self - .get_active_offered_tlcs(local) + let sent_tlc_value = self + .get_active_sent_tlcs(local) .map(|tlc| tlc.tlc.amount) .sum::(); let to_local_value = - self.to_local_amount + self.local_reserved_ckb_amount as u128 - offered_tlc_value; + self.to_local_amount + self.local_reserved_ckb_amount as u128 - sent_tlc_value; let to_remote_value = self.to_remote_amount + self.remote_reserved_ckb_amount as u128 - received_tlc_value; @@ -5451,12 +5449,12 @@ pub struct TLC { } impl TLC { - pub fn is_offered(&self) -> bool { - self.id.is_offered() + pub fn is_sent(&self) -> bool { + self.id.is_sent() } pub fn is_received(&self) -> bool { - !self.is_offered() + !self.is_sent() } // Change this tlc to the opposite side. @@ -5465,13 +5463,13 @@ impl TLC { } /// Get the value for the field `htlc_type` in commitment lock witness. - /// - Lowest 1 bit: 0 if the tlc is offered by the remote party, 1 otherwise. + /// - Lowest 1 bit: 0 if the tlc is sent by the remote party, 1 otherwise. /// - High 7 bits: /// - 0: ckb hash /// - 1: sha256 pub fn get_htlc_type(&self) -> u8 { - let offered_flag = if self.is_offered() { 0u8 } else { 1u8 }; - ((self.hash_algorithm as u8) << 1) + offered_flag + let sent_flag = if self.is_sent() { 0u8 } else { 1u8 }; + ((self.hash_algorithm as u8) << 1) + sent_flag } fn get_hash(&self) -> ShortHash { @@ -5480,7 +5478,7 @@ impl TLC { fn get_id(&self) -> u64 { match self.id { - TLCId::Offered(id) => id, + TLCId::Sent(id) => id, TLCId::Received(id) => id, } } @@ -5514,13 +5512,13 @@ pub struct DetailedTLCInfo { } impl DetailedTLCInfo { - fn is_offered(&self) -> bool { - self.tlc.is_offered() + fn is_sent(&self) -> bool { + self.tlc.is_sent() } fn get_commitment_numbers(&self, local: bool) -> CommitmentNumbers { let am_i_sending_the_tlc = { - if self.is_offered() { + if self.is_sent() { local } else { !local diff --git a/src/rpc/README.md b/src/rpc/README.md index 9ffd8955..8ee7d9ad 100644 --- a/src/rpc/README.md +++ b/src/rpc/README.md @@ -113,7 +113,7 @@ Lists all active channels that the node is participating in. * `status` - The status of the channel * `local_balance` - The balance of the channel owned by the local node * `remote_balance` - The balance of the channel owned by the remote peer - * `offered_tlc_balance` - The total balance of currently offered TLCs in the channel + * `sent_tlc_balance` - The total balance of currently sent TLCs in the channel * `received_tlc_balance` - The total balance of currently received TLCs in the channel * `created_at` - The timestamp when the channel was created, in milliseconds diff --git a/src/rpc/channel.rs b/src/rpc/channel.rs index ab22350d..bc3146c7 100644 --- a/src/rpc/channel.rs +++ b/src/rpc/channel.rs @@ -99,7 +99,7 @@ pub(crate) struct Channel { #[serde_as(as = "U128Hex")] local_balance: u128, #[serde_as(as = "U128Hex")] - offered_tlc_balance: u128, + sent_tlc_balance: u128, #[serde_as(as = "U128Hex")] remote_balance: u128, #[serde_as(as = "U128Hex")] @@ -352,7 +352,7 @@ where state: state.state, local_balance: state.get_local_balance(), remote_balance: state.get_remote_balance(), - offered_tlc_balance: state.get_offered_tlc_balance(), + sent_tlc_balance: state.get_sent_tlc_balance(), received_tlc_balance: state.get_received_tlc_balance(), created_at: state.get_created_at_in_microseconds(), })