Skip to content

Commit

Permalink
rename offered in tlc into sent
Browse files Browse the repository at this point in the history
  • Loading branch information
chenyukang committed Oct 15, 2024
1 parent 5befc49 commit f22c71b
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 58 deletions.
108 changes: 53 additions & 55 deletions src/fiber/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 }),
Expand Down Expand Up @@ -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<TLCId> 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),
}
}

Expand Down Expand Up @@ -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::<u128>()
}
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -3202,16 +3202,16 @@ 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();
}

pub fn increment_next_received_tlc_id(&mut self) {
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> {
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -3555,30 +3555,28 @@ impl ChannelActorState {
local_commitment: bool,
) -> impl Iterator<Item = &DetailedTLCInfo> {
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<Item = &DetailedTLCInfo> {
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,
Expand All @@ -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)
Expand All @@ -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<Item = (&DetailedTLCInfo, Pubkey, Pubkey)> {
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)
})
Expand All @@ -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::<Vec<_>>(),
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::<Vec<_>>(),
);
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)));
Expand Down Expand Up @@ -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 {
Expand All @@ -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
Expand All @@ -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());
Expand All @@ -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,
Expand Down Expand Up @@ -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()
{
Expand Down Expand Up @@ -5040,13 +5038,13 @@ impl ChannelActorState {
.get_active_received_tlcs(local)
.map(|tlc| tlc.tlc.amount)
.sum::<u128>();
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::<u128>();

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;

Expand Down Expand Up @@ -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.
Expand All @@ -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 {
Expand All @@ -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,
}
}
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/rpc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions src/rpc/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down Expand Up @@ -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(),
})
Expand Down

0 comments on commit f22c71b

Please sign in to comment.