Skip to content

Commit

Permalink
add test case for lazy oracle counter reset
Browse files Browse the repository at this point in the history
  • Loading branch information
0o-de-lally committed Nov 3, 2023
1 parent a249421 commit cdc0a37
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 1 deletion.
32 changes: 32 additions & 0 deletions framework/libra-framework/sources/ol_sources/oracle.move
Original file line number Diff line number Diff line change
Expand Up @@ -320,4 +320,36 @@ module ol_framework::oracle {
},
);
}

//////// GETTERS ////////

#[view]
/// returns the number of proofs for a miner in the current epoch
public fun get_count_in_epoch(miner_addr: address): u64 acquires Tower {
if (exists<Tower>(miner_addr)) {
let s = borrow_global<Tower>(miner_addr);
if (s.latest_epoch_mining == epoch_helper::get_current_epoch()) {
return s.count_proofs_in_epoch
};
};
0
}

//////// TEST HELPERS ////////
#[test_only]
public fun set_tower(root: &signer, addr: address, count_proofs_in_epoch:
u64, latest_epoch_mining: u64) acquires Tower {
system_addresses::assert_ol(root);
let state = borrow_global_mut<Tower>(addr);
state.count_proofs_in_epoch = count_proofs_in_epoch;
state.latest_epoch_mining = latest_epoch_mining
}

#[test_only]
/// returns the number of proofs for a miner in the current epoch
public fun get_exact_count(miner_addr: address): u64 acquires Tower {
let s = borrow_global<Tower>(miner_addr);
return s.count_proofs_in_epoch
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ module ol_framework::test_tower {
use ol_framework::stake;
use diem_framework::timestamp;
use std::vector;
use ol_framework::oracle;

// use std::debug::print;

Expand Down Expand Up @@ -120,4 +121,45 @@ module ol_framework::test_tower {
assert!(alice_bal_pre < alice_bal, 73570003);
}

}
#[test(root = @ol_framework, cousin_alice = @0x87515d94a244235a1433d7117bc0cb154c613c2f4b1e67ca8d98a542ee3f59f5)]
fun counters_lazy_reset(root: signer, cousin_alice: signer) {
let a_addr = signer::address_of(&cousin_alice);
let _vals = mock::genesis_n_vals(&root, 1);

ol_account::create_account(&root, a_addr);

tower_state::minerstate_commit(
&cousin_alice,
vdf_fixtures::alice_0_easy_chal(),
vdf_fixtures::alice_0_easy_sol(),
vdf_fixtures::easy_difficulty(),
vdf_fixtures::security(),
);

let count_pre = oracle::get_exact_count(a_addr);
assert!(count_pre==1, 7347001);

oracle::set_tower(&root, a_addr, 73, 0);

let count_more = oracle::get_exact_count(a_addr);
assert!(count_more==73, 7347002);

mock::trigger_epoch(&root);

let count_pre_reset = oracle::get_exact_count(a_addr);
assert!(count_pre_reset==73, 7347003);

tower_state::minerstate_commit(
&cousin_alice,
vdf_fixtures::alice_1_easy_chal(),
vdf_fixtures::alice_1_easy_sol(),
vdf_fixtures::easy_difficulty(),
vdf_fixtures::security(),
);

let count_post_reset = oracle::get_exact_count(a_addr);
assert!(count_post_reset==1, 7347004);

}

}

0 comments on commit cdc0a37

Please sign in to comment.