Skip to content
Open
4 changes: 2 additions & 2 deletions Scarb.lock
Original file line number Diff line number Diff line change
Expand Up @@ -147,15 +147,15 @@ dependencies = [
[[package]]
name = "starkware_utils"
version = "1.0.0"
source = "git+https://github.com/starkware-libs/starkware-starknet-utils?rev=e1955423808045de868987b8fb0b43f5cbdf5699#e1955423808045de868987b8fb0b43f5cbdf5699"
source = "git+https://github.com/starkware-libs/starkware-starknet-utils?rev=7b46975d5612fb1b920bb248941030bf6c295d44#7b46975d5612fb1b920bb248941030bf6c295d44"
dependencies = [
"openzeppelin",
]

[[package]]
name = "starkware_utils_testing"
version = "1.0.0"
source = "git+https://github.com/starkware-libs/starkware-starknet-utils?rev=e1955423808045de868987b8fb0b43f5cbdf5699#e1955423808045de868987b8fb0b43f5cbdf5699"
source = "git+https://github.com/starkware-libs/starkware-starknet-utils?rev=7b46975d5612fb1b920bb248941030bf6c295d44#7b46975d5612fb1b920bb248941030bf6c295d44"
dependencies = [
"openzeppelin",
"snforge_std",
Expand Down
4 changes: 2 additions & 2 deletions Scarb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ edition = "2024_07"
[dependencies]
starknet = "2.12.0"
openzeppelin = "2.0.0"
starkware_utils = { git = "https://github.com/starkware-libs/starkware-starknet-utils", rev = "e1955423808045de868987b8fb0b43f5cbdf5699" }
starkware_utils = { git = "https://github.com/starkware-libs/starkware-starknet-utils", rev = "7b46975d5612fb1b920bb248941030bf6c295d44" }

[dev-dependencies]
assert_macros = "2.12.0"
snforge_std = "0.49.0"
starkware_utils_testing = { git = "https://github.com/starkware-libs/starkware-starknet-utils", rev = "e1955423808045de868987b8fb0b43f5cbdf5699" }
starkware_utils_testing = { git = "https://github.com/starkware-libs/starkware-starknet-utils", rev = "7b46975d5612fb1b920bb248941030bf6c295d44" }

[scripts]
test = "snforge test"
Expand Down
43 changes: 43 additions & 0 deletions docs/spec.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@
- [contract\_parameters\_v1](#contract_parameters_v1-2)
- [on\_receive](#on_receive)
- [get\_alpha](#get_alpha)
- [get\_block\_duration\_config](#get_block_duration_config)
- [set\_block\_duration\_config](#set_block_duration_config)
- [Events](#events-2)
- [Mint Request](#mint-request)
- [Minting Curve Contract](#minting-curve-contract)
Expand Down Expand Up @@ -237,6 +239,7 @@
- [CONSENSUS\_REWARDS\_IS\_ACTIVE](#consensus_rewards_is_active)
- [INVALID\_STAKER](#invalid_staker)
- [INVALID\_TOKEN\_DECIMALS](#invalid_token_decimals)
- [INVALID\_MIN\_MAX\_BLOCK\_DURATION](#invalid_min_max_block_duration)
- [Structs](#structs)
- [StakerPoolInfoV1](#stakerpoolinfov1)
- [StakerInfoV1](#stakerinfov1)
Expand All @@ -254,6 +257,7 @@
- [AttestationInfo](#attestationinfo)
- [EpochInfo](#epochinfo)
- [MintingCurveContractInfo](#mintingcurvecontractinfo)
- [BlockDurationConfig](#blockdurationconfig)
- [Type aliases](#type-aliases)
- [Amount](#amount)
- [Commission](#commission)
Expand Down Expand Up @@ -2344,6 +2348,36 @@ Returns the alpha parameter, as percentage, used when computing BTC rewards.
#### access control <!-- omit from toc -->
Any address can execute.

### get_block_duration_config
```rust
fn get_block_duration_config(self: @TContractState) -> BlockdurationConfig;
```
#### description <!-- omit from toc -->
Returns [BlockdurationConfig](#blockdurationconfig).
#### emits <!-- omit from toc -->
#### errors <!-- omit from toc -->
#### pre-condition <!-- omit from toc -->
#### logic <!-- omit from toc -->
#### access control <!-- omit from toc -->
Any address can execute.

### get_block_duration_config
```rust
fn set_block_duration_config(ref self: TContractState, block_duration_config: BlockdurationConfig);
```
#### description <!-- omit from toc -->
Set the block duration configuration.
#### emits <!-- omit from toc -->
#### errors <!-- omit from toc -->
1. [ONLY\_APP\_GOVERNOR](#only_app_governor)
2. [INVALID\_MIN\_MAX\_BLOCK\_duration](#invalid_min_max_block_duration)
#### pre-condition <!-- omit from toc -->
1. 0 < `block_duration_config.weighted_avg_factor` <= 100
2. 0 < `block_duration_config.min_block_duration` <= `block_duration_config.max_block_duration`
#### logic <!-- omit from toc -->
#### access control <!-- omit from toc -->
Only app governor.

## Events
### Mint Request
| data | type | keyed |
Expand Down Expand Up @@ -2797,6 +2831,9 @@ Only token admin.
### INVALID_STAKER
"Staker is invalid for getting rewards"

### INVALID_MIN_MAX_BLOCK_DURATION
"Invalid min/max block duration"

# Structs
### StakerPoolInfoV1
| name | type |
Expand Down Expand Up @@ -2918,6 +2955,12 @@ Only token admin.
| c_num | Inflation |
| c_denom | Inflation |

### BlockDurationConfig
| name | type |
| ------------------- | ---- |
| min_block_duration | u64 |
| max_block_duration | u64 |

# Type aliases
### Amount
Amount: u128
Expand Down
19 changes: 12 additions & 7 deletions src/flow_test/flow_ideas.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,6 @@
- member change balance and test with view of current epoch balance

## `update_rewards`
- staker with only btc pool.
- staker with empty pool (STRK + BTC).
- staker with 2 btc pools with different decimals.
- staker immediately/one epoch after he called intent.
- update rewards for 2 different blocks in the same epoch - should be same rewards.
- Change epoch len in blocks - rewards should be changed.
- disable_rewards = true, advance block, disable_rewards = false, advance block, disable_rewards = true, test rewards.
- update rewards for some different blocks in the same epoch, test rewards of pool member.
- staker change balance, attest, change balance, attest, set_v3, change balance, update_rewards, change_balance, update_rewards, test rewards.
- with member from previous versions.
Expand Down Expand Up @@ -49,3 +42,15 @@ more ideas:
- enable token, update rewards, advance epoch, update rewards, advance epoch, update rewards - token does not get rewards until after 2 epochs
- same as above with disable (can be implemented together as one test)
- enable token A and disable token B, next epoch upgrade, test views and rewards.

## block rewards by timestamp
- advance blocks with different block times and check the avg is calculated correctly
- update_rewards for blocks in same epoch - same rewards, then advance epoch, different rewards, update rewards for blocks in same epoch - same rewards.
- update rewards is not called every block, still rewards is updated correctly (miss block, miss first block in epoch, miss epoch)
- set block time config and test rewards after

## rewards by timestamp - migration
- set_consensus_rewards to future epoch, call update_rewards before consensus epoch and after, test rewards.
- set_consensus_rewards to future epoch, call update_rewards only after consensus epoch, test rewards.
- set_consensus_rewards to curr_epoch + 2. test rewards before and after. tets avg block time is update correctly.
- set_consensus_rewards, update_rewards, then set_consensus_rewards to later epoch, update_rewards, then set_consensus_rewards to earlier epoch, update_rewards, test avg block time.
29 changes: 13 additions & 16 deletions src/flow_test/flows.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,12 @@ use staking::staking::objects::{EpochInfoTrait, NormalizedAmountTrait, StakerVer
use staking::staking::staking::Staking::V3_PREV_CONTRACT_VERSION;
use staking::staking::utils::STRK_WEIGHT_FACTOR;
use staking::test_utils::constants::{
BTC_18D_CONFIG, BTC_8D_CONFIG, BTC_DECIMALS_18, BTC_DECIMALS_8, EPOCH_DURATION, PUBLIC_KEY,
STRK_BASE_VALUE, TEST_BTC_DECIMALS, TEST_MIN_BTC_FOR_REWARDS, TEST_ONE_BTC,
AVG_BLOCK_DURATION, BTC_18D_CONFIG, BTC_8D_CONFIG, BTC_DECIMALS_18, BTC_DECIMALS_8,
EPOCH_DURATION, PUBLIC_KEY, STRK_BASE_VALUE, TEST_BTC_DECIMALS, TEST_MIN_BTC_FOR_REWARDS,
TEST_ONE_BTC,
};
use staking::test_utils::{
calculate_pool_member_rewards, calculate_staker_btc_pool_rewards_v2,
advance_blocks, calculate_pool_member_rewards, calculate_staker_btc_pool_rewards_v2,
calculate_staker_strk_rewards_v2, calculate_staker_strk_rewards_with_balances_v2,
calculate_staker_strk_rewards_with_balances_v3, calculate_strk_pool_rewards_v1,
calculate_strk_pool_rewards_v2, calculate_strk_pool_rewards_v3,
Expand All @@ -50,9 +51,7 @@ use starkware_utils::errors::{Describable, ErrorDisplay};
use starkware_utils::math::abs::wide_abs_diff;
use starkware_utils::math::utils::mul_wide_and_div;
use starkware_utils::time::time::{Time, TimeDelta};
use starkware_utils_testing::test_utils::{
advance_block_number_global, assert_panic_with_error, cheat_caller_address_once,
};
use starkware_utils_testing::test_utils::{assert_panic_with_error, cheat_caller_address_once};

/// Flow - Basic Stake:
/// Staker - Stake with pool - cover if pool_enabled=true
Expand Down Expand Up @@ -8173,9 +8172,9 @@ pub(crate) impl DelegatorRewardsAttestationConsensusFlowImpl of FlowTrait<

system.advance_k_epochs();
system.update_rewards(:staker, disable_rewards: false);
advance_block_number_global(blocks: 1);
advance_blocks(blocks: 1, block_duration: AVG_BLOCK_DURATION);
system.update_rewards(:staker, disable_rewards: false);
advance_block_number_global(blocks: 1);
advance_blocks(blocks: 1, block_duration: AVG_BLOCK_DURATION);
system.update_rewards(:staker, disable_rewards: false);
let expected_rewards = calculate_strk_pool_rewards_v3(
staker_address: staker.staker.address, :staking_contract, :minting_curve_contract,
Expand Down Expand Up @@ -8297,9 +8296,9 @@ pub(crate) impl DelegatorChangeBalanceRewardsAttestationConsensusFlowImpl of Flo

system.advance_k_epochs();
system.update_rewards(:staker, disable_rewards: false);
advance_block_number_global(blocks: 1);
advance_blocks(blocks: 1, block_duration: AVG_BLOCK_DURATION);
system.update_rewards(:staker, disable_rewards: false);
advance_block_number_global(blocks: 1);
advance_blocks(blocks: 1, block_duration: AVG_BLOCK_DURATION);
system.update_rewards(:staker, disable_rewards: false);
let expected_rewards = calculate_strk_pool_rewards_v3(
staker_address: staker.staker.address, :staking_contract, :minting_curve_contract,
Expand Down Expand Up @@ -8371,7 +8370,6 @@ pub(crate) impl DelegatorBeforeUpgradeRewardsConsensusFlowImpl of FlowTrait<
amount_own: stake_amount,
pool_amount: pool_balance,
:commission,
:staking_contract,
:minting_curve_contract,
);
assert!(expected_rewards.is_non_zero());
Expand All @@ -8381,9 +8379,9 @@ pub(crate) impl DelegatorBeforeUpgradeRewardsConsensusFlowImpl of FlowTrait<

system.advance_k_epochs();
system.update_rewards(:staker, disable_rewards: false);
advance_block_number_global(blocks: 1);
advance_blocks(blocks: 1, block_duration: AVG_BLOCK_DURATION);
system.update_rewards(:staker, disable_rewards: false);
advance_block_number_global(blocks: 1);
advance_blocks(blocks: 1, block_duration: AVG_BLOCK_DURATION);
system.update_rewards(:staker, disable_rewards: false);
let expected_rewards = calculate_strk_pool_rewards_v3(
staker_address: staker.staker.address, :staking_contract, :minting_curve_contract,
Expand Down Expand Up @@ -8465,7 +8463,6 @@ pub(crate) impl DelegatorChangeBalanceBeforeUpgradeRewardsConsensusFlowImpl of F
amount_own: stake_amount,
pool_amount: pool_balance,
:commission,
:staking_contract,
:minting_curve_contract,
);
assert!(expected_rewards.is_non_zero());
Expand All @@ -8475,9 +8472,9 @@ pub(crate) impl DelegatorChangeBalanceBeforeUpgradeRewardsConsensusFlowImpl of F

system.advance_k_epochs();
system.update_rewards(:staker, disable_rewards: false);
advance_block_number_global(blocks: 1);
advance_blocks(blocks: 1, block_duration: AVG_BLOCK_DURATION);
system.update_rewards(:staker, disable_rewards: false);
advance_block_number_global(blocks: 1);
advance_blocks(blocks: 1, block_duration: AVG_BLOCK_DURATION);
system.update_rewards(:staker, disable_rewards: false);
let expected_rewards = calculate_strk_pool_rewards_v3(
staker_address: staker.staker.address, :staking_contract, :minting_curve_contract,
Expand Down
Loading