-
Notifications
You must be signed in to change notification settings - Fork 19
TradableKitty piece
#171
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
base: main
Are you sure you want to change the base?
TradableKitty piece
#171
Changes from 2 commits
b522b1b
2e0885d
80d2e99
5461173
eea8241
1f5855b
4d4501b
32be05f
7b0a12e
389576b
c77120b
d57f9b1
db5748d
02aaf78
9c4f903
9e2c908
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -50,7 +50,12 @@ mod tests; | |
| Debug, | ||
| TypeInfo, | ||
| )] | ||
| pub struct FreeKittyConstraintChecker; | ||
| pub enum FreeKittyConstraintChecker { | ||
| /// A mint transaction that creates kitty without parents. | ||
| Mint, | ||
| /// A typical Breed transaction where kitties are consumed and new family(Parents(mom,dad) and child) is created. | ||
| Breed, | ||
| } | ||
|
|
||
| #[derive( | ||
| Serialize, | ||
|
|
@@ -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], | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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:
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
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.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok. I updated the struct as you suggested. |
||
| } | ||
|
|
||
| impl KittyData { | ||
|
|
@@ -187,7 +193,7 @@ impl KittyData { | |
| v, | ||
| ) | ||
| .into()], | ||
| checker: FreeKittyConstraintChecker.into(), | ||
| checker: FreeKittyConstraintChecker::Mint.into(), | ||
| } | ||
| } | ||
| } | ||
|
|
@@ -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.
|
||
| } | ||
| } | ||
| } | ||
|
|
@@ -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. | ||
|
|
@@ -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
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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?
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @muraca
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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:
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) | ||
| } | ||
| } | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.