Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename clvm-derive representations #316

Merged
merged 5 commits into from
Nov 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions chia-tools/src/bin/run-spend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,37 +130,37 @@ 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,
pub parent_amount: u64,
}

#[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,
pub inner_solution: NodePtr,
}

#[derive(FromClvm, ToClvm, Debug)]
#[clvm(proper_list)]
#[clvm(list)]
pub struct EveSingletonSolution {
pub lineage_proof: EveProof,
pub amount: u64,
Expand Down
4 changes: 2 additions & 2 deletions chia-wallet/src/proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,15 @@ 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],
pub amount: u64,
}

#[derive(Debug, Clone, PartialEq, Eq, ToClvm, FromClvm)]
#[clvm(proper_list)]
#[clvm(list)]
pub struct EveProof {
pub parent_coin_info: [u8; 32],
pub amount: u64,
Expand Down
8 changes: 4 additions & 4 deletions chia-wallet/src/puzzles/cat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,21 @@ 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],
pub inner_puzzle: NodePtr,
}

#[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<LineageProof>,
Expand All @@ -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],
Expand Down
2 changes: 1 addition & 1 deletion chia-wallet/src/puzzles/did.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down
12 changes: 6 additions & 6 deletions chia-wallet/src/puzzles/nft.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ 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,
pub mint_total: usize,
}

#[derive(Debug, Clone, PartialEq, Eq, ToClvm, FromClvm)]
#[clvm(curried_args)]
#[clvm(curry)]
pub struct NftStateLayerArgs {
pub mod_hash: [u8; 32],
pub metadata: NodePtr,
Expand All @@ -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]>,
Expand All @@ -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],
Expand Down
6 changes: 3 additions & 3 deletions chia-wallet/src/puzzles/singleton.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -20,15 +20,15 @@ pub struct SingletonStruct {
}

#[derive(Debug, Clone, PartialEq, Eq, ToClvm, FromClvm)]
#[clvm(proper_list)]
#[clvm(list)]
pub struct SingletonSolution {
pub proof: Proof,
pub amount: u64,
pub inner_solution: NodePtr,
}

#[derive(Debug, Clone, PartialEq, Eq, ToClvm, FromClvm)]
#[clvm(proper_list)]
#[clvm(list)]
pub struct LauncherSolution {
pub singleton_puzzle_hash: [u8; 32],
pub amount: u64,
Expand Down
4 changes: 2 additions & 2 deletions chia-wallet/src/puzzles/standard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<PublicKey>,
pub delegated_puzzle: NodePtr,
Expand Down
4 changes: 2 additions & 2 deletions clvm-derive/src/from_clvm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,15 @@ 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 ),
),
Repr::Tuple => (
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 ),
),
Expand Down
67 changes: 31 additions & 36 deletions clvm-derive/src/helpers.rs
Original file line number Diff line number Diff line change
@@ -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",
})
}
}

Expand All @@ -23,8 +25,15 @@ pub struct ClvmDeriveArgs {
}

pub fn parse_args(attrs: &[Attribute]) -> ClvmDeriveArgs {
let mut repr: Option<Repr> = 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<Repr> {
let mut repr: Option<Repr> = None;
for attr in attrs {
if let Some(ident) = attr.path().get_ident() {
if ident == "clvm" {
Expand All @@ -33,37 +42,23 @@ pub fn parse_args(attrs: &[Attribute]) -> ClvmDeriveArgs {
.unwrap();

for arg in args {
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),
let existing = repr;

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}`");
}
}
}
}
}

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) {
Expand Down
4 changes: 2 additions & 2 deletions clvm-derive/src/to_clvm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
20 changes: 10 additions & 10 deletions clvm-traits/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -90,28 +90,28 @@ mod tests {
}

#[test]
fn test_proper_list() {
fn test_list() {
#[derive(Debug, ToClvm, FromClvm, PartialEq, Eq)]
#[clvm(proper_list)]
struct ProperListStruct {
#[clvm(list)]
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() {
fn test_curry() {
#[derive(Debug, ToClvm, FromClvm, PartialEq, Eq)]
#[clvm(curried_args)]
struct CurriedArgsStruct {
#[clvm(curry)]
struct CurryStruct {
a: u64,
b: i32,
}

check(
CurriedArgsStruct { a: 52, b: -32 },
CurryStruct { a: 52, b: -32 },
"ff04ffff0134ffff04ffff0181e0ff018080",
);
}
Expand Down
6 changes: 3 additions & 3 deletions src/fast_forward.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,22 @@ 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,
pub parent_amount: u64,
}

#[derive(FromClvm, ToClvm, Debug)]
#[clvm(proper_list)]
#[clvm(list)]
pub struct SingletonSolution {
pub lineage_proof: LineageProof,
pub amount: u64,
Expand Down
Loading