Skip to content

Commit

Permalink
Use Bytes32 instead of [u8; 32]
Browse files Browse the repository at this point in the history
  • Loading branch information
Rigidity committed Nov 15, 2023
1 parent fef6c97 commit 0f70262
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 36 deletions.
17 changes: 12 additions & 5 deletions chia-wallet/fuzz/fuzz_targets/roundtrip.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
#![no_main]
use std::fmt;

use chia_wallet::{nft::NftMetadata, Proof};
use libfuzzer_sys::{arbitrary::Unstructured, fuzz_target};

#[cfg(fuzzing)]
use clvm_traits::{FromPtr, ToPtr};
#[cfg(fuzzing)]
use clvmr::Allocator;
use libfuzzer_sys::{
arbitrary::{Arbitrary, Unstructured},
fuzz_target,
};
#[cfg(fuzzing)]
use libfuzzer_sys::arbitrary::Arbitrary;
#[cfg(fuzzing)]
use std::fmt;

fuzz_target!(|data: &[u8]| {
let mut u = Unstructured::new(data);
roundtrip::<NftMetadata>(&mut u);
roundtrip::<Proof>(&mut u);
});

#[cfg(fuzzing)]
fn roundtrip<'a, T>(u: &mut Unstructured<'a>)
where
T: Arbitrary<'a> + ToPtr + FromPtr + PartialEq + fmt::Debug,
Expand All @@ -25,3 +29,6 @@ where
let obj2 = T::from_ptr(&a, ptr).unwrap();
assert_eq!(obj, obj2);
}

#[cfg(not(fuzzing))]
fn roundtrip<T>(_u: &mut Unstructured) {}
13 changes: 7 additions & 6 deletions chia-wallet/src/proof.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use arbitrary::{Arbitrary, Unstructured};
use chia_protocol::Bytes32;
use clvm_traits::{from_clvm, to_clvm, FromClvm, ToClvm};

#[derive(Debug, Clone, PartialEq, Eq)]
Expand Down Expand Up @@ -35,13 +36,13 @@ impl<'a> Arbitrary<'a> for Proof {
let is_eve = u.ratio(3, 10)?;
if is_eve {
Ok(Self::Eve(EveProof {
parent_coin_info: u.arbitrary()?,
parent_coin_info: u.arbitrary::<[u8; 32]>()?.into(),
amount: u.arbitrary()?,
}))
} else {
Ok(Self::Lineage(LineageProof {
parent_coin_info: u.arbitrary()?,
inner_puzzle_hash: u.arbitrary()?,
parent_coin_info: u.arbitrary::<[u8; 32]>()?.into(),
inner_puzzle_hash: u.arbitrary::<[u8; 32]>()?.into(),
amount: u.arbitrary()?,
}))
}
Expand All @@ -51,14 +52,14 @@ impl<'a> Arbitrary<'a> for Proof {
#[derive(Debug, Clone, PartialEq, Eq, ToClvm, FromClvm)]
#[clvm(list)]
pub struct LineageProof {
pub parent_coin_info: [u8; 32],
pub inner_puzzle_hash: [u8; 32],
pub parent_coin_info: Bytes32,
pub inner_puzzle_hash: Bytes32,
pub amount: u64,
}

#[derive(Debug, Clone, PartialEq, Eq, ToClvm, FromClvm)]
#[clvm(list)]
pub struct EveProof {
pub parent_coin_info: [u8; 32],
pub parent_coin_info: Bytes32,
pub amount: u64,
}
12 changes: 6 additions & 6 deletions chia-wallet/src/puzzles/cat.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use chia_bls::PublicKey;
use chia_protocol::Coin;
use chia_protocol::{Bytes32, Coin};
use clvm_traits::{FromClvm, ToClvm};
use hex_literal::hex;

Expand All @@ -8,8 +8,8 @@ use crate::LineageProof;
#[derive(Debug, Clone, PartialEq, Eq, ToClvm, FromClvm)]
#[clvm(curry)]
pub struct CatArgs<I> {
pub mod_hash: [u8; 32],
pub tail_program_hash: [u8; 32],
pub mod_hash: Bytes32,
pub tail_program_hash: Bytes32,
pub inner_puzzle: I,
}

Expand All @@ -24,7 +24,7 @@ pub struct EverythingWithSignatureTailArgs {
pub struct CatSolution<I> {
pub inner_puzzle_solution: I,
pub lineage_proof: Option<LineageProof>,
pub prev_coin_id: [u8; 32],
pub prev_coin_id: Bytes32,
pub this_coin_info: Coin,
pub next_coin_proof: CoinProof,
pub prev_subtotal: i64,
Expand All @@ -34,8 +34,8 @@ pub struct CatSolution<I> {
#[derive(Debug, Clone, PartialEq, Eq, ToClvm, FromClvm)]
#[clvm(list)]
pub struct CoinProof {
pub parent_coin_info: [u8; 32],
pub inner_puzzle_hash: [u8; 32],
pub parent_coin_info: Bytes32,
pub inner_puzzle_hash: Bytes32,
pub amount: u64,
}

Expand Down
3 changes: 2 additions & 1 deletion chia-wallet/src/puzzles/did.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use chia_protocol::Bytes32;
use clvm_traits::{clvm_tuple, from_clvm, match_tuple, to_clvm, FromClvm, FromClvmError, ToClvm};
use hex_literal::hex;

Expand All @@ -7,7 +8,7 @@ use crate::singleton::SingletonStruct;
#[clvm(curry)]
pub struct DidArgs<I, M> {
pub inner_puzzle: I,
pub recovery_did_list_hash: [u8; 32],
pub recovery_did_list_hash: Bytes32,
pub num_verifications_required: u64,
pub singleton_struct: SingletonStruct,
pub metadata: M,
Expand Down
23 changes: 12 additions & 11 deletions chia-wallet/src/puzzles/nft.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use arbitrary::Arbitrary;
use chia_protocol::Bytes32;
use clvm_traits::{from_clvm, to_clvm, FromClvm, Raw, ToClvm};
use hex_literal::hex;

Expand All @@ -7,17 +7,17 @@ use crate::singleton::SingletonStruct;
#[derive(Debug, Clone, PartialEq, Eq, ToClvm, FromClvm)]
#[clvm(curry)]
pub struct NftIntermediateLauncherArgs {
pub launcher_puzzle_hash: [u8; 32],
pub launcher_puzzle_hash: Bytes32,
pub mint_number: usize,
pub mint_total: usize,
}

#[derive(Debug, Clone, PartialEq, Eq, ToClvm, FromClvm)]
#[clvm(curry)]
pub struct NftStateLayerArgs<I, M> {
pub mod_hash: [u8; 32],
pub mod_hash: Bytes32,
pub metadata: M,
pub metadata_updater_puzzle_hash: [u8; 32],
pub metadata_updater_puzzle_hash: Bytes32,
pub inner_puzzle: I,
}

Expand All @@ -30,8 +30,8 @@ pub struct NftStateLayerSolution<I> {
#[derive(Debug, Clone, PartialEq, Eq, ToClvm, FromClvm)]
#[clvm(curry)]
pub struct NftOwnershipLayerArgs<I, P> {
pub mod_hash: [u8; 32],
pub current_owner: Option<[u8; 32]>,
pub mod_hash: Bytes32,
pub current_owner: Option<Bytes32>,
pub transfer_program: P,
pub inner_puzzle: I,
}
Expand All @@ -46,20 +46,21 @@ pub struct NftOwnershipLayerSolution<I> {
#[clvm(curry)]
pub struct NftRoyaltyTransferPuzzleArgs {
pub singleton_struct: SingletonStruct,
pub royalty_puzzle_hash: [u8; 32],
pub royalty_puzzle_hash: Bytes32,
pub trade_price_percentage: u16,
}

#[derive(Debug, Clone, PartialEq, Eq, Arbitrary)]
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(fuzzing, derive(arbitrary::Arbitrary))]
pub struct NftMetadata {
pub edition_number: u64,
pub edition_total: u64,
pub data_uris: Vec<String>,
pub data_hash: Option<[u8; 32]>,
pub data_hash: Option<Bytes32>,
pub metadata_uris: Vec<String>,
pub metadata_hash: Option<[u8; 32]>,
pub metadata_hash: Option<Bytes32>,
pub license_uris: Vec<String>,
pub license_hash: Option<[u8; 32]>,
pub license_hash: Option<Bytes32>,
}

impl Default for NftMetadata {
Expand Down
15 changes: 8 additions & 7 deletions chia-wallet/src/puzzles/singleton.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use chia_protocol::Bytes32;
use clvm_traits::{FromClvm, ToClvm};
use hex_literal::hex;

Expand All @@ -13,9 +14,9 @@ pub struct SingletonArgs<I> {
#[derive(Debug, Clone, PartialEq, Eq, ToClvm, FromClvm)]
#[clvm(tuple)]
pub struct SingletonStruct {
pub mod_hash: [u8; 32],
pub launcher_id: [u8; 32],
pub launcher_puzzle_hash: [u8; 32],
pub mod_hash: Bytes32,
pub launcher_id: Bytes32,
pub launcher_puzzle_hash: Bytes32,
}

#[derive(Debug, Clone, PartialEq, Eq, ToClvm, FromClvm)]
Expand All @@ -29,17 +30,17 @@ pub struct SingletonSolution<I> {
#[derive(Debug, Clone, PartialEq, Eq, ToClvm, FromClvm)]
#[clvm(list)]
pub struct LauncherSolution<T> {
pub singleton_puzzle_hash: [u8; 32],
pub singleton_puzzle_hash: Bytes32,
pub amount: u64,
pub key_value_list: T,
}

impl SingletonStruct {
pub fn new(launcher_id: [u8; 32]) -> Self {
pub fn new(launcher_id: Bytes32) -> Self {
Self {
mod_hash: SINGLETON_TOP_LAYER_PUZZLE_HASH,
mod_hash: SINGLETON_TOP_LAYER_PUZZLE_HASH.into(),
launcher_id,
launcher_puzzle_hash: SINGLETON_LAUNCHER_PUZZLE_HASH,
launcher_puzzle_hash: SINGLETON_LAUNCHER_PUZZLE_HASH.into(),
}
}
}
Expand Down

0 comments on commit 0f70262

Please sign in to comment.