Skip to content

Commit

Permalink
types: enable proptest::Arbitrary impls via the 'proptest' feature
Browse files Browse the repository at this point in the history
  • Loading branch information
bmwill committed Dec 4, 2024
1 parent 5062e71 commit 6918fd8
Show file tree
Hide file tree
Showing 26 changed files with 162 additions and 156 deletions.
12 changes: 5 additions & 7 deletions crates/sui-sdk-types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ serde = ["dep:serde", "dep:serde_derive", "dep:serde_with", "dep:bcs", "dep:serd
schemars = ["serde", "dep:schemars", "dep:serde_json"]
rand = ["dep:rand_core"]
hash = ["dep:blake2"]
proptest = ["dep:proptest", "dep:test-strategy", "serde"]

[dependencies]
base64ct = { version = "1.6.0", features = ["alloc"] }
Expand All @@ -52,20 +53,17 @@ rand_core = { version = "0.6.4", optional = true }
# Hash support
blake2 = { version = "0.10.6", optional = true }

# proptest support
proptest = { version = "1.5.0", default-features = false, features = ["std"], optional = true }
test-strategy = { version = "0.4", optional = true }

[dev-dependencies]
bcs = "0.1.6"
serde_json = "1.0.128"
num-bigint = "0.4.6"
jsonschema = { version = "0.20", default-features = false }
paste = "1.0.15"

# proptest support in tests
#
# Pin to this specific commit in order to work around an issue where proptest doesn't build properly in wasm environments
# see https://github.com/proptest-rs/proptest/pull/270 for more info
proptest = { git = "https://github.com/bmwill/proptest.git", rev = "bc36db126183bce18c8bc595f0c0cfeac48b870c", default-features = false, features = ["std"] }
test-strategy = "0.4"

[target.wasm32-unknown-unknown.dev-dependencies]
wasm-bindgen-test = "0.3"
getrandom = { version = "0.2", features = ["js"] }
Expand Down
12 changes: 10 additions & 2 deletions crates/sui-sdk-types/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,20 @@ clippy:

.PHONY: test
test:
cargo nextest run --all-features
# Note on proptest:
#
# Pin to this specific commit in order to work around an issue where proptest doesn't build properly in wasm environments
# see https://github.com/proptest-rs/proptest/pull/270 for more info
cargo nextest run --all-features --config 'patch.crates-io.proptest.git="https://github.com/bmwill/proptest.git"' --config 'patch.crates-io.proptest.rev="0a140789d25f4999632b8b88aedb1e2ba056151d"'
cargo test --doc

.PHONY: wasm
wasm:
CC=clang wasm-pack test --node --all-features
# Note on proptest:
#
# Pin to this specific commit in order to work around an issue where proptest doesn't build properly in wasm environments
# see https://github.com/proptest-rs/proptest/pull/270 for more info
CC=clang wasm-pack test --node --all-features --config 'patch.crates-io.proptest.git="https://github.com/bmwill/proptest.git"' --config 'patch.crates-io.proptest.rev="0a140789d25f4999632b8b88aedb1e2ba056151d"'

%:
$(MAKE) -C ../.. $@
2 changes: 1 addition & 1 deletion crates/sui-sdk-types/src/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ mod signing_message {
/// to ensure no hashing collision for any ObjectId vs Address which is derived
/// as the hash of `flag || pubkey`.
#[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)]
#[cfg_attr(test, derive(test_strategy::Arbitrary))]
#[cfg_attr(feature = "proptest", derive(test_strategy::Arbitrary))]
#[repr(u8)]
enum HashingIntent {
#[cfg(feature = "serde")]
Expand Down
2 changes: 1 addition & 1 deletion crates/sui-sdk-types/src/types/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
feature = "serde",
derive(serde_derive::Serialize, serde_derive::Deserialize)
)]
#[cfg_attr(test, derive(test_strategy::Arbitrary))]
#[cfg_attr(feature = "proptest", derive(test_strategy::Arbitrary))]
pub struct Address(
#[cfg_attr(
feature = "serde",
Expand Down
26 changes: 13 additions & 13 deletions crates/sui-sdk-types/src/types/checkpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub type ProtocolVersion = u64;
derive(schemars::JsonSchema),
schemars(tag = "type", rename_all = "snake_case")
)]
#[cfg_attr(test, derive(test_strategy::Arbitrary))]
#[cfg_attr(feature = "proptest", derive(test_strategy::Arbitrary))]
pub enum CheckpointCommitment {
EcmhLiveObjectSet { digest: Digest },
// Other commitment types (e.g. merkle roots) go here.
Expand All @@ -36,7 +36,7 @@ pub enum CheckpointCommitment {
derive(serde_derive::Serialize, serde_derive::Deserialize)
)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[cfg_attr(test, derive(test_strategy::Arbitrary))]
#[cfg_attr(feature = "proptest", derive(test_strategy::Arbitrary))]
pub struct EndOfEpochData {
/// next_epoch_committee is `Some` if and only if the current checkpoint is
/// the last checkpoint of an epoch.
Expand All @@ -59,7 +59,7 @@ pub struct EndOfEpochData {

#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[cfg_attr(test, derive(test_strategy::Arbitrary))]
#[cfg_attr(feature = "proptest", derive(test_strategy::Arbitrary))]
pub struct CheckpointSummary {
#[cfg_attr(feature = "schemars", schemars(with = "crate::_schemars::U64"))]
pub epoch: EpochId,
Expand Down Expand Up @@ -109,17 +109,17 @@ pub struct CheckpointSummary {
derive(serde_derive::Serialize, serde_derive::Deserialize)
)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[cfg_attr(test, derive(test_strategy::Arbitrary))]
#[cfg_attr(feature = "proptest", derive(test_strategy::Arbitrary))]
pub struct SignedCheckpointSummary {
pub checkpoint: CheckpointSummary,
pub signature: ValidatorAggregatedSignature,
}

#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[cfg_attr(test, derive(test_strategy::Arbitrary))]
#[cfg_attr(feature = "proptest", derive(test_strategy::Arbitrary))]
pub struct CheckpointContents(
#[cfg_attr(test, any(proptest::collection::size_range(0..=2).lift()))]
#[cfg_attr(feature = "proptest", any(proptest::collection::size_range(0..=2).lift()))]
Vec<CheckpointTransactionInfo>,
);

Expand All @@ -143,11 +143,11 @@ impl CheckpointContents {
derive(serde_derive::Serialize, serde_derive::Deserialize)
)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[cfg_attr(test, derive(test_strategy::Arbitrary))]
#[cfg_attr(feature = "proptest", derive(test_strategy::Arbitrary))]
pub struct CheckpointTransactionInfo {
pub transaction: TransactionDigest,
pub effects: TransactionEffectsDigest,
#[cfg_attr(test, any(proptest::collection::size_range(0..=2).lift()))]
#[cfg_attr(feature = "proptest", any(proptest::collection::size_range(0..=2).lift()))]
pub signatures: Vec<UserSignature>,
}

Expand All @@ -157,11 +157,11 @@ pub struct CheckpointTransactionInfo {
derive(serde_derive::Serialize, serde_derive::Deserialize)
)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[cfg_attr(test, derive(test_strategy::Arbitrary))]
#[cfg_attr(feature = "proptest", derive(test_strategy::Arbitrary))]
pub struct CheckpointData {
pub checkpoint_summary: SignedCheckpointSummary,
pub checkpoint_contents: CheckpointContents,
#[cfg_attr(test, any(proptest::collection::size_range(0..=1).lift()))]
#[cfg_attr(feature = "proptest", any(proptest::collection::size_range(0..=1).lift()))]
pub transactions: Vec<CheckpointTransaction>,
}

Expand All @@ -171,7 +171,7 @@ pub struct CheckpointData {
derive(serde_derive::Serialize, serde_derive::Deserialize)
)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[cfg_attr(test, derive(test_strategy::Arbitrary))]
#[cfg_attr(feature = "proptest", derive(test_strategy::Arbitrary))]
pub struct CheckpointTransaction {
/// The input Transaction
#[cfg_attr(
Expand All @@ -185,10 +185,10 @@ pub struct CheckpointTransaction {
/// The events, if any, emitted by this transaciton during execution
pub events: Option<TransactionEvents>,
/// The state of all inputs to this transaction as they were prior to execution.
#[cfg_attr(test, any(proptest::collection::size_range(0..=2).lift()))]
#[cfg_attr(feature = "proptest", any(proptest::collection::size_range(0..=2).lift()))]
pub input_objects: Vec<Object>,
/// The state of all output objects created or mutated by this transaction.
#[cfg_attr(test, any(proptest::collection::size_range(0..=2).lift()))]
#[cfg_attr(feature = "proptest", any(proptest::collection::size_range(0..=2).lift()))]
pub output_objects: Vec<Object>,
}

Expand Down
4 changes: 2 additions & 2 deletions crates/sui-sdk-types/src/types/crypto/bls12381.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
derive(serde_derive::Serialize, serde_derive::Deserialize)
)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[cfg_attr(test, derive(test_strategy::Arbitrary))]
#[cfg_attr(feature = "proptest", derive(test_strategy::Arbitrary))]
pub struct Bls12381PublicKey(
#[cfg_attr(
feature = "serde",
Expand Down Expand Up @@ -107,7 +107,7 @@ impl std::fmt::Debug for Bls12381PublicKey {
derive(serde_derive::Serialize, serde_derive::Deserialize)
)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[cfg_attr(test, derive(test_strategy::Arbitrary))]
#[cfg_attr(feature = "proptest", derive(test_strategy::Arbitrary))]
pub struct Bls12381Signature(
#[cfg_attr(
feature = "serde",
Expand Down
4 changes: 2 additions & 2 deletions crates/sui-sdk-types/src/types/crypto/ed25519.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
derive(serde_derive::Serialize, serde_derive::Deserialize)
)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[cfg_attr(test, derive(test_strategy::Arbitrary))]
#[cfg_attr(feature = "proptest", derive(test_strategy::Arbitrary))]
pub struct Ed25519PublicKey(
#[cfg_attr(
feature = "serde",
Expand Down Expand Up @@ -105,7 +105,7 @@ impl std::fmt::Debug for Ed25519PublicKey {
derive(serde_derive::Serialize, serde_derive::Deserialize)
)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[cfg_attr(test, derive(test_strategy::Arbitrary))]
#[cfg_attr(feature = "proptest", derive(test_strategy::Arbitrary))]
pub struct Ed25519Signature(
#[cfg_attr(
feature = "serde",
Expand Down
2 changes: 1 addition & 1 deletion crates/sui-sdk-types/src/types/crypto/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ macro_rules! impl_base64_helper {

#[allow(unused)]
#[derive(Debug, PartialEq)]
#[cfg_attr(test, derive(test_strategy::Arbitrary))]
#[cfg_attr(feature = "proptest", derive(test_strategy::Arbitrary))]
struct $fromstr([u8; $base::LENGTH]);

impl std::str::FromStr for $fromstr {
Expand Down
16 changes: 8 additions & 8 deletions crates/sui-sdk-types/src/types/crypto/multisig.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const MAX_COMMITTEE_SIZE: usize = 10;
// const MAX_BITMAP_VALUE: BitmapUnit = 0b1111111111;

#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(test, derive(test_strategy::Arbitrary))]
#[cfg_attr(feature = "proptest", derive(test_strategy::Arbitrary))]
pub enum MultisigMemberPublicKey {
Ed25519(Ed25519PublicKey),
Secp256k1(Secp256k1PublicKey),
Expand All @@ -31,7 +31,7 @@ pub enum MultisigMemberPublicKey {
derive(serde_derive::Serialize, serde_derive::Deserialize)
)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[cfg_attr(test, derive(test_strategy::Arbitrary))]
#[cfg_attr(feature = "proptest", derive(test_strategy::Arbitrary))]
pub struct MultisigMember {
public_key: MultisigMemberPublicKey,
weight: WeightUnit,
Expand All @@ -57,10 +57,10 @@ impl MultisigMember {
derive(serde_derive::Serialize, serde_derive::Deserialize)
)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[cfg_attr(test, derive(test_strategy::Arbitrary))]
#[cfg_attr(feature = "proptest", derive(test_strategy::Arbitrary))]
pub struct MultisigCommittee {
/// A list of committee members and their corresponding weight.
#[cfg_attr(test, any(proptest::collection::size_range(0..=10).lift()))]
#[cfg_attr(feature = "proptest", any(proptest::collection::size_range(0..=10).lift()))]
members: Vec<MultisigMember>,
/// If the total weight of the public keys corresponding to verified signatures is larger than threshold, the Multisig is verified.
threshold: ThresholdUnit,
Expand Down Expand Up @@ -115,12 +115,12 @@ impl MultisigCommittee {
/// The struct that contains signatures and public keys necessary for authenticating a Multisig.
#[derive(Debug, Clone)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[cfg_attr(test, derive(test_strategy::Arbitrary))]
#[cfg_attr(feature = "proptest", derive(test_strategy::Arbitrary))]
pub struct MultisigAggregatedSignature {
/// The plain signature encoded with signature scheme.
///
/// The signatures must be in the same order as they are listed in the committee.
#[cfg_attr(test, any(proptest::collection::size_range(0..=10).lift()))]
#[cfg_attr(feature = "proptest", any(proptest::collection::size_range(0..=10).lift()))]
signatures: Vec<MultisigMemberSignature>,
/// A bitmap that indicates the position of which public key the signature should be authenticated with.
bitmap: BitmapUnit,
Expand All @@ -133,7 +133,7 @@ pub struct MultisigAggregatedSignature {
with = "Option<crate::_schemars::Base64>",
)
)]
#[cfg_attr(test, strategy(proptest::strategy::Just(None)))]
#[cfg_attr(feature = "proptest", strategy(proptest::strategy::Just(None)))]
legacy_bitmap: Option<roaring::RoaringBitmap>,
/// The public key encoded with each public key with its signature scheme used along with the corresponding weight.
committee: MultisigCommittee,
Expand Down Expand Up @@ -199,7 +199,7 @@ fn roaring_bitmap_to_u16(roaring: &roaring::RoaringBitmap) -> Result<BitmapUnit,
}

#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(test, derive(test_strategy::Arbitrary))]
#[cfg_attr(feature = "proptest", derive(test_strategy::Arbitrary))]
pub enum MultisigMemberSignature {
Ed25519(Ed25519Signature),
Secp256k1(Secp256k1Signature),
Expand Down
2 changes: 1 addition & 1 deletion crates/sui-sdk-types/src/types/crypto/passkey.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ mod serialization {
}
}

#[cfg(test)]
#[cfg(feature = "proptest")]
impl proptest::arbitrary::Arbitrary for PasskeyAuthenticator {
type Parameters = ();
type Strategy = proptest::strategy::BoxedStrategy<Self>;
Expand Down
4 changes: 2 additions & 2 deletions crates/sui-sdk-types/src/types/crypto/secp256k1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
derive(serde_derive::Serialize, serde_derive::Deserialize)
)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[cfg_attr(test, derive(test_strategy::Arbitrary))]
#[cfg_attr(feature = "proptest", derive(test_strategy::Arbitrary))]
pub struct Secp256k1PublicKey(
#[cfg_attr(
feature = "serde",
Expand Down Expand Up @@ -107,7 +107,7 @@ impl std::fmt::Debug for Secp256k1PublicKey {
derive(serde_derive::Serialize, serde_derive::Deserialize)
)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[cfg_attr(test, derive(test_strategy::Arbitrary))]
#[cfg_attr(feature = "proptest", derive(test_strategy::Arbitrary))]
pub struct Secp256k1Signature(
#[cfg_attr(
feature = "serde",
Expand Down
4 changes: 2 additions & 2 deletions crates/sui-sdk-types/src/types/crypto/secp256r1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ impl Secp256r1PrivateKey {
derive(serde_derive::Serialize, serde_derive::Deserialize)
)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[cfg_attr(test, derive(test_strategy::Arbitrary))]
#[cfg_attr(feature = "proptest", derive(test_strategy::Arbitrary))]
pub struct Secp256r1PublicKey(
#[cfg_attr(
feature = "serde",
Expand Down Expand Up @@ -125,7 +125,7 @@ impl std::fmt::Debug for Secp256r1PublicKey {
derive(serde_derive::Serialize, serde_derive::Deserialize)
)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[cfg_attr(test, derive(test_strategy::Arbitrary))]
#[cfg_attr(feature = "proptest", derive(test_strategy::Arbitrary))]
pub struct Secp256r1Signature(
#[cfg_attr(
feature = "serde",
Expand Down
6 changes: 3 additions & 3 deletions crates/sui-sdk-types/src/types/crypto/signature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use super::ZkLoginAuthenticator;
derive(schemars::JsonSchema),
schemars(tag = "scheme", rename_all = "lowercase")
)]
#[cfg_attr(test, derive(test_strategy::Arbitrary))]
#[cfg_attr(feature = "proptest", derive(test_strategy::Arbitrary))]
pub enum SimpleSignature {
Ed25519 {
signature: Ed25519Signature,
Expand Down Expand Up @@ -297,7 +297,7 @@ impl<'de> serde::Deserialize<'de> for SimpleSignature {
}

#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]
#[cfg_attr(test, derive(test_strategy::Arbitrary))]
#[cfg_attr(feature = "proptest", derive(test_strategy::Arbitrary))]
#[repr(u8)]
pub enum SignatureScheme {
Ed25519 = 0x00,
Expand Down Expand Up @@ -380,7 +380,7 @@ impl std::fmt::Display for InvalidSignatureScheme {
}

#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(test, derive(test_strategy::Arbitrary))]
#[cfg_attr(feature = "proptest", derive(test_strategy::Arbitrary))]
pub enum UserSignature {
Simple(SimpleSignature),
Multisig(MultisigAggregatedSignature),
Expand Down
10 changes: 5 additions & 5 deletions crates/sui-sdk-types/src/types/crypto/validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::types::checkpoint::StakeUnit;
derive(serde_derive::Serialize, serde_derive::Deserialize)
)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[cfg_attr(test, derive(test_strategy::Arbitrary))]
#[cfg_attr(feature = "proptest", derive(test_strategy::Arbitrary))]
pub struct ValidatorCommittee {
#[cfg_attr(feature = "serde", serde(with = "crate::_serde::ReadableDisplay"))]
#[cfg_attr(feature = "schemars", schemars(with = "crate::_schemars::U64"))]
Expand All @@ -23,7 +23,7 @@ pub struct ValidatorCommittee {
derive(serde_derive::Serialize, serde_derive::Deserialize)
)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[cfg_attr(test, derive(test_strategy::Arbitrary))]
#[cfg_attr(feature = "proptest", derive(test_strategy::Arbitrary))]
pub struct ValidatorCommitteeMember {
#[cfg_attr(feature = "serde", serde(with = "ValidatorPublicKeySerialization"))]
#[cfg_attr(feature = "schemars", schemars(with = "Bls12381PublicKey"))]
Expand All @@ -39,7 +39,7 @@ pub struct ValidatorCommitteeMember {
derive(serde_derive::Serialize, serde_derive::Deserialize)
)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[cfg_attr(test, derive(test_strategy::Arbitrary))]
#[cfg_attr(feature = "proptest", derive(test_strategy::Arbitrary))]
pub struct ValidatorAggregatedSignature {
#[cfg_attr(feature = "serde", serde(with = "crate::_serde::ReadableDisplay"))]
#[cfg_attr(feature = "schemars", schemars(with = "crate::_schemars::U64"))]
Expand All @@ -48,7 +48,7 @@ pub struct ValidatorAggregatedSignature {
#[cfg_attr(feature = "serde", serde(with = "RoaringBitMapSerialization"))]
#[cfg_attr(feature = "schemars", schemars(with = "crate::_schemars::Base64"))]
#[cfg_attr(
test,
feature = "proptest",
strategy(proptest::strategy::Just(roaring::RoaringBitmap::default()))
)]
pub bitmap: roaring::RoaringBitmap,
Expand Down Expand Up @@ -100,7 +100,7 @@ impl<'de> serde_with::DeserializeAs<'de, Bls12381PublicKey> for BinaryValidatorP
derive(serde_derive::Serialize, serde_derive::Deserialize)
)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[cfg_attr(test, derive(test_strategy::Arbitrary))]
#[cfg_attr(feature = "proptest", derive(test_strategy::Arbitrary))]
pub struct ValidatorSignature {
#[cfg_attr(feature = "serde", serde(with = "crate::_serde::ReadableDisplay"))]
#[cfg_attr(feature = "schemars", schemars(with = "crate::_schemars::U64"))]
Expand Down
Loading

0 comments on commit 6918fd8

Please sign in to comment.