Skip to content

Commit

Permalink
more tests for fill_seats, and thermostat. patches
Browse files Browse the repository at this point in the history
  • Loading branch information
0o-de-lally committed Feb 23, 2023
1 parent 26ea59e commit 4e53184
Show file tree
Hide file tree
Showing 8 changed files with 219 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,40 +77,40 @@ address DiemFramework {
public fun is_family(left: address, right: address): (bool, address) acquires Ancestry {
let is_family = false;
let common_ancestor = @0x0;
print(&100300);
// print(&100300);
print(&exists<Ancestry>(left));
print(&exists<Ancestry>(right));

// if (exists<Ancestry>(left) && exists<Ancestry>(right)) {
// if tree is empty it will still work.
print(&100310);
// print(&100310);
let left_tree = get_tree(left);
print(&100311);
// print(&100311);
let right_tree = get_tree(right);

print(&100320);
// print(&100320);

// check for direct relationship.
if (Vector::contains(&left_tree, &right)) return (true, right);
if (Vector::contains(&right_tree, &left)) return (true, left);

print(&100330);
// print(&100330);
let i = 0;
// check every address on the list if there are overlaps.
while (i < Vector::length<address>(&left_tree)) {
print(&100341);
// print(&100341);
let family_addr = Vector::borrow(&left_tree, i);
if (Vector::contains(&right_tree, family_addr)) {
is_family = true;
common_ancestor = *family_addr;
print(&100342);
// print(&100342);
break
};
i = i + 1;
};
print(&100350);
// print(&100350);
// };
print(&100360);
// print(&100360);
(is_family, common_ancestor)
}

Expand Down
22 changes: 11 additions & 11 deletions diem-move/diem-framework/DPN/sources/0L/Ancestry.move
Original file line number Diff line number Diff line change
Expand Up @@ -77,40 +77,40 @@ address DiemFramework {
public fun is_family(left: address, right: address): (bool, address) acquires Ancestry {
let is_family = false;
let common_ancestor = @0x0;
print(&100300);
print(&exists<Ancestry>(left));
print(&exists<Ancestry>(right));
// print(&100300);
// print(&exists<Ancestry>(left));
// print(&exists<Ancestry>(right));

// if (exists<Ancestry>(left) && exists<Ancestry>(right)) {
// if tree is empty it will still work.
print(&100310);
// print(&100310);
let left_tree = get_tree(left);
print(&100311);
// print(&100311);
let right_tree = get_tree(right);

print(&100320);
// print(&100320);

// check for direct relationship.
if (Vector::contains(&left_tree, &right)) return (true, right);
if (Vector::contains(&right_tree, &left)) return (true, left);

print(&100330);
// print(&100330);
let i = 0;
// check every address on the list if there are overlaps.
while (i < Vector::length<address>(&left_tree)) {
print(&100341);
// print(&100341);
let family_addr = Vector::borrow(&left_tree, i);
if (Vector::contains(&right_tree, family_addr)) {
is_family = true;
common_ancestor = *family_addr;
print(&100342);
// print(&100342);
break
};
i = i + 1;
};
print(&100350);
// print(&100350);
// };
print(&100360);
// print(&100360);
(is_family, common_ancestor)
}

Expand Down
4 changes: 2 additions & 2 deletions diem-move/diem-framework/DPN/sources/0L/EpochBoundary.move
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ module EpochBoundary {
// Subsidy::calculate_subsidy(vm, compliant_nodes_count);
print(&800400);

let reward = ProofOfFee::get_consensus_reward();
let (reward, _, _) = ProofOfFee::get_consensus_reward();
process_fullnodes(vm, reward);

print(&800500);
Expand Down Expand Up @@ -245,7 +245,7 @@ module EpochBoundary {
print(&800900106);

// trigger the thermostat if the reward needs to be adjusted
ProofOfFee::reward_thermostat(vm);
ProofOfFee::reward_thermostat(vm, &proposed_set);
print(&800900107);
// Reconfig should be the last event.
// Reconfigure the network
Expand Down
109 changes: 79 additions & 30 deletions diem-move/diem-framework/DPN/sources/0L/ProofOfFee.move
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ address DiemFramework {
use DiemFramework::DiemAccount;
use DiemFramework::Debug::print;
use DiemFramework::Vouch;
use Std::FixedPoint32;

const ENOT_AN_ACTIVE_VALIDATOR: u64 = 190001;
const EBID_ABOVE_MAX_PCT: u64 = 190002;
Expand All @@ -36,8 +37,8 @@ address DiemFramework {
struct ConsensusReward has key {
value: u64,
clearing_price: u64,
average_winning_bid: u64,
avg_bid_history: vector<u64>,
median_win_bid: u64,
median_history: vector<u64>,
}
public fun init_genesis_baseline_reward(vm: &signer) {
if (Signer::address_of(vm) != @VMReserved) return;
Expand All @@ -48,8 +49,8 @@ address DiemFramework {
ConsensusReward {
value: GENESIS_BASELINE_REWARD,
clearing_price: 0,
average_winning_bid: 0,
avg_bid_history: Vector::empty<u64>(),
median_win_bid: 0,
median_history: Vector::empty<u64>(),
}
);
}
Expand Down Expand Up @@ -102,40 +103,62 @@ address DiemFramework {
public fun get_sorted_vals(): vector<address> acquires ProofOfFeeAuction {
let eligible_validators = ValidatorUniverse::get_eligible_validators();
let length = Vector::length<address>(&eligible_validators);
print(&length);
// Vector to store each address's node_weight
let weights = Vector::empty<u64>();
let filtered_vals = Vector::empty<address>();
let k = 0;
while (k < length) {
// TODO: Ensure that this address is an active validator

let cur_address = *Vector::borrow<address>(&eligible_validators, k);
// Ensure that this address is an active validator
let (bid, _) = current_bid(cur_address);
let (bid, expire) = current_bid(cur_address);
print(&bid);
print(&expire);
if (DiemConfig::get_current_epoch() > expire) {
k = k + 1;
continue
};
Vector::push_back<u64>(&mut weights, bid);
Vector::push_back<address>(&mut filtered_vals, cur_address);
k = k + 1;
};

print(&weights);

// Sorting the accounts vector based on value (weights).
// Bubble sort algorithm
let len_filtered = Vector::length<address>(&filtered_vals);
print(&len_filtered);
print(&Vector::length(&weights));
if (len_filtered < 2) return filtered_vals;
let i = 0;
while (i < length){
while (i < len_filtered){
let j = 0;
while(j < length-i-1){
while(j < len_filtered-i-1){
print(&8888801);

let value_j = *(Vector::borrow<u64>(&weights, j));
print(&8888802);
let value_jp1 = *(Vector::borrow<u64>(&weights, j+1));
if(value_j > value_jp1){
print(&8888803);
Vector::swap<u64>(&mut weights, j, j+1);
Vector::swap<address>(&mut eligible_validators, j, j+1);
print(&8888804);
Vector::swap<address>(&mut filtered_vals, j, j+1);
};
j = j + 1;
print(&8888805);
};
i = i + 1;
print(&8888806);
};

print(&filtered_vals);
// Reverse to have sorted order - high to low.
Vector::reverse<address>(&mut eligible_validators);
Vector::reverse<address>(&mut filtered_vals);

return eligible_validators
return filtered_vals
}

// here we place the bidders into their seats
Expand All @@ -156,7 +179,7 @@ address DiemFramework {
public fun fill_seats_and_get_price(vm: &signer, set_size: u64, proven_nodes: &vector<address>): (vector<address>, u64) acquires ProofOfFeeAuction, ConsensusReward {
if (Signer::address_of(vm) != @VMReserved) return (Vector::empty<address>(), 0);

let baseline_reward = get_consensus_reward();
let (baseline_reward, _, _) = get_consensus_reward();

let seats_to_fill = Vector::empty<address>();
// print(&set_size);
Expand All @@ -167,6 +190,7 @@ address DiemFramework {

print(&8006010202);
let sorted_vals_by_bid = get_sorted_vals();
print(&sorted_vals_by_bid);

let i = 0u64;
while (
Expand All @@ -176,6 +200,9 @@ address DiemFramework {
// print(&i);
let val = Vector::borrow(&sorted_vals_by_bid, i);
let (bid, expire) = current_bid(*val);
print(val);
print(&bid);
print(&expire);
// fail fast if the validator is jailed.
// NOTE: epoch reconfigure needs to reset the jail
// before calling the proof of fee.
Expand All @@ -198,19 +225,27 @@ address DiemFramework {
// or if the bid expired.

// belt and suspenders, expiry
print(&DiemConfig::get_current_epoch());
if (DiemConfig::get_current_epoch() > expire) {
i = i + 1;
continue
};

let coin_required = bid * baseline_reward;
print(&80060102042);
let unlocked_coins = DiemAccount::unlocked_amount(*val);
print(&unlocked_coins);
let coin_required = FixedPoint32::multiply_u64(baseline_reward, FixedPoint32::create_from_rational(bid, 1000));
// let coin_required = bid * baseline_reward;

print(&coin_required);
if (
DiemAccount::unlocked_amount(*val) < coin_required
unlocked_coins < coin_required
) {
i = i + 1;
continue
};

print(&80060102043);

// check if a proven node
if (Vector::contains(proven_nodes, val)) {
Expand All @@ -235,23 +270,33 @@ address DiemFramework {
print(&8006010208);
print(&seats_to_fill);



// get the median and set history
set_history(vm, &seats_to_fill);

if (Vector::is_empty(&seats_to_fill)) {
return (seats_to_fill, 0)
};
// if (Vector::is_empty(&seats_to_fill)) {
// return (seats_to_fill, 0)
// };

// Find the clearing price which all validators will pay
let lowest_bidder = Vector::borrow(&seats_to_fill, Vector::length(&seats_to_fill) - 1);

let (lowest_bid, _) = current_bid(*lowest_bidder);
return (seats_to_fill, lowest_bid)
let (lowest_bid_pct, _) = current_bid(*lowest_bidder);
print(&99999999999999);
print(&lowest_bid_pct);

// update the clearing price
let cr = borrow_global_mut<ConsensusReward>(@VMReserved);
cr.clearing_price = lowest_bid_pct;

return (seats_to_fill, lowest_bid_pct)
}

// Adjust the reward at the end of the epoch
// as described in the paper, the epoch reward needs to be adjustable
// given that the implicit bond needs to be sufficient, eg 5-10x the reward.
public fun reward_thermostat(vm: &signer) acquires ConsensusReward {
public fun reward_thermostat(vm: &signer, _vals: &vector<address>) acquires ConsensusReward {
if (Signer::address_of(vm) != @VMReserved) {
return
};
Expand All @@ -270,14 +315,14 @@ address DiemFramework {
let cr = borrow_global_mut<ConsensusReward>(@VMReserved);

print(&8006010551);
let len = Vector::length<u64>(&cr.avg_bid_history);
let len = Vector::length<u64>(&cr.median_history);
let i = 0;

let epochs_above = 0;
let epochs_below = 0;
while (i < 10 && i < len) { // max ten days, but may have less in history, filling set should truncate the history at 10 epochs.
print(&8006010552);
let avg_bid = *Vector::borrow<u64>(&cr.avg_bid_history, i);
let avg_bid = *Vector::borrow<u64>(&cr.median_history, i);
print(&8006010553);
if (avg_bid > bid_upper_bound) {
epochs_above = epochs_above + 1;
Expand Down Expand Up @@ -357,14 +402,18 @@ address DiemFramework {
return
};

print(&99901);
let median_bid = get_median(seats_to_fill);
// push to history
let cr = borrow_global_mut<ConsensusReward>(@VMReserved);
if (Vector::length(&cr.avg_bid_history) < 10) {
Vector::push_back(&mut cr.avg_bid_history, median_bid);
cr.median_win_bid = median_bid;
if (Vector::length(&cr.median_history) < 10) {
print(&99902);
Vector::push_back(&mut cr.median_history, median_bid);
} else {
Vector::remove(&mut cr.avg_bid_history, 0);
Vector::push_back(&mut cr.avg_bid_history, median_bid);
print(&99903);
Vector::remove(&mut cr.median_history, 0);
Vector::push_back(&mut cr.median_history, median_bid);
};
}

Expand All @@ -389,9 +438,9 @@ address DiemFramework {


// get the baseline reward from ConsensusReward
public fun get_consensus_reward(): u64 acquires ConsensusReward {
public fun get_consensus_reward(): (u64, u64, u64) acquires ConsensusReward {
let b = borrow_global<ConsensusReward>(@VMReserved );
return b.value
return (b.value, b.clearing_price, b.median_win_bid)
}

// CONSENSUS CRITICAL
Expand All @@ -418,8 +467,8 @@ address DiemFramework {
// validator can set a bid. See transaction script below.
// the validator can set an "expiry epoch: for the bid.
// Zero means never expires.
// Bids are denomiated in percentages, with two decimal places..
// i.e. 1234 = 12.34%
// Bids are denomiated in percentages, with ONE decimal place..
// i.e. 0123 = 12.3%
// Provisionally 110% is the maximum bid. Which could be reviewed.
public fun set_bid(account_sig: &signer, bid: u64, expiry_epoch: u64) acquires ProofOfFeeAuction {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
processed 2 tasks
Loading

0 comments on commit 4e53184

Please sign in to comment.