migration to rust-bitcoin 0.17
[rust-lightning] / fuzz / fuzz_targets / full_stack_target.rs
index 067c73d217e2517c4e2f43c03aaad92895d898d7..937c735064bed101861437f74372c85eba988747 100644 (file)
@@ -18,12 +18,13 @@ use bitcoin::blockdata::script::{Builder, Script};
 use bitcoin::blockdata::opcodes;
 use bitcoin::consensus::encode::deserialize;
 use bitcoin::network::constants::Network;
-use bitcoin::util::hash::{BitcoinHash, Sha256dHash};
+use bitcoin::util::hash::BitcoinHash;
 
 use bitcoin_hashes::Hash as TraitImport;
 use bitcoin_hashes::HashEngine as TraitImportEngine;
 use bitcoin_hashes::sha256::Hash as Sha256;
 use bitcoin_hashes::hash160::Hash as Hash160;
+use bitcoin_hashes::sha256d::Hash as Sha256dHash;
 
 use lightning::chain::chaininterface::{BroadcasterInterface,ConfirmationTarget,ChainListener,FeeEstimator,ChainWatchInterfaceUtil};
 use lightning::chain::transaction::OutPoint;
@@ -281,6 +282,29 @@ impl KeysInterface for KeyProvider {
                fill_bytes(&mut session_key);
                SecretKey::from_slice(&session_key).unwrap()
        }
+
+       fn get_channel_id(&self) -> [u8; 32] {
+               let mut channel_id = [0; 32];
+               fill_bytes(&mut channel_id);
+               for i in 0..4 {
+                       // byteswap the u64s in channel_id to make it distinct from get_session_key (and match
+                       // old code that wrote out different endianness).
+                       let mut t;
+                       t = channel_id[i*8 + 0];
+                       channel_id[i*8 + 0] = channel_id[i*8 + 7];
+                       channel_id[i*8 + 7] = t;
+                       t = channel_id[i*8 + 1];
+                       channel_id[i*8 + 1] = channel_id[i*8 + 6];
+                       channel_id[i*8 + 6] = t;
+                       t = channel_id[i*8 + 2];
+                       channel_id[i*8 + 2] = channel_id[i*8 + 5];
+                       channel_id[i*8 + 5] = t;
+                       t = channel_id[i*8 + 3];
+                       channel_id[i*8 + 3] = channel_id[i*8 + 4];
+                       channel_id[i*8 + 4] = t;
+               }
+               channel_id
+       }
 }
 
 #[inline]
