1111//! properly with a signer implementation that asynchronously derives signatures.
1212
1313use crate :: prelude:: * ;
14+ use crate :: util:: ser:: Writeable ;
1415use bitcoin:: secp256k1:: Secp256k1 ;
1516
1617use crate :: chain:: channelmonitor:: LATENCY_GRACE_PERIOD_BLOCKS ;
@@ -20,7 +21,7 @@ use crate::ln::chan_utils::ClosingTransaction;
2021use crate :: ln:: channel:: DISCONNECT_PEER_AWAITING_RESPONSE_TICKS ;
2122use crate :: ln:: channel_state:: { ChannelDetails , ChannelShutdownState } ;
2223use crate :: ln:: channelmanager:: { PaymentId , RAACommitmentOrder , RecipientOnionFields } ;
23- use crate :: ln:: msgs:: { BaseMessageHandler , ChannelMessageHandler , MessageSendEvent } ;
24+ use crate :: ln:: msgs:: { BaseMessageHandler , ChannelMessageHandler , ErrorAction , MessageSendEvent } ;
2425use crate :: ln:: { functional_test_utils:: * , msgs} ;
2526use crate :: sign:: ecdsa:: EcdsaChannelSigner ;
2627use crate :: sign:: SignerProvider ;
@@ -1447,3 +1448,104 @@ fn test_no_disconnect_while_async_commitment_signed_expecting_remote_revoke_and_
14471448 assert ! ( nodes[ 0 ] . node. get_and_clear_pending_msg_events( ) . is_empty( ) ) ;
14481449 assert ! ( nodes[ 1 ] . node. get_and_clear_pending_msg_events( ) . into_iter( ) . any( has_disconnect_event) ) ;
14491450}
1451+
1452+ #[ test]
1453+ fn test_async_panic_on_stale_state ( ) {
1454+ // Test that we panic if the counterparty sends us a `channel_reestablish` message with a
1455+ // `next_remote_commitment_number` greater than what we know with a valid corresponding secret,
1456+ // proving that we have lost state, when we have an async signer that is not able to immediately
1457+ // fetch the corresponding point to verify.
1458+ let chanmon_cfgs = create_chanmon_cfgs ( 2 ) ;
1459+ let node_cfgs = create_node_cfgs ( 2 , & chanmon_cfgs) ;
1460+ let stale_persister;
1461+ let stale_chain_monitor;
1462+ let node_chanmgrs = create_node_chanmgrs ( 2 , & node_cfgs, & [ None , None ] ) ;
1463+ let stale_node;
1464+ let mut nodes = create_network ( 2 , & node_cfgs, & node_chanmgrs) ;
1465+
1466+ let node_id_0 = nodes[ 0 ] . node . get_our_node_id ( ) ;
1467+ let node_id_1 = nodes[ 1 ] . node . get_our_node_id ( ) ;
1468+ let chan_id = create_announced_chan_between_nodes ( & nodes, 0 , 1 ) . 2 ;
1469+
1470+ let encoded_stale_node_1 = nodes[ 1 ] . node . encode ( ) ;
1471+ let encoded_stale_monitor_1 = get_monitor ! ( nodes[ 1 ] , chan_id) . encode ( ) ;
1472+
1473+ send_payment ( & nodes[ 0 ] , & [ & nodes[ 1 ] ] , 1_000_000 ) ;
1474+
1475+ nodes[ 0 ] . node . peer_disconnected ( node_id_1) ;
1476+ nodes[ 1 ] . node . peer_disconnected ( node_id_0) ;
1477+
1478+ reload_node ! (
1479+ nodes[ 1 ] ,
1480+ encoded_stale_node_1,
1481+ & [ & encoded_stale_monitor_1] ,
1482+ stale_persister,
1483+ stale_chain_monitor,
1484+ stale_node
1485+ ) ;
1486+
1487+ nodes[ 1 ] . disable_channel_signer_op ( & node_id_0, & chan_id, SignerOp :: GetPerCommitmentPoint ) ;
1488+
1489+ connect_nodes ( & nodes[ 0 ] , & nodes[ 1 ] ) ;
1490+ let reestablish_0_to_1 = get_chan_reestablish_msgs ! ( nodes[ 0 ] , nodes[ 1 ] ) ;
1491+ nodes[ 1 ] . node . handle_channel_reestablish ( node_id_0, & reestablish_0_to_1[ 0 ] ) ;
1492+
1493+ nodes[ 1 ] . enable_channel_signer_op ( & node_id_0, & chan_id, SignerOp :: GetPerCommitmentPoint ) ;
1494+ std:: panic:: catch_unwind ( || nodes[ 1 ] . node . signer_unblocked ( None ) ) . unwrap_err ( ) ;
1495+ nodes[ 1 ] . logger . assert_log_contains (
1496+ "lightning::ln::channel" ,
1497+ "We have fallen behind - we have received proof that if we broadcast our counterparty is going to claim all our funds." ,
1498+ 1 ,
1499+ ) ;
1500+
1501+ std:: panic:: catch_unwind ( || drop ( nodes) ) . unwrap_err ( ) ;
1502+ }
1503+
1504+ #[ test]
1505+ fn test_async_force_close_on_invalid_secret_for_stale_state ( ) {
1506+ // Test that we force close a channel if the counterparty sends us a `channel_reestablish`
1507+ // message with a `next_remote_commitment_number` greater than what we know with an invalid
1508+ // corresponding secret when we have an async signer that is not able to immediately fetch the
1509+ // corresponding point to verify.
1510+ let chanmon_cfgs = create_chanmon_cfgs ( 2 ) ;
1511+ let node_cfgs = create_node_cfgs ( 2 , & chanmon_cfgs) ;
1512+ let node_chanmgrs = create_node_chanmgrs ( 2 , & node_cfgs, & [ None , None ] ) ;
1513+ let mut nodes = create_network ( 2 , & node_cfgs, & node_chanmgrs) ;
1514+
1515+ let node_id_0 = nodes[ 0 ] . node . get_our_node_id ( ) ;
1516+ let node_id_1 = nodes[ 1 ] . node . get_our_node_id ( ) ;
1517+ let chan_id = create_announced_chan_between_nodes ( & nodes, 0 , 1 ) . 2 ;
1518+
1519+ send_payment ( & nodes[ 0 ] , & [ & nodes[ 1 ] ] , 1_000_000 ) ;
1520+
1521+ nodes[ 0 ] . node . peer_disconnected ( node_id_1) ;
1522+ nodes[ 1 ] . node . peer_disconnected ( node_id_0) ;
1523+
1524+ nodes[ 1 ] . disable_channel_signer_op ( & node_id_0, & chan_id, SignerOp :: GetPerCommitmentPoint ) ;
1525+
1526+ connect_nodes ( & nodes[ 0 ] , & nodes[ 1 ] ) ;
1527+ let mut reestablish_0_to_1 = get_chan_reestablish_msgs ! ( nodes[ 0 ] , nodes[ 1 ] ) ;
1528+ let _ = get_chan_reestablish_msgs ! ( nodes[ 1 ] , nodes[ 0 ] ) ;
1529+ reestablish_0_to_1[ 0 ] . next_remote_commitment_number += 1 ;
1530+ nodes[ 1 ] . node . handle_channel_reestablish ( node_id_0, & reestablish_0_to_1[ 0 ] ) ;
1531+
1532+ assert ! ( nodes[ 1 ] . node. get_and_clear_pending_events( ) . is_empty( ) ) ;
1533+ let msg_events = nodes[ 1 ] . node . get_and_clear_pending_msg_events ( ) ;
1534+ match & msg_events[ 0 ] {
1535+ MessageSendEvent :: HandleError {
1536+ action : ErrorAction :: DisconnectPeerWithWarning { .. } ,
1537+ ..
1538+ } => { } ,
1539+ _ => panic ! ( "Unexpected event" ) ,
1540+ }
1541+
1542+ nodes[ 1 ] . enable_channel_signer_op ( & node_id_0, & chan_id, SignerOp :: GetPerCommitmentPoint ) ;
1543+ nodes[ 1 ] . node . signer_unblocked ( None ) ;
1544+
1545+ let closure_reason = ClosureReason :: ProcessingError {
1546+ err : "Peer sent a channel_reestablish indicating we're stale with an invalid commitment secret" . to_owned ( ) ,
1547+ } ;
1548+ check_added_monitors ( & nodes[ 1 ] , 1 ) ;
1549+ check_closed_broadcast ( & nodes[ 1 ] , 1 , true ) ;
1550+ check_closed_event ( & nodes[ 1 ] , 1 , closure_reason, & [ node_id_0] , 100_000 ) ;
1551+ }
0 commit comments