Skip to content

Commit

Permalink
add unit tests for thermostat. Patch a couple bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
0o-de-lally committed Feb 23, 2023
1 parent 68e566e commit 03f25e8
Show file tree
Hide file tree
Showing 23 changed files with 290 additions and 97 deletions.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,7 @@
//// V6 ////
// CONSENSUS CRITICAL
// pick the validators based on proof of fee.
// <b>false</b> because we want the default behavior of the function: filtered by audit
<b>let</b> sorted_bids = <a href="ProofOfFee.md#0x1_ProofOfFee_get_sorted_vals">ProofOfFee::get_sorted_vals</a>(<b>false</b>);
<b>let</b> (auction_winners, price) = <a href="ProofOfFee.md#0x1_ProofOfFee_fill_seats_and_get_price">ProofOfFee::fill_seats_and_get_price</a>(vm, <a href="EpochBoundary.md#0x1_EpochBoundary_MOCK_VAL_SIZE">MOCK_VAL_SIZE</a>, &sorted_bids, outgoing_compliant_set);
// TODO: Don't <b>use</b> <b>copy</b> above, do a borrow.
Expand Down Expand Up @@ -368,7 +369,7 @@
print(&800900106);

// trigger the thermostat <b>if</b> the reward needs <b>to</b> be adjusted
<a href="ProofOfFee.md#0x1_ProofOfFee_reward_thermostat">ProofOfFee::reward_thermostat</a>(vm, &proposed_set);
<a href="ProofOfFee.md#0x1_ProofOfFee_reward_thermostat">ProofOfFee::reward_thermostat</a>(vm);
print(&800900107);
// Reconfig should be the last event.
// Reconfigure the network
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
- [Function `init_bidding`](#0x1_ProofOfFee_init_bidding)
- [Function `update_pof_bid`](#0x1_ProofOfFee_update_pof_bid)
- [Function `test_set_val_bids`](#0x1_ProofOfFee_test_set_val_bids)
- [Function `test_mock_reward`](#0x1_ProofOfFee_test_mock_reward)


<pre><code><b>use</b> <a href="Debug.md#0x1_Debug">0x1::Debug</a>;
Expand Down Expand Up @@ -486,7 +487,7 @@



<pre><code><b>public</b> <b>fun</b> <a href="ProofOfFee.md#0x1_ProofOfFee_reward_thermostat">reward_thermostat</a>(vm: &signer, _vals: &vector&lt;<b>address</b>&gt;)
<pre><code><b>public</b> <b>fun</b> <a href="ProofOfFee.md#0x1_ProofOfFee_reward_thermostat">reward_thermostat</a>(vm: &signer)
</code></pre>


Expand All @@ -495,7 +496,7 @@
<summary>Implementation</summary>


<pre><code><b>public</b> <b>fun</b> <a href="ProofOfFee.md#0x1_ProofOfFee_reward_thermostat">reward_thermostat</a>(vm: &signer, _vals: &vector&lt;<b>address</b>&gt;) <b>acquires</b> <a href="ProofOfFee.md#0x1_ProofOfFee_ConsensusReward">ConsensusReward</a> {
<pre><code><b>public</b> <b>fun</b> <a href="ProofOfFee.md#0x1_ProofOfFee_reward_thermostat">reward_thermostat</a>(vm: &signer) <b>acquires</b> <a href="ProofOfFee.md#0x1_ProofOfFee_ConsensusReward">ConsensusReward</a> {
<b>if</b> (<a href="../../../../../../../DPN/releases/artifacts/current/build/MoveStdlib/docs/Signer.md#0x1_Signer_address_of">Signer::address_of</a>(vm) != @VMReserved) {
<b>return</b>
};
Expand Down Expand Up @@ -891,4 +892,41 @@ find the median bid to push to history



</details>

<a name="0x1_ProofOfFee_test_mock_reward"></a>

## Function `test_mock_reward`



<pre><code><b>public</b> <b>fun</b> <a href="ProofOfFee.md#0x1_ProofOfFee_test_mock_reward">test_mock_reward</a>(vm: &signer, value: u64, clearing_price: u64, median_win_bid: u64, median_history: vector&lt;u64&gt;)
</code></pre>



<details>
<summary>Implementation</summary>


<pre><code><b>public</b> <b>fun</b> <a href="ProofOfFee.md#0x1_ProofOfFee_test_mock_reward">test_mock_reward</a>(
vm: &signer,
value: u64,
clearing_price: u64,
median_win_bid: u64,
median_history: vector&lt;u64&gt;,
) <b>acquires</b> <a href="ProofOfFee.md#0x1_ProofOfFee_ConsensusReward">ConsensusReward</a> {
<a href="Testnet.md#0x1_Testnet_assert_testnet">Testnet::assert_testnet</a>(vm);

<b>let</b> cr = <b>borrow_global_mut</b>&lt;<a href="ProofOfFee.md#0x1_ProofOfFee_ConsensusReward">ConsensusReward</a>&gt;(@VMReserved );
cr.value = value;
cr.clearing_price = clearing_price;
cr.median_win_bid = median_win_bid;
cr.median_history = median_history;

}
</code></pre>



</details>
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ module EpochBoundary {
//// V6 ////
// CONSENSUS CRITICAL
// pick the validators based on proof of fee.
// false because we want the default behavior of the function: filtered by audit
let sorted_bids = ProofOfFee::get_sorted_vals(false);
let (auction_winners, price) = ProofOfFee::fill_seats_and_get_price(vm, MOCK_VAL_SIZE, &sorted_bids, outgoing_compliant_set);
// TODO: Don't use copy above, do a borrow.
Expand Down Expand Up @@ -246,7 +247,7 @@ module EpochBoundary {
print(&800900106);

// trigger the thermostat if the reward needs to be adjusted
ProofOfFee::reward_thermostat(vm, &proposed_set);
ProofOfFee::reward_thermostat(vm);
print(&800900107);
// Reconfig should be the last event.
// Reconfigure the network
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ address DiemFramework {
// 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, _vals: &vector<address>) acquires ConsensusReward {
public fun reward_thermostat(vm: &signer) acquires ConsensusReward {
if (Signer::address_of(vm) != @VMReserved) {
return
};
Expand Down Expand Up @@ -549,6 +549,23 @@ address DiemFramework {
i = i + 1;
};
}

public fun test_mock_reward(
vm: &signer,
value: u64,
clearing_price: u64,
median_win_bid: u64,
median_history: vector<u64>,
) acquires ConsensusReward {
Testnet::assert_testnet(vm);

let cr = borrow_global_mut<ConsensusReward>(@VMReserved );
cr.value = value;
cr.clearing_price = clearing_price;
cr.median_win_bid = median_win_bid;
cr.median_history = median_history;

}
}
}

Expand Down
2 changes: 1 addition & 1 deletion diem-move/diem-framework/DPN/sources/0L/EpochBoundary.move
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ module EpochBoundary {
print(&800900106);

// trigger the thermostat if the reward needs to be adjusted
ProofOfFee::reward_thermostat(vm, &proposed_set);
ProofOfFee::reward_thermostat(vm);
print(&800900107);
// Reconfig should be the last event.
// Reconfigure the network
Expand Down
47 changes: 33 additions & 14 deletions diem-move/diem-framework/DPN/sources/0L/ProofOfFee.move
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ address DiemFramework {
// 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, _vals: &vector<address>) acquires ConsensusReward {
public fun reward_thermostat(vm: &signer) acquires ConsensusReward {
if (Signer::address_of(vm) != @VMReserved) {
return
};
Expand All @@ -336,7 +336,7 @@ address DiemFramework {

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.
while (i < 16 && i < len) { // max ten days, but may have less in history, filling set should truncate the history at 15 epochs.
print(&8006010552);
let avg_bid = *Vector::borrow<u64>(&cr.median_history, i);
print(&8006010553);
Expand Down Expand Up @@ -370,19 +370,21 @@ address DiemFramework {
// implicit bond is very high on validators. E.g.
// at 1% median bid, the implicit bond is 100x the reward.
// We need to DECREASE the reward
print(&8006010558);

if (epochs_above > short_window) {
print(&8006010558);
print(&epochs_above);
if (epochs_above > long_window) {
// decrease the reward by 10%
cr.value = cr.value - (cr.value / 10);
print(&8006010559);


cr.value = cr.value - (cr.value / 10);
return // return early since we can't increase and decrease simultaneously
} else if (epochs_above > long_window) {
} else if (epochs_above > short_window) {
// decrease the reward by 5%
print(&80060105510);

cr.value = cr.value - (cr.value / 20);


return // return early since we can't increase and decrease simultaneously
};

Expand All @@ -396,16 +398,16 @@ address DiemFramework {
// we need to INCREASE the reward, so that the bond is more meaningful.
print(&80060105511);

if (epochs_below > short_window) {
print(&80060105512);

// decrease the reward by 5%
cr.value = cr.value + (cr.value / 20);
} else if (epochs_above > long_window) {
if (epochs_above > long_window) {
print(&80060105513);

// decrease the reward by 10%
cr.value = cr.value + (cr.value / 10);
} else if (epochs_below > short_window) {
print(&80060105512);

// decrease the reward by 5%
cr.value = cr.value + (cr.value / 20);
};
};
};
Expand Down Expand Up @@ -549,6 +551,23 @@ address DiemFramework {
i = i + 1;
};
}

public fun test_mock_reward(
vm: &signer,
value: u64,
clearing_price: u64,
median_win_bid: u64,
median_history: vector<u64>,
) acquires ConsensusReward {
Testnet::assert_testnet(vm);

let cr = borrow_global_mut<ConsensusReward>(@VMReserved );
cr.value = value;
cr.clearing_price = clearing_price;
cr.median_win_bid = median_win_bid;
cr.median_history = median_history;

}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ script {

let (_val_universe, _their_bids, _their_expiry) = TestFixtures::pof_default(&vm);
// Ok now Eve changes her mind. Will force the bid to expire.
ProofOfFee::set_bid(&eve_sig, 0, 1);
ProofOfFee::set_bid(&eve_sig, 0, 0);

let sorted = ProofOfFee::get_sorted_vals(false);
let len = Vector::length(&sorted);
Expand All @@ -43,8 +43,6 @@ script {
print(&clear_price);
assert!(clear_price == 1, 1002);
print(&median_bid);
assert!(median_bid == 3, 1003);


assert!(median_bid == 2, 1003);
}
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
processed 2 tasks
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
//# init --validators Alice

// Scenario: The reward is too high during 5 days (short window). People are bidding over 95% of the baseline fee.


//# run --admin-script --signers DiemRoot Alice
script {
use DiemFramework::ProofOfFee;
use DiemFramework::Debug::print;
use Std::Vector;

fun main(vm: signer, _a_sig: signer) {

let start_value = 0960; // 96% of baseline fee.
let median_history = Vector::empty<u64>();

// we need at least 10 epochs above the 95% range.
let i = 0;
while (i < 12) {
// let factor = i * 10;
// let value = start_value + factor;
// print(&value);
Vector::push_back(&mut median_history, start_value);
i = i + 1;
};


ProofOfFee::test_mock_reward(
&vm,
100,
50,
33,
median_history,
);

// no changes until we run the thermostat.
let (value, clearing, median) = ProofOfFee::get_consensus_reward();
assert!(value == 100, 1000);
assert!(clearing == 50, 1001);
assert!(median == 33, 1002);

ProofOfFee::reward_thermostat(&vm);

// In the decrease case during a short period, we decrease by 5%
// No other parameters of consensus reward should change on calling this function.
let (value, clearing, median) = ProofOfFee::get_consensus_reward();
assert!(value == 90, 1000);
assert!(clearing == 50, 1001);
assert!(median == 33, 1002);

print(&value);
print(&clearing);
print(&median);

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

0 comments on commit 03f25e8

Please sign in to comment.