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

TradableKitty piece #171

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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,
JoshOrndorff marked this conversation as resolved.
Show resolved Hide resolved
/// 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
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
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
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
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",
NadigerAmit marked this conversation as resolved.
Show resolved Hide 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)
}
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
Loading