Skip to content

Commit d5c6057

Browse files
committed
Track block height in ChannelMonitor
1 parent 2cdf697 commit d5c6057

File tree

3 files changed

+25
-12
lines changed

3 files changed

+25
-12
lines changed

lightning/src/chain/channelmonitor.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -743,6 +743,8 @@ pub(crate) struct ChannelMonitorImpl<Signer: Sign> {
743743
// their last_block_hash from its state and not based on updated copies that didn't run through
744744
// the full block_connected).
745745
last_block_hash: BlockHash,
746+
last_block_height: u32,
747+
746748
secp_ctx: Secp256k1<secp256k1::All>, //TODO: dedup this a bit...
747749
}
748750

@@ -955,6 +957,7 @@ impl<Signer: Sign> Writeable for ChannelMonitorImpl<Signer> {
955957
}
956958

957959
self.last_block_hash.write(writer)?;
960+
writer.write_all(&byte_utils::be32_to_array(self.last_block_height))?;
958961

959962
writer.write_all(&byte_utils::be64_to_array(self.onchain_events_waiting_threshold_conf.len() as u64))?;
960963
for ref entry in self.onchain_events_waiting_threshold_conf.iter() {
@@ -998,7 +1001,7 @@ impl<Signer: Sign> ChannelMonitor<Signer> {
9981001
funding_redeemscript: Script, channel_value_satoshis: u64,
9991002
commitment_transaction_number_obscure_factor: u64,
10001003
initial_holder_commitment_tx: HolderCommitmentTransaction,
1001-
last_block_hash: BlockHash) -> ChannelMonitor<Signer> {
1004+
last_block_hash: BlockHash, last_block_height: u32) -> ChannelMonitor<Signer> {
10021005

10031006
assert!(commitment_transaction_number_obscure_factor <= (1 << 48));
10041007
let our_channel_close_key_hash = WPubkeyHash::hash(&shutdown_pubkey.serialize());
@@ -1086,6 +1089,8 @@ impl<Signer: Sign> ChannelMonitor<Signer> {
10861089
holder_tx_signed: false,
10871090

10881091
last_block_hash,
1092+
last_block_height,
1093+
10891094
secp_ctx,
10901095
}),
10911096
}
@@ -2125,6 +2130,7 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
21252130

21262131
self.onchain_tx_handler.update_claims_view(&txn_matched, claimable_outpoints, Some(height), &&*broadcaster, &&*fee_estimator, &&*logger);
21272132
self.last_block_hash = block_hash;
2133+
self.last_block_height = height;
21282134

21292135
// Determine new outputs to watch by comparing against previously known outputs to watch,
21302136
// updating the latter in the process.
@@ -2164,6 +2170,7 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
21642170
self.onchain_tx_handler.block_disconnected(height, broadcaster, fee_estimator, logger);
21652171

21662172
self.last_block_hash = header.prev_blockhash;
2173+
self.last_block_height = height - 1;
21672174
}
21682175

21692176
/// Filters a block's `txdata` for transactions spending watched outputs or for any child
@@ -2735,6 +2742,7 @@ impl<'a, Signer: Sign, K: KeysInterface<Signer = Signer>> ReadableArgs<&'a K>
27352742
}
27362743

27372744
let last_block_hash: BlockHash = Readable::read(reader)?;
2745+
let last_block_height: u32 = Readable::read(reader)?;
27382746

27392747
let waiting_threshold_conf_len: u64 = Readable::read(reader)?;
27402748
let mut onchain_events_waiting_threshold_conf = Vec::with_capacity(cmp::min(waiting_threshold_conf_len as usize, MAX_ALLOC_SIZE / 128));
@@ -2827,6 +2835,8 @@ impl<'a, Signer: Sign, K: KeysInterface<Signer = Signer>> ReadableArgs<&'a K>
28272835
holder_tx_signed,
28282836

28292837
last_block_hash,
2838+
last_block_height,
2839+
28302840
secp_ctx,
28312841
}),
28322842
}))
@@ -2946,12 +2956,13 @@ mod tests {
29462956
// Prune with one old state and a holder commitment tx holding a few overlaps with the
29472957
// old state.
29482958
let last_block_hash = genesis_block(Network::Testnet).block_hash();
2959+
let last_block_height = 0;
29492960
let monitor = ChannelMonitor::new(Secp256k1::new(), keys,
29502961
&PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[42; 32]).unwrap()), 0, &Script::new(),
29512962
(OutPoint { txid: Txid::from_slice(&[43; 32]).unwrap(), index: 0 }, Script::new()),
29522963
&channel_parameters,
29532964
Script::new(), 46, 0,
2954-
HolderCommitmentTransaction::dummy(), last_block_hash);
2965+
HolderCommitmentTransaction::dummy(), last_block_hash, last_block_height);
29552966

29562967
monitor.provide_latest_holder_commitment_tx(HolderCommitmentTransaction::dummy(), preimages_to_holder_htlcs!(preimages[0..10])).unwrap();
29572968
let dummy_txid = dummy_tx.txid();

lightning/src/ln/channel.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1529,7 +1529,7 @@ impl<Signer: Sign> Channel<Signer> {
15291529
&self.get_counterparty_pubkeys().funding_pubkey
15301530
}
15311531

1532-
pub fn funding_created<L: Deref>(&mut self, msg: &msgs::FundingCreated, last_block_hash: BlockHash, logger: &L) -> Result<(msgs::FundingSigned, ChannelMonitor<Signer>), ChannelError> where L::Target: Logger {
1532+
pub fn funding_created<L: Deref>(&mut self, msg: &msgs::FundingCreated, last_block_hash: BlockHash, last_block_height: u32, logger: &L) -> Result<(msgs::FundingSigned, ChannelMonitor<Signer>), ChannelError> where L::Target: Logger {
15331533
if self.is_outbound() {
15341534
return Err(ChannelError::Close("Received funding_created for an outbound channel?".to_owned()));
15351535
}
@@ -1583,7 +1583,7 @@ impl<Signer: Sign> Channel<Signer> {
15831583
&self.channel_transaction_parameters,
15841584
funding_redeemscript.clone(), self.channel_value_satoshis,
15851585
obscure_factor,
1586-
holder_commitment_tx, last_block_hash);
1586+
holder_commitment_tx, last_block_hash, last_block_height);
15871587

15881588
channel_monitor.provide_latest_counterparty_commitment_tx(counterparty_initial_commitment_txid, Vec::new(), self.cur_counterparty_commitment_transaction_number, self.counterparty_cur_commitment_point.unwrap(), logger);
15891589

@@ -1600,7 +1600,7 @@ impl<Signer: Sign> Channel<Signer> {
16001600

16011601
/// Handles a funding_signed message from the remote end.
16021602
/// If this call is successful, broadcast the funding transaction (and not before!)
1603-
pub fn funding_signed<L: Deref>(&mut self, msg: &msgs::FundingSigned, last_block_hash: BlockHash, logger: &L) -> Result<ChannelMonitor<Signer>, ChannelError> where L::Target: Logger {
1603+
pub fn funding_signed<L: Deref>(&mut self, msg: &msgs::FundingSigned, last_block_hash: BlockHash, last_block_height: u32, logger: &L) -> Result<ChannelMonitor<Signer>, ChannelError> where L::Target: Logger {
16041604
if !self.is_outbound() {
16051605
return Err(ChannelError::Close("Received funding_signed for an inbound channel?".to_owned()));
16061606
}
@@ -1653,7 +1653,7 @@ impl<Signer: Sign> Channel<Signer> {
16531653
&self.channel_transaction_parameters,
16541654
funding_redeemscript.clone(), self.channel_value_satoshis,
16551655
obscure_factor,
1656-
holder_commitment_tx, last_block_hash);
1656+
holder_commitment_tx, last_block_hash, last_block_height);
16571657

16581658
channel_monitor.provide_latest_counterparty_commitment_tx(counterparty_initial_bitcoin_tx.txid, Vec::new(), self.cur_counterparty_commitment_transaction_number, self.counterparty_cur_commitment_point.unwrap(), logger);
16591659

@@ -5006,6 +5006,7 @@ mod tests {
50065006
let network = Network::Testnet;
50075007
let chain_hash = genesis_block(network).header.block_hash();
50085008
let last_block_hash = chain_hash;
5009+
let last_block_height = 0;
50095010
let keys_provider = test_utils::TestKeysInterface::new(&seed, network);
50105011

50115012
// Go through the flow of opening a channel between two nodes.
@@ -5031,10 +5032,10 @@ mod tests {
50315032
}]};
50325033
let funding_outpoint = OutPoint{ txid: tx.txid(), index: 0 };
50335034
let funding_created_msg = node_a_chan.get_outbound_funding_created(funding_outpoint, &&logger).unwrap();
5034-
let (funding_signed_msg, _) = node_b_chan.funding_created(&funding_created_msg, last_block_hash, &&logger).unwrap();
5035+
let (funding_signed_msg, _) = node_b_chan.funding_created(&funding_created_msg, last_block_hash, last_block_height, &&logger).unwrap();
50355036

50365037
// Node B --> Node A: funding signed
5037-
let _ = node_a_chan.funding_signed(&funding_signed_msg, last_block_hash, &&logger);
5038+
let _ = node_a_chan.funding_signed(&funding_signed_msg, last_block_hash, last_block_height, &&logger);
50385039

50395040
// Now disconnect the two nodes and check that the commitment point in
50405041
// Node B's channel_reestablish message is sane.

lightning/src/ln/channelmanager.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,7 @@ pub struct ChainParameters {
477477
}
478478

479479
/// The best known block as identified by its hash and height.
480+
#[derive(Clone, Copy)]
480481
pub struct BestBlock {
481482
block_hash: BlockHash,
482483
height: u32,
@@ -2500,15 +2501,15 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
25002501

25012502
fn internal_funding_created(&self, counterparty_node_id: &PublicKey, msg: &msgs::FundingCreated) -> Result<(), MsgHandleErrInternal> {
25022503
let ((funding_msg, monitor), mut chan) = {
2503-
let last_block_hash = self.best_block.read().unwrap().block_hash();
2504+
let best_block = *self.best_block.read().unwrap();
25042505
let mut channel_lock = self.channel_state.lock().unwrap();
25052506
let channel_state = &mut *channel_lock;
25062507
match channel_state.by_id.entry(msg.temporary_channel_id.clone()) {
25072508
hash_map::Entry::Occupied(mut chan) => {
25082509
if chan.get().get_counterparty_node_id() != *counterparty_node_id {
25092510
return Err(MsgHandleErrInternal::send_err_msg_no_close("Got a message for a channel from the wrong node!".to_owned(), msg.temporary_channel_id));
25102511
}
2511-
(try_chan_entry!(self, chan.get_mut().funding_created(msg, last_block_hash, &self.logger), channel_state, chan), chan.remove())
2512+
(try_chan_entry!(self, chan.get_mut().funding_created(msg, best_block.block_hash(), best_block.height(), &self.logger), channel_state, chan), chan.remove())
25122513
},
25132514
hash_map::Entry::Vacant(_) => return Err(MsgHandleErrInternal::send_err_msg_no_close("Failed to find corresponding channel".to_owned(), msg.temporary_channel_id))
25142515
}
@@ -2557,15 +2558,15 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
25572558

25582559
fn internal_funding_signed(&self, counterparty_node_id: &PublicKey, msg: &msgs::FundingSigned) -> Result<(), MsgHandleErrInternal> {
25592560
let (funding_txo, user_id) = {
2560-
let last_block_hash = self.best_block.read().unwrap().block_hash();
2561+
let best_block = *self.best_block.read().unwrap();
25612562
let mut channel_lock = self.channel_state.lock().unwrap();
25622563
let channel_state = &mut *channel_lock;
25632564
match channel_state.by_id.entry(msg.channel_id) {
25642565
hash_map::Entry::Occupied(mut chan) => {
25652566
if chan.get().get_counterparty_node_id() != *counterparty_node_id {
25662567
return Err(MsgHandleErrInternal::send_err_msg_no_close("Got a message for a channel from the wrong node!".to_owned(), msg.channel_id));
25672568
}
2568-
let monitor = match chan.get_mut().funding_signed(&msg, last_block_hash, &self.logger) {
2569+
let monitor = match chan.get_mut().funding_signed(&msg, best_block.block_hash(), best_block.height(), &self.logger) {
25692570
Ok(update) => update,
25702571
Err(e) => try_chan_entry!(self, Err(e), channel_state, chan),
25712572
};

0 commit comments

Comments
 (0)