Skip to content

Commit

Permalink
[sui-framework] Check for SUI in private balance functions (MystenLab…
Browse files Browse the repository at this point in the history
…s#19088)

## Description 

- `balance::create_staking_rewards` and
`balance::destroy_storage_rebates` are private functions used by
validators to deal with SUI staking rewards and storage rebates.
- This change is nonfunctional, but should make it more clear within
Move to understand their usage

## Test plan 

- Run tests

---

## Release notes

Check each box that your changes affect. If none of the boxes relate to
your changes, release notes aren't required.

For each box you select, include information after the relevant heading
that describes the impact of your changes that a user might notice and
any actions they must take to implement updates.

- [ ] Protocol: 
- [ ] Nodes (Validators and Full nodes): 
- [ ] Indexer: 
- [ ] JSON-RPC: 
- [ ] GraphQL: 
- [ ] CLI: 
- [ ] Rust SDK:
- [ ] REST API:
  • Loading branch information
tnowacki authored Aug 23, 2024
1 parent f401919 commit db183b6
Show file tree
Hide file tree
Showing 10 changed files with 1,039 additions and 21 deletions.
31 changes: 30 additions & 1 deletion crates/sui-framework/docs/sui-framework/balance.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ custom coins with <code><a href="../sui-framework/balance.md#0x2_balance_Supply"
- [Function `destroy_supply`](#0x2_balance_destroy_supply)


<pre><code><b>use</b> <a href="../sui-framework/tx_context.md#0x2_tx_context">0x2::tx_context</a>;
<pre><code><b>use</b> <a href="../move-stdlib/ascii.md#0x1_ascii">0x1::ascii</a>;
<b>use</b> <a href="../move-stdlib/type_name.md#0x1_type_name">0x1::type_name</a>;
<b>use</b> <a href="../sui-framework/tx_context.md#0x2_tx_context">0x2::tx_context</a>;
</code></pre>


Expand Down Expand Up @@ -123,6 +125,16 @@ For when trying to withdraw more than there is.



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

System operation performed for a coin other than SUI


<pre><code><b>const</b> <a href="../sui-framework/balance.md#0x2_balance_ENotSUI">ENotSUI</a>: <a href="../move-stdlib/u64.md#0x1_u64">u64</a> = 4;
</code></pre>



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

For when an overflow is happening on Supply operations.
Expand All @@ -133,6 +145,15 @@ For when an overflow is happening on Supply operations.



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



<pre><code><b>const</b> <a href="../sui-framework/balance.md#0x2_balance_SUI_TYPE_NAME">SUI_TYPE_NAME</a>: <a href="../move-stdlib/vector.md#0x1_vector">vector</a>&lt;u8&gt; = [48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 50, 58, 58, 115, 117, 105, 58, 58, 83, 85, 73];
</code></pre>



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

## Function `value`
Expand Down Expand Up @@ -414,6 +435,10 @@ and nowhere else.

<pre><code><b>fun</b> <a href="../sui-framework/balance.md#0x2_balance_create_staking_rewards">create_staking_rewards</a>&lt;T&gt;(value: <a href="../move-stdlib/u64.md#0x1_u64">u64</a>, ctx: &TxContext): <a href="../sui-framework/balance.md#0x2_balance_Balance">Balance</a>&lt;T&gt; {
<b>assert</b>!(ctx.sender() == @0x0, <a href="../sui-framework/balance.md#0x2_balance_ENotSystemAddress">ENotSystemAddress</a>);
<b>assert</b>!(
std::type_name::get&lt;T&gt;().into_string().into_bytes() == <a href="../sui-framework/balance.md#0x2_balance_SUI_TYPE_NAME">SUI_TYPE_NAME</a>,
<a href="../sui-framework/balance.md#0x2_balance_ENotSUI">ENotSUI</a>,
);
<a href="../sui-framework/balance.md#0x2_balance_Balance">Balance</a> { value }
}
</code></pre>
Expand Down Expand Up @@ -442,6 +467,10 @@ and nowhere else.

<pre><code><b>fun</b> <a href="../sui-framework/balance.md#0x2_balance_destroy_storage_rebates">destroy_storage_rebates</a>&lt;T&gt;(self: <a href="../sui-framework/balance.md#0x2_balance_Balance">Balance</a>&lt;T&gt;, ctx: &TxContext) {
<b>assert</b>!(ctx.sender() == @0x0, <a href="../sui-framework/balance.md#0x2_balance_ENotSystemAddress">ENotSystemAddress</a>);
<b>assert</b>!(
std::type_name::get&lt;T&gt;().into_string().into_bytes() == <a href="../sui-framework/balance.md#0x2_balance_SUI_TYPE_NAME">SUI_TYPE_NAME</a>,
<a href="../sui-framework/balance.md#0x2_balance_ENotSUI">ENotSUI</a>,
);
<b>let</b> <a href="../sui-framework/balance.md#0x2_balance_Balance">Balance</a> { value: _ } = self;
}
</code></pre>
Expand Down
13 changes: 13 additions & 0 deletions crates/sui-framework/packages/sui-framework/sources/balance.move
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ module sui::balance {
const ENotEnough: u64 = 2;
/// Sender is not @0x0 the system address.
const ENotSystemAddress: u64 = 3;
/// System operation performed for a coin other than SUI
const ENotSUI: u64 = 4;

/// A Supply of T. Used for minting and burning.
/// Wrapped into a `TreasuryCap` in the `Coin` module.
Expand Down Expand Up @@ -91,12 +93,19 @@ module sui::balance {
let Balance { value: _ } = balance;
}

const SUI_TYPE_NAME: vector<u8> =
b"0000000000000000000000000000000000000000000000000000000000000002::sui::SUI";

#[allow(unused_function)]
/// CAUTION: this function creates a `Balance` without increasing the supply.
/// It should only be called by the epoch change system txn to create staking rewards,
/// and nowhere else.
fun create_staking_rewards<T>(value: u64, ctx: &TxContext): Balance<T> {
assert!(ctx.sender() == @0x0, ENotSystemAddress);
assert!(
std::type_name::get<T>().into_string().into_bytes() == SUI_TYPE_NAME,
ENotSUI,
);
Balance { value }
}

Expand All @@ -106,6 +115,10 @@ module sui::balance {
/// and nowhere else.
fun destroy_storage_rebates<T>(self: Balance<T>, ctx: &TxContext) {
assert!(ctx.sender() == @0x0, ENotSystemAddress);
assert!(
std::type_name::get<T>().into_string().into_bytes() == SUI_TYPE_NAME,
ENotSUI,
);
let Balance { value: _ } = self;
}

Expand Down
Binary file modified crates/sui-framework/packages_compiled/sui-framework
Binary file not shown.
2 changes: 1 addition & 1 deletion crates/sui-open-rpc/spec/openrpc.json
Original file line number Diff line number Diff line change
Expand Up @@ -1293,7 +1293,7 @@
"name": "Result",
"value": {
"minSupportedProtocolVersion": "1",
"maxSupportedProtocolVersion": "55",
"maxSupportedProtocolVersion": "56",
"protocolVersion": "6",
"featureFlags": {
"accept_zklogin_in_multisig": false,
Expand Down
3 changes: 2 additions & 1 deletion crates/sui-protocol-config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use tracing::{info, warn};

/// The minimum and maximum protocol versions supported by this build.
const MIN_PROTOCOL_VERSION: u64 = 1;
const MAX_PROTOCOL_VERSION: u64 = 55;
const MAX_PROTOCOL_VERSION: u64 = 56;

// Record history of protocol version allocations here:
//
Expand Down Expand Up @@ -2683,6 +2683,7 @@ impl ProtocolConfig {

cfg.feature_flags.rethrow_serialization_type_layout_errors = true;
}
56 => {}
// Use this template when making changes:
//
// // modify an existing constant.
Expand Down
Loading

0 comments on commit db183b6

Please sign in to comment.