From 02a463fbd20369f24fe8f0f3bb53cd896d2779cd Mon Sep 17 00:00:00 2001 From: muraca Date: Tue, 7 Nov 2023 12:15:50 +0100 Subject: [PATCH] beautify `TuxedoGenesisConfig` Signed-off-by: muraca --- tuxedo-template-runtime/src/lib.rs | 56 +++++++----------------------- wardrobe/kitties/src/lib.rs | 39 ++++++++++++++++++++- wardrobe/money/src/lib.rs | 18 +++++++++- 3 files changed, 67 insertions(+), 46 deletions(-) diff --git a/tuxedo-template-runtime/src/lib.rs b/tuxedo-template-runtime/src/lib.rs index 044e4700b..a7c92e8a1 100644 --- a/tuxedo-template-runtime/src/lib.rs +++ b/tuxedo-template-runtime/src/lib.rs @@ -9,7 +9,6 @@ #[cfg(feature = "std")] include!(concat!(env!("OUT_DIR"), "/wasm_binary.rs")); -use kitties::FreeKittyConstraintChecker; use parity_scale_codec::{Decode, Encode}; use scale_info::TypeInfo; use sp_consensus_aura::sr25519::AuthorityId as AuraId; @@ -110,9 +109,8 @@ pub struct TuxedoGenesisConfig(pub Vec); impl Default for TuxedoGenesisConfig { fn default() -> Self { use hex_literal::hex; - use kitties::{KittyDNA, KittyData, Parent}; - use money::{Coin, MoneyConstraintChecker}; - use sp_api::HashT; + use kitties::{KittyData, Parent}; + use money::Coin; const SHAWN_PUB_KEY_BYTES: [u8; 32] = hex!("d2bf4b844dfefd6772a8843e669f943408966a977e3ae2af1dd78e0f55f4df67"); @@ -121,44 +119,14 @@ impl Default for TuxedoGenesisConfig { let signatories = vec![SHAWN_PUB_KEY_BYTES.into(), ANDREW_PUB_KEY_BYTES.into()]; let genesis_transactions = vec![ - // Money Transaction - Transaction { - inputs: vec![], - peeks: vec![], - outputs: vec![ - (Coin::<0>(100), SigCheck::new(SHAWN_PUB_KEY_BYTES)).into(), - (Coin::<0>(100), ThresholdMultiSignature::new(1, signatories)).into(), - ], - checker: MoneyConstraintChecker::Mint.into(), - }, - // Kitty Transaction - Transaction { - inputs: vec![], - peeks: vec![], - outputs: vec![ - ( - KittyData { - parent: Parent::Mom(Default::default()), - dna: KittyDNA(BlakeTwo256::hash_of(b"mother")), - ..Default::default() - }, - UpForGrabs, - ) - .into(), - ( - KittyData { - parent: Parent::Dad(Default::default()), - dna: KittyDNA(BlakeTwo256::hash_of(b"father")), - ..Default::default() - }, - UpForGrabs, - ) - .into(), - ], - checker: FreeKittyConstraintChecker.into(), - }, - // TODO: Initial Transactions for Existence + // Money Transactions + Coin::<0>::mint(100, SigCheck::new(SHAWN_PUB_KEY_BYTES)), + Coin::<0>::mint(100, ThresholdMultiSignature::new(1, signatories)), + // Kitty Transactions + KittyData::mint(Parent::mom(), b"mother", UpForGrabs), + KittyData::mint(Parent::dad(), b"father", UpForGrabs), ]; + // TODO: Initial Transactions for Existence TuxedoGenesisConfig(genesis_transactions) } @@ -523,14 +491,14 @@ mod tests { }, }; - let tx = TuxedoGenesisConfig::default().0.get(0).unwrap().clone(); + let tx = TuxedoGenesisConfig::default().0.get(1).unwrap().clone(); - assert_eq!(tx.outputs.get(1), Some(&genesis_multi_sig_utxo)); + assert_eq!(tx.outputs.get(0), Some(&genesis_multi_sig_utxo)); let tx_hash = BlakeTwo256::hash_of(&tx.encode()); let output_ref = OutputRef { tx_hash, - index: 1_u32, + index: 0_u32, }; let encoded_utxo = diff --git a/wardrobe/kitties/src/lib.rs b/wardrobe/kitties/src/lib.rs index 9091c8f4c..49d7d4162 100644 --- a/wardrobe/kitties/src/lib.rs +++ b/wardrobe/kitties/src/lib.rs @@ -28,7 +28,9 @@ use sp_runtime::{ use sp_std::prelude::*; use tuxedo_core::{ dynamic_typing::{DynamicallyTypedData, UtxoData}, - ensure, SimpleConstraintChecker, + ensure, + types::Transaction, + SimpleConstraintChecker, Verifier, }; #[cfg(test)] @@ -111,6 +113,16 @@ pub enum Parent { Dad(DadKittyStatus), } +impl Parent { + pub fn dad() -> Self { + Parent::Dad(DadKittyStatus::RearinToGo) + } + + pub fn mom() -> Self { + Parent::Mom(MomKittyStatus::RearinToGo) + } +} + impl Default for Parent { fn default() -> Self { Parent::Mom(MomKittyStatus::RearinToGo) @@ -155,6 +167,31 @@ pub struct KittyData { pub num_breedings: u128, } +impl KittyData { + /// Create a mint transaction for a single Kitty. + pub fn mint(parent: Parent, dna_preimage: &[u8], v: V) -> Transaction + where + V: Verifier, + OV: Verifier + From, + OC: tuxedo_core::ConstraintChecker + From, + { + Transaction { + inputs: vec![], + peeks: vec![], + outputs: vec![( + KittyData { + parent, + dna: KittyDNA(BlakeTwo256::hash(dna_preimage)), + ..Default::default() + }, + v, + ) + .into()], + checker: FreeKittyConstraintChecker.into(), + } + } +} + impl Default for KittyData { fn default() -> Self { Self { diff --git a/wardrobe/money/src/lib.rs b/wardrobe/money/src/lib.rs index e3b714818..b012cc847 100644 --- a/wardrobe/money/src/lib.rs +++ b/wardrobe/money/src/lib.rs @@ -11,7 +11,8 @@ use tuxedo_core::{ dynamic_typing::{DynamicallyTypedData, UtxoData}, ensure, traits::Cash, - SimpleConstraintChecker, + types::Transaction, + SimpleConstraintChecker, Verifier, }; #[cfg(test)] @@ -75,6 +76,21 @@ impl Coin { pub fn new(amt: u128) -> Self { Coin(amt) } + + /// Create a mint transaction for a single Coin. + pub fn mint(amt: u128, v: V) -> Transaction + where + V: Verifier, + OV: Verifier + From, + OC: tuxedo_core::ConstraintChecker + From>, + { + Transaction { + inputs: vec![], + peeks: vec![], + outputs: vec![(Self::new(amt), v).into()], + checker: MoneyConstraintChecker::Mint.into(), + } + } } impl UtxoData for Coin {