Skip to content

Commit

Permalink
merge main
Browse files Browse the repository at this point in the history
  • Loading branch information
chenyukang committed Oct 23, 2024
2 parents de5b9c3 + 7e49685 commit dbc68fd
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 110 deletions.
56 changes: 26 additions & 30 deletions src/fiber/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2455,12 +2455,9 @@ impl ChannelActorState {
None => {
let channel_outpoint = self.get_funding_transaction_outpoint();
let capacity = if self.funding_udt_type_script.is_some() {
self.to_local_amount + self.to_remote_amount
self.get_total_udt_amount()
} else {
self.to_local_amount
+ self.to_remote_amount
+ self.local_reserved_ckb_amount as u128
+ self.remote_reserved_ckb_amount as u128
self.get_total_ckb_amount() as u128
};

let (node1_id, node2_id) = if self.local_is_node1() {
Expand Down Expand Up @@ -3131,28 +3128,38 @@ impl ChannelActorState {
}
}

fn get_total_reserved_ckb_amount(&self) -> u64 {
self.local_reserved_ckb_amount + self.remote_reserved_ckb_amount
}

fn get_total_ckb_amount(&self) -> u64 {
self.to_local_amount as u64
+ self.to_remote_amount as u64
+ self.get_total_reserved_ckb_amount()
}

fn get_total_udt_amount(&self) -> u128 {
self.to_local_amount + self.to_remote_amount
}

// Send RevokeAndAck message to the counterparty, and update the
// channel state accordingly.
fn send_revoke_and_ack_message(&mut self, network: &ActorRef<NetworkActorMessage>) {
let commitment_tx_fee =
calculate_commitment_tx_fee(self.commitment_fee_rate, &self.funding_udt_type_script);
let lock_script = self.get_remote_shutdown_script();
let (output, output_data) = if let Some(udt_type_script) = &self.funding_udt_type_script {
let capacity = self.local_reserved_ckb_amount + self.remote_reserved_ckb_amount
- commitment_tx_fee;
let capacity = self.get_total_reserved_ckb_amount() - commitment_tx_fee;
let output = CellOutput::new_builder()
.lock(lock_script)
.type_(Some(udt_type_script.clone()).pack())
.capacity(capacity.pack())
.build();

let output_data = (self.to_local_amount + self.to_remote_amount)
.to_le_bytes()
.pack();
let output_data = self.get_total_udt_amount().to_le_bytes().pack();
(output, output_data)
} else {
let capacity =
(self.to_local_amount + self.to_remote_amount) as u64 - commitment_tx_fee;
let capacity = self.get_total_ckb_amount() - commitment_tx_fee;
let output = CellOutput::new_builder()
.lock(lock_script)
.capacity(capacity.pack())
Expand Down Expand Up @@ -4625,21 +4632,17 @@ impl ChannelActorState {
calculate_commitment_tx_fee(self.commitment_fee_rate, &self.funding_udt_type_script);
let lock_script = self.get_local_shutdown_script();
let (output, output_data) = if let Some(udt_type_script) = &self.funding_udt_type_script {
let capacity = self.local_reserved_ckb_amount + self.remote_reserved_ckb_amount
- commitment_tx_fee;
let capacity = self.get_total_reserved_ckb_amount() - commitment_tx_fee;
let output = CellOutput::new_builder()
.lock(lock_script)
.type_(Some(udt_type_script.clone()).pack())
.capacity(capacity.pack())
.build();

let output_data = (self.to_local_amount + self.to_remote_amount)
.to_le_bytes()
.pack();
let output_data = self.get_total_udt_amount().to_le_bytes().pack();
(output, output_data)
} else {
let capacity =
(self.to_local_amount + self.to_remote_amount) as u64 - commitment_tx_fee;
let capacity = self.get_total_ckb_amount() - commitment_tx_fee;
let output = CellOutput::new_builder()
.lock(lock_script)
.capacity(capacity.pack())
Expand Down Expand Up @@ -4889,14 +4892,10 @@ impl ChannelActorState {
);
debug!("current_capacity: {}, remote_reserved_ckb_amount: {}, local_reserved_ckb_amount: {}",
current_capacity, self.remote_reserved_ckb_amount, self.local_reserved_ckb_amount);
let is_udt_amount_ok = udt_amount == self.to_remote_amount + self.to_local_amount;
let is_udt_amount_ok = udt_amount == self.get_total_udt_amount();
return Ok(is_udt_amount_ok);
} else {
let is_complete = current_capacity
== (self.to_local_amount
+ self.to_remote_amount
+ self.local_reserved_ckb_amount as u128
+ self.remote_reserved_ckb_amount as u128) as u64;
let is_complete = current_capacity == self.get_total_ckb_amount();
Ok(is_complete)
}
}
Expand Down Expand Up @@ -5226,13 +5225,10 @@ impl ChannelActorState {
.capacity(capacity.pack())
.build();

let output_data = (self.to_local_amount + self.to_remote_amount)
.to_le_bytes()
.pack();
let output_data = self.get_total_udt_amount().to_le_bytes().pack();
(output, output_data)
} else {
let capacity =
(self.to_local_amount + self.to_remote_amount) as u64 - commitment_tx_fee;
let capacity = self.get_total_ckb_amount() - commitment_tx_fee;
let output = CellOutput::new_builder()
.lock(commitment_lock_script)
.capacity(capacity.pack())
Expand Down
2 changes: 2 additions & 0 deletions src/fiber/fee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ pub(crate) fn default_minimal_ckb_amount(is_udt: bool) -> u64 {
}

fn commitment_tx_size(udt_type_script: &Option<Script>) -> usize {
// when there is pending htlcs, the commitment lock args will be 56 bytes, otherwise 46 bytes.
// to simplify the calculation, we use hardcoded 56 bytes here.
let commitment_lock_script = get_script_by_contract(Contract::CommitmentLock, &[0u8; 56]);
let cell_deps = get_cell_deps(vec![Contract::FundingLock], udt_type_script);

Expand Down
24 changes: 21 additions & 3 deletions src/fiber/tests/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use crate::{
fiber::{
channel::{
derive_private_key, derive_revocation_pubkey, derive_tlc_pubkey, AddTlcCommand,
ChannelCommand, ChannelCommandWithId, InMemorySigner, RemoveTlcCommand,
ShutdownCommand, DEFAULT_COMMITMENT_FEE_RATE,
ChannelActorStateStore, ChannelCommand, ChannelCommandWithId, InMemorySigner,
RemoveTlcCommand, ShutdownCommand, DEFAULT_COMMITMENT_FEE_RATE,
},
config::DEFAULT_CHANNEL_MINIMAL_CKB_AMOUNT,
hash_algorithm::HashAlgorithm,
Expand All @@ -18,7 +18,7 @@ use ckb_jsonrpc_types::Status;
use ckb_types::{
core::FeeRate,
packed::{CellInput, Script, Transaction},
prelude::{AsTransactionBuilder, Builder, Entity, Pack},
prelude::{AsTransactionBuilder, Builder, Entity, IntoTransactionView, Pack, Unpack},
};
use ractor::call;

Expand Down Expand Up @@ -1216,3 +1216,21 @@ async fn test_force_close_channel_when_remote_is_offline() {
.expect("node_a alive")
.expect("successfully shutdown channel");
}

#[tokio::test]
async fn test_commitment_tx_capacity() {
let (amount_a, amount_b) = (16200000000, 6200000000);
let (node_a, _node_b, channel_id) =
create_nodes_with_established_channel(amount_a, amount_b, true).await;

let state = node_a.store.get_channel_actor_state(&channel_id).unwrap();
let commitment_tx = state.latest_commitment_transaction.unwrap().into_view();
let output_capacity: u64 = commitment_tx.output(0).unwrap().capacity().unpack();

// default fee rate is 1000 shannons per kb, and there is a gap of 20 bytes between the mock commitment tx and the real one
// ref to fn commitment_tx_size
assert_eq!(
amount_a + amount_b - (commitment_tx.data().serialized_size_in_block() + 20) as u128,
output_capacity as u128
);
}
20 changes: 0 additions & 20 deletions tests/bruno/e2e/cross-chain-hub/16-node1-list-ln-channel.bru

This file was deleted.

33 changes: 0 additions & 33 deletions tests/bruno/e2e/cross-chain-hub/17-node1-close-ln-channel.bru

This file was deleted.

24 changes: 0 additions & 24 deletions tests/bruno/e2e/cross-chain-hub/18-node1-list-ln-channel.bru

This file was deleted.

0 comments on commit dbc68fd

Please sign in to comment.