Skip to content

Commit ab49a0e

Browse files
committed
Construct all ChannelMonitor mutexes in the same function
When we add lockorder detection based on mutex construction site rather than mutex instance in the next commit, ChannelMonitor's PartialEq implementation causes spurious failures. This is caused by the lockorder detection logic considering the ChannelMonitor inner mutex to be two distinct mutexes - one when monitors are deserialized and one when monitors are created fresh. Instead, we attempt to tell the lockorder detection logic that they are the same by ensuring they're constructed in the same place - in this case a util method.
1 parent 0627c0c commit ab49a0e

File tree

1 file changed

+87
-84
lines changed

1 file changed

+87
-84
lines changed

lightning/src/chain/channelmonitor.rs

Lines changed: 87 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -965,6 +965,13 @@ impl<Signer: Sign> Writeable for ChannelMonitorImpl<Signer> {
965965
}
966966

967967
impl<Signer: Sign> ChannelMonitor<Signer> {
968+
/// For lockorder enforcement purposes, we need to have a single site which constructs the
969+
/// `inner` mutex, otherwise cases where we lock two monitors at the same time (eg in our
970+
/// PartialEq implementation) we may decide a lockorder violation has occurred.
971+
fn construct_monitor(imp: ChannelMonitorImpl<Signer>) -> Self {
972+
ChannelMonitor { inner: Mutex::new(imp) }
973+
}
974+
968975
pub(crate) fn new(secp_ctx: Secp256k1<secp256k1::All>, keys: Signer, shutdown_script: Option<Script>,
969976
on_counterparty_tx_csv: u16, destination_script: &Script, funding_info: (OutPoint, Script),
970977
channel_parameters: &ChannelTransactionParameters,
@@ -1012,60 +1019,58 @@ impl<Signer: Sign> ChannelMonitor<Signer> {
10121019
let mut outputs_to_watch = HashMap::new();
10131020
outputs_to_watch.insert(funding_info.0.txid, vec![(funding_info.0.index as u32, funding_info.1.clone())]);
10141021

1015-
ChannelMonitor {
1016-
inner: Mutex::new(ChannelMonitorImpl {
1017-
latest_update_id: 0,
1018-
commitment_transaction_number_obscure_factor,
1022+
Self::construct_monitor(ChannelMonitorImpl {
1023+
latest_update_id: 0,
1024+
commitment_transaction_number_obscure_factor,
10191025

1020-
destination_script: destination_script.clone(),
1021-
broadcasted_holder_revokable_script: None,
1022-
counterparty_payment_script,
1023-
shutdown_script,
1026+
destination_script: destination_script.clone(),
1027+
broadcasted_holder_revokable_script: None,
1028+
counterparty_payment_script,
1029+
shutdown_script,
10241030

1025-
channel_keys_id,
1026-
holder_revocation_basepoint,
1027-
funding_info,
1028-
current_counterparty_commitment_txid: None,
1029-
prev_counterparty_commitment_txid: None,
1031+
channel_keys_id,
1032+
holder_revocation_basepoint,
1033+
funding_info,
1034+
current_counterparty_commitment_txid: None,
1035+
prev_counterparty_commitment_txid: None,
10301036

1031-
counterparty_commitment_params,
1032-
funding_redeemscript,
1033-
channel_value_satoshis,
1034-
their_cur_per_commitment_points: None,
1037+
counterparty_commitment_params,
1038+
funding_redeemscript,
1039+
channel_value_satoshis,
1040+
their_cur_per_commitment_points: None,
10351041

1036-
on_holder_tx_csv: counterparty_channel_parameters.selected_contest_delay,
1042+
on_holder_tx_csv: counterparty_channel_parameters.selected_contest_delay,
10371043

1038-
commitment_secrets: CounterpartyCommitmentSecrets::new(),
1039-
counterparty_claimable_outpoints: HashMap::new(),
1040-
counterparty_commitment_txn_on_chain: HashMap::new(),
1041-
counterparty_hash_commitment_number: HashMap::new(),
1044+
commitment_secrets: CounterpartyCommitmentSecrets::new(),
1045+
counterparty_claimable_outpoints: HashMap::new(),
1046+
counterparty_commitment_txn_on_chain: HashMap::new(),
1047+
counterparty_hash_commitment_number: HashMap::new(),
10421048

1043-
prev_holder_signed_commitment_tx: None,
1044-
current_holder_commitment_tx: holder_commitment_tx,
1045-
current_counterparty_commitment_number: 1 << 48,
1046-
current_holder_commitment_number,
1049+
prev_holder_signed_commitment_tx: None,
1050+
current_holder_commitment_tx: holder_commitment_tx,
1051+
current_counterparty_commitment_number: 1 << 48,
1052+
current_holder_commitment_number,
10471053

1048-
payment_preimages: HashMap::new(),
1049-
pending_monitor_events: Vec::new(),
1050-
pending_events: Vec::new(),
1054+
payment_preimages: HashMap::new(),
1055+
pending_monitor_events: Vec::new(),
1056+
pending_events: Vec::new(),
10511057

1052-
onchain_events_awaiting_threshold_conf: Vec::new(),
1053-
outputs_to_watch,
1058+
onchain_events_awaiting_threshold_conf: Vec::new(),
1059+
outputs_to_watch,
10541060

1055-
onchain_tx_handler,
1061+
onchain_tx_handler,
10561062

1057-
lockdown_from_offchain: false,
1058-
holder_tx_signed: false,
1059-
funding_spend_seen: false,
1060-
funding_spend_confirmed: None,
1061-
htlcs_resolved_on_chain: Vec::new(),
1063+
lockdown_from_offchain: false,
1064+
holder_tx_signed: false,
1065+
funding_spend_seen: false,
1066+
funding_spend_confirmed: None,
1067+
htlcs_resolved_on_chain: Vec::new(),
10621068

1063-
best_block,
1064-
counterparty_node_id: Some(counterparty_node_id),
1069+
best_block,
1070+
counterparty_node_id: Some(counterparty_node_id),
10651071

1066-
secp_ctx,
1067-
}),
1068-
}
1072+
secp_ctx,
1073+
})
10691074
}
10701075

10711076
#[cfg(test)]
@@ -3361,60 +3366,58 @@ impl<'a, Signer: Sign, K: KeysInterface<Signer = Signer>> ReadableArgs<&'a K>
33613366
let mut secp_ctx = Secp256k1::new();
33623367
secp_ctx.seeded_randomize(&keys_manager.get_secure_random_bytes());
33633368

3364-
Ok((best_block.block_hash(), ChannelMonitor {
3365-
inner: Mutex::new(ChannelMonitorImpl {
3366-
latest_update_id,
3367-
commitment_transaction_number_obscure_factor,
3369+
Ok((best_block.block_hash(), ChannelMonitor::construct_monitor(ChannelMonitorImpl {
3370+
latest_update_id,
3371+
commitment_transaction_number_obscure_factor,
33683372

3369-
destination_script,
3370-
broadcasted_holder_revokable_script,
3371-
counterparty_payment_script,
3372-
shutdown_script,
3373+
destination_script,
3374+
broadcasted_holder_revokable_script,
3375+
counterparty_payment_script,
3376+
shutdown_script,
33733377

3374-
channel_keys_id,
3375-
holder_revocation_basepoint,
3376-
funding_info,
3377-
current_counterparty_commitment_txid,
3378-
prev_counterparty_commitment_txid,
3378+
channel_keys_id,
3379+
holder_revocation_basepoint,
3380+
funding_info,
3381+
current_counterparty_commitment_txid,
3382+
prev_counterparty_commitment_txid,
33793383

3380-
counterparty_commitment_params,
3381-
funding_redeemscript,
3382-
channel_value_satoshis,
3383-
their_cur_per_commitment_points,
3384+
counterparty_commitment_params,
3385+
funding_redeemscript,
3386+
channel_value_satoshis,
3387+
their_cur_per_commitment_points,
33843388

3385-
on_holder_tx_csv,
3389+
on_holder_tx_csv,
33863390

3387-
commitment_secrets,
3388-
counterparty_claimable_outpoints,
3389-
counterparty_commitment_txn_on_chain,
3390-
counterparty_hash_commitment_number,
3391+
commitment_secrets,
3392+
counterparty_claimable_outpoints,
3393+
counterparty_commitment_txn_on_chain,
3394+
counterparty_hash_commitment_number,
33913395

3392-
prev_holder_signed_commitment_tx,
3393-
current_holder_commitment_tx,
3394-
current_counterparty_commitment_number,
3395-
current_holder_commitment_number,
3396+
prev_holder_signed_commitment_tx,
3397+
current_holder_commitment_tx,
3398+
current_counterparty_commitment_number,
3399+
current_holder_commitment_number,
33963400

3397-
payment_preimages,
3398-
pending_monitor_events: pending_monitor_events.unwrap(),
3399-
pending_events,
3401+
payment_preimages,
3402+
pending_monitor_events: pending_monitor_events.unwrap(),
3403+
pending_events,
34003404

3401-
onchain_events_awaiting_threshold_conf,
3402-
outputs_to_watch,
3405+
onchain_events_awaiting_threshold_conf,
3406+
outputs_to_watch,
34033407

3404-
onchain_tx_handler,
3408+
onchain_tx_handler,
34053409

3406-
lockdown_from_offchain,
3407-
holder_tx_signed,
3408-
funding_spend_seen: funding_spend_seen.unwrap(),
3409-
funding_spend_confirmed,
3410-
htlcs_resolved_on_chain: htlcs_resolved_on_chain.unwrap(),
3410+
lockdown_from_offchain,
3411+
holder_tx_signed,
3412+
funding_spend_seen: funding_spend_seen.unwrap(),
3413+
funding_spend_confirmed,
3414+
htlcs_resolved_on_chain: htlcs_resolved_on_chain.unwrap(),
34113415

3412-
best_block,
3413-
counterparty_node_id,
3416+
best_block,
3417+
counterparty_node_id,
34143418

3415-
secp_ctx,
3416-
}),
3417-
}))
3419+
secp_ctx,
3420+
})))
34183421
}
34193422
}
34203423

0 commit comments

Comments
 (0)