@@ -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 } ;
42
+ use ln:: chan_utils:: { CounterpartyCommitmentSecrets , HTLCOutputInCommitment , HTLCType , ChannelTransactionParameters , HolderCommitmentTransaction } ;
43
43
use ln:: channelmanager:: { HTLCSource , PaymentPreimage , PaymentHash } ;
44
44
use ln:: onchaintx:: { OnchainTxHandler , InputDescriptors } ;
45
45
use chain:: chaininterface:: { BroadcasterInterface , FeeEstimator } ;
@@ -489,7 +489,7 @@ const MIN_SERIALIZATION_VERSION: u8 = 1;
489
489
#[ derive( Clone ) ]
490
490
pub ( crate ) enum ChannelMonitorUpdateStep {
491
491
LatestHolderCommitmentTXInfo {
492
- commitment_tx : HolderCommitmentTransaction ,
492
+ commitment_info : HolderCommitmentTransaction ,
493
493
htlc_outputs : Vec < ( HTLCOutputInCommitment , Option < Signature > , Option < HTLCSource > ) > ,
494
494
} ,
495
495
LatestCounterpartyCommitmentTXInfo {
@@ -517,9 +517,9 @@ pub(crate) enum ChannelMonitorUpdateStep {
517
517
impl Writeable for ChannelMonitorUpdateStep {
518
518
fn write < W : Writer > ( & self , w : & mut W ) -> Result < ( ) , :: std:: io:: Error > {
519
519
match self {
520
- & ChannelMonitorUpdateStep :: LatestHolderCommitmentTXInfo { ref commitment_tx , ref htlc_outputs } => {
520
+ & ChannelMonitorUpdateStep :: LatestHolderCommitmentTXInfo { ref commitment_info , ref htlc_outputs } => {
521
521
0u8 . write ( w) ?;
522
- commitment_tx . write ( w) ?;
522
+ commitment_info . write ( w) ?;
523
523
( htlc_outputs. len ( ) as u64 ) . write ( w) ?;
524
524
for & ( ref output, ref signature, ref source) in htlc_outputs. iter ( ) {
525
525
output. write ( w) ?;
@@ -560,7 +560,7 @@ impl Readable for ChannelMonitorUpdateStep {
560
560
match Readable :: read ( r) ? {
561
561
0u8 => {
562
562
Ok ( ChannelMonitorUpdateStep :: LatestHolderCommitmentTXInfo {
563
- commitment_tx : Readable :: read ( r) ?,
563
+ commitment_info : Readable :: read ( r) ?,
564
564
htlc_outputs : {
565
565
let len: u64 = Readable :: read ( r) ?;
566
566
let mut res = Vec :: new ( ) ;
@@ -948,35 +948,47 @@ impl<ChanSigner: ChannelKeys + Writeable> ChannelMonitor<ChanSigner> {
948
948
949
949
impl < ChanSigner : ChannelKeys > ChannelMonitor < ChanSigner > {
950
950
pub ( crate ) fn new ( keys : ChanSigner , shutdown_pubkey : & PublicKey ,
951
- on_counterparty_tx_csv : u16 , destination_script : & Script , funding_info : ( OutPoint , Script ) ,
952
- counterparty_htlc_base_key : & PublicKey , counterparty_delayed_payment_base_key : & PublicKey ,
953
- on_holder_tx_csv : u16 , funding_redeemscript : Script , channel_value_satoshis : u64 ,
954
- commitment_transaction_number_obscure_factor : u64 ,
955
- initial_holder_commitment_tx : HolderCommitmentTransaction ) -> ChannelMonitor < ChanSigner > {
951
+ on_counterparty_tx_csv : u16 , destination_script : & Script , funding_info : ( OutPoint , Script ) ,
952
+ channel_parameters : & ChannelTransactionParameters ,
953
+ funding_redeemscript : Script , channel_value_satoshis : u64 ,
954
+ commitment_transaction_number_obscure_factor : u64 ,
955
+ initial_holder_commitment_info : HolderCommitmentTransaction ) -> ChannelMonitor < ChanSigner > {
956
956
957
957
assert ! ( commitment_transaction_number_obscure_factor <= ( 1 << 48 ) ) ;
958
958
let our_channel_close_key_hash = WPubkeyHash :: hash ( & shutdown_pubkey. serialize ( ) ) ;
959
959
let shutdown_script = Builder :: new ( ) . push_opcode ( opcodes:: all:: OP_PUSHBYTES_0 ) . push_slice ( & our_channel_close_key_hash[ ..] ) . into_script ( ) ;
960
960
let payment_key_hash = WPubkeyHash :: hash ( & keys. pubkeys ( ) . payment_point . serialize ( ) ) ;
961
961
let counterparty_payment_script = Builder :: new ( ) . push_opcode ( opcodes:: all:: OP_PUSHBYTES_0 ) . push_slice ( & payment_key_hash[ ..] ) . into_script ( ) ;
962
962
963
- let counterparty_tx_cache = CounterpartyCommitmentTransaction { counterparty_delayed_payment_base_key : * counterparty_delayed_payment_base_key, counterparty_htlc_base_key : * counterparty_htlc_base_key, on_counterparty_tx_csv, per_htlc : HashMap :: new ( ) } ;
964
-
965
- let mut onchain_tx_handler = OnchainTxHandler :: new ( destination_script. clone ( ) , keys. clone ( ) , on_holder_tx_csv) ;
966
-
967
- let holder_tx_sequence = initial_holder_commitment_tx. unsigned_tx . input [ 0 ] . sequence as u64 ;
968
- let holder_tx_locktime = initial_holder_commitment_tx. unsigned_tx . lock_time as u64 ;
969
- let holder_commitment_tx = HolderSignedTx {
970
- txid : initial_holder_commitment_tx. txid ( ) ,
971
- revocation_key : initial_holder_commitment_tx. keys . revocation_key ,
972
- a_htlc_key : initial_holder_commitment_tx. keys . broadcaster_htlc_key ,
973
- b_htlc_key : initial_holder_commitment_tx. keys . countersignatory_htlc_key ,
974
- delayed_payment_key : initial_holder_commitment_tx. keys . broadcaster_delayed_payment_key ,
975
- per_commitment_point : initial_holder_commitment_tx. keys . per_commitment_point ,
976
- feerate_per_kw : initial_holder_commitment_tx. feerate_per_kw ,
977
- htlc_outputs : Vec :: new ( ) , // There are never any HTLCs in the initial commitment transactions
963
+ let counterparty_delayed_payment_base_key = channel_parameters. counterparty_pubkeys . delayed_payment_basepoint ;
964
+ let counterparty_htlc_base_key = channel_parameters. counterparty_pubkeys . htlc_basepoint ;
965
+ let counterparty_tx_cache = CounterpartyCommitmentTransaction { counterparty_delayed_payment_base_key, counterparty_htlc_base_key, on_counterparty_tx_csv, per_htlc : HashMap :: new ( ) } ;
966
+
967
+ let mut onchain_tx_handler = OnchainTxHandler :: new ( destination_script. clone ( ) , keys. clone ( ) , channel_parameters. clone ( ) ) ;
968
+
969
+ let secp_ctx = Secp256k1 :: new ( ) ;
970
+
971
+ let directed_channel_parameters = channel_parameters. to_directed ( true ) ;
972
+ let txid = initial_holder_commitment_info. calculate_txid ( & directed_channel_parameters, & secp_ctx) ;
973
+
974
+ // block for Rust 1.34 compat
975
+ let ( holder_commitment_tx, current_holder_commitment_number) = {
976
+ let info = & initial_holder_commitment_info. info ;
977
+ let tx_keys = & info. keys ;
978
+ let current_holder_commitment_number = info. commitment_number ;
979
+ let holder_commitment_tx = HolderSignedTx {
980
+ txid,
981
+ revocation_key : tx_keys. revocation_key ,
982
+ a_htlc_key : tx_keys. broadcaster_htlc_key ,
983
+ b_htlc_key : tx_keys. countersignatory_htlc_key ,
984
+ delayed_payment_key : tx_keys. broadcaster_delayed_payment_key ,
985
+ per_commitment_point : tx_keys. per_commitment_point ,
986
+ feerate_per_kw : info. feerate_per_kw ,
987
+ htlc_outputs : Vec :: new ( ) , // There are never any HTLCs in the initial commitment transactions
988
+ } ;
989
+ ( holder_commitment_tx, current_holder_commitment_number)
978
990
} ;
979
- onchain_tx_handler. provide_latest_holder_tx ( initial_holder_commitment_tx ) ;
991
+ onchain_tx_handler. provide_latest_holder_tx ( initial_holder_commitment_info ) ;
980
992
981
993
let mut outputs_to_watch = HashMap :: new ( ) ;
982
994
outputs_to_watch. insert ( funding_info. 0 . txid , vec ! [ ( funding_info. 0 . index as u32 , funding_info. 1 . clone( ) ) ] ) ;
@@ -1000,7 +1012,7 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
1000
1012
channel_value_satoshis,
1001
1013
their_cur_revocation_points : None ,
1002
1014
1003
- on_holder_tx_csv,
1015
+ on_holder_tx_csv : channel_parameters . counterparty_selected_contest_delay ,
1004
1016
1005
1017
commitment_secrets : CounterpartyCommitmentSecrets :: new ( ) ,
1006
1018
counterparty_claimable_outpoints : HashMap :: new ( ) ,
@@ -1010,7 +1022,7 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
1010
1022
prev_holder_signed_commitment_tx : None ,
1011
1023
current_holder_commitment_tx : holder_commitment_tx,
1012
1024
current_counterparty_commitment_number : 1 << 48 ,
1013
- current_holder_commitment_number : 0xffff_ffff_ffff - ( ( ( ( holder_tx_sequence & 0xffffff ) << 3 * 8 ) | ( holder_tx_locktime as u64 & 0xffffff ) ) ^ commitment_transaction_number_obscure_factor ) ,
1025
+ current_holder_commitment_number,
1014
1026
1015
1027
payment_preimages : HashMap :: new ( ) ,
1016
1028
pending_monitor_events : Vec :: new ( ) ,
@@ -1025,7 +1037,7 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
1025
1037
holder_tx_signed : false ,
1026
1038
1027
1039
last_block_hash : Default :: default ( ) ,
1028
- secp_ctx : Secp256k1 :: new ( ) ,
1040
+ secp_ctx,
1029
1041
}
1030
1042
}
1031
1043
@@ -1133,22 +1145,26 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
1133
1145
/// is important that any clones of this channel monitor (including remote clones) by kept
1134
1146
/// up-to-date as our holder commitment transaction is updated.
1135
1147
/// Panics if set_on_holder_tx_csv has never been called.
1136
- fn provide_latest_holder_commitment_tx_info ( & mut self , commitment_tx : HolderCommitmentTransaction , htlc_outputs : Vec < ( HTLCOutputInCommitment , Option < Signature > , Option < HTLCSource > ) > ) -> Result < ( ) , MonitorUpdateError > {
1137
- let txid = commitment_tx. txid ( ) ;
1138
- let sequence = commitment_tx. unsigned_tx . input [ 0 ] . sequence as u64 ;
1139
- let locktime = commitment_tx. unsigned_tx . lock_time as u64 ;
1140
- let mut new_holder_commitment_tx = HolderSignedTx {
1141
- txid,
1142
- revocation_key : commitment_tx. keys . revocation_key ,
1143
- a_htlc_key : commitment_tx. keys . broadcaster_htlc_key ,
1144
- b_htlc_key : commitment_tx. keys . countersignatory_htlc_key ,
1145
- delayed_payment_key : commitment_tx. keys . broadcaster_delayed_payment_key ,
1146
- per_commitment_point : commitment_tx. keys . per_commitment_point ,
1147
- feerate_per_kw : commitment_tx. feerate_per_kw ,
1148
- htlc_outputs,
1148
+ fn provide_latest_holder_commitment_tx_info ( & mut self , commitment_info : HolderCommitmentTransaction , htlc_outputs : Vec < ( HTLCOutputInCommitment , Option < Signature > , Option < HTLCSource > ) > ) -> Result < ( ) , MonitorUpdateError > {
1149
+ let txid = commitment_info. calculate_txid ( & self . onchain_tx_handler . channel_transaction_parameters . to_directed ( true ) , & self . secp_ctx ) ;
1150
+
1151
+ // block for Rust 1.34 compat
1152
+ let mut new_holder_commitment_tx = {
1153
+ let info = & commitment_info. info ;
1154
+ let tx_keys = & info. keys ;
1155
+ self . current_holder_commitment_number = info. commitment_number ;
1156
+ HolderSignedTx {
1157
+ txid,
1158
+ revocation_key : tx_keys. revocation_key ,
1159
+ a_htlc_key : tx_keys. broadcaster_htlc_key ,
1160
+ b_htlc_key : tx_keys. countersignatory_htlc_key ,
1161
+ delayed_payment_key : tx_keys. broadcaster_delayed_payment_key ,
1162
+ per_commitment_point : tx_keys. per_commitment_point ,
1163
+ feerate_per_kw : info. feerate_per_kw ,
1164
+ htlc_outputs,
1165
+ }
1149
1166
} ;
1150
- self . onchain_tx_handler . provide_latest_holder_tx ( commitment_tx) ;
1151
- self . current_holder_commitment_number = 0xffff_ffff_ffff - ( ( ( ( sequence & 0xffffff ) << 3 * 8 ) | ( locktime as u64 & 0xffffff ) ) ^ self . commitment_transaction_number_obscure_factor ) ;
1167
+ self . onchain_tx_handler . provide_latest_holder_tx ( commitment_info) ;
1152
1168
mem:: swap ( & mut new_holder_commitment_tx, & mut self . current_holder_commitment_tx ) ;
1153
1169
self . prev_holder_signed_commitment_tx = Some ( new_holder_commitment_tx) ;
1154
1170
if self . holder_tx_signed {
@@ -1236,10 +1252,10 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
1236
1252
}
1237
1253
for update in updates. updates . iter ( ) {
1238
1254
match update {
1239
- ChannelMonitorUpdateStep :: LatestHolderCommitmentTXInfo { commitment_tx , htlc_outputs } => {
1255
+ ChannelMonitorUpdateStep :: LatestHolderCommitmentTXInfo { commitment_info , htlc_outputs } => {
1240
1256
log_trace ! ( logger, "Updating ChannelMonitor with latest holder commitment transaction info" ) ;
1241
1257
if self . lockdown_from_offchain { panic ! ( ) ; }
1242
- self . provide_latest_holder_commitment_tx_info ( commitment_tx . clone ( ) , htlc_outputs. clone ( ) ) ?
1258
+ self . provide_latest_holder_commitment_tx_info ( commitment_info . clone ( ) , htlc_outputs. clone ( ) ) ?
1243
1259
} ,
1244
1260
ChannelMonitorUpdateStep :: LatestCounterpartyCommitmentTXInfo { unsigned_commitment_tx, htlc_outputs, commitment_number, their_revocation_point } => {
1245
1261
log_trace ! ( logger, "Updating ChannelMonitor with latest counterparty commitment transaction info" ) ;
@@ -2589,7 +2605,7 @@ mod tests {
2589
2605
use ln:: channelmanager:: { PaymentPreimage , PaymentHash } ;
2590
2606
use ln:: onchaintx:: { OnchainTxHandler , InputDescriptors } ;
2591
2607
use ln:: chan_utils;
2592
- use ln:: chan_utils:: { HTLCOutputInCommitment , HolderCommitmentTransaction } ;
2608
+ use ln:: chan_utils:: { HTLCOutputInCommitment , ChannelPublicKeys , ChannelTransactionParameters , HolderCommitmentTransaction } ;
2593
2609
use util:: test_utils:: { TestLogger , TestBroadcaster , TestFeeEstimator } ;
2594
2610
use bitcoin:: secp256k1:: key:: { SecretKey , PublicKey } ;
2595
2611
use bitcoin:: secp256k1:: Secp256k1 ;
@@ -2662,14 +2678,29 @@ mod tests {
2662
2678
( 0 , 0 )
2663
2679
) ;
2664
2680
2681
+ let counterparty_pubkeys = ChannelPublicKeys {
2682
+ funding_pubkey : PublicKey :: from_secret_key ( & secp_ctx, & SecretKey :: from_slice ( & [ 44 ; 32 ] ) . unwrap ( ) ) ,
2683
+ revocation_basepoint : PublicKey :: from_secret_key ( & secp_ctx, & SecretKey :: from_slice ( & [ 45 ; 32 ] ) . unwrap ( ) ) ,
2684
+ payment_point : PublicKey :: from_secret_key ( & secp_ctx, & SecretKey :: from_slice ( & [ 46 ; 32 ] ) . unwrap ( ) ) ,
2685
+ delayed_payment_basepoint : PublicKey :: from_secret_key ( & secp_ctx, & SecretKey :: from_slice ( & [ 47 ; 32 ] ) . unwrap ( ) ) ,
2686
+ htlc_basepoint : PublicKey :: from_secret_key ( & secp_ctx, & SecretKey :: from_slice ( & [ 48 ; 32 ] ) . unwrap ( ) )
2687
+ } ;
2688
+ let channel_parameters = ChannelTransactionParameters {
2689
+ holder_pubkeys : keys. holder_channel_pubkeys . clone ( ) ,
2690
+ counterparty_pubkeys,
2691
+ holder_selected_contest_delay : 66 ,
2692
+ counterparty_selected_contest_delay : 67 ,
2693
+ funding_outpoint : Default :: default ( ) ,
2694
+ is_outbound_from_holder : true
2695
+ } ;
2665
2696
// Prune with one old state and a holder commitment tx holding a few overlaps with the
2666
2697
// old state.
2667
2698
let mut monitor = ChannelMonitor :: new ( keys,
2668
- & PublicKey :: from_secret_key ( & secp_ctx, & SecretKey :: from_slice ( & [ 42 ; 32 ] ) . unwrap ( ) ) , 0 , & Script :: new ( ) ,
2669
- ( OutPoint { txid : Txid :: from_slice ( & [ 43 ; 32 ] ) . unwrap ( ) , index : 0 } , Script :: new ( ) ) ,
2670
- & PublicKey :: from_secret_key ( & secp_ctx , & SecretKey :: from_slice ( & [ 44 ; 32 ] ) . unwrap ( ) ) ,
2671
- & PublicKey :: from_secret_key ( & secp_ctx , & SecretKey :: from_slice ( & [ 45 ; 32 ] ) . unwrap ( ) ) ,
2672
- 10 , Script :: new ( ) , 46 , 0 , HolderCommitmentTransaction :: dummy ( ) ) ;
2699
+ & PublicKey :: from_secret_key ( & secp_ctx, & SecretKey :: from_slice ( & [ 42 ; 32 ] ) . unwrap ( ) ) , 0 , & Script :: new ( ) ,
2700
+ ( OutPoint { txid : Txid :: from_slice ( & [ 43 ; 32 ] ) . unwrap ( ) , index : 0 } , Script :: new ( ) ) ,
2701
+ & channel_parameters ,
2702
+ Script :: new ( ) , 46 , 0 ,
2703
+ HolderCommitmentTransaction :: dummy ( ) ) ;
2673
2704
2674
2705
monitor. provide_latest_holder_commitment_tx_info ( HolderCommitmentTransaction :: dummy ( ) , preimages_to_holder_htlcs ! ( preimages[ 0 ..10 ] ) ) . unwrap ( ) ;
2675
2706
monitor. provide_latest_counterparty_commitment_tx_info ( & dummy_tx, preimages_slice_to_htlc_outputs ! ( preimages[ 5 ..15 ] ) , 281474976710655 , dummy_key, & logger) ;
0 commit comments