From 78037df1d4517a5119d1af14a2cc4c6463e55ab7 Mon Sep 17 00:00:00 2001 From: root Date: Fri, 25 Oct 2024 20:14:19 +0300 Subject: [PATCH 1/6] Added some more fuzzers and example inputs --- fuzz/in/keypair/examplekeys.bin | Bin 0 -> 95 bytes fuzz/in/normal_keypair/examplekeys.bin | Bin 0 -> 95 bytes fuzz/in/staking_contract/example_contract | Bin 0 -> 11 bytes fuzz/src/backups/00_testcases.rs | 60 ++++++++++++++++++++++ fuzz/src/backups/bitset.rs | 9 ++++ fuzz/src/backups/key_nibbles.rs | 9 ++++ fuzz/src/backups/trie_node.rs | 9 ++++ fuzz/src/bin/bitset.rs | 1 - fuzz/src/bin/coin.rs | 8 +++ fuzz/src/bin/htlc.rs | 10 ++++ fuzz/src/bin/key_nibbles.rs | 1 - fuzz/src/bin/keypair.rs | 8 +++ fuzz/src/bin/normal_keypair.rs | 9 ++++ fuzz/src/bin/staking_contract.rs | 8 +++ fuzz/src/bin/transaction.rs | 8 +++ fuzz/src/bin/trie_node.rs | 1 - 16 files changed, 138 insertions(+), 3 deletions(-) create mode 100644 fuzz/in/keypair/examplekeys.bin create mode 100644 fuzz/in/normal_keypair/examplekeys.bin create mode 100644 fuzz/in/staking_contract/example_contract create mode 100644 fuzz/src/backups/00_testcases.rs create mode 100644 fuzz/src/backups/bitset.rs create mode 100644 fuzz/src/backups/key_nibbles.rs create mode 100644 fuzz/src/backups/trie_node.rs create mode 100644 fuzz/src/bin/coin.rs create mode 100644 fuzz/src/bin/htlc.rs create mode 100644 fuzz/src/bin/keypair.rs create mode 100644 fuzz/src/bin/normal_keypair.rs create mode 100644 fuzz/src/bin/staking_contract.rs create mode 100644 fuzz/src/bin/transaction.rs diff --git a/fuzz/in/keypair/examplekeys.bin b/fuzz/in/keypair/examplekeys.bin new file mode 100644 index 0000000000000000000000000000000000000000..67de4825cc5d644f892bcb326624a853ff048a78 GIT binary patch literal 95 zcmV-l0HFVwBX|k(vwP5oh*Z+Gwr)(o3!$ObLwCb;^0v{xJ?wZjzpg%dxSMS=8$S2Z z>7Z(VX=lFx?fKxm{xr3V(IyU-KyC>YZcG^u`1YEd;TStYi8wxOd5_f)N;m-ey#SFG BFb)6! literal 0 HcmV?d00001 diff --git a/fuzz/in/normal_keypair/examplekeys.bin b/fuzz/in/normal_keypair/examplekeys.bin new file mode 100644 index 0000000000000000000000000000000000000000..67de4825cc5d644f892bcb326624a853ff048a78 GIT binary patch literal 95 zcmV-l0HFVwBX|k(vwP5oh*Z+Gwr)(o3!$ObLwCb;^0v{xJ?wZjzpg%dxSMS=8$S2Z z>7Z(VX=lFx?fKxm{xr3V(IyU-KyC>YZcG^u`1YEd;TStYi8wxOd5_f)N;m-ey#SFG BFb)6! literal 0 HcmV?d00001 diff --git a/fuzz/in/staking_contract/example_contract b/fuzz/in/staking_contract/example_contract new file mode 100644 index 0000000000000000000000000000000000000000..5bbb39a69018955b0d933fa5d477f7d0b0d9f752 GIT binary patch literal 11 KcmZQzfCB&k3jhHC literal 0 HcmV?d00001 diff --git a/fuzz/src/backups/00_testcases.rs b/fuzz/src/backups/00_testcases.rs new file mode 100644 index 0000000000..e6cf8300d8 --- /dev/null +++ b/fuzz/src/backups/00_testcases.rs @@ -0,0 +1,60 @@ +use std::{fs, io, iter}; + +use nimiq_collections::BitSet; +use nimiq_primitives::{ + key_nibbles::KeyNibbles, + trie::trie_node::{TrieNode, TrieNodeChild}, +}; +use nimiq_serde::Serialize; + +fn write_any(filename: &str, object: T) -> io::Result<()> { + let bytes = object.serialize_to_vec(); + fs::write(filename, bytes) +} + +fn trie_node_with_value() -> TrieNode { + let mut result = TrieNode::new_empty("".parse().unwrap()); + result.value = Some(vec![123]); + result +} + +fn trie_node_with_child() -> TrieNode { + let mut result = TrieNode::new_empty("".parse().unwrap()); + result.children[7] = Some(TrieNodeChild { + suffix: "ab".parse().unwrap(), + hash: Default::default(), + }); + result +} + +fn main() -> io::Result<()> { + let write = + |name: &str, object: KeyNibbles| write_any(&format!("in/key_nibbles/{name}"), object); + fs::create_dir_all("in/key_nibbles")?; + write("root", KeyNibbles::ROOT)?; + write("one0", "0".parse().unwrap())?; + write("one1", "1".parse().unwrap())?; + write("onef", "f".parse().unwrap())?; + write("two", "9a".parse().unwrap())?; + write("longer1", "68656c6c6f2c20776f726c6421".parse().unwrap())?; + write("longer2", "68656c6c6f2c20776f726c64215".parse().unwrap())?; + + let write = |name: &str, object: BitSet| write_any(&format!("in/bitset/{name}"), object); + fs::create_dir_all("in/bitset")?; + write("empty", iter::empty().collect())?; + write("one0", iter::once(0).collect())?; + write("one1", iter::once(1).collect())?; + write("one511", iter::once(511).collect())?; + write("one512", iter::once(512).collect())?; + write("full512", (0..512).collect())?; + + let write = |name: &str, object: TrieNode| write_any(&format!("in/trie_node/{name}"), object); + fs::create_dir_all("in/trie_node")?; + write("root", TrieNode::new_root())?; + write("root_incomplete", TrieNode::new_root_incomplete())?; + write("empty", TrieNode::new_empty("".parse().unwrap()))?; + write("with_child", trie_node_with_child())?; + write("with_value", trie_node_with_value())?; + + Ok(()) +} diff --git a/fuzz/src/backups/bitset.rs b/fuzz/src/backups/bitset.rs new file mode 100644 index 0000000000..1ce9a79a71 --- /dev/null +++ b/fuzz/src/backups/bitset.rs @@ -0,0 +1,9 @@ +fn main() { + #[cfg(feature = "fuzz")] + afl::fuzz!(|data: &[u8]| { + use nimiq_collections::BitSet; + use nimiq_serde::Deserialize as _; + + let _ = BitSet::deserialize_from_vec(data); + }) +} diff --git a/fuzz/src/backups/key_nibbles.rs b/fuzz/src/backups/key_nibbles.rs new file mode 100644 index 0000000000..ad694d570a --- /dev/null +++ b/fuzz/src/backups/key_nibbles.rs @@ -0,0 +1,9 @@ +fn main() { + #[cfg(feature = "fuzz")] + afl::fuzz!(|data: &[u8]| { + use nimiq_primitives::key_nibbles::KeyNibbles; + use nimiq_serde::Deserialize as _; + + let _ = KeyNibbles::deserialize_from_vec(data); + }) +} diff --git a/fuzz/src/backups/trie_node.rs b/fuzz/src/backups/trie_node.rs new file mode 100644 index 0000000000..dab2f0c23a --- /dev/null +++ b/fuzz/src/backups/trie_node.rs @@ -0,0 +1,9 @@ +fn main() { + #[cfg(feature = "fuzz")] + afl::fuzz!(|data: &[u8]| { + use nimiq_primitives::trie::trie_node::TrieNode; + use nimiq_serde::Deserialize as _; + + let _ = TrieNode::deserialize_from_vec(data); + }) +} diff --git a/fuzz/src/bin/bitset.rs b/fuzz/src/bin/bitset.rs index 1ce9a79a71..f4fdd5534f 100644 --- a/fuzz/src/bin/bitset.rs +++ b/fuzz/src/bin/bitset.rs @@ -3,7 +3,6 @@ fn main() { afl::fuzz!(|data: &[u8]| { use nimiq_collections::BitSet; use nimiq_serde::Deserialize as _; - let _ = BitSet::deserialize_from_vec(data); }) } diff --git a/fuzz/src/bin/coin.rs b/fuzz/src/bin/coin.rs new file mode 100644 index 0000000000..3332ffc860 --- /dev/null +++ b/fuzz/src/bin/coin.rs @@ -0,0 +1,8 @@ +fn main() { + #[cfg(feature = "fuzz")] + afl::fuzz!(|data: &[u8]| { + use nimiq_serde::Deserialize as _; + use nimiq_primitives::coin::Coin; + let _ = Coin::deserialize_from_vec(data); + }) +} diff --git a/fuzz/src/bin/htlc.rs b/fuzz/src/bin/htlc.rs new file mode 100644 index 0000000000..5cc5d319c3 --- /dev/null +++ b/fuzz/src/bin/htlc.rs @@ -0,0 +1,10 @@ +fn main() { + #[cfg(feature = "fuzz")] + afl::fuzz!(|data: &[u8]| { + use nimiq_serde::Deserialize as _; + use nimiq_account::{ + HashedTimeLockedContract + }; + let _ = HashedTimeLockedContract::deserialize_from_vec(data); + }) +} \ No newline at end of file diff --git a/fuzz/src/bin/key_nibbles.rs b/fuzz/src/bin/key_nibbles.rs index ad694d570a..263c21e16a 100644 --- a/fuzz/src/bin/key_nibbles.rs +++ b/fuzz/src/bin/key_nibbles.rs @@ -3,7 +3,6 @@ fn main() { afl::fuzz!(|data: &[u8]| { use nimiq_primitives::key_nibbles::KeyNibbles; use nimiq_serde::Deserialize as _; - let _ = KeyNibbles::deserialize_from_vec(data); }) } diff --git a/fuzz/src/bin/keypair.rs b/fuzz/src/bin/keypair.rs new file mode 100644 index 0000000000..a31196e973 --- /dev/null +++ b/fuzz/src/bin/keypair.rs @@ -0,0 +1,8 @@ +fn main() { + #[cfg(feature = "fuzz")] + afl::fuzz!(|data: &[u8]| { + use nimiq_bls::{KeyPair}; + use nimiq_serde::Deserialize as _; + let _ = KeyPair::deserialize_from_vec(data); + }) +} diff --git a/fuzz/src/bin/normal_keypair.rs b/fuzz/src/bin/normal_keypair.rs new file mode 100644 index 0000000000..5520fc1675 --- /dev/null +++ b/fuzz/src/bin/normal_keypair.rs @@ -0,0 +1,9 @@ +fn main() { + #[cfg(feature = "fuzz")] + afl::fuzz!(|data: &[u8]| { + use nimiq_keys::KeyPair; + use nimiq_serde::Deserialize as _; + use nimiq_primitives::coin::Coin; + let _ = KeyPair::deserialize_from_vec(data); + }) +} diff --git a/fuzz/src/bin/staking_contract.rs b/fuzz/src/bin/staking_contract.rs new file mode 100644 index 0000000000..03a3ff792d --- /dev/null +++ b/fuzz/src/bin/staking_contract.rs @@ -0,0 +1,8 @@ +fn main() { + #[cfg(feature = "fuzz")] + afl::fuzz!(|data: &[u8]| { + use nimiq_serde::Deserialize as _; + use nimiq_account::StakingContract; + let _ = StakingContract::deserialize_from_vec(data); + }) +} \ No newline at end of file diff --git a/fuzz/src/bin/transaction.rs b/fuzz/src/bin/transaction.rs new file mode 100644 index 0000000000..4af537c0cd --- /dev/null +++ b/fuzz/src/bin/transaction.rs @@ -0,0 +1,8 @@ +fn main() { + #[cfg(feature = "fuzz")] + afl::fuzz!(|data: &[u8]| { + use nimiq_serde::Deserialize as _; + use nimiq_transaction::{Transaction}; + let _ = Transaction::deserialize_from_vec(data); + }) +} diff --git a/fuzz/src/bin/trie_node.rs b/fuzz/src/bin/trie_node.rs index dab2f0c23a..b727628b50 100644 --- a/fuzz/src/bin/trie_node.rs +++ b/fuzz/src/bin/trie_node.rs @@ -3,7 +3,6 @@ fn main() { afl::fuzz!(|data: &[u8]| { use nimiq_primitives::trie::trie_node::TrieNode; use nimiq_serde::Deserialize as _; - let _ = TrieNode::deserialize_from_vec(data); }) } From c94bf25e9835cc30570471b0308dd167adcebedd Mon Sep 17 00:00:00 2001 From: root Date: Fri, 25 Oct 2024 20:16:51 +0300 Subject: [PATCH 2/6] Removed backup directory --- fuzz/src/backups/00_testcases.rs | 60 -------------------------------- fuzz/src/backups/bitset.rs | 9 ----- fuzz/src/backups/key_nibbles.rs | 9 ----- fuzz/src/backups/trie_node.rs | 9 ----- 4 files changed, 87 deletions(-) delete mode 100644 fuzz/src/backups/00_testcases.rs delete mode 100644 fuzz/src/backups/bitset.rs delete mode 100644 fuzz/src/backups/key_nibbles.rs delete mode 100644 fuzz/src/backups/trie_node.rs diff --git a/fuzz/src/backups/00_testcases.rs b/fuzz/src/backups/00_testcases.rs deleted file mode 100644 index e6cf8300d8..0000000000 --- a/fuzz/src/backups/00_testcases.rs +++ /dev/null @@ -1,60 +0,0 @@ -use std::{fs, io, iter}; - -use nimiq_collections::BitSet; -use nimiq_primitives::{ - key_nibbles::KeyNibbles, - trie::trie_node::{TrieNode, TrieNodeChild}, -}; -use nimiq_serde::Serialize; - -fn write_any(filename: &str, object: T) -> io::Result<()> { - let bytes = object.serialize_to_vec(); - fs::write(filename, bytes) -} - -fn trie_node_with_value() -> TrieNode { - let mut result = TrieNode::new_empty("".parse().unwrap()); - result.value = Some(vec![123]); - result -} - -fn trie_node_with_child() -> TrieNode { - let mut result = TrieNode::new_empty("".parse().unwrap()); - result.children[7] = Some(TrieNodeChild { - suffix: "ab".parse().unwrap(), - hash: Default::default(), - }); - result -} - -fn main() -> io::Result<()> { - let write = - |name: &str, object: KeyNibbles| write_any(&format!("in/key_nibbles/{name}"), object); - fs::create_dir_all("in/key_nibbles")?; - write("root", KeyNibbles::ROOT)?; - write("one0", "0".parse().unwrap())?; - write("one1", "1".parse().unwrap())?; - write("onef", "f".parse().unwrap())?; - write("two", "9a".parse().unwrap())?; - write("longer1", "68656c6c6f2c20776f726c6421".parse().unwrap())?; - write("longer2", "68656c6c6f2c20776f726c64215".parse().unwrap())?; - - let write = |name: &str, object: BitSet| write_any(&format!("in/bitset/{name}"), object); - fs::create_dir_all("in/bitset")?; - write("empty", iter::empty().collect())?; - write("one0", iter::once(0).collect())?; - write("one1", iter::once(1).collect())?; - write("one511", iter::once(511).collect())?; - write("one512", iter::once(512).collect())?; - write("full512", (0..512).collect())?; - - let write = |name: &str, object: TrieNode| write_any(&format!("in/trie_node/{name}"), object); - fs::create_dir_all("in/trie_node")?; - write("root", TrieNode::new_root())?; - write("root_incomplete", TrieNode::new_root_incomplete())?; - write("empty", TrieNode::new_empty("".parse().unwrap()))?; - write("with_child", trie_node_with_child())?; - write("with_value", trie_node_with_value())?; - - Ok(()) -} diff --git a/fuzz/src/backups/bitset.rs b/fuzz/src/backups/bitset.rs deleted file mode 100644 index 1ce9a79a71..0000000000 --- a/fuzz/src/backups/bitset.rs +++ /dev/null @@ -1,9 +0,0 @@ -fn main() { - #[cfg(feature = "fuzz")] - afl::fuzz!(|data: &[u8]| { - use nimiq_collections::BitSet; - use nimiq_serde::Deserialize as _; - - let _ = BitSet::deserialize_from_vec(data); - }) -} diff --git a/fuzz/src/backups/key_nibbles.rs b/fuzz/src/backups/key_nibbles.rs deleted file mode 100644 index ad694d570a..0000000000 --- a/fuzz/src/backups/key_nibbles.rs +++ /dev/null @@ -1,9 +0,0 @@ -fn main() { - #[cfg(feature = "fuzz")] - afl::fuzz!(|data: &[u8]| { - use nimiq_primitives::key_nibbles::KeyNibbles; - use nimiq_serde::Deserialize as _; - - let _ = KeyNibbles::deserialize_from_vec(data); - }) -} diff --git a/fuzz/src/backups/trie_node.rs b/fuzz/src/backups/trie_node.rs deleted file mode 100644 index dab2f0c23a..0000000000 --- a/fuzz/src/backups/trie_node.rs +++ /dev/null @@ -1,9 +0,0 @@ -fn main() { - #[cfg(feature = "fuzz")] - afl::fuzz!(|data: &[u8]| { - use nimiq_primitives::trie::trie_node::TrieNode; - use nimiq_serde::Deserialize as _; - - let _ = TrieNode::deserialize_from_vec(data); - }) -} From dd2a5eecb6164bd2892c0176e04e6a87349ee5ff Mon Sep 17 00:00:00 2001 From: root Date: Fri, 25 Oct 2024 21:16:59 +0300 Subject: [PATCH 3/6] Now builds fuzzers correctly --- Cargo.lock | 4 ++++ fuzz/Cargo.toml | 5 +++++ fuzz/src/bin/keypair.rs | 2 +- fuzz/src/bin/normal_keypair.rs | 3 +-- 4 files changed, 11 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 35cc40bd78..06c10887df 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3813,9 +3813,13 @@ name = "nimiq-fuzz" version = "1.0.0-rc.0" dependencies = [ "afl", + "nimiq-account", + "nimiq-bls", "nimiq-collections", + "nimiq-keys", "nimiq-primitives", "nimiq-serde", + "nimiq-transaction", ] [[package]] diff --git a/fuzz/Cargo.toml b/fuzz/Cargo.toml index 07b94fccec..564b543b8f 100644 --- a/fuzz/Cargo.toml +++ b/fuzz/Cargo.toml @@ -19,6 +19,11 @@ afl = { version = "0.15.11", optional = true } nimiq-collections = { workspace = true } nimiq-primitives = { workspace = true, features = ["key-nibbles", "serde-derive", "trie"] } nimiq-serde = { workspace = true } +nimiq-bls = { workspace = true } +nimiq-keys = { workspace = true } +nimiq-account = { workspace = true } +nimiq-transaction = { workspace = true } + [features] fuzz = ["afl"] diff --git a/fuzz/src/bin/keypair.rs b/fuzz/src/bin/keypair.rs index a31196e973..1dbfc594f4 100644 --- a/fuzz/src/bin/keypair.rs +++ b/fuzz/src/bin/keypair.rs @@ -1,8 +1,8 @@ fn main() { #[cfg(feature = "fuzz")] afl::fuzz!(|data: &[u8]| { - use nimiq_bls::{KeyPair}; use nimiq_serde::Deserialize as _; + use nimiq_bls::{KeyPair}; let _ = KeyPair::deserialize_from_vec(data); }) } diff --git a/fuzz/src/bin/normal_keypair.rs b/fuzz/src/bin/normal_keypair.rs index 5520fc1675..f04e9a82b3 100644 --- a/fuzz/src/bin/normal_keypair.rs +++ b/fuzz/src/bin/normal_keypair.rs @@ -1,9 +1,8 @@ fn main() { #[cfg(feature = "fuzz")] afl::fuzz!(|data: &[u8]| { - use nimiq_keys::KeyPair; use nimiq_serde::Deserialize as _; - use nimiq_primitives::coin::Coin; + use nimiq_keys::KeyPair; let _ = KeyPair::deserialize_from_vec(data); }) } From 8ab70baa894895dc150b0c2abedbaf6947dc282a Mon Sep 17 00:00:00 2001 From: root Date: Sat, 26 Oct 2024 00:18:09 +0300 Subject: [PATCH 4/6] Added "round-trip" fuzzing for staking contracts. --- fuzz/src/bin/staking_contract.rs | 57 ++++++++++++++++++++++++++++++-- 1 file changed, 54 insertions(+), 3 deletions(-) diff --git a/fuzz/src/bin/staking_contract.rs b/fuzz/src/bin/staking_contract.rs index 03a3ff792d..b4981b96fe 100644 --- a/fuzz/src/bin/staking_contract.rs +++ b/fuzz/src/bin/staking_contract.rs @@ -1,8 +1,59 @@ fn main() { #[cfg(feature = "fuzz")] afl::fuzz!(|data: &[u8]| { - use nimiq_serde::Deserialize as _; + use nimiq_serde::{Deserialize, Serialize}; use nimiq_account::StakingContract; - let _ = StakingContract::deserialize_from_vec(data); + let res = StakingContract::deserialize_from_vec(data); // err I think is of type DeserializeError + + // Now check if contr exists. If it does (aka. the original data was a valid staking contract) then try to serialize it back to a vector, then check if the original vector and the new vector are the same, if they aren't then there is a bug in the parsing logic. + + // The existance of error implies that contr does not exist. + + match res { + Ok(v) => { + // First calculate the minimum of the two. + let otherdata = StakingContract::serialize_to_vec(&v); + + /* + let buf = (0..64).collect::>(); + let z: &[u8; 64] = &(&buf[..]).try_into().unwrap(); + */ + + assert!((otherdata.len() <= data.len()), "The size of the serialized version was bigger than the original vector! This shouldn't happen!"); + //data.resize((otherdata.len()), 0); + + // let comparison_bullshit = data.collect::>(); // Collect the stuff + + // arg[..30].try_into().unwrap() + + let bullshit: &[u8] = data[..(otherdata.len())].try_into().unwrap(); // Yuck!!! + + assert_eq!(bullshit, otherdata); + }, + Err(e) => { + // println!("Error thing!!!\n"); + return; + }, + } + + /* + if (err == None) { + // Contr exists + assert!(contr, "contr didn't exist, even though err == None!!!!"); // Debug. + // Now try to serialize to vec + let serialized = StakingContract::serialize_to_vec(contr); + // Now check if they are equal: + assert_eq!(data, serialized); + } + */ + + }) -} \ No newline at end of file +} + +/* + let contract_2a = + StakingContract::deserialize_from_vec(&contract_2.serialize_to_vec()).unwrap(); + + assert_eq!(contract_2, contract_2a); +*/ From 29bc98f6c4bc22f504c34acb76a7c148017f5898 Mon Sep 17 00:00:00 2001 From: root Date: Mon, 4 Nov 2024 03:37:57 +0200 Subject: [PATCH 5/6] Cleaned up code as per the suggestions at https://github.com/nimiq/core-rs-albatross/pull/3003/files/8ab70baa894895dc150b0c2abedbaf6947dc282a --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index ad7c4f6587..3f10975053 100644 --- a/README.md +++ b/README.md @@ -80,6 +80,8 @@ Alternatively, you can install it directly from git: cargo install --git https://github.com/nimiq/core-rs-albatross.git ``` +some changes here maybe + ## Documentation Extensive documentation explaining how the protocol is built up, the JSON-RPC specification, how to get started building applications on top of Nimiq and more can be found at the [Nimiq Developer Center](https://www.nimiq.com/developers/). From 3f13e9e0d49d5bd32953f243bb78f1b028ab2704 Mon Sep 17 00:00:00 2001 From: root Date: Mon, 4 Nov 2024 03:39:58 +0200 Subject: [PATCH 6/6] Cleaned up code --- README.md | 2 -- fuzz/Cargo.toml | 7 ++--- fuzz/src/bin/staking_contract.rs | 52 +++++--------------------------- 3 files changed, 10 insertions(+), 51 deletions(-) diff --git a/README.md b/README.md index 3f10975053..ad7c4f6587 100644 --- a/README.md +++ b/README.md @@ -80,8 +80,6 @@ Alternatively, you can install it directly from git: cargo install --git https://github.com/nimiq/core-rs-albatross.git ``` -some changes here maybe - ## Documentation Extensive documentation explaining how the protocol is built up, the JSON-RPC specification, how to get started building applications on top of Nimiq and more can be found at the [Nimiq Developer Center](https://www.nimiq.com/developers/). diff --git a/fuzz/Cargo.toml b/fuzz/Cargo.toml index 564b543b8f..eb0747a800 100644 --- a/fuzz/Cargo.toml +++ b/fuzz/Cargo.toml @@ -16,14 +16,13 @@ workspace = true [dependencies] afl = { version = "0.15.11", optional = true } +nimiq-account = { workspace = true } +nimiq-bls = { workspace = true } nimiq-collections = { workspace = true } +nimiq-keys = { workspace = true } nimiq-primitives = { workspace = true, features = ["key-nibbles", "serde-derive", "trie"] } nimiq-serde = { workspace = true } -nimiq-bls = { workspace = true } -nimiq-keys = { workspace = true } -nimiq-account = { workspace = true } nimiq-transaction = { workspace = true } - [features] fuzz = ["afl"] diff --git a/fuzz/src/bin/staking_contract.rs b/fuzz/src/bin/staking_contract.rs index b4981b96fe..84b0e22437 100644 --- a/fuzz/src/bin/staking_contract.rs +++ b/fuzz/src/bin/staking_contract.rs @@ -4,56 +4,18 @@ fn main() { use nimiq_serde::{Deserialize, Serialize}; use nimiq_account::StakingContract; let res = StakingContract::deserialize_from_vec(data); // err I think is of type DeserializeError - - // Now check if contr exists. If it does (aka. the original data was a valid staking contract) then try to serialize it back to a vector, then check if the original vector and the new vector are the same, if they aren't then there is a bug in the parsing logic. - - // The existance of error implies that contr does not exist. - + // Now check if contract exists. If it does (aka. the original data was a valid staking contract) then try to serialize it back to a vector, then check if the original vector and the new vector are the same, if they aren't then there is a bug in the parsing logic. + // The existence of error implies that contract does not exist. match res { Ok(v) => { - // First calculate the minimum of the two. - let otherdata = StakingContract::serialize_to_vec(&v); - - /* - let buf = (0..64).collect::>(); - let z: &[u8; 64] = &(&buf[..]).try_into().unwrap(); - */ - - assert!((otherdata.len() <= data.len()), "The size of the serialized version was bigger than the original vector! This shouldn't happen!"); - //data.resize((otherdata.len()), 0); - - // let comparison_bullshit = data.collect::>(); // Collect the stuff - - // arg[..30].try_into().unwrap() - - let bullshit: &[u8] = data[..(otherdata.len())].try_into().unwrap(); // Yuck!!! - - assert_eq!(bullshit, otherdata); + let serialized = StakingContract::serialize_to_vec(&v); + assert!((serialized.len() <= data.len()), "The size of the serialized version was bigger than the original vector! This shouldn't happen!"); + let original_data_segment: &[u8] = data[..(serialized.len())].try_into().unwrap(); // This ugly stuff has to be done, because the serialization function ignores extra bytes at the end so we can not compare the byte vectors by themselves. Yuck!!! + assert_eq!(original_data_segment, serialized); }, Err(e) => { - // println!("Error thing!!!\n"); return; }, } - - /* - if (err == None) { - // Contr exists - assert!(contr, "contr didn't exist, even though err == None!!!!"); // Debug. - // Now try to serialize to vec - let serialized = StakingContract::serialize_to_vec(contr); - // Now check if they are equal: - assert_eq!(data, serialized); - } - */ - - }) -} - -/* - let contract_2a = - StakingContract::deserialize_from_vec(&contract_2.serialize_to_vec()).unwrap(); - - assert_eq!(contract_2, contract_2a); -*/ +} \ No newline at end of file