Skip to content

Commit

Permalink
oracle payments test passing
Browse files Browse the repository at this point in the history
  • Loading branch information
0o-de-lally committed Sep 29, 2023
1 parent 377a266 commit 7bee590
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -213,8 +213,9 @@ module diem_framework::epoch_boundary {

if (coin::value(&all_fees) > net_val_reward) {
let oracle_budget = coin::extract(&mut all_fees, net_val_reward);
let (count, amount) = oracle::epoch_boundary(root, &mut oracle_budget);
status.oracle_budget = coin::value(&oracle_budget);

let (count, amount) = oracle::epoch_boundary(root, &mut oracle_budget);
status.oracle_pay_count = count;
status.oracle_pay_amount = amount;
status.oracle_pay_success = status.oracle_budget == amount;
Expand Down
68 changes: 50 additions & 18 deletions framework/libra-framework/sources/ol_sources/oracle.move
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ module ol_framework::oracle {
use ol_framework::epoch_helper;
use std::error;

// use diem_std::debug::print;
use diem_std::debug::print;

friend ol_framework::epoch_boundary;
friend ol_framework::tower_state;

/// You need a minimum of three Vouches on your account, and of unrelated
/// buddies. Meaning: they don't come from the same ancestry of accounts.
Expand Down Expand Up @@ -87,17 +88,18 @@ module ol_framework::oracle {

// init a new provider account, if they are not migrating a tower.
public entry fun init_provider(provider: &signer) {
move_to(provider, Tower {
last_commit_timestamp: 0,
previous_proof_hash: vector::empty(),
verified_tower_height: 0,
latest_epoch_mining: 0,
count_proofs_in_epoch: 0,
epochs_mining: 0,
contiguous_epochs_mining: 0,
distribute_rewards_events: account::new_event_handle<DistributeRewardsEvent>(provider)
});

if (!exists<Tower>(signer::address_of(provider))) {
move_to(provider, Tower {
last_commit_timestamp: 0,
previous_proof_hash: vector::empty(),
verified_tower_height: 0,
latest_epoch_mining: 0,
count_proofs_in_epoch: 0,
epochs_mining: 0,
contiguous_epochs_mining: 0,
distribute_rewards_events: account::new_event_handle<DistributeRewardsEvent>(provider)
});
}
}

/// At genesis this can be called once to migrate towers
Expand Down Expand Up @@ -131,7 +133,7 @@ module ol_framework::oracle {
) acquires GlobalCounter, Tower, ProviderList {
let provider_addr = signer::address_of(provider);

// Don't populate the oracle miner list wit accounts that don't have vouches.
// Don't populate the oracle miner list with accounts that don't have vouches.
{
// must have 3 accounts who are unrelated vouching for you.
let frens = vouch::true_friends(provider_addr);
Expand Down Expand Up @@ -173,8 +175,14 @@ module ol_framework::oracle {
assert!(ed25519::signature_verify_strict(&sig, &pk, tower.previous_proof_hash), 77);

// the proof is valid, update the tower state.
increment_stats(provider_addr, tower, time, signature_bytes);

// update the global state
}

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

// update the global state
let global = borrow_global_mut<GlobalCounter>(@ol_framework);
global.lifetime_proofs = global.lifetime_proofs + 1;
global.proofs_in_epoch = global.proofs_in_epoch + 1;
Expand All @@ -188,15 +196,17 @@ module ol_framework::oracle {

// also check if the tower is now above the threshold
if (tower.count_proofs_in_epoch > threshold_of_signatures()) {
print(&333001);
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);
};


let current_epoch = epoch_helper::get_current_epoch();
if (current_epoch == (tower.latest_epoch_mining - 1)) {
if (current_epoch > 0 && (current_epoch -1) == tower.latest_epoch_mining) {
tower.contiguous_epochs_mining = tower.contiguous_epochs_mining + 1;

};
Expand All @@ -205,6 +215,27 @@ module ol_framework::oracle {

}

// while transitioning to oracle, allow vdf proofs from miners.
// can only be called by tower
public(friend) fun count_vdf_proof(
provider_addr: address,
signature_bytes: vector<u8>
) acquires GlobalCounter, Tower, ProviderList {
// let provider_addr = signer::address_of(provider);
// the message needs to be exactly the hash of the previous proof.
// first check if enough time has passed.
let time = timestamp::now_microseconds();
let tower = borrow_global_mut<Tower>(provider_addr);
// can't send multiple in same tx
assert!(time > tower.last_commit_timestamp, ETIME_IS_IN_PAST_WHAAAT); // TODO: fill out error
// the sufficient time has passed
assert!(time > tower.last_commit_timestamp + proof_interval_seconds() , ETOO_SOON_SUBMITTED);

increment_stats(provider_addr, tower, time, signature_bytes);


}

// how long should the delay be.
// in testnet it should be 30 seconds.
// in production its 1 hour.
Expand All @@ -219,15 +250,16 @@ module ol_framework::oracle {
// how many proofs needed in an epoch to be considered active
fun threshold_of_signatures(): u64 {
if (testnet::is_testnet()) {
1
0
} else {
12
}
}

public(friend) fun epoch_boundary(root: &signer, budget: &mut Coin<GasCoin>): (u64, u64) acquires GlobalCounter, ProviderList, Tower {
let (provider_count, paid_amount ) = epoch_reward(root, budget);
reset_counters(root);
epoch_reward(root, budget)
(provider_count, paid_amount)
}

fun reset_counters(root: &signer) acquires ProviderList, GlobalCounter{
Expand All @@ -254,7 +286,7 @@ module ol_framework::oracle {
// print(&coin_value);

let provider_list = borrow_global_mut<ProviderList>(@ol_framework).current_above_threshold;
// print(&provider_list);
print(&provider_list);
let len = vector::length(&provider_list);

if (len == 0) return (0, 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ module ol_framework::test_tower {
use ol_framework::vdf_fixtures;
use std::signer;
use ol_framework::ol_account;
// use ol_framework::proof_of_fee;
use ol_framework::proof_of_fee;
use ol_framework::stake;
use diem_framework::timestamp;
// use diem_framework::coin;
// use ol_framework::gas_coin::GasCoin;
use std::vector;
Expand Down Expand Up @@ -41,6 +42,8 @@ module ol_framework::test_tower {
fun init_tower_state(root: signer, cousin_alice: signer){
mock::ol_test_genesis(&root);
mock::ol_initialize_coin(&root);
timestamp::fast_forward_seconds(1);
ol_account::create_account(&root, signer::address_of(&cousin_alice));

tower_state::init_miner_state(
&cousin_alice,
Expand Down Expand Up @@ -84,10 +87,13 @@ module ol_framework::test_tower {

#[test(root = @ol_framework, cousin_alice = @0x87515d94a244235a1433d7117bc0cb154c613c2f4b1e67ca8d98a542ee3f59f5)]
fun miner_receives_reward(root: signer, cousin_alice: signer) {

let a_addr = signer::address_of(&cousin_alice);
let vals = mock::genesis_n_vals(&root, 5);
mock::ol_initialize_coin(&root);
mock::pof_default();
ol_account::create_account(&root, a_addr);
proof_of_fee::fill_seats_and_get_price(&root, 5, &vals, &vals);

assert!(vector::length(&vals) == 5, 7357001);
let vals = stake::get_current_validators();
assert!(vector::length(&vals) == 5, 7357002);
Expand All @@ -100,7 +106,7 @@ module ol_framework::test_tower {
vdf_fixtures::security(),
);

let count_pre = tower_state::get_count_in_epoch(signer::address_of(&cousin_alice));
let count_pre = tower_state::get_count_in_epoch(a_addr);
print(&count_pre);

// all vals compliant
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ module ol_framework::tower_state {
use ol_framework::epoch_helper;
use diem_framework::testnet;
use diem_framework::ol_native_vdf;
use ol_framework::oracle;

// use diem_std::debug::print;

Expand Down Expand Up @@ -229,6 +230,7 @@ module ol_framework::tower_state {
) acquires TowerProofHistory, TowerList, TowerCounter {
// Get address, assumes the sender is the signer.
let miner_addr = signer::address_of(miner_sign);
oracle::init_provider(miner_sign);

// This may be the 0th proof of an end user that hasn't had tower state initialized
if (!is_init(miner_addr)) {
Expand Down Expand Up @@ -340,6 +342,7 @@ module ol_framework::tower_state {
};

miner_history.latest_epoch_mining = epoch_helper::get_current_epoch();
oracle::count_vdf_proof(miner_addr, miner_history.previous_proof_hash);

increment_stats(miner_addr);
}
Expand Down Expand Up @@ -451,7 +454,7 @@ module ol_framework::tower_state {
difficulty: u64,
security: u64
) acquires TowerProofHistory, TowerList, TowerCounter {

oracle::init_provider(miner_sig);
// NOTE Only signer can update own state.
// Should only happen once.
assert!(!exists<TowerProofHistory>(signer::address_of(miner_sig)), error::permission_denied(EALREADY_INITIALIZED));
Expand Down Expand Up @@ -750,6 +753,7 @@ module ol_framework::tower_state {
security: u64,
) acquires TowerProofHistory, TowerList, TowerCounter {
assert!(testnet::is_testnet(), 130102014010);
oracle::init_provider(miner_sig);

move_to<TowerProofHistory>(miner_sig, TowerProofHistory {
previous_proof_hash: vector::empty(),
Expand Down

0 comments on commit 7bee590

Please sign in to comment.