@@ -15,6 +15,7 @@ module minitia_std::vip_score {
15
15
16
16
struct Scores has store {
17
17
total_score: u64 ,
18
+ is_finalized: bool ,
18
19
score: table::Table <address /* user */ , u64 >,
19
20
}
20
21
@@ -43,6 +44,9 @@ module minitia_std::vip_score {
43
44
/// The score is invalid.
44
45
const EINVALID_SCORE : u64 = 7 ;
45
46
47
+ /// The stage is already finalized.
48
+ const EFINALIED_STAGE : u64 = 8 ;
49
+
46
50
//
47
51
// Events
48
52
//
@@ -126,6 +130,7 @@ module minitia_std::vip_score {
126
130
if (!table::contains (&module_store.scores, stage)) {
127
131
table::add (&mut module_store.scores, stage, Scores {
128
132
total_score: 0 ,
133
+ is_finalized: false ,
129
134
score: table::new <address , u64 >()
130
135
});
131
136
};
@@ -144,7 +149,10 @@ module minitia_std::vip_score {
144
149
assert !(table::contains (&module_store.scores, stage), error::invalid_argument (EINVALID_STAGE ));
145
150
146
151
let scores = table::borrow_mut (&mut module_store.scores, stage);
152
+ assert !(!scores.is_finalized, error::invalid_argument (EFINALIED_STAGE ));
153
+
147
154
let score = table::borrow_mut_with_default (&mut scores.score, addr, 0 );
155
+
148
156
*score = *score + amount;
149
157
scores.total_score = scores.total_score + amount;
150
158
@@ -171,6 +179,8 @@ module minitia_std::vip_score {
171
179
assert !(table::contains (&module_store.scores, stage), error::invalid_argument (EINVALID_STAGE ));
172
180
173
181
let scores = table::borrow_mut (&mut module_store.scores, stage);
182
+ assert !(!scores.is_finalized, error::invalid_argument (EFINALIED_STAGE ));
183
+
174
184
let score = table::borrow_mut (&mut scores.score, addr);
175
185
assert !(*score >= amount, error::invalid_argument (EINSUFFICIENT_SCORE ));
176
186
*score = *score - amount;
@@ -193,12 +203,14 @@ module minitia_std::vip_score {
193
203
amount: u64
194
204
) acquires ModuleStore {
195
205
check_deployer_permission (deployer);
196
- assert !(amount > 0 , error::invalid_argument (EINVALID_SCORE ));
206
+ assert !(amount >= 0 , error::invalid_argument (EINVALID_SCORE ));
197
207
198
208
let module_store = borrow_global_mut <ModuleStore >(@minitia_std );
199
209
assert !(table::contains (&module_store.scores, stage), error::invalid_argument (EINVALID_STAGE ));
200
210
201
211
let scores = table::borrow_mut (&mut module_store.scores, stage);
212
+ assert !(!scores.is_finalized, error::invalid_argument (EFINALIED_STAGE ));
213
+
202
214
let score = table::borrow_mut_with_default (&mut scores.score, addr, 0 );
203
215
204
216
if (*score > amount) {
@@ -222,6 +234,18 @@ module minitia_std::vip_score {
222
234
//
223
235
// Entry functions
224
236
//
237
+ public entry fun finalize_script (
238
+ deployer: &signer ,
239
+ stage: u64
240
+ ) acquires ModuleStore {
241
+ check_deployer_permission (deployer);
242
+ let module_store = borrow_global_mut <ModuleStore >(@minitia_std );
243
+ assert !(table::contains (&module_store.scores, stage), error::invalid_argument (EINVALID_STAGE ));
244
+
245
+ let scores = table::borrow_mut (&mut module_store.scores, stage);
246
+ assert !(!scores.is_finalized, error::invalid_argument (EFINALIED_STAGE ));
247
+ scores.is_finalized = true ;
248
+ }
225
249
226
250
public entry fun update_score_script (
227
251
deployer: &signer ,
@@ -339,6 +363,34 @@ module minitia_std::vip_score {
339
363
init_module_for_test (chain);
340
364
remove_deployer_script (chain, signer ::address_of (deployer));
341
365
}
366
+
367
+ #[test(chain = @0x1 , deployer = @0x2 )]
368
+ #[expected_failure(abort_code = 0x10006 , location = Self)]
369
+ fun failed_not_match_length (chain: &signer , deployer: &signer ) acquires ModuleStore {
370
+ init_module_for_test (chain);
371
+ add_deployer_script (chain, signer ::address_of (deployer));
372
+
373
+ update_score_script (
374
+ deployer,
375
+ 1 ,
376
+ vector [@0x123 , @0x234 ],
377
+ vector []
378
+ );
379
+ }
380
+
381
+ #[test(chain = @0x1 , deployer = @0x2 , user = @0x123 )]
382
+ #[expected_failure(abort_code = 0x10008 , location = Self)]
383
+ fun failed_finalized_stage (chain: &signer , deployer: &signer , user: address ) acquires ModuleStore {
384
+ init_module_for_test (chain);
385
+ add_deployer_script (chain, signer ::address_of (deployer));
386
+ prepare_stage (deployer, 1 );
387
+
388
+ increase_score (deployer, user, 1 , 100 );
389
+ assert !(get_score (user, 1 ) == 100 , 1 );
390
+ finalize_script (deployer, 1 );
391
+ increase_score (deployer, user, 1 , 100 );
392
+ }
393
+
342
394
343
395
#[test(chain = @0x1 , deployer_a = @0x2 , deployer_b = @0x3 , user_a = @0x123 , user_b = @0x456 )]
344
396
fun test_e2e (chain: &signer , deployer_a: &signer , deployer_b: &signer , user_a: address , user_b: address ) acquires ModuleStore {
0 commit comments