@@ -3206,16 +3206,16 @@ mod tests {
32063206 use chain:: chaininterface;
32073207 use chain:: transaction:: OutPoint ;
32083208 use chain:: chaininterface:: ChainListener ;
3209- use ln:: channelmanager:: { ChannelManager , OnionKeys , PaymentFailReason , RAACommitmentOrder } ;
3210- use ln:: channelmonitor:: { ChannelMonitorUpdateErr , CLTV_CLAIM_BUFFER , HTLC_FAIL_TIMEOUT_BLOCKS } ;
3209+ use ln:: channelmanager:: { ChannelManager , ChannelManagerReadArgs , OnionKeys , PaymentFailReason , RAACommitmentOrder } ;
3210+ use ln:: channelmonitor:: { ChannelMonitor , ChannelMonitorUpdateErr , CLTV_CLAIM_BUFFER , HTLC_FAIL_TIMEOUT_BLOCKS , ManyChannelMonitor } ;
32113211 use ln:: router:: { Route , RouteHop , Router } ;
32123212 use ln:: msgs;
32133213 use ln:: msgs:: { ChannelMessageHandler , RoutingMessageHandler } ;
32143214 use util:: test_utils;
32153215 use util:: events:: { Event , EventsProvider , MessageSendEvent , MessageSendEventsProvider } ;
32163216 use util:: errors:: APIError ;
32173217 use util:: logger:: Logger ;
3218- use util:: ser:: Writeable ;
3218+ use util:: ser:: { Writeable , Writer , ReadableArgs } ;
32193219
32203220 use bitcoin:: util:: hash:: Sha256dHash ;
32213221 use bitcoin:: blockdata:: block:: { Block , BlockHeader } ;
@@ -3410,6 +3410,7 @@ mod tests {
34103410 chan_monitor : Arc < test_utils:: TestChannelMonitor > ,
34113411 node : Arc < ChannelManager > ,
34123412 router : Router ,
3413+ node_secret : SecretKey ,
34133414 network_payment_count : Rc < RefCell < u8 > > ,
34143415 network_chan_count : Rc < RefCell < u32 > > ,
34153416 }
@@ -4076,14 +4077,14 @@ mod tests {
40764077 let chain_monitor = Arc :: new ( chaininterface:: ChainWatchInterfaceUtil :: new ( Network :: Testnet , Arc :: clone ( & logger) ) ) ;
40774078 let tx_broadcaster = Arc :: new ( test_utils:: TestBroadcaster { txn_broadcasted : Mutex :: new ( Vec :: new ( ) ) } ) ;
40784079 let chan_monitor = Arc :: new ( test_utils:: TestChannelMonitor :: new ( chain_monitor. clone ( ) , tx_broadcaster. clone ( ) ) ) ;
4079- let node_id = {
4080+ let node_secret = {
40804081 let mut key_slice = [ 0 ; 32 ] ;
40814082 rng. fill_bytes ( & mut key_slice) ;
40824083 SecretKey :: from_slice ( & secp_ctx, & key_slice) . unwrap ( )
40834084 } ;
4084- let node = ChannelManager :: new ( node_id . clone ( ) , 0 , true , Network :: Testnet , feeest. clone ( ) , chan_monitor. clone ( ) , chain_monitor. clone ( ) , tx_broadcaster. clone ( ) , Arc :: clone ( & logger) ) . unwrap ( ) ;
4085- let router = Router :: new ( PublicKey :: from_secret_key ( & secp_ctx, & node_id ) , chain_monitor. clone ( ) , Arc :: clone ( & logger) ) ;
4086- nodes. push ( Node { chain_monitor, tx_broadcaster, chan_monitor, node, router,
4085+ let node = ChannelManager :: new ( node_secret . clone ( ) , 0 , true , Network :: Testnet , feeest. clone ( ) , chan_monitor. clone ( ) , chain_monitor. clone ( ) , tx_broadcaster. clone ( ) , Arc :: clone ( & logger) ) . unwrap ( ) ;
4086+ let router = Router :: new ( PublicKey :: from_secret_key ( & secp_ctx, & node_secret ) , chain_monitor. clone ( ) , Arc :: clone ( & logger) ) ;
4087+ nodes. push ( Node { chain_monitor, tx_broadcaster, chan_monitor, node, router, node_secret ,
40874088 network_payment_count : payment_count. clone ( ) ,
40884089 network_chan_count : chan_count. clone ( ) ,
40894090 } ) ;
@@ -6886,4 +6887,135 @@ mod tests {
68866887 sign_msg ! ( unsigned_msg) ;
68876888 assert ! ( nodes[ 0 ] . router. handle_channel_announcement( & chan_announcement) . is_err( ) ) ;
68886889 }
6890+
6891+ struct VecWriter ( Vec < u8 > ) ;
6892+ impl Writer for VecWriter {
6893+ fn write_all ( & mut self , buf : & [ u8 ] ) -> Result < ( ) , :: std:: io:: Error > {
6894+ self . 0 . extend_from_slice ( buf) ;
6895+ Ok ( ( ) )
6896+ }
6897+ fn size_hint ( & mut self , size : usize ) {
6898+ self . 0 . reserve_exact ( size) ;
6899+ }
6900+ }
6901+
6902+ #[ test]
6903+ fn test_simple_manager_serialize_deserialize ( ) {
6904+ let mut nodes = create_network ( 2 ) ;
6905+ create_announced_chan_between_nodes ( & nodes, 0 , 1 ) ;
6906+
6907+ let ( our_payment_preimage, _) = route_payment ( & nodes[ 0 ] , & [ & nodes[ 1 ] ] , 1000000 ) ;
6908+ let ( _, our_payment_hash) = route_payment ( & nodes[ 0 ] , & [ & nodes[ 1 ] ] , 1000000 ) ;
6909+
6910+ nodes[ 1 ] . node . peer_disconnected ( & nodes[ 0 ] . node . get_our_node_id ( ) , false ) ;
6911+
6912+ let nodes_0_serialized = nodes[ 0 ] . node . encode ( ) ;
6913+ let mut chan_0_monitor_serialized = VecWriter ( Vec :: new ( ) ) ;
6914+ nodes[ 0 ] . chan_monitor . simple_monitor . monitors . lock ( ) . unwrap ( ) . iter ( ) . next ( ) . unwrap ( ) . 1 . write_for_disk ( & mut chan_0_monitor_serialized) . unwrap ( ) ;
6915+
6916+ nodes[ 0 ] . chan_monitor = Arc :: new ( test_utils:: TestChannelMonitor :: new ( nodes[ 0 ] . chain_monitor . clone ( ) , nodes[ 0 ] . tx_broadcaster . clone ( ) ) ) ;
6917+ let mut chan_0_monitor_read = & chan_0_monitor_serialized. 0 [ ..] ;
6918+ let ( _, chan_0_monitor) = <( Sha256dHash , ChannelMonitor ) >:: read ( & mut chan_0_monitor_read, Arc :: new ( test_utils:: TestLogger :: new ( ) ) ) . unwrap ( ) ;
6919+ assert ! ( chan_0_monitor_read. is_empty( ) ) ;
6920+
6921+ let mut nodes_0_read = & nodes_0_serialized[ ..] ;
6922+ let ( _, nodes_0_deserialized) = {
6923+ let mut channel_monitors = HashMap :: new ( ) ;
6924+ channel_monitors. insert ( chan_0_monitor. get_funding_txo ( ) . unwrap ( ) , & chan_0_monitor) ;
6925+ <( Sha256dHash , ChannelManager ) >:: read ( & mut nodes_0_read, ChannelManagerReadArgs {
6926+ our_network_key : nodes[ 0 ] . node_secret . clone ( ) ,
6927+ fee_estimator : Arc :: new ( test_utils:: TestFeeEstimator { sat_per_kw : 253 } ) ,
6928+ monitor : nodes[ 0 ] . chan_monitor . clone ( ) ,
6929+ chain_monitor : nodes[ 0 ] . chain_monitor . clone ( ) ,
6930+ tx_broadcaster : nodes[ 0 ] . tx_broadcaster . clone ( ) ,
6931+ logger : Arc :: new ( test_utils:: TestLogger :: new ( ) ) ,
6932+ channel_monitors : & channel_monitors,
6933+ } ) . unwrap ( )
6934+ } ;
6935+ assert ! ( nodes_0_read. is_empty( ) ) ;
6936+
6937+ assert ! ( nodes[ 0 ] . chan_monitor. add_update_monitor( chan_0_monitor. get_funding_txo( ) . unwrap( ) , chan_0_monitor) . is_ok( ) ) ;
6938+ nodes[ 0 ] . node = Arc :: new ( nodes_0_deserialized) ;
6939+ check_added_monitors ! ( nodes[ 0 ] , 1 ) ;
6940+
6941+ reconnect_nodes ( & nodes[ 0 ] , & nodes[ 1 ] , false , ( 0 , 0 ) , ( 0 , 0 ) , ( 0 , 0 ) , ( 0 , 0 ) , ( false , false ) ) ;
6942+
6943+ fail_payment ( & nodes[ 0 ] , & [ & nodes[ 1 ] ] , our_payment_hash) ;
6944+ claim_payment ( & nodes[ 0 ] , & [ & nodes[ 1 ] ] , our_payment_preimage) ;
6945+ }
6946+
6947+ #[ test]
6948+ fn test_manager_serialize_deserialize_inconsistent_monitor ( ) {
6949+ // Test deserializing a ChannelManager with a out-of-date ChannelMonitor
6950+ let mut nodes = create_network ( 4 ) ;
6951+ create_announced_chan_between_nodes ( & nodes, 0 , 1 ) ;
6952+ create_announced_chan_between_nodes ( & nodes, 2 , 0 ) ;
6953+ let ( _, _, channel_id, funding_tx) = create_announced_chan_between_nodes ( & nodes, 0 , 3 ) ;
6954+
6955+ let ( our_payment_preimage, _) = route_payment ( & nodes[ 2 ] , & [ & nodes[ 0 ] , & nodes[ 1 ] ] , 1000000 ) ;
6956+
6957+ // Serialize the ChannelManager here, but the monitor we keep up-to-date
6958+ let nodes_0_serialized = nodes[ 0 ] . node . encode ( ) ;
6959+
6960+ route_payment ( & nodes[ 0 ] , & [ & nodes[ 3 ] ] , 1000000 ) ;
6961+ nodes[ 1 ] . node . peer_disconnected ( & nodes[ 0 ] . node . get_our_node_id ( ) , false ) ;
6962+ nodes[ 2 ] . node . peer_disconnected ( & nodes[ 0 ] . node . get_our_node_id ( ) , false ) ;
6963+ nodes[ 3 ] . node . peer_disconnected ( & nodes[ 0 ] . node . get_our_node_id ( ) , false ) ;
6964+
6965+ // Now the ChannelMonitor (which is now out-of-sync with ChannelManager for channel w/
6966+ // nodes[3])
6967+ let mut node_0_monitors_serialized = Vec :: new ( ) ;
6968+ for monitor in nodes[ 0 ] . chan_monitor . simple_monitor . monitors . lock ( ) . unwrap ( ) . iter ( ) {
6969+ let mut writer = VecWriter ( Vec :: new ( ) ) ;
6970+ monitor. 1 . write_for_disk ( & mut writer) . unwrap ( ) ;
6971+ node_0_monitors_serialized. push ( writer. 0 ) ;
6972+ }
6973+
6974+ nodes[ 0 ] . chan_monitor = Arc :: new ( test_utils:: TestChannelMonitor :: new ( nodes[ 0 ] . chain_monitor . clone ( ) , nodes[ 0 ] . tx_broadcaster . clone ( ) ) ) ;
6975+ let mut node_0_monitors = Vec :: new ( ) ;
6976+ for serialized in node_0_monitors_serialized. iter ( ) {
6977+ let mut read = & serialized[ ..] ;
6978+ let ( _, monitor) = <( Sha256dHash , ChannelMonitor ) >:: read ( & mut read, Arc :: new ( test_utils:: TestLogger :: new ( ) ) ) . unwrap ( ) ;
6979+ assert ! ( read. is_empty( ) ) ;
6980+ node_0_monitors. push ( monitor) ;
6981+ }
6982+
6983+ let mut nodes_0_read = & nodes_0_serialized[ ..] ;
6984+ let ( _, nodes_0_deserialized) = <( Sha256dHash , ChannelManager ) >:: read ( & mut nodes_0_read, ChannelManagerReadArgs {
6985+ our_network_key : nodes[ 0 ] . node_secret . clone ( ) ,
6986+ fee_estimator : Arc :: new ( test_utils:: TestFeeEstimator { sat_per_kw : 253 } ) ,
6987+ monitor : nodes[ 0 ] . chan_monitor . clone ( ) ,
6988+ chain_monitor : nodes[ 0 ] . chain_monitor . clone ( ) ,
6989+ tx_broadcaster : nodes[ 0 ] . tx_broadcaster . clone ( ) ,
6990+ logger : Arc :: new ( test_utils:: TestLogger :: new ( ) ) ,
6991+ channel_monitors : & node_0_monitors. iter ( ) . map ( |monitor| { ( monitor. get_funding_txo ( ) . unwrap ( ) , monitor) } ) . collect ( ) ,
6992+ } ) . unwrap ( ) ;
6993+ assert ! ( nodes_0_read. is_empty( ) ) ;
6994+
6995+ { // Channel close should result in a commitment tx and an HTLC tx
6996+ let txn = nodes[ 0 ] . tx_broadcaster . txn_broadcasted . lock ( ) . unwrap ( ) ;
6997+ assert_eq ! ( txn. len( ) , 2 ) ;
6998+ assert_eq ! ( txn[ 0 ] . input[ 0 ] . previous_output. txid, funding_tx. txid( ) ) ;
6999+ assert_eq ! ( txn[ 1 ] . input[ 0 ] . previous_output. txid, txn[ 0 ] . txid( ) ) ;
7000+ }
7001+
7002+ for monitor in node_0_monitors. drain ( ..) {
7003+ assert ! ( nodes[ 0 ] . chan_monitor. add_update_monitor( monitor. get_funding_txo( ) . unwrap( ) , monitor) . is_ok( ) ) ;
7004+ check_added_monitors ! ( nodes[ 0 ] , 1 ) ;
7005+ }
7006+ nodes[ 0 ] . node = Arc :: new ( nodes_0_deserialized) ;
7007+
7008+ // nodes[1] and nodes[2] have no lost state with nodes[0]...
7009+ reconnect_nodes ( & nodes[ 0 ] , & nodes[ 1 ] , false , ( 0 , 0 ) , ( 0 , 0 ) , ( 0 , 0 ) , ( 0 , 0 ) , ( false , false ) ) ;
7010+ reconnect_nodes ( & nodes[ 0 ] , & nodes[ 2 ] , false , ( 0 , 0 ) , ( 0 , 0 ) , ( 0 , 0 ) , ( 0 , 0 ) , ( false , false ) ) ;
7011+ //... and we can even still claim the payment!
7012+ claim_payment ( & nodes[ 2 ] , & [ & nodes[ 0 ] , & nodes[ 1 ] ] , our_payment_preimage) ;
7013+
7014+ nodes[ 3 ] . node . peer_connected ( & nodes[ 0 ] . node . get_our_node_id ( ) ) ;
7015+ let reestablish = get_event_msg ! ( nodes[ 3 ] , MessageSendEvent :: SendChannelReestablish , nodes[ 0 ] . node. get_our_node_id( ) ) ;
7016+ nodes[ 0 ] . node . peer_connected ( & nodes[ 3 ] . node . get_our_node_id ( ) ) ;
7017+ if let Err ( msgs:: HandleError { action : Some ( msgs:: ErrorAction :: SendErrorMessage { msg } ) , .. } ) = nodes[ 0 ] . node . handle_channel_reestablish ( & nodes[ 3 ] . node . get_our_node_id ( ) , & reestablish) {
7018+ assert_eq ! ( msg. channel_id, channel_id) ;
7019+ } else { panic ! ( "Unexpected result" ) ; }
7020+ }
68897021}
0 commit comments