@@ -3191,16 +3191,16 @@ mod tests {
3191
3191
use chain:: chaininterface;
3192
3192
use chain:: transaction:: OutPoint ;
3193
3193
use chain:: chaininterface:: ChainListener ;
3194
- use ln:: channelmanager:: { ChannelManager , OnionKeys , RAACommitmentOrder } ;
3195
- use ln:: channelmonitor:: { ChannelMonitorUpdateErr , CLTV_CLAIM_BUFFER , HTLC_FAIL_TIMEOUT_BLOCKS } ;
3194
+ use ln:: channelmanager:: { ChannelManager , ChannelManagerReadArgs , OnionKeys , RAACommitmentOrder } ;
3195
+ use ln:: channelmonitor:: { ChannelMonitor , ChannelMonitorUpdateErr , CLTV_CLAIM_BUFFER , HTLC_FAIL_TIMEOUT_BLOCKS , ManyChannelMonitor } ;
3196
3196
use ln:: router:: { Route , RouteHop , Router } ;
3197
3197
use ln:: msgs;
3198
3198
use ln:: msgs:: { ChannelMessageHandler , RoutingMessageHandler } ;
3199
3199
use util:: test_utils;
3200
3200
use util:: events:: { Event , EventsProvider , MessageSendEvent , MessageSendEventsProvider } ;
3201
3201
use util:: errors:: APIError ;
3202
3202
use util:: logger:: Logger ;
3203
- use util:: ser:: Writeable ;
3203
+ use util:: ser:: { Writeable , Writer , ReadableArgs } ;
3204
3204
3205
3205
use bitcoin:: util:: hash:: Sha256dHash ;
3206
3206
use bitcoin:: blockdata:: block:: { Block , BlockHeader } ;
@@ -3395,6 +3395,7 @@ mod tests {
3395
3395
chan_monitor : Arc < test_utils:: TestChannelMonitor > ,
3396
3396
node : Arc < ChannelManager > ,
3397
3397
router : Router ,
3398
+ node_secret : SecretKey ,
3398
3399
network_payment_count : Rc < RefCell < u8 > > ,
3399
3400
network_chan_count : Rc < RefCell < u32 > > ,
3400
3401
}
@@ -4061,14 +4062,14 @@ mod tests {
4061
4062
let chain_monitor = Arc :: new ( chaininterface:: ChainWatchInterfaceUtil :: new ( Network :: Testnet , Arc :: clone ( & logger) ) ) ;
4062
4063
let tx_broadcaster = Arc :: new ( test_utils:: TestBroadcaster { txn_broadcasted : Mutex :: new ( Vec :: new ( ) ) } ) ;
4063
4064
let chan_monitor = Arc :: new ( test_utils:: TestChannelMonitor :: new ( chain_monitor. clone ( ) , tx_broadcaster. clone ( ) ) ) ;
4064
- let node_id = {
4065
+ let node_secret = {
4065
4066
let mut key_slice = [ 0 ; 32 ] ;
4066
4067
rng. fill_bytes ( & mut key_slice) ;
4067
4068
SecretKey :: from_slice ( & secp_ctx, & key_slice) . unwrap ( )
4068
4069
} ;
4069
- 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 ( ) ;
4070
- let router = Router :: new ( PublicKey :: from_secret_key ( & secp_ctx, & node_id ) , chain_monitor. clone ( ) , Arc :: clone ( & logger) ) ;
4071
- nodes. push ( Node { chain_monitor, tx_broadcaster, chan_monitor, node, router,
4070
+ 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 ( ) ;
4071
+ let router = Router :: new ( PublicKey :: from_secret_key ( & secp_ctx, & node_secret ) , chain_monitor. clone ( ) , Arc :: clone ( & logger) ) ;
4072
+ nodes. push ( Node { chain_monitor, tx_broadcaster, chan_monitor, node, router, node_secret ,
4072
4073
network_payment_count : payment_count. clone ( ) ,
4073
4074
network_chan_count : chan_count. clone ( ) ,
4074
4075
} ) ;
@@ -6871,4 +6872,135 @@ mod tests {
6871
6872
sign_msg ! ( unsigned_msg) ;
6872
6873
assert ! ( nodes[ 0 ] . router. handle_channel_announcement( & chan_announcement) . is_err( ) ) ;
6873
6874
}
6875
+
6876
+ struct VecWriter ( Vec < u8 > ) ;
6877
+ impl Writer for VecWriter {
6878
+ fn write_all ( & mut self , buf : & [ u8 ] ) -> Result < ( ) , :: std:: io:: Error > {
6879
+ self . 0 . extend_from_slice ( buf) ;
6880
+ Ok ( ( ) )
6881
+ }
6882
+ fn size_hint ( & mut self , size : usize ) {
6883
+ self . 0 . reserve_exact ( size) ;
6884
+ }
6885
+ }
6886
+
6887
+ #[ test]
6888
+ fn test_simple_manager_serialize_deserialize ( ) {
6889
+ let mut nodes = create_network ( 2 ) ;
6890
+ create_announced_chan_between_nodes ( & nodes, 0 , 1 ) ;
6891
+
6892
+ let ( our_payment_preimage, _) = route_payment ( & nodes[ 0 ] , & [ & nodes[ 1 ] ] , 1000000 ) ;
6893
+ let ( _, our_payment_hash) = route_payment ( & nodes[ 0 ] , & [ & nodes[ 1 ] ] , 1000000 ) ;
6894
+
6895
+ nodes[ 1 ] . node . peer_disconnected ( & nodes[ 0 ] . node . get_our_node_id ( ) , false ) ;
6896
+
6897
+ let nodes_0_serialized = nodes[ 0 ] . node . encode ( ) ;
6898
+ let mut chan_0_monitor_serialized = VecWriter ( Vec :: new ( ) ) ;
6899
+ nodes[ 0 ] . chan_monitor . simple_monitor . monitors . lock ( ) . unwrap ( ) . iter ( ) . next ( ) . unwrap ( ) . 1 . write_for_disk ( & mut chan_0_monitor_serialized) . unwrap ( ) ;
6900
+
6901
+ nodes[ 0 ] . chan_monitor = Arc :: new ( test_utils:: TestChannelMonitor :: new ( nodes[ 0 ] . chain_monitor . clone ( ) , nodes[ 0 ] . tx_broadcaster . clone ( ) ) ) ;
6902
+ let mut chan_0_monitor_read = & chan_0_monitor_serialized. 0 [ ..] ;
6903
+ let ( _, chan_0_monitor) = <( Sha256dHash , ChannelMonitor ) >:: read ( & mut chan_0_monitor_read, Arc :: new ( test_utils:: TestLogger :: new ( ) ) ) . unwrap ( ) ;
6904
+ assert ! ( chan_0_monitor_read. is_empty( ) ) ;
6905
+
6906
+ let mut nodes_0_read = & nodes_0_serialized[ ..] ;
6907
+ let ( _, nodes_0_deserialized) = {
6908
+ let mut channel_monitors = HashMap :: new ( ) ;
6909
+ channel_monitors. insert ( chan_0_monitor. get_funding_txo ( ) . unwrap ( ) , & chan_0_monitor) ;
6910
+ <( Sha256dHash , ChannelManager ) >:: read ( & mut nodes_0_read, ChannelManagerReadArgs {
6911
+ our_network_key : nodes[ 0 ] . node_secret . clone ( ) ,
6912
+ fee_estimator : Arc :: new ( test_utils:: TestFeeEstimator { sat_per_kw : 253 } ) ,
6913
+ monitor : nodes[ 0 ] . chan_monitor . clone ( ) ,
6914
+ chain_monitor : nodes[ 0 ] . chain_monitor . clone ( ) ,
6915
+ tx_broadcaster : nodes[ 0 ] . tx_broadcaster . clone ( ) ,
6916
+ logger : Arc :: new ( test_utils:: TestLogger :: new ( ) ) ,
6917
+ channel_monitors : & channel_monitors,
6918
+ } ) . unwrap ( )
6919
+ } ;
6920
+ assert ! ( nodes_0_read. is_empty( ) ) ;
6921
+
6922
+ assert ! ( nodes[ 0 ] . chan_monitor. add_update_monitor( chan_0_monitor. get_funding_txo( ) . unwrap( ) , chan_0_monitor) . is_ok( ) ) ;
6923
+ nodes[ 0 ] . node = Arc :: new ( nodes_0_deserialized) ;
6924
+ check_added_monitors ! ( nodes[ 0 ] , 1 ) ;
6925
+
6926
+ reconnect_nodes ( & nodes[ 0 ] , & nodes[ 1 ] , false , ( 0 , 0 ) , ( 0 , 0 ) , ( 0 , 0 ) , ( 0 , 0 ) , ( false , false ) ) ;
6927
+
6928
+ fail_payment ( & nodes[ 0 ] , & [ & nodes[ 1 ] ] , our_payment_hash) ;
6929
+ claim_payment ( & nodes[ 0 ] , & [ & nodes[ 1 ] ] , our_payment_preimage) ;
6930
+ }
6931
+
6932
+ #[ test]
6933
+ fn test_manager_serialize_deserialize_inconsistent_monitor ( ) {
6934
+ // Test deserializing a ChannelManager with a out-of-date ChannelMonitor
6935
+ let mut nodes = create_network ( 4 ) ;
6936
+ create_announced_chan_between_nodes ( & nodes, 0 , 1 ) ;
6937
+ create_announced_chan_between_nodes ( & nodes, 2 , 0 ) ;
6938
+ let ( _, _, channel_id, funding_tx) = create_announced_chan_between_nodes ( & nodes, 0 , 3 ) ;
6939
+
6940
+ let ( our_payment_preimage, _) = route_payment ( & nodes[ 2 ] , & [ & nodes[ 0 ] , & nodes[ 1 ] ] , 1000000 ) ;
6941
+
6942
+ // Serialize the ChannelManager here, but the monitor we keep up-to-date
6943
+ let nodes_0_serialized = nodes[ 0 ] . node . encode ( ) ;
6944
+
6945
+ route_payment ( & nodes[ 0 ] , & [ & nodes[ 3 ] ] , 1000000 ) ;
6946
+ nodes[ 1 ] . node . peer_disconnected ( & nodes[ 0 ] . node . get_our_node_id ( ) , false ) ;
6947
+ nodes[ 2 ] . node . peer_disconnected ( & nodes[ 0 ] . node . get_our_node_id ( ) , false ) ;
6948
+ nodes[ 3 ] . node . peer_disconnected ( & nodes[ 0 ] . node . get_our_node_id ( ) , false ) ;
6949
+
6950
+ // Now the ChannelMonitor (which is now out-of-sync with ChannelManager for channel w/
6951
+ // nodes[3])
6952
+ let mut node_0_monitors_serialized = Vec :: new ( ) ;
6953
+ for monitor in nodes[ 0 ] . chan_monitor . simple_monitor . monitors . lock ( ) . unwrap ( ) . iter ( ) {
6954
+ let mut writer = VecWriter ( Vec :: new ( ) ) ;
6955
+ monitor. 1 . write_for_disk ( & mut writer) . unwrap ( ) ;
6956
+ node_0_monitors_serialized. push ( writer. 0 ) ;
6957
+ }
6958
+
6959
+ nodes[ 0 ] . chan_monitor = Arc :: new ( test_utils:: TestChannelMonitor :: new ( nodes[ 0 ] . chain_monitor . clone ( ) , nodes[ 0 ] . tx_broadcaster . clone ( ) ) ) ;
6960
+ let mut node_0_monitors = Vec :: new ( ) ;
6961
+ for serialized in node_0_monitors_serialized. iter ( ) {
6962
+ let mut read = & serialized[ ..] ;
6963
+ let ( _, monitor) = <( Sha256dHash , ChannelMonitor ) >:: read ( & mut read, Arc :: new ( test_utils:: TestLogger :: new ( ) ) ) . unwrap ( ) ;
6964
+ assert ! ( read. is_empty( ) ) ;
6965
+ node_0_monitors. push ( monitor) ;
6966
+ }
6967
+
6968
+ let mut nodes_0_read = & nodes_0_serialized[ ..] ;
6969
+ let ( _, nodes_0_deserialized) = <( Sha256dHash , ChannelManager ) >:: read ( & mut nodes_0_read, ChannelManagerReadArgs {
6970
+ our_network_key : nodes[ 0 ] . node_secret . clone ( ) ,
6971
+ fee_estimator : Arc :: new ( test_utils:: TestFeeEstimator { sat_per_kw : 253 } ) ,
6972
+ monitor : nodes[ 0 ] . chan_monitor . clone ( ) ,
6973
+ chain_monitor : nodes[ 0 ] . chain_monitor . clone ( ) ,
6974
+ tx_broadcaster : nodes[ 0 ] . tx_broadcaster . clone ( ) ,
6975
+ logger : Arc :: new ( test_utils:: TestLogger :: new ( ) ) ,
6976
+ channel_monitors : & node_0_monitors. iter ( ) . map ( |monitor| { ( monitor. get_funding_txo ( ) . unwrap ( ) , monitor) } ) . collect ( ) ,
6977
+ } ) . unwrap ( ) ;
6978
+ assert ! ( nodes_0_read. is_empty( ) ) ;
6979
+
6980
+ { // Channel close should result in a commitment tx and an HTLC tx
6981
+ let txn = nodes[ 0 ] . tx_broadcaster . txn_broadcasted . lock ( ) . unwrap ( ) ;
6982
+ assert_eq ! ( txn. len( ) , 2 ) ;
6983
+ assert_eq ! ( txn[ 0 ] . input[ 0 ] . previous_output. txid, funding_tx. txid( ) ) ;
6984
+ assert_eq ! ( txn[ 1 ] . input[ 0 ] . previous_output. txid, txn[ 0 ] . txid( ) ) ;
6985
+ }
6986
+
6987
+ for monitor in node_0_monitors. drain ( ..) {
6988
+ assert ! ( nodes[ 0 ] . chan_monitor. add_update_monitor( monitor. get_funding_txo( ) . unwrap( ) , monitor) . is_ok( ) ) ;
6989
+ check_added_monitors ! ( nodes[ 0 ] , 1 ) ;
6990
+ }
6991
+ nodes[ 0 ] . node = Arc :: new ( nodes_0_deserialized) ;
6992
+
6993
+ // nodes[1] and nodes[2] have no lost state with nodes[0]...
6994
+ reconnect_nodes ( & nodes[ 0 ] , & nodes[ 1 ] , false , ( 0 , 0 ) , ( 0 , 0 ) , ( 0 , 0 ) , ( 0 , 0 ) , ( false , false ) ) ;
6995
+ reconnect_nodes ( & nodes[ 0 ] , & nodes[ 2 ] , false , ( 0 , 0 ) , ( 0 , 0 ) , ( 0 , 0 ) , ( 0 , 0 ) , ( false , false ) ) ;
6996
+ //... and we can even still claim the payment!
6997
+ claim_payment ( & nodes[ 2 ] , & [ & nodes[ 0 ] , & nodes[ 1 ] ] , our_payment_preimage) ;
6998
+
6999
+ nodes[ 3 ] . node . peer_connected ( & nodes[ 0 ] . node . get_our_node_id ( ) ) ;
7000
+ let reestablish = get_event_msg ! ( nodes[ 3 ] , MessageSendEvent :: SendChannelReestablish , nodes[ 0 ] . node. get_our_node_id( ) ) ;
7001
+ nodes[ 0 ] . node . peer_connected ( & nodes[ 3 ] . node . get_our_node_id ( ) ) ;
7002
+ 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) {
7003
+ assert_eq ! ( msg. channel_id, channel_id) ;
7004
+ } else { panic ! ( "Unexpected result" ) ; }
7005
+ }
6874
7006
}
0 commit comments