Skip to content

Commit

Permalink
refactor: replace multiply_then_divide using math64::mul_div (#10047)
Browse files Browse the repository at this point in the history
Co-authored-by: Kevin <[email protected]>
  • Loading branch information
0xbe1 and movekevin authored Oct 3, 2023
1 parent 61a95f9 commit 0f444af
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 38 deletions.
36 changes: 6 additions & 30 deletions aptos-move/framework/aptos-framework/doc/delegation_pool.md
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,6 @@ transferred to A
- [Function `update_governanace_records_for_redeem_active_shares`](#0x1_delegation_pool_update_governanace_records_for_redeem_active_shares)
- [Function `update_governanace_records_for_redeem_pending_inactive_shares`](#0x1_delegation_pool_update_governanace_records_for_redeem_pending_inactive_shares)
- [Function `multiply_then_divide`](#0x1_delegation_pool_multiply_then_divide)
- [Function `to_u128`](#0x1_delegation_pool_to_u128)
- [Specification](#@Specification_1)


Expand Down Expand Up @@ -3360,15 +3359,15 @@ whether the lockup expired on the stake pool.
// operator `active` rewards not persisted yet <b>to</b> the active shares pool
<b>let</b> commission_active = total_coins(&pool.active_shares);
commission_active = <b>if</b> (active &gt; commission_active) {
<a href="delegation_pool.md#0x1_delegation_pool_multiply_then_divide">multiply_then_divide</a>(active - commission_active, pool.operator_commission_percentage, <a href="delegation_pool.md#0x1_delegation_pool_MAX_FEE">MAX_FEE</a>)
<a href="../../aptos-stdlib/doc/math64.md#0x1_math64_mul_div">math64::mul_div</a>(active - commission_active, pool.operator_commission_percentage, <a href="delegation_pool.md#0x1_delegation_pool_MAX_FEE">MAX_FEE</a>)
} <b>else</b> {
// handle <a href="../../aptos-stdlib/doc/any.md#0x1_any">any</a> slashing applied <b>to</b> `active` <a href="stake.md#0x1_stake">stake</a>
0
};
// operator `pending_inactive` rewards not persisted yet <b>to</b> the pending_inactive shares pool
<b>let</b> commission_pending_inactive = total_coins(<a href="delegation_pool.md#0x1_delegation_pool_pending_inactive_shares_pool">pending_inactive_shares_pool</a>(pool));
commission_pending_inactive = <b>if</b> (pending_inactive &gt; commission_pending_inactive) {
<a href="delegation_pool.md#0x1_delegation_pool_multiply_then_divide">multiply_then_divide</a>(
<a href="../../aptos-stdlib/doc/math64.md#0x1_math64_mul_div">math64::mul_div</a>(
pending_inactive - commission_pending_inactive,
pool.operator_commission_percentage,
<a href="delegation_pool.md#0x1_delegation_pool_MAX_FEE">MAX_FEE</a>
Expand Down Expand Up @@ -3667,9 +3666,11 @@ shares pools, assign commission to operator and eventually prepare delegation po

## Function `multiply_then_divide`

Deprecated, prefer math64::mul_div


<pre><code><b>public</b> <b>fun</b> <a href="delegation_pool.md#0x1_delegation_pool_multiply_then_divide">multiply_then_divide</a>(x: u64, y: u64, z: u64): u64
<pre><code>#[deprecated]
<b>public</b> <b>fun</b> <a href="delegation_pool.md#0x1_delegation_pool_multiply_then_divide">multiply_then_divide</a>(x: u64, y: u64, z: u64): u64
</code></pre>


Expand All @@ -3679,32 +3680,7 @@ shares pools, assign commission to operator and eventually prepare delegation po


<pre><code><b>public</b> <b>fun</b> <a href="delegation_pool.md#0x1_delegation_pool_multiply_then_divide">multiply_then_divide</a>(x: u64, y: u64, z: u64): u64 {
<b>let</b> result = (<a href="delegation_pool.md#0x1_delegation_pool_to_u128">to_u128</a>(x) * <a href="delegation_pool.md#0x1_delegation_pool_to_u128">to_u128</a>(y)) / <a href="delegation_pool.md#0x1_delegation_pool_to_u128">to_u128</a>(z);
(result <b>as</b> u64)
}
</code></pre>



</details>

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

## Function `to_u128`



<pre><code><b>fun</b> <a href="delegation_pool.md#0x1_delegation_pool_to_u128">to_u128</a>(num: u64): u128
</code></pre>



<details>
<summary>Implementation</summary>


<pre><code><b>fun</b> <a href="delegation_pool.md#0x1_delegation_pool_to_u128">to_u128</a>(num: u64): u128 {
(num <b>as</b> u128)
<a href="../../aptos-stdlib/doc/math64.md#0x1_math64_mul_div">math64::mul_div</a>(x, y, z)
}
</code></pre>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ module aptos_framework::delegation_pool {
use std::signer;
use std::vector;

use aptos_std::math64;
use aptos_std::pool_u64_unbound::{Self as pool_u64, total_coins};
use aptos_std::table::{Self, Table};
use aptos_std::smart_table::{Self, SmartTable};
Expand Down Expand Up @@ -1412,15 +1413,15 @@ module aptos_framework::delegation_pool {
// operator `active` rewards not persisted yet to the active shares pool
let commission_active = total_coins(&pool.active_shares);
commission_active = if (active > commission_active) {
multiply_then_divide(active - commission_active, pool.operator_commission_percentage, MAX_FEE)
math64::mul_div(active - commission_active, pool.operator_commission_percentage, MAX_FEE)
} else {
// handle any slashing applied to `active` stake
0
};
// operator `pending_inactive` rewards not persisted yet to the pending_inactive shares pool
let commission_pending_inactive = total_coins(pending_inactive_shares_pool(pool));
commission_pending_inactive = if (pending_inactive > commission_pending_inactive) {
multiply_then_divide(
math64::mul_div(
pending_inactive - commission_pending_inactive,
pool.operator_commission_percentage,
MAX_FEE
Expand Down Expand Up @@ -1590,13 +1591,10 @@ module aptos_framework::delegation_pool {
current_delegated_votes.pending_inactive_shares = current_delegated_votes.pending_inactive_shares - shares_to_redeem;
}

#[deprecated]
/// Deprecated, prefer math64::mul_div
public fun multiply_then_divide(x: u64, y: u64, z: u64): u64 {
let result = (to_u128(x) * to_u128(y)) / to_u128(z);
(result as u64)
}

fun to_u128(num: u64): u128 {
(num as u128)
math64::mul_div(x, y, z)
}

#[test_only]
Expand Down

0 comments on commit 0f444af

Please sign in to comment.