-
Notifications
You must be signed in to change notification settings - Fork 108
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Derive Arbitrary impls for a bunch of chain and network types (#2179)
Enable proptests for internal and external network protocol messages, using times with the correct protocol-specific ranges. (4 or 8 bytes.)
- Loading branch information
Showing
10 changed files
with
73 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
use chrono::{TimeZone, Utc, MAX_DATETIME, MIN_DATETIME}; | ||
use proptest::{arbitrary::any, prelude::*}; | ||
|
||
/// Returns a strategy that produces an arbitrary [`chrono::DateTime<Utc>`], | ||
/// based on the full valid range of the type. | ||
/// | ||
/// Both the seconds and nanoseconds values are randomised. But leap nanoseconds | ||
/// are never present. | ||
/// https://docs.rs/chrono/0.4.19/chrono/struct.DateTime.html#method.timestamp_subsec_nanos | ||
/// | ||
/// Zebra uses these times internally, via [`Utc::now`]. | ||
/// | ||
/// Some parts of the Zcash network protocol ([`Version`] messages) also use times | ||
/// with an 8-byte seconds value. Unlike this function, they have zero | ||
/// nanoseconds values. | ||
pub fn datetime_full() -> impl Strategy<Value = chrono::DateTime<Utc>> { | ||
( | ||
MIN_DATETIME.timestamp()..=MAX_DATETIME.timestamp(), | ||
0..1_000_000_000_u32, | ||
) | ||
.prop_map(|(secs, nsecs)| Utc.timestamp(secs, nsecs)) | ||
} | ||
|
||
/// Returns a strategy that produces an arbitrary time from a [`u32`] number | ||
/// of seconds past the epoch. | ||
/// | ||
/// The nanoseconds value is always zero. | ||
/// | ||
/// The Zcash protocol typically uses 4-byte seconds values, except for the | ||
/// [`Version`] message. | ||
pub fn datetime_u32() -> impl Strategy<Value = chrono::DateTime<Utc>> { | ||
any::<u32>().prop_map(|secs| Utc.timestamp(secs.into(), 0)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters