Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions tuxedo-template-runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ sp-consensus-grandpa = { default_features = false, workspace = true }
# Tuxedo Core and Pieces
amoeba = { default-features = false, path = "../wardrobe/amoeba" }
kitties = { default-features = false, path = "../wardrobe/kitties" }
tradable-kitties = { default-features = false, path = "../wardrobe/tradable_kitties" }
money = { default-features = false, path = "../wardrobe/money" }
poe = { default-features = false, path = "../wardrobe/poe" }
runtime-upgrade = { default-features = false, path = "../wardrobe/runtime_upgrade" }
Expand Down
5 changes: 5 additions & 0 deletions tuxedo-template-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ pub use money;
pub use poe;
pub use runtime_upgrade;
pub use timestamp;
pub use tradable_kitties;

/// Opaque types. These are used by the CLI to instantiate machinery that don't need to know
/// the specifics of the runtime. They can then be made to be agnostic over specific formats
Expand Down Expand Up @@ -170,6 +171,8 @@ pub enum OuterConstraintChecker {
Money(money::MoneyConstraintChecker<0>),
/// Checks Free Kitty transactions
FreeKittyConstraintChecker(kitties::FreeKittyConstraintChecker),
/// Checks Paid Kitty transactions
TradableKittyConstraintChecker(tradable_kitties::TradableKittyConstraintChecker<0>),
/// Checks that an amoeba can split into two new amoebas
AmoebaMitosis(amoeba::AmoebaMitosis),
/// Checks that a single amoeba is simply removed from the state
Expand Down Expand Up @@ -205,6 +208,8 @@ pub enum OuterConstraintChecker {
Money(money::MoneyConstraintChecker<0>),
/// Checks Free Kitty transactions
FreeKittyConstraintChecker(kitties::FreeKittyConstraintChecker),
/// Checks Paid Kitty transactions
TradableKittyConstraintChecker(tradable_kitties::TradableKittyConstraintChecker<0>),
/// Checks that an amoeba can split into two new amoebas
AmoebaMitosis(amoeba::AmoebaMitosis),
/// Checks that a single amoeba is simply removed from the state
Expand Down
60 changes: 47 additions & 13 deletions wardrobe/kitties/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,12 @@ mod tests;
Debug,
TypeInfo,
)]
pub struct FreeKittyConstraintChecker;
pub enum FreeKittyConstraintChecker {
/// A mint transaction that creates kitty without parents.
Mint,
Comment thread
JoshOrndorff marked this conversation as resolved.
Outdated
/// A typical Breed transaction where kitties are consumed and new family(Parents(mom,dad) and child) is created.
Breed,
}

#[derive(
Serialize,
Expand Down Expand Up @@ -165,6 +170,7 @@ pub struct KittyData {
pub free_breedings: u64, // Ignore in breed for money case
pub dna: KittyDNA,
pub num_breedings: u128,
pub name: [u8; 4],
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, I think the fields should follow a more logical order, such as:

  • DNA
  • Name
  • Parents
  • free_breedings
  • num_breedings

Copy link
Copy Markdown
Contributor Author

@NadigerAmit NadigerAmit Mar 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is generally better to add new members at the end of a struct. This practice aligns with the principle of maintaining backward compatibility. When we add a new member at the end of the struct, existing code that uses the struct won't be affected, as the layout of the existing members remains unchanged.

If you add a new member in the middle of a struct, it can break existing code that relies on the order and size of the struct members. This is because the memory layout of the struct may change, leading to potential issues with code that assumes a specific order or size.

By appending new members at the end, we follow a practice commonly referred to as "struct versioning" or "extensible struct pattern," where you ensure that new fields are added without affecting the existing layout. This helps in maintaining compatibility and minimizes the risk of introducing errors in the existing codebase.

As of now, I don't see any code which is relying on the layout of the structure.
If it is a strong request, I will update it. Otherwise, I want to keep it as it is.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is generally better to add new members at the end of a struct. This practice aligns with the principle of maintaining backward compatibility.

Although you are not wrong, at this stage of the development we don't need to care about this, and we should prioritize doing stuff that makes sense and is clear and understandable.

And sometimes, you do want to break compatibility.

Copy link
Copy Markdown
Contributor Author

@NadigerAmit NadigerAmit Mar 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok. I updated the struct as you suggested.

}

