6
6
//! - **Create:** Generate a new kitty.
7
7
//! To submit a valid transaction for creating a kitty, adhere to the following structure:
8
8
//! 1. Input must be empty.
9
- //! 2. Output must contain only the newly created kitty as a child.
10
-
9
+ //! 2. Output must contain only the newly created kittities as a child.
10
+ //!
11
+ //! **Note 1:** Multiple kitties can be created at the same time in the same txn..
12
+ //!
11
13
//! - **Update Name:** Modify the name of a kitty.
12
14
//! To submit a valid transaction for updating a kitty's name, adhere to the following structure:
13
15
//! 1. Input must be the kitty to be updated.
14
16
//! 2. Output must contain the kitty with the updated name.
15
17
//!
16
- //! **Note:** All other properties such as DNA, parents, free breedings, etc., must remain unaltered in the output.
18
+ //! **Note 1:** All other properties such as DNA, parents, free breedings, etc., must remain unaltered in the output.
19
+ //! **Note 2:** The input and output kitties must follow same order.
17
20
//!
18
21
//! - **Breed:** Breeds a new kitty using mom and dad based on below factors
19
- //! 1.) Mom and Tired have to be in a state where they are ready to breed
22
+ //! 1.) Mom and Dad have to be in a state where they are ready to breed
20
23
//! 2.) Each Mom and Dad have some DNA and the child will have unique DNA combined from the both of them
21
24
//! Linkable back to the Mom and Dad
22
25
//! 3.) The game also allows Kitties to have a cooling off period inbetween breeding before they can be bred again.
23
26
//! 4.) A rest operation allows for a Mom Kitty and a Dad Kitty to be cooled off
24
27
//!
25
- //! In order to submit a valid transaction you must strutucture it as follows:
28
+ //! In order to submit a valid transaction you must structure it as follows:
26
29
//! 1.) Input must contain 1 mom and 1 dad
27
30
//! 2.) Output must contain Mom, Dad, and newly created Child
28
31
//! 3.) A child's DNA is calculated by:
@@ -41,7 +44,7 @@ use sp_runtime::{
41
44
traits:: { BlakeTwo256 , Hash as HashT } ,
42
45
transaction_validity:: TransactionPriority ,
43
46
} ;
44
- use sp_std:: collections:: btree_map :: BTreeMap ;
47
+ use sp_std:: collections:: btree_set :: BTreeSet ; // For checking the uniqueness of input and output based on dna.
45
48
use sp_std:: prelude:: * ;
46
49
use tuxedo_core:: {
47
50
dynamic_typing:: { DynamicallyTypedData , UtxoData } ,
@@ -53,6 +56,10 @@ use tuxedo_core::{
53
56
#[ cfg( test) ]
54
57
mod tests;
55
58
59
+ /// The main constraint checker for the kitty piece. Allows below :
60
+ /// Create : Allows creation of kitty without parents, Multiple kitties can be created in same txn.
61
+ /// UpdateKittyName : Allows updating the name of the kitty s, Multiple kitty name can be updated in same txn.
62
+ /// Breed : Allows breeding of kitty.
56
63
#[ derive(
57
64
Serialize ,
58
65
Deserialize ,
@@ -68,14 +75,15 @@ mod tests;
68
75
TypeInfo ,
69
76
) ]
70
77
pub enum FreeKittyConstraintChecker {
71
- /// A transaction that creates kitty without parents.
78
+ /// Txn that creates kitty without parents.Multiple kitties can be created at the same time .
72
79
Create ,
73
- /// A Transaction that updates kitty Name.
80
+ /// Txn that updates kitty Name. Multiple kitty names can be updated. input & output must follow the same order .
74
81
UpdateKittyName ,
75
- /// A transaction where kitties are consumed and new family(Parents(mom,dad) and child) is created.
82
+ /// Txn where kitties are consumed and new family(Parents(mom,dad) and child) is created.
76
83
Breed ,
77
84
}
78
85
86
+ /// Dad kitty status with respect to breeding.
79
87
#[ derive(
80
88
Serialize ,
81
89
Deserialize ,
@@ -93,10 +101,13 @@ pub enum FreeKittyConstraintChecker {
93
101
) ]
94
102
pub enum DadKittyStatus {
95
103
#[ default]
104
+ /// Can breed.
96
105
RearinToGo ,
106
+ /// Can't breed due to tired.
97
107
Tired ,
98
108
}
99
109
110
+ /// Mad kitty status with respect to breeding.
100
111
#[ derive(
101
112
Serialize ,
102
113
Deserialize ,
@@ -114,10 +125,13 @@ pub enum DadKittyStatus {
114
125
) ]
115
126
pub enum MomKittyStatus {
116
127
#[ default]
128
+ /// Can breed.
117
129
RearinToGo ,
130
+ /// Can't breed due to recent child kitty delivery.
118
131
HadBirthRecently ,
119
132
}
120
133
134
+ /// Parent stuct contains 1 mom kitty and 1 dad kitty.
121
135
#[ derive(
122
136
Serialize ,
123
137
Deserialize ,
@@ -170,6 +184,12 @@ impl Default for Parent {
170
184
) ]
171
185
pub struct KittyDNA ( pub H256 ) ;
172
186
187
+ /// Kitty data contains basic informationsuch as below :
188
+ /// parent: 1 mom kitty and 1 dad kitty.
189
+ /// free_breedings: Free breeding allowed on a kitty.
190
+ /// dna :Its a unique per kitty.
191
+ /// num_breedings: number of free breedings are remaining.
192
+ /// name: Name of kitty.
173
193
#[ derive(
174
194
Serialize ,
175
195
Deserialize ,
@@ -233,6 +253,7 @@ impl UtxoData for KittyData {
233
253
const TYPE_ID : [ u8 ; 4 ] = * b"Kitt" ;
234
254
}
235
255
256
+ /// Reasons that kitty opertaion may go wrong.
236
257
#[ derive(
237
258
Serialize ,
238
259
Deserialize ,
@@ -293,8 +314,10 @@ pub enum ConstraintCheckerError {
293
314
CreatingWithInputs ,
294
315
/// No input for kitty Update.
295
316
InvalidNumberOfInputOutput ,
296
- /// Updating nothing
297
- OutputUtxoMissingError ,
317
+ /// Duplicate kitty found i.e based on the DNA.
318
+ DuplicateKittyFound ,
319
+ /// Dna mismatch between input and output.
320
+ DnaMismatchBetweenInputAndOutput ,
298
321
/// Name is not updated
299
322
KittyNameUnAltered ,
300
323
/// Kitty FreeBreeding cannot be updated.
@@ -593,8 +616,8 @@ impl SimpleConstraintChecker for FreeKittyConstraintChecker {
593
616
594
617
/// Checks:
595
618
/// - Input and output is of kittyType
596
- /// - Only name is updated and ther basicproperties are not updated.
597
- ///
619
+ /// - Only name is updated and ther basic properties are not updated.
620
+ /// - Order between input and output must be same.
598
621
pub fn can_kitty_name_be_updated (
599
622
input_data : & [ DynamicallyTypedData ] ,
600
623
output_data : & [ DynamicallyTypedData ] ,
@@ -603,34 +626,35 @@ pub fn can_kitty_name_be_updated(
603
626
input_data. len( ) == output_data. len( ) && !input_data. is_empty( ) ,
604
627
{ ConstraintCheckerError :: InvalidNumberOfInputOutput }
605
628
) ;
629
+ let mut dna_to_kitty_set: BTreeSet < KittyDNA > = BTreeSet :: new ( ) ;
606
630
607
- let mut map: BTreeMap < KittyDNA , KittyData > = BTreeMap :: new ( ) ;
608
-
609
- for utxo in input_data {
610
- let utxo_kitty = utxo
631
+ for i in 0 ..input_data. len ( ) {
632
+ let utxo_input_kitty = input_data[ i]
633
+ . clone ( )
611
634
. extract :: < KittyData > ( )
612
635
. map_err ( |_| ConstraintCheckerError :: BadlyTyped ) ?;
613
- map. insert ( utxo_kitty. clone ( ) . dna , utxo_kitty) ;
614
- }
615
636
616
- for utxo in output_data {
617
- let utxo_output_kitty = utxo
637
+ if dna_to_kitty_set. contains ( & utxo_input_kitty. dna ) {
638
+ return Err ( ConstraintCheckerError :: DuplicateKittyFound ) ;
639
+ } else {
640
+ dna_to_kitty_set. insert ( utxo_input_kitty. clone ( ) . dna ) ;
641
+ }
642
+
643
+ let utxo_output_kitty = output_data[ i]
644
+ . clone ( )
618
645
. extract :: < KittyData > ( )
619
646
. map_err ( |_| ConstraintCheckerError :: BadlyTyped ) ?;
620
-
621
- if let Some ( input_kitty) = map. remove ( & utxo_output_kitty. dna ) {
622
- // Element found, access the value
623
- check_kitty_name_update ( & input_kitty, & utxo_output_kitty) ?;
624
- } else {
625
- return Err ( ConstraintCheckerError :: OutputUtxoMissingError ) ;
647
+ if utxo_input_kitty. dna != utxo_output_kitty. dna {
648
+ return Err ( ConstraintCheckerError :: DnaMismatchBetweenInputAndOutput ) ;
626
649
}
650
+ check_kitty_name_update ( & utxo_input_kitty, & utxo_output_kitty) ?;
627
651
}
628
652
return Ok ( 0 ) ;
629
653
}
630
654
631
655
/// Checks:
632
656
/// - Private function used by can_kitty_name_be_updated.
633
- /// - Only name is updated and ther basicproperties are not updated.
657
+ /// - Only name is updated and ther basic properties are not updated.
634
658
///
635
659
fn check_kitty_name_update (
636
660
original_kitty : & KittyData ,
0 commit comments