@@ -198,7 +198,12 @@ pub struct HTLCUpdate {
198
198
pub ( crate ) payment_preimage : Option < PaymentPreimage > ,
199
199
pub ( crate ) source : HTLCSource
200
200
}
201
- impl_writeable ! ( HTLCUpdate , 0 , { payment_hash, payment_preimage, source } ) ;
201
+ impl_writeable_tlv_based ! ( HTLCUpdate , {
202
+ ( 0 , payment_hash) ,
203
+ ( 2 , source) ,
204
+ } , {
205
+ ( 4 , payment_preimage)
206
+ } , { } ) ;
202
207
203
208
/// If an HTLC expires within this many blocks, don't try to claim it in a shared transaction,
204
209
/// instead claiming it in its own individual transaction.
@@ -264,6 +269,17 @@ struct HolderSignedTx {
264
269
feerate_per_kw : u32 ,
265
270
htlc_outputs : Vec < ( HTLCOutputInCommitment , Option < Signature > , Option < HTLCSource > ) > ,
266
271
}
272
+ impl_writeable_tlv_based ! ( HolderSignedTx , {
273
+ ( 0 , txid) ,
274
+ ( 2 , revocation_key) ,
275
+ ( 4 , a_htlc_key) ,
276
+ ( 6 , b_htlc_key) ,
277
+ ( 8 , delayed_payment_key) ,
278
+ ( 10 , per_commitment_point) ,
279
+ ( 12 , feerate_per_kw) ,
280
+ } , { } , {
281
+ ( 14 , htlc_outputs)
282
+ } ) ;
267
283
268
284
/// We use this to track counterparty commitment transactions and htlcs outputs and
269
285
/// use it to generate any justice or 2nd-stage preimage/timeout transactions.
@@ -359,6 +375,22 @@ enum OnchainEvent {
359
375
} ,
360
376
}
361
377
378
+ impl_writeable_tlv_based ! ( OnchainEventEntry , {
379
+ ( 0 , txid) ,
380
+ ( 2 , height) ,
381
+ ( 4 , event) ,
382
+ } , { } , { } ) ;
383
+
384
+ impl_writeable_tlv_based_enum ! ( OnchainEvent ,
385
+ ( 0 , HTLCUpdate ) => {
386
+ ( 0 , source) ,
387
+ ( 2 , payment_hash) ,
388
+ } , { } , { } ,
389
+ ( 1 , MaturingOutput ) => {
390
+ ( 0 , descriptor) ,
391
+ } , { } , { } ,
392
+ ; ) ;
393
+
362
394
#[ cfg_attr( any( test, feature = "fuzztarget" , feature = "_test_utils" ) , derive( PartialEq ) ) ]
363
395
#[ derive( Clone ) ]
364
396
pub ( crate ) enum ChannelMonitorUpdateStep {
@@ -388,98 +420,30 @@ pub(crate) enum ChannelMonitorUpdateStep {
388
420
} ,
389
421
}
390
422
391
- impl Writeable for ChannelMonitorUpdateStep {
392
- fn write < W : Writer > ( & self , w : & mut W ) -> Result < ( ) , :: std:: io:: Error > {
393
- match self {
394
- & ChannelMonitorUpdateStep :: LatestHolderCommitmentTXInfo { ref commitment_tx, ref htlc_outputs } => {
395
- 0u8 . write ( w) ?;
396
- commitment_tx. write ( w) ?;
397
- ( htlc_outputs. len ( ) as u64 ) . write ( w) ?;
398
- for & ( ref output, ref signature, ref source) in htlc_outputs. iter ( ) {
399
- output. write ( w) ?;
400
- signature. write ( w) ?;
401
- source. write ( w) ?;
402
- }
403
- }
404
- & ChannelMonitorUpdateStep :: LatestCounterpartyCommitmentTXInfo { commitment_txid, ref htlc_outputs, ref commitment_number, ref their_revocation_point } => {
405
- 1u8 . write ( w) ?;
406
- commitment_txid. write ( w) ?;
407
- commitment_number. write ( w) ?;
408
- their_revocation_point. write ( w) ?;
409
- ( htlc_outputs. len ( ) as u64 ) . write ( w) ?;
410
- for & ( ref output, ref source) in htlc_outputs. iter ( ) {
411
- output. write ( w) ?;
412
- source. as_ref ( ) . map ( |b| b. as_ref ( ) ) . write ( w) ?;
413
- }
414
- } ,
415
- & ChannelMonitorUpdateStep :: PaymentPreimage { ref payment_preimage } => {
416
- 2u8 . write ( w) ?;
417
- payment_preimage. write ( w) ?;
418
- } ,
419
- & ChannelMonitorUpdateStep :: CommitmentSecret { ref idx, ref secret } => {
420
- 3u8 . write ( w) ?;
421
- idx. write ( w) ?;
422
- secret. write ( w) ?;
423
- } ,
424
- & ChannelMonitorUpdateStep :: ChannelForceClosed { ref should_broadcast } => {
425
- 4u8 . write ( w) ?;
426
- should_broadcast. write ( w) ?;
427
- } ,
428
- }
429
- Ok ( ( ) )
430
- }
431
- }
432
- impl Readable for ChannelMonitorUpdateStep {
433
- fn read < R : :: std:: io:: Read > ( r : & mut R ) -> Result < Self , DecodeError > {
434
- match Readable :: read ( r) ? {
435
- 0u8 => {
436
- Ok ( ChannelMonitorUpdateStep :: LatestHolderCommitmentTXInfo {
437
- commitment_tx : Readable :: read ( r) ?,
438
- htlc_outputs : {
439
- let len: u64 = Readable :: read ( r) ?;
440
- let mut res = Vec :: new ( ) ;
441
- for _ in 0 ..len {
442
- res. push ( ( Readable :: read ( r) ?, Readable :: read ( r) ?, Readable :: read ( r) ?) ) ;
443
- }
444
- res
445
- } ,
446
- } )
447
- } ,
448
- 1u8 => {
449
- Ok ( ChannelMonitorUpdateStep :: LatestCounterpartyCommitmentTXInfo {
450
- commitment_txid : Readable :: read ( r) ?,
451
- commitment_number : Readable :: read ( r) ?,
452
- their_revocation_point : Readable :: read ( r) ?,
453
- htlc_outputs : {
454
- let len: u64 = Readable :: read ( r) ?;
455
- let mut res = Vec :: new ( ) ;
456
- for _ in 0 ..len {
457
- res. push ( ( Readable :: read ( r) ?, <Option < HTLCSource > as Readable >:: read ( r) ?. map ( |o| Box :: new ( o) ) ) ) ;
458
- }
459
- res
460
- } ,
461
- } )
462
- } ,
463
- 2u8 => {
464
- Ok ( ChannelMonitorUpdateStep :: PaymentPreimage {
465
- payment_preimage : Readable :: read ( r) ?,
466
- } )
467
- } ,
468
- 3u8 => {
469
- Ok ( ChannelMonitorUpdateStep :: CommitmentSecret {
470
- idx : Readable :: read ( r) ?,
471
- secret : Readable :: read ( r) ?,
472
- } )
473
- } ,
474
- 4u8 => {
475
- Ok ( ChannelMonitorUpdateStep :: ChannelForceClosed {
476
- should_broadcast : Readable :: read ( r) ?
477
- } )
478
- } ,
479
- _ => Err ( DecodeError :: InvalidValue ) ,
480
- }
481
- }
482
- }
423
+ impl_writeable_tlv_based_enum ! ( ChannelMonitorUpdateStep ,
424
+ ( 0 , LatestHolderCommitmentTXInfo ) => {
425
+ ( 0 , commitment_tx) ,
426
+ } , { } , {
427
+ ( 2 , htlc_outputs) ,
428
+ } ,
429
+ ( 1 , LatestCounterpartyCommitmentTXInfo ) => {
430
+ ( 0 , commitment_txid) ,
431
+ ( 2 , commitment_number) ,
432
+ ( 4 , their_revocation_point) ,
433
+ } , { } , {
434
+ ( 6 , htlc_outputs) ,
435
+ } ,
436
+ ( 2 , PaymentPreimage ) => {
437
+ ( 0 , payment_preimage) ,
438
+ } , { } , { } ,
439
+ ( 3 , CommitmentSecret ) => {
440
+ ( 0 , idx) ,
441
+ ( 2 , secret) ,
442
+ } , { } , { } ,
443
+ ( 4 , ChannelForceClosed ) => {
444
+ ( 0 , should_broadcast) ,
445
+ } , { } , { } ,
446
+ ; ) ;
483
447
484
448
/// A ChannelMonitor handles chain events (blocks connected and disconnected) and generates
485
449
/// on-chain transactions to ensure no loss of funds occurs.
@@ -754,38 +718,14 @@ impl<Signer: Sign> Writeable for ChannelMonitorImpl<Signer> {
754
718
writer. write_all ( & byte_utils:: be48_to_array ( * commitment_number) ) ?;
755
719
}
756
720
757
- macro_rules! serialize_holder_tx {
758
- ( $holder_tx: expr) => {
759
- $holder_tx. txid. write( writer) ?;
760
- writer. write_all( & $holder_tx. revocation_key. serialize( ) ) ?;
761
- writer. write_all( & $holder_tx. a_htlc_key. serialize( ) ) ?;
762
- writer. write_all( & $holder_tx. b_htlc_key. serialize( ) ) ?;
763
- writer. write_all( & $holder_tx. delayed_payment_key. serialize( ) ) ?;
764
- writer. write_all( & $holder_tx. per_commitment_point. serialize( ) ) ?;
765
-
766
- writer. write_all( & byte_utils:: be32_to_array( $holder_tx. feerate_per_kw) ) ?;
767
- writer. write_all( & byte_utils:: be64_to_array( $holder_tx. htlc_outputs. len( ) as u64 ) ) ?;
768
- for & ( ref htlc_output, ref sig, ref htlc_source) in $holder_tx. htlc_outputs. iter( ) {
769
- serialize_htlc_in_commitment!( htlc_output) ;
770
- if let & Some ( ref their_sig) = sig {
771
- 1u8 . write( writer) ?;
772
- writer. write_all( & their_sig. serialize_compact( ) ) ?;
773
- } else {
774
- 0u8 . write( writer) ?;
775
- }
776
- htlc_source. write( writer) ?;
777
- }
778
- }
779
- }
780
-
781
721
if let Some ( ref prev_holder_tx) = self . prev_holder_signed_commitment_tx {
782
722
writer. write_all ( & [ 1 ; 1 ] ) ?;
783
- serialize_holder_tx ! ( prev_holder_tx) ;
723
+ prev_holder_tx. write ( writer ) ? ;
784
724
} else {
785
725
writer. write_all ( & [ 0 ; 1 ] ) ?;
786
726
}
787
727
788
- serialize_holder_tx ! ( self . current_holder_commitment_tx) ;
728
+ self . current_holder_commitment_tx . write ( writer ) ? ;
789
729
790
730
writer. write_all ( & byte_utils:: be48_to_array ( self . current_counterparty_commitment_number ) ) ?;
791
731
writer. write_all ( & byte_utils:: be48_to_array ( self . current_holder_commitment_number ) ) ?;
@@ -816,19 +756,7 @@ impl<Signer: Sign> Writeable for ChannelMonitorImpl<Signer> {
816
756
817
757
writer. write_all ( & byte_utils:: be64_to_array ( self . onchain_events_awaiting_threshold_conf . len ( ) as u64 ) ) ?;
818
758
for ref entry in self . onchain_events_awaiting_threshold_conf . iter ( ) {
819
- entry. txid . write ( writer) ?;
820
- writer. write_all ( & byte_utils:: be32_to_array ( entry. height ) ) ?;
821
- match entry. event {
822
- OnchainEvent :: HTLCUpdate { ref source, ref payment_hash } => {
823
- 0u8 . write ( writer) ?;
824
- source. write ( writer) ?;
825
- payment_hash. write ( writer) ?;
826
- } ,
827
- OnchainEvent :: MaturingOutput { ref descriptor } => {
828
- 1u8 . write ( writer) ?;
829
- descriptor. write ( writer) ?;
830
- } ,
831
- }
759
+ entry. write ( writer) ?;
832
760
}
833
761
834
762
( self . outputs_to_watch . len ( ) as u64 ) . write ( writer) ?;
@@ -2726,46 +2654,14 @@ impl<'a, Signer: Sign, K: KeysInterface<Signer = Signer>> ReadableArgs<&'a K>
2726
2654
}
2727
2655
}
2728
2656
2729
- macro_rules! read_holder_tx {
2730
- ( ) => {
2731
- {
2732
- let txid = Readable :: read( reader) ?;
2733
- let revocation_key = Readable :: read( reader) ?;
2734
- let a_htlc_key = Readable :: read( reader) ?;
2735
- let b_htlc_key = Readable :: read( reader) ?;
2736
- let delayed_payment_key = Readable :: read( reader) ?;
2737
- let per_commitment_point = Readable :: read( reader) ?;
2738
- let feerate_per_kw: u32 = Readable :: read( reader) ?;
2739
-
2740
- let htlcs_len: u64 = Readable :: read( reader) ?;
2741
- let mut htlcs = Vec :: with_capacity( cmp:: min( htlcs_len as usize , MAX_ALLOC_SIZE / 128 ) ) ;
2742
- for _ in 0 ..htlcs_len {
2743
- let htlc = read_htlc_in_commitment!( ) ;
2744
- let sigs = match <u8 as Readable >:: read( reader) ? {
2745
- 0 => None ,
2746
- 1 => Some ( Readable :: read( reader) ?) ,
2747
- _ => return Err ( DecodeError :: InvalidValue ) ,
2748
- } ;
2749
- htlcs. push( ( htlc, sigs, Readable :: read( reader) ?) ) ;
2750
- }
2751
-
2752
- HolderSignedTx {
2753
- txid,
2754
- revocation_key, a_htlc_key, b_htlc_key, delayed_payment_key, per_commitment_point, feerate_per_kw,
2755
- htlc_outputs: htlcs
2756
- }
2757
- }
2758
- }
2759
- }
2760
-
2761
2657
let prev_holder_signed_commitment_tx = match <u8 as Readable >:: read ( reader) ? {
2762
2658
0 => None ,
2763
2659
1 => {
2764
- Some ( read_holder_tx ! ( ) )
2660
+ Some ( Readable :: read ( reader ) ? )
2765
2661
} ,
2766
2662
_ => return Err ( DecodeError :: InvalidValue ) ,
2767
2663
} ;
2768
- let current_holder_commitment_tx = read_holder_tx ! ( ) ;
2664
+ let current_holder_commitment_tx = Readable :: read ( reader ) ? ;
2769
2665
2770
2666
let current_counterparty_commitment_number = <U48 as Readable >:: read ( reader) ?. 0 ;
2771
2667
let current_holder_commitment_number = <U48 as Readable >:: read ( reader) ?. 0 ;
@@ -2804,26 +2700,7 @@ impl<'a, Signer: Sign, K: KeysInterface<Signer = Signer>> ReadableArgs<&'a K>
2804
2700
let waiting_threshold_conf_len: u64 = Readable :: read ( reader) ?;
2805
2701
let mut onchain_events_awaiting_threshold_conf = Vec :: with_capacity ( cmp:: min ( waiting_threshold_conf_len as usize , MAX_ALLOC_SIZE / 128 ) ) ;
2806
2702
for _ in 0 ..waiting_threshold_conf_len {
2807
- let txid = Readable :: read ( reader) ?;
2808
- let height = Readable :: read ( reader) ?;
2809
- let event = match <u8 as Readable >:: read ( reader) ? {
2810
- 0 => {
2811
- let htlc_source = Readable :: read ( reader) ?;
2812
- let hash = Readable :: read ( reader) ?;
2813
- OnchainEvent :: HTLCUpdate {
2814
- source : htlc_source,
2815
- payment_hash : hash,
2816
- }
2817
- } ,
2818
- 1 => {
2819
- let descriptor = Readable :: read ( reader) ?;
2820
- OnchainEvent :: MaturingOutput {
2821
- descriptor
2822
- }
2823
- } ,
2824
- _ => return Err ( DecodeError :: InvalidValue ) ,
2825
- } ;
2826
- onchain_events_awaiting_threshold_conf. push ( OnchainEventEntry { txid, height, event } ) ;
2703
+ onchain_events_awaiting_threshold_conf. push ( Readable :: read ( reader) ?) ;
2827
2704
}
2828
2705
2829
2706
let outputs_to_watch_len: u64 = Readable :: read ( reader) ?;
0 commit comments