Skip to content

Commit 33c06c5

Browse files
committed
Merge rust-bitcoin#4722: Add Arbitary impls for some bip152, bip158 & merkle block types
7afb798 Add Arbitary impls for some bip152, bip158 & merkle block types (Shing Him Ng) Pull request description: These are some children types of some `p2p` types for which I'm updating the fuzzing targets ACKs for top commit: tcharding: ACK 7afb798 apoelstra: ACK 7afb798; successfully ran local tests Tree-SHA512: 70bf0aa518e2144f02646403de818403a83795288cc23f5e65947d0c477a4d29cca65f096a5d5b63ff7808cac381dc4a6e7e65b4f0fc5086a8e186fe6f206450
2 parents f047946 + 7afb798 commit 33c06c5

3 files changed

Lines changed: 78 additions & 0 deletions

File tree

bitcoin/src/bip152.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ use core::{convert, fmt, mem};
99
#[cfg(feature = "std")]
1010
use std::error;
1111

12+
#[cfg(feature = "arbitrary")]
13+
use arbitrary::{Arbitrary, Unstructured};
1214
use hashes::{sha256, siphash24};
1315
use internals::array::ArrayExt as _;
1416
use internals::ToU64 as _;
@@ -404,6 +406,46 @@ impl BlockTransactions {
404406
}
405407
}
406408

409+
#[cfg(feature = "arbitrary")]
410+
impl<'a> Arbitrary<'a> for ShortId {
411+
fn arbitrary(u: &mut Unstructured<'a>) -> arbitrary::Result<Self> {
412+
Ok(ShortId(u.arbitrary()?))
413+
}
414+
}
415+
416+
#[cfg(feature = "arbitrary")]
417+
impl<'a> Arbitrary<'a> for PrefilledTransaction {
418+
fn arbitrary(u: &mut Unstructured<'a>) -> arbitrary::Result<Self> {
419+
Ok(PrefilledTransaction { idx: u.arbitrary()?, tx: u.arbitrary()? })
420+
}
421+
}
422+
423+
#[cfg(feature = "arbitrary")]
424+
impl<'a> Arbitrary<'a> for HeaderAndShortIds {
425+
fn arbitrary(u: &mut Unstructured<'a>) -> arbitrary::Result<Self> {
426+
Ok(HeaderAndShortIds {
427+
header: u.arbitrary()?,
428+
nonce: u.arbitrary()?,
429+
short_ids: Vec::<ShortId>::arbitrary(u)?,
430+
prefilled_txs: Vec::<PrefilledTransaction>::arbitrary(u)?,
431+
})
432+
}
433+
}
434+
435+
#[cfg(feature = "arbitrary")]
436+
impl<'a> Arbitrary<'a> for BlockTransactions {
437+
fn arbitrary(u: &mut Unstructured<'a>) -> arbitrary::Result<Self> {
438+
Ok(BlockTransactions { block_hash: u.arbitrary()?, transactions: Vec::<Transaction>::arbitrary(u)? })
439+
}
440+
}
441+
442+
#[cfg(feature = "arbitrary")]
443+
impl<'a> Arbitrary<'a> for BlockTransactionsRequest {
444+
fn arbitrary(u: &mut Unstructured<'a>) -> arbitrary::Result<Self> {
445+
Ok(BlockTransactionsRequest { block_hash: u.arbitrary()?, indexes: Vec::<u64>::arbitrary(u)? })
446+
}
447+
}
448+
407449
#[cfg(test)]
408450
mod test {
409451
use hex::FromHex;

bitcoin/src/bip158.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ use core::cmp::{self, Ordering};
4141
use core::convert::Infallible;
4242
use core::fmt;
4343

44+
#[cfg(feature = "arbitrary")]
45+
use arbitrary::{Arbitrary, Unstructured};
4446
use hashes::{sha256d, siphash24, HashEngine as _};
4547
use internals::array::ArrayExt as _;
4648
use internals::{write_err, ToU64 as _};
@@ -574,6 +576,20 @@ impl<'a, W: Write> BitStreamWriter<'a, W> {
574576
}
575577
}
576578

579+
#[cfg(feature = "arbitrary")]
580+
impl<'a> Arbitrary<'a> for FilterHash {
581+
fn arbitrary(u: &mut Unstructured<'a>) -> arbitrary::Result<Self> {
582+
Ok(FilterHash::from_byte_array(u.arbitrary()?))
583+
}
584+
}
585+
586+
#[cfg(feature = "arbitrary")]
587+
impl<'a> Arbitrary<'a> for FilterHeader {
588+
fn arbitrary(u: &mut Unstructured<'a>) -> arbitrary::Result<Self> {
589+
Ok(FilterHeader::from_byte_array(u.arbitrary()?))
590+
}
591+
}
592+
577593
#[cfg(test)]
578594
mod test {
579595
use std::collections::HashMap;

bitcoin/src/merkle_tree/block.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
use core::convert::Infallible;
1313
use core::fmt;
1414

15+
#[cfg(feature = "arbitrary")]
16+
use arbitrary::{Arbitrary, Unstructured};
1517
use internals::ToU64 as _;
1618
use io::{BufRead, Write};
1719

@@ -510,6 +512,24 @@ impl std::error::Error for MerkleBlockError {
510512
}
511513
}
512514

515+
#[cfg(feature = "arbitrary")]
516+
impl<'a> Arbitrary<'a> for PartialMerkleTree {
517+
fn arbitrary(u: &mut Unstructured<'a>) -> arbitrary::Result<Self> {
518+
Ok(PartialMerkleTree {
519+
num_transactions: u.arbitrary()?,
520+
bits: Vec::<bool>::arbitrary(u)?,
521+
hashes: Vec::<TxMerkleNode>::arbitrary(u)?,
522+
})
523+
}
524+
}
525+
526+
#[cfg(feature = "arbitrary")]
527+
impl<'a> Arbitrary<'a> for MerkleBlock {
528+
fn arbitrary(u: &mut Unstructured<'a>) -> arbitrary::Result<Self> {
529+
Ok(MerkleBlock { header: u.arbitrary()?, txn: u.arbitrary()? })
530+
}
531+
}
532+
513533
#[cfg(test)]
514534
mod tests {
515535
use hex::{DisplayHex, FromHex};

0 commit comments

Comments
 (0)