Skip to content

Commit

Permalink
Merge pull request #529 from Chia-Network/new-clvm-derive
Browse files Browse the repository at this point in the history
  • Loading branch information
Rigidity committed May 23, 2024
2 parents 4a78212 + db2d940 commit f991bd4
Show file tree
Hide file tree
Showing 19 changed files with 1,649 additions and 675 deletions.
2 changes: 1 addition & 1 deletion crates/chia-puzzles/src/proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use clvm_traits::{FromClvm, ToClvm};

#[derive(Debug, Clone, Copy, PartialEq, Eq, ToClvm, FromClvm)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
#[clvm(untagged, tuple)]
#[clvm(transparent)]
pub enum Proof {
Lineage(LineageProof),
Eve(EveProof),
Expand Down
7 changes: 4 additions & 3 deletions crates/chia-puzzles/src/puzzles/offer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,23 @@ use hex_literal::hex;

#[derive(Debug, Clone, PartialEq, Eq, ToClvm, FromClvm)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
#[clvm(tuple)]
#[clvm(transparent)]
pub struct SettlementPaymentsSolution {
pub notarized_payments: Vec<NotarizedPayment>,
}

#[derive(Debug, Clone, PartialEq, Eq, ToClvm, FromClvm)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
#[clvm(tuple)]
#[clvm(list)]
pub struct NotarizedPayment {
pub nonce: Bytes32,
#[clvm(rest)]
pub payments: Vec<Payment>,
}

#[derive(Debug, Clone, PartialEq, Eq, ToClvm, FromClvm)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
#[clvm(tuple, untagged)]
#[clvm(transparent)]
pub enum Payment {
WithoutMemos(PaymentWithoutMemos),
WithMemos(PaymentWithMemos),
Expand Down
3 changes: 2 additions & 1 deletion crates/chia-puzzles/src/puzzles/singleton.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,11 @@ impl SingletonArgs<TreeHash> {

#[derive(Debug, Clone, Copy, PartialEq, Eq, ToClvm, FromClvm)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
#[clvm(tuple)]
#[clvm(list)]
pub struct SingletonStruct {
pub mod_hash: Bytes32,
pub launcher_id: Bytes32,
#[clvm(rest)]
pub launcher_puzzle_hash: Bytes32,
}

Expand Down
45 changes: 45 additions & 0 deletions crates/clvm-derive/src/apply_constants.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
use proc_macro2::TokenStream;
use quote::ToTokens;
use syn::{punctuated::Punctuated, Data, DeriveInput, Fields};

use crate::parser::parse_clvm_options;

pub fn impl_apply_constants(mut ast: DeriveInput) -> TokenStream {
match &mut ast.data {
Data::Enum(data_enum) => {
for variant in data_enum.variants.iter_mut() {
remove_fields(&mut variant.fields);
}
}
Data::Struct(data_struct) => {
remove_fields(&mut data_struct.fields);
}
_ => {}
}

ast.into_token_stream()
}

fn remove_fields(fields: &mut Fields) {
match fields {
syn::Fields::Named(fields) => {
let retained_fields = fields
.named
.clone()
.into_iter()
.filter(|field| parse_clvm_options(&field.attrs).constant.is_none());

fields.named = Punctuated::from_iter(retained_fields);
}
syn::Fields::Unnamed(fields) => {
let retained_fields = fields
.unnamed
.clone()
.into_iter()
.filter(|field| parse_clvm_options(&field.attrs).constant.is_none());

fields.unnamed = Punctuated::from_iter(retained_fields);
}
syn::Fields::Unit => {}
}
}
Loading

0 comments on commit f991bd4

Please sign in to comment.