@@ -28,7 +28,7 @@ use ln::chan_utils;
28
28
use ln:: chan_utils:: HTLCOutputInCommitment ;
29
29
use chain:: chaininterface:: { ChainListener , ChainWatchInterface , BroadcasterInterface } ;
30
30
use chain:: transaction:: OutPoint ;
31
- use util:: ser:: Readable ;
31
+ use util:: ser:: { Readable , Writer } ;
32
32
use util:: sha2:: Sha256 ;
33
33
use util:: byte_utils;
34
34
@@ -531,159 +531,160 @@ impl ChannelMonitor {
531
531
}
532
532
533
533
/// Serializes into a vec, with various modes for the exposed pub fns
534
- fn serialize ( & self , for_local_storage : bool ) -> Vec < u8 > {
535
- let mut res = Vec :: new ( ) ;
536
- res. push ( SERIALIZATION_VERSION ) ;
537
- res. push ( MIN_SERIALIZATION_VERSION ) ;
534
+ fn write < W : Writer > ( & self , writer : & mut W , for_local_storage : bool ) -> Result < ( ) , :: std:: io:: Error > {
535
+ //TODO: We still write out all the serialization here manually instead of using the fancy
536
+ //serialization framework we have, we should migrate things over to it.
537
+ writer. write_all ( & [ SERIALIZATION_VERSION ; 1 ] ) ?;
538
+ writer. write_all ( & [ MIN_SERIALIZATION_VERSION ; 1 ] ) ?;
538
539
539
540
match & self . funding_txo {
540
541
& Some ( ( ref outpoint, ref script) ) => {
541
- res . extend_from_slice ( & outpoint. txid [ ..] ) ;
542
- res . extend_from_slice ( & byte_utils:: be16_to_array ( outpoint. index ) ) ;
543
- res . extend_from_slice ( & byte_utils:: be64_to_array ( script. len ( ) as u64 ) ) ;
544
- res . extend_from_slice ( & script[ ..] ) ;
542
+ writer . write_all ( & outpoint. txid [ ..] ) ? ;
543
+ writer . write_all ( & byte_utils:: be16_to_array ( outpoint. index ) ) ? ;
544
+ writer . write_all ( & byte_utils:: be64_to_array ( script. len ( ) as u64 ) ) ? ;
545
+ writer . write_all ( & script[ ..] ) ? ;
545
546
} ,
546
547
& None => {
547
548
// We haven't even been initialized...not sure why anyone is serializing us, but
548
549
// not much to give them.
549
- return res ;
550
+ return Ok ( ( ) ) ;
550
551
} ,
551
552
}
552
553
553
554
// Set in initial Channel-object creation, so should always be set by now:
554
- res . extend_from_slice ( & byte_utils:: be48_to_array ( self . commitment_transaction_number_obscure_factor ) ) ;
555
+ writer . write_all ( & byte_utils:: be48_to_array ( self . commitment_transaction_number_obscure_factor ) ) ? ;
555
556
556
557
match self . key_storage {
557
558
KeyStorage :: PrivMode { ref revocation_base_key, ref htlc_base_key } => {
558
- res . push ( 0 ) ;
559
- res . extend_from_slice ( & revocation_base_key[ ..] ) ;
560
- res . extend_from_slice ( & htlc_base_key[ ..] ) ;
559
+ writer . write_all ( & [ 0 ; 1 ] ) ? ;
560
+ writer . write_all ( & revocation_base_key[ ..] ) ? ;
561
+ writer . write_all ( & htlc_base_key[ ..] ) ? ;
561
562
} ,
562
563
KeyStorage :: SigsMode { .. } => unimplemented ! ( ) ,
563
564
}
564
565
565
- res . extend_from_slice ( & self . delayed_payment_base_key . serialize ( ) ) ;
566
- res . extend_from_slice ( & self . their_htlc_base_key . as_ref ( ) . unwrap ( ) . serialize ( ) ) ;
567
- res . extend_from_slice ( & self . their_delayed_payment_base_key . as_ref ( ) . unwrap ( ) . serialize ( ) ) ;
566
+ writer . write_all ( & self . delayed_payment_base_key . serialize ( ) ) ? ;
567
+ writer . write_all ( & self . their_htlc_base_key . as_ref ( ) . unwrap ( ) . serialize ( ) ) ? ;
568
+ writer . write_all ( & self . their_delayed_payment_base_key . as_ref ( ) . unwrap ( ) . serialize ( ) ) ? ;
568
569
569
570
match self . their_cur_revocation_points {
570
571
Some ( ( idx, pubkey, second_option) ) => {
571
- res . extend_from_slice ( & byte_utils:: be48_to_array ( idx) ) ;
572
- res . extend_from_slice ( & pubkey. serialize ( ) ) ;
572
+ writer . write_all ( & byte_utils:: be48_to_array ( idx) ) ? ;
573
+ writer . write_all ( & pubkey. serialize ( ) ) ? ;
573
574
match second_option {
574
575
Some ( second_pubkey) => {
575
- res . extend_from_slice ( & second_pubkey. serialize ( ) ) ;
576
+ writer . write_all ( & second_pubkey. serialize ( ) ) ? ;
576
577
} ,
577
578
None => {
578
- res . extend_from_slice ( & [ 0 ; 33 ] ) ;
579
+ writer . write_all ( & [ 0 ; 33 ] ) ? ;
579
580
} ,
580
581
}
581
582
} ,
582
583
None => {
583
- res . extend_from_slice ( & byte_utils:: be48_to_array ( 0 ) ) ;
584
+ writer . write_all ( & byte_utils:: be48_to_array ( 0 ) ) ? ;
584
585
} ,
585
586
}
586
587
587
- res . extend_from_slice ( & byte_utils:: be16_to_array ( self . our_to_self_delay ) ) ;
588
- res . extend_from_slice ( & byte_utils:: be16_to_array ( self . their_to_self_delay . unwrap ( ) ) ) ;
588
+ writer . write_all ( & byte_utils:: be16_to_array ( self . our_to_self_delay ) ) ? ;
589
+ writer . write_all ( & byte_utils:: be16_to_array ( self . their_to_self_delay . unwrap ( ) ) ) ? ;
589
590
590
591
for & ( ref secret, ref idx) in self . old_secrets . iter ( ) {
591
- res . extend_from_slice ( secret) ;
592
- res . extend_from_slice ( & byte_utils:: be64_to_array ( * idx) ) ;
592
+ writer . write_all ( secret) ? ;
593
+ writer . write_all ( & byte_utils:: be64_to_array ( * idx) ) ? ;
593
594
}
594
595
595
596
macro_rules! serialize_htlc_in_commitment {
596
597
( $htlc_output: expr) => {
597
- res . push ( $htlc_output. offered as u8 ) ;
598
- res . extend_from_slice ( & byte_utils:: be64_to_array( $htlc_output. amount_msat) ) ;
599
- res . extend_from_slice ( & byte_utils:: be32_to_array( $htlc_output. cltv_expiry) ) ;
600
- res . extend_from_slice ( & $htlc_output. payment_hash) ;
601
- res . extend_from_slice ( & byte_utils:: be32_to_array( $htlc_output. transaction_output_index) ) ;
598
+ writer . write_all ( & [ $htlc_output. offered as u8 ; 1 ] ) ? ;
599
+ writer . write_all ( & byte_utils:: be64_to_array( $htlc_output. amount_msat) ) ? ;
600
+ writer . write_all ( & byte_utils:: be32_to_array( $htlc_output. cltv_expiry) ) ? ;
601
+ writer . write_all ( & $htlc_output. payment_hash) ? ;
602
+ writer . write_all ( & byte_utils:: be32_to_array( $htlc_output. transaction_output_index) ) ? ;
602
603
}
603
604
}
604
605
605
- res . extend_from_slice ( & byte_utils:: be64_to_array ( self . remote_claimable_outpoints . len ( ) as u64 ) ) ;
606
+ writer . write_all ( & byte_utils:: be64_to_array ( self . remote_claimable_outpoints . len ( ) as u64 ) ) ? ;
606
607
for ( txid, htlc_outputs) in self . remote_claimable_outpoints . iter ( ) {
607
- res . extend_from_slice ( & txid[ ..] ) ;
608
- res . extend_from_slice ( & byte_utils:: be64_to_array ( htlc_outputs. len ( ) as u64 ) ) ;
608
+ writer . write_all ( & txid[ ..] ) ? ;
609
+ writer . write_all ( & byte_utils:: be64_to_array ( htlc_outputs. len ( ) as u64 ) ) ? ;
609
610
for htlc_output in htlc_outputs. iter ( ) {
610
611
serialize_htlc_in_commitment ! ( htlc_output) ;
611
612
}
612
613
}
613
614
614
615
{
615
616
let remote_commitment_txn_on_chain = self . remote_commitment_txn_on_chain . lock ( ) . unwrap ( ) ;
616
- res . extend_from_slice ( & byte_utils:: be64_to_array ( remote_commitment_txn_on_chain. len ( ) as u64 ) ) ;
617
+ writer . write_all ( & byte_utils:: be64_to_array ( remote_commitment_txn_on_chain. len ( ) as u64 ) ) ? ;
617
618
for ( txid, commitment_number) in remote_commitment_txn_on_chain. iter ( ) {
618
- res . extend_from_slice ( & txid[ ..] ) ;
619
- res . extend_from_slice ( & byte_utils:: be48_to_array ( * commitment_number) ) ;
619
+ writer . write_all ( & txid[ ..] ) ? ;
620
+ writer . write_all ( & byte_utils:: be48_to_array ( * commitment_number) ) ? ;
620
621
}
621
622
}
622
623
623
624
if for_local_storage {
624
- res . extend_from_slice ( & byte_utils:: be64_to_array ( self . remote_hash_commitment_number . len ( ) as u64 ) ) ;
625
+ writer . write_all ( & byte_utils:: be64_to_array ( self . remote_hash_commitment_number . len ( ) as u64 ) ) ? ;
625
626
for ( payment_hash, commitment_number) in self . remote_hash_commitment_number . iter ( ) {
626
- res . extend_from_slice ( payment_hash) ;
627
- res . extend_from_slice ( & byte_utils:: be48_to_array ( * commitment_number) ) ;
627
+ writer . write_all ( payment_hash) ? ;
628
+ writer . write_all ( & byte_utils:: be48_to_array ( * commitment_number) ) ? ;
628
629
}
629
630
} else {
630
- res . extend_from_slice ( & byte_utils:: be64_to_array ( 0 ) ) ;
631
+ writer . write_all ( & byte_utils:: be64_to_array ( 0 ) ) ? ;
631
632
}
632
633
633
634
macro_rules! serialize_local_tx {
634
635
( $local_tx: expr) => {
635
636
let tx_ser = serialize:: serialize( & $local_tx. tx) . unwrap( ) ;
636
- res . extend_from_slice ( & byte_utils:: be64_to_array( tx_ser. len( ) as u64 ) ) ;
637
- res . extend_from_slice ( & tx_ser) ;
637
+ writer . write_all ( & byte_utils:: be64_to_array( tx_ser. len( ) as u64 ) ) ? ;
638
+ writer . write_all ( & tx_ser) ? ;
638
639
639
- res . extend_from_slice ( & $local_tx. revocation_key. serialize( ) ) ;
640
- res . extend_from_slice ( & $local_tx. a_htlc_key. serialize( ) ) ;
641
- res . extend_from_slice ( & $local_tx. b_htlc_key. serialize( ) ) ;
642
- res . extend_from_slice ( & $local_tx. delayed_payment_key. serialize( ) ) ;
640
+ writer . write_all ( & $local_tx. revocation_key. serialize( ) ) ? ;
641
+ writer . write_all ( & $local_tx. a_htlc_key. serialize( ) ) ? ;
642
+ writer . write_all ( & $local_tx. b_htlc_key. serialize( ) ) ? ;
643
+ writer . write_all ( & $local_tx. delayed_payment_key. serialize( ) ) ? ;
643
644
644
- res . extend_from_slice ( & byte_utils:: be64_to_array( $local_tx. feerate_per_kw) ) ;
645
- res . extend_from_slice ( & byte_utils:: be64_to_array( $local_tx. htlc_outputs. len( ) as u64 ) ) ;
645
+ writer . write_all ( & byte_utils:: be64_to_array( $local_tx. feerate_per_kw) ) ? ;
646
+ writer . write_all ( & byte_utils:: be64_to_array( $local_tx. htlc_outputs. len( ) as u64 ) ) ? ;
646
647
for & ( ref htlc_output, ref their_sig, ref our_sig) in $local_tx. htlc_outputs. iter( ) {
647
648
serialize_htlc_in_commitment!( htlc_output) ;
648
- res . extend_from_slice ( & their_sig. serialize_compact( & self . secp_ctx) ) ;
649
- res . extend_from_slice ( & our_sig. serialize_compact( & self . secp_ctx) ) ;
649
+ writer . write_all ( & their_sig. serialize_compact( & self . secp_ctx) ) ? ;
650
+ writer . write_all ( & our_sig. serialize_compact( & self . secp_ctx) ) ? ;
650
651
}
651
652
}
652
653
}
653
654
654
655
if let Some ( ref prev_local_tx) = self . prev_local_signed_commitment_tx {
655
- res . push ( 1 ) ;
656
+ writer . write_all ( & [ 1 ; 1 ] ) ? ;
656
657
serialize_local_tx ! ( prev_local_tx) ;
657
658
} else {
658
- res . push ( 0 ) ;
659
+ writer . write_all ( & [ 0 ; 1 ] ) ? ;
659
660
}
660
661
661
662
if let Some ( ref cur_local_tx) = self . current_local_signed_commitment_tx {
662
- res . push ( 1 ) ;
663
+ writer . write_all ( & [ 1 ; 1 ] ) ? ;
663
664
serialize_local_tx ! ( cur_local_tx) ;
664
665
} else {
665
- res . push ( 0 ) ;
666
+ writer . write_all ( & [ 0 ; 1 ] ) ? ;
666
667
}
667
668
668
- res . extend_from_slice ( & byte_utils:: be64_to_array ( self . payment_preimages . len ( ) as u64 ) ) ;
669
+ writer . write_all ( & byte_utils:: be64_to_array ( self . payment_preimages . len ( ) as u64 ) ) ? ;
669
670
for payment_preimage in self . payment_preimages . values ( ) {
670
- res . extend_from_slice ( payment_preimage) ;
671
+ writer . write_all ( payment_preimage) ? ;
671
672
}
672
673
673
- res . extend_from_slice ( & byte_utils:: be64_to_array ( self . destination_script . len ( ) as u64 ) ) ;
674
- res . extend_from_slice ( & self . destination_script [ ..] ) ;
674
+ writer . write_all ( & byte_utils:: be64_to_array ( self . destination_script . len ( ) as u64 ) ) ? ;
675
+ writer . write_all ( & self . destination_script [ ..] ) ? ;
675
676
676
- res
677
+ Ok ( ( ) )
677
678
}
678
679
679
- /// Encodes this monitor into a byte array , suitable for writing to disk.
680
- pub fn serialize_for_disk ( & self ) -> Vec < u8 > {
681
- self . serialize ( true )
680
+ /// Writes this monitor into the given writer , suitable for writing to disk.
681
+ pub fn write_for_disk < W : Writer > ( & self , writer : & mut W ) -> Result < ( ) , :: std :: io :: Error > {
682
+ self . write ( writer , true )
682
683
}
683
684
684
- /// Encodes this monitor into a byte array , suitable for sending to a remote watchtower
685
- pub fn serialize_for_watchtower ( & self ) -> Vec < u8 > {
686
- self . serialize ( false )
685
+ /// Encodes this monitor into the given writer , suitable for sending to a remote watchtower
686
+ pub fn write_for_watchtower < W : Writer > ( & self , writer : & mut W ) -> Result < ( ) , :: std :: io :: Error > {
687
+ self . write ( writer , false )
687
688
}
688
689
689
690
//TODO: Functions to serialize/deserialize (with different forms depending on which information
0 commit comments