Skip to content

Commit 2b87cc8

Browse files
committed
Insert channel funding outputs into Wallet
When doing a splice, to properly calculate fees we need the channel's funding utxo in our wallet, otherwise our wallet won't know the channel's original size. This adds the channel funding txo on ChannelReady events and modifies the splicing test to make sure we can calculate fees on splice-ins.
1 parent 198ff30 commit 2b87cc8

File tree

3 files changed

+47
-4
lines changed

3 files changed

+47
-4
lines changed

src/event.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1458,6 +1458,31 @@ where
14581458
counterparty_node_id,
14591459
funding_txo,
14601460
);
1461+
1462+
let chans =
1463+
self.channel_manager.list_channels_with_counterparty(&counterparty_node_id);
1464+
let chan_output = chans
1465+
.iter()
1466+
.find(|c| c.user_channel_id == user_channel_id)
1467+
.and_then(|c| c.get_funding_output());
1468+
match chan_output {
1469+
None => {
1470+
log_error!(
1471+
self.logger,
1472+
"Failed to find channel info for pending channel {channel_id} with counterparty {counterparty_node_id}"
1473+
);
1474+
return Err(ReplayEvent());
1475+
},
1476+
Some(output) => {
1477+
if let Err(e) = self.wallet.insert_txo(funding_txo, output) {
1478+
log_error!(
1479+
self.logger,
1480+
"Failed to insert funding TXO into wallet: {e}"
1481+
);
1482+
return Err(ReplayEvent());
1483+
}
1484+
},
1485+
}
14611486
} else {
14621487
log_info!(
14631488
self.logger,

src/wallet/mod.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use bitcoin::secp256k1::ecdh::SharedSecret;
2626
use bitcoin::secp256k1::ecdsa::{RecoverableSignature, Signature};
2727
use bitcoin::secp256k1::{All, PublicKey, Scalar, Secp256k1, SecretKey};
2828
use bitcoin::{
29-
Address, Amount, FeeRate, ScriptBuf, Transaction, TxOut, Txid, WPubkeyHash, Weight,
29+
Address, Amount, FeeRate, OutPoint, ScriptBuf, Transaction, TxOut, Txid, WPubkeyHash, Weight,
3030
WitnessProgram, WitnessVersion,
3131
};
3232
use lightning::chain::chaininterface::BroadcasterInterface;
@@ -153,6 +153,19 @@ impl Wallet {
153153
Ok(())
154154
}
155155

156+
pub(crate) fn insert_txo(&self, outpoint: OutPoint, txout: TxOut) -> Result<(), Error> {
157+
let mut locked_wallet = self.inner.lock().unwrap();
158+
locked_wallet.insert_txout(outpoint, txout);
159+
160+
let mut locked_persister = self.persister.lock().unwrap();
161+
locked_wallet.persist(&mut locked_persister).map_err(|e| {
162+
log_error!(self.logger, "Failed to persist wallet: {}", e);
163+
Error::PersistenceFailed
164+
})?;
165+
166+
Ok(())
167+
}
168+
156169
fn update_payment_store<'a>(
157170
&self, locked_wallet: &'a mut PersistedWallet<KVStoreWalletPersister>,
158171
) -> Result<(), Error> {

tests/integration_tests_rust.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -995,7 +995,7 @@ async fn splice_channel() {
995995
// Splice-in funds for Node B so that it has outbound liquidity to make a payment
996996
node_b.splice_in(&user_channel_id_b, node_a.node_id(), 4_000_000).unwrap();
997997

998-
expect_splice_pending_event!(node_a, node_b.node_id());
998+
let txo = expect_splice_pending_event!(node_a, node_b.node_id());
999999
expect_splice_pending_event!(node_b, node_a.node_id());
10001000

10011001
generate_blocks_and_wait(&bitcoind.client, &electrsd.client, 6).await;
@@ -1006,11 +1006,16 @@ async fn splice_channel() {
10061006
expect_channel_ready_event!(node_a, node_b.node_id());
10071007
expect_channel_ready_event!(node_b, node_a.node_id());
10081008

1009-
let splice_in_fee_sat = 252;
1009+
let expected_splice_in_fee_sat = 252;
1010+
1011+
let payments = node_b.list_payments();
1012+
let payment =
1013+
payments.into_iter().find(|p| p.id == PaymentId(txo.txid.to_byte_array())).unwrap();
1014+
assert_eq!(payment.fee_paid_msat, Some(expected_splice_in_fee_sat * 1_000));
10101015

10111016
assert_eq!(
10121017
node_b.list_balances().total_onchain_balance_sats,
1013-
premine_amount_sat - 4_000_000 - splice_in_fee_sat
1018+
premine_amount_sat - 4_000_000 - expected_splice_in_fee_sat
10141019
);
10151020
assert_eq!(node_b.list_balances().total_lightning_balance_sats, 4_000_000);
10161021

0 commit comments

Comments
 (0)