-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.rs
459 lines (363 loc) · 12.8 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
#![cfg_attr(not(feature = "std"), no_std)]
/// Edit this file to define custom logic or remove it if it is not needed.
/// Learn more about FRAME and the core library of Substrate FRAME pallets:
/// <https://substrate.dev/docs/en/knowledgebase/runtime/frame>
pub use pallet::*;
#[cfg(test)]
mod mock;
#[cfg(test)]
mod tests;
#[cfg(feature = "runtime-benchmarks")]
mod benchmarking;
#[frame_support::pallet]
pub mod pallet {
use sp_runtime::traits::MaybeDisplay;
use sp_runtime::traits::AtLeast32Bit;
use frame_support::dispatch::fmt::Debug;
use frame_support::{dispatch::DispatchResult, pallet_prelude::*};
use frame_system::pallet_prelude::*;
use sp_std::prelude::*;
#[derive(Encode, Decode, Debug, Clone, PartialEq)]
pub enum Access {
External,
InternalExternal,
}
impl Default for Access {
fn default() -> Self {
Access::External
}
}
#[derive(Encode, Decode, Debug, Clone, Copy, PartialEq)]
pub enum Route {
External = 0,
Internal = 1,
}
/// Configure the pallet by specifying the parameters and types on which it depends.
#[pallet::config]
pub trait Config: frame_system::Config {
/// Because this pallet emits events, it depends on the runtime's definition of an event.
type Event: From<Event<Self>> + IsType<<Self as frame_system::Config>::Event>;
/// Account index (aka nonce) type. This stores the number of previous transactions
/// associated with a sender account.
type GameID: Parameter
+ Member
+ MaybeSerializeDeserialize
+ Debug
+ Default
+ MaybeDisplay
+ AtLeast32Bit
+ Copy
+ Encode
+ Decode;
}
#[pallet::pallet]
#[pallet::generate_store(pub(super) trait Store)]
pub struct Pallet<T>(_);
pub(super) type Skey = Vec<u8>;
pub(super) type Sval = Vec<u8>;
pub(super) type DataEntry = (Skey,Sval);
pub(super) type DataRecord = Vec<DataEntry>;
pub(super) type Permission<T> = (<T as self::Config>::GameID, Access);
pub(super) type UserID<T> = (<T as self::Config>::GameID, <T as frame_system::Config>::AccountId);
#[pallet::storage]
#[pallet::getter(fn world_data_map)]
pub(super) type WorldDataMap<T: Config> = StorageDoubleMap<_, Twox64Concat, T::GameID, Twox64Concat, Route, DataRecord, ValueQuery>;
#[pallet::storage]
// #[pallet::getter(fn user_data_map)]
pub(super) type UserDataMap<T: Config> = StorageDoubleMap<_, Twox64Concat, UserID<T>, Twox64Concat, Route, DataRecord, ValueQuery>;
#[pallet::storage]
// #[pallet::getter(fn authorities_map)]
pub(super) type AuthoritiesMap<T: Config> = StorageMap<_, Twox64Concat, T::AccountId, Vec<Permission<T>>, ValueQuery>;
// Pallets use events to inform users when important changes are made.
// https://substrate.dev/docs/en/knowledgebase/runtime/events
#[pallet::event]
#[pallet::metadata(T::AccountId = "AccountId")]
#[pallet::generate_deposit(pub(super) fn deposit_event)]
pub enum Event<T: Config> {
/// Event documentation should end with an array that provides descriptive names for event
/// parameters. [something, who]
SomethingStored(u32, T::AccountId),
// [current level, who leveled up]
LevelUp(u32, T::AccountId),
// [Game world, updated data]
WorldDataUpdate(T::GameID, DataEntry)
}
// Errors inform users that something went wrong.
#[pallet::error]
pub enum Error<T> {
/// Error names should be descriptive.
NoneValue,
/// Errors should have helpful documentation associated with them.
StorageOverflow,
AlreadyRegisteredGame,
AlreadyRegisteredAuthority,
InvalidAuthority,
InvalidAccess,
NotFound,
BadSize,
Fake
}
#[pallet::genesis_config]
pub struct GenesisConfig<T: Config> {
pub fps_game_authority: T::AccountId,
pub fps_game_id: T::GameID,
pub platformer_game_authority: T::AccountId,
pub platformer_game_id: T::GameID,
}
#[cfg(feature = "std")]
impl<T: Config> Default for GenesisConfig<T> {
fn default() -> Self {
Self {
fps_game_authority: Default::default(),
fps_game_id: Default::default(),
platformer_game_authority: Default::default(),
platformer_game_id: Default::default()
}
}
}
#[pallet::genesis_build]
impl<T: Config> GenesisBuild<T> for GenesisConfig<T> {
fn build(&self) {
// FPS
<AuthoritiesMap<T>>::insert(&self.fps_game_authority,
vec!((self.fps_game_id, Access::InternalExternal)));
let entry : DataEntry= (
String::into_bytes(String::from("Time")),
1i32.to_le_bytes().to_vec(),
);
<WorldDataMap<T>>::insert(&self.fps_game_id, Route::External, vec!(&entry));
// PLATFORMER
<AuthoritiesMap<T>>::insert(&self.platformer_game_authority,
vec!((self.platformer_game_id, Access::InternalExternal)));
let entry1 : DataEntry= (
String::into_bytes(String::from("Kills")),
0u32.to_le_bytes().to_vec(),
);
let entry2 : DataEntry= (
String::into_bytes(String::from("Deaths")),
0u32.to_le_bytes().to_vec(),
);
<WorldDataMap<T>>::insert(&self.platformer_game_id, Route::External, vec!(&entry1,&entry2));
}
}
fn is_authority<T: Config>(who : &T::AccountId, game : T::GameID) -> (bool, Access)
{
let bad_result = (false, Access::default());
// Are they listed in authorities at all?
let permissions = match <AuthoritiesMap<T>>::try_get(who)
{
Err(_) => {return bad_result;},
Ok(p) => p
};
// Do they have authority for the specified game?
for p in permissions
{
if p.0 == game
{
return (true, p.1);
}
}
bad_result
}
fn game_exists<T: Config>(game : &T::GameID) -> bool
{
for authority in <AuthoritiesMap<T>>::iter()
{
let permissions = authority.1;
if permissions.iter().any(|e| e.0 == *game)
{
return true;
}
}
false
}
fn is_authorized_call<T: Config> (origin: OriginFor<T>, game : T::GameID, route : Route) -> Result<T::AccountId, sp_runtime::DispatchError>
{
let who = ensure_signed(origin)?;
let who_auth = is_authority::<T>(&who, game);
ensure!(who_auth.0, Error::<T>::InvalidAuthority);
if route == Route::Internal
{
ensure!(who_auth.1 == Access::InternalExternal, Error::<T>::InvalidAccess)
}
Ok(who)
}
// Dispatchable functions allows users to interact with the pallet and invoke state changes.
// These functions materialize as "extrinsics", which are often compared to transactions.
// Dispatchable functions must be annotated with a weight and must return a DispatchResult.
#[pallet::call]
impl<T: Config> Pallet<T> {
#[pallet::weight(10_000)]
pub fn world_remove_data_record(origin: OriginFor<T>, game : T::GameID, entry : DataEntry, route : Route) -> DispatchResult
{
is_authorized_call::<T>(origin, game, route)?;
ensure!(<WorldDataMap<T>>::contains_key(game, route), Error::<T>::NotFound);
<WorldDataMap<T>>::mutate(game, route, |x| {
match x.iter().position(|cur_entry| cur_entry.0 == entry.0) {
None => Err(Error::<T>::NotFound)?,
Some(d) => {
x.swap_remove(d);
Ok(())
}
}
})
}
#[pallet::weight(10_000)]
pub fn world_update_data_record(origin: OriginFor<T>, game : T::GameID, entry : DataEntry, route : Route) -> DispatchResult
{
is_authorized_call::<T>(origin, game, route)?;
if ! <WorldDataMap<T>>::contains_key(game, route)
{
<WorldDataMap<T>>::insert(game, route, vec!(&entry));
}
else
{
<WorldDataMap<T>>::mutate(game, route, |record| {
match record.iter().position(|cur_entry| cur_entry.0 == (&entry).0) {
// Generate a new entry.
None => { record.push(entry.clone()); },
// Assign new value to existing entry.
Some(index) => { record[index].1 = (&entry).1.clone(); }
};
});
}
// Emit an event.
Self::deposit_event(Event::WorldDataUpdate(game, entry));
Ok(())
}
#[pallet::weight(10_000)]
pub fn world_mod_data_record(origin: OriginFor<T>, game : T::GameID, new_entry : DataEntry, route : Route, ) -> DispatchResult
{
is_authorized_call::<T>(origin, game, route)?;
ensure!(<WorldDataMap<T>>::contains_key(game, route), Error::<T>::NotFound);
<WorldDataMap<T>>::mutate(game, route, |record :&mut DataRecord| {
if false //HACK. IDK why I need this. Compiler compaints.
{
return Err(Error::<T>::Fake);
}
let index : usize = record.iter().position(|cur_entry : &DataEntry| cur_entry.0 == (&new_entry).0).ok_or(Error::<T>::NotFound)?;
let exisiting_entry = &mut record[index];
//Ensure same length
const SUPPORTED_BYTE_LENGTH : usize = 4;
ensure!(exisiting_entry.1.len() == SUPPORTED_BYTE_LENGTH, Error::<T>::BadSize);
ensure!(new_entry.1.len() == SUPPORTED_BYTE_LENGTH, Error::<T>::BadSize);
// Interpret existing numeric value.
let existing_numeric : i32;
{
let mut vec_data : [u8; SUPPORTED_BYTE_LENGTH] = Default::default();
vec_data.copy_from_slice(&exisiting_entry.1[0..SUPPORTED_BYTE_LENGTH]);
existing_numeric = i32::from_le_bytes(vec_data);
}
// Interpret incoming numberic value.
let incoming_numeric : i32;
{
let mut vec_data : [u8; SUPPORTED_BYTE_LENGTH] = Default::default();
vec_data.copy_from_slice(&new_entry.1[0..SUPPORTED_BYTE_LENGTH]);
incoming_numeric = i32::from_le_bytes(vec_data);
}
// Increment|Decrement value.
let result_numeric = existing_numeric + incoming_numeric;
let result_numeric = result_numeric.to_le_bytes();
exisiting_entry.1 = result_numeric.to_vec();
Ok(())
})?;
// // Emit an event.
// Self::deposit_event(Event::WorldDataUpdate(game, new_entry));
Ok(())
}
#[pallet::weight(10_000)]
pub fn user_remove_data_record(origin: OriginFor<T>, game : T::GameID, user : T::AccountId, route : Route, entry_key : Skey) -> DispatchResult
{
is_authorized_call::<T>(origin, game, route)?;
let map_key = (game, user);
ensure!(<UserDataMap<T>>::contains_key(&map_key, route), Error::<T>::NotFound);
<UserDataMap<T>>::mutate(&map_key, route, |x| {
match x.iter().position(|cur_entry| cur_entry.0 == entry_key) {
None => Err(Error::<T>::NotFound)?,
Some(d) => {
x.swap_remove(d);
Ok(())
}
}
})
}
#[pallet::weight(10_000)]
pub fn user_update_data_record(origin: OriginFor<T>, game : T::GameID, user : T::AccountId, route : Route, entry : DataEntry) -> DispatchResult
{
is_authorized_call::<T>(origin, game, route)?;
let map_key = (game, user);
if ! <UserDataMap<T>>::contains_key(&map_key, route)
{
<UserDataMap<T>>::insert(&map_key, route, vec!(entry));
}
else
{
<UserDataMap<T>>::mutate(&map_key, route, |x| {
match x.iter().position(|cur_entry| cur_entry.0 == entry.0) {
None => { x.push(entry); },
Some(d) => { x[d].1 = entry.1; }
};
});
}
Ok(())
}
#[pallet::weight(10_000)]
pub fn register_game(origin: OriginFor<T>, game : T::GameID) -> DispatchResult
{
let who = ensure_signed(origin)?;
frame_support::ensure!(! game_exists::<T>(&game), Error::<T>::AlreadyRegisteredGame);
let new_entry = (game, Access::InternalExternal);
<AuthoritiesMap<T>>::insert(&who, vec!(new_entry));
Ok(())
}
#[pallet::weight(10_000)]
pub fn add_authority(origin: OriginFor<T>, game : T::GameID, new_authority : T::AccountId, access : Access) -> DispatchResult
{
let who = ensure_signed(origin)?;
// Ensure only authorities add other authorities.
let who_auth = is_authority::<T>(&who, game);
frame_support::ensure!(who_auth.0, Error::<T>::InvalidAuthority);
// Ensure new authority is not already registered
let new_auth = is_authority::<T>(&new_authority, game);
frame_support::ensure!(!new_auth.0, Error::<T>::InvalidAuthority);
// Does new authority have a map entry?
match <AuthoritiesMap<T>>::try_get(new_authority.clone()) //Hack: workaround for borrowing in match arms.
{
// Update entry.
Ok(_) => {
<AuthoritiesMap<T>>::mutate(&new_authority, |x| {
let entry = (game,access);
x.push(entry);
});
},
// Create entry.
Err(_) => {
let new_entry = (game, access);
<AuthoritiesMap<T>>::insert(&new_authority, vec!(new_entry));
}
};
Ok(())
}
#[pallet::weight(10_000)]
pub fn remove_authority(origin: OriginFor<T>, game : T::GameID, removed_authority : T::AccountId) -> DispatchResult
{
let who = ensure_signed(origin)?;
// Ensure only authorities remove authorities.
let who_auth = is_authority::<T>(&who, game);
frame_support::ensure!(who_auth.0, Error::<T>::InvalidAuthority);
// If removing auhtority other than self...
if who != removed_authority
{
// Ensure removed authority is already registered
let new_auth = is_authority::<T>(&removed_authority, game);
frame_support::ensure!(new_auth.0, Error::<T>::InvalidAuthority);
}
// Mutate for removal & return Result. We should not error here at this point.
<AuthoritiesMap<T>>::mutate(&removed_authority, |x| {
let index = x.iter().position(|p| p.0 == game).ok_or(Error::<T>::InvalidAuthority)?;
x.swap_remove(index);
Ok(())
})
}
}
}