Merge pull request #2642 from Sharmalm/2346
[rust-lightning] / fuzz / src / chanmon_consistency.rs
index 8afc2e15187edb5397474a64b2cf33e34359a667..f654908771f0fec964b40384ce6178bd0b267c71 100644 (file)
@@ -20,9 +20,9 @@
 
 use bitcoin::blockdata::constants::genesis_block;
 use bitcoin::blockdata::transaction::{Transaction, TxOut};
-use bitcoin::blockdata::script::{Builder, Script};
+use bitcoin::blockdata::script::{Builder, ScriptBuf};
 use bitcoin::blockdata::opcodes;
-use bitcoin::blockdata::locktime::PackedLockTime;
+use bitcoin::blockdata::locktime::absolute::LockTime;
 use bitcoin::network::constants::Network;
 
 use bitcoin::hashes::Hash as TraitImport;
@@ -80,9 +80,9 @@ impl FeeEstimator for FuzzEstimator {
                // always return a HighPriority feerate here which is >= the maximum Normal feerate and a
                // Background feerate which is <= the minimum Normal feerate.
                match conf_target {
-                       ConfirmationTarget::HighPriority => MAX_FEE,
-                       ConfirmationTarget::Background|ConfirmationTarget::MempoolMinimum => 253,
-                       ConfirmationTarget::Normal => cmp::min(self.ret_val.load(atomic::Ordering::Acquire), MAX_FEE),
+                       ConfirmationTarget::OnChainSweep => MAX_FEE,
+                       ConfirmationTarget::ChannelCloseMinimum|ConfirmationTarget::AnchorChannelFee|ConfirmationTarget::MinAllowedAnchorChannelRemoteFee|ConfirmationTarget::MinAllowedNonAnchorChannelRemoteFee => 253,
+                       ConfirmationTarget::NonAnchorChannelFee => cmp::min(self.ret_val.load(atomic::Ordering::Acquire), MAX_FEE),
                }
        }
 }