impl KittyData {
Expand All @@ -187,7 +193,7 @@ impl KittyData {
v,
)
.into()],
checker: FreeKittyConstraintChecker.into(),
checker: FreeKittyConstraintChecker::Mint.into(),
}
}
}
Expand All @@ -199,6 +205,7 @@ impl Default for KittyData {
free_breedings: 2,
dna: KittyDNA(H256::from_slice(b"mom_kitty_1asdfasdfasdfasdfasdfa")),
num_breedings: 3,
name: *b"kity",
Comment thread
NadigerAmit marked this conversation as resolved.
}
}
}
Expand Down Expand Up @@ -261,9 +268,15 @@ pub enum ConstraintCheckerError {
TooManyBreedingsForKitty,
/// Not enough free breedings available for these parents.
NotEnoughFreeBreedings,
/// Incorrect number of outputs when it comes to Minting.
IncorrectNumberOfKittiesForMintOperation,
/// The transaction attempts to mint no Kitty.
MintingNothing,
/// Inputs(Parents) not required for mint.
MintingWithInputs,
}

trait Breed {
pub trait Breed {
/// The Cost to breed a kitty if it is not free.
const COST: u128;
/// Number of free breedings a kitty will have.
Expand Down Expand Up @@ -510,18 +523,39 @@ impl SimpleConstraintChecker for FreeKittyConstraintChecker {
_peeks: &[DynamicallyTypedData],
output_data: &[DynamicallyTypedData],
) -> Result<TransactionPriority, Self::Error> {
// Input must be a Mom and a Dad
ensure!(input_data.len() == 2, Self::Error::TwoParentsDoNotExist);

let mom = KittyData::try_from(&input_data[0])?;
let dad = KittyData::try_from(&input_data[1])?;
KittyHelpers::can_breed(&mom, &dad)?;
match &self {
Self::Mint => {
// Make sure there are no inputs being consumed
ensure!(
input_data.is_empty(),
ConstraintCheckerError::MintingWithInputs
);
// Make sure there is at least one output being minted
ensure!(
!output_data.is_empty(),
ConstraintCheckerError::MintingNothing
);
// Make sure the outputs are the right type
for utxo in output_data {
let _utxo_kitty = utxo
.extract::<KittyData>()
.map_err(|_| ConstraintCheckerError::BadlyTyped)?;
}
Ok(0)
Comment on lines +586 to +605
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we somehow check that a Kitty with the same DNA does not exist?
cc @JoshOrndorff

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is one of the reasons I didn't like minting kitties from scratch

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I raised this question earlier with Joshy and found that no need to check for duplicate DNA check since there can be twin kitties with duplicate DNA. So I removed the duplicate DNA check.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, we state in multiple parts of the documentation that DNA is unique 😄

As far as I remember, the twins use-case was not initially part of Kitties, what's the reason behind adding it?

Moreover, twins in my opinion should not have the exact same DNA, as it is also in real life

Copy link
Copy Markdown
Contributor Author

@NadigerAmit NadigerAmit Mar 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@muraca
Please see the discussion here: #171 (comment)
Earlier I implemented the DNA check for create operation and other operations also.
There were 2 options from Joshy either "universal creator pattern" or allow duplicate DNA : I chose the 2nd option i.e allow duplicate DNA kitties.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On a call recently, I encouraged @NadigerAmit to design the game fully before trying to build it and get a PR approved. Specifically I encouraged him to consider:

  1. Who is going to be able to mint in production? Only a centralized team? Or any rando? How will minting be sybil resistant?
  2. Should every kitty have unique DNA? If so implement a universal creator pattern as shown in NFT with Royalties piece #185 .

I don't think there are right vs wrong answers. But you need a design and you need to be consistent about it. I worry we reached a point where Amit feels very "close" to getting this PR merged, but I feel the design work isn't even done to compare the code against.

}
Self::Breed => {
ensure!(input_data.len() == 2, Self::Error::TwoParentsDoNotExist);
let mom = KittyData::try_from(&input_data[0])?;
let dad = KittyData::try_from(&input_data[1])?;
KittyHelpers::can_breed(&mom, &dad)?;

// Output must be Mom, Dad, Child
ensure!(output_data.len() == 3, Self::Error::NotEnoughFamilyMembers);
// Output must be Mom, Dad, Child
ensure!(output_data.len() == 3, Self::Error::NotEnoughFamilyMembers);

KittyHelpers::check_new_family(&mom, &dad, output_data)?;
KittyHelpers::check_new_family(&mom, &dad, output_data)?;

Ok(0)
Ok(0)
}
}
}
}
Loading