Skip to content

Commit b21048a

Browse files
committed
Check funding txout format when transaction is confirmed in channel
1 parent f452403 commit b21048a

File tree

3 files changed

+32
-12
lines changed

3 files changed

+32
-12
lines changed

fuzz/fuzz_targets/channel_target.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ extern crate lightning;
33
extern crate secp256k1;
44

55
use bitcoin::blockdata::block::BlockHeader;
6-
use bitcoin::blockdata::transaction::Transaction;
6+
use bitcoin::blockdata::transaction::{Transaction, TxOut};
77
use bitcoin::util::hash::Sha256dHash;
88
use bitcoin::network::serialize::{serialize, BitcoinHash};
99

@@ -166,18 +166,22 @@ pub fn do_test(data: &[u8]) {
166166

167167
let their_pubkey = get_pubkey!();
168168

169-
let tx = Transaction { version: 0, lock_time: 0, input: Vec::new(), output: Vec::new() };
170-
let funding_output = (Sha256dHash::from_data(&serialize(&tx).unwrap()[..]), 0);
169+
let mut tx = Transaction { version: 0, lock_time: 0, input: Vec::new(), output: Vec::new() };
171170

172171
let mut channel = if get_slice!(1)[0] != 0 {
173-
let mut chan = Channel::new_outbound(&fee_est, their_pubkey, slice_to_be24(get_slice!(3)), get_slice!(1)[0] == 0, slice_to_be64(get_slice!(8)));
172+
let chan_value = slice_to_be24(get_slice!(3));
173+
let mut chan = Channel::new_outbound(&fee_est, their_pubkey, chan_value, get_slice!(1)[0] == 0, slice_to_be64(get_slice!(8)));
174174
chan.get_open_channel(Sha256dHash::from(get_slice!(32)), &fee_est).unwrap();
175175
let accept_chan = if get_slice!(1)[0] == 0 {
176176
decode_msg_with_len16!(msgs::AcceptChannel, 270, 1)
177177
} else {
178178
decode_msg!(msgs::AcceptChannel, 270)
179179
};
180180
return_err!(chan.accept_channel(&accept_chan));
181+
182+
tx.output.push(TxOut{ value: chan_value, script_pubkey: chan.get_funding_redeemscript().to_v0_p2wsh() });
183+
let funding_output = (Sha256dHash::from_data(&serialize(&tx).unwrap()[..]), 0);
184+
181185
chan.get_outbound_funding_created(funding_output.0.clone(), funding_output.1).unwrap();
182186
let funding_signed = decode_msg!(msgs::FundingSigned, 32+64);
183187
return_err!(chan.funding_signed(&funding_signed));
@@ -193,6 +197,10 @@ pub fn do_test(data: &[u8]) {
193197
Err(_) => return,
194198
};
195199
chan.get_accept_channel().unwrap();
200+
201+
tx.output.push(TxOut{ value: open_chan.funding_satoshis, script_pubkey: chan.get_funding_redeemscript().to_v0_p2wsh() });
202+
let funding_output = (Sha256dHash::from_data(&serialize(&tx).unwrap()[..]), 0);
203+
196204
let mut funding_created = decode_msg!(msgs::FundingCreated, 32+32+2+64);
197205
funding_created.funding_txid = funding_output.0.clone();
198206
funding_created.funding_output_index = funding_output.1;

src/ln/channel.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1639,10 +1639,16 @@ impl Channel {
16391639
if non_shutdown_state & !(ChannelState::TheirFundingLocked as u32) == ChannelState::FundingSent as u32 {
16401640
for (ref tx, index_in_block) in txn_matched.iter().zip(indexes_of_txn_matched) {
16411641
if tx.txid() == self.channel_monitor.get_funding_txo().unwrap().0 {
1642-
self.funding_tx_confirmations = 1;
1643-
self.short_channel_id = Some(((height as u64) << (5*8)) |
1644-
((*index_in_block as u64) << (2*8)) |
1645-
((self.channel_monitor.get_funding_txo().unwrap().1 as u64) << (2*8)));
1642+
let txo_idx = self.channel_monitor.get_funding_txo().unwrap().1 as usize;
1643+
if txo_idx >= tx.output.len() || tx.output[txo_idx].script_pubkey != self.get_funding_redeemscript().to_v0_p2wsh() ||
1644+
tx.output[txo_idx].value != self.channel_value_satoshis {
1645+
self.channel_state = ChannelState::ShutdownComplete as u32;
1646+
} else {
1647+
self.funding_tx_confirmations = 1;
1648+
self.short_channel_id = Some(((height as u64) << (5*8)) |
1649+
((*index_in_block as u64) << (2*8)) |
1650+
((self.channel_monitor.get_funding_txo().unwrap().1 as u64) << (2*8)));
1651+
}
16461652
}
16471653
}
16481654
}

src/ln/channelmanager.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -906,6 +906,7 @@ impl ChainListener for ChannelManager {
906906
},
907907
None => {}
908908
}
909+
//TODO: Check if channel was closed (or disabled) here
909910
}
910911
for to_insert in short_to_ids_to_insert {
911912
channel_state.short_to_id.insert(to_insert.0, to_insert.1);
@@ -1496,7 +1497,7 @@ mod tests {
14961497
use bitcoin::util::hash::Sha256dHash;
14971498
use bitcoin::util::uint::Uint256;
14981499
use bitcoin::blockdata::block::BlockHeader;
1499-
use bitcoin::blockdata::transaction::Transaction;
1500+
use bitcoin::blockdata::transaction::{Transaction, TxOut};
15001501
use bitcoin::network::constants::Network;
15011502
use bitcoin::network::serialize::serialize;
15021503
use bitcoin::network::serialize::BitcoinHash;
@@ -1680,16 +1681,21 @@ mod tests {
16801681
node_a.handle_accept_channel(&node_b.get_our_node_id(), &accept_chan).unwrap();
16811682

16821683
let chan_id = unsafe { CHAN_COUNT };
1683-
let tx = Transaction { version: chan_id as u32, lock_time: 0, input: Vec::new(), output: Vec::new() };
1684-
let funding_output = (Sha256dHash::from_data(&serialize(&tx).unwrap()[..]), chan_id);
1684+
let tx;
1685+
let funding_output;
16851686

16861687
let events_1 = node_a.get_and_clear_pending_events();
16871688
assert_eq!(events_1.len(), 1);
16881689
match events_1[0] {
1689-
Event::FundingGenerationReady { ref temporary_channel_id, ref channel_value_satoshis, output_script: _, user_channel_id } => {
1690+
Event::FundingGenerationReady { ref temporary_channel_id, ref channel_value_satoshis, ref output_script, user_channel_id } => {
16901691
assert_eq!(*channel_value_satoshis, 100000);
16911692
assert_eq!(user_channel_id, 42);
16921693

1694+
tx = Transaction { version: chan_id as u32, lock_time: 0, input: Vec::new(), output: vec![TxOut {
1695+
value: *channel_value_satoshis, script_pubkey: output_script.clone(),
1696+
}]};
1697+
funding_output = (Sha256dHash::from_data(&serialize(&tx).unwrap()[..]), 0);
1698+
16931699
node_a.funding_transaction_generated(&temporary_channel_id, funding_output.clone());
16941700
//TODO: Check that we got added to chan_monitor_a!
16951701
},

0 commit comments

Comments
 (0)