Skip to content

Commit

Permalink
rename ckb amounts
Browse files Browse the repository at this point in the history
  • Loading branch information
chenyukang committed Jun 19, 2024
1 parent ded2d9c commit 0ac9a01
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 40 deletions.
56 changes: 32 additions & 24 deletions src/ckb/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ use crate::{
};

use super::{
config::{MIN_CHANNEL_CAPACITY, MIN_UDT_OCCUPIED_CAPACITY},
config::{MIN_CHANNEL_CKB_AMOUNT, MIN_UDT_OCCUPIED_CAPACITY},
key::blake2b_hash_with_salt,
network::CFNMessageWithPeerId,
serde_utils::EntityHex,
Expand Down Expand Up @@ -208,8 +208,8 @@ impl<S> ChannelActor<S> {
state.to_remote_amount,
state.get_funding_lock_script(),
state.funding_udt_type_script.clone(),
state.local_ckb_amount,
state.remote_ckb_amount,
state.local_reserve_ckb_amount,
state.remote_reserve_ckb_amount,
),
))
.expect(ASSUME_NETWORK_ACTOR_ALIVE);
Expand Down Expand Up @@ -1292,14 +1292,12 @@ pub struct ChannelActorState {
// This value will only change after we have resolved a tlc.
pub to_remote_amount: u128,

// only used for UDT scenario:
// `to_local_amount` and `to_remote_amount` are the amount of UDT,
// while `local_ckb_amount` and `remote_ckb_amount` are the amount of CKB
// of the underlying cells that bear the UDT.
// We keep track of the CKB amount from partners in the channel in
// order to construct closing/commitment transactions.
pub local_ckb_amount: u64,
pub remote_ckb_amount: u64,
// these two amounts used to keep the minimal ckb amount for the two parties
// TLC operations will not affect these two amounts, only used to keep the commitment transactions
// to be valid, so that any party can close the channel at any time.
// Note: the values are different for the UDT scenario
pub local_reserve_ckb_amount: u64,
pub remote_reserve_ckb_amount: u64,

// The commitment fee rate is used to calculate the fee for the commitment transactions.
// this fee rate is only paid by the initiator of the channel, so we can use `is_acceptor`
Expand Down Expand Up @@ -1574,8 +1572,8 @@ impl ChannelActorState {
local_shutdown_fee: None,
remote_shutdown_signature: None,
remote_shutdown_fee: None,
local_ckb_amount: DEFAULT_UDT_MINIMAL_CKB_AMOUNT,
remote_ckb_amount: DEFAULT_UDT_MINIMAL_CKB_AMOUNT,
local_reserve_ckb_amount: DEFAULT_UDT_MINIMAL_CKB_AMOUNT,
remote_reserve_ckb_amount: DEFAULT_UDT_MINIMAL_CKB_AMOUNT,

#[cfg(debug_assertions)]
total_amount: local_value + remote_value,
Expand Down Expand Up @@ -1621,8 +1619,8 @@ impl ChannelActorState {
remote_shutdown_fee: None,
local_shutdown_signature: None,
remote_shutdown_signature: None,
local_ckb_amount: DEFAULT_UDT_MINIMAL_CKB_AMOUNT,
remote_ckb_amount: DEFAULT_UDT_MINIMAL_CKB_AMOUNT,
local_reserve_ckb_amount: DEFAULT_UDT_MINIMAL_CKB_AMOUNT,
remote_reserve_ckb_amount: DEFAULT_UDT_MINIMAL_CKB_AMOUNT,

#[cfg(debug_assertions)]
total_amount: value,
Expand Down Expand Up @@ -2015,7 +2013,11 @@ impl ChannelActorState {
pub fn get_funding_request(&self, fee_rate: u64) -> FundingRequest {
FundingRequest {
udt_info: self.funding_udt_type_script.as_ref().map(|script| {
FundingUdtInfo::new(script, self.local_ckb_amount, self.remote_ckb_amount)
FundingUdtInfo::new(
script,
self.local_reserve_ckb_amount,
self.remote_reserve_ckb_amount,
)
}),
script: self.get_funding_lock_script(),
local_amount: self.to_local_amount as u64,
Expand Down Expand Up @@ -2267,7 +2269,7 @@ impl ChannelActorState {
let available_max_fee = if self.funding_udt_type_script.is_none() {
self.to_local_amount as u64 - MIN_OCCUPIED_CAPACITY
} else {
self.local_ckb_amount - MIN_UDT_OCCUPIED_CAPACITY
self.local_reserve_ckb_amount - MIN_UDT_OCCUPIED_CAPACITY
};
debug!(
"verify_shutdown_fee local_amount: {} remote_amount: {} min_occupied_capacity: {}",
Expand Down Expand Up @@ -2308,7 +2310,7 @@ impl ChannelActorState {
pub fn check_add_tlc_amount(&self, amount: u128) -> ProcessingChannelResult {
debug!("begin check_add_tlc_amount: {}", amount);
let available_amount = if self.funding_udt_type_script.is_none() {
self.to_local_amount - MIN_CHANNEL_CAPACITY as u128
self.to_local_amount - MIN_CHANNEL_CKB_AMOUNT as u128
} else {
self.to_local_amount
};
Expand Down Expand Up @@ -3211,10 +3213,10 @@ impl ChannelActorState {
self.to_local_amount, self.to_remote_amount
);

let local_capacity: u64 = self.local_ckb_amount - local_shutdown_fee as u64;
let local_capacity: u64 = self.local_reserve_ckb_amount - local_shutdown_fee as u64;
debug!(
"shutdown_tx local_capacity: {} - {} = {}",
self.local_ckb_amount, local_shutdown_fee, local_capacity
self.local_reserve_ckb_amount, local_shutdown_fee, local_capacity
);
let local_output = CellOutput::new_builder()
.lock(local_shutdown_script.clone())
Expand All @@ -3223,10 +3225,10 @@ impl ChannelActorState {
.build();
let local_output_data = self.to_local_amount.to_le_bytes().pack();

let remote_capacity: u64 = self.remote_ckb_amount - remote_shutdown_fee as u64;
let remote_capacity: u64 = self.remote_reserve_ckb_amount - remote_shutdown_fee as u64;
debug!(
"shutdown_tx remote_capacity: {} - {} = {}",
self.remote_ckb_amount, remote_shutdown_fee, remote_capacity
self.remote_reserve_ckb_amount, remote_shutdown_fee, remote_capacity
);
let remote_output = CellOutput::new_builder()
.lock(remote_shutdown_script.clone())
Expand Down Expand Up @@ -3483,9 +3485,15 @@ impl ChannelActorState {
if let Some(udt_type_script) = &self.funding_udt_type_script {
//FIXME(yukang): we need to add logic for transaction fee here
let (time_locked_ckb_amount, immediately_spendable_ckb_amount) = if local {
(self.local_ckb_amount, self.remote_ckb_amount)
(
self.local_reserve_ckb_amount,
self.remote_reserve_ckb_amount,
)
} else {
(self.remote_ckb_amount, self.local_ckb_amount)
(
self.remote_reserve_ckb_amount,
self.local_reserve_ckb_amount,
)
};

let immediate_output_data = immediately_spendable_value.to_le_bytes().pack();
Expand Down
8 changes: 4 additions & 4 deletions src/ckb/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ pub const MIN_UDT_OCCUPIED_CAPACITY: u64 = 142 * CKB_SHANNONS; // 142 CKB for UD
/// 62 CKB minimal channel amount, at any time a partner should keep at least
/// `MIN_OCCUPIED_CAPACITY` CKB in the channel, so that he can build a valid shutdown transaction
/// and pay proper fee.
pub const MIN_CHANNEL_CAPACITY: u64 = MIN_OCCUPIED_CAPACITY + DEFAULT_MIN_SHUTDOWN_FEE;
pub const MIN_CHANNEL_CKB_AMOUNT: u64 = MIN_OCCUPIED_CAPACITY + DEFAULT_MIN_SHUTDOWN_FEE;

/// 162 CKB to open a channel,
/// 100 CKB for minimal inbound liquidity, 61 CKB for occupied capacity
pub const MIN_CHANNEL_OPEN_CAPACITY: u64 =
pub const MIN_CHANNEL_OPEN_CKB_AMOUNT: u64 =
DEFAULT_MIN_INBOUND_LIQUIDITY + MIN_OCCUPIED_CAPACITY + DEFAULT_MIN_SHUTDOWN_FEE;

// See comment in `LdkConfig` for why do we need to specify both name and long,
Expand Down Expand Up @@ -86,12 +86,12 @@ pub struct CkbConfig {
impl CkbConfig {
pub fn open_channel_min_ckb_funding_amount(&self) -> u64 {
self.open_channel_min_ckb_funding_amount
.unwrap_or(MIN_CHANNEL_OPEN_CAPACITY)
.unwrap_or(MIN_CHANNEL_OPEN_CKB_AMOUNT)
}

pub fn auto_accept_channel_ckb_funding_amount(&self) -> u64 {
self.auto_accept_channel_ckb_funding_amount
.unwrap_or(MIN_CHANNEL_CAPACITY)
.unwrap_or(MIN_CHANNEL_CKB_AMOUNT)
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/ckb/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -364,8 +364,8 @@ where
remote,
script,
funding_script,
local_ckb_amount,
remote_ckb_amount,
local_reserve_ckb_amount,
remote_reserve_ckb_amount,
) => {
assert_ne!(new, old, "new and old channel id must be different");
if let Some(session) = state.get_peer_session(&peer_id) {
Expand All @@ -389,8 +389,8 @@ where
udt_info: funding_script.as_ref().map(|type_script| {
FundingUdtInfo::new(
type_script,
local_ckb_amount,
remote_ckb_amount,
local_reserve_ckb_amount,
remote_reserve_ckb_amount,
)
}),
script,
Expand Down
16 changes: 8 additions & 8 deletions src/ckb_chain/funding/funding_tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,21 +62,21 @@ pub struct FundingUdtInfo {
#[serde_as(as = "EntityHex")]
pub type_script: packed::Script,
/// CKB amount to be provided by the local party.
pub local_ckb_amount: u64,
pub local_reserve_ckb_amount: u64,
/// CKB amount to be provided by the remote party.
pub remote_ckb_amount: u64,
pub remote_reserve_ckb_amount: u64,
}

impl FundingUdtInfo {
pub fn new(
type_script: &packed::Script,
local_ckb_amount: u64,
remote_ckb_amount: u64,
local_reserve_ckb_amount: u64,
remote_reserve_ckb_amount: u64,
) -> Self {
Self {
type_script: type_script.clone(),
local_ckb_amount,
remote_ckb_amount,
local_reserve_ckb_amount,
remote_reserve_ckb_amount,
}
}
}
Expand Down Expand Up @@ -179,14 +179,14 @@ impl FundingTxBuilder {
match self.request.udt_info {
Some(ref udt_info) => {
let mut udt_amount = self.request.local_amount as u128;
let mut ckb_amount = udt_info.local_ckb_amount;
let mut ckb_amount = udt_info.local_reserve_ckb_amount;

// To make tx building easier, do not include the amount not funded yet in the
// funding cell.
if remote_funded {
udt_amount += self.request.remote_amount as u128;
ckb_amount = ckb_amount
.checked_add(udt_info.remote_ckb_amount)
.checked_add(udt_info.remote_reserve_ckb_amount)
.ok_or(FundingError::InvalidChannel)?;
}

Expand Down

0 comments on commit 0ac9a01

Please sign in to comment.