Skip to content

Commit 457e48e

Browse files
authored
Merge pull request #1179 from TheBlueMatt/2021-11-fix-announce-sigs-broadcast-time
Disconnect announcement_signatures sending from funding_locked
2 parents dfc93b4 + ed1163a commit 457e48e

11 files changed

+543
-203
lines changed

fuzz/src/chanmon_consistency.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ impl KeysInterface for KeyProvider {
188188
let id = self.rand_bytes_id.fetch_add(1, atomic::Ordering::Relaxed);
189189
let keys = InMemorySigner::new(
190190
&secp_ctx,
191+
self.get_node_secret(),
191192
SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, self.node_id]).unwrap(),
192193
SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, self.node_id]).unwrap(),
193194
SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, self.node_id]).unwrap(),
@@ -211,7 +212,7 @@ impl KeysInterface for KeyProvider {
211212
fn read_chan_signer(&self, buffer: &[u8]) -> Result<Self::Signer, DecodeError> {
212213
let mut reader = std::io::Cursor::new(buffer);
213214

214-
let inner: InMemorySigner = Readable::read(&mut reader)?;
215+
let inner: InMemorySigner = ReadableArgs::read(&mut reader, self.get_node_secret())?;
215216
let state = self.make_enforcement_state_cell(inner.commitment_seed);
216217

217218
Ok(EnforcingSigner {

fuzz/src/full_stack.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ use lightning::util::errors::APIError;
4545
use lightning::util::events::Event;
4646
use lightning::util::enforcing_trait_impls::{EnforcingSigner, EnforcementState};
4747
use lightning::util::logger::Logger;
48-
use lightning::util::ser::Readable;
48+
use lightning::util::ser::ReadableArgs;
4949

5050
use utils::test_logger;
5151
use utils::test_persister::TestPersister;
@@ -293,6 +293,7 @@ impl KeysInterface for KeyProvider {
293293
EnforcingSigner::new(if inbound {
294294
InMemorySigner::new(
295295
&secp_ctx,
296+
self.node_secret.clone(),
296297
SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, ctr]).unwrap(),
297298
SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, ctr]).unwrap(),
298299
SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, ctr]).unwrap(),
@@ -305,6 +306,7 @@ impl KeysInterface for KeyProvider {
305306
} else {
306307
InMemorySigner::new(
307308
&secp_ctx,
309+
self.node_secret.clone(),
308310
SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, ctr]).unwrap(),
309311
SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, ctr]).unwrap(),
310312
SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, ctr]).unwrap(),
@@ -324,7 +326,7 @@ impl KeysInterface for KeyProvider {
324326
}
325327

326328
fn read_chan_signer(&self, mut data: &[u8]) -> Result<EnforcingSigner, DecodeError> {
327-
let inner: InMemorySigner = Readable::read(&mut data)?;
329+
let inner: InMemorySigner = ReadableArgs::read(&mut data, self.node_secret.clone())?;
328330
let state = Arc::new(Mutex::new(EnforcementState::new()));
329331

330332
Ok(EnforcingSigner::new_with_revoked(

lightning/src/chain/channelmonitor.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3479,6 +3479,7 @@ mod tests {
34793479
SecretKey::from_slice(&[41; 32]).unwrap(),
34803480
SecretKey::from_slice(&[41; 32]).unwrap(),
34813481
SecretKey::from_slice(&[41; 32]).unwrap(),
3482+
SecretKey::from_slice(&[41; 32]).unwrap(),
34823483
[41; 32],
34833484
0,
34843485
[0; 32]

lightning/src/chain/keysinterface.rs

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ use bitcoin::secp256k1::recovery::RecoverableSignature;
3131
use bitcoin::secp256k1;
3232

3333
use util::{byte_utils, transaction_utils};
34-
use util::ser::{Writeable, Writer, Readable};
34+
use util::ser::{Writeable, Writer, Readable, ReadableArgs};
3535

3636
use chain::transaction::OutPoint;
3737
use ln::{chan_utils, PaymentPreimage};
@@ -346,13 +346,17 @@ pub trait BaseSign {
346346
/// chosen to forgo their output as dust.
347347
fn sign_closing_transaction(&self, closing_tx: &ClosingTransaction, secp_ctx: &Secp256k1<secp256k1::All>) -> Result<Signature, ()>;
348348

349-
/// Signs a channel announcement message with our funding key, proving it comes from one
350-
/// of the channel participants.
349+
/// Signs a channel announcement message with our funding key and our node secret key (aka
350+
/// node_id or network_key), proving it comes from one of the channel participants.
351+
///
352+
/// The first returned signature should be from our node secret key, the second from our
353+
/// funding key.
351354
///
352355
/// Note that if this fails or is rejected, the channel will not be publicly announced and
353356
/// our counterparty may (though likely will not) close the channel on us for violating the
354357
/// protocol.
355-
fn sign_channel_announcement(&self, msg: &UnsignedChannelAnnouncement, secp_ctx: &Secp256k1<secp256k1::All>) -> Result<Signature, ()>;
358+
fn sign_channel_announcement(&self, msg: &UnsignedChannelAnnouncement, secp_ctx: &Secp256k1<secp256k1::All>)
359+
-> Result<(Signature, Signature), ()>;
356360

357361
/// Set the counterparty static channel data, including basepoints,
358362
/// counterparty_selected/holder_selected_contest_delay and funding outpoint.
@@ -447,6 +451,8 @@ pub struct InMemorySigner {
447451
pub commitment_seed: [u8; 32],
448452
/// Holder public keys and basepoints
449453
pub(crate) holder_channel_pubkeys: ChannelPublicKeys,
454+
/// Private key of our node secret, used for signing channel announcements
455+
node_secret: SecretKey,
450456
/// Counterparty public keys and counterparty/holder selected_contest_delay, populated on channel acceptance
451457
channel_parameters: Option<ChannelTransactionParameters>,
452458
/// The total value of this channel
@@ -459,6 +465,7 @@ impl InMemorySigner {
459465
/// Create a new InMemorySigner
460466
pub fn new<C: Signing>(
461467
secp_ctx: &Secp256k1<C>,
468+
node_secret: SecretKey,
462469
funding_key: SecretKey,
463470
revocation_base_key: SecretKey,
464471
payment_key: SecretKey,
@@ -478,6 +485,7 @@ impl InMemorySigner {
478485
delayed_payment_base_key,
479486
htlc_base_key,
480487
commitment_seed,
488+
node_secret,
481489
channel_value_satoshis,
482490
holder_channel_pubkeys,
483491
channel_parameters: None,
@@ -720,9 +728,10 @@ impl BaseSign for InMemorySigner {
720728
Ok(closing_tx.trust().sign(&self.funding_key, &channel_funding_redeemscript, self.channel_value_satoshis, secp_ctx))
721729
}
722730

723-
fn sign_channel_announcement(&self, msg: &UnsignedChannelAnnouncement, secp_ctx: &Secp256k1<secp256k1::All>) -> Result<Signature, ()> {
731+
fn sign_channel_announcement(&self, msg: &UnsignedChannelAnnouncement, secp_ctx: &Secp256k1<secp256k1::All>)
732+
-> Result<(Signature, Signature), ()> {
724733
let msghash = hash_to_message!(&Sha256dHash::hash(&msg.encode()[..])[..]);
725-
Ok(secp_ctx.sign(&msghash, &self.funding_key))
734+
Ok((secp_ctx.sign(&msghash, &self.node_secret), secp_ctx.sign(&msghash, &self.funding_key)))
726735
}
727736

728737
fn ready_channel(&mut self, channel_parameters: &ChannelTransactionParameters) {
@@ -757,8 +766,8 @@ impl Writeable for InMemorySigner {
757766
}
758767
}
759768

760-
impl Readable for InMemorySigner {
761-
fn read<R: io::Read>(reader: &mut R) -> Result<Self, DecodeError> {
769+
impl ReadableArgs<SecretKey> for InMemorySigner {
770+
fn read<R: io::Read>(reader: &mut R, node_secret: SecretKey) -> Result<Self, DecodeError> {
762771
let _ver = read_ver_prefix!(reader, SERIALIZATION_VERSION);
763772

764773
let funding_key = Readable::read(reader)?;
@@ -784,6 +793,7 @@ impl Readable for InMemorySigner {
784793
payment_key,
785794
delayed_payment_base_key,
786795
htlc_base_key,
796+
node_secret,
787797
commitment_seed,
788798
channel_value_satoshis,
789799
holder_channel_pubkeys,
@@ -937,6 +947,7 @@ impl KeysManager {
937947

938948
InMemorySigner::new(
939949
&self.secp_ctx,
950+
self.node_secret,
940951
funding_key,
941952
revocation_base_key,
942953
payment_key,
@@ -1119,7 +1130,7 @@ impl KeysInterface for KeysManager {
11191130
}
11201131

11211132
fn read_chan_signer(&self, reader: &[u8]) -> Result<Self::Signer, DecodeError> {
1122-
InMemorySigner::read(&mut io::Cursor::new(reader))
1133+
InMemorySigner::read(&mut io::Cursor::new(reader), self.get_node_secret())
11231134
}
11241135

11251136
fn sign_invoice(&self, hrp_bytes: &[u8], invoice_data: &[u5]) -> Result<RecoverableSignature, ()> {

lightning/src/ln/chanmon_update_fail_tests.rs

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use chain::channelmonitor::ChannelMonitor;
2020
use chain::transaction::OutPoint;
2121
use chain::{ChannelMonitorUpdateErr, Listen, Watch};
2222
use ln::channelmanager::{ChannelManager, ChannelManagerReadArgs, RAACommitmentOrder, PaymentSendFailure};
23+
use ln::channel::AnnouncementSigsState;
2324
use ln::features::InitFeatures;
2425
use ln::msgs;
2526
use ln::msgs::{ChannelMessageHandler, RoutingMessageHandler};
@@ -1402,6 +1403,11 @@ fn monitor_failed_no_reestablish_response() {
14021403
let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
14031404
let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
14041405
let channel_id = create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known()).2;
1406+
{
1407+
let mut lock;
1408+
get_channel_ref!(nodes[0], lock, channel_id).announcement_sigs_state = AnnouncementSigsState::PeerReceived;
1409+
get_channel_ref!(nodes[1], lock, channel_id).announcement_sigs_state = AnnouncementSigsState::PeerReceived;
1410+
}
14051411

14061412
// Route the payment and deliver the initial commitment_signed (with a monitor update failure
14071413
// on receipt).
@@ -1789,9 +1795,9 @@ fn monitor_update_claim_fail_no_response() {
17891795
claim_payment(&nodes[0], &[&nodes[1]], payment_preimage_2);
17901796
}
17911797

1792-
// confirm_a_first and restore_b_before_conf are wholly unrelated to earlier bools and
17931798
// restore_b_before_conf has no meaning if !confirm_a_first
1794-
fn do_during_funding_monitor_fail(confirm_a_first: bool, restore_b_before_conf: bool) {
1799+
// restore_b_before_lock has no meaning if confirm_a_first
1800+
fn do_during_funding_monitor_fail(confirm_a_first: bool, restore_b_before_conf: bool, restore_b_before_lock: bool) {
17951801
// Test that if the monitor update generated by funding_transaction_generated fails we continue
17961802
// the channel setup happily after the update is restored.
17971803
let chanmon_cfgs = create_chanmon_cfgs(2);
@@ -1833,6 +1839,8 @@ fn do_during_funding_monitor_fail(confirm_a_first: bool, restore_b_before_conf:
18331839
if confirm_a_first {
18341840
confirm_transaction(&nodes[0], &funding_tx);
18351841
nodes[1].node.handle_funding_locked(&nodes[0].node.get_our_node_id(), &get_event_msg!(nodes[0], MessageSendEvent::SendFundingLocked, nodes[1].node.get_our_node_id()));
1842+
assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
1843+
assert!(nodes[1].node.get_and_clear_pending_events().is_empty());
18361844
} else {
18371845
assert!(!restore_b_before_conf);
18381846
confirm_transaction(&nodes[1], &funding_tx);
@@ -1851,20 +1859,32 @@ fn do_during_funding_monitor_fail(confirm_a_first: bool, restore_b_before_conf:
18511859
assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
18521860
assert!(nodes[1].node.get_and_clear_pending_events().is_empty());
18531861
}
1862+
if !confirm_a_first && !restore_b_before_lock {
1863+
confirm_transaction(&nodes[0], &funding_tx);
1864+
nodes[1].node.handle_funding_locked(&nodes[0].node.get_our_node_id(), &get_event_msg!(nodes[0], MessageSendEvent::SendFundingLocked, nodes[1].node.get_our_node_id()));
1865+
assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
1866+
assert!(nodes[1].node.get_and_clear_pending_events().is_empty());
1867+
}
18541868

18551869
chanmon_cfgs[1].persister.set_update_ret(Ok(()));
18561870
let (outpoint, latest_update, _) = nodes[1].chain_monitor.latest_monitor_update_id.lock().unwrap().get(&channel_id).unwrap().clone();
18571871
nodes[1].chain_monitor.chain_monitor.force_channel_monitor_updated(outpoint, latest_update);
18581872
check_added_monitors!(nodes[1], 0);
18591873

18601874
let (channel_id, (announcement, as_update, bs_update)) = if !confirm_a_first {
1861-
nodes[0].node.handle_funding_locked(&nodes[1].node.get_our_node_id(), &get_event_msg!(nodes[1], MessageSendEvent::SendFundingLocked, nodes[0].node.get_our_node_id()));
1862-
1863-
confirm_transaction(&nodes[0], &funding_tx);
1864-
let (funding_locked, channel_id) = create_chan_between_nodes_with_value_confirm_second(&nodes[1], &nodes[0]);
1865-
(channel_id, create_chan_between_nodes_with_value_b(&nodes[0], &nodes[1], &funding_locked))
1875+
if !restore_b_before_lock {
1876+
let (funding_locked, channel_id) = create_chan_between_nodes_with_value_confirm_second(&nodes[0], &nodes[1]);
1877+
(channel_id, create_chan_between_nodes_with_value_b(&nodes[1], &nodes[0], &funding_locked))
1878+
} else {
1879+
nodes[0].node.handle_funding_locked(&nodes[1].node.get_our_node_id(), &get_event_msg!(nodes[1], MessageSendEvent::SendFundingLocked, nodes[0].node.get_our_node_id()));
1880+
confirm_transaction(&nodes[0], &funding_tx);
1881+
let (funding_locked, channel_id) = create_chan_between_nodes_with_value_confirm_second(&nodes[1], &nodes[0]);
1882+
(channel_id, create_chan_between_nodes_with_value_b(&nodes[0], &nodes[1], &funding_locked))
1883+
}
18661884
} else {
18671885
if restore_b_before_conf {
1886+
assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
1887+
assert!(nodes[1].node.get_and_clear_pending_events().is_empty());
18681888
confirm_transaction(&nodes[1], &funding_tx);
18691889
}
18701890
let (funding_locked, channel_id) = create_chan_between_nodes_with_value_confirm_second(&nodes[0], &nodes[1]);
@@ -1884,9 +1904,10 @@ fn do_during_funding_monitor_fail(confirm_a_first: bool, restore_b_before_conf:
18841904

18851905
#[test]
18861906
fn during_funding_monitor_fail() {
1887-
do_during_funding_monitor_fail(true, true);
1888-
do_during_funding_monitor_fail(true, false);
1889-
do_during_funding_monitor_fail(false, false);
1907+
do_during_funding_monitor_fail(true, true, false);
1908+
do_during_funding_monitor_fail(true, false, false);
1909+
do_during_funding_monitor_fail(false, false, false);
1910+
do_during_funding_monitor_fail(false, false, true);
18901911
}
18911912

18921913
#[test]

0 commit comments

Comments
 (0)