@@ -138,7 +138,7 @@ impl TestChainMonitor {
        }
 }
 impl chain::Watch<TestChannelSigner> for TestChainMonitor {
-       fn watch_channel(&self, funding_txo: OutPoint, monitor: channelmonitor::ChannelMonitor<TestChannelSigner>) -> chain::ChannelMonitorUpdateStatus {
+       fn watch_channel(&self, funding_txo: OutPoint, monitor: channelmonitor::ChannelMonitor<TestChannelSigner>) -> Result<chain::ChannelMonitorUpdateStatus, ()> {
                let mut ser = VecWriter(Vec::new());
                monitor.write(&mut ser).unwrap();
                if let Some(_) = self.latest_monitors.lock().unwrap().insert(funding_txo, (monitor.get_latest_update_id(), ser.0)) {
@@ -155,7 +155,7 @@ impl chain::Watch<TestChannelSigner> for TestChainMonitor {
                };
                let deserialized_monitor = <(BlockHash, channelmonitor::ChannelMonitor<TestChannelSigner>)>::
                        read(&mut Cursor::new(&map_entry.get().1), (&*self.keys, &*self.keys)).unwrap().1;
-               deserialized_monitor.update_monitor(update, &&TestBroadcaster{}, &FuzzEstimator { ret_val: atomic::AtomicU32::new(253) }, &self.logger).unwrap();
+               deserialized_monitor.update_monitor(update, &&TestBroadcaster{}, &&FuzzEstimator { ret_val: atomic::AtomicU32::new(253) }, &self.logger).unwrap();
                let mut ser = VecWriter(Vec::new());
                deserialized_monitor.write(&mut ser).unwrap();
                map_entry.insert((update.update_id, ser.0));
@@ -230,14 +230,16 @@ impl NodeSigner for KeyProvider {
 }
 
 impl SignerProvider for KeyProvider {
-       type Signer = TestChannelSigner;
+       type EcdsaSigner = TestChannelSigner;
+       #[cfg(taproot)]
+       type TaprootSigner = TestChannelSigner;
 
        fn generate_channel_keys_id(&self, _inbound: bool, _channel_value_satoshis: u64, _user_channel_id: u128) -> [u8; 32] {
                let id = self.rand_bytes_id.fetch_add(1, atomic::Ordering::Relaxed) as u8;
                [id; 32]
        }
 
-       fn derive_channel_signer(&self, channel_value_satoshis: u64, channel_keys_id: [u8; 32]) -> Self::Signer {
+       fn derive_channel_signer(&self, channel_value_satoshis: u64, channel_keys_id: [u8; 32]) -> Self::EcdsaSigner {
                let secp_ctx = Secp256k1::signing_only();
                let id = channel_keys_id[0];
                let keys = InMemorySigner::new(
@@ -256,7 +258,7 @@ impl SignerProvider for KeyProvider {
                TestChannelSigner::new_with_revoked(keys, revoked_commitment, false)
        }
 
-       fn read_chan_signer(&self, buffer: &[u8]) -> Result<Self::Signer, DecodeError> {
+       fn read_chan_signer(&self, buffer: &[u8]) -> Result<Self::EcdsaSigner, DecodeError> {
                let mut reader = std::io::Cursor::new(buffer);
 
                let inner: InMemorySigner = ReadableArgs::read(&mut reader, self)?;
@@ -266,14 +268,15 @@ impl SignerProvider for KeyProvider {
                        inner,
                        state,
                        disable_revocation_policy_check: false,
+                       available: Arc::new(Mutex::new(true)),
                })
        }
 
-       fn get_destination_script(&self) -> Result<Script, ()> {
+       fn get_destination_script(&self, _channel_keys_id: [u8; 32]) -> Result<ScriptBuf, ()> {
                let secp_ctx = Secp256k1::signing_only();
                let channel_monitor_claim_key = SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, self.node_secret[31]]).unwrap();
                let our_channel_monitor_claim_key_hash = WPubkeyHash::hash(&PublicKey::from_secret_key(&secp_ctx, &channel_monitor_claim_key).serialize());
-               Ok(Builder::new().push_opcode(opcodes::all::OP_PUSHBYTES_0).push_slice(&our_channel_monitor_claim_key_hash[..]).into_script())
+               Ok(Builder::new().push_opcode(opcodes::all::OP_PUSHBYTES_0).push_slice(our_channel_monitor_claim_key_hash).into_script())
        }
 
        fn get_shutdown_scriptpubkey(&self) -> Result<ShutdownScript, ()> {
@@ -342,7 +345,7 @@ type ChanMan<'a> = ChannelManager<Arc<TestChainMonitor>, Arc<TestBroadcaster>, A
 fn get_payment_secret_hash(dest: &ChanMan, payment_id: &mut u8) -> Option<(PaymentSecret, PaymentHash)> {
        let mut payment_hash;
        for _ in 0..256 {
-               payment_hash = PaymentHash(Sha256::hash(&[*payment_id; 1]).into_inner());
+               payment_hash = PaymentHash(Sha256::hash(&[*payment_id; 1]).to_byte_array());
                if let Ok(payment_secret) = dest.create_inbound_payment_for_hash(payment_hash, None, 3600, None) {
                        return Some((payment_secret, payment_hash));
                }
@@ -371,6 +374,7 @@ fn send_payment(source: &ChanMan, dest: &ChanMan, dest_chan_id: u64, amt: u64, p
                        channel_features: dest.channel_features(),
                        fee_msat: amt,
                        cltv_expiry_delta: 200,
+                       maybe_announced_channel: true,
                }], blinded_tail: None }],
                route_params: None,
        }, payment_hash, RecipientOnionFields::secret_only(payment_secret), PaymentId(payment_id)) {
@@ -405,6 +409,7 @@ fn send_hop_payment(source: &ChanMan, middle: &ChanMan, middle_chan_id: u64, des
                        channel_features: middle.channel_features(),
                        fee_msat: first_hop_fee,
                        cltv_expiry_delta: 100,
+                       maybe_announced_channel: true,
                }, RouteHop {
                        pubkey: dest.get_our_node_id(),
                        node_features: dest.node_features(),
@@ -412,6 +417,7 @@ fn send_hop_payment(source: &ChanMan, middle: &ChanMan, middle_chan_id: u64, des
                        channel_features: dest.channel_features(),
                        fee_msat: amt,
                        cltv_expiry_delta: 200,
+                       maybe_announced_channel: true,
                }], blinded_tail: None }],
                route_params: None,
        }, payment_hash, RecipientOnionFields::secret_only(payment_secret), PaymentId(payment_id)) {
@@ -428,7 +434,7 @@ fn send_hop_payment(source: &ChanMan, middle: &ChanMan, middle_chan_id: u64, des
 }
 
 #[inline]
-pub fn do_test<Out: Output>(data: &[u8], underlying_out: Out) {
+pub fn do_test<Out: Output>(data: &[u8], underlying_out: Out, anchors: bool) {
        let out = SearchingOutput::new(underlying_out);
        let broadcast = Arc::new(TestBroadcaster{});
        let router = FuzzRouter {};
@@ -446,6 +452,10 @@ pub fn do_test<Out: Output>(data: &[u8], underlying_out: Out) {
                        let mut config = UserConfig::default();
                        config.channel_config.forwarding_fee_proportional_millionths = 0;
                        config.channel_handshake_config.announced_channel = true;
+                       if anchors {
+                               config.channel_handshake_config.negotiate_anchors_zero_fee_htlc_tx = true;
+                               config.manually_accept_inbound_channels = true;
+                       }
                        let network = Network::Bitcoin;
                        let best_block_timestamp = genesis_block(network).header.time;
                        let params = ChainParameters {
@@ -469,6 +479,10 @@ pub fn do_test<Out: Output>(data: &[u8], underlying_out: Out) {
                        let mut config = UserConfig::default();
                        config.channel_config.forwarding_fee_proportional_millionths = 0;
                        config.channel_handshake_config.announced_channel = true;
+                       if anchors {
+                               config.channel_handshake_config.negotiate_anchors_zero_fee_htlc_tx = true;
+                               config.manually_accept_inbound_channels = true;
+                       }
 
                        let mut monitors = HashMap::new();
                        let mut old_monitors = $old_monitors.latest_monitors.lock().unwrap();
@@ -497,7 +511,7 @@ pub fn do_test<Out: Output>(data: &[u8], underlying_out: Out) {
                        let res = (<(BlockHash, ChanMan)>::read(&mut Cursor::new(&$ser.0), read_args).expect("Failed to read manager").1, chain_monitor.clone());
                        for (funding_txo, mon) in monitors.drain() {
                                assert_eq!(chain_monitor.chain_monitor.watch_channel(funding_txo, mon),
-                                       ChannelMonitorUpdateStatus::Completed);
+                                       Ok(ChannelMonitorUpdateStatus::Completed));
                        }
                        res
                } }
@@ -505,7 +519,7 @@ pub fn do_test<Out: Output>(data: &[u8], underlying_out: Out) {
 
        let mut channel_txn = Vec::new();
        macro_rules! make_channel {
-               ($source: expr, $dest: expr, $chan_id: expr) => { {
+               ($source: expr, $dest: expr, $dest_keys_manager: expr, $chan_id: expr) => { {
                        $source.peer_connected(&$dest.get_our_node_id(), &Init {
                                features: $dest.init_features(), networks: None, remote_network_address: None
                        }, true).unwrap();
@@ -513,7 +527,7 @@ pub fn do_test<Out: Output>(data: &[u8], underlying_out: Out) {
                                features: $source.init_features(), networks: None, remote_network_address: None
                        }, false).unwrap();
 
-                       $source.create_channel($dest.get_our_node_id(), 100_000, 42, 0, None).unwrap();
+                       $source.create_channel($dest.get_our_node_id(), 100_000, 42, 0, None, None).unwrap();
                        let open_channel = {
                                let events = $source.get_and_clear_pending_msg_events();
                                assert_eq!(events.len(), 1);
@@ -524,6 +538,22 @@ pub fn do_test<Out: Output>(data: &[u8], underlying_out: Out) {
 
                        $dest.handle_open_channel(&$source.get_our_node_id(), &open_channel);
                        let accept_channel = {
+                               if anchors {
+                                       let events = $dest.get_and_clear_pending_events();
+                                       assert_eq!(events.len(), 1);
+                                       if let events::Event::OpenChannelRequest {
+                                               ref temporary_channel_id, ref counterparty_node_id, ..
+                                       } = events[0] {
+                                               let mut random_bytes = [0u8; 16];
+                                               random_bytes.copy_from_slice(&$dest_keys_manager.get_secure_random_bytes()[..16]);
+                                               let user_channel_id = u128::from_be_bytes(random_bytes);
+                                               $dest.accept_inbound_channel(
+                                                       temporary_channel_id,
+                                                       counterparty_node_id,
+                                                       user_channel_id,
+                                               ).unwrap();
+                                       } else { panic!("Wrong event type"); }
+                               }
                                let events = $dest.get_and_clear_pending_msg_events();
                                assert_eq!(events.len(), 1);
                                if let events::MessageSendEvent::SendAcceptChannel { ref msg, .. } = events[0] {
@@ -537,7 +567,7 @@ pub fn do_test<Out: Output>(data: &[u8], underlying_out: Out) {
                                let events = $source.get_and_clear_pending_events();
                                assert_eq!(events.len(), 1);
                                if let events::Event::FundingGenerationReady { ref temporary_channel_id, ref channel_value_satoshis, ref output_script, .. } = events[0] {
-                                       let tx = Transaction { version: $chan_id, lock_time: PackedLockTime::ZERO, input: Vec::new(), output: vec![TxOut {
+                                       let tx = Transaction { version: $chan_id, lock_time: LockTime::ZERO, input: Vec::new(), output: vec![TxOut {
                                                value: *channel_value_satoshis, script_pubkey: output_script.clone(),
                                        }]};
                                        funding_output = OutPoint { txid: tx.txid(), index: 0 };
@@ -635,8 +665,8 @@ pub fn do_test<Out: Output>(data: &[u8], underlying_out: Out) {
 
        let mut nodes = [node_a, node_b, node_c];
 
-       let chan_1_funding = make_channel!(nodes[0], nodes[1], 0);
-       let chan_2_funding = make_channel!(nodes[1], nodes[2], 1);
+       let chan_1_funding = make_channel!(nodes[0], nodes[1], keys_manager_b, 0);
+       let chan_2_funding = make_channel!(nodes[1], nodes[2], keys_manager_c, 1);
 
        for node in nodes.iter() {
                confirm_txn!(node);
@@ -1195,7 +1225,10 @@ pub fn do_test<Out: Output>(data: &[u8], underlying_out: Out) {
                        0x6d => { send_hop_payment(&nodes[2], &nodes[1], chan_b, &nodes[0], chan_a, 1, &mut payment_id, &mut payment_idx); },
 
                        0x80 => {
-                               let max_feerate = last_htlc_clear_fee_a * FEE_SPIKE_BUFFER_FEE_INCREASE_MULTIPLE as u32;
+                               let mut max_feerate = last_htlc_clear_fee_a;
+                               if !anchors {
+                                       max_feerate *= FEE_SPIKE_BUFFER_FEE_INCREASE_MULTIPLE as u32;
+                               }
                                if fee_est_a.ret_val.fetch_add(250, atomic::Ordering::AcqRel) + 250 > max_feerate {
                                        fee_est_a.ret_val.store(max_feerate, atomic::Ordering::Release);
                                }
@@ -1204,7 +1237,10 @@ pub fn do_test<Out: Output>(data: &[u8], underlying_out: Out) {
                        0x81 => { fee_est_a.ret_val.store(253, atomic::Ordering::Release); nodes[0].maybe_update_chan_fees(); },
 
                        0x84 => {
-                               let max_feerate = last_htlc_clear_fee_b * FEE_SPIKE_BUFFER_FEE_INCREASE_MULTIPLE as u32;
+                               let mut max_feerate = last_htlc_clear_fee_b;
+                               if !anchors {
+                                       max_feerate *= FEE_SPIKE_BUFFER_FEE_INCREASE_MULTIPLE as u32;
+                               }
                                if fee_est_b.ret_val.fetch_add(250, atomic::Ordering::AcqRel) + 250 > max_feerate {
                                        fee_est_b.ret_val.store(max_feerate, atomic::Ordering::Release);
                                }
@@ -1213,7 +1249,10 @@ pub fn do_test<Out: Output>(data: &[u8], underlying_out: Out) {
                        0x85 => { fee_est_b.ret_val.store(253, atomic::Ordering::Release); nodes[1].maybe_update_chan_fees(); },
 
                        0x88 => {
-                               let max_feerate = last_htlc_clear_fee_c * FEE_SPIKE_BUFFER_FEE_INCREASE_MULTIPLE as u32;
+                               let mut max_feerate = last_htlc_clear_fee_c;
+                               if !anchors {
+                                       max_feerate *= FEE_SPIKE_BUFFER_FEE_INCREASE_MULTIPLE as u32;
+                               }
                                if fee_est_c.ret_val.fetch_add(250, atomic::Ordering::AcqRel) + 250 > max_feerate {
                                        fee_est_c.ret_val.store(max_feerate, atomic::Ordering::Release);
                                }
@@ -1334,10 +1373,12 @@ impl<O: Output> SearchingOutput<O> {
 }
 
 pub fn chanmon_consistency_test<Out: Output>(data: &[u8], out: Out) {
-       do_test(data, out);
+       do_test(data, out.clone(), false);
+       do_test(data, out, true);
 }
 
 #[no_mangle]
 pub extern "C" fn chanmon_consistency_run(data: *const u8, datalen: usize) {
-       do_test(unsafe { std::slice::from_raw_parts(data, datalen) }, test_logger::DevNull{});
+       do_test(unsafe { std::slice::from_raw_parts(data, datalen) }, test_logger::DevNull{}, false);
+       do_test(unsafe { std::slice::from_raw_parts(data, datalen) }, test_logger::DevNull{}, true);
 }