Skip to content

Commit 0627c0c

Browse files
committed
Fix some test theoretical lock inversions
In the next commit we add lockorder testing based on the line each mutex was created on rather than the particular mutex instance. This causes some additional test failure because of lockorder inversions for the same mutex across different tests, which is fixed here.
1 parent 2a3bf03 commit 0627c0c

File tree

4 files changed

+45
-34
lines changed

4 files changed

+45
-34
lines changed

lightning/src/ln/functional_test_utils.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,11 @@ impl<'a, 'b, 'c> Drop for Node<'a, 'b, 'c> {
352352
}
353353
}
354354

355+
let broadcaster = test_utils::TestBroadcaster {
356+
txn_broadcasted: Mutex::new(self.tx_broadcaster.txn_broadcasted.lock().unwrap().clone()),
357+
blocks: Arc::new(Mutex::new(self.tx_broadcaster.blocks.lock().unwrap().clone())),
358+
};
359+
355360
// Before using all the new monitors to check the watch outpoints, use the full set of
356361
// them to ensure we can write and reload our ChannelManager.
357362
{
@@ -367,20 +372,13 @@ impl<'a, 'b, 'c> Drop for Node<'a, 'b, 'c> {
367372
keys_manager: self.keys_manager,
368373
fee_estimator: &test_utils::TestFeeEstimator { sat_per_kw: Mutex::new(253) },
369374
chain_monitor: self.chain_monitor,
370-
tx_broadcaster: &test_utils::TestBroadcaster {
371-
txn_broadcasted: Mutex::new(self.tx_broadcaster.txn_broadcasted.lock().unwrap().clone()),
372-
blocks: Arc::new(Mutex::new(self.tx_broadcaster.blocks.lock().unwrap().clone())),
373-
},
375+
tx_broadcaster: &broadcaster,
374376
logger: &self.logger,
375377
channel_monitors,
376378
}).unwrap();
377379
}
378380

379381
let persister = test_utils::TestPersister::new();
380-
let broadcaster = test_utils::TestBroadcaster {
381-
txn_broadcasted: Mutex::new(self.tx_broadcaster.txn_broadcasted.lock().unwrap().clone()),
382-
blocks: Arc::new(Mutex::new(self.tx_broadcaster.blocks.lock().unwrap().clone())),
383-
};
384382
let chain_source = test_utils::TestChainSource::new(Network::Testnet);
385383
let chain_monitor = test_utils::TestChainMonitor::new(Some(&chain_source), &broadcaster, &self.logger, &feeest, &persister, &self.keys_manager);
386384
for deserialized_monitor in deserialized_monitors.drain(..) {

lightning/src/ln/functional_tests.rs

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3410,7 +3410,7 @@ fn test_htlc_ignore_latest_remote_commitment() {
34103410
check_added_monitors!(nodes[0], 1);
34113411
check_closed_event!(nodes[0], 1, ClosureReason::HolderForceClosed);
34123412

3413-
let node_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap();
3413+
let node_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
34143414
assert_eq!(node_txn.len(), 3);
34153415
assert_eq!(node_txn[0], node_txn[1]);
34163416

@@ -4828,7 +4828,7 @@ fn test_claim_on_remote_sizeable_push_msat() {
48284828
check_added_monitors!(nodes[0], 1);
48294829
check_closed_event!(nodes[0], 1, ClosureReason::HolderForceClosed);
48304830

4831-
let node_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap();
4831+
let node_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
48324832
assert_eq!(node_txn.len(), 1);
48334833
check_spends!(node_txn[0], chan.3);
48344834
assert_eq!(node_txn[0].output.len(), 2); // We can't force trimming of to_remote output as channel_reserve_satoshis block us to do so at channel opening
@@ -5034,7 +5034,7 @@ fn test_static_spendable_outputs_justice_tx_revoked_htlc_timeout_tx() {
50345034
check_closed_event!(nodes[0], 1, ClosureReason::CommitmentTxConfirmed);
50355035
connect_blocks(&nodes[0], TEST_FINAL_CLTV - 1); // Confirm blocks until the HTLC expires
50365036

5037-
let revoked_htlc_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap();
5037+
let revoked_htlc_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
50385038
assert_eq!(revoked_htlc_txn.len(), 2);
50395039
check_spends!(revoked_htlc_txn[0], chan_1.3);
50405040
assert_eq!(revoked_htlc_txn[1].input.len(), 1);
@@ -7839,7 +7839,7 @@ fn test_bump_penalty_txn_on_revoked_htlcs() {
78397839
check_closed_event!(nodes[1], 1, ClosureReason::CommitmentTxConfirmed);
78407840
connect_blocks(&nodes[1], 49); // Confirm blocks until the HTLC expires (note CLTV was explicitly 50 above)
78417841

7842-
let revoked_htlc_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap();
7842+
let revoked_htlc_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
78437843
assert_eq!(revoked_htlc_txn.len(), 3);
78447844
check_spends!(revoked_htlc_txn[1], chan.3);
78457845

@@ -8100,22 +8100,26 @@ fn test_counterparty_raa_skip_no_crash() {
81008100
let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
81018101
let channel_id = create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known()).2;
81028102

8103-
let mut guard = nodes[0].node.channel_state.lock().unwrap();
8104-
let keys = guard.by_id.get_mut(&channel_id).unwrap().get_signer();
8103+
let per_commitment_secret;
8104+
let next_per_commitment_point;
8105+
{
8106+
let mut guard = nodes[0].node.channel_state.lock().unwrap();
8107+
let keys = guard.by_id.get_mut(&channel_id).unwrap().get_signer();
81058108

8106-
const INITIAL_COMMITMENT_NUMBER: u64 = (1 << 48) - 1;
8109+
const INITIAL_COMMITMENT_NUMBER: u64 = (1 << 48) - 1;
81078110

8108-
// Make signer believe we got a counterparty signature, so that it allows the revocation
8109-
keys.get_enforcement_state().last_holder_commitment -= 1;
8110-
let per_commitment_secret = keys.release_commitment_secret(INITIAL_COMMITMENT_NUMBER);
8111+
// Make signer believe we got a counterparty signature, so that it allows the revocation
8112+
keys.get_enforcement_state().last_holder_commitment -= 1;
8113+
per_commitment_secret = keys.release_commitment_secret(INITIAL_COMMITMENT_NUMBER);
81118114

8112-
// Must revoke without gaps
8113-
keys.get_enforcement_state().last_holder_commitment -= 1;
8114-
keys.release_commitment_secret(INITIAL_COMMITMENT_NUMBER - 1);
8115+
// Must revoke without gaps
8116+
keys.get_enforcement_state().last_holder_commitment -= 1;
8117+
keys.release_commitment_secret(INITIAL_COMMITMENT_NUMBER - 1);
81158118

8116-
keys.get_enforcement_state().last_holder_commitment -= 1;
8117-
let next_per_commitment_point = PublicKey::from_secret_key(&Secp256k1::new(),
8118-
&SecretKey::from_slice(&keys.release_commitment_secret(INITIAL_COMMITMENT_NUMBER - 2)).unwrap());
8119+
keys.get_enforcement_state().last_holder_commitment -= 1;
8120+
next_per_commitment_point = PublicKey::from_secret_key(&Secp256k1::new(),
8121+
&SecretKey::from_slice(&keys.release_commitment_secret(INITIAL_COMMITMENT_NUMBER - 2)).unwrap());
8122+
}
81198123

81208124
nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(),
81218125
&msgs::RevokeAndACK { channel_id, per_commitment_secret, next_per_commitment_point });
@@ -8457,12 +8461,12 @@ fn test_reject_funding_before_inbound_channel_accepted() {
84578461
// `MessageSendEvent::SendAcceptChannel` event. The message is passed to `nodes[0]`
84588462
// `handle_accept_channel`, which is required in order for `create_funding_transaction` to
84598463
// succeed when `nodes[0]` is passed to it.
8460-
{
8464+
let accept_chan_msg = {
84618465
let mut lock;
84628466
let channel = get_channel_ref!(&nodes[1], lock, temp_channel_id);
8463-
let accept_chan_msg = channel.get_accept_channel_message();
8464-
nodes[0].node.handle_accept_channel(&nodes[1].node.get_our_node_id(), InitFeatures::known(), &accept_chan_msg);
8465-
}
8467+
channel.get_accept_channel_message()
8468+
};
8469+
nodes[0].node.handle_accept_channel(&nodes[1].node.get_our_node_id(), InitFeatures::known(), &accept_chan_msg);
84668470

84678471
let (temporary_channel_id, tx, _) = create_funding_transaction(&nodes[0], &nodes[1].node.get_our_node_id(), 100000, 42);
84688472

lightning/src/ln/peer_handler.rs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1926,11 +1926,18 @@ mod tests {
19261926
peer_a.new_inbound_connection(fd_a.clone(), None).unwrap();
19271927
assert_eq!(peer_a.read_event(&mut fd_a, &initial_data).unwrap(), false);
19281928
peer_a.process_events();
1929-
assert_eq!(peer_b.read_event(&mut fd_b, &fd_a.outbound_data.lock().unwrap().split_off(0)).unwrap(), false);
1929+
1930+
let a_data = fd_a.outbound_data.lock().unwrap().split_off(0);
1931+
assert_eq!(peer_b.read_event(&mut fd_b, &a_data).unwrap(), false);
1932+
19301933
peer_b.process_events();
1931-
assert_eq!(peer_a.read_event(&mut fd_a, &fd_b.outbound_data.lock().unwrap().split_off(0)).unwrap(), false);
1934+
let b_data = fd_b.outbound_data.lock().unwrap().split_off(0);
1935+
assert_eq!(peer_a.read_event(&mut fd_a, &b_data).unwrap(), false);
1936+
19321937
peer_a.process_events();
1933-
assert_eq!(peer_b.read_event(&mut fd_b, &fd_a.outbound_data.lock().unwrap().split_off(0)).unwrap(), false);
1938+
let a_data = fd_a.outbound_data.lock().unwrap().split_off(0);
1939+
assert_eq!(peer_b.read_event(&mut fd_b, &a_data).unwrap(), false);
1940+
19341941
(fd_a.clone(), fd_b.clone())
19351942
}
19361943

@@ -2084,14 +2091,16 @@ mod tests {
20842091

20852092
assert_eq!(peers[0].read_event(&mut fd_a, &initial_data).unwrap(), false);
20862093
peers[0].process_events();
2087-
assert_eq!(peers[1].read_event(&mut fd_b, &fd_a.outbound_data.lock().unwrap().split_off(0)).unwrap(), false);
2094+
let a_data = fd_a.outbound_data.lock().unwrap().split_off(0);
2095+
assert_eq!(peers[1].read_event(&mut fd_b, &a_data).unwrap(), false);
20882096
peers[1].process_events();
20892097

20902098
// ...but if we get a second timer tick, we should disconnect the peer
20912099
peers[0].timer_tick_occurred();
20922100
assert_eq!(peers[0].peers.read().unwrap().len(), 0);
20932101

2094-
assert!(peers[0].read_event(&mut fd_a, &fd_b.outbound_data.lock().unwrap().split_off(0)).is_err());
2102+
let b_data = fd_b.outbound_data.lock().unwrap().split_off(0);
2103+
assert!(peers[0].read_event(&mut fd_a, &b_data).is_err());
20952104
}
20962105

20972106
#[test]

lightning/src/ln/reorg_tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ fn do_test_onchain_htlc_reorg(local_commitment: bool, claim: bool) {
8282
check_added_monitors!(nodes[2], 1);
8383
check_closed_broadcast!(nodes[2], true); // We should get a BroadcastChannelUpdate (and *only* a BroadcstChannelUpdate)
8484
check_closed_event!(nodes[2], 1, ClosureReason::CommitmentTxConfirmed);
85-
let node_2_commitment_txn = nodes[2].tx_broadcaster.txn_broadcasted.lock().unwrap();
85+
let node_2_commitment_txn = nodes[2].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
8686
assert_eq!(node_2_commitment_txn.len(), 3); // ChannelMonitor: 1 offered HTLC-Claim, ChannelManger: 1 local commitment tx, 1 Received HTLC-Claim
8787
assert_eq!(node_2_commitment_txn[1].output.len(), 2); // to-remote and Received HTLC (to-self is dust)
8888
check_spends!(node_2_commitment_txn[1], chan_2.3);

0 commit comments

Comments
 (0)