-
Notifications
You must be signed in to change notification settings - Fork 409
Use correct to_remote script in counterparty commitments #2605
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
TheBlueMatt
merged 5 commits into
lightningdevkit:main
from
wpaulino:anchors-monitor-track-to-remote-script
Sep 29, 2023
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
33b745f
Use correct to_remote script in counterparty commitments
wpaulino fa2a2ef
Support signing to_remote anchors variant for StaticPaymentOutput
wpaulino 3299d88
Fix off-by-one max witness estimate for P2WPKH StaticPaymentDescriptor
wpaulino 9f3bb7d
Fix incorrect anchors `counterparty_payment_script` upon deserialization
wpaulino f464aa9
Expose witness_script for StaticPaymentOutputDescriptor
wpaulino File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2287,16 +2287,20 @@ fn test_anchors_aggregated_revoked_htlc_tx() { | |
|
||
assert!(nodes[1].chain_monitor.chain_monitor.get_and_clear_pending_events().is_empty()); | ||
let spendable_output_events = nodes[0].chain_monitor.chain_monitor.get_and_clear_pending_events(); | ||
assert_eq!(spendable_output_events.len(), 2); | ||
for event in spendable_output_events.iter() { | ||
assert_eq!(spendable_output_events.len(), 4); | ||
for event in spendable_output_events { | ||
if let Event::SpendableOutputs { outputs, channel_id } = event { | ||
assert_eq!(outputs.len(), 1); | ||
assert!(vec![chan_b.2, chan_a.2].contains(&channel_id.unwrap())); | ||
let spend_tx = nodes[0].keys_manager.backing.spend_spendable_outputs( | ||
&[&outputs[0]], Vec::new(), Script::new_op_return(&[]), 253, None, &Secp256k1::new(), | ||
).unwrap(); | ||
|
||
check_spends!(spend_tx, revoked_claim_transactions.get(&spend_tx.input[0].previous_output.txid).unwrap()); | ||
if let SpendableOutputDescriptor::StaticPaymentOutput(_) = &outputs[0] { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i think one way to harden the test can be to count the |
||
check_spends!(spend_tx, &revoked_commitment_a, &revoked_commitment_b); | ||
} else { | ||
check_spends!(spend_tx, revoked_claim_transactions.get(&spend_tx.input[0].previous_output.txid).unwrap()); | ||
} | ||
} else { | ||
panic!("unexpected event"); | ||
} | ||
|
@@ -2314,3 +2318,90 @@ fn test_anchors_aggregated_revoked_htlc_tx() { | |
// revoked commitment which Bob has the preimage for. | ||
assert_eq!(nodes[1].chain_monitor.chain_monitor.get_claimable_balances(&[]).len(), 6); | ||
} | ||
|
||
fn do_test_anchors_monitor_fixes_counterparty_payment_script_on_reload(confirm_commitment_before_reload: bool) { | ||
// Tests that we'll fix a ChannelMonitor's `counterparty_payment_script` for an anchor outputs | ||
// channel upon deserialization. | ||
let chanmon_cfgs = create_chanmon_cfgs(2); | ||
let node_cfgs = create_node_cfgs(2, &chanmon_cfgs); | ||
let persister; | ||
let chain_monitor; | ||
let mut user_config = test_default_channel_config(); | ||
user_config.channel_handshake_config.negotiate_anchors_zero_fee_htlc_tx = true; | ||
user_config.manually_accept_inbound_channels = true; | ||
let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[Some(user_config), Some(user_config)]); | ||
let node_deserialized; | ||
let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs); | ||
|
||
let (_, _, chan_id, funding_tx) = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100_000, 50_000_000); | ||
|
||
// Set the monitor's `counterparty_payment_script` to a dummy P2WPKH script. | ||
let secp = Secp256k1::new(); | ||
let privkey = bitcoin::PrivateKey::from_slice(&[1; 32], bitcoin::Network::Testnet).unwrap(); | ||
let pubkey = bitcoin::PublicKey::from_private_key(&secp, &privkey); | ||
let p2wpkh_script = Script::new_v0_p2wpkh(&pubkey.wpubkey_hash().unwrap()); | ||
get_monitor!(nodes[1], chan_id).set_counterparty_payment_script(p2wpkh_script.clone()); | ||
assert_eq!(get_monitor!(nodes[1], chan_id).get_counterparty_payment_script(), p2wpkh_script); | ||
|
||
// Confirm the counterparty's commitment and reload the monitor (either before or after) such | ||
// that we arrive at the correct `counterparty_payment_script` after the reload. | ||
nodes[0].node.force_close_broadcasting_latest_txn(&chan_id, &nodes[1].node.get_our_node_id()).unwrap(); | ||
check_added_monitors(&nodes[0], 1); | ||
check_closed_broadcast(&nodes[0], 1, true); | ||
check_closed_event!(&nodes[0], 1, ClosureReason::HolderForceClosed, false, | ||
[nodes[1].node.get_our_node_id()], 100000); | ||
|
||
let commitment_tx = { | ||
let mut txn = nodes[0].tx_broadcaster.unique_txn_broadcast(); | ||
assert_eq!(txn.len(), 1); | ||
assert_eq!(txn[0].output.len(), 4); | ||
check_spends!(txn[0], funding_tx); | ||
txn.pop().unwrap() | ||
}; | ||
|
||
mine_transaction(&nodes[0], &commitment_tx); | ||
let commitment_tx_conf_height = if confirm_commitment_before_reload { | ||
// We should expect our round trip serialization check to fail as we're writing the monitor | ||
// with the incorrect P2WPKH script but reading it with the correct P2WSH script. | ||
*nodes[1].chain_monitor.expect_monitor_round_trip_fail.lock().unwrap() = Some(chan_id); | ||
let commitment_tx_conf_height = block_from_scid(&mine_transaction(&nodes[1], &commitment_tx)); | ||
let serialized_monitor = get_monitor!(nodes[1], chan_id).encode(); | ||
reload_node!(nodes[1], user_config, &nodes[1].node.encode(), &[&serialized_monitor], persister, chain_monitor, node_deserialized); | ||
commitment_tx_conf_height | ||
} else { | ||
let serialized_monitor = get_monitor!(nodes[1], chan_id).encode(); | ||
reload_node!(nodes[1], user_config, &nodes[1].node.encode(), &[&serialized_monitor], persister, chain_monitor, node_deserialized); | ||
let commitment_tx_conf_height = block_from_scid(&mine_transaction(&nodes[1], &commitment_tx)); | ||
check_added_monitors(&nodes[1], 1); | ||
check_closed_broadcast(&nodes[1], 1, true); | ||
commitment_tx_conf_height | ||
}; | ||
check_closed_event!(&nodes[1], 1, ClosureReason::CommitmentTxConfirmed, false, | ||
[nodes[0].node.get_our_node_id()], 100000); | ||
assert!(get_monitor!(nodes[1], chan_id).get_counterparty_payment_script().is_v0_p2wsh()); | ||
|
||
connect_blocks(&nodes[0], ANTI_REORG_DELAY - 1); | ||
connect_blocks(&nodes[1], ANTI_REORG_DELAY - 1); | ||
|
||
if confirm_commitment_before_reload { | ||
// If we saw the commitment before our `counterparty_payment_script` was fixed, we'll never | ||
// get the spendable output event for the `to_remote` output, so we'll need to get it | ||
// manually via `get_spendable_outputs`. | ||
check_added_monitors(&nodes[1], 1); | ||
let outputs = get_monitor!(nodes[1], chan_id).get_spendable_outputs(&commitment_tx, commitment_tx_conf_height); | ||
assert_eq!(outputs.len(), 1); | ||
let spend_tx = nodes[1].keys_manager.backing.spend_spendable_outputs( | ||
&[&outputs[0]], Vec::new(), Builder::new().push_opcode(opcodes::all::OP_RETURN).into_script(), | ||
253, None, &secp | ||
).unwrap(); | ||
check_spends!(spend_tx, &commitment_tx); | ||
} else { | ||
test_spendable_output(&nodes[1], &commitment_tx); | ||
} | ||
} | ||
|
||
#[test] | ||
fn test_anchors_monitor_fixes_counterparty_payment_script_on_reload() { | ||
do_test_anchors_monitor_fixes_counterparty_payment_script_on_reload(false); | ||
do_test_anchors_monitor_fixes_counterparty_payment_script_on_reload(true); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.