@@ -428,7 +452,7 @@ pub fn do_test(data: &[u8], logger: &Arc<Logger>) {
                                        // fulfill this HTLC, but if they are, we can just take the first byte and
                                        // place that anywhere in our preimage.
                                        if &payment.0[1..] != &[0; 31] {
-                                               channelmanager.fail_htlc_backwards(&payment, 0);
+                                               channelmanager.fail_htlc_backwards(&payment);
                                        } else {
                                                let mut payment_preimage = PaymentPreimage([0; 32]);
                                                payment_preimage.0[0] = payment.0[0];
@@ -438,7 +462,7 @@ pub fn do_test(data: &[u8], logger: &Arc<Logger>) {
                        },
                        9 => {
                                for payment in payments_received.drain(..) {
-                                       channelmanager.fail_htlc_backwards(&payment, 0);
+                                       channelmanager.fail_htlc_backwards(&payment);
                                }
                        },
                        10 => {
@@ -531,7 +555,7 @@ pub fn do_test(data: &[u8], logger: &Arc<Logger>) {
 #[cfg(feature = "afl")]
 fn main() {
        fuzz!(|data| {
-               let logger: Arc<Logger> = Arc::new(test_logger::TestLogger{});
+               let logger: Arc<Logger> = Arc::new(test_logger::TestLogger::new("".to_owned()));
                do_test(data, &logger);
        });
 }
@@ -542,7 +566,7 @@ fn main() {
 fn main() {
        loop {
                fuzz!(|data| {
-                       let logger: Arc<Logger> = Arc::new(test_logger::TestLogger{});
+                       let logger: Arc<Logger> = Arc::new(test_logger::TestLogger::new("".to_owned()));
                        do_test(data, &logger);
                });
        }
@@ -552,7 +576,7 @@ fn main() {
 #[macro_use] extern crate libfuzzer_sys;
 #[cfg(feature = "libfuzzer_fuzz")]
 fuzz_target!(|data: &[u8]| {
-       let logger: Arc<Logger> = Arc::new(test_logger::TestLogger{});
+       let logger: Arc<Logger> = Arc::new(test_logger::TestLogger::new("".to_owned()));
        do_test(data, &logger);
 });
 
@@ -566,7 +590,7 @@ mod tests {
 
        #[test]
        fn duplicate_crash() {
-               let logger: Arc<Logger> = Arc::new(test_logger::TestLogger{});
+               let logger: Arc<Logger> = Arc::new(test_logger::TestLogger::new("".to_owned()));
                super::do_test(&::hex::decode("00").unwrap(), &logger);
        }
 
@@ -592,6 +616,12 @@ mod tests {
                // What each byte represents is broken down below, and then everything is concatenated into
                // one large test at the end (you want %s/ -.*//g %s/\n\| \|\t\|\///g).
 
+               // Following BOLT 8, lightning message on the wire are: 2-byte encrypted message length + 
+               // 16-byte MAC of the encrypted message length + encrypted Lightning message + 16-byte MAC
+               // of the Lightning message
+               // I.e 2nd inbound read, len 18 : 0006 (encrypted message length) + 03000000000000000000000000000000 (MAC of the encrypted message length)
+               // Len 22 : 0010 00000000 (encrypted lightning message) + 03000000000000000000000000000000 (MAC of the Lightning message)
+
                // 0000000000000000000000000000000000000000000000000000000000000000 - our network key
                // 00000000 - fee_proportional_millionths
                // 01 - announce_channels_publicly
@@ -604,7 +634,7 @@ mod tests {
                // 030012 - inbound read from peer id 0 of len 18
                // 0006 03000000000000000000000000000000 - message header indicating message length 6
                // 030016 - inbound read from peer id 0 of len 22
-               // 0010 00000000 03000000000000000000000000000000 - init message with no features (type 16)
+               // 0010 00000000 03000000000000000000000000000000 - init message with no features (type 16) and mac
                //
                // 030012 - inbound read from peer id 0 of len 18
                // 0141 03000000000000000000000000000000 - message header indicating message length 321
@@ -613,7 +643,7 @@ mod tests {
                // 030053 - inbound read from peer id 0 of len 83
                // 030000000000000000000000000000000000000000000000000000000000000005 030000000000000000000000000000000000000000000000000000000000000000 01 03000000000000000000000000000000 - rest of open_channel and mac
                //
-               // 00fd00fd00fd - Three feerate requests (all returning min feerate, which our open_channel also uses)
+               // 00fd00fd00fd - Three feerate requests (all returning min feerate, which our open_channel also uses) (gonna be ingested by FuzzEstimator)
                // - client should now respond with accept_channel (CHECK 1: type 33 to peer 03000000)
                //
                // 030012 - inbound read from peer id 0 of len 18
@@ -655,7 +685,7 @@ mod tests {
                // 0010 00000000 01000000000000000000000000000000 - init message with no features (type 16)
                //
                // 05 01 030200000000000000000000000000000000000000000000000000000000000000 00c350 0003e8 - create outbound channel to peer 1 for 50k sat
-               // 00fd00fd00fd - Three feerate requests (all returning min feerate)
+               // 00fd00fd00fd - Three feerate requests (all returning min feerate) (gonna be ingested by FuzzEstimator)
                //
                // 030112 - inbound read from peer id 1 of len 18
                // 0110 01000000000000000000000000000000 - message header indicating message length 272
@@ -694,7 +724,7 @@ mod tests {
                // 0300c1 - inbound read from peer id 0 of len 193
                // ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff ef00000000000000000000000000000000000000000000000000000000000000 03000000000000000000000000000000 - end of update_add_htlc from 0 to 1 via client and mac
                //
-               // 00fd - A feerate request (returning min feerate, which our open_channel also uses)
+               // 00fd - A feerate request (returning min feerate, which our open_channel also uses) (gonna be ingested by FuzzEstimator)
                //
                // 030012 - inbound read from peer id 0 of len 18
                // 0064 03000000000000000000000000000000 - message header indicating message length 100
@@ -753,7 +783,7 @@ mod tests {
                // 0300c1 - inbound read from peer id 0 of len 193
                // ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff ef00000000000000000000000000000000000000000000000000000000000000 03000000000000000000000000000000 - end of update_add_htlc from 0 to 1 via client and mac
                //
-               // 00fd - A feerate request (returning min feerate, which our open_channel also uses)
+               // 00fd - A feerate request (returning min feerate, which our open_channel also uses) (gonna be ingested by FuzzEstimator)
                //
                // - now respond to the update_fulfill_htlc+commitment_signed messages the client sent to peer 0
                // 030012 - inbound read from peer id 0 of len 18
@@ -831,7 +861,7 @@ mod tests {
                // 0300c1 - inbound read from peer id 0 of len 193
                // ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff ef00000000000000000000000000000000000000000000000000000000000000 03000000000000000000000000000000 - end of update_add_htlc from 0 to 1 via client and mac
                //
-               // 00fd - A feerate request (returning min feerate, which our open_channel also uses)
+               // 00fd - A feerate request (returning min feerate, which our open_channel also uses) (gonna be ingested by FuzzEstimator)
                //
                // 030012 - inbound read from peer id 0 of len 18
                // 00a4 03000000000000000000000000000000 - message header indicating message length 164
@@ -848,10 +878,10 @@ mod tests {
                // - client now sends id 1 update_add_htlc and commitment_signed (CHECK 7 duplicate)
                //
                // 0c007d - connect a block with one transaction of len 125
-               // 02000000013f00000000000000000000000000000000000000000000000000000000000000000000000000000080020001000000000000220020e2000000000000000000000000000000000000000000000000000000000000006cc10000000000001600142e0000000000000000000000000000000000000005000020 - the funding transaction
-               // 00fd - A feerate request (returning min feerate, which our open_channel also uses)
+               // 02000000013f00000000000000000000000000000000000000000000000000000000000000000000000000000080020001000000000000220020e2000000000000000000000000000000000000000000000000000000000000006cc10000000000001600142e0000000000000000000000000000000000000005000020 - the commitment transaction for channel 3f00000000000000000000000000000000000000000000000000000000000000
+               // 00fd - A feerate request (returning min feerate, which our open_channel also uses) (gonna be ingested by FuzzEstimator)
                // 0c005e - connect a block with one transaction of len 94
-               // 0200000001fb00000000000000000000000000000000000000000000000000000000000000000000000000000000014f00000000000000220020f60000000000000000000000000000000000000000000000000000000000000000000000 - the funding transaction
+               // 0200000001fb00000000000000000000000000000000000000000000000000000000000000000000000000000000014f00000000000000220020f60000000000000000000000000000000000000000000000000000000000000000000000 - the commitment transaction for channel 3d00000000000000000000000000000000000000000000000000000000000000
                //
                // 07 - process the now-pending HTLC forward
                // - client now fails the HTLC backwards as it was unable to extract the payment preimage (CHECK 9 duplicate and CHECK 10)