-
Notifications
You must be signed in to change notification settings - Fork 4
/
lib.rs
158 lines (128 loc) · 4.13 KB
/
lib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#![cfg_attr(not(feature = "std"), no_std)]
use codec::{Encode, Decode};
use frame_support::{
decl_module, decl_storage, decl_event, decl_error, ensure, StorageValue, StorageDoubleMap,
traits::Randomness, RuntimeDebug, dispatch::{DispatchResult, DispatchError},
};
use sp_io::hashing::blake2_128;
use frame_system::ensure_signed;
#[cfg(test)]
mod tests;
#[derive(Encode, Decode, Clone, RuntimeDebug, PartialEq, Eq)]
pub struct Kitty(pub [u8; 16]);
#[derive(Encode, Decode, Clone, Copy, RuntimeDebug, PartialEq, Eq)]
pub enum KittyGender {
Male,
Female,
}
impl Kitty {
pub fn gender(&self) -> KittyGender {
if self.0[0] % 2 == 0 {
KittyGender::Male
} else {
KittyGender::Female
}
}
}
pub trait Trait: frame_system::Trait {
type Event: From<Event<Self>> + Into<<Self as frame_system::Trait>::Event>;
}
decl_storage! {
trait Store for Module<T: Trait> as Kitties {
/// Stores all the kitties, key is the kitty id
pub Kitties get(fn kitties): double_map hasher(blake2_128_concat) T::AccountId, hasher(blake2_128_concat) u32 => Option<Kitty>;
/// Stores the next kitty ID
pub NextKittyId get(fn next_kitty_id): u32;
}
}
decl_event! {
pub enum Event<T> where
<T as frame_system::Trait>::AccountId,
{
/// A kitty is created. \[owner, kitty_id, kitty\]
KittyCreated(AccountId, u32, Kitty),
/// A new kitten is bred. \[owner, kitty_id, kitty\]
KittyBred(AccountId, u32, Kitty),
}
}
decl_error! {
pub enum Error for Module<T: Trait> {
KittiesIdOverflow,
InvalidKittyId,
SameGender,
}
}
decl_module! {
pub struct Module<T: Trait> for enum Call where origin: T::Origin {
type Error = Error<T>;
fn deposit_event() = default;
/// Create a new kitty
#[weight = 1000]
pub fn create(origin) {
let sender = ensure_signed(origin)?;
// TODO: refactor this method to use
// `Self::random_value` and `Self::get_next_kitty_id`
// to simplify the implementation
NextKittyId::try_mutate(|next_id| -> DispatchResult {
let current_id = *next_id;
*next_id = next_id.checked_add(1).ok_or(Error::<T>::KittiesIdOverflow)?;
let payload = (
<pallet_randomness_collective_flip::Module<T> as Randomness<T::Hash>>::random_seed(),
&sender,
<frame_system::Module<T>>::extrinsic_index(),
);
let dna = payload.using_encoded(blake2_128);
// Create and store kitty
let kitty = Kitty(dna);
Kitties::<T>::insert(&sender, current_id, kitty.clone());
// Emit event
Self::deposit_event(RawEvent::KittyCreated(sender, current_id, kitty));
Ok(())
})?;
}
/// Breed kitties
#[weight = 1000]
pub fn breed(origin, kitty_id_1: u32, kitty_id_2: u32) {
let sender = ensure_signed(origin)?;
let kitty1 = Self::kitties(&sender, kitty_id_1).ok_or(Error::<T>::InvalidKittyId)?;
let kitty2 = Self::kitties(&sender, kitty_id_2).ok_or(Error::<T>::InvalidKittyId)?;
ensure!(kitty1.gender() != kitty2.gender(), Error::<T>::SameGender);
let kitty_id = Self::get_next_kitty_id()?;
let kitty1_dna = kitty1.0;
let kitty2_dna = kitty2.0;
let selector = Self::random_value(&sender);
let mut new_dna = [0u8; 16];
// Combine parents and selector to create new kitty
for i in 0..kitty1_dna.len() {
new_dna[i] = combine_dna(kitty1_dna[i], kitty2_dna[i], selector[i]);
}
let new_kitty = Kitty(new_dna);
Kitties::<T>::insert(&sender, kitty_id, &new_kitty);
Self::deposit_event(RawEvent::KittyBred(sender, kitty_id, new_kitty));
}
}
}
pub fn combine_dna(dna1: u8, dna2: u8, selector: u8) -> u8 {
// TODO: finish this implementation
// selector[bit_index] == 0 -> use dna1[bit_index]
// selector[bit_index] == 1 -> use dna2[bit_index]
// e.g.
// selector = 0b00000001
// dna1 = 0b10101010
// dna2 = 0b00001111
// result = 0b10101011
0
}
impl<T: Trait> Module<T> {
fn get_next_kitty_id() -> sp_std::result::Result<u32, DispatchError> {
NextKittyId::try_mutate(|next_id| -> sp_std::result::Result<u32, DispatchError> {
let current_id = *next_id;
*next_id = next_id.checked_add(1).ok_or(Error::<T>::KittiesIdOverflow)?;
Ok(current_id)
})
}
fn random_value(sender: &T::AccountId) -> [u8; 16] {
// TODO: finish this implementation
Default::default()
}
}