From 4102bfe553ad00d955a86bfc3e66234521c763b7 Mon Sep 17 00:00:00 2001 From: Rigidity Date: Thu, 16 Nov 2023 13:20:30 -0500 Subject: [PATCH 1/5] Rename clvm-derive representations --- chia-tools/src/bin/run-spend.rs | 10 ++--- chia-wallet/src/proof.rs | 4 +- chia-wallet/src/puzzles/cat.rs | 8 ++-- chia-wallet/src/puzzles/nft.rs | 12 ++--- chia-wallet/src/puzzles/singleton.rs | 6 +-- chia-wallet/src/puzzles/standard.rs | 4 +- clvm-derive/src/from_clvm.rs | 4 +- clvm-derive/src/helpers.rs | 65 +++++++++++++--------------- clvm-derive/src/to_clvm.rs | 4 +- clvm-traits/src/lib.rs | 8 ++-- src/fast_forward.rs | 6 +-- 11 files changed, 63 insertions(+), 68 deletions(-) diff --git a/chia-tools/src/bin/run-spend.rs b/chia-tools/src/bin/run-spend.rs index a3f73991c..a386380c0 100644 --- a/chia-tools/src/bin/run-spend.rs +++ b/chia-tools/src/bin/run-spend.rs @@ -130,14 +130,14 @@ pub struct SingletonStruct { } #[derive(FromClvm, ToClvm, Debug)] -#[clvm(curried_args)] +#[clvm(curry)] pub struct SingletonArgs { pub singleton_struct: SingletonStruct, pub inner_puzzle: NodePtr, } #[derive(FromClvm, ToClvm, Debug)] -#[clvm(proper_list)] +#[clvm(list)] pub struct LineageProof { pub parent_parent_coin_id: Bytes32, pub parent_inner_puzzle_hash: Bytes32, @@ -145,14 +145,14 @@ pub struct LineageProof { } #[derive(FromClvm, ToClvm, Debug)] -#[clvm(proper_list)] +#[clvm(list)] pub struct EveProof { pub parent_parent_coin_id: Bytes32, pub parent_amount: u64, } #[derive(FromClvm, ToClvm, Debug)] -#[clvm(proper_list)] +#[clvm(list)] pub struct SingletonSolution { pub lineage_proof: LineageProof, pub amount: u64, @@ -160,7 +160,7 @@ pub struct SingletonSolution { } #[derive(FromClvm, ToClvm, Debug)] -#[clvm(proper_list)] +#[clvm(list)] pub struct EveSingletonSolution { pub lineage_proof: EveProof, pub amount: u64, diff --git a/chia-wallet/src/proof.rs b/chia-wallet/src/proof.rs index 69c421db7..aadc97cf8 100644 --- a/chia-wallet/src/proof.rs +++ b/chia-wallet/src/proof.rs @@ -44,7 +44,7 @@ impl<'a> Arbitrary<'a> for Proof { } #[derive(Debug, Clone, PartialEq, Eq, ToClvm, FromClvm)] -#[clvm(proper_list)] +#[clvm(list)] pub struct LineageProof { pub parent_coin_info: [u8; 32], pub inner_puzzle_hash: [u8; 32], @@ -52,7 +52,7 @@ pub struct LineageProof { } #[derive(Debug, Clone, PartialEq, Eq, ToClvm, FromClvm)] -#[clvm(proper_list)] +#[clvm(list)] pub struct EveProof { pub parent_coin_info: [u8; 32], pub amount: u64, diff --git a/chia-wallet/src/puzzles/cat.rs b/chia-wallet/src/puzzles/cat.rs index 7f2a97d99..9be53ce9d 100644 --- a/chia-wallet/src/puzzles/cat.rs +++ b/chia-wallet/src/puzzles/cat.rs @@ -7,7 +7,7 @@ use hex_literal::hex; use crate::LineageProof; #[derive(Debug, Clone, PartialEq, Eq, ToClvm, FromClvm)] -#[clvm(curried_args)] +#[clvm(curry)] pub struct CatArgs { pub mod_hash: [u8; 32], pub tail_program_hash: [u8; 32], @@ -15,13 +15,13 @@ pub struct CatArgs { } #[derive(Debug, Clone, PartialEq, Eq, ToClvm, FromClvm)] -#[clvm(curried_args)] +#[clvm(curry)] pub struct EverythingWithSignatureTailArgs { pub public_key: PublicKey, } #[derive(Debug, Clone, PartialEq, Eq, ToClvm, FromClvm)] -#[clvm(proper_list)] +#[clvm(list)] pub struct CatSolution { pub inner_puzzle_solution: NodePtr, pub lineage_proof: Option, @@ -33,7 +33,7 @@ pub struct CatSolution { } #[derive(Debug, Clone, PartialEq, Eq, ToClvm, FromClvm)] -#[clvm(proper_list)] +#[clvm(list)] pub struct CoinProof { pub parent_coin_info: [u8; 32], pub inner_puzzle_hash: [u8; 32], diff --git a/chia-wallet/src/puzzles/nft.rs b/chia-wallet/src/puzzles/nft.rs index 5b8a1a9d6..4a9bf68b5 100644 --- a/chia-wallet/src/puzzles/nft.rs +++ b/chia-wallet/src/puzzles/nft.rs @@ -6,7 +6,7 @@ use hex_literal::hex; use crate::singleton::SingletonStruct; #[derive(Debug, Clone, PartialEq, Eq, ToClvm, FromClvm)] -#[clvm(curried_args)] +#[clvm(curry)] pub struct NftIntermediateLauncherArgs { pub launcher_puzzle_hash: [u8; 32], pub mint_number: usize, @@ -14,7 +14,7 @@ pub struct NftIntermediateLauncherArgs { } #[derive(Debug, Clone, PartialEq, Eq, ToClvm, FromClvm)] -#[clvm(curried_args)] +#[clvm(curry)] pub struct NftStateLayerArgs { pub mod_hash: [u8; 32], pub metadata: NodePtr, @@ -23,13 +23,13 @@ pub struct NftStateLayerArgs { } #[derive(Debug, Clone, PartialEq, Eq, ToClvm, FromClvm)] -#[clvm(proper_list)] +#[clvm(list)] pub struct NftStateLayerSolution { pub inner_solution: NodePtr, } #[derive(Debug, Clone, PartialEq, Eq, ToClvm, FromClvm)] -#[clvm(curried_args)] +#[clvm(curry)] pub struct NftOwnershipLayerArgs { pub mod_hash: [u8; 32], pub current_owner: Option<[u8; 32]>, @@ -38,13 +38,13 @@ pub struct NftOwnershipLayerArgs { } #[derive(Debug, Clone, PartialEq, Eq, ToClvm, FromClvm)] -#[clvm(proper_list)] +#[clvm(list)] pub struct NftOwnershipLayerSolution { pub inner_solution: NodePtr, } #[derive(Debug, Clone, PartialEq, Eq, ToClvm, FromClvm)] -#[clvm(curried_args)] +#[clvm(curry)] pub struct NftRoyaltyTransferPuzzleArgs { pub singleton_struct: SingletonStruct, pub royalty_puzzle_hash: [u8; 32], diff --git a/chia-wallet/src/puzzles/singleton.rs b/chia-wallet/src/puzzles/singleton.rs index a56606e4a..58bdb046d 100644 --- a/chia-wallet/src/puzzles/singleton.rs +++ b/chia-wallet/src/puzzles/singleton.rs @@ -5,7 +5,7 @@ use hex_literal::hex; use crate::Proof; #[derive(Debug, Clone, PartialEq, Eq, ToClvm, FromClvm)] -#[clvm(curried_args)] +#[clvm(curry)] pub struct SingletonArgs { pub singleton_struct: SingletonStruct, pub inner_puzzle: NodePtr, @@ -20,7 +20,7 @@ pub struct SingletonStruct { } #[derive(Debug, Clone, PartialEq, Eq, ToClvm, FromClvm)] -#[clvm(proper_list)] +#[clvm(list)] pub struct SingletonSolution { pub proof: Proof, pub amount: u64, @@ -28,7 +28,7 @@ pub struct SingletonSolution { } #[derive(Debug, Clone, PartialEq, Eq, ToClvm, FromClvm)] -#[clvm(proper_list)] +#[clvm(list)] pub struct LauncherSolution { pub singleton_puzzle_hash: [u8; 32], pub amount: u64, diff --git a/chia-wallet/src/puzzles/standard.rs b/chia-wallet/src/puzzles/standard.rs index e9e0e61c2..90e6c2a54 100644 --- a/chia-wallet/src/puzzles/standard.rs +++ b/chia-wallet/src/puzzles/standard.rs @@ -5,13 +5,13 @@ use clvmr::allocator::NodePtr; use hex_literal::hex; #[derive(Debug, Clone, PartialEq, Eq, ToClvm, FromClvm)] -#[clvm(curried_args)] +#[clvm(curry)] pub struct StandardArgs { pub synthetic_key: PublicKey, } #[derive(Debug, Clone, PartialEq, Eq, ToClvm, FromClvm)] -#[clvm(proper_list)] +#[clvm(list)] pub struct StandardSolution { pub original_public_key: Option, pub delegated_puzzle: NodePtr, diff --git a/clvm-derive/src/from_clvm.rs b/clvm-derive/src/from_clvm.rs index b561f887a..4d295b398 100644 --- a/clvm-derive/src/from_clvm.rs +++ b/clvm-derive/src/from_clvm.rs @@ -43,7 +43,7 @@ pub fn from_clvm(mut ast: DeriveInput) -> TokenStream { // `match_macro` decodes a nested tuple containing each of the struct field types within. // `destructure_macro` destructures the values into the field names, to be stored in the struct. let (match_macro, destructure_macro) = match args.repr { - Repr::ProperList => ( + Repr::List => ( quote!( #crate_name::match_list ), quote!( #crate_name::destructure_list ), ), @@ -51,7 +51,7 @@ pub fn from_clvm(mut ast: DeriveInput) -> TokenStream { quote!( #crate_name::match_tuple ), quote!( #crate_name::destructure_tuple ), ), - Repr::CurriedArgs => ( + Repr::Curry => ( quote!( #crate_name::match_curried_args ), quote!( #crate_name::destructure_curried_args ), ), diff --git a/clvm-derive/src/helpers.rs b/clvm-derive/src/helpers.rs index 7f0bfce59..2379db250 100644 --- a/clvm-derive/src/helpers.rs +++ b/clvm-derive/src/helpers.rs @@ -1,20 +1,22 @@ +use std::fmt; + use proc_macro2::Ident; use syn::{punctuated::Punctuated, Attribute, GenericParam, Generics, Token, TypeParamBound}; #[derive(Debug, Copy, Clone, PartialEq, Eq)] pub enum Repr { Tuple, - ProperList, - CurriedArgs, + List, + Curry, } -impl ToString for Repr { - fn to_string(&self) -> String { - match self { - Self::Tuple => "tuple".to_string(), - Self::ProperList => "proper_list".to_string(), - Self::CurriedArgs => "curried_args".to_string(), - } +impl fmt::Display for Repr { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.write_str(match self { + Self::Tuple => "tuple", + Self::List => "list", + Self::Curry => "curry", + }) } } @@ -23,8 +25,15 @@ pub struct ClvmDeriveArgs { } pub fn parse_args(attrs: &[Attribute]) -> ClvmDeriveArgs { - let mut repr: Option = None; + let repr = parse_repr(attrs); + ClvmDeriveArgs { + repr: repr + .expect("expected clvm attribute parameter of either `tuple`, `list`, or `curry`"), + } +} +pub fn parse_repr(attrs: &[Attribute]) -> Option { + let mut repr: Option = None; for attr in attrs { if let Some(ident) = attr.path().get_ident() { if ident == "clvm" { @@ -33,37 +42,23 @@ pub fn parse_args(attrs: &[Attribute]) -> ClvmDeriveArgs { .unwrap(); for arg in args { + let existing = repr; + match arg.to_string().as_str() { - "tuple" => { - if let Some(existing) = repr { - panic!("`tuple` conflicts with `{}`", existing.to_string()); - } - repr = Some(Repr::Tuple); - } - "proper_list" => { - if let Some(existing) = repr { - panic!("`proper_list` conflicts with `{}`", existing.to_string()); - } - repr = Some(Repr::ProperList); - } - "curried_args" => { - if let Some(existing) = repr { - panic!("`curried_args` conflicts with `{}`", existing.to_string()); - } - repr = Some(Repr::CurriedArgs); - } - ident => panic!("unknown argument `{}`", ident), + "tuple" => repr = Some(Repr::Tuple), + "list" => repr = Some(Repr::List), + "curry" => repr = Some(Repr::Curry), + ident => panic!("unknown argument `{ident}`"), + } + + if let Some(existing) = existing { + panic!("`{arg}` conflicts with `{existing}`"); } } } } } - - ClvmDeriveArgs { - repr: repr.expect( - "expected clvm attribute parameter of either `tuple`, `proper_list`, or `curried_args`", - ), - } + repr } pub fn add_trait_bounds(generics: &mut Generics, bound: TypeParamBound) { diff --git a/clvm-derive/src/to_clvm.rs b/clvm-derive/src/to_clvm.rs index ab7a25f0d..32c228ae0 100644 --- a/clvm-derive/src/to_clvm.rs +++ b/clvm-derive/src/to_clvm.rs @@ -42,9 +42,9 @@ pub fn to_clvm(mut ast: DeriveInput) -> TokenStream { // `list_macro` encodes a nested tuple containing each of the struct field values within. let list_macro = match args.repr { - Repr::ProperList => quote!( #crate_name::clvm_list ), + Repr::List => quote!( #crate_name::clvm_list ), Repr::Tuple => quote!( #crate_name::clvm_tuple ), - Repr::CurriedArgs => quote!( #crate_name::clvm_curried_args ), + Repr::Curry => quote!( #crate_name::clvm_curried_args ), }; add_trait_bounds(&mut ast.generics, parse_quote!(#crate_name::ToClvm)); diff --git a/clvm-traits/src/lib.rs b/clvm-traits/src/lib.rs index 35d3c51cf..22cc5ace0 100644 --- a/clvm-traits/src/lib.rs +++ b/clvm-traits/src/lib.rs @@ -8,8 +8,8 @@ //! with one of the following encodings: //! //! * `#[clvm(tuple)]` for unterminated lists such as `(A . (B . C))`. -//! * `#[clvm(proper_list)]` for proper lists such as `(A B C)`, or in other words `(A . (B . (C . ())))`. -//! * `#[clvm(curried_args)]` for curried arguments such as `(c (q . A) (c (q . B) (c (q . C) 1)))`. +//! * `#[clvm(list)]` for proper lists such as `(A B C)`, or in other words `(A . (B . (C . ())))`. +//! * `#[clvm(curry)]` for curried arguments such as `(c (q . A) (c (q . B) (c (q . C) 1)))`. #![cfg_attr( feature = "derive", @@ -92,7 +92,7 @@ mod tests { #[test] fn test_proper_list() { #[derive(Debug, ToClvm, FromClvm, PartialEq, Eq)] - #[clvm(proper_list)] + #[clvm(list)] struct ProperListStruct { a: u64, b: i32, @@ -104,7 +104,7 @@ mod tests { #[test] fn test_curried_args() { #[derive(Debug, ToClvm, FromClvm, PartialEq, Eq)] - #[clvm(curried_args)] + #[clvm(curry)] struct CurriedArgsStruct { a: u64, b: i32, diff --git a/src/fast_forward.rs b/src/fast_forward.rs index 733912c45..49d20e7ed 100644 --- a/src/fast_forward.rs +++ b/src/fast_forward.rs @@ -16,14 +16,14 @@ pub struct SingletonStruct { } #[derive(FromClvm, ToClvm, Debug)] -#[clvm(curried_args)] +#[clvm(curry)] pub struct SingletonArgs { pub singleton_struct: SingletonStruct, pub inner_puzzle: NodePtr, } #[derive(FromClvm, ToClvm, Debug)] -#[clvm(proper_list)] +#[clvm(list)] pub struct LineageProof { pub parent_parent_coin_id: Bytes32, pub parent_inner_puzzle_hash: Bytes32, @@ -31,7 +31,7 @@ pub struct LineageProof { } #[derive(FromClvm, ToClvm, Debug)] -#[clvm(proper_list)] +#[clvm(list)] pub struct SingletonSolution { pub lineage_proof: LineageProof, pub amount: u64, From 19c86f4f71817a87acc4874de4bd1d19c3083a81 Mon Sep 17 00:00:00 2001 From: Rigidity Date: Thu, 16 Nov 2023 13:20:45 -0500 Subject: [PATCH 2/5] Missed one --- chia-wallet/src/puzzles/did.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/chia-wallet/src/puzzles/did.rs b/chia-wallet/src/puzzles/did.rs index e9c61f099..dc260f64f 100644 --- a/chia-wallet/src/puzzles/did.rs +++ b/chia-wallet/src/puzzles/did.rs @@ -5,7 +5,7 @@ use hex_literal::hex; use crate::singleton::SingletonStruct; #[derive(Debug, Clone, PartialEq, Eq, ToClvm, FromClvm)] -#[clvm(curried_args)] +#[clvm(curry)] pub struct DidArgs { pub inner_puzzle: NodePtr, pub recovery_did_list_hash: [u8; 32], From ae8549d23863e0bc67f23ec734f540e6b12cd7da Mon Sep 17 00:00:00 2001 From: Rigidity Date: Thu, 16 Nov 2023 13:31:50 -0500 Subject: [PATCH 3/5] Rename tests --- clvm-traits/src/lib.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/clvm-traits/src/lib.rs b/clvm-traits/src/lib.rs index 22cc5ace0..fce34e45c 100644 --- a/clvm-traits/src/lib.rs +++ b/clvm-traits/src/lib.rs @@ -93,25 +93,25 @@ mod tests { fn test_proper_list() { #[derive(Debug, ToClvm, FromClvm, PartialEq, Eq)] #[clvm(list)] - struct ProperListStruct { + struct ListStruct { a: u64, b: i32, } - check(ProperListStruct { a: 52, b: -32 }, "ff34ff81e080"); + check(ListStruct { a: 52, b: -32 }, "ff34ff81e080"); } #[test] fn test_curried_args() { #[derive(Debug, ToClvm, FromClvm, PartialEq, Eq)] #[clvm(curry)] - struct CurriedArgsStruct { + struct CurryStruct { a: u64, b: i32, } check( - CurriedArgsStruct { a: 52, b: -32 }, + CurryStruct { a: 52, b: -32 }, "ff04ffff0134ffff04ffff0181e0ff018080", ); } From e251ab7a87dfc44dc0705bd047c7d4625b462ee4 Mon Sep 17 00:00:00 2001 From: Rigidity Date: Thu, 16 Nov 2023 13:32:21 -0500 Subject: [PATCH 4/5] Rename test fns --- clvm-traits/src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/clvm-traits/src/lib.rs b/clvm-traits/src/lib.rs index fce34e45c..bb860438a 100644 --- a/clvm-traits/src/lib.rs +++ b/clvm-traits/src/lib.rs @@ -90,7 +90,7 @@ mod tests { } #[test] - fn test_proper_list() { + fn test_list() { #[derive(Debug, ToClvm, FromClvm, PartialEq, Eq)] #[clvm(list)] struct ListStruct { @@ -102,7 +102,7 @@ mod tests { } #[test] - fn test_curried_args() { + fn test_curry() { #[derive(Debug, ToClvm, FromClvm, PartialEq, Eq)] #[clvm(curry)] struct CurryStruct { From 41716103b8031fbc648a02f7994bdb43ae745d90 Mon Sep 17 00:00:00 2001 From: Rigidity Date: Thu, 16 Nov 2023 18:00:49 -0500 Subject: [PATCH 5/5] Factor out assignment --- clvm-derive/src/helpers.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/clvm-derive/src/helpers.rs b/clvm-derive/src/helpers.rs index 2379db250..4f126be02 100644 --- a/clvm-derive/src/helpers.rs +++ b/clvm-derive/src/helpers.rs @@ -44,12 +44,12 @@ pub fn parse_repr(attrs: &[Attribute]) -> Option { for arg in args { let existing = repr; - match arg.to_string().as_str() { - "tuple" => repr = Some(Repr::Tuple), - "list" => repr = Some(Repr::List), - "curry" => repr = Some(Repr::Curry), + repr = Some(match arg.to_string().as_str() { + "tuple" => Repr::Tuple, + "list" => Repr::List, + "curry" => Repr::Curry, ident => panic!("unknown argument `{ident}`"), - } + }); if let Some(existing) = existing { panic!("`{arg}` conflicts with `{existing}`");