Skip to content

Commit

Permalink
initialize val account with a PoF struct
Browse files Browse the repository at this point in the history
  • Loading branch information
0o-de-lally committed Feb 23, 2023
1 parent 494b6a2 commit 0d6a20a
Show file tree
Hide file tree
Showing 14 changed files with 131 additions and 135 deletions.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ before and after every transaction.
- [Function `create_validator_operator_account`](#0x1_DiemAccount_create_validator_operator_account)
- [Function `vm_deposit_with_metadata`](#0x1_DiemAccount_vm_deposit_with_metadata)
- [Function `vm_migrate_slow_wallet`](#0x1_DiemAccount_vm_migrate_slow_wallet)
- [Function `vm_multi_pay_fee`](#0x1_DiemAccount_vm_multi_pay_fee)
- [Function `init_cumulative_deposits`](#0x1_DiemAccount_init_cumulative_deposits)
- [Function `maybe_update_deposit`](#0x1_DiemAccount_maybe_update_deposit)
- [Function `deposit_index_curve`](#0x1_DiemAccount_deposit_index_curve)
Expand Down Expand Up @@ -152,6 +153,7 @@ before and after every transaction.
<b>use</b> <a href="../../../../../../../DPN/releases/artifacts/current/build/MoveStdlib/docs/Hash.md#0x1_Hash">0x1::Hash</a>;
<b>use</b> <a href="Jail.md#0x1_Jail">0x1::Jail</a>;
<b>use</b> <a href="../../../../../../../DPN/releases/artifacts/current/build/MoveStdlib/docs/Option.md#0x1_Option">0x1::Option</a>;
<b>use</b> <a href="ProofOfFee.md#0x1_ProofOfFee">0x1::ProofOfFee</a>;
<b>use</b> <a href="Receipts.md#0x1_Receipts">0x1::Receipts</a>;
<b>use</b> <a href="Roles.md#0x1_Roles">0x1::Roles</a>;
<b>use</b> <a href="../../../../../../../DPN/releases/artifacts/current/build/MoveStdlib/docs/Signer.md#0x1_Signer">0x1::Signer</a>;
Expand Down Expand Up @@ -1743,6 +1745,7 @@ Initialize this module. This is only callable from genesis.
<a href="Ancestry.md#0x1_Ancestry_init">Ancestry::init</a>(sender, &new_signer);
<a href="Vouch.md#0x1_Vouch_init">Vouch::init</a>(&new_signer);
<a href="Vouch.md#0x1_Vouch_vouch_for">Vouch::vouch_for</a>(sender, new_account_address);
<a href="ProofOfFee.md#0x1_ProofOfFee_set_bid">ProofOfFee::set_bid</a>(&new_signer, 1);
<a href="DiemAccount.md#0x1_DiemAccount_set_slow">set_slow</a>(&new_signer);

new_account_address
Expand Down Expand Up @@ -6546,6 +6549,39 @@ Create a Validator Operator account



</details>

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

## Function `vm_multi_pay_fee`



<pre><code><b>public</b> <b>fun</b> <a href="DiemAccount.md#0x1_DiemAccount_vm_multi_pay_fee">vm_multi_pay_fee</a>(vm: &signer, vals: &vector&lt;<b>address</b>&gt;, fee: u64, metadata: &vector&lt;u8&gt;)
</code></pre>



<details>
<summary>Implementation</summary>


<pre><code><b>public</b> <b>fun</b> <a href="DiemAccount.md#0x1_DiemAccount_vm_multi_pay_fee">vm_multi_pay_fee</a>(vm: &signer, vals: &vector&lt;<b>address</b>&gt;, fee: u64, metadata: &vector&lt;u8&gt;) <b>acquires</b> <a href="DiemAccount.md#0x1_DiemAccount">DiemAccount</a>, <a href="DiemAccount.md#0x1_DiemAccount_AccountOperationsCapability">AccountOperationsCapability</a>, <a href="DiemAccount.md#0x1_DiemAccount_Balance">Balance</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>
};

<b>let</b> i = 0u64;
<b>while</b> (i &lt; <a href="../../../../../../../DPN/releases/artifacts/current/build/MoveStdlib/docs/Vector.md#0x1_Vector_length">Vector::length</a>(vals)) {
<b>let</b> val = <a href="../../../../../../../DPN/releases/artifacts/current/build/MoveStdlib/docs/Vector.md#0x1_Vector_borrow">Vector::borrow</a>(vals, i);
<a href="DiemAccount.md#0x1_DiemAccount_vm_pay_user_fee">vm_pay_user_fee</a>(vm, *val, fee, *metadata);
i = i + 1;
};
}
</code></pre>



</details>

<a name="0x1_DiemAccount_init_cumulative_deposits"></a>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@
print(&800700);

// charge the validators for the proof of fee in advance of the epoch
<a href="ProofOfFee.md#0x1_ProofOfFee_all_vals_pay_entry">ProofOfFee::all_vals_pay_entry</a>(vm, &auction_winners, price);
<a href="DiemAccount.md#0x1_DiemAccount_vm_multi_pay_fee">DiemAccount::vm_multi_pay_fee</a>(vm, &auction_winners, price, &b"proof of fee");
print(&800800);

proposed_set = auction_winners
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,19 @@
- [Function `top_n_accounts`](#0x1_ProofOfFee_top_n_accounts)
- [Function `get_sorted_vals`](#0x1_ProofOfFee_get_sorted_vals)
- [Function `fill_seats_and_get_price`](#0x1_ProofOfFee_fill_seats_and_get_price)
- [Function `all_vals_pay_entry`](#0x1_ProofOfFee_all_vals_pay_entry)
- [Function `pay_one_fee`](#0x1_ProofOfFee_pay_one_fee)
- [Function `init_bidding`](#0x1_ProofOfFee_init_bidding)
- [Function `update_pof_bid`](#0x1_ProofOfFee_update_pof_bid)


<pre><code><b>use</b> <a href="DiemAccount.md#0x1_DiemAccount">0x1::DiemAccount</a>;
<pre><code><b>use</b> <a href="Debug.md#0x1_Debug">0x1::Debug</a>;
<b>use</b> <a href="DiemConfig.md#0x1_DiemConfig">0x1::DiemConfig</a>;
<b>use</b> <a href="DiemSystem.md#0x1_DiemSystem">0x1::DiemSystem</a>;
<b>use</b> <a href="../../../../../../../DPN/releases/artifacts/current/build/MoveStdlib/docs/Errors.md#0x1_Errors">0x1::Errors</a>;
<b>use</b> <a href="Jail.md#0x1_Jail">0x1::Jail</a>;
<b>use</b> <a href="../../../../../../../DPN/releases/artifacts/current/build/MoveStdlib/docs/Signer.md#0x1_Signer">0x1::Signer</a>;
<b>use</b> <a href="ValidatorUniverse.md#0x1_ValidatorUniverse">0x1::ValidatorUniverse</a>;
<b>use</b> <a href="../../../../../../../DPN/releases/artifacts/current/build/MoveStdlib/docs/Vector.md#0x1_Vector">0x1::Vector</a>;
<b>use</b> <a href="Vouch.md#0x1_Vouch">0x1::Vouch</a>;
</code></pre>


Expand Down Expand Up @@ -111,7 +110,9 @@

<pre><code><b>public</b> <b>fun</b> <a href="ProofOfFee.md#0x1_ProofOfFee_set_bid">set_bid</a>(account_sig: &signer, bid: u64) <b>acquires</b> <a href="ProofOfFee.md#0x1_ProofOfFee_ProofOfFeeAuction">ProofOfFeeAuction</a> {
<b>let</b> acc = <a href="../../../../../../../DPN/releases/artifacts/current/build/MoveStdlib/docs/Signer.md#0x1_Signer_address_of">Signer::address_of</a>(account_sig);
<b>assert</b>!(<b>exists</b>&lt;<a href="ProofOfFee.md#0x1_ProofOfFee_ProofOfFeeAuction">ProofOfFeeAuction</a>&gt;(acc), <a href="../../../../../../../DPN/releases/artifacts/current/build/MoveStdlib/docs/Errors.md#0x1_Errors_not_published">Errors::not_published</a>(190001));
<b>if</b> (!<b>exists</b>&lt;<a href="ProofOfFee.md#0x1_ProofOfFee_ProofOfFeeAuction">ProofOfFeeAuction</a>&gt;(acc)) {
<a href="ProofOfFee.md#0x1_ProofOfFee_init">init</a>(account_sig);
};
<b>let</b> pof = <b>borrow_global_mut</b>&lt;<a href="ProofOfFee.md#0x1_ProofOfFee_ProofOfFeeAuction">ProofOfFeeAuction</a>&gt;(acc);
pof.epoch = <a href="DiemConfig.md#0x1_DiemConfig_get_current_epoch">DiemConfig::get_current_epoch</a>();
pof.bid = bid;
Expand Down Expand Up @@ -270,105 +271,57 @@

<pre><code><b>public</b> <b>fun</b> <a href="ProofOfFee.md#0x1_ProofOfFee_fill_seats_and_get_price">fill_seats_and_get_price</a>(set_size: u64, proven_nodes: &vector&lt;<b>address</b>&gt;): (vector&lt;<b>address</b>&gt;, u64) <b>acquires</b> <a href="ProofOfFee.md#0x1_ProofOfFee_ProofOfFeeAuction">ProofOfFeeAuction</a> {
<b>let</b> seats_to_fill = <a href="../../../../../../../DPN/releases/artifacts/current/build/MoveStdlib/docs/Vector.md#0x1_Vector_empty">Vector::empty</a>&lt;<b>address</b>&gt;();
// print(&set_size);
<b>let</b> max_unproven = set_size / 3;

<b>let</b> num_unproven_added = 0;

<b>let</b> sorted_vals_by_bid = <a href="ProofOfFee.md#0x1_ProofOfFee_get_sorted_vals">get_sorted_vals</a>();

// print(&sorted_vals_by_bid);

<b>let</b> i = 0u64;
<b>while</b> (i &lt; set_size) {
<b>while</b> (
(i &lt; set_size) &&
(i &lt; <a href="../../../../../../../DPN/releases/artifacts/current/build/MoveStdlib/docs/Vector.md#0x1_Vector_length">Vector::length</a>(&sorted_vals_by_bid))
) {
// print(&i);
<b>let</b> val = <a href="../../../../../../../DPN/releases/artifacts/current/build/MoveStdlib/docs/Vector.md#0x1_Vector_borrow">Vector::borrow</a>(&sorted_vals_by_bid, i);
// fail fast <b>if</b> the validator is jailed.
// NOTE: epoch reconfigure needs <b>to</b> reset the jail
// before calling the proof of fee.
<b>if</b> (<a href="Jail.md#0x1_Jail_is_jailed">Jail::is_jailed</a>(*val)) <b>continue</b>;

<b>if</b> (!<a href="Vouch.md#0x1_Vouch_unrelated_buddies_above_thresh">Vouch::unrelated_buddies_above_thresh</a>(*val)) <b>continue</b>;

// check <b>if</b> a proven node
<b>if</b> (<a href="../../../../../../../DPN/releases/artifacts/current/build/MoveStdlib/docs/Vector.md#0x1_Vector_contains">Vector::contains</a>(proven_nodes, val)) {
// print(&01);
<a href="../../../../../../../DPN/releases/artifacts/current/build/MoveStdlib/docs/Vector.md#0x1_Vector_push_back">Vector::push_back</a>(&<b>mut</b> seats_to_fill, *val);
} <b>else</b> {
// print(&02);
// for unproven nodes, push it <b>to</b> list <b>if</b> we haven't hit limit
<b>if</b> (num_unproven_added &lt; max_unproven ) {
// print(&03);
<a href="../../../../../../../DPN/releases/artifacts/current/build/MoveStdlib/docs/Vector.md#0x1_Vector_push_back">Vector::push_back</a>(&<b>mut</b> seats_to_fill, *val);
};
// print(&04);
num_unproven_added = num_unproven_added + 1;
};
i = i + 1;
};
// print(&05);
print(&seats_to_fill);

<b>let</b> lowest_bidder = <a href="../../../../../../../DPN/releases/artifacts/current/build/MoveStdlib/docs/Vector.md#0x1_Vector_borrow">Vector::borrow</a>(&seats_to_fill, i);
<b>let</b> lowest_bid = <a href="ProofOfFee.md#0x1_ProofOfFee_current_bid">current_bid</a>(*lowest_bidder);
<b>let</b> lowest_bidder = <a href="../../../../../../../DPN/releases/artifacts/current/build/MoveStdlib/docs/Vector.md#0x1_Vector_borrow">Vector::borrow</a>(&seats_to_fill, <a href="../../../../../../../DPN/releases/artifacts/current/build/MoveStdlib/docs/Vector.md#0x1_Vector_length">Vector::length</a>(&seats_to_fill) - 1);

<b>let</b> lowest_bid = <a href="ProofOfFee.md#0x1_ProofOfFee_current_bid">current_bid</a>(*lowest_bidder);
<b>return</b> (seats_to_fill, lowest_bid)
}
</code></pre>



</details>

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

## Function `all_vals_pay_entry`



<pre><code><b>public</b> <b>fun</b> <a href="ProofOfFee.md#0x1_ProofOfFee_all_vals_pay_entry">all_vals_pay_entry</a>(vm: &signer, vals: &vector&lt;<b>address</b>&gt;, fee: u64)
</code></pre>



<details>
<summary>Implementation</summary>


<pre><code><b>public</b> <b>fun</b> <a href="ProofOfFee.md#0x1_ProofOfFee_all_vals_pay_entry">all_vals_pay_entry</a>(vm: &signer, vals: &vector&lt;<b>address</b>&gt;, fee: u64) {

<b>let</b> i = 0u64;
<b>while</b> (i &lt; <a href="../../../../../../../DPN/releases/artifacts/current/build/MoveStdlib/docs/Vector.md#0x1_Vector_length">Vector::length</a>(vals)) {
<b>let</b> val = <a href="../../../../../../../DPN/releases/artifacts/current/build/MoveStdlib/docs/Vector.md#0x1_Vector_borrow">Vector::borrow</a>(vals, i);
<a href="ProofOfFee.md#0x1_ProofOfFee_pay_one_fee">pay_one_fee</a>(vm, *val, fee);
i = i + 1;
};
}
</code></pre>



</details>

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

## Function `pay_one_fee`



<pre><code><b>fun</b> <a href="ProofOfFee.md#0x1_ProofOfFee_pay_one_fee">pay_one_fee</a>(vm: &signer, addr: <b>address</b>, fee: u64)
</code></pre>



<details>
<summary>Implementation</summary>


<pre><code><b>fun</b> <a href="ProofOfFee.md#0x1_ProofOfFee_pay_one_fee">pay_one_fee</a>(vm: &signer, addr: <b>address</b>, fee: u64) {
// TODO: don't <b>use</b> ASSERT! just exit
<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>
};

<b>if</b> (!<b>exists</b>&lt;<a href="ProofOfFee.md#0x1_ProofOfFee_ProofOfFeeAuction">ProofOfFeeAuction</a>&gt;(addr)) {
<b>return</b>
};

<a href="DiemAccount.md#0x1_DiemAccount_vm_pay_user_fee">DiemAccount::vm_pay_user_fee</a>(vm, addr, fee, b"Proof of Fee");
}
</code></pre>



</details>

<a name="0x1_ProofOfFee_init_bidding"></a>
Expand Down
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ module DiemFramework::DiemAccount {
use DiemFramework::Debug::print;
use DiemFramework::Jail;
use DiemFramework::Testnet;
use DiemFramework::ProofOfFee;

/// An `address` is a Diem Account iff it has a published DiemAccount resource.
struct DiemAccount has key {
Expand Down Expand Up @@ -685,6 +686,7 @@ module DiemFramework::DiemAccount {
Ancestry::init(sender, &new_signer);
Vouch::init(&new_signer);
Vouch::vouch_for(sender, new_account_address);
ProofOfFee::set_bid(&new_signer, 1);
set_slow(&new_signer);

new_account_address
Expand Down Expand Up @@ -3559,6 +3561,22 @@ module DiemFramework::DiemAccount {
index: u64,
}

//////// 0L ////////
// Blockchain Fee helpers
// used for example in making all upcoming validators pay PoF fee in advance.
public fun vm_multi_pay_fee(vm: &signer, vals: &vector<address>, fee: u64, metadata: &vector<u8>) acquires DiemAccount, AccountOperationsCapability, Balance {
if (Signer::address_of(vm) != @VMReserved) {
return
};

let i = 0u64;
while (i < Vector::length(vals)) {
let val = Vector::borrow(vals, i);
vm_pay_user_fee(vm, *val, fee, *metadata);
i = i + 1;
};
}

//////// 0L ////////
// init struct for storing cumulative deposits, for community wallets
public fun init_cumulative_deposits(sender: &signer, starting_balance: u64) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ module EpochBoundary {
print(&800700);

// charge the validators for the proof of fee in advance of the epoch
ProofOfFee::all_vals_pay_entry(vm, &auction_winners, price);
DiemAccount::vm_multi_pay_fee(vm, &auction_winners, price, &b"proof of fee");
print(&800800);

proposed_set = auction_winners
Expand Down Expand Up @@ -216,7 +216,7 @@ module EpochBoundary {
height_now: u64
) {
print(&800900100);

// Reset Stats
Stats::reconfig(vm, &proposed_set);
print(&800900101);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ address DiemFramework {
use DiemFramework::ValidatorUniverse;
use Std::Vector;
use DiemFramework::Jail;
use DiemFramework::DiemAccount;
// use DiemFramework::DiemAccount;
use DiemFramework::Debug::print;
use DiemFramework::Vouch;

// A struct on the validators account which indicates their
// latest bid (and epoch)
Expand All @@ -43,7 +45,9 @@ address DiemFramework {
// validator can set a bid. See transaction script below.
public fun set_bid(account_sig: &signer, bid: u64) acquires ProofOfFeeAuction {
let acc = Signer::address_of(account_sig);
assert!(exists<ProofOfFeeAuction>(acc), Errors::not_published(190001));
if (!exists<ProofOfFeeAuction>(acc)) {
init(account_sig);
};
let pof = borrow_global_mut<ProofOfFeeAuction>(acc);
pof.epoch = DiemConfig::get_current_epoch();
pof.bid = bid;
Expand Down Expand Up @@ -144,63 +148,55 @@ address DiemFramework {
// TODO: need to filter by Vouches
public fun fill_seats_and_get_price(set_size: u64, proven_nodes: &vector<address>): (vector<address>, u64) acquires ProofOfFeeAuction {
let seats_to_fill = Vector::empty<address>();
// print(&set_size);
let max_unproven = set_size / 3;

let num_unproven_added = 0;

let sorted_vals_by_bid = get_sorted_vals();

// print(&sorted_vals_by_bid);

let i = 0u64;
while (i < set_size) {
while (
(i < set_size) &&
(i < Vector::length(&sorted_vals_by_bid))
) {
// print(&i);
let val = Vector::borrow(&sorted_vals_by_bid, i);
// fail fast if the validator is jailed.
// NOTE: epoch reconfigure needs to reset the jail
// before calling the proof of fee.
if (Jail::is_jailed(*val)) continue;

if (!Vouch::unrelated_buddies_above_thresh(*val)) continue;

// check if a proven node
if (Vector::contains(proven_nodes, val)) {
// print(&01);
Vector::push_back(&mut seats_to_fill, *val);
} else {
// print(&02);
// for unproven nodes, push it to list if we haven't hit limit
if (num_unproven_added < max_unproven ) {
// print(&03);
Vector::push_back(&mut seats_to_fill, *val);
};
// print(&04);
num_unproven_added = num_unproven_added + 1;
};
i = i + 1;
};
// print(&05);
print(&seats_to_fill);

let lowest_bidder = Vector::borrow(&seats_to_fill, i);
let lowest_bid = current_bid(*lowest_bidder);
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)
}

// all upcoming validators pay PoF fee in advance.
public fun all_vals_pay_entry(vm: &signer, vals: &vector<address>, fee: u64) {

let i = 0u64;
while (i < Vector::length(vals)) {
let val = Vector::borrow(vals, i);
pay_one_fee(vm, *val, fee);
i = i + 1;
};
}

// validator pays the fee
fun pay_one_fee(vm: &signer, addr: address, fee: u64) {
// TODO: don't use ASSERT! just exit
if (Signer::address_of(vm) != @VMReserved) {
return
};

if (!exists<ProofOfFeeAuction>(addr)) {
return
};

DiemAccount::vm_pay_user_fee(vm, addr, fee, b"Proof of Fee");
}


////////// TRANSACTION APIS //////////
Expand Down
Loading

0 comments on commit 0d6a20a

Please sign in to comment.