Skip to content

Commit c39ff87

Browse files
authored
Merge pull request #3115 from alecchendev/2024-06-specific-async-sign
Refactor async signing test utils to toggle specific method availability
2 parents 2498864 + 21eeca4 commit c39ff87

File tree

7 files changed

+167
-73
lines changed

7 files changed

+167
-73
lines changed

fuzz/src/chanmon_consistency.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -396,12 +396,7 @@ impl SignerProvider for KeyProvider {
396396
let inner: InMemorySigner = ReadableArgs::read(&mut reader, self)?;
397397
let state = self.make_enforcement_state_cell(inner.commitment_seed);
398398

399-
Ok(TestChannelSigner {
400-
inner,
401-
state,
402-
disable_revocation_policy_check: false,
403-
available: Arc::new(Mutex::new(true)),
404-
})
399+
Ok(TestChannelSigner::new_with_revoked(inner, state, false))
405400
}
406401

407402
fn get_destination_script(&self, _channel_keys_id: [u8; 32]) -> Result<ScriptBuf, ()> {

lightning/src/chain/channelmonitor.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1924,9 +1924,9 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitor<Signer> {
19241924
}
19251925

19261926
#[cfg(test)]
1927-
pub fn do_signer_call<F: FnMut(&Signer) -> ()>(&self, mut f: F) {
1928-
let inner = self.inner.lock().unwrap();
1929-
f(&inner.onchain_tx_handler.signer);
1927+
pub fn do_mut_signer_call<F: FnMut(&mut Signer) -> ()>(&self, mut f: F) {
1928+
let mut inner = self.inner.lock().unwrap();
1929+
f(&mut inner.onchain_tx_handler.signer);
19301930
}
19311931
}
19321932

lightning/src/ln/async_signer_tests.rs

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use crate::events::{Event, MessageSendEvent, MessageSendEventsProvider, ClosureR
2020
use crate::ln::functional_test_utils::*;
2121
use crate::ln::msgs::ChannelMessageHandler;
2222
use crate::ln::channelmanager::{PaymentId, RecipientOnionFields};
23+
use crate::util::test_channel_signer::SignerOp;
2324

2425
#[test]
2526
fn test_async_commitment_signature_for_funding_created() {
@@ -43,7 +44,7 @@ fn test_async_commitment_signature_for_funding_created() {
4344
// But! Let's make node[0]'s signer be unavailable: we should *not* broadcast a funding_created
4445
// message...
4546
let (temporary_channel_id, tx, _) = create_funding_transaction(&nodes[0], &nodes[1].node.get_our_node_id(), 100000, 42);
46-
nodes[0].set_channel_signer_available(&nodes[1].node.get_our_node_id(), &temporary_channel_id, false);
47+
nodes[0].disable_channel_signer_op(&nodes[1].node.get_our_node_id(), &temporary_channel_id, SignerOp::SignCounterpartyCommitment);
4748
nodes[0].node.funding_transaction_generated(&temporary_channel_id, &nodes[1].node.get_our_node_id(), tx.clone()).unwrap();
4849
check_added_monitors(&nodes[0], 0);
4950

@@ -57,7 +58,7 @@ fn test_async_commitment_signature_for_funding_created() {
5758
channels[0].channel_id
5859
};
5960

60-
nodes[0].set_channel_signer_available(&nodes[1].node.get_our_node_id(), &chan_id, true);
61+
nodes[0].enable_channel_signer_op(&nodes[1].node.get_our_node_id(), &chan_id, SignerOp::SignCounterpartyCommitment);
6162
nodes[0].node.signer_unblocked(Some((nodes[1].node.get_our_node_id(), chan_id)));
6263

6364
let mut funding_created_msg = get_event_msg!(nodes[0], MessageSendEvent::SendFundingCreated, nodes[1].node.get_our_node_id());
@@ -98,7 +99,7 @@ fn test_async_commitment_signature_for_funding_signed() {
9899

99100
// Now let's make node[1]'s signer be unavailable while handling the `funding_created`. It should
100101
// *not* broadcast a `funding_signed`...
101-
nodes[1].set_channel_signer_available(&nodes[0].node.get_our_node_id(), &temporary_channel_id, false);
102+
nodes[1].disable_channel_signer_op(&nodes[0].node.get_our_node_id(), &temporary_channel_id, SignerOp::SignCounterpartyCommitment);
102103
nodes[1].node.handle_funding_created(&nodes[0].node.get_our_node_id(), &funding_created_msg);
103104
check_added_monitors(&nodes[1], 1);
104105

@@ -111,7 +112,7 @@ fn test_async_commitment_signature_for_funding_signed() {
111112
assert_eq!(channels.len(), 1, "expected one channel, not {}", channels.len());
112113
channels[0].channel_id
113114
};
114-
nodes[1].set_channel_signer_available(&nodes[0].node.get_our_node_id(), &chan_id, true);
115+
nodes[1].enable_channel_signer_op(&nodes[0].node.get_our_node_id(), &chan_id, SignerOp::SignCounterpartyCommitment);
115116
nodes[1].node.signer_unblocked(Some((nodes[0].node.get_our_node_id(), chan_id)));
116117

117118
expect_channel_pending_event(&nodes[1], &nodes[0].node.get_our_node_id());
@@ -152,14 +153,14 @@ fn test_async_commitment_signature_for_commitment_signed() {
152153

153154
// Mark dst's signer as unavailable and handle src's commitment_signed: while dst won't yet have a
154155
// `commitment_signed` of its own to offer, it should publish a `revoke_and_ack`.
155-
dst.set_channel_signer_available(&src.node.get_our_node_id(), &chan_id, false);
156+
dst.disable_channel_signer_op(&src.node.get_our_node_id(), &chan_id, SignerOp::SignCounterpartyCommitment);
156157
dst.node.handle_commitment_signed(&src.node.get_our_node_id(), &payment_event.commitment_msg);
157158
check_added_monitors(dst, 1);
158159

159160
get_event_msg!(dst, MessageSendEvent::SendRevokeAndACK, src.node.get_our_node_id());
160161

161162
// Mark dst's signer as available and retry: we now expect to see dst's `commitment_signed`.
162-
dst.set_channel_signer_available(&src.node.get_our_node_id(), &chan_id, true);
163+
dst.enable_channel_signer_op(&src.node.get_our_node_id(), &chan_id, SignerOp::SignCounterpartyCommitment);
163164
dst.node.signer_unblocked(Some((src.node.get_our_node_id(), chan_id)));
164165

165166
let events = dst.node.get_and_clear_pending_msg_events();
@@ -215,7 +216,7 @@ fn test_async_commitment_signature_for_funding_signed_0conf() {
215216

216217
// Now let's make node[1]'s signer be unavailable while handling the `funding_created`. It should
217218
// *not* broadcast a `funding_signed`...
218-
nodes[1].set_channel_signer_available(&nodes[0].node.get_our_node_id(), &temporary_channel_id, false);
219+
nodes[1].disable_channel_signer_op(&nodes[0].node.get_our_node_id(), &temporary_channel_id, SignerOp::SignCounterpartyCommitment);
219220
nodes[1].node.handle_funding_created(&nodes[0].node.get_our_node_id(), &funding_created_msg);
220221
check_added_monitors(&nodes[1], 1);
221222

@@ -230,7 +231,7 @@ fn test_async_commitment_signature_for_funding_signed_0conf() {
230231
};
231232

232233
// At this point, we basically expect the channel to open like a normal zero-conf channel.
233-
nodes[1].set_channel_signer_available(&nodes[0].node.get_our_node_id(), &chan_id, true);
234+
nodes[1].enable_channel_signer_op(&nodes[0].node.get_our_node_id(), &chan_id, SignerOp::SignCounterpartyCommitment);
234235
nodes[1].node.signer_unblocked(Some((nodes[0].node.get_our_node_id(), chan_id)));
235236

236237
let (funding_signed, channel_ready_1) = {
@@ -299,7 +300,7 @@ fn test_async_commitment_signature_for_peer_disconnect() {
299300

300301
// Mark dst's signer as unavailable and handle src's commitment_signed: while dst won't yet have a
301302
// `commitment_signed` of its own to offer, it should publish a `revoke_and_ack`.
302-
dst.set_channel_signer_available(&src.node.get_our_node_id(), &chan_id, false);
303+
dst.disable_channel_signer_op(&src.node.get_our_node_id(), &chan_id, SignerOp::SignCounterpartyCommitment);
303304
dst.node.handle_commitment_signed(&src.node.get_our_node_id(), &payment_event.commitment_msg);
304305
check_added_monitors(dst, 1);
305306

@@ -314,7 +315,7 @@ fn test_async_commitment_signature_for_peer_disconnect() {
314315
reconnect_nodes(reconnect_args);
315316

316317
// Mark dst's signer as available and retry: we now expect to see dst's `commitment_signed`.
317-
dst.set_channel_signer_available(&src.node.get_our_node_id(), &chan_id, true);
318+
dst.enable_channel_signer_op(&src.node.get_our_node_id(), &chan_id, SignerOp::SignCounterpartyCommitment);
318319
dst.node.signer_unblocked(Some((src.node.get_our_node_id(), chan_id)));
319320

320321
{
@@ -366,7 +367,6 @@ fn do_test_async_holder_signatures(anchors: bool, remote_commitment: bool) {
366367
route_payment(&nodes[0], &[&nodes[1]], 1_000_000);
367368
let error_message = "Channel force-closed";
368369

369-
nodes[0].set_channel_signer_available(&nodes[1].node.get_our_node_id(), &chan_id, false);
370370

371371
if remote_commitment {
372372
// Make the counterparty broadcast its latest commitment.
@@ -375,6 +375,8 @@ fn do_test_async_holder_signatures(anchors: bool, remote_commitment: bool) {
375375
check_closed_broadcast(&nodes[1], 1, true);
376376
check_closed_event(&nodes[1], 1, ClosureReason::HolderForceClosed { broadcasted_latest_txn: Some(true) }, false, &[nodes[0].node.get_our_node_id()], 100_000);
377377
} else {
378+
nodes[0].disable_channel_signer_op(&nodes[1].node.get_our_node_id(), &chan_id, SignerOp::SignHolderCommitment);
379+
nodes[0].disable_channel_signer_op(&nodes[1].node.get_our_node_id(), &chan_id, SignerOp::SignHolderHtlcTransaction);
378380
// We'll connect blocks until the sender has to go onchain to time out the HTLC.
379381
connect_blocks(&nodes[0], TEST_FINAL_CLTV + LATENCY_GRACE_PERIOD_BLOCKS + 1);
380382

@@ -383,7 +385,8 @@ fn do_test_async_holder_signatures(anchors: bool, remote_commitment: bool) {
383385
assert!(nodes[0].chain_monitor.chain_monitor.get_and_clear_pending_events().is_empty());
384386

385387
// Mark it as available now, we should see the signed commitment transaction.
386-
nodes[0].set_channel_signer_available(&nodes[1].node.get_our_node_id(), &chan_id, true);
388+
nodes[0].enable_channel_signer_op(&nodes[1].node.get_our_node_id(), &chan_id, SignerOp::SignHolderCommitment);
389+
nodes[0].enable_channel_signer_op(&nodes[1].node.get_our_node_id(), &chan_id, SignerOp::SignHolderHtlcTransaction);
387390
get_monitor!(nodes[0], chan_id).signer_unblocked(nodes[0].tx_broadcaster, nodes[0].fee_estimator, &nodes[0].logger);
388391
}
389392

@@ -409,7 +412,13 @@ fn do_test_async_holder_signatures(anchors: bool, remote_commitment: bool) {
409412

410413
// Mark it as unavailable again to now test the HTLC transaction. We'll mine the commitment such
411414
// that the HTLC transaction is retried.
412-
nodes[0].set_channel_signer_available(&nodes[1].node.get_our_node_id(), &chan_id, false);
415+
let sign_htlc_op = if remote_commitment {
416+
SignerOp::SignCounterpartyHtlcTransaction
417+
} else {
418+
SignerOp::SignHolderHtlcTransaction
419+
};
420+
nodes[0].disable_channel_signer_op(&nodes[1].node.get_our_node_id(), &chan_id, SignerOp::SignHolderCommitment);
421+
nodes[0].disable_channel_signer_op(&nodes[1].node.get_our_node_id(), &chan_id, sign_htlc_op);
413422
mine_transaction(&nodes[0], &commitment_tx);
414423

415424
check_added_monitors(&nodes[0], 1);
@@ -426,10 +435,12 @@ fn do_test_async_holder_signatures(anchors: bool, remote_commitment: bool) {
426435
if anchors && !remote_commitment {
427436
handle_bump_htlc_event(&nodes[0], 1);
428437
}
429-
assert!(nodes[0].tx_broadcaster.txn_broadcast().is_empty());
438+
let txn = nodes[0].tx_broadcaster.txn_broadcast();
439+
assert!(txn.is_empty(), "expected no transaction to be broadcast, got {:?}", txn);
430440

431441
// Mark it as available now, we should see the signed HTLC transaction.
432-
nodes[0].set_channel_signer_available(&nodes[1].node.get_our_node_id(), &chan_id, true);
442+
nodes[0].enable_channel_signer_op(&nodes[1].node.get_our_node_id(), &chan_id, SignerOp::SignHolderCommitment);
443+
nodes[0].enable_channel_signer_op(&nodes[1].node.get_our_node_id(), &chan_id, sign_htlc_op);
433444
get_monitor!(nodes[0], chan_id).signer_unblocked(nodes[0].tx_broadcaster, nodes[0].fee_estimator, &nodes[0].logger);
434445

435446
if anchors && !remote_commitment {
@@ -443,9 +454,21 @@ fn do_test_async_holder_signatures(anchors: bool, remote_commitment: bool) {
443454
}
444455

445456
#[test]
446-
fn test_async_holder_signatures() {
457+
fn test_async_holder_signatures_no_anchors() {
447458
do_test_async_holder_signatures(false, false);
459+
}
460+
461+
#[test]
462+
fn test_async_holder_signatures_remote_commitment_no_anchors() {
448463
do_test_async_holder_signatures(false, true);
464+
}
465+
466+
#[test]
467+
fn test_async_holder_signatures_anchors() {
449468
do_test_async_holder_signatures(true, false);
469+
}
470+
471+
#[test]
472+
fn test_async_holder_signatures_remote_commitment_anchors() {
450473
do_test_async_holder_signatures(true, true);
451474
}

lightning/src/ln/channel.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2118,8 +2118,8 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
21182118

21192119
/// Returns the holder signer for this channel.
21202120
#[cfg(test)]
2121-
pub fn get_signer(&self) -> &ChannelSignerType<SP> {
2122-
return &self.holder_signer
2121+
pub fn get_mut_signer(&mut self) -> &mut ChannelSignerType<SP> {
2122+
return &mut self.holder_signer
21232123
}
21242124

21252125
/// Only allowed immediately after deserialization if get_outbound_scid_alias returns 0,

lightning/src/ln/functional_test_utils.rs

Lines changed: 52 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ use crate::util::errors::APIError;
3131
use crate::util::logger::Logger;
3232
use crate::util::scid_utils;
3333
use crate::util::test_channel_signer::TestChannelSigner;
34+
#[cfg(test)]
35+
use crate::util::test_channel_signer::SignerOp;
3436
use crate::util::test_utils;
3537
use crate::util::test_utils::{panicking, TestChainMonitor, TestScorer, TestKeysInterface};
3638
use crate::util::ser::{ReadableArgs, Writeable};
@@ -482,46 +484,74 @@ impl<'a, 'b, 'c> Node<'a, 'b, 'c> {
482484
pub fn get_block_header(&self, height: u32) -> Header {
483485
self.blocks.lock().unwrap()[height as usize].0.header
484486
}
485-
/// Changes the channel signer's availability for the specified peer and channel.
487+
488+
/// Toggles this node's signer to be available for the given signer operation.
489+
/// This is useful for testing behavior for restoring an async signer that previously
490+
/// could not return a signature immediately.
491+
#[cfg(test)]
492+
pub fn enable_channel_signer_op(&self, peer_id: &PublicKey, chan_id: &ChannelId, signer_op: SignerOp) {
493+
self.set_channel_signer_ops(peer_id, chan_id, signer_op, true);
494+
}
495+
496+
/// Toggles this node's signer to be unavailable, returning `Err` for the given signer operation.
497+
/// This is useful for testing behavior for an async signer that cannot return a signature
498+
/// immediately.
499+
#[cfg(test)]
500+
pub fn disable_channel_signer_op(&self, peer_id: &PublicKey, chan_id: &ChannelId, signer_op: SignerOp) {
501+
self.set_channel_signer_ops(peer_id, chan_id, signer_op, false);
502+
}
503+
504+
/// Changes the channel signer's availability for the specified peer, channel, and signer
505+
/// operation.
486506
///
487-
/// When `available` is set to `true`, the channel signer will behave normally. When set to
488-
/// `false`, the channel signer will act like an off-line remote signer and will return `Err` for
489-
/// several of the signing methods. Currently, only `get_per_commitment_point` and
490-
/// `release_commitment_secret` are affected by this setting.
507+
/// For the specified signer operation, when `available` is set to `true`, the channel signer
508+
/// will behave normally, returning `Ok`. When set to `false`, and the channel signer will
509+
/// act like an off-line remote signer, returning `Err`. This applies to the signer in all
510+
/// relevant places, i.e. the channel manager, chain monitor, and the keys manager.
491511
#[cfg(test)]
492-
pub fn set_channel_signer_available(&self, peer_id: &PublicKey, chan_id: &ChannelId, available: bool) {
512+
fn set_channel_signer_ops(&self, peer_id: &PublicKey, chan_id: &ChannelId, signer_op: SignerOp, available: bool) {
493513
use crate::sign::ChannelSigner;
494514
log_debug!(self.logger, "Setting channel signer for {} as available={}", chan_id, available);
495515

496516
let per_peer_state = self.node.per_peer_state.read().unwrap();
497-
let chan_lock = per_peer_state.get(peer_id).unwrap().lock().unwrap();
517+
let mut chan_lock = per_peer_state.get(peer_id).unwrap().lock().unwrap();
498518

499519
let mut channel_keys_id = None;
500-
if let Some(chan) = chan_lock.channel_by_id.get(chan_id).map(|phase| phase.context()) {
501-
chan.get_signer().as_ecdsa().unwrap().set_available(available);
520+
if let Some(chan) = chan_lock.channel_by_id.get_mut(chan_id).map(|phase| phase.context_mut()) {
521+
let signer = chan.get_mut_signer().as_mut_ecdsa().unwrap();
522+
if available {
523+
signer.enable_op(signer_op);
524+
} else {
525+
signer.disable_op(signer_op);
526+
}
502527
channel_keys_id = Some(chan.channel_keys_id);
503528
}
504529

505-
let mut monitor = None;
506-
for (funding_txo, channel_id) in self.chain_monitor.chain_monitor.list_monitors() {
507-
if *chan_id == channel_id {
508-
monitor = self.chain_monitor.chain_monitor.get_monitor(funding_txo).ok();
509-
}
510-
}
530+
let monitor = self.chain_monitor.chain_monitor.list_monitors().into_iter()
531+
.find(|(_, channel_id)| *channel_id == *chan_id)
532+
.and_then(|(funding_txo, _)| self.chain_monitor.chain_monitor.get_monitor(funding_txo).ok());
511533
if let Some(monitor) = monitor {
512-
monitor.do_signer_call(|signer| {
534+
monitor.do_mut_signer_call(|signer| {
513535
channel_keys_id = channel_keys_id.or(Some(signer.inner.channel_keys_id()));
514-
signer.set_available(available)
536+
if available {
537+
signer.enable_op(signer_op);
538+
} else {
539+
signer.disable_op(signer_op);
540+
}
515541
});
516542
}
517543

544+
let channel_keys_id = channel_keys_id.unwrap();
545+
let mut unavailable_signers_ops = self.keys_manager.unavailable_signers_ops.lock().unwrap();
546+
let entry = unavailable_signers_ops.entry(channel_keys_id).or_insert(new_hash_set());
518547
if available {
519-
self.keys_manager.unavailable_signers.lock().unwrap()
520-
.remove(channel_keys_id.as_ref().unwrap());
548+
entry.remove(&signer_op);
549+
if entry.is_empty() {
550+
unavailable_signers_ops.remove(&channel_keys_id);
551+
}
521552
} else {
522-
self.keys_manager.unavailable_signers.lock().unwrap()
523-
.insert(channel_keys_id.unwrap());
524-
}
553+
entry.insert(signer_op);
554+
};
525555
}
526556
}
527557

0 commit comments

Comments
 (0)