Skip to content

Commit

Permalink
[tower] patch tower counter add tests (0LNetworkCommunity#81)
Browse files Browse the repository at this point in the history
Co-authored-by: 0o-de-lally <[email protected]>
  • Loading branch information
2 people authored and hemulin committed Nov 3, 2023
1 parent ff2b665 commit 6d48adc
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 23 deletions.
71 changes: 49 additions & 22 deletions framework/libra-framework/sources/ol_sources/oracle.move
Original file line number Diff line number Diff line change
Expand Up @@ -184,55 +184,50 @@ module ol_framework::oracle {

}

fun increment_stats(provider_addr: address, tower: &mut Tower, time: u64, signature_bytes: vector<u8>,) acquires GlobalCounter, ProviderList {
fun increment_stats(provider_addr: address, tower: &mut Tower, time: u64, signature_bytes: vector<u8>) acquires GlobalCounter, ProviderList {

// update the global state
let global = borrow_global_mut<GlobalCounter>(@ol_framework);
// is this a proof in a new epoch?
let current_epoch = epoch_helper::get_current_epoch();

// if this is the first proof this epoch;
if (current_epoch > tower.latest_epoch_mining ) {

if (current_epoch > tower.latest_epoch_mining) { // 370 , 10 = true
// we lazily reset this counter
global.proofs_in_epoch = 0;
tower.count_proofs_in_epoch = 0;
// and if this first proof is exaclty in a contiguous epoch
// it qualifies as a streak
if (tower.latest_epoch_mining + 1 == current_epoch) {
if (tower.latest_epoch_mining + 1 == current_epoch) { // 11, 370, false
tower.contiguous_epochs_mining = tower.contiguous_epochs_mining + 1;
} else if (tower.latest_epoch_mining + 1 < current_epoch) { // reset it
// 11, 370, true
tower.contiguous_epochs_mining = 0;
}
};

global.lifetime_proofs = global.lifetime_proofs + 1;
global.proofs_in_epoch = global.proofs_in_epoch + 1;


// exit early if above threshold
assert!(
tower.count_proofs_in_epoch < globals::get_epoch_mining_thres_upper(),
error::invalid_state(EABOVE_SUBMISSION_THRESH)
);
// update providers state
tower.last_commit_timestamp = time;
tower.previous_proof_hash = signature_bytes;
tower.verified_tower_height = tower.verified_tower_height + 1;
tower.count_proofs_in_epoch = tower.count_proofs_in_epoch + 1;
tower.epochs_mining = tower.epochs_mining + 1;
tower.latest_epoch_mining = epoch_helper::get_current_epoch();

assert!(
tower.count_proofs_in_epoch < globals::get_epoch_mining_thres_upper(),
error::invalid_state(EABOVE_SUBMISSION_THRESH)
);

// update globals
global.lifetime_proofs = global.lifetime_proofs + 1;
global.proofs_in_epoch = global.proofs_in_epoch + 1;
// also check if the tower is now above the threshold
if (tower.count_proofs_in_epoch > globals::get_epoch_mining_thres_lower()) {
// print(&333001);
if (tower.count_proofs_in_epoch > globals::get_epoch_mining_thres_lower()) {
global.proofs_in_epoch_above_thresh = global.proofs_in_epoch_above_thresh + 1;
// also add to the provider list which would be elegible for rewards
let provider_list = borrow_global_mut<ProviderList>(@ol_framework);
vector::push_back(&mut provider_list.current_above_threshold, provider_addr);
// print(provider_list);
};


tower.epochs_mining = tower.epochs_mining + 1;
tower.latest_epoch_mining = epoch_helper::get_current_epoch();

}

// while transitioning to oracle, allow vdf proofs from miners.
Expand Down Expand Up @@ -325,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 6d48adc

Please sign in to comment.