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

Add min_fee_rate option commitment transaction #68

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
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
46 changes: 37 additions & 9 deletions src/ckb/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ pub struct ChannelCommandWithId {
}

pub const DEFAULT_FEE_RATE: u64 = 0;
pub const DEFAULT_COMMITMENT_FEE_RATE: u64 = 0;
pub const DEFAULT_COMMITMENT_FEE_RATE: u64 = 2_000;
pub const DEFAULT_MAX_TLC_VALUE_IN_FLIGHT: u128 = u128::MAX;
pub const DEFAULT_MAX_ACCEPT_TLCS: u64 = u64::MAX;
pub const DEFAULT_MIN_TLC_VALUE: u128 = 0;
Expand All @@ -149,7 +149,13 @@ pub enum ChannelInitializationParameter {
/// To open a new channel to another peer, the funding amount,
/// the temporary channel id a unique channel seed to generate
/// channel secrets must be given.
OpenChannel(u128, [u8; 32], Option<Script>, oneshot::Sender<Hash256>),
OpenChannel(
u128,
[u8; 32],
Option<Script>,
oneshot::Sender<Hash256>,
Option<u64>,
),
/// To accept a new channel from another peer, the funding amount,
/// a unique channel seed to generate unique channel id,
/// original OpenChannel message and an oneshot
Expand Down Expand Up @@ -947,6 +953,7 @@ where
let OpenChannel {
channel_id,
chain_hash,
commitment_fee_rate,
funding_udt_type_script,
funding_amount,
to_local_delay,
Expand All @@ -966,6 +973,7 @@ where
let mut state = ChannelActorState::new_inbound_channel(
*channel_id,
my_funding_amount,
*commitment_fee_rate,
funding_udt_type_script.clone(),
&seed,
peer_id.clone(),
Expand Down Expand Up @@ -1033,14 +1041,17 @@ where
seed,
funding_udt_type_script,
tx,
min_fee_rate,
) => {
let peer_id = self.peer_id.clone();
info!("Trying to open a channel to {:?}", &peer_id);

let commitment_fee_rate = min_fee_rate.unwrap_or(DEFAULT_COMMITMENT_FEE_RATE);
let mut channel = ChannelActorState::new_outbound_channel(
&seed,
self.peer_id.clone(),
funding_amount,
commitment_fee_rate,
funding_udt_type_script.clone(),
LockTime::new(DEFAULT_TO_LOCAL_DELAY_BLOCKS),
);
Expand All @@ -1052,7 +1063,7 @@ where
funding_udt_type_script,
funding_amount: channel.to_local_amount,
funding_fee_rate: DEFAULT_FEE_RATE,
commitment_fee_rate: DEFAULT_COMMITMENT_FEE_RATE,
commitment_fee_rate,
max_tlc_value_in_flight: DEFAULT_MAX_TLC_VALUE_IN_FLIGHT,
max_accept_tlcs: DEFAULT_MAX_ACCEPT_TLCS,
min_tlc_value: DEFAULT_MIN_TLC_VALUE,
Expand Down Expand Up @@ -1228,6 +1239,11 @@ pub struct ChannelActorState {
pub local_ckb_amount: u64,
pub remote_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`
// field to determine who should pay the fee.
pub commitment_fee_rate: u64,

// Signer is used to sign the commitment transactions.
pub signer: InMemorySigner,

Expand Down Expand Up @@ -1451,6 +1467,7 @@ impl ChannelActorState {
pub fn new_inbound_channel<'a>(
temp_channel_id: Hash256,
local_value: u128,
commitment_fee_rate: u64,
funding_udt_type_script: Option<Script>,
seed: &[u8],
peer_id: PeerId,
Expand Down Expand Up @@ -1481,12 +1498,13 @@ impl ChannelActorState {
is_acceptor: true,
funding_udt_type_script,
to_local_amount: local_value,
to_remote_amount: remote_value,
commitment_fee_rate,
id: channel_id,
next_offering_tlc_id: 0,
next_receiving_tlc_id: 0,
pending_offered_tlcs: Default::default(),
pending_received_tlcs: Default::default(),
to_remote_amount: remote_value,
local_shutdown_script: None,
local_channel_parameters: ChannelParametersOneParty {
pubkeys: local_pubkeys,
Expand Down Expand Up @@ -1515,6 +1533,7 @@ impl ChannelActorState {
seed: &[u8],
peer_id: PeerId,
value: u128,
commitment_fee_rate: u64,
funding_udt_type_script: Option<Script>,
to_local_delay: LockTime,
) -> Self {
Expand All @@ -1529,12 +1548,13 @@ impl ChannelActorState {
funding_udt_type_script,
is_acceptor: false,
to_local_amount: value,
to_remote_amount: 0,
commitment_fee_rate,
id: temp_channel_id,
next_offering_tlc_id: 0,
next_receiving_tlc_id: 0,
pending_offered_tlcs: Default::default(),
pending_received_tlcs: Default::default(),
to_remote_amount: 0,
signer,
local_channel_parameters: ChannelParametersOneParty {
pubkeys: local_pubkeys,
Expand Down Expand Up @@ -1711,14 +1731,19 @@ impl ChannelActorState {
get_script_by_contract(Contract::FundingLock, args.as_slice())
}

pub fn get_funding_request(&self, fee_rate: u64) -> FundingRequest {
pub fn get_funding_request(&self) -> FundingRequest {
let commitment_fee_rate = if self.is_acceptor {
self.commitment_fee_rate
} else {
self.commitment_fee_rate
};
FundingRequest {
udt_info: self.funding_udt_type_script.as_ref().map(|script| {
FundingUdtInfo::new(script, self.local_ckb_amount, self.remote_ckb_amount)
}),
script: self.get_funding_lock_script(),
local_amount: self.to_local_amount as u64,
local_fee_rate: fee_rate,
local_fee_rate: commitment_fee_rate,
remote_amount: self.to_remote_amount as u64,
}
}
Expand Down Expand Up @@ -2133,8 +2158,7 @@ impl ChannelActorState {
NetworkActorCommand::UpdateChannelFunding(
self.get_id(),
msg.tx.clone(),
// TODO: use fee rate set by the user.
self.get_funding_request(20000),
self.get_funding_request(),
),
))
.expect(ASSUME_NETWORK_ACTOR_ALIVE);
Expand Down Expand Up @@ -3591,6 +3615,7 @@ mod tests {
peer_id: node_b.peer_id.clone(),
funding_amount: 100000000000,
funding_udt_type_script: None,
min_fee_rate: None,
},
rpc_reply,
))
Expand Down Expand Up @@ -3624,6 +3649,7 @@ mod tests {
peer_id: node_b.peer_id.clone(),
funding_amount: 100000000000,
funding_udt_type_script: None,
min_fee_rate: None,
},
rpc_reply,
))
Expand Down Expand Up @@ -3673,6 +3699,7 @@ mod tests {
peer_id: node_b.peer_id.clone(),
funding_amount: 100000000000,
funding_udt_type_script: None,
min_fee_rate: None,
},
rpc_reply,
))
Expand Down Expand Up @@ -3789,6 +3816,7 @@ mod tests {
peer_id: node_b.peer_id.clone(),
funding_amount: 100000000000,
funding_udt_type_script: None,
min_fee_rate: None,
},
rpc_reply,
))
Expand Down
3 changes: 3 additions & 0 deletions src/ckb/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ pub struct OpenChannelCommand {
pub peer_id: PeerId,
pub funding_amount: u128,
pub funding_udt_type_script: Option<Script>,
pub min_fee_rate: Option<u64>,
}

#[derive(Debug)]
Expand Down Expand Up @@ -742,6 +743,7 @@ impl NetworkActorState {
peer_id,
funding_amount,
funding_udt_type_script,
min_fee_rate,
} = open_channel;
let seed = self.generate_channel_seed();
let (tx, rx) = oneshot::channel::<Hash256>();
Expand All @@ -753,6 +755,7 @@ impl NetworkActorState {
seed,
funding_udt_type_script,
tx,
min_fee_rate,
),
network.clone().get_cell(),
)
Expand Down
3 changes: 3 additions & 0 deletions src/rpc/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ pub struct OpenChannelParams {
#[serde_as(as = "U128Hex")]
pub funding_amount: u128,
pub funding_udt_type_script: Option<Script>,
#[serde_as(as = "Option<U64Hex>")]
pub commitment_fee_rate: Option<u64>,
}

#[derive(Clone, Serialize)]
Expand Down Expand Up @@ -188,6 +190,7 @@ where
.funding_udt_type_script
.clone()
.map(|s| s.into()),
min_fee_rate: params.commitment_fee_rate,
},
rpc_reply,
))
Expand Down
Loading