Skip to content

Commit 2db1f1f

Browse files
committed
Track block height in ChannelMonitor
1 parent 5610ca1 commit 2db1f1f

File tree

3 files changed

+34
-30
lines changed

3 files changed

+34
-30
lines changed

lightning/src/chain/channelmonitor.rs

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ use bitcoin::secp256k1;
3737
use ln::msgs::DecodeError;
3838
use ln::chan_utils;
3939
use ln::chan_utils::{CounterpartyCommitmentSecrets, HTLCOutputInCommitment, HTLCType, ChannelTransactionParameters, HolderCommitmentTransaction};
40-
use ln::channelmanager::{HTLCSource, PaymentPreimage, PaymentHash};
40+
use ln::channelmanager::{BestBlock, HTLCSource, PaymentPreimage, PaymentHash};
4141
use ln::onchaintx::{OnchainTxHandler, InputDescriptors};
4242
use chain;
4343
use chain::WatchedOutput;
@@ -735,12 +735,13 @@ pub(crate) struct ChannelMonitorImpl<Signer: Sign> {
735735
// remote monitor out-of-order with regards to the block view.
736736
holder_tx_signed: bool,
737737

738-
// We simply modify last_block_hash in Channel's block_connected so that serialization is
738+
// We simply modify best_block in Channel's block_connected so that serialization is
739739
// consistent but hopefully the users' copy handles block_connected in a consistent way.
740740
// (we do *not*, however, update them in update_monitor to ensure any local user copies keep
741-
// their last_block_hash from its state and not based on updated copies that didn't run through
741+
// their best_block from its state and not based on updated copies that didn't run through
742742
// the full block_connected).
743-
last_block_hash: BlockHash,
743+
best_block: BestBlock,
744+
744745
secp_ctx: Secp256k1<secp256k1::All>, //TODO: dedup this a bit...
745746
}
746747

@@ -952,7 +953,8 @@ impl<Signer: Sign> Writeable for ChannelMonitorImpl<Signer> {
952953
event.write(writer)?;
953954
}
954955

955-
self.last_block_hash.write(writer)?;
956+
self.best_block.block_hash().write(writer)?;
957+
writer.write_all(&byte_utils::be32_to_array(self.best_block.height()))?;
956958

957959
writer.write_all(&byte_utils::be64_to_array(self.onchain_events_waiting_threshold_conf.len() as u64))?;
958960
for ref entry in self.onchain_events_waiting_threshold_conf.iter() {
@@ -996,7 +998,7 @@ impl<Signer: Sign> ChannelMonitor<Signer> {
996998
funding_redeemscript: Script, channel_value_satoshis: u64,
997999
commitment_transaction_number_obscure_factor: u64,
9981000
initial_holder_commitment_tx: HolderCommitmentTransaction,
999-
last_block_hash: BlockHash) -> ChannelMonitor<Signer> {
1001+
best_block: BestBlock) -> ChannelMonitor<Signer> {
10001002

10011003
assert!(commitment_transaction_number_obscure_factor <= (1 << 48));
10021004
let our_channel_close_key_hash = WPubkeyHash::hash(&shutdown_pubkey.serialize());
@@ -1083,7 +1085,8 @@ impl<Signer: Sign> ChannelMonitor<Signer> {
10831085
lockdown_from_offchain: false,
10841086
holder_tx_signed: false,
10851087

1086-
last_block_hash,
1088+
best_block,
1089+
10871090
secp_ctx,
10881091
}),
10891092
}
@@ -2132,7 +2135,7 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
21322135
}
21332136

21342137
self.onchain_tx_handler.update_claims_view(&txn_matched, claimable_outpoints, Some(height), &&*broadcaster, &&*fee_estimator, &&*logger);
2135-
self.last_block_hash = block_hash;
2138+
self.best_block = BestBlock::new(block_hash, height);
21362139

21372140
// Determine new outputs to watch by comparing against previously known outputs to watch,
21382141
// updating the latter in the process.
@@ -2171,7 +2174,7 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
21712174

21722175
self.onchain_tx_handler.block_disconnected(height, broadcaster, fee_estimator, logger);
21732176

2174-
self.last_block_hash = header.prev_blockhash;
2177+
self.best_block = BestBlock::new(header.prev_blockhash, height - 1);
21752178
}
21762179

21772180
/// Filters a block's `txdata` for transactions spending watched outputs or for any child
@@ -2742,7 +2745,7 @@ impl<'a, Signer: Sign, K: KeysInterface<Signer = Signer>> ReadableArgs<&'a K>
27422745
}
27432746
}
27442747

2745-
let last_block_hash: BlockHash = Readable::read(reader)?;
2748+
let best_block = BestBlock::new(Readable::read(reader)?, Readable::read(reader)?);
27462749

27472750
let waiting_threshold_conf_len: u64 = Readable::read(reader)?;
27482751
let mut onchain_events_waiting_threshold_conf = Vec::with_capacity(cmp::min(waiting_threshold_conf_len as usize, MAX_ALLOC_SIZE / 128));
@@ -2789,7 +2792,7 @@ impl<'a, Signer: Sign, K: KeysInterface<Signer = Signer>> ReadableArgs<&'a K>
27892792
let mut secp_ctx = Secp256k1::new();
27902793
secp_ctx.seeded_randomize(&keys_manager.get_secure_random_bytes());
27912794

2792-
Ok((last_block_hash.clone(), ChannelMonitor {
2795+
Ok((best_block.block_hash(), ChannelMonitor {
27932796
inner: Mutex::new(ChannelMonitorImpl {
27942797
latest_update_id,
27952798
commitment_transaction_number_obscure_factor,
@@ -2834,7 +2837,8 @@ impl<'a, Signer: Sign, K: KeysInterface<Signer = Signer>> ReadableArgs<&'a K>
28342837
lockdown_from_offchain,
28352838
holder_tx_signed,
28362839

2837-
last_block_hash,
2840+
best_block,
2841+
28382842
secp_ctx,
28392843
}),
28402844
}))
@@ -2843,7 +2847,6 @@ impl<'a, Signer: Sign, K: KeysInterface<Signer = Signer>> ReadableArgs<&'a K>
28432847

28442848
#[cfg(test)]
28452849
mod tests {
2846-
use bitcoin::blockdata::constants::genesis_block;
28472850
use bitcoin::blockdata::script::{Script, Builder};
28482851
use bitcoin::blockdata::opcodes;
28492852
use bitcoin::blockdata::transaction::{Transaction, TxIn, TxOut, SigHashType};
@@ -2857,7 +2860,7 @@ mod tests {
28572860
use hex;
28582861
use chain::channelmonitor::ChannelMonitor;
28592862
use chain::transaction::OutPoint;
2860-
use ln::channelmanager::{PaymentPreimage, PaymentHash};
2863+
use ln::channelmanager::{BestBlock, PaymentPreimage, PaymentHash};
28612864
use ln::onchaintx::{OnchainTxHandler, InputDescriptors};
28622865
use ln::chan_utils;
28632866
use ln::chan_utils::{HTLCOutputInCommitment, ChannelPublicKeys, ChannelTransactionParameters, HolderCommitmentTransaction, CounterpartyChannelTransactionParameters};
@@ -2953,13 +2956,13 @@ mod tests {
29532956
};
29542957
// Prune with one old state and a holder commitment tx holding a few overlaps with the
29552958
// old state.
2956-
let last_block_hash = genesis_block(Network::Testnet).block_hash();
2959+
let best_block = BestBlock::from_genesis(Network::Testnet);
29572960
let monitor = ChannelMonitor::new(Secp256k1::new(), keys,
29582961
&PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[42; 32]).unwrap()), 0, &Script::new(),
29592962
(OutPoint { txid: Txid::from_slice(&[43; 32]).unwrap(), index: 0 }, Script::new()),
29602963
&channel_parameters,
29612964
Script::new(), 46, 0,
2962-
HolderCommitmentTransaction::dummy(), last_block_hash);
2965+
HolderCommitmentTransaction::dummy(), best_block);
29632966

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

lightning/src/ln/channel.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use bitcoin::secp256k1;
2424
use ln::features::{ChannelFeatures, InitFeatures};
2525
use ln::msgs;
2626
use ln::msgs::{DecodeError, OptionalField, DataLossProtect};
27-
use ln::channelmanager::{PendingHTLCStatus, HTLCSource, HTLCFailReason, HTLCFailureMsg, PendingHTLCInfo, RAACommitmentOrder, PaymentPreimage, PaymentHash, BREAKDOWN_TIMEOUT, MIN_CLTV_EXPIRY_DELTA, MAX_LOCAL_BREAKDOWN_TIMEOUT};
27+
use ln::channelmanager::{BestBlock, PendingHTLCStatus, HTLCSource, HTLCFailReason, HTLCFailureMsg, PendingHTLCInfo, RAACommitmentOrder, PaymentPreimage, PaymentHash, BREAKDOWN_TIMEOUT, MIN_CLTV_EXPIRY_DELTA, MAX_LOCAL_BREAKDOWN_TIMEOUT};
2828
use ln::chan_utils::{CounterpartyCommitmentSecrets, TxCreationKeys, HTLCOutputInCommitment, HTLC_SUCCESS_TX_WEIGHT, HTLC_TIMEOUT_TX_WEIGHT, make_funding_redeemscript, ChannelPublicKeys, CommitmentTransaction, HolderCommitmentTransaction, ChannelTransactionParameters, CounterpartyChannelTransactionParameters, MAX_HTLCS, get_commitment_transaction_number_obscure_factor};
2929
use ln::chan_utils;
3030
use chain::chaininterface::{FeeEstimator,ConfirmationTarget};
@@ -1531,7 +1531,7 @@ impl<Signer: Sign> Channel<Signer> {
15311531
&self.get_counterparty_pubkeys().funding_pubkey
15321532
}
15331533

1534-
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 {
1534+
pub fn funding_created<L: Deref>(&mut self, msg: &msgs::FundingCreated, best_block: BestBlock, logger: &L) -> Result<(msgs::FundingSigned, ChannelMonitor<Signer>), ChannelError> where L::Target: Logger {
15351535
if self.is_outbound() {
15361536
return Err(ChannelError::Close("Received funding_created for an outbound channel?".to_owned()));
15371537
}
@@ -1585,7 +1585,7 @@ impl<Signer: Sign> Channel<Signer> {
15851585
&self.channel_transaction_parameters,
15861586
funding_redeemscript.clone(), self.channel_value_satoshis,
15871587
obscure_factor,
1588-
holder_commitment_tx, last_block_hash);
1588+
holder_commitment_tx, best_block);
15891589

15901590
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);
15911591

@@ -1602,7 +1602,7 @@ impl<Signer: Sign> Channel<Signer> {
16021602

16031603
/// Handles a funding_signed message from the remote end.
16041604
/// If this call is successful, broadcast the funding transaction (and not before!)
1605-
pub fn funding_signed<L: Deref>(&mut self, msg: &msgs::FundingSigned, last_block_hash: BlockHash, logger: &L) -> Result<(ChannelMonitor<Signer>, Transaction), ChannelError> where L::Target: Logger {
1605+
pub fn funding_signed<L: Deref>(&mut self, msg: &msgs::FundingSigned, best_block: BestBlock, logger: &L) -> Result<(ChannelMonitor<Signer>, Transaction), ChannelError> where L::Target: Logger {
16061606
if !self.is_outbound() {
16071607
return Err(ChannelError::Close("Received funding_signed for an inbound channel?".to_owned()));
16081608
}
@@ -1655,7 +1655,7 @@ impl<Signer: Sign> Channel<Signer> {
16551655
&self.channel_transaction_parameters,
16561656
funding_redeemscript.clone(), self.channel_value_satoshis,
16571657
obscure_factor,
1658-
holder_commitment_tx, last_block_hash);
1658+
holder_commitment_tx, best_block);
16591659

16601660
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);
16611661

@@ -4825,7 +4825,7 @@ mod tests {
48254825
use bitcoin::network::constants::Network;
48264826
use bitcoin::hashes::hex::FromHex;
48274827
use hex;
4828-
use ln::channelmanager::{HTLCSource, PaymentPreimage, PaymentHash};
4828+
use ln::channelmanager::{BestBlock, HTLCSource, PaymentPreimage, PaymentHash};
48294829
use ln::channel::{Channel,InboundHTLCOutput,OutboundHTLCOutput,InboundHTLCState,OutboundHTLCState,HTLCOutputInCommitment,HTLCCandidate,HTLCInitiator,TxCreationKeys};
48304830
use ln::channel::MAX_FUNDING_SATOSHIS;
48314831
use ln::features::InitFeatures;
@@ -5038,8 +5038,8 @@ mod tests {
50385038
let secp_ctx = Secp256k1::new();
50395039
let seed = [42; 32];
50405040
let network = Network::Testnet;
5041-
let chain_hash = genesis_block(network).header.block_hash();
5042-
let last_block_hash = chain_hash;
5041+
let best_block = BestBlock::from_genesis(network);
5042+
let chain_hash = best_block.block_hash();
50435043
let keys_provider = test_utils::TestKeysInterface::new(&seed, network);
50445044

50455045
// Go through the flow of opening a channel between two nodes.
@@ -5065,10 +5065,10 @@ mod tests {
50655065
}]};
50665066
let funding_outpoint = OutPoint{ txid: tx.txid(), index: 0 };
50675067
let funding_created_msg = node_a_chan.get_outbound_funding_created(tx.clone(), funding_outpoint, &&logger).unwrap();
5068-
let (funding_signed_msg, _) = node_b_chan.funding_created(&funding_created_msg, last_block_hash, &&logger).unwrap();
5068+
let (funding_signed_msg, _) = node_b_chan.funding_created(&funding_created_msg, best_block, &&logger).unwrap();
50695069

50705070
// Node B --> Node A: funding signed
5071-
let _ = node_a_chan.funding_signed(&funding_signed_msg, last_block_hash, &&logger);
5071+
let _ = node_a_chan.funding_signed(&funding_signed_msg, best_block, &&logger);
50725072

50735073
// Now disconnect the two nodes and check that the commitment point in
50745074
// 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
@@ -478,6 +478,7 @@ pub struct ChainParameters {
478478
}
479479

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

25492550
fn internal_funding_created(&self, counterparty_node_id: &PublicKey, msg: &msgs::FundingCreated) -> Result<(), MsgHandleErrInternal> {
25502551
let ((funding_msg, monitor), mut chan) = {
2551-
let last_block_hash = self.best_block.read().unwrap().block_hash();
2552+
let best_block = *self.best_block.read().unwrap();
25522553
let mut channel_lock = self.channel_state.lock().unwrap();
25532554
let channel_state = &mut *channel_lock;
25542555
match channel_state.by_id.entry(msg.temporary_channel_id.clone()) {
25552556
hash_map::Entry::Occupied(mut chan) => {
25562557
if chan.get().get_counterparty_node_id() != *counterparty_node_id {
25572558
return Err(MsgHandleErrInternal::send_err_msg_no_close("Got a message for a channel from the wrong node!".to_owned(), msg.temporary_channel_id));
25582559
}
2559-
(try_chan_entry!(self, chan.get_mut().funding_created(msg, last_block_hash, &self.logger), channel_state, chan), chan.remove())
2560+
(try_chan_entry!(self, chan.get_mut().funding_created(msg, best_block, &self.logger), channel_state, chan), chan.remove())
25602561
},
25612562
hash_map::Entry::Vacant(_) => return Err(MsgHandleErrInternal::send_err_msg_no_close("Failed to find corresponding channel".to_owned(), msg.temporary_channel_id))
25622563
}
@@ -2605,15 +2606,15 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
26052606

26062607
fn internal_funding_signed(&self, counterparty_node_id: &PublicKey, msg: &msgs::FundingSigned) -> Result<(), MsgHandleErrInternal> {
26072608
let funding_tx = {
2608-
let last_block_hash = self.best_block.read().unwrap().block_hash();
2609+
let best_block = *self.best_block.read().unwrap();
26092610
let mut channel_lock = self.channel_state.lock().unwrap();
26102611
let channel_state = &mut *channel_lock;
26112612
match channel_state.by_id.entry(msg.channel_id) {
26122613
hash_map::Entry::Occupied(mut chan) => {
26132614
if chan.get().get_counterparty_node_id() != *counterparty_node_id {
26142615
return Err(MsgHandleErrInternal::send_err_msg_no_close("Got a message for a channel from the wrong node!".to_owned(), msg.channel_id));
26152616
}
2616-
let (monitor, funding_tx) = match chan.get_mut().funding_signed(&msg, last_block_hash, &self.logger) {
2617+
let (monitor, funding_tx) = match chan.get_mut().funding_signed(&msg, best_block, &self.logger) {
26172618
Ok(update) => update,
26182619
Err(e) => try_chan_entry!(self, Err(e), channel_state, chan),
26192620
};

0 commit comments

Comments
 (0)