Skip to content

Commit 31093ad

Browse files
committed
Pass along ChannelManager's last_block_hash
ChannelMonitor keeps track of the last block connected. However, it is initialized with the default block hash, which is a problem if the ChannelMonitor is serialized before a block is connected. Instead, pass ChannelManager's last_block_hash, which is initialized with a "birthday" hash, when creating a new ChannelMonitor.
1 parent caabc4e commit 31093ad

File tree

3 files changed

+20
-12
lines changed

3 files changed

+20
-12
lines changed

lightning/src/chain/channelmonitor.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -980,7 +980,8 @@ impl<Signer: Sign> ChannelMonitor<Signer> {
980980
channel_parameters: &ChannelTransactionParameters,
981981
funding_redeemscript: Script, channel_value_satoshis: u64,
982982
commitment_transaction_number_obscure_factor: u64,
983-
initial_holder_commitment_tx: HolderCommitmentTransaction) -> ChannelMonitor<Signer> {
983+
initial_holder_commitment_tx: HolderCommitmentTransaction,
984+
last_block_hash: BlockHash) -> ChannelMonitor<Signer> {
984985

985986
assert!(commitment_transaction_number_obscure_factor <= (1 << 48));
986987
let our_channel_close_key_hash = WPubkeyHash::hash(&shutdown_pubkey.serialize());
@@ -1067,7 +1068,7 @@ impl<Signer: Sign> ChannelMonitor<Signer> {
10671068
lockdown_from_offchain: false,
10681069
holder_tx_signed: false,
10691070

1070-
last_block_hash: Default::default(),
1071+
last_block_hash,
10711072
secp_ctx,
10721073
}),
10731074
}
@@ -2789,6 +2790,7 @@ impl<'a, Signer: Sign, K: KeysInterface<Signer = Signer>> ReadableArgs<&'a K>
27892790

27902791
#[cfg(test)]
27912792
mod tests {
2793+
use bitcoin::blockdata::constants::genesis_block;
27922794
use bitcoin::blockdata::script::{Script, Builder};
27932795
use bitcoin::blockdata::opcodes;
27942796
use bitcoin::blockdata::transaction::{Transaction, TxIn, TxOut, SigHashType};
@@ -2798,6 +2800,7 @@ mod tests {
27982800
use bitcoin::hashes::sha256::Hash as Sha256;
27992801
use bitcoin::hashes::hex::FromHex;
28002802
use bitcoin::hash_types::Txid;
2803+
use bitcoin::network::constants::Network;
28012804
use hex;
28022805
use chain::channelmonitor::ChannelMonitor;
28032806
use chain::transaction::OutPoint;
@@ -2897,12 +2900,13 @@ mod tests {
28972900
};
28982901
// Prune with one old state and a holder commitment tx holding a few overlaps with the
28992902
// old state.
2903+
let last_block_hash = genesis_block(Network::Testnet).block_hash();
29002904
let monitor = ChannelMonitor::new(Secp256k1::new(), keys,
29012905
&PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[42; 32]).unwrap()), 0, &Script::new(),
29022906
(OutPoint { txid: Txid::from_slice(&[43; 32]).unwrap(), index: 0 }, Script::new()),
29032907
&channel_parameters,
29042908
Script::new(), 46, 0,
2905-
HolderCommitmentTransaction::dummy());
2909+
HolderCommitmentTransaction::dummy(), last_block_hash);
29062910

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

lightning/src/ln/channel.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1516,7 +1516,7 @@ impl<Signer: Sign> Channel<Signer> {
15161516
&self.get_counterparty_pubkeys().funding_pubkey
15171517
}
15181518

1519-
pub fn funding_created<L: Deref>(&mut self, msg: &msgs::FundingCreated, logger: &L) -> Result<(msgs::FundingSigned, ChannelMonitor<Signer>), ChannelError> where L::Target: Logger {
1519+
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 {
15201520
if self.is_outbound() {
15211521
return Err(ChannelError::Close("Received funding_created for an outbound channel?".to_owned()));
15221522
}
@@ -1570,7 +1570,7 @@ impl<Signer: Sign> Channel<Signer> {
15701570
&self.channel_transaction_parameters,
15711571
funding_redeemscript.clone(), self.channel_value_satoshis,
15721572
obscure_factor,
1573-
holder_commitment_tx);
1573+
holder_commitment_tx, last_block_hash);
15741574

15751575
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);
15761576

@@ -1587,7 +1587,7 @@ impl<Signer: Sign> Channel<Signer> {
15871587

15881588
/// Handles a funding_signed message from the remote end.
15891589
/// If this call is successful, broadcast the funding transaction (and not before!)
1590-
pub fn funding_signed<L: Deref>(&mut self, msg: &msgs::FundingSigned, logger: &L) -> Result<ChannelMonitor<Signer>, ChannelError> where L::Target: Logger {
1590+
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 {
15911591
if !self.is_outbound() {
15921592
return Err(ChannelError::Close("Received funding_signed for an inbound channel?".to_owned()));
15931593
}
@@ -1640,7 +1640,7 @@ impl<Signer: Sign> Channel<Signer> {
16401640
&self.channel_transaction_parameters,
16411641
funding_redeemscript.clone(), self.channel_value_satoshis,
16421642
obscure_factor,
1643-
holder_commitment_tx);
1643+
holder_commitment_tx, last_block_hash);
16441644

16451645
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);
16461646

@@ -4910,6 +4910,8 @@ mod tests {
49104910
let secp_ctx = Secp256k1::new();
49114911
let seed = [42; 32];
49124912
let network = Network::Testnet;
4913+
let chain_hash = genesis_block(network).header.block_hash();
4914+
let last_block_hash = chain_hash;
49134915
let keys_provider = test_utils::TestKeysInterface::new(&seed, network);
49144916

49154917
// Go through the flow of opening a channel between two nodes.
@@ -4920,7 +4922,7 @@ mod tests {
49204922
let mut node_a_chan = Channel::<EnforcingSigner>::new_outbound(&&feeest, &&keys_provider, node_b_node_id, 10000000, 100000, 42, &config).unwrap();
49214923

49224924
// Create Node B's channel by receiving Node A's open_channel message
4923-
let open_channel_msg = node_a_chan.get_open_channel(genesis_block(network).header.block_hash());
4925+
let open_channel_msg = node_a_chan.get_open_channel(chain_hash);
49244926
let node_b_node_id = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[7; 32]).unwrap());
49254927
let mut node_b_chan = Channel::<EnforcingSigner>::new_from_req(&&feeest, &&keys_provider, node_b_node_id, InitFeatures::known(), &open_channel_msg, 7, &config).unwrap();
49264928

@@ -4935,10 +4937,10 @@ mod tests {
49354937
}]};
49364938
let funding_outpoint = OutPoint{ txid: tx.txid(), index: 0 };
49374939
let funding_created_msg = node_a_chan.get_outbound_funding_created(funding_outpoint, &&logger).unwrap();
4938-
let (funding_signed_msg, _) = node_b_chan.funding_created(&funding_created_msg, &&logger).unwrap();
4940+
let (funding_signed_msg, _) = node_b_chan.funding_created(&funding_created_msg, last_block_hash, &&logger).unwrap();
49394941

49404942
// Node B --> Node A: funding signed
4941-
let _ = node_a_chan.funding_signed(&funding_signed_msg, &&logger);
4943+
let _ = node_a_chan.funding_signed(&funding_signed_msg, last_block_hash, &&logger);
49424944

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

lightning/src/ln/channelmanager.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2461,7 +2461,8 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
24612461
if chan.get().get_counterparty_node_id() != *counterparty_node_id {
24622462
return Err(MsgHandleErrInternal::send_err_msg_no_close("Got a message for a channel from the wrong node!".to_owned(), msg.temporary_channel_id));
24632463
}
2464-
(try_chan_entry!(self, chan.get_mut().funding_created(msg, &self.logger), channel_state, chan), chan.remove())
2464+
let last_block_hash = *self.last_block_hash.lock().unwrap();
2465+
(try_chan_entry!(self, chan.get_mut().funding_created(msg, last_block_hash, &self.logger), channel_state, chan), chan.remove())
24652466
},
24662467
hash_map::Entry::Vacant(_) => return Err(MsgHandleErrInternal::send_err_msg_no_close("Failed to find corresponding channel".to_owned(), msg.temporary_channel_id))
24672468
}
@@ -2517,7 +2518,8 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
25172518
if chan.get().get_counterparty_node_id() != *counterparty_node_id {
25182519
return Err(MsgHandleErrInternal::send_err_msg_no_close("Got a message for a channel from the wrong node!".to_owned(), msg.channel_id));
25192520
}
2520-
let monitor = match chan.get_mut().funding_signed(&msg, &self.logger) {
2521+
let last_block_hash = *self.last_block_hash.lock().unwrap();
2522+
let monitor = match chan.get_mut().funding_signed(&msg, last_block_hash, &self.logger) {
25212523
Ok(update) => update,
25222524
Err(e) => try_chan_entry!(self, Err(e), channel_state, chan),
25232525
};

0 commit comments

Comments
 (0)