@@ -10,13 +10,14 @@ use bitcoin::util::bip143;
10
10
11
11
use bitcoin_hashes:: sha256d:: Hash as Sha256dHash ;
12
12
13
- use secp256k1:: Secp256k1 ;
13
+ use secp256k1:: { Secp256k1 , Signature } ;
14
14
use secp256k1;
15
15
16
16
use ln:: msgs:: DecodeError ;
17
17
use ln:: channelmonitor:: { ANTI_REORG_DELAY , CLTV_SHARED_CLAIM_BUFFER , InputMaterial , ClaimRequest } ;
18
+ use ln:: channelmanager:: HTLCSource ;
18
19
use ln:: chan_utils;
19
- use ln:: chan_utils:: { HTLCType , LocalCommitmentTransaction , TxCreationKeys } ;
20
+ use ln:: chan_utils:: { HTLCType , LocalCommitmentTransaction , TxCreationKeys , HTLCOutputInCommitment } ;
20
21
use chain:: chaininterface:: { FeeEstimator , BroadcasterInterface , ConfirmationTarget , MIN_RELAY_FEE_SAT_PER_1000_WEIGHT } ;
21
22
use chain:: keysinterface:: ChannelKeys ;
22
23
use util:: logger:: Logger ;
@@ -54,6 +55,7 @@ enum OnchainEvent {
54
55
struct HTLCTxCache {
55
56
local_keys : TxCreationKeys ,
56
57
feerate_per_kw : u64 ,
58
+ per_htlc : HashMap < u32 , ( HTLCOutputInCommitment , Option < Signature > ) >
57
59
}
58
60
59
61
/// Higher-level cache structure needed to re-generate bumped claim txn if needed
@@ -202,6 +204,16 @@ impl<ChanSigner: ChannelKeys + Writeable> OnchainTxHandler<ChanSigner> {
202
204
( $cache: expr) => {
203
205
$cache. local_keys. write( writer) ?;
204
206
$cache. feerate_per_kw. write( writer) ?;
207
+ writer. write_all( & byte_utils:: be64_to_array( $cache. per_htlc. len( ) as u64 ) ) ?;
208
+ for ( _, & ( ref htlc, ref sig) ) in $cache. per_htlc. iter( ) {
209
+ htlc. write( writer) ?;
210
+ if let & Some ( ref their_sig) = sig {
211
+ 1u8 . write( writer) ?;
212
+ writer. write_all( & their_sig. serialize_compact( ) ) ?;
213
+ } else {
214
+ 0u8 . write( writer) ?;
215
+ }
216
+ }
205
217
}
206
218
}
207
219
@@ -268,9 +280,21 @@ impl<ChanSigner: ChannelKeys + Readable> ReadableArgs<Arc<Logger>> for OnchainTx
268
280
{
269
281
let local_keys = Readable :: read( reader) ?;
270
282
let feerate_per_kw = Readable :: read( reader) ?;
283
+ let htlcs_count: u64 = Readable :: read( reader) ?;
284
+ let mut per_htlc = HashMap :: with_capacity( cmp:: min( htlcs_count as usize , MAX_ALLOC_SIZE / 32 ) ) ;
285
+ for _ in 0 ..htlcs_count {
286
+ let htlc: HTLCOutputInCommitment = Readable :: read( reader) ?;
287
+ let sigs = match <u8 as Readable >:: read( reader) ? {
288
+ 0 => None ,
289
+ 1 => Some ( Readable :: read( reader) ?) ,
290
+ _ => return Err ( DecodeError :: InvalidValue ) ,
291
+ } ;
292
+ per_htlc. insert( htlc. transaction_output_index. unwrap( ) , ( htlc, sigs) ) ;
293
+ }
271
294
HTLCTxCache {
272
295
local_keys,
273
296
feerate_per_kw,
297
+ per_htlc
274
298
}
275
299
}
276
300
}
@@ -821,7 +845,7 @@ impl<ChanSigner: ChannelKeys> OnchainTxHandler<ChanSigner> {
821
845
}
822
846
}
823
847
824
- pub ( super ) fn provide_latest_local_tx ( & mut self , tx : LocalCommitmentTransaction , local_keys : chan_utils:: TxCreationKeys , feerate_per_kw : u64 ) -> Result < ( ) , ( ) > {
848
+ pub ( super ) fn provide_latest_local_tx ( & mut self , tx : LocalCommitmentTransaction , local_keys : chan_utils:: TxCreationKeys , feerate_per_kw : u64 , htlc_outputs : Vec < ( HTLCOutputInCommitment , Option < Signature > , Option < HTLCSource > ) > ) -> Result < ( ) , ( ) > {
825
849
// To prevent any unsafe state discrepancy between offchain and onchain, once local
826
850
// commitment transaction has been signed due to an event (either block height for
827
851
// HTLC-timeout or channel force-closure), don't allow any further update of local
@@ -833,9 +857,16 @@ impl<ChanSigner: ChannelKeys> OnchainTxHandler<ChanSigner> {
833
857
self . prev_local_commitment = self . local_commitment . take ( ) ;
834
858
self . local_commitment = Some ( tx) ;
835
859
self . prev_htlc_cache = self . current_htlc_cache . take ( ) ;
860
+ let mut per_htlc = HashMap :: with_capacity ( htlc_outputs. len ( ) ) ;
861
+ for htlc in htlc_outputs {
862
+ if htlc. 0 . transaction_output_index . is_some ( ) { // Discard dust HTLC as we will never have to generate onchain tx for them
863
+ per_htlc. insert ( htlc. 0 . transaction_output_index . unwrap ( ) , ( htlc. 0 , htlc. 1 ) ) ;
864
+ }
865
+ }
836
866
self . current_htlc_cache = Some ( HTLCTxCache {
837
867
local_keys,
838
868
feerate_per_kw,
869
+ per_htlc
839
870
} ) ;
840
871
Ok ( ( ) )
841
872
}
0 commit comments