Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] validator and delegator stress unit tests #7574

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions crates/sui-framework/docs/staking_pool.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
- [Function `staked_sui_amount`](#0x2_staking_pool_staked_sui_amount)
- [Function `delegation_request_epoch`](#0x2_staking_pool_delegation_request_epoch)
- [Function `delegation_token_amount`](#0x2_staking_pool_delegation_token_amount)
- [Function `staked_sui_id`](#0x2_staking_pool_staked_sui_id)
- [Function `pool_token_exchange_rate`](#0x2_staking_pool_pool_token_exchange_rate)
- [Function `new_pending_withdraw_entry`](#0x2_staking_pool_new_pending_withdraw_entry)
- [Function `get_sui_amount`](#0x2_staking_pool_get_sui_amount)
Expand Down Expand Up @@ -1279,6 +1280,28 @@ Destroy an empty delegation that no longer contains any SUI or pool tokens.



</details>

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

## Function `staked_sui_id`



<pre><code><b>public</b> <b>fun</b> <a href="staking_pool.md#0x2_staking_pool_staked_sui_id">staked_sui_id</a>(delegation: &<a href="staking_pool.md#0x2_staking_pool_Delegation">staking_pool::Delegation</a>): <a href="object.md#0x2_object_ID">object::ID</a>
</code></pre>



<details>
<summary>Implementation</summary>


<pre><code><b>public</b> <b>fun</b> <a href="staking_pool.md#0x2_staking_pool_staked_sui_id">staked_sui_id</a>(delegation: &<a href="staking_pool.md#0x2_staking_pool_Delegation">Delegation</a>): ID { delegation.staked_sui_id }
</code></pre>



</details>

<a name="0x2_staking_pool_pool_token_exchange_rate"></a>
Expand Down
26 changes: 26 additions & 0 deletions crates/sui-framework/docs/sui_system.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
- [Function `advance_epoch_safe_mode`](#0x2_sui_system_advance_epoch_safe_mode)
- [Function `epoch`](#0x2_sui_system_epoch)
- [Function `epoch_start_timestamp_ms`](#0x2_sui_system_epoch_start_timestamp_ms)
- [Function `storage_fund_balance`](#0x2_sui_system_storage_fund_balance)
- [Function `validator_delegate_amount`](#0x2_sui_system_validator_delegate_amount)
- [Function `validator_stake_amount`](#0x2_sui_system_validator_stake_amount)
- [Function `get_reporters_of`](#0x2_sui_system_get_reporters_of)
Expand Down Expand Up @@ -1235,6 +1236,31 @@ Returns unix timestamp of the start of current epoch



</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>(self: &<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>(self: &<a href="sui_system.md#0x2_sui_system_SuiSystemState">SuiSystemState</a>): u64 {
<a href="balance.md#0x2_balance_value">balance::value</a>(&self.storage_fund)
}
</code></pre>



</details>

<a name="0x2_sui_system_validator_delegate_amount"></a>
Expand Down
2 changes: 2 additions & 0 deletions crates/sui-framework/sources/governance/staking_pool.move
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,8 @@ module sui::staking_pool {

public fun delegation_token_amount(delegation: &Delegation): u64 { balance::value(&delegation.pool_tokens) }

public fun staked_sui_id(delegation: &Delegation): ID { delegation.staked_sui_id }

public fun pool_token_exchange_rate(pool: &StakingPool): PoolTokenExchangeRate {
PoolTokenExchangeRate {
sui_amount: pool.sui_balance,
Expand Down
5 changes: 5 additions & 0 deletions crates/sui-framework/sources/governance/sui_system.move
Original file line number Diff line number Diff line change
Expand Up @@ -572,6 +572,11 @@ module sui::sui_system {
public fun epoch_start_timestamp_ms(self: &SuiSystemState): u64 {
self.epoch_start_timestamp_ms
}

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

/// Returns the amount of stake delegated to `validator_addr`.
/// Aborts if `validator_addr` is not an active validator.
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))
}
}
5 changes: 4 additions & 1 deletion crates/sui-framework/sources/test/test_scenario.move
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,9 @@ module sui::test_scenario {
/// Returns true if the object with `ID` id was in the inventory for `account`
public native fun was_taken_from_address(account: address, id: ID): bool;

/// Returns true if there is an object of type `T` with `ID` id in the inventory for `account`.
public native fun is_owned_by_address_of_type<T: key>(account: address, id: ID): bool;

// == from sender ==

/// helper for `take_from_address_by_id` that operates over the transaction sender
Expand Down Expand Up @@ -339,7 +342,7 @@ module sui::test_scenario {
// == internal ==

// internal function that ends the transaction, realizing changes
native fun end_transaction(): TransactionEffects;
public native fun end_transaction(): TransactionEffects;

// TODO: Add API's for inspecting user events, printing the user's inventory, ...

Expand Down
14 changes: 8 additions & 6 deletions crates/sui-framework/sources/tx_context.move
Original file line number Diff line number Diff line change
Expand Up @@ -84,16 +84,18 @@ module sui::tx_context {
}

#[test_only]
/// Utility for creating 256 unique input hashes
/// Utility for creating 256 unique input hashes.
/// These hashes are guaranteed to be unique given a unique `hint: u64`
fun dummy_tx_hash_with_hint(hint: u64): vector<u8> {
// split the 64 bits of hint into 2 bit increments
let tx_hash = vector[];
let i = 1;
let hash_length = TX_HASH_LENGTH;
while (i <= hash_length) {
let value = i ^ hint % 256;
while (hint != 0) {
// grab the lowest 2 bits, hint ^ 0b11
let value = hint & 0x3;
vector::push_back(&mut tx_hash, (value as u8));
i = i + 1;
hint = hint >> 2;
};
while (vector::length(&tx_hash) < TX_HASH_LENGTH) vector::push_back(&mut tx_hash, 0);
tx_hash
}

Expand Down
2 changes: 2 additions & 0 deletions crates/sui-framework/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,9 @@ pub fn run_move_unit_tests(
path,
build_config,
UnitTestingConfig {
gas_limit: Some(1_000_000_000),
report_stacktrace_on_abort: true,
ignore_compile_warnings: true,
..config
},
natives::all_natives(MOVE_STDLIB_ADDRESS, SUI_FRAMEWORK_ADDRESS),
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 @@ -215,6 +215,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
24 changes: 24 additions & 0 deletions crates/sui-framework/src/natives/test_scenario.rs
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,30 @@ pub fn was_taken_from_address(
))
}

// native fun is_owned_by_address_of_type<T>(account: address, id: ID): bool;
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)],
))
}

// native fun take_immutable_by_id<T: key>(id: ID): T;
pub fn take_immutable_by_id(
context: &mut NativeContext,
Expand Down
Loading