@@ -4686,6 +4686,146 @@ macro_rules! check_spendable_outputs {
4686
4686
}
4687
4687
}
4688
4688
4689
+ #[test]
4690
+ fn test_peer_storage_on_revoked_txn() {
4691
+ let chanmon_cfgs = create_chanmon_cfgs(2);
4692
+ let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
4693
+ let (persister, chain_monitor);
4694
+
4695
+ let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
4696
+ let nodes_0_deserialized;
4697
+ let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
4698
+ let nodes_0_serialized = nodes[0].node.encode();
4699
+
4700
+ let (_a, _b, channel_id, funding_tx) = create_announced_chan_between_nodes(&nodes, 1, 0);
4701
+
4702
+ send_payment(&nodes[1], &vec!(&nodes[0])[..], 10000);
4703
+ send_payment(&nodes[1], &vec!(&nodes[0])[..], 10000);
4704
+
4705
+ let revoked_local_txn = get_local_commitment_txn!(nodes[1], channel_id);
4706
+ assert_eq!(revoked_local_txn[0].input.len(), 1);
4707
+ assert_eq!(revoked_local_txn[0].input[0].previous_output.txid, funding_tx.txid());
4708
+
4709
+ send_payment(&nodes[1], &vec!(&nodes[0])[..], 10000);
4710
+
4711
+ nodes[0].node.peer_disconnected(nodes[1].node.get_our_node_id());
4712
+ nodes[1].node.peer_disconnected(nodes[0].node.get_our_node_id());
4713
+
4714
+ // // Lets drop the monitor and clear the chain_monitor as well.
4715
+ nodes[0].chain_source.clear_watched_txn_and_outputs();
4716
+ reload_node!(nodes[0], test_default_channel_config(), &nodes_0_serialized, &[], persister, chain_monitor, nodes_0_deserialized);
4717
+ let persister: &dyn Persist<TestChannelSigner> = &chanmon_cfgs[0].persister;
4718
+
4719
+ let fundrecoverer
4720
+ = FundRecoverer::new(node_cfgs[0].keys_manager, node_cfgs[0].logger,test_default_channel_config(), ChainParameters {network: Network::Testnet,
4721
+ best_block: BestBlock::from_network(Network::Testnet)}, node_cfgs[0].keys_manager, node_cfgs[0].keys_manager, Some(&chanmon_cfgs[0].chain_source),
4722
+ persister, node_cfgs[0].fee_estimator, node_cfgs[0].tx_broadcaster);
4723
+
4724
+ fundrecoverer.peer_connected(nodes[1].node.get_our_node_id(), &msgs::Init {
4725
+ features: nodes[0].node.init_features(), networks: None, remote_network_address: None
4726
+ }, true).unwrap();
4727
+
4728
+ nodes[1].node.peer_connected(nodes[0].node.get_our_node_id(), &msgs::Init {
4729
+ features: nodes[0].node.init_features(), networks: None, remote_network_address: None
4730
+ }, true).unwrap();
4731
+ let msg_events = nodes[1].node.get_and_clear_pending_msg_events();
4732
+ // 0th - SendYourPeerStorageMessage
4733
+ // 1st - SendPeerStorage
4734
+ // 2nd - SendChannelReestablish
4735
+ assert_eq!(msg_events.len(), 3);
4736
+
4737
+ // println!("events array is {:?}", msg_events);
4738
+ for msg in msg_events {
4739
+ if let MessageSendEvent::SendChannelReestablish { ref node_id, ref msg } = msg {
4740
+ fundrecoverer.handle_channel_reestablish(nodes[1].node.get_our_node_id(), msg);
4741
+ assert_eq!(*node_id, nodes[0].node.get_our_node_id());
4742
+ } else if let MessageSendEvent::SendPeerStorageMessage { ref node_id, ref msg } = msg {
4743
+ fundrecoverer.handle_peer_storage(nodes[1].node.get_our_node_id(), msg);
4744
+ assert_eq!(*node_id, nodes[0].node.get_our_node_id());
4745
+ } else if let MessageSendEvent::SendYourPeerStorageMessage { ref node_id, ref msg } = msg {
4746
+ fundrecoverer.handle_your_peer_storage(nodes[1].node.get_our_node_id(), msg);
4747
+ assert_eq!(*node_id, nodes[0].node.get_our_node_id());
4748
+ } else {
4749
+ panic!("Unexpected event")
4750
+ }
4751
+ }
4752
+
4753
+ let bogus_chan_reestablish = fundrecoverer.get_and_clear_pending_msg_events();
4754
+ assert_eq!(bogus_chan_reestablish.len(), 1);
4755
+
4756
+ match bogus_chan_reestablish[0] {
4757
+ MessageSendEvent::SendChannelReestablish {ref node_id, ref msg} => {
4758
+ assert_eq!(nodes[1].node.get_our_node_id(), *node_id);
4759
+ nodes[1].node.handle_channel_reestablish(nodes[0].node.get_our_node_id(), msg);
4760
+ },
4761
+ _ => panic!("Unexpected event"),
4762
+ }
4763
+
4764
+
4765
+
4766
+ let block = create_dummy_block(nodes[0].best_block_hash(), 42, vec![revoked_local_txn[0].clone()]);
4767
+ connect_block(&nodes[1], &block);
4768
+ // Since we are using fundrecoverer as Chain::watch.
4769
+ let txdata: Vec<_> = block.txdata.iter().enumerate().collect();
4770
+ let height = nodes[0].best_block_info().1 + 1;
4771
+
4772
+ nodes[0].blocks.lock().unwrap().push((block.clone(), height));
4773
+ fundrecoverer.best_block_updated(&block.header, height);
4774
+ println!("here i am and i am here");
4775
+ fundrecoverer.transactions_confirmed(&block.header, &txdata, height);
4776
+ println!("here i am and i am here1");
4777
+ check_closed_broadcast!(nodes[1], true);
4778
+
4779
+ let events_2 = nodes[1].node.get_and_clear_pending_events();
4780
+ assert_eq!(events_2.len(), 1);
4781
+ match events_2[0] {
4782
+ Event::ChannelClosed {..} => {}, // If we actually processed we'd receive the payment
4783
+ _ => panic!("Unexpected event"),
4784
+ }
4785
+ check_added_monitors!(nodes[1], 1);
4786
+
4787
+ let panelty = node_cfgs[0].tx_broadcaster.txn_broadcasted.lock().unwrap().clone();
4788
+ assert_eq!(panelty.len(), 1);
4789
+ assert_eq!(panelty[0].input.len(), 1);
4790
+
4791
+ let block = create_dummy_block(nodes[1].best_block_hash(), 42, vec![panelty[0].clone()]);
4792
+ let txdata: Vec<_> = block.txdata.iter().enumerate().collect();
4793
+ connect_block(&nodes[1], &block);
4794
+
4795
+ nodes[0].blocks.lock().unwrap().push((block.clone(), height + 1));
4796
+ fundrecoverer.best_block_updated(&block.header, height + 1);
4797
+ println!("here i am and i am here");
4798
+ fundrecoverer.transactions_confirmed(&block.header, &txdata, height + 1);
4799
+
4800
+ let mut dummy_block = create_dummy_block(nodes[1].best_block_hash(), height, Vec::new());
4801
+ for i in 1..CHAN_CONFIRM_DEPTH {
4802
+ let prev_blockhash = dummy_block.header.block_hash();
4803
+ let dummy_txdata: Vec<_> = dummy_block.txdata.iter().enumerate().collect();
4804
+ fundrecoverer.best_block_updated(&dummy_block.header, height + i + 1);
4805
+ fundrecoverer.transactions_confirmed(&dummy_block.header, &dummy_txdata, height + i + 1);
4806
+ dummy_block = create_dummy_block(prev_blockhash, height + i + 1, Vec::new());
4807
+ }
4808
+
4809
+ // Lets drop the monitor and clear the chain_monitor as well.
4810
+ nodes[0].chain_source.clear_watched_txn_and_outputs();
4811
+
4812
+ for event in fundrecoverer.get_and_clear_pending_events() {
4813
+ match event {
4814
+ Event::SpendableOutputs { mut outputs, channel_id: _ } => {
4815
+ for outp in outputs.drain(..) {
4816
+ match outp {
4817
+ SpendableOutputDescriptor::StaticOutput{output, ..} => {
4818
+ assert_eq!(output.value.to_sat(), panelty[0].output[0].value.to_sat());
4819
+ },
4820
+ _ => panic!("Unexpected event"),
4821
+ }
4822
+ }
4823
+ },
4824
+ _ => panic!("Unexpected event"),
4825
+ };
4826
+ }
4827
+ }
4828
+
4689
4829
#[test]
4690
4830
fn test_claim_sizeable_push_msat() {
4691
4831
// Incidentally test SpendableOutput event generation due to detection of to_local output on commitment tx
0 commit comments