Skip to content

Commit

Permalink
beautify TuxedoGenesisConfig
Browse files Browse the repository at this point in the history
Signed-off-by: muraca <mmuraca247@gmail.com>
  • Loading branch information
muraca committed Nov 7, 2023
1 parent a55975d commit 02a463f
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 46 deletions.
56 changes: 12 additions & 44 deletions tuxedo-template-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -110,9 +109,8 @@ pub struct TuxedoGenesisConfig(pub Vec<Transaction>);
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");
Expand All @@ -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)
}
Expand Down Expand Up @@ -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 =
Expand Down
39 changes: 38 additions & 1 deletion wardrobe/kitties/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -155,6 +167,31 @@ pub struct KittyData {
pub num_breedings: u128,
}

impl KittyData {
/// Create a mint transaction for a single Kitty.
pub fn mint<V, OV, OC>(parent: Parent, dna_preimage: &[u8], v: V) -> Transaction<OV, OC>
where
V: Verifier,
OV: Verifier + From<V>,
OC: tuxedo_core::ConstraintChecker<OV> + From<FreeKittyConstraintChecker>,
{
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 {
Expand Down
18 changes: 17 additions & 1 deletion wardrobe/money/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ use tuxedo_core::{
dynamic_typing::{DynamicallyTypedData, UtxoData},
ensure,
traits::Cash,
SimpleConstraintChecker,
types::Transaction,
SimpleConstraintChecker, Verifier,
};

#[cfg(test)]
Expand Down Expand Up @@ -75,6 +76,21 @@ impl<const ID: u8> Coin<ID> {
pub fn new(amt: u128) -> Self {
Coin(amt)
}

/// Create a mint transaction for a single Coin.
pub fn mint<V, OV, OC>(amt: u128, v: V) -> Transaction<OV, OC>
where
V: Verifier,
OV: Verifier + From<V>,
OC: tuxedo_core::ConstraintChecker<OV> + From<MoneyConstraintChecker<ID>>,
{
Transaction {
inputs: vec![],
peeks: vec![],
outputs: vec![(Self::new(amt), v).into()],
checker: MoneyConstraintChecker::Mint.into(),
}
}
}

impl<const ID: u8> UtxoData for Coin<ID> {
Expand Down

0 comments on commit 02a463f

Please sign in to comment.