@@ -39,7 +39,7 @@ use bitcoin::secp256k1;
39
39
40
40
use ln:: msgs:: DecodeError ;
41
41
use ln:: chan_utils;
42
- use ln:: chan_utils:: { CounterpartyCommitmentSecrets , HTLCOutputInCommitment , HolderCommitmentTransaction , HTLCType , ChannelPublicKeys } ;
42
+ use ln:: chan_utils:: { CounterpartyCommitmentSecrets , HTLCOutputInCommitment , HTLCType , ChannelStaticInfo , HolderCommitmentTransactionInfo } ;
43
43
use ln:: channelmanager:: { HTLCSource , PaymentPreimage , PaymentHash } ;
44
44
use ln:: onchaintx:: { OnchainTxHandler , InputDescriptors } ;
45
45
use chain:: chaininterface:: { BroadcasterInterface , FeeEstimator } ;
@@ -474,7 +474,7 @@ const MIN_SERIALIZATION_VERSION: u8 = 1;
474
474
#[ derive( Clone ) ]
475
475
pub ( crate ) enum ChannelMonitorUpdateStep {
476
476
LatestHolderCommitmentTXInfo {
477
- commitment_tx : HolderCommitmentTransaction ,
477
+ commitment_info : HolderCommitmentTransactionInfo ,
478
478
htlc_outputs : Vec < ( HTLCOutputInCommitment , Option < Signature > , Option < HTLCSource > ) > ,
479
479
} ,
480
480
LatestCounterpartyCommitmentTXInfo {
@@ -502,7 +502,7 @@ pub(crate) enum ChannelMonitorUpdateStep {
502
502
impl Writeable for ChannelMonitorUpdateStep {
503
503
fn write < W : Writer > ( & self , w : & mut W ) -> Result < ( ) , :: std:: io:: Error > {
504
504
match self {
505
- & ChannelMonitorUpdateStep :: LatestHolderCommitmentTXInfo { ref commitment_tx, ref htlc_outputs } => {
505
+ & ChannelMonitorUpdateStep :: LatestHolderCommitmentTXInfo { commitment_info : ref commitment_tx, ref htlc_outputs } => {
506
506
0u8 . write ( w) ?;
507
507
commitment_tx. write ( w) ?;
508
508
( htlc_outputs. len ( ) as u64 ) . write ( w) ?;
@@ -545,7 +545,7 @@ impl Readable for ChannelMonitorUpdateStep {
545
545
match Readable :: read ( r) ? {
546
546
0u8 => {
547
547
Ok ( ChannelMonitorUpdateStep :: LatestHolderCommitmentTXInfo {
548
- commitment_tx : Readable :: read ( r) ?,
548
+ commitment_info : Readable :: read ( r) ?,
549
549
htlc_outputs : {
550
550
let len: u64 = Readable :: read ( r) ?;
551
551
let mut res = Vec :: new ( ) ;
@@ -935,46 +935,48 @@ impl<ChanSigner: ChannelKeys + Writeable> ChannelMonitor<ChanSigner> {
935
935
936
936
impl < ChanSigner : ChannelKeys > ChannelMonitor < ChanSigner > {
937
937
pub ( crate ) fn new ( keys : ChanSigner , shutdown_pubkey : & PublicKey ,
938
- on_counterparty_tx_csv : u16 , destination_script : & Script , funding_info : ( OutPoint , Script ) ,
939
- counterparty_pubkeys : & ChannelPublicKeys ,
940
- on_holder_tx_csv : u16 , funding_redeemscript : Script , channel_value_satoshis : u64 ,
941
- commitment_transaction_number_obscure_factor : u64 ,
942
- is_outbound : bool ,
943
- initial_holder_commitment_tx : HolderCommitmentTransaction ) -> ChannelMonitor < ChanSigner > {
938
+ on_counterparty_tx_csv : u16 , destination_script : & Script , funding_info : ( OutPoint , Script ) ,
939
+ channel_static_info : & ChannelStaticInfo ,
940
+ funding_redeemscript : Script , channel_value_satoshis : u64 ,
941
+ commitment_transaction_number_obscure_factor : u64 ,
942
+ initial_holder_commitment_info : HolderCommitmentTransactionInfo ) -> ChannelMonitor < ChanSigner > {
944
943
945
944
assert ! ( commitment_transaction_number_obscure_factor <= ( 1 << 48 ) ) ;
946
945
let our_channel_close_key_hash = WPubkeyHash :: hash ( & shutdown_pubkey. serialize ( ) ) ;
947
946
let shutdown_script = Builder :: new ( ) . push_opcode ( opcodes:: all:: OP_PUSHBYTES_0 ) . push_slice ( & our_channel_close_key_hash[ ..] ) . into_script ( ) ;
948
947
let payment_key_hash = WPubkeyHash :: hash ( & keys. pubkeys ( ) . payment_point . serialize ( ) ) ;
949
948
let counterparty_payment_script = Builder :: new ( ) . push_opcode ( opcodes:: all:: OP_PUSHBYTES_0 ) . push_slice ( & payment_key_hash[ ..] ) . into_script ( ) ;
950
949
950
+ let counterparty_pubkeys = & channel_static_info. counterparty_pubkeys ;
951
951
let counterparty_delayed_payment_base_key = counterparty_pubkeys. delayed_payment_basepoint ;
952
952
let counterparty_htlc_base_key = counterparty_pubkeys. htlc_basepoint ;
953
953
let counterparty_tx_cache = CounterpartyCommitmentTransaction { counterparty_delayed_payment_base_key, counterparty_htlc_base_key, on_counterparty_tx_csv, per_htlc : HashMap :: new ( ) } ;
954
954
955
- let mut onchain_tx_handler = OnchainTxHandler :: new ( destination_script. clone ( ) , keys. clone ( ) , on_holder_tx_csv ) ;
955
+ let mut onchain_tx_handler = OnchainTxHandler :: new ( destination_script. clone ( ) , keys. clone ( ) , channel_static_info . clone ( ) ) ;
956
956
957
- let holder_tx_sequence = initial_holder_commitment_tx. unsigned_tx . input [ 0 ] . sequence as u64 ;
958
- let holder_tx_locktime = initial_holder_commitment_tx. unsigned_tx . lock_time as u64 ;
957
+ let current_holder_commitment_number = initial_holder_commitment_info. info . commitment_number ;
958
+ let secp_ctx = Secp256k1 :: new ( ) ;
959
+
960
+ let txid = initial_holder_commitment_info. txid ( channel_static_info, & secp_ctx) ;
959
961
let holder_commitment_tx = HolderSignedTx {
960
- txid : initial_holder_commitment_tx . txid ( ) ,
961
- revocation_key : initial_holder_commitment_tx . keys . revocation_key ,
962
- a_htlc_key : initial_holder_commitment_tx . keys . broadcaster_htlc_key ,
963
- b_htlc_key : initial_holder_commitment_tx . keys . countersignatory_htlc_key ,
964
- delayed_payment_key : initial_holder_commitment_tx . keys . broadcaster_delayed_payment_key ,
965
- per_commitment_point : initial_holder_commitment_tx . keys . per_commitment_point ,
966
- feerate_per_kw : initial_holder_commitment_tx . feerate_per_kw ,
962
+ txid,
963
+ revocation_key : initial_holder_commitment_info . info . keys . revocation_key ,
964
+ a_htlc_key : initial_holder_commitment_info . info . keys . broadcaster_htlc_key ,
965
+ b_htlc_key : initial_holder_commitment_info . info . keys . countersignatory_htlc_key ,
966
+ delayed_payment_key : initial_holder_commitment_info . info . keys . broadcaster_delayed_payment_key ,
967
+ per_commitment_point : initial_holder_commitment_info . info . keys . per_commitment_point ,
968
+ feerate_per_kw : initial_holder_commitment_info . info . feerate_per_kw ,
967
969
htlc_outputs : Vec :: new ( ) , // There are never any HTLCs in the initial commitment transactions
968
970
} ;
969
- onchain_tx_handler. provide_latest_holder_tx ( initial_holder_commitment_tx ) ;
971
+ onchain_tx_handler. provide_latest_holder_tx ( initial_holder_commitment_info ) ;
970
972
971
973
let mut outputs_to_watch = HashMap :: new ( ) ;
972
974
outputs_to_watch. insert ( funding_info. 0 . txid , vec ! [ ( funding_info. 0 . index as u32 , funding_info. 1 . clone( ) ) ] ) ;
973
975
974
976
ChannelMonitor {
975
977
latest_update_id : 0 ,
976
978
commitment_transaction_number_obscure_factor,
977
- is_outbound,
979
+ is_outbound : channel_static_info . is_outbound_from_holder ,
978
980
979
981
destination_script : destination_script. clone ( ) ,
980
982
broadcasted_holder_revokable_script : None ,
@@ -991,7 +993,7 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
991
993
channel_value_satoshis,
992
994
their_cur_revocation_points : None ,
993
995
994
- on_holder_tx_csv,
996
+ on_holder_tx_csv : channel_static_info . counterparty_selected_contest_delay ,
995
997
996
998
commitment_secrets : CounterpartyCommitmentSecrets :: new ( ) ,
997
999
counterparty_claimable_outpoints : HashMap :: new ( ) ,
@@ -1001,7 +1003,7 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
1001
1003
prev_holder_signed_commitment_tx : None ,
1002
1004
current_holder_commitment_tx : holder_commitment_tx,
1003
1005
current_counterparty_commitment_number : 1 << 48 ,
1004
- current_holder_commitment_number : 0xffff_ffff_ffff - ( ( ( ( holder_tx_sequence & 0xffffff ) << 3 * 8 ) | ( holder_tx_locktime as u64 & 0xffffff ) ) ^ commitment_transaction_number_obscure_factor ) ,
1006
+ current_holder_commitment_number,
1005
1007
1006
1008
payment_preimages : HashMap :: new ( ) ,
1007
1009
pending_monitor_events : Vec :: new ( ) ,
@@ -1016,7 +1018,7 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
1016
1018
holder_tx_signed : false ,
1017
1019
1018
1020
last_block_hash : Default :: default ( ) ,
1019
- secp_ctx : Secp256k1 :: new ( ) ,
1021
+ secp_ctx : secp_ctx ,
1020
1022
}
1021
1023
}
1022
1024
@@ -1124,22 +1126,20 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
1124
1126
/// is important that any clones of this channel monitor (including remote clones) by kept
1125
1127
/// up-to-date as our holder commitment transaction is updated.
1126
1128
/// Panics if set_on_holder_tx_csv has never been called.
1127
- fn provide_latest_holder_commitment_tx_info ( & mut self , commitment_tx : HolderCommitmentTransaction , htlc_outputs : Vec < ( HTLCOutputInCommitment , Option < Signature > , Option < HTLCSource > ) > ) -> Result < ( ) , MonitorUpdateError > {
1128
- let txid = commitment_tx. txid ( ) ;
1129
- let sequence = commitment_tx. unsigned_tx . input [ 0 ] . sequence as u64 ;
1130
- let locktime = commitment_tx. unsigned_tx . lock_time as u64 ;
1129
+ fn provide_latest_holder_commitment_tx_info ( & mut self , commitment_info : HolderCommitmentTransactionInfo , htlc_outputs : Vec < ( HTLCOutputInCommitment , Option < Signature > , Option < HTLCSource > ) > ) -> Result < ( ) , MonitorUpdateError > {
1130
+ let txid = commitment_info. txid ( & self . onchain_tx_handler . channel_static_info , & self . secp_ctx ) ;
1131
1131
let mut new_holder_commitment_tx = HolderSignedTx {
1132
1132
txid,
1133
- revocation_key : commitment_tx . keys . revocation_key ,
1134
- a_htlc_key : commitment_tx . keys . broadcaster_htlc_key ,
1135
- b_htlc_key : commitment_tx . keys . countersignatory_htlc_key ,
1136
- delayed_payment_key : commitment_tx . keys . broadcaster_delayed_payment_key ,
1137
- per_commitment_point : commitment_tx . keys . per_commitment_point ,
1138
- feerate_per_kw : commitment_tx . feerate_per_kw ,
1133
+ revocation_key : commitment_info . info . keys . revocation_key ,
1134
+ a_htlc_key : commitment_info . info . keys . broadcaster_htlc_key ,
1135
+ b_htlc_key : commitment_info . info . keys . countersignatory_htlc_key ,
1136
+ delayed_payment_key : commitment_info . info . keys . broadcaster_delayed_payment_key ,
1137
+ per_commitment_point : commitment_info . info . keys . per_commitment_point ,
1138
+ feerate_per_kw : commitment_info . info . feerate_per_kw ,
1139
1139
htlc_outputs,
1140
1140
} ;
1141
- self . onchain_tx_handler . provide_latest_holder_tx ( commitment_tx ) ;
1142
- self . current_holder_commitment_number = 0xffff_ffff_ffff - ( ( ( ( sequence & 0xffffff ) << 3 * 8 ) | ( locktime as u64 & 0xffffff ) ) ^ self . commitment_transaction_number_obscure_factor ) ;
1141
+ self . current_holder_commitment_number = commitment_info . info . commitment_number ;
1142
+ self . onchain_tx_handler . provide_latest_holder_tx ( commitment_info ) ;
1143
1143
mem:: swap ( & mut new_holder_commitment_tx, & mut self . current_holder_commitment_tx ) ;
1144
1144
self . prev_holder_signed_commitment_tx = Some ( new_holder_commitment_tx) ;
1145
1145
if self . holder_tx_signed {
@@ -1177,7 +1177,7 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
1177
1177
}
1178
1178
for update in updates. updates . iter ( ) {
1179
1179
match update {
1180
- ChannelMonitorUpdateStep :: LatestHolderCommitmentTXInfo { commitment_tx, htlc_outputs } => {
1180
+ ChannelMonitorUpdateStep :: LatestHolderCommitmentTXInfo { commitment_info : commitment_tx, htlc_outputs } => {
1181
1181
if self . lockdown_from_offchain { panic ! ( ) ; }
1182
1182
self . provide_latest_holder_commitment_tx_info ( commitment_tx. clone ( ) , htlc_outputs. clone ( ) ) ?
1183
1183
} ,
@@ -2493,7 +2493,7 @@ mod tests {
2493
2493
use ln:: channelmanager:: { PaymentPreimage , PaymentHash } ;
2494
2494
use ln:: onchaintx:: { OnchainTxHandler , InputDescriptors } ;
2495
2495
use ln:: chan_utils;
2496
- use ln:: chan_utils:: { HTLCOutputInCommitment , HolderCommitmentTransaction , ChannelPublicKeys } ;
2496
+ use ln:: chan_utils:: { HTLCOutputInCommitment , ChannelPublicKeys , ChannelStaticInfo , HolderCommitmentTransactionInfo } ;
2497
2497
use util:: test_utils:: TestLogger ;
2498
2498
use bitcoin:: secp256k1:: key:: { SecretKey , PublicKey } ;
2499
2499
use bitcoin:: secp256k1:: Secp256k1 ;
@@ -2571,16 +2571,24 @@ mod tests {
2571
2571
delayed_payment_basepoint : PublicKey :: from_secret_key ( & secp_ctx, & SecretKey :: from_slice ( & [ 47 ; 32 ] ) . unwrap ( ) ) ,
2572
2572
htlc_basepoint : PublicKey :: from_secret_key ( & secp_ctx, & SecretKey :: from_slice ( & [ 48 ; 32 ] ) . unwrap ( ) )
2573
2573
} ;
2574
+ let channel_static_info = ChannelStaticInfo {
2575
+ holder_pubkeys : keys. holder_channel_pubkeys . clone ( ) ,
2576
+ counterparty_pubkeys,
2577
+ holder_selected_contest_delay : 66 ,
2578
+ counterparty_selected_contest_delay : 67 ,
2579
+ funding_outpoint : Default :: default ( ) ,
2580
+ is_outbound_from_holder : true
2581
+ } ;
2574
2582
// Prune with one old state and a holder commitment tx holding a few overlaps with the
2575
2583
// old state.
2576
2584
let mut monitor = ChannelMonitor :: new ( keys,
2577
2585
& PublicKey :: from_secret_key ( & secp_ctx, & SecretKey :: from_slice ( & [ 42 ; 32 ] ) . unwrap ( ) ) , 0 , & Script :: new ( ) ,
2578
2586
( OutPoint { txid : Txid :: from_slice ( & [ 43 ; 32 ] ) . unwrap ( ) , index : 0 } , Script :: new ( ) ) ,
2579
- & counterparty_pubkeys ,
2580
- 10 , Script :: new ( ) , 46 , 0 ,
2581
- true , HolderCommitmentTransaction :: dummy ( ) ) ;
2587
+ & channel_static_info ,
2588
+ Script :: new ( ) , 46 , 0 ,
2589
+ HolderCommitmentTransactionInfo :: dummy ( ) ) ;
2582
2590
2583
- monitor. provide_latest_holder_commitment_tx_info ( HolderCommitmentTransaction :: dummy ( ) , preimages_to_holder_htlcs ! ( preimages[ 0 ..10 ] ) ) . unwrap ( ) ;
2591
+ monitor. provide_latest_holder_commitment_tx_info ( HolderCommitmentTransactionInfo :: dummy ( ) , preimages_to_holder_htlcs ! ( preimages[ 0 ..10 ] ) ) . unwrap ( ) ;
2584
2592
monitor. provide_latest_counterparty_commitment_tx_info ( & dummy_tx, preimages_slice_to_htlc_outputs ! ( preimages[ 5 ..15 ] ) , 281474976710655 , dummy_key, & logger) ;
2585
2593
monitor. provide_latest_counterparty_commitment_tx_info ( & dummy_tx, preimages_slice_to_htlc_outputs ! ( preimages[ 15 ..20 ] ) , 281474976710654 , dummy_key, & logger) ;
2586
2594
monitor. provide_latest_counterparty_commitment_tx_info ( & dummy_tx, preimages_slice_to_htlc_outputs ! ( preimages[ 17 ..20 ] ) , 281474976710653 , dummy_key, & logger) ;
@@ -2606,15 +2614,15 @@ mod tests {
2606
2614
2607
2615
// Now update holder commitment tx info, pruning only element 18 as we still care about the
2608
2616
// previous commitment tx's preimages too
2609
- monitor. provide_latest_holder_commitment_tx_info ( HolderCommitmentTransaction :: dummy ( ) , preimages_to_holder_htlcs ! ( preimages[ 0 ..5 ] ) ) . unwrap ( ) ;
2617
+ monitor. provide_latest_holder_commitment_tx_info ( HolderCommitmentTransactionInfo :: dummy ( ) , preimages_to_holder_htlcs ! ( preimages[ 0 ..5 ] ) ) . unwrap ( ) ;
2610
2618
secret[ 0 ..32 ] . clone_from_slice ( & hex:: decode ( "2273e227a5b7449b6e70f1fb4652864038b1cbf9cd7c043a7d6456b7fc275ad8" ) . unwrap ( ) ) ;
2611
2619
monitor. provide_secret ( 281474976710653 , secret. clone ( ) ) . unwrap ( ) ;
2612
2620
assert_eq ! ( monitor. payment_preimages. len( ) , 12 ) ;
2613
2621
test_preimages_exist ! ( & preimages[ 0 ..10 ] , monitor) ;
2614
2622
test_preimages_exist ! ( & preimages[ 18 ..20 ] , monitor) ;
2615
2623
2616
2624
// But if we do it again, we'll prune 5-10
2617
- monitor. provide_latest_holder_commitment_tx_info ( HolderCommitmentTransaction :: dummy ( ) , preimages_to_holder_htlcs ! ( preimages[ 0 ..3 ] ) ) . unwrap ( ) ;
2625
+ monitor. provide_latest_holder_commitment_tx_info ( HolderCommitmentTransactionInfo :: dummy ( ) , preimages_to_holder_htlcs ! ( preimages[ 0 ..3 ] ) ) . unwrap ( ) ;
2618
2626
secret[ 0 ..32 ] . clone_from_slice ( & hex:: decode ( "27cddaa5624534cb6cb9d7da077cf2b22ab21e9b506fd4998a51d54502e99116" ) . unwrap ( ) ) ;
2619
2627
monitor. provide_secret ( 281474976710652 , secret. clone ( ) ) . unwrap ( ) ;
2620
2628
assert_eq ! ( monitor. payment_preimages. len( ) , 5 ) ;
0 commit comments