Skip to content

Commit

Permalink
[sui-framework] Initial port of delegation stress tests
Browse files Browse the repository at this point in the history
This is the initial port of the delegation stress test that Emma wrote
in #7574 to the new delegation flow. We'll need to (and want to) add
additional actions (e.g., candidacy, removal etc).
  • Loading branch information
tzakian committed Mar 8, 2023
1 parent 6f82a32 commit 1bc63d1
Show file tree
Hide file tree
Showing 9 changed files with 407 additions and 0 deletions.
25 changes: 25 additions & 0 deletions crates/sui-framework/docs/staking_pool.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
- [Function `get_token_amount`](#0x2_staking_pool_get_token_amount)
- [Function `initial_exchange_rate`](#0x2_staking_pool_initial_exchange_rate)
- [Function `check_balance_invariants`](#0x2_staking_pool_check_balance_invariants)
- [Function `validator_address`](#0x2_staking_pool_validator_address)


<pre><code><b>use</b> <a href="">0x1::option</a>;
Expand Down Expand Up @@ -1408,4 +1409,28 @@ Returns true if the provided staking pool is preactive at the provided epoch.



</details>

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

## Function `validator_address`



<pre><code><b>public</b> <b>fun</b> <a href="staking_pool.md#0x2_staking_pool_validator_address">validator_address</a>(staked_sui: &<a href="staking_pool.md#0x2_staking_pool_StakedSui">staking_pool::StakedSui</a>): <b>address</b>
</code></pre>



<details>
<summary>Implementation</summary>


<pre><code><b>public</b> <b>fun</b> <a href="staking_pool.md#0x2_staking_pool_validator_address">validator_address</a>(staked_sui: &<a href="staking_pool.md#0x2_staking_pool_StakedSui">StakedSui</a>): <b>address</b> {
staked_sui.validator_address
}
</code></pre>



</details>
27 changes: 27 additions & 0 deletions crates/sui-framework/docs/sui_system.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
- [Function `validators`](#0x2_sui_system_validators)
- [Function `active_validator_by_address`](#0x2_sui_system_active_validator_by_address)
- [Function `pending_validator_by_address`](#0x2_sui_system_pending_validator_by_address)
- [Function `storage_fund_balance`](#0x2_sui_system_storage_fund_balance)


<pre><code><b>use</b> <a href="">0x1::ascii</a>;
Expand Down Expand Up @@ -1946,4 +1947,30 @@ Return the currently pending validator by address



</details>

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

## Function `storage_fund_balance`

Return the balance of the storage fund.


<pre><code><b>public</b> <b>fun</b> <a href="sui_system.md#0x2_sui_system_storage_fund_balance">storage_fund_balance</a>(wrapper: &<a href="sui_system.md#0x2_sui_system_SuiSystemState">sui_system::SuiSystemState</a>): u64
</code></pre>



<details>
<summary>Implementation</summary>


<pre><code><b>public</b> <b>fun</b> <a href="sui_system.md#0x2_sui_system_storage_fund_balance">storage_fund_balance</a>(wrapper: &<a href="sui_system.md#0x2_sui_system_SuiSystemState">SuiSystemState</a>): u64 {
<b>let</b> self = <a href="sui_system.md#0x2_sui_system_load_system_state">load_system_state</a>(wrapper);
<a href="balance.md#0x2_balance_value">balance::value</a>(&self.storage_fund)
}
</code></pre>



</details>
4 changes: 4 additions & 0 deletions crates/sui-framework/sources/governance/staking_pool.move
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,10 @@ module sui::staking_pool {

// ==== test-related functions ====

public fun validator_address(staked_sui: &StakedSui): address {
staked_sui.validator_address
}

// Given the `staked_sui` receipt calculate the current rewards (in terms of SUI) for it.
#[test_only]
public fun calculate_rewards(
Expand Down
6 changes: 6 additions & 0 deletions crates/sui-framework/sources/governance/sui_system.move
Original file line number Diff line number Diff line change
Expand Up @@ -824,6 +824,12 @@ module sui::sui_system {
validator_set::get_pending_validator_ref(validators(self), validator_address)
}

/// Return the balance of the storage fund.
public fun storage_fund_balance(wrapper: &SuiSystemState): u64 {
let self = load_system_state(wrapper);
balance::value(&self.storage_fund)
}

#[test_only]
public fun set_epoch_for_testing(wrapper: &mut SuiSystemState, epoch_num: u64) {
let self = load_system_state_mut(wrapper);
Expand Down
4 changes: 4 additions & 0 deletions crates/sui-framework/sources/test/test_random.move
Original file line number Diff line number Diff line change
Expand Up @@ -141,4 +141,8 @@ module sui::test_random {
public fun next_bool(random: &mut Random): bool {
next_u8(random) % 2 == 1
}

public fun next_address(random: &mut Random): address {
sui::address::from_u256(next_u256_in_range(random, 1461501637330902918203684832716283019655932542975))
}
}
2 changes: 2 additions & 0 deletions crates/sui-framework/sources/test/test_scenario.move
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,8 @@ module sui::test_scenario {
/// Returns all ids of type `T` transferred to address `account`.
public native fun ids_for_address<T: key>(account: address): vector<ID>;

public native fun is_owned_by_address_of_type<T>(account: address, id: ID): bool;

/// helper that returns true iff `most_recent_id_for_address` returns some
public fun has_most_recent_for_address<T: key>(account: address): bool {
option::is_some(&most_recent_id_for_address<T>(account))
Expand Down
5 changes: 5 additions & 0 deletions crates/sui-framework/src/natives/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,11 @@ pub fn all_natives(
"ids_for_address",
make_native!(test_scenario::ids_for_address),
),
(
"test_scenario",
"is_owned_by_address_of_type",
make_native!(test_scenario::is_owned_by_address_of_type),
),
(
"transfer",
"transfer_internal",
Expand Down
23 changes: 23 additions & 0 deletions crates/sui-framework/src/natives/test_scenario.rs
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,29 @@ pub fn was_taken_shared(
))
}

pub fn is_owned_by_address_of_type(
context: &mut NativeContext,
ty_args: Vec<Type>,
mut args: VecDeque<Value>,
) -> PartialVMResult<NativeResult> {
let specified_ty = get_specified_ty(ty_args);
let id = pop_id(&mut args)?;
let account: SuiAddress = pop_arg!(args, AccountAddress).into();
assert!(args.is_empty());
let object_runtime: &mut ObjectRuntime = context.extensions_mut().get_mut();
let inventories = &mut object_runtime.test_inventories;
let is_owned_by_address = inventories
.address_inventories
.get(&account)
.and_then(|inv| inv.get(&specified_ty))
.map(|s| s.contains_key(&id))
.unwrap_or(false);
Ok(NativeResult::ok(
legacy_test_cost(),
smallvec![Value::bool(is_owned_by_address)],
))
}

// impls

fn take_from_inventory(
Expand Down
Loading

0 comments on commit 1bc63d1

Please sign in to comment.