Skip to content

Commit d049019

Browse files
authored
add finalized flag (#18)
* add finalized flag * add update snapshot interface * add oerator releasetime cond
1 parent db713e8 commit d049019

File tree

4 files changed

+138
-1
lines changed

4 files changed

+138
-1
lines changed
238 Bytes
Binary file not shown.

precompile/binaries/stdlib/vip.mv

430 Bytes
Binary file not shown.

precompile/modules/initia_stdlib/sources/vip/vip.move

+85
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ module initia_std::vip {
4545
const ESNAPSHOT_ALREADY_EXISTS: u64 = 16;
4646
const EINVALID_BATCH_ARGUMENT: u64 = 17;
4747
const EINVALID_TOTAL_REWARD: u64 = 18;
48+
const ESNAPSHOT_NOT_EXISTS: u64 = 19;
49+
const EALREADY_RELEASED: u64 = 20;
4850

4951
//
5052
// Constants
@@ -134,6 +136,11 @@ module initia_std::vip {
134136
maximum_tvl: u64,
135137
}
136138

139+
struct SnapshotResponse has drop {
140+
merkle_root: vector<u8>,
141+
total_l2_score: u64
142+
}
143+
137144
struct StageDataResponse has drop {
138145
pool_split_ratio: Decimal256,
139146
total_operator_funded_reward: u64,
@@ -758,6 +765,28 @@ module initia_std::vip {
758765
});
759766
}
760767

768+
public entry fun update_snapshot(
769+
agent: &signer,
770+
bridge_id: u64,
771+
stage: u64,
772+
merkle_root: vector<u8>,
773+
total_l2_score: u64,
774+
) acquires ModuleStore {
775+
check_agent_permission(agent);
776+
let module_store = borrow_global_mut<ModuleStore>(@initia_std);
777+
assert!(table::contains(&module_store.stage_data, table_key::encode_u64(stage)), error::not_found(ESTAGE_DATA_NOT_FOUND));
778+
let stage_data = table::borrow_mut(&mut module_store.stage_data, table_key::encode_u64(stage));
779+
780+
let (_, block_time) = block::get_block_info();
781+
assert!(block_time < stage_data.user_vesting_release_time, error::unavailable(EALREADY_RELEASED));
782+
assert!(block_time < stage_data.operator_vesting_release_time, error::unavailable(EALREADY_RELEASED));
783+
assert!(table::contains(&stage_data.snapshots, table_key::encode_u64(bridge_id)), error::not_found(ESNAPSHOT_NOT_EXISTS));
784+
785+
let snapshot = table::borrow_mut(&mut stage_data.snapshots, table_key::encode_u64(bridge_id));
786+
snapshot.merkle_root = merkle_root;
787+
snapshot.total_l2_score = total_l2_score;
788+
}
789+
761790
public entry fun claim_operator_reward_script(
762791
operator: &signer,
763792
bridge_id: u64,
@@ -973,6 +1002,21 @@ module initia_std::vip {
9731002
//
9741003
// View Functions
9751004
//
1005+
1006+
#[view]
1007+
public fun get_snapshot(bridge_id: u64, stage: u64): SnapshotResponse acquires ModuleStore {
1008+
let module_store = borrow_global<ModuleStore>(@initia_std);
1009+
1010+
assert!(table::contains(&module_store.stage_data, table_key::encode_u64(stage)), error::not_found(ESTAGE_DATA_NOT_FOUND));
1011+
let snapshots = table::borrow(&module_store.stage_data, table_key::encode_u64(stage));
1012+
assert!(table::contains(&snapshots.snapshots, table_key::encode_u64(bridge_id)), error::not_found(ESNAPSHOT_NOT_EXISTS));
1013+
let snapshot = table::borrow(&snapshots.snapshots, table_key::encode_u64(bridge_id));
1014+
1015+
SnapshotResponse {
1016+
merkle_root: snapshot.merkle_root,
1017+
total_l2_score: snapshot.total_l2_score,
1018+
}
1019+
}
9761020

9771021
#[view]
9781022
public fun get_expected_reward(bridge_id: u64, fund_reward_amount: u64): u64 acquires ModuleStore {
@@ -1993,6 +2037,47 @@ module initia_std::vip {
19932037
), 0);
19942038
}
19952039

2040+
#[test(chain=@0x1, operator=@0x111)]
2041+
fun test_update_snapshot(chain: &signer, operator: &signer)
2042+
acquires ModuleStore {
2043+
let bridge_id = test_setup(
2044+
chain,
2045+
operator,
2046+
1,
2047+
@0x1111,
2048+
10000000000000000,
2049+
);
2050+
let release_time = 1000;
2051+
fund_reward_script(chain, 1, release_time, release_time);
2052+
submit_snapshot(chain, bridge_id, 1, x"8888888888888888888888888888888888888888888888888888888888888888", 0);
2053+
let snapshot = get_snapshot(bridge_id, 1);
2054+
assert!(snapshot.merkle_root == x"8888888888888888888888888888888888888888888888888888888888888888", 0);
2055+
assert!(snapshot.total_l2_score == 0, 0);
2056+
2057+
update_snapshot(chain, bridge_id, 1, x"7777777777777777777777777777777777777777777777777777777777777777", 100);
2058+
let snapshot = get_snapshot(bridge_id, 1);
2059+
assert!(snapshot.merkle_root == x"7777777777777777777777777777777777777777777777777777777777777777", 100);
2060+
assert!(snapshot.total_l2_score == 100, 0);
2061+
}
2062+
2063+
#[test(chain=@0x1, operator=@0x111)]
2064+
#[expected_failure(abort_code = 0xD0014, location = Self)]
2065+
fun failed_update_snapshot(chain: &signer, operator: &signer)
2066+
acquires ModuleStore {
2067+
let bridge_id = test_setup(
2068+
chain,
2069+
operator,
2070+
1,
2071+
@0x1111,
2072+
10000000000000000,
2073+
);
2074+
let release_time = 0;
2075+
fund_reward_script(chain, 1, release_time, release_time);
2076+
submit_snapshot(chain, bridge_id, 1, x"8888888888888888888888888888888888888888888888888888888888888888", 0);
2077+
update_snapshot(chain, bridge_id, 1, x"7777777777777777777777777777777777777777777777777777777777777777", 100);
2078+
}
2079+
2080+
19962081
#[test(chain=@0x1, operator=@0x111, operator2=@0x222)]
19972082
fun test_get_next_stage(chain: &signer, operator: &signer, operator2: &signer)
19982083
acquires ModuleStore, TestCapability {

precompile/modules/minitia_stdlib/sources/vip/score.move

+53-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ module minitia_std::vip_score {
1515

1616
struct Scores has store {
1717
total_score: u64,
18+
is_finalized: bool,
1819
score: table::Table<address /* user */, u64>,
1920
}
2021

@@ -43,6 +44,9 @@ module minitia_std::vip_score {
4344
/// The score is invalid.
4445
const EINVALID_SCORE: u64 = 7;
4546

47+
/// The stage is already finalized.
48+
const EFINALIED_STAGE: u64 = 8;
49+
4650
//
4751
// Events
4852
//
@@ -126,6 +130,7 @@ module minitia_std::vip_score {
126130
if (!table::contains(&module_store.scores, stage)) {
127131
table::add(&mut module_store.scores, stage, Scores {
128132
total_score: 0,
133+
is_finalized: false,
129134
score: table::new<address, u64>()
130135
});
131136
};
@@ -144,7 +149,10 @@ module minitia_std::vip_score {
144149
assert!(table::contains(&module_store.scores, stage), error::invalid_argument(EINVALID_STAGE));
145150

146151
let scores = table::borrow_mut(&mut module_store.scores, stage);
152+
assert!(!scores.is_finalized, error::invalid_argument(EFINALIED_STAGE));
153+
147154
let score = table::borrow_mut_with_default(&mut scores.score, addr, 0);
155+
148156
*score = *score + amount;
149157
scores.total_score = scores.total_score + amount;
150158

@@ -171,6 +179,8 @@ module minitia_std::vip_score {
171179
assert!(table::contains(&module_store.scores, stage), error::invalid_argument(EINVALID_STAGE));
172180

173181
let scores = table::borrow_mut(&mut module_store.scores, stage);
182+
assert!(!scores.is_finalized, error::invalid_argument(EFINALIED_STAGE));
183+
174184
let score = table::borrow_mut(&mut scores.score, addr);
175185
assert!(*score >= amount, error::invalid_argument(EINSUFFICIENT_SCORE));
176186
*score = *score - amount;
@@ -193,12 +203,14 @@ module minitia_std::vip_score {
193203
amount: u64
194204
) acquires ModuleStore {
195205
check_deployer_permission(deployer);
196-
assert!(amount > 0, error::invalid_argument(EINVALID_SCORE));
206+
assert!(amount >= 0, error::invalid_argument(EINVALID_SCORE));
197207

198208
let module_store = borrow_global_mut<ModuleStore>(@minitia_std);
199209
assert!(table::contains(&module_store.scores, stage), error::invalid_argument(EINVALID_STAGE));
200210

201211
let scores = table::borrow_mut(&mut module_store.scores, stage);
212+
assert!(!scores.is_finalized, error::invalid_argument(EFINALIED_STAGE));
213+
202214
let score = table::borrow_mut_with_default(&mut scores.score, addr, 0);
203215

204216
if (*score > amount) {
@@ -222,6 +234,18 @@ module minitia_std::vip_score {
222234
//
223235
// Entry functions
224236
//
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+
}
225249

226250
public entry fun update_score_script(
227251
deployer: &signer,
@@ -339,6 +363,34 @@ module minitia_std::vip_score {
339363
init_module_for_test(chain);
340364
remove_deployer_script(chain, signer::address_of(deployer));
341365
}
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+
342394

343395
#[test(chain = @0x1, deployer_a = @0x2, deployer_b = @0x3, user_a = @0x123, user_b = @0x456)]
344396
fun test_e2e(chain: &signer, deployer_a: &signer, deployer_b: &signer, user_a: address, user_b: address) acquires ModuleStore {

0 commit comments

Comments
 (0)