diff --git a/framework/libra-framework/sources/ol_sources/oracle.move b/framework/libra-framework/sources/ol_sources/oracle.move index 42fde1668..0d715c2d1 100644 --- a/framework/libra-framework/sources/ol_sources/oracle.move +++ b/framework/libra-framework/sources/ol_sources/oracle.move @@ -184,7 +184,7 @@ module ol_framework::oracle { } - fun increment_stats(provider_addr: address, tower: &mut Tower, time: u64, signature_bytes: vector,) acquires GlobalCounter, ProviderList { + fun increment_stats(provider_addr: address, tower: &mut Tower, time: u64, signature_bytes: vector) acquires GlobalCounter, ProviderList { // update the global state let global = borrow_global_mut(@ol_framework); @@ -192,47 +192,42 @@ module ol_framework::oracle { 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(@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. @@ -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(miner_addr)) { + let s = borrow_global(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(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(miner_addr); + return s.count_proofs_in_epoch + } + } \ No newline at end of file diff --git a/framework/libra-framework/sources/ol_sources/tests/tower.test.move b/framework/libra-framework/sources/ol_sources/tests/tower.test.move index 51b28c746..b1dd83a24 100644 --- a/framework/libra-framework/sources/ol_sources/tests/tower.test.move +++ b/framework/libra-framework/sources/ol_sources/tests/tower.test.move @@ -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; @@ -120,4 +121,45 @@ module ol_framework::test_tower { assert!(alice_bal_pre < alice_bal, 73570003); } -} \ No newline at end of file + #[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); + + } + +}