Skip to content

Commit

Permalink
Add transaction scripts for oracle delegation (#735)
Browse files Browse the repository at this point in the history
* add transaction scripts for oracle delegation

* cleanup

* change hard coded path

* docs

* on genesis: enable oracle delegation

* patch integration test
  • Loading branch information
0o-de-lally authored Oct 8, 2021
1 parent 8b4457a commit 4240824
Show file tree
Hide file tree
Showing 35 changed files with 570 additions and 105 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ endif

fixture-stdlib:
make stdlib
cp language/stdlib/staged/stdlib.mv ol/fixtures/stdlib/fresh_stdlib.mv
cp language/diem-framework/staged/stdlib.mv ol/fixtures/stdlib/fresh_stdlib.mv

#### HELPERS ####
check:
Expand Down
22 changes: 17 additions & 5 deletions language/diem-framework/modules/0L/Oracle.move
Original file line number Diff line number Diff line change
Expand Up @@ -339,11 +339,21 @@ address 0x1 {
}

public fun enable_delegation (sender: &signer) {
move_to<VoteDelegation>(sender, VoteDelegation{
vote_delegated: false,
delegates: Vector::empty<address>(),
delegated_to_address: Signer::address_of(sender),
});
if (!exists<VoteDelegation>(Signer::address_of(sender))) {
move_to<VoteDelegation>(sender, VoteDelegation{
vote_delegated: false,
delegates: Vector::empty<address>(),
delegated_to_address: Signer::address_of(sender),
});
}
}

public fun has_delegated (account: address): bool acquires VoteDelegation {
if (exists<VoteDelegation>(account)) {
let del = borrow_global<VoteDelegation>(account);
return del.vote_delegated
};
false
}

public fun check_number_delegates (addr: address): u64 acquires VoteDelegation {
Expand All @@ -354,6 +364,8 @@ address 0x1 {

public fun delegate_vote (sender: &signer, vote_dest: address) acquires VoteDelegation{
assert(exists<VoteDelegation>(Signer::address_of(sender)), Errors::not_published(DELEGATION_NOT_ENABLED));

// check if the receipient/destination has enabled delegation.
assert(exists<VoteDelegation>(vote_dest), Errors::not_published(DELEGATION_NOT_ENABLED));

let del = borrow_global_mut<VoteDelegation>(Signer::address_of(sender));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,29 @@
address 0x1 {
module OracleScripts {
use 0x1::Oracle;

public(script) fun ol_oracle_tx(sender: signer, id: u64, data: vector<u8>) {
Oracle::handler(&sender, id, data);
}

/// A validator (Alice) can delegate the authority for the operation of an upgrade to another validator (Bob). When Oracle delegation happens, effectively the consensus voting power of Alice, is added to Bob only for the effect of calculating the preference on electing a stdlib binary. Whatever binary Bob proposes, Alice will also propose without needing to be submitting transactions.
public(script) fun ol_delegate_vote(sender: signer, dest: address) {
// if for some reason not delegated
Oracle::enable_delegation(&sender);

Oracle::delegate_vote(&sender, dest);
}

/// First Bob must have delegation enabled, which can be done with:
public(script) fun ol_enable_delegation(sender: signer) {
Oracle::enable_delegation(&sender);
}
/// Alice can remove Bob as the delegate with this function.
public(script) fun ol_remove_delegation(sender: signer) {
Oracle::remove_delegate_vote(&sender);
}

}
}
6 changes: 4 additions & 2 deletions language/diem-framework/modules/DiemAccount.move
Original file line number Diff line number Diff line change
Expand Up @@ -563,13 +563,15 @@ module DiemAccount {

// User can join validator universe list, but will only join if
// the mining is above the threshold in the preceeding period.
ValidatorUniverse::add_self(&new_signer);
ValidatorUniverse::add_self(&new_signer);

make_account(new_signer, auth_key_prefix);
make_account(new_op_account, op_auth_key_prefix);

MinerState::reset_rate_limit(sender);



// Transfer for owner
onboarding_gas_transfer<GAS>(sender, new_account_address);
// Transfer for operator as well
Expand Down
2 changes: 2 additions & 0 deletions language/diem-framework/modules/doc/DiemAccount.md
Original file line number Diff line number Diff line change
Expand Up @@ -1515,6 +1515,8 @@ Initialize this module. This is only callable from genesis.

<a href="MinerState.md#0x1_MinerState_reset_rate_limit">MinerState::reset_rate_limit</a>(sender);



// Transfer for owner
<a href="DiemAccount.md#0x1_DiemAccount_onboarding_gas_transfer">onboarding_gas_transfer</a>&lt;<a href="GAS.md#0x1_GAS">GAS</a>&gt;(sender, new_account_address);
// Transfer for operator <b>as</b> well
Expand Down
43 changes: 38 additions & 5 deletions language/diem-framework/modules/doc/Oracle.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
- [Function `get_threshold`](#0x1_Oracle_get_threshold)
- [Function `calculate_proportional_voting_threshold`](#0x1_Oracle_calculate_proportional_voting_threshold)
- [Function `enable_delegation`](#0x1_Oracle_enable_delegation)
- [Function `has_delegated`](#0x1_Oracle_has_delegated)
- [Function `check_number_delegates`](#0x1_Oracle_check_number_delegates)
- [Function `delegate_vote`](#0x1_Oracle_delegate_vote)
- [Function `remove_delegate_vote`](#0x1_Oracle_remove_delegate_vote)
Expand Down Expand Up @@ -890,11 +891,41 @@


<pre><code><b>public</b> <b>fun</b> <a href="Oracle.md#0x1_Oracle_enable_delegation">enable_delegation</a> (sender: &signer) {
move_to&lt;<a href="Oracle.md#0x1_Oracle_VoteDelegation">VoteDelegation</a>&gt;(sender, <a href="Oracle.md#0x1_Oracle_VoteDelegation">VoteDelegation</a>{
vote_delegated: <b>false</b>,
delegates: <a href="../../../../../../move-stdlib/docs/Vector.md#0x1_Vector_empty">Vector::empty</a>&lt;address&gt;(),
delegated_to_address: <a href="../../../../../../move-stdlib/docs/Signer.md#0x1_Signer_address_of">Signer::address_of</a>(sender),
});
<b>if</b> (!<b>exists</b>&lt;<a href="Oracle.md#0x1_Oracle_VoteDelegation">VoteDelegation</a>&gt;(<a href="../../../../../../move-stdlib/docs/Signer.md#0x1_Signer_address_of">Signer::address_of</a>(sender))) {
move_to&lt;<a href="Oracle.md#0x1_Oracle_VoteDelegation">VoteDelegation</a>&gt;(sender, <a href="Oracle.md#0x1_Oracle_VoteDelegation">VoteDelegation</a>{
vote_delegated: <b>false</b>,
delegates: <a href="../../../../../../move-stdlib/docs/Vector.md#0x1_Vector_empty">Vector::empty</a>&lt;address&gt;(),
delegated_to_address: <a href="../../../../../../move-stdlib/docs/Signer.md#0x1_Signer_address_of">Signer::address_of</a>(sender),
});
}
}
</code></pre>



</details>

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

## Function `has_delegated`



<pre><code><b>public</b> <b>fun</b> <a href="Oracle.md#0x1_Oracle_has_delegated">has_delegated</a>(account: address): bool
</code></pre>



<details>
<summary>Implementation</summary>


<pre><code><b>public</b> <b>fun</b> <a href="Oracle.md#0x1_Oracle_has_delegated">has_delegated</a> (account: address): bool <b>acquires</b> <a href="Oracle.md#0x1_Oracle_VoteDelegation">VoteDelegation</a> {
<b>if</b> (<b>exists</b>&lt;<a href="Oracle.md#0x1_Oracle_VoteDelegation">VoteDelegation</a>&gt;(account)) {
<b>let</b> del = borrow_global&lt;<a href="Oracle.md#0x1_Oracle_VoteDelegation">VoteDelegation</a>&gt;(account);
<b>return</b> del.vote_delegated
};
<b>false</b>
}
</code></pre>

Expand Down Expand Up @@ -945,6 +976,8 @@

<pre><code><b>public</b> <b>fun</b> <a href="Oracle.md#0x1_Oracle_delegate_vote">delegate_vote</a> (sender: &signer, vote_dest: address) <b>acquires</b> <a href="Oracle.md#0x1_Oracle_VoteDelegation">VoteDelegation</a>{
<b>assert</b>(<b>exists</b>&lt;<a href="Oracle.md#0x1_Oracle_VoteDelegation">VoteDelegation</a>&gt;(<a href="../../../../../../move-stdlib/docs/Signer.md#0x1_Signer_address_of">Signer::address_of</a>(sender)), <a href="../../../../../../move-stdlib/docs/Errors.md#0x1_Errors_not_published">Errors::not_published</a>(<a href="Oracle.md#0x1_Oracle_DELEGATION_NOT_ENABLED">DELEGATION_NOT_ENABLED</a>));

// check <b>if</b> the receipient/destination has enabled delegation.
<b>assert</b>(<b>exists</b>&lt;<a href="Oracle.md#0x1_Oracle_VoteDelegation">VoteDelegation</a>&gt;(vote_dest), <a href="../../../../../../move-stdlib/docs/Errors.md#0x1_Errors_not_published">Errors::not_published</a>(<a href="Oracle.md#0x1_Oracle_DELEGATION_NOT_ENABLED">DELEGATION_NOT_ENABLED</a>));

<b>let</b> del = borrow_global_mut&lt;<a href="Oracle.md#0x1_Oracle_VoteDelegation">VoteDelegation</a>&gt;(<a href="../../../../../../move-stdlib/docs/Signer.md#0x1_Signer_address_of">Signer::address_of</a>(sender));
Expand Down
81 changes: 81 additions & 0 deletions language/diem-framework/modules/doc/ol_oracle.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@


- [Function `ol_oracle_tx`](#0x1_OracleScripts_ol_oracle_tx)
- [Function `ol_delegate_vote`](#0x1_OracleScripts_ol_delegate_vote)
- [Function `ol_enable_delegation`](#0x1_OracleScripts_ol_enable_delegation)
- [Function `ol_remove_delegation`](#0x1_OracleScripts_ol_remove_delegation)


<pre><code><b>use</b> <a href="Oracle.md#0x1_Oracle">0x1::Oracle</a>;
Expand Down Expand Up @@ -35,6 +38,84 @@



</details>

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

## Function `ol_delegate_vote`

A validator (Alice) can delegate the authority for the operation of an upgrade to another validator (Bob). When Oracle delegation happens, effectively the consensus voting power of Alice, is added to Bob only for the effect of calculating the preference on electing a stdlib binary. Whatever binary Bob proposes, Alice will also propose without needing to be submitting transactions.


<pre><code><b>public</b>(<b>script</b>) <b>fun</b> <a href="ol_oracle.md#0x1_OracleScripts_ol_delegate_vote">ol_delegate_vote</a>(sender: signer, dest: address)
</code></pre>



<details>
<summary>Implementation</summary>


<pre><code><b>public</b>(<b>script</b>) <b>fun</b> <a href="ol_oracle.md#0x1_OracleScripts_ol_delegate_vote">ol_delegate_vote</a>(sender: signer, dest: address) {
// <b>if</b> for some reason not delegated
<a href="Oracle.md#0x1_Oracle_enable_delegation">Oracle::enable_delegation</a>(&sender);

<a href="Oracle.md#0x1_Oracle_delegate_vote">Oracle::delegate_vote</a>(&sender, dest);
}
</code></pre>



</details>

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

## Function `ol_enable_delegation`

First Bob must have delegation enabled, which can be done with:


<pre><code><b>public</b>(<b>script</b>) <b>fun</b> <a href="ol_oracle.md#0x1_OracleScripts_ol_enable_delegation">ol_enable_delegation</a>(sender: signer)
</code></pre>



<details>
<summary>Implementation</summary>


<pre><code><b>public</b>(<b>script</b>) <b>fun</b> <a href="ol_oracle.md#0x1_OracleScripts_ol_enable_delegation">ol_enable_delegation</a>(sender: signer) {
<a href="Oracle.md#0x1_Oracle_enable_delegation">Oracle::enable_delegation</a>(&sender);
}
</code></pre>



</details>

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

## Function `ol_remove_delegation`

Alice can remove Bob as the delegate with this function.


<pre><code><b>public</b>(<b>script</b>) <b>fun</b> <a href="ol_oracle.md#0x1_OracleScripts_ol_remove_delegation">ol_remove_delegation</a>(sender: signer)
</code></pre>



<details>
<summary>Implementation</summary>


<pre><code><b>public</b>(<b>script</b>) <b>fun</b> <a href="ol_oracle.md#0x1_OracleScripts_ol_remove_delegation">ol_remove_delegation</a>(sender: signer) {
<a href="Oracle.md#0x1_Oracle_remove_delegate_vote">Oracle::remove_delegate_vote</a>(&sender);
}
</code></pre>



</details>


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1515,6 +1515,8 @@ Initialize this module. This is only callable from genesis.

<a href="MinerState.md#0x1_MinerState_reset_rate_limit">MinerState::reset_rate_limit</a>(sender);



// Transfer for owner
<a href="DiemAccount.md#0x1_DiemAccount_onboarding_gas_transfer">onboarding_gas_transfer</a>&lt;<a href="GAS.md#0x1_GAS">GAS</a>&gt;(sender, new_account_address);
// Transfer for operator <b>as</b> well
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
- [Function `get_threshold`](#0x1_Oracle_get_threshold)
- [Function `calculate_proportional_voting_threshold`](#0x1_Oracle_calculate_proportional_voting_threshold)
- [Function `enable_delegation`](#0x1_Oracle_enable_delegation)
- [Function `has_delegated`](#0x1_Oracle_has_delegated)
- [Function `check_number_delegates`](#0x1_Oracle_check_number_delegates)
- [Function `delegate_vote`](#0x1_Oracle_delegate_vote)
- [Function `remove_delegate_vote`](#0x1_Oracle_remove_delegate_vote)
Expand Down Expand Up @@ -890,11 +891,41 @@


<pre><code><b>public</b> <b>fun</b> <a href="Oracle.md#0x1_Oracle_enable_delegation">enable_delegation</a> (sender: &signer) {
move_to&lt;<a href="Oracle.md#0x1_Oracle_VoteDelegation">VoteDelegation</a>&gt;(sender, <a href="Oracle.md#0x1_Oracle_VoteDelegation">VoteDelegation</a>{
vote_delegated: <b>false</b>,
delegates: <a href="../../../../../../move-stdlib/docs/Vector.md#0x1_Vector_empty">Vector::empty</a>&lt;address&gt;(),
delegated_to_address: <a href="../../../../../../move-stdlib/docs/Signer.md#0x1_Signer_address_of">Signer::address_of</a>(sender),
});
<b>if</b> (!<b>exists</b>&lt;<a href="Oracle.md#0x1_Oracle_VoteDelegation">VoteDelegation</a>&gt;(<a href="../../../../../../move-stdlib/docs/Signer.md#0x1_Signer_address_of">Signer::address_of</a>(sender))) {
move_to&lt;<a href="Oracle.md#0x1_Oracle_VoteDelegation">VoteDelegation</a>&gt;(sender, <a href="Oracle.md#0x1_Oracle_VoteDelegation">VoteDelegation</a>{
vote_delegated: <b>false</b>,
delegates: <a href="../../../../../../move-stdlib/docs/Vector.md#0x1_Vector_empty">Vector::empty</a>&lt;address&gt;(),
delegated_to_address: <a href="../../../../../../move-stdlib/docs/Signer.md#0x1_Signer_address_of">Signer::address_of</a>(sender),
});
}
}
</code></pre>



</details>

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

## Function `has_delegated`



<pre><code><b>public</b> <b>fun</b> <a href="Oracle.md#0x1_Oracle_has_delegated">has_delegated</a>(account: address): bool
</code></pre>



<details>
<summary>Implementation</summary>


<pre><code><b>public</b> <b>fun</b> <a href="Oracle.md#0x1_Oracle_has_delegated">has_delegated</a> (account: address): bool <b>acquires</b> <a href="Oracle.md#0x1_Oracle_VoteDelegation">VoteDelegation</a> {
<b>if</b> (<b>exists</b>&lt;<a href="Oracle.md#0x1_Oracle_VoteDelegation">VoteDelegation</a>&gt;(account)) {
<b>let</b> del = borrow_global&lt;<a href="Oracle.md#0x1_Oracle_VoteDelegation">VoteDelegation</a>&gt;(account);
<b>return</b> del.vote_delegated
};
<b>false</b>
}
</code></pre>

Expand Down Expand Up @@ -945,6 +976,8 @@

<pre><code><b>public</b> <b>fun</b> <a href="Oracle.md#0x1_Oracle_delegate_vote">delegate_vote</a> (sender: &signer, vote_dest: address) <b>acquires</b> <a href="Oracle.md#0x1_Oracle_VoteDelegation">VoteDelegation</a>{
<b>assert</b>(<b>exists</b>&lt;<a href="Oracle.md#0x1_Oracle_VoteDelegation">VoteDelegation</a>&gt;(<a href="../../../../../../move-stdlib/docs/Signer.md#0x1_Signer_address_of">Signer::address_of</a>(sender)), <a href="../../../../../../move-stdlib/docs/Errors.md#0x1_Errors_not_published">Errors::not_published</a>(<a href="Oracle.md#0x1_Oracle_DELEGATION_NOT_ENABLED">DELEGATION_NOT_ENABLED</a>));

// check <b>if</b> the receipient/destination has enabled delegation.
<b>assert</b>(<b>exists</b>&lt;<a href="Oracle.md#0x1_Oracle_VoteDelegation">VoteDelegation</a>&gt;(vote_dest), <a href="../../../../../../move-stdlib/docs/Errors.md#0x1_Errors_not_published">Errors::not_published</a>(<a href="Oracle.md#0x1_Oracle_DELEGATION_NOT_ENABLED">DELEGATION_NOT_ENABLED</a>));

<b>let</b> del = borrow_global_mut&lt;<a href="Oracle.md#0x1_Oracle_VoteDelegation">VoteDelegation</a>&gt;(<a href="../../../../../../move-stdlib/docs/Signer.md#0x1_Signer_address_of">Signer::address_of</a>(sender));
Expand Down
Loading

0 comments on commit 4240824

Please sign in to comment.