@@ -738,7 +738,7 @@ pub struct ChannelMonitor<ChanSigner: ChannelKeys> {
738
738
// various monitors for one channel being out of sync, and us broadcasting a local
739
739
// transaction for which we have deleted claim information on some watchtowers.
740
740
prev_local_signed_commitment_tx : Option < LocalSignedTx > ,
741
- current_local_signed_commitment_tx : Option < LocalSignedTx > ,
741
+ current_local_commitment_tx : LocalSignedTx ,
742
742
743
743
// Used just for ChannelManager to make sure it has the latest channel data during
744
744
// deserialization
@@ -809,7 +809,7 @@ impl<ChanSigner: ChannelKeys> PartialEq for ChannelMonitor<ChanSigner> {
809
809
self . prev_local_signed_commitment_tx != other. prev_local_signed_commitment_tx ||
810
810
self . current_remote_commitment_number != other. current_remote_commitment_number ||
811
811
self . current_local_commitment_number != other. current_local_commitment_number ||
812
- self . current_local_signed_commitment_tx != other. current_local_signed_commitment_tx ||
812
+ self . current_local_commitment_tx != other. current_local_commitment_tx ||
813
813
self . payment_preimages != other. payment_preimages ||
814
814
self . pending_htlcs_updated != other. pending_htlcs_updated ||
815
815
self . pending_events . len ( ) != other. pending_events . len ( ) || // We trust events to round-trip properly
@@ -963,12 +963,7 @@ impl<ChanSigner: ChannelKeys + Writeable> ChannelMonitor<ChanSigner> {
963
963
writer. write_all ( & [ 0 ; 1 ] ) ?;
964
964
}
965
965
966
- if let Some ( ref cur_local_tx) = self . current_local_signed_commitment_tx {
967
- writer. write_all ( & [ 1 ; 1 ] ) ?;
968
- serialize_local_tx ! ( cur_local_tx) ;
969
- } else {
970
- writer. write_all ( & [ 0 ; 1 ] ) ?;
971
- }
966
+ serialize_local_tx ! ( self . current_local_commitment_tx) ;
972
967
973
968
writer. write_all ( & byte_utils:: be48_to_array ( self . current_remote_commitment_number ) ) ?;
974
969
writer. write_all ( & byte_utils:: be48_to_array ( self . current_local_commitment_number ) ) ?;
@@ -1031,12 +1026,34 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
1031
1026
their_htlc_base_key : & PublicKey , their_delayed_payment_base_key : & PublicKey ,
1032
1027
their_to_self_delay : u16 , funding_redeemscript : Script , channel_value_satoshis : u64 ,
1033
1028
commitment_transaction_number_obscure_factor : u64 ,
1029
+ initial_local_commitment_tx : LocalCommitmentTransaction ,
1034
1030
logger : Arc < Logger > ) -> ChannelMonitor < ChanSigner > {
1035
1031
1036
1032
assert ! ( commitment_transaction_number_obscure_factor <= ( 1 << 48 ) ) ;
1037
1033
let our_channel_close_key_hash = Hash160 :: hash ( & shutdown_pubkey. serialize ( ) ) ;
1038
1034
let shutdown_script = Builder :: new ( ) . push_opcode ( opcodes:: all:: OP_PUSHBYTES_0 ) . push_slice ( & our_channel_close_key_hash[ ..] ) . into_script ( ) ;
1039
1035
1036
+ let mut onchain_tx_handler = OnchainTxHandler :: new ( destination_script. clone ( ) , keys. clone ( ) , their_to_self_delay, logger. clone ( ) ) ;
1037
+
1038
+ let local_tx_sequence = initial_local_commitment_tx. without_valid_witness ( ) . input [ 0 ] . sequence as u64 ;
1039
+ let local_tx_locktime = initial_local_commitment_tx. without_valid_witness ( ) . lock_time as u64 ;
1040
+ let local_commitment_tx = LocalSignedTx {
1041
+ txid : initial_local_commitment_tx. txid ( ) ,
1042
+ revocation_key : initial_local_commitment_tx. local_keys . revocation_key ,
1043
+ a_htlc_key : initial_local_commitment_tx. local_keys . a_htlc_key ,
1044
+ b_htlc_key : initial_local_commitment_tx. local_keys . b_htlc_key ,
1045
+ delayed_payment_key : initial_local_commitment_tx. local_keys . a_delayed_payment_key ,
1046
+ per_commitment_point : initial_local_commitment_tx. local_keys . per_commitment_point ,
1047
+ feerate_per_kw : initial_local_commitment_tx. feerate_per_kw ,
1048
+ htlc_outputs : Vec :: new ( ) , // There are never any HTLCs in the initial commitment transactions
1049
+ } ;
1050
+ // Returning a monitor error before updating tracking points means in case of using
1051
+ // a concurrent watchtower implementation for same channel, if this one doesn't
1052
+ // reject update as we do, you MAY have the latest local valid commitment tx onchain
1053
+ // for which you want to spend outputs. We're NOT robust again this scenario right
1054
+ // now but we should consider it later.
1055
+ onchain_tx_handler. provide_latest_local_tx ( initial_local_commitment_tx) . unwrap ( ) ;
1056
+
1040
1057
ChannelMonitor {
1041
1058
latest_update_id : 0 ,
1042
1059
commitment_transaction_number_obscure_factor,
@@ -1046,14 +1063,14 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
1046
1063
broadcasted_remote_payment_script : None ,
1047
1064
shutdown_script,
1048
1065
1049
- keys : keys . clone ( ) ,
1066
+ keys,
1050
1067
funding_info,
1051
1068
current_remote_commitment_txid : None ,
1052
1069
prev_remote_commitment_txid : None ,
1053
1070
1054
1071
their_htlc_base_key : their_htlc_base_key. clone ( ) ,
1055
1072
their_delayed_payment_base_key : their_delayed_payment_base_key. clone ( ) ,
1056
- funding_redeemscript : funding_redeemscript . clone ( ) ,
1073
+ funding_redeemscript,
1057
1074
channel_value_satoshis : channel_value_satoshis,
1058
1075
their_cur_revocation_points : None ,
1059
1076
@@ -1066,9 +1083,9 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
1066
1083
remote_hash_commitment_number : HashMap :: new ( ) ,
1067
1084
1068
1085
prev_local_signed_commitment_tx : None ,
1069
- current_local_signed_commitment_tx : None ,
1086
+ current_local_commitment_tx : local_commitment_tx ,
1070
1087
current_remote_commitment_number : 1 << 48 ,
1071
- current_local_commitment_number : 0xffff_ffff_ffff ,
1088
+ current_local_commitment_number : 0xffff_ffff_ffff - ( ( ( ( local_tx_sequence & 0xffffff ) << 3 * 8 ) | ( local_tx_locktime as u64 & 0xffffff ) ) ^ commitment_transaction_number_obscure_factor ) ,
1072
1089
1073
1090
payment_preimages : HashMap :: new ( ) ,
1074
1091
pending_htlcs_updated : Vec :: new ( ) ,
@@ -1077,7 +1094,7 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
1077
1094
onchain_events_waiting_threshold_conf : HashMap :: new ( ) ,
1078
1095
outputs_to_watch : HashMap :: new ( ) ,
1079
1096
1080
- onchain_tx_handler : OnchainTxHandler :: new ( destination_script . clone ( ) , keys , their_to_self_delay , logger . clone ( ) ) ,
1097
+ onchain_tx_handler,
1081
1098
1082
1099
lockdown_from_offchain : false ,
1083
1100
@@ -1104,13 +1121,13 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
1104
1121
}
1105
1122
1106
1123
if !self . payment_preimages . is_empty ( ) {
1107
- let local_signed_commitment_tx = self . current_local_signed_commitment_tx . as_ref ( ) . expect ( "Channel needs at least an initial commitment tx !" ) ;
1124
+ let cur_local_signed_commitment_tx = & self . current_local_commitment_tx ;
1108
1125
let prev_local_signed_commitment_tx = self . prev_local_signed_commitment_tx . as_ref ( ) ;
1109
1126
let min_idx = self . get_min_seen_secret ( ) ;
1110
1127
let remote_hash_commitment_number = & mut self . remote_hash_commitment_number ;
1111
1128
1112
1129
self . payment_preimages . retain ( |& k, _| {
1113
- for & ( ref htlc, _, _) in & local_signed_commitment_tx . htlc_outputs {
1130
+ for & ( ref htlc, _, _) in cur_local_signed_commitment_tx . htlc_outputs . iter ( ) {
1114
1131
if k == htlc. payment_hash {
1115
1132
return true
1116
1133
}
@@ -1199,7 +1216,7 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
1199
1216
let txid = commitment_tx. txid ( ) ;
1200
1217
let sequence = commitment_tx. without_valid_witness ( ) . input [ 0 ] . sequence as u64 ;
1201
1218
let locktime = commitment_tx. without_valid_witness ( ) . lock_time as u64 ;
1202
- let new_local_signed_commitment_tx = LocalSignedTx {
1219
+ let mut new_local_commitment_tx = LocalSignedTx {
1203
1220
txid,
1204
1221
revocation_key : commitment_tx. local_keys . revocation_key ,
1205
1222
a_htlc_key : commitment_tx. local_keys . a_htlc_key ,
@@ -1218,8 +1235,8 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
1218
1235
return Err ( MonitorUpdateError ( "Local commitment signed has already been signed, no further update of LOCAL commitment transaction is allowed" ) ) ;
1219
1236
}
1220
1237
self . current_local_commitment_number = 0xffff_ffff_ffff - ( ( ( ( sequence & 0xffffff ) << 3 * 8 ) | ( locktime as u64 & 0xffffff ) ) ^ self . commitment_transaction_number_obscure_factor ) ;
1221
- self . prev_local_signed_commitment_tx = self . current_local_signed_commitment_tx . take ( ) ;
1222
- self . current_local_signed_commitment_tx = Some ( new_local_signed_commitment_tx ) ;
1238
+ mem :: swap ( & mut new_local_commitment_tx , & mut self . current_local_commitment_tx ) ;
1239
+ self . prev_local_signed_commitment_tx = Some ( new_local_commitment_tx ) ;
1223
1240
Ok ( ( ) )
1224
1241
}
1225
1242
@@ -1676,15 +1693,12 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
1676
1693
// HTLCs set may differ between last and previous local commitment txn, in case of one them hitting chain, ensure we cancel all HTLCs backward
1677
1694
let mut is_local_tx = false ;
1678
1695
1679
- if let & Some ( ref local_tx) = & self . current_local_signed_commitment_tx {
1680
- if local_tx. txid == commitment_txid {
1681
- is_local_tx = true ;
1682
- log_trace ! ( self , "Got latest local commitment tx broadcast, searching for available HTLCs to claim" ) ;
1683
- let mut res = self . broadcast_by_local_state ( tx, local_tx) ;
1684
- append_onchain_update ! ( res) ;
1685
- }
1686
- }
1687
- if let & Some ( ref local_tx) = & self . prev_local_signed_commitment_tx {
1696
+ if self . current_local_commitment_tx . txid == commitment_txid {
1697
+ is_local_tx = true ;
1698
+ log_trace ! ( self , "Got latest local commitment tx broadcast, searching for available HTLCs to claim" ) ;
1699
+ let mut res = self . broadcast_by_local_state ( tx, & self . current_local_commitment_tx ) ;
1700
+ append_onchain_update ! ( res) ;
1701
+ } else if let & Some ( ref local_tx) = & self . prev_local_signed_commitment_tx {
1688
1702
if local_tx. txid == commitment_txid {
1689
1703
is_local_tx = true ;
1690
1704
log_trace ! ( self , "Got previous local commitment tx broadcast, searching for available HTLCs to claim" ) ;
@@ -1706,9 +1720,7 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
1706
1720
}
1707
1721
1708
1722
if is_local_tx {
1709
- if let & Some ( ref local_tx) = & self . current_local_signed_commitment_tx {
1710
- fail_dust_htlcs_after_threshold_conf ! ( local_tx) ;
1711
- }
1723
+ fail_dust_htlcs_after_threshold_conf ! ( self . current_local_commitment_tx) ;
1712
1724
if let & Some ( ref local_tx) = & self . prev_local_signed_commitment_tx {
1713
1725
fail_dust_htlcs_after_threshold_conf ! ( local_tx) ;
1714
1726
}
@@ -1731,18 +1743,16 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
1731
1743
if let Some ( commitment_tx) = self . onchain_tx_handler . get_fully_signed_local_tx ( ) {
1732
1744
let txid = commitment_tx. txid ( ) ;
1733
1745
let mut res = vec ! [ commitment_tx] ;
1734
- if let & Some ( ref local_tx) = & self . current_local_signed_commitment_tx {
1735
- for htlc in local_tx. htlc_outputs . iter ( ) {
1736
- if let Some ( htlc_index) = htlc. 0 . transaction_output_index {
1737
- let preimage = if let Some ( preimage) = self . payment_preimages . get ( & htlc. 0 . payment_hash ) { Some ( * preimage) } else { None } ;
1738
- if let Some ( htlc_tx) = self . onchain_tx_handler . get_fully_signed_htlc_tx ( txid, htlc_index, preimage) {
1739
- res. push ( htlc_tx) ;
1740
- }
1746
+ for htlc in self . current_local_commitment_tx . htlc_outputs . iter ( ) {
1747
+ if let Some ( htlc_index) = htlc. 0 . transaction_output_index {
1748
+ let preimage = if let Some ( preimage) = self . payment_preimages . get ( & htlc. 0 . payment_hash ) { Some ( * preimage) } else { None } ;
1749
+ if let Some ( htlc_tx) = self . onchain_tx_handler . get_fully_signed_htlc_tx ( txid, htlc_index, preimage) {
1750
+ res. push ( htlc_tx) ;
1741
1751
}
1742
1752
}
1743
- // We throw away the generated waiting_first_conf data as we aren't (yet) confirmed and we don't actually know what the caller wants to do.
1744
- // The data will be re-generated and tracked in check_spend_local_transaction if we get a confirmation.
1745
1753
}
1754
+ // We throw away the generated waiting_first_conf data as we aren't (yet) confirmed and we don't actually know what the caller wants to do.
1755
+ // The data will be re-generated and tracked in check_spend_local_transaction if we get a confirmation.
1746
1756
return res
1747
1757
}
1748
1758
Vec :: new ( )
@@ -1757,13 +1767,11 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
1757
1767
if let Some ( commitment_tx) = self . onchain_tx_handler . get_fully_signed_copy_local_tx ( ) {
1758
1768
let txid = commitment_tx. txid ( ) ;
1759
1769
let mut res = vec ! [ commitment_tx] ;
1760
- if let & Some ( ref local_tx) = & self . current_local_signed_commitment_tx {
1761
- for htlc in local_tx. htlc_outputs . iter ( ) {
1762
- if let Some ( htlc_index) = htlc. 0 . transaction_output_index {
1763
- let preimage = if let Some ( preimage) = self . payment_preimages . get ( & htlc. 0 . payment_hash ) { Some ( * preimage) } else { None } ;
1764
- if let Some ( htlc_tx) = self . onchain_tx_handler . get_fully_signed_htlc_tx ( txid, htlc_index, preimage) {
1765
- res. push ( htlc_tx) ;
1766
- }
1770
+ for htlc in self . current_local_commitment_tx . htlc_outputs . iter ( ) {
1771
+ if let Some ( htlc_index) = htlc. 0 . transaction_output_index {
1772
+ let preimage = if let Some ( preimage) = self . payment_preimages . get ( & htlc. 0 . payment_hash ) { Some ( * preimage) } else { None } ;
1773
+ if let Some ( htlc_tx) = self . onchain_tx_handler . get_fully_signed_htlc_tx ( txid, htlc_index, preimage) {
1774
+ res. push ( htlc_tx) ;
1767
1775
}
1768
1776
}
1769
1777
}
@@ -1832,21 +1840,17 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
1832
1840
1833
1841
self . is_paying_spendable_output ( & tx, height) ;
1834
1842
}
1835
- let should_broadcast = if let Some ( _) = self . current_local_signed_commitment_tx {
1836
- self . would_broadcast_at_height ( height)
1837
- } else { false } ;
1843
+ let should_broadcast = self . would_broadcast_at_height ( height) ;
1838
1844
if should_broadcast {
1839
1845
claimable_outpoints. push ( ClaimRequest { absolute_timelock : height, aggregable : false , outpoint : BitcoinOutPoint { txid : self . funding_info . 0 . txid . clone ( ) , vout : self . funding_info . 0 . index as u32 } , witness_data : InputMaterial :: Funding { channel_value : self . channel_value_satoshis } } ) ;
1840
1846
}
1841
- if let Some ( ref cur_local_tx) = self . current_local_signed_commitment_tx {
1842
- if should_broadcast {
1843
- if let Some ( commitment_tx) = self . onchain_tx_handler . get_fully_signed_local_tx ( ) {
1844
- let ( mut new_outpoints, new_outputs, _) = self . broadcast_by_local_state ( & commitment_tx, cur_local_tx) ;
1845
- if !new_outputs. is_empty ( ) {
1846
- watch_outputs. push ( ( cur_local_tx. txid . clone ( ) , new_outputs) ) ;
1847
- }
1848
- claimable_outpoints. append ( & mut new_outpoints) ;
1847
+ if should_broadcast {
1848
+ if let Some ( commitment_tx) = self . onchain_tx_handler . get_fully_signed_local_tx ( ) {
1849
+ let ( mut new_outpoints, new_outputs, _) = self . broadcast_by_local_state ( & commitment_tx, & self . current_local_commitment_tx ) ;
1850
+ if !new_outputs. is_empty ( ) {
1851
+ watch_outputs. push ( ( self . current_local_commitment_tx . txid . clone ( ) , new_outputs) ) ;
1849
1852
}
1853
+ claimable_outpoints. append ( & mut new_outpoints) ;
1850
1854
}
1851
1855
}
1852
1856
if let Some ( events) = self . onchain_events_waiting_threshold_conf . remove ( & height) {
@@ -1942,9 +1946,7 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
1942
1946
}
1943
1947
}
1944
1948
1945
- if let Some ( ref cur_local_tx) = self . current_local_signed_commitment_tx {
1946
- scan_commitment ! ( cur_local_tx. htlc_outputs. iter( ) . map( |& ( ref a, _, _) | a) , true ) ;
1947
- }
1949
+ scan_commitment ! ( self . current_local_commitment_tx. htlc_outputs. iter( ) . map( |& ( ref a, _, _) | a) , true ) ;
1948
1950
1949
1951
if let Some ( ref txid) = self . current_remote_commitment_txid {
1950
1952
if let Some ( ref htlc_outputs) = self . remote_claimable_outpoints . get ( txid) {
@@ -2035,11 +2037,9 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
2035
2037
}
2036
2038
}
2037
2039
2038
- if let Some ( ref current_local_signed_commitment_tx) = self . current_local_signed_commitment_tx {
2039
- if input. previous_output . txid == current_local_signed_commitment_tx. txid {
2040
- scan_commitment ! ( current_local_signed_commitment_tx. htlc_outputs. iter( ) . map( |& ( ref a, _, ref b) | ( a, b. as_ref( ) ) ) ,
2041
- "our latest local commitment tx" , true ) ;
2042
- }
2040
+ if input. previous_output . txid == self . current_local_commitment_tx . txid {
2041
+ scan_commitment ! ( self . current_local_commitment_tx. htlc_outputs. iter( ) . map( |& ( ref a, _, ref b) | ( a, b. as_ref( ) ) ) ,
2042
+ "our latest local commitment tx" , true ) ;
2043
2043
}
2044
2044
if let Some ( ref prev_local_signed_commitment_tx) = self . prev_local_signed_commitment_tx {
2045
2045
if input. previous_output . txid == prev_local_signed_commitment_tx. txid {
@@ -2324,14 +2324,7 @@ impl<ChanSigner: ChannelKeys + Readable> ReadableArgs<Arc<Logger>> for (Sha256dH
2324
2324
} ,
2325
2325
_ => return Err ( DecodeError :: InvalidValue ) ,
2326
2326
} ;
2327
-
2328
- let current_local_signed_commitment_tx = match <u8 as Readable >:: read ( reader) ? {
2329
- 0 => None ,
2330
- 1 => {
2331
- Some ( read_local_tx ! ( ) )
2332
- } ,
2333
- _ => return Err ( DecodeError :: InvalidValue ) ,
2334
- } ;
2327
+ let current_local_commitment_tx = read_local_tx ! ( ) ;
2335
2328
2336
2329
let current_remote_commitment_number = <U48 as Readable >:: read ( reader) ?. 0 ;
2337
2330
let current_local_commitment_number = <U48 as Readable >:: read ( reader) ?. 0 ;
@@ -2436,7 +2429,7 @@ impl<ChanSigner: ChannelKeys + Readable> ReadableArgs<Arc<Logger>> for (Sha256dH
2436
2429
remote_hash_commitment_number,
2437
2430
2438
2431
prev_local_signed_commitment_tx,
2439
- current_local_signed_commitment_tx ,
2432
+ current_local_commitment_tx ,
2440
2433
current_remote_commitment_number,
2441
2434
current_local_commitment_number,
2442
2435
@@ -2555,7 +2548,7 @@ mod tests {
2555
2548
( OutPoint { txid : Sha256dHash :: from_slice ( & [ 43 ; 32 ] ) . unwrap ( ) , index : 0 } , Script :: new ( ) ) ,
2556
2549
& PublicKey :: from_secret_key ( & secp_ctx, & SecretKey :: from_slice ( & [ 44 ; 32 ] ) . unwrap ( ) ) ,
2557
2550
& PublicKey :: from_secret_key ( & secp_ctx, & SecretKey :: from_slice ( & [ 45 ; 32 ] ) . unwrap ( ) ) ,
2558
- 10 , Script :: new ( ) , 46 , 0 , logger. clone ( ) ) ;
2551
+ 10 , Script :: new ( ) , 46 , 0 , LocalCommitmentTransaction :: dummy ( ) , logger. clone ( ) ) ;
2559
2552
2560
2553
monitor. provide_latest_local_commitment_tx_info ( LocalCommitmentTransaction :: dummy ( ) , preimages_to_local_htlcs ! ( preimages[ 0 ..10 ] ) ) . unwrap ( ) ;
2561
2554
monitor. provide_latest_remote_commitment_tx_info ( & dummy_tx, preimages_slice_to_htlc_outputs ! ( preimages[ 5 ..15 ] ) , 281474976710655 , dummy_key) ;
0 commit comments