Skip to content

Commit 4563238

Browse files
authored
Merge pull request #71 from TheBlueMatt/2018-07-watch-funding
Ensure the funding transaction is registered to be monitored
2 parents 27476d2 + 896f5b8 commit 4563238

File tree

4 files changed

+26
-17
lines changed

4 files changed

+26
-17
lines changed

src/chain/chaininterface.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use std::sync::atomic::{AtomicUsize, Ordering};
1212
/// events).
1313
pub trait ChainWatchInterface: Sync + Send {
1414
/// Provides a scriptPubKey which much be watched for.
15-
fn install_watch_script(&self, script_pub_key: Script);
15+
fn install_watch_script(&self, script_pub_key: &Script);
1616

1717
/// Provides an outpoint which must be watched for, providing any transactions which spend the
1818
/// given outpoint.
@@ -70,9 +70,9 @@ pub struct ChainWatchInterfaceUtil {
7070

7171
/// Register listener
7272
impl ChainWatchInterface for ChainWatchInterfaceUtil {
73-
fn install_watch_script(&self, script_pub_key: Script) {
73+
fn install_watch_script(&self, script_pub_key: &Script) {
7474
let mut watched = self.watched.lock().unwrap();
75-
watched.0.push(Script::from(script_pub_key));
75+
watched.0.push(script_pub_key.clone());
7676
self.reentered.fetch_add(1, Ordering::Relaxed);
7777
}
7878

@@ -103,7 +103,7 @@ impl ChainWatchInterfaceUtil {
103103
}
104104
}
105105

106-
/// Notify listeners that a block was connected.
106+
/// Notify listeners that a block was connected given a full, unfiltered block.
107107
/// Handles re-scanning the block and calling block_connected again if listeners register new
108108
/// watch data during the callbacks for you (see ChainListener::block_connected for more info).
109109
pub fn block_connected_with_filtering(&self, block: &Block, height: u32) {
@@ -135,7 +135,8 @@ impl ChainWatchInterfaceUtil {
135135
}
136136
}
137137

138-
/// Notify listeners that a block was connected.
138+
/// Notify listeners that a block was connected, given pre-filtered list of transactions in the
139+
/// block which matched the filter (probably using does_match_tx).
139140
/// Returns true if notified listeners registered additional watch data (implying that the
140141
/// block must be re-scanned and this function called again prior to further block_connected
141142
/// calls, see ChainListener::block_connected for more info).

src/ln/channel.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1843,6 +1843,10 @@ impl Channel {
18431843
self.channel_update_count
18441844
}
18451845

1846+
pub fn should_announce(&self) -> bool {
1847+
self.announce_publicly
1848+
}
1849+
18461850
/// Gets the fee we'd want to charge for adding an HTLC output to this Channel
18471851
pub fn get_our_fee_base_msat(&self, fee_estimator: &FeeEstimator) -> u32 {
18481852
// For lack of a better metric, we calculate what it would cost to consolidate the new HTLC

src/ln/channelmanager.rs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -710,7 +710,7 @@ impl ChannelManager {
710710
}
711711

712712
fn get_announcement_sigs(&self, chan: &Channel) -> Result<Option<msgs::AnnouncementSignatures>, HandleError> {
713-
if !chan.is_usable() { return Ok(None) }
713+
if !chan.is_usable() || !chan.should_announce() { return Ok(None) }
714714

715715
let (announcement, our_bitcoin_sig) = chan.get_channel_announcement(self.get_our_node_id(), self.genesis_hash.clone())?;
716716
let msghash = Message::from_slice(&Sha256dHash::from_data(&announcement.encode()[..])[..]).unwrap();
@@ -1839,7 +1839,7 @@ mod tests {
18391839
use bitcoin::util::misc::hex_bytes;
18401840
use bitcoin::util::hash::Sha256dHash;
18411841
use bitcoin::util::uint::Uint256;
1842-
use bitcoin::blockdata::block::BlockHeader;
1842+
use bitcoin::blockdata::block::{Block, BlockHeader};
18431843
use bitcoin::blockdata::transaction::{Transaction, TxOut};
18441844
use bitcoin::network::constants::Network;
18451845
use bitcoin::network::serialize::serialize;
@@ -2010,6 +2010,7 @@ mod tests {
20102010
}
20112011

20122012
fn confirm_transaction(chain: &chaininterface::ChainWatchInterfaceUtil, tx: &Transaction, chan_id: u32) {
2013+
assert!(chain.does_match_tx(tx));
20132014
let mut header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
20142015
chain.block_connected_checked(&header, 1, &[tx; 1], &[chan_id; 1]);
20152016
for i in 2..100 {
@@ -2792,9 +2793,9 @@ mod tests {
27922793
// Simple case with no pending HTLCs:
27932794
nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id(), true);
27942795
{
2795-
let node_txn = test_txn_broadcast(&nodes[1], &chan_1, None, HTLCType::NONE);
2796+
let mut node_txn = test_txn_broadcast(&nodes[1], &chan_1, None, HTLCType::NONE);
27962797
let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
2797-
nodes[0].chain_monitor.block_connected_checked(&header, 1, &[&node_txn[0]; 1], &[4; 1]);
2798+
nodes[0].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![node_txn.drain(..).next().unwrap()] }, 1);
27982799
assert_eq!(nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().len(), 0);
27992800
}
28002801
get_announce_close_broadcast_events(&nodes, 0, 1);
@@ -2807,9 +2808,9 @@ mod tests {
28072808
// Simple case of one pending HTLC to HTLC-Timeout
28082809
nodes[1].node.peer_disconnected(&nodes[2].node.get_our_node_id(), true);
28092810
{
2810-
let node_txn = test_txn_broadcast(&nodes[1], &chan_2, None, HTLCType::TIMEOUT);
2811+
let mut node_txn = test_txn_broadcast(&nodes[1], &chan_2, None, HTLCType::TIMEOUT);
28112812
let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
2812-
nodes[2].chain_monitor.block_connected_checked(&header, 1, &[&node_txn[0]; 1], &[4; 1]);
2813+
nodes[2].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![node_txn.drain(..).next().unwrap()] }, 1);
28132814
assert_eq!(nodes[2].tx_broadcaster.txn_broadcasted.lock().unwrap().len(), 0);
28142815
}
28152816
get_announce_close_broadcast_events(&nodes, 1, 2);
@@ -2848,7 +2849,7 @@ mod tests {
28482849
claim_funds!(nodes[3], nodes[2], payment_preimage_1);
28492850

28502851
let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
2851-
nodes[3].chain_monitor.block_connected_checked(&header, 1, &[&node_txn[0]; 1], &[4; 1]);
2852+
nodes[3].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![node_txn[0].clone()] }, 1);
28522853

28532854
check_preimage_claim(&nodes[3], &node_txn);
28542855
}
@@ -2882,7 +2883,7 @@ mod tests {
28822883
test_txn_broadcast(&nodes[4], &chan_4, None, HTLCType::SUCCESS);
28832884

28842885
header = BlockHeader { version: 0x20000000, prev_blockhash: header.bitcoin_hash(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
2885-
nodes[4].chain_monitor.block_connected_checked(&header, TEST_FINAL_CLTV - 5, &[&node_txn[0]; 1], &[4; 1]);
2886+
nodes[4].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![node_txn[0].clone()] }, TEST_FINAL_CLTV - 5);
28862887

28872888
check_preimage_claim(&nodes[4], &node_txn);
28882889
}
@@ -2902,7 +2903,7 @@ mod tests {
29022903

29032904
{
29042905
let mut header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
2905-
nodes[1].chain_monitor.block_connected_checked(&header, 1, &vec![&revoked_local_txn[0]; 1], &[4; 1]);
2906+
nodes[1].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![revoked_local_txn[0].clone()] }, 1);
29062907
{
29072908
let mut node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap();
29082909
assert_eq!(node_txn.len(), 1);
@@ -2914,10 +2915,10 @@ mod tests {
29142915
node_txn.clear();
29152916
}
29162917

2917-
nodes[0].chain_monitor.block_connected_checked(&header, 1, &vec![&revoked_local_txn[0]; 1], &[4; 0]);
2918+
nodes[0].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![revoked_local_txn[0].clone()] }, 1);
29182919
let node_txn = test_txn_broadcast(&nodes[0], &chan_5, Some(revoked_local_txn[0].clone()), HTLCType::TIMEOUT);
29192920
header = BlockHeader { version: 0x20000000, prev_blockhash: header.bitcoin_hash(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
2920-
nodes[1].chain_monitor.block_connected_checked(&header, 1, &[&node_txn[1]; 1], &[4; 1]);
2921+
nodes[1].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![node_txn[1].clone()] }, 1);
29212922

29222923
//TODO: At this point nodes[1] should claim the revoked HTLC-Timeout output, but that's
29232924
//not yet implemented in ChannelMonitor

src/ln/channelmonitor.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,10 @@ impl<Key : Send + cmp::Eq + hash::Hash + 'static> SimpleManyChannelMonitor<Key>
9494
};
9595
match &monitor.funding_txo {
9696
&None => self.chain_monitor.watch_all_txn(),
97-
&Some((ref outpoint, ref script)) => self.chain_monitor.install_watch_outpoint((outpoint.txid, outpoint.index as u32), script),
97+
&Some((ref outpoint, ref script)) => {
98+
self.chain_monitor.install_watch_script(script);
99+
self.chain_monitor.install_watch_outpoint((outpoint.txid, outpoint.index as u32), script);
100+
},
98101
}
99102
monitors.insert(key, monitor);
100103
Ok(())

0 commit comments

Comments
 (0)