Skip to content

Commit

Permalink
[examples] prototype of frenemies game
Browse files Browse the repository at this point in the history
  • Loading branch information
sblackshear committed Jan 10, 2023
1 parent 2db9b44 commit 09a2035
Show file tree
Hide file tree
Showing 18 changed files with 1,237 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ expression: common_costs_estimate
---
{
"MergeCoin": {
"computation_cost": 6833,
"storage_cost": 9795,
"computation_cost": 6847,
"storage_cost": 9817,
"storage_rebate": 0
},
"Publish": {
"computation_cost": 7637,
"storage_cost": 10918,
"computation_cost": 7652,
"storage_cost": 10939,
"storage_rebate": 0
},
"SharedCounterAssertValue": {
Expand All @@ -29,8 +29,8 @@ expression: common_costs_estimate
"storage_rebate": 0
},
"SplitCoin": {
"computation_cost": 6811,
"storage_cost": 9763,
"computation_cost": 6825,
"storage_cost": 9784,
"storage_rebate": 0
},
"TransferPortionSuiCoin": {
Expand Down
30 changes: 30 additions & 0 deletions crates/sui-framework/docs/math.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Basic math for nicer programmability

- [Function `max`](#0x2_math_max)
- [Function `min`](#0x2_math_min)
- [Function `diff`](#0x2_math_diff)
- [Function `pow`](#0x2_math_pow)
- [Function `sqrt`](#0x2_math_sqrt)
- [Function `sqrt_u128`](#0x2_math_sqrt_u128)
Expand Down Expand Up @@ -73,6 +74,35 @@ Return the smaller of <code>x</code> and <code>y</code>



</details>

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

## Function `diff`

Return the absolute value of x - y


<pre><code><b>public</b> <b>fun</b> <a href="math.md#0x2_math_diff">diff</a>(x: u64, y: u64): u64
</code></pre>



<details>
<summary>Implementation</summary>


<pre><code><b>public</b> <b>fun</b> <a href="math.md#0x2_math_diff">diff</a>(x: u64, y: u64): u64 {
<b>if</b> (x &gt; y) {
x - y
} <b>else</b> {
y - x
}
}
</code></pre>



</details>

<a name="0x2_math_pow"></a>
Expand Down
38 changes: 32 additions & 6 deletions crates/sui-framework/docs/sui_system.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
- [Function `get_reporters_of`](#0x2_sui_system_get_reporters_of)
- [Function `extract_coin_balance`](#0x2_sui_system_extract_coin_balance)
- [Function `extract_locked_coin_balance`](#0x2_sui_system_extract_locked_coin_balance)
- [Function `validators`](#0x2_sui_system_validators)


<pre><code><b>use</b> <a href="">0x1::option</a>;
Expand Down Expand Up @@ -1104,13 +1105,13 @@ Extract required Balance from vector of Coin<SUI>, transfer the remainder back t
<b>let</b> amount = <a href="_destroy_some">option::destroy_some</a>(amount);
<b>let</b> <a href="balance.md#0x2_balance">balance</a> = <a href="balance.md#0x2_balance_split">balance::split</a>(&<b>mut</b> total_balance, amount);
// <a href="transfer.md#0x2_transfer">transfer</a> back the remainder <b>if</b> non zero.
<b>if</b> (<a href="balance.md#0x2_balance_value">balance::value</a>(&total_balance) &gt; 0){
<b>if</b> (<a href="balance.md#0x2_balance_value">balance::value</a>(&total_balance) &gt; 0) {
<a href="transfer.md#0x2_transfer_transfer">transfer::transfer</a>(<a href="coin.md#0x2_coin_from_balance">coin::from_balance</a>(total_balance, ctx), <a href="tx_context.md#0x2_tx_context_sender">tx_context::sender</a>(ctx));
}<b>else</b>{
} <b>else</b> {
<a href="balance.md#0x2_balance_destroy_zero">balance::destroy_zero</a>(total_balance);
};
<a href="balance.md#0x2_balance">balance</a>
}<b>else</b> {
} <b>else</b> {
total_balance
}
}
Expand Down Expand Up @@ -1157,18 +1158,43 @@ Extract required Balance from vector of LockedCoin<SUI>, transfer the remainder
<b>if</b> (<a href="_is_some">option::is_some</a>(&amount)){
<b>let</b> amount = <a href="_destroy_some">option::destroy_some</a>(amount);
<b>let</b> <a href="balance.md#0x2_balance">balance</a> = <a href="balance.md#0x2_balance_split">balance::split</a>(&<b>mut</b> total_balance, amount);
<b>if</b> (<a href="balance.md#0x2_balance_value">balance::value</a>(&total_balance) &gt; 0){
<b>if</b> (<a href="balance.md#0x2_balance_value">balance::value</a>(&total_balance) &gt; 0) {
<a href="locked_coin.md#0x2_locked_coin_new_from_balance">locked_coin::new_from_balance</a>(total_balance, first_lock, <a href="tx_context.md#0x2_tx_context_sender">tx_context::sender</a>(ctx), ctx);
}<b>else</b>{
} <b>else</b> {
<a href="balance.md#0x2_balance_destroy_zero">balance::destroy_zero</a>(total_balance);
};
(<a href="balance.md#0x2_balance">balance</a>, first_lock)
}<b>else</b>{
} <b>else</b>{
(total_balance, first_lock)
}
}
</code></pre>



</details>

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

## Function `validators`

Return the current validator set


<pre><code><b>public</b> <b>fun</b> <a href="sui_system.md#0x2_sui_system_validators">validators</a>(self: &<a href="sui_system.md#0x2_sui_system_SuiSystemState">sui_system::SuiSystemState</a>): &<a href="validator_set.md#0x2_validator_set_ValidatorSet">validator_set::ValidatorSet</a>
</code></pre>



<details>
<summary>Implementation</summary>


<pre><code><b>public</b> <b>fun</b> <a href="sui_system.md#0x2_sui_system_validators">validators</a>(self: &<a href="sui_system.md#0x2_sui_system_SuiSystemState">SuiSystemState</a>): &ValidatorSet {
&self.validators
}
</code></pre>



</details>
26 changes: 26 additions & 0 deletions crates/sui-framework/docs/validator.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
- [Function `sui_address`](#0x2_validator_sui_address)
- [Function `stake_amount`](#0x2_validator_stake_amount)
- [Function `delegate_amount`](#0x2_validator_delegate_amount)
- [Function `total_stake`](#0x2_validator_total_stake)
- [Function `pending_stake_amount`](#0x2_validator_pending_stake_amount)
- [Function `pending_withdraw`](#0x2_validator_pending_withdraw)
- [Function `gas_price`](#0x2_validator_gas_price)
Expand Down Expand Up @@ -789,6 +790,31 @@ Called by <code><a href="validator_set.md#0x2_validator_set">validator_set</a></



</details>

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

## Function `total_stake`

Return the total amount staked with this validator, including both validator stake and deledgated stake


<pre><code><b>public</b> <b>fun</b> <a href="validator.md#0x2_validator_total_stake">total_stake</a>(self: &<a href="validator.md#0x2_validator_Validator">validator::Validator</a>): u64
</code></pre>



<details>
<summary>Implementation</summary>


<pre><code><b>public</b> <b>fun</b> <a href="validator.md#0x2_validator_total_stake">total_stake</a>(self: &<a href="validator.md#0x2_validator_Validator">Validator</a>): u64 {
<a href="validator.md#0x2_validator_stake_amount">stake_amount</a>(self) + <a href="validator.md#0x2_validator_delegate_amount">delegate_amount</a>(self)
}
</code></pre>



</details>

<a name="0x2_validator_pending_stake_amount"></a>
Expand Down
28 changes: 27 additions & 1 deletion crates/sui-framework/docs/validator_set.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
- [Function `compute_reward_distribution`](#0x2_validator_set_compute_reward_distribution)
- [Function `distribute_reward`](#0x2_validator_set_distribute_reward)
- [Function `derive_next_epoch_validators`](#0x2_validator_set_derive_next_epoch_validators)
- [Function `active_validators`](#0x2_validator_set_active_validators)


<pre><code><b>use</b> <a href="">0x1::option</a>;
Expand Down Expand Up @@ -89,7 +90,7 @@
<code>total_delegation_stake: u64</code>
</dt>
<dd>
Total amount of stake from delegation, at the beginning of the epoch.
Total amount of stake from delegation, at the beginning of the epoch.
</dd>
<dt>
<code>quorum_stake_threshold: u64</code>
Expand Down Expand Up @@ -1483,4 +1484,29 @@ TODO: If we want to enforce a % on stake threshold, this is the function to do i



</details>

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

## Function `active_validators`

Return the active validators in <code>self</code>


<pre><code><b>public</b> <b>fun</b> <a href="validator_set.md#0x2_validator_set_active_validators">active_validators</a>(self: &<a href="validator_set.md#0x2_validator_set_ValidatorSet">validator_set::ValidatorSet</a>): &<a href="">vector</a>&lt;<a href="validator.md#0x2_validator_Validator">validator::Validator</a>&gt;
</code></pre>



<details>
<summary>Implementation</summary>


<pre><code><b>public</b> <b>fun</b> <a href="validator_set.md#0x2_validator_set_active_validators">active_validators</a>(self: &<a href="validator_set.md#0x2_validator_set_ValidatorSet">ValidatorSet</a>): &<a href="">vector</a>&lt;Validator&gt; {
&self.active_validators
}
</code></pre>



</details>
19 changes: 19 additions & 0 deletions crates/sui-framework/sources/governance/genesis.move
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,23 @@ module sui::genesis {
INIT_STAKE_SUBSIDY_AMOUNT,
);
}

#[test_only]
public fun create_for_testing(ctx: &mut TxContext) {
let chain_id = 1;
let validators = vector[];
let sui_supply = sui::new(ctx);
let storage_fund = balance::increase_supply(&mut sui_supply, INIT_STORAGE_FUND);

sui_system::create(
chain_id,
validators,
sui_supply,
storage_fund,
INIT_MAX_VALIDATOR_COUNT,
INIT_MIN_VALIDATOR_STAKE,
INIT_STORAGE_GAS_PRICE,
INIT_STAKE_SUBSIDY_AMOUNT
)
}
}
31 changes: 18 additions & 13 deletions crates/sui-framework/sources/governance/sui_system.move
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ module sui::sui_system {
parameters: SystemParameters,
/// The reference gas price for the current epoch.
reference_gas_price: u64,
/// A map storing the records of validator reporting each other during the current epoch.
/// A map storing the records of validator reporting each other during the current epoch.
/// There is an entry in the map for each validator that has been reported
/// at least once. The entry VecSet contains all the validators that reported
/// them. If a validator has never been reported they don't have an entry in this map.
Expand Down Expand Up @@ -358,7 +358,7 @@ module sui::sui_system {
// Both the reporter and the reported have to be validators.
assert!(validator_set::is_active_validator(&self.validators, sender), ENOT_VALIDATOR);
assert!(validator_set::is_active_validator(&self.validators, validator_addr), ENOT_VALIDATOR);
assert!(sender != validator_addr, ECANNOT_REPORT_ONESELF);
assert!(sender != validator_addr, ECANNOT_REPORT_ONESELF);

if (!vec_map::contains(&self.validator_report_records, &validator_addr)) {
vec_map::insert(&mut self.validator_report_records, validator_addr, vec_set::singleton(sender));
Expand Down Expand Up @@ -389,7 +389,7 @@ module sui::sui_system {
/// It does the following things:
/// 1. Add storage charge to the storage fund.
/// 2. Burn the storage rebates from the storage fund. These are already refunded to transaction sender's
/// gas coins.
/// gas coins.
/// 3. Distribute computation charge to validator stake and delegation stake.
/// 4. Update all validators.
public entry fun advance_epoch(
Expand All @@ -398,7 +398,7 @@ module sui::sui_system {
storage_charge: u64,
computation_charge: u64,
storage_rebate: u64,
storage_fund_reinvest_rate: u64, // share of storage fund's rewards that's reinvested
storage_fund_reinvest_rate: u64, // share of storage fund's rewards that's reinvested
// into storage fund, in basis point.
ctx: &mut TxContext,
) {
Expand All @@ -425,7 +425,7 @@ module sui::sui_system {

let storage_fund_reward_amount = (storage_fund_balance as u128) * computation_charge_u128 / total_stake_u128;
let storage_fund_reward = balance::split(&mut computation_reward, (storage_fund_reward_amount as u64));
let storage_fund_reinvestment_amount =
let storage_fund_reinvestment_amount =
storage_fund_reward_amount * (storage_fund_reinvest_rate as u128) / BASIS_POINT_DENOMINATOR;
let storage_fund_reinvestment = balance::split(
&mut storage_fund_reward,
Expand All @@ -447,7 +447,7 @@ module sui::sui_system {
// Derive the reference gas price for the new epoch
self.reference_gas_price = validator_set::derive_reference_gas_price(&self.validators);
// Because of precision issues with integer divisions, we expect that there will be some
// remaining balance in `delegator_reward`, `storage_fund_reward` and `computation_reward`.
// remaining balance in `delegator_reward`, `storage_fund_reward` and `computation_reward`.
// All of these go to the storage fund.
balance::join(&mut self.storage_fund, delegator_reward);
balance::join(&mut self.storage_fund, storage_fund_reward);
Expand All @@ -464,7 +464,7 @@ module sui::sui_system {

spec advance_epoch {
/// Total supply of SUI increases by the amount of stake subsidy we minted.
ensures balance::supply_value(self.sui_supply)
ensures balance::supply_value(self.sui_supply)
== old(balance::supply_value(self.sui_supply)) + old(stake_subsidy::current_epoch_subsidy_amount(self.stake_subsidy));
}

Expand Down Expand Up @@ -506,13 +506,13 @@ module sui::sui_system {
let amount = option::destroy_some(amount);
let balance = balance::split(&mut total_balance, amount);
// transfer back the remainder if non zero.
if (balance::value(&total_balance) > 0){
if (balance::value(&total_balance) > 0) {
transfer::transfer(coin::from_balance(total_balance, ctx), tx_context::sender(ctx));
}else{
} else {
balance::destroy_zero(total_balance);
};
balance
}else {
} else {
total_balance
}
}
Expand All @@ -539,17 +539,22 @@ module sui::sui_system {
if (option::is_some(&amount)){
let amount = option::destroy_some(amount);
let balance = balance::split(&mut total_balance, amount);
if (balance::value(&total_balance) > 0){
if (balance::value(&total_balance) > 0) {
locked_coin::new_from_balance(total_balance, first_lock, tx_context::sender(ctx), ctx);
}else{
} else {
balance::destroy_zero(total_balance);
};
(balance, first_lock)
}else{
} else{
(total_balance, first_lock)
}
}

/// Return the current validator set
public fun validators(self: &SuiSystemState): &ValidatorSet {
&self.validators
}

#[test_only]
public fun set_epoch_for_testing(self: &mut SuiSystemState, epoch_num: u64) {
self.epoch = epoch_num
Expand Down
5 changes: 5 additions & 0 deletions crates/sui-framework/sources/governance/validator.move
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,11 @@ module sui::validator {
staking_pool::sui_balance(&self.delegation_staking_pool)
}

/// Return the total amount staked with this validator, including both validator stake and deledgated stake
public fun total_stake(self: &Validator): u64 {
stake_amount(self) + delegate_amount(self)
}

public fun pending_stake_amount(self: &Validator): u64 {
self.pending_stake
}
Expand Down
7 changes: 6 additions & 1 deletion crates/sui-framework/sources/governance/validator_set.move
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ module sui::validator_set {
/// at the beginning of the epoch.
total_validator_stake: u64,

/// Total amount of stake from delegation, at the beginning of the epoch.
/// Total amount of stake from delegation, at the beginning of the epoch.
total_delegation_stake: u64,

/// The amount of accumulated stake to reach a quorum among all active validators.
Expand Down Expand Up @@ -683,6 +683,11 @@ module sui::validator_set {
result
}

/// Return the active validators in `self`
public fun active_validators(self: &ValidatorSet): &vector<Validator> {
&self.active_validators
}

#[test_only]
public fun destroy_for_testing(
self: ValidatorSet,
Expand Down
Loading

0 comments on commit 09a2035

Please sign in to comment.