Skip to content
Open
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
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
24 changes: 12 additions & 12 deletions src/flow_test/flow_ideas.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,6 @@
- staker change balance and test with view of current epoch balance
- 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.

## k=1 -> k=2 balances
- delegator claim rewards when last change is in epoch + 2, then advance epochs and claim again to see no missing rewards
- same as above, also when there is change in epoch + 1
Expand Down Expand Up @@ -49,3 +37,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.
83 changes: 83 additions & 0 deletions src/flow_test/flows.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -9064,3 +9064,86 @@ pub(crate) impl StakerV1ChangeBalanceAttestFlowImpl of FlowTrait<StakerV1ChangeB
assert!(system.staker_claim_rewards(:staker) == expected_rewards);
}
}

/// Flow:
/// Staker stake.
/// Delegator delegate.
/// Upgrade to V3.
/// Start consensus rewards.
/// update_rewards - test rewards.
#[derive(Drop, Copy)]
pub(crate) struct UpdateRewardsWithOldDelegatorFlow {
pub(crate) staker: Option<Staker>,
pub(crate) stake_amount: Option<Amount>,
pub(crate) commission: Option<Commission>,
pub(crate) delegator: Option<Delegator>,
pub(crate) delegated_amount: Option<Amount>,
pub(crate) pool: Option<ContractAddress>,
}
pub(crate) impl UpdateRewardsWithOldDelegatorFlowImpl of MultiVersionFlowTrait<
UpdateRewardsWithOldDelegatorFlow,
> {
fn versions(self: UpdateRewardsWithOldDelegatorFlow) -> Span<ReleaseVersion> {
[V0, V1, V2].span()
}

fn setup(
ref self: UpdateRewardsWithOldDelegatorFlow,
ref system: SystemState,
version: ReleaseVersion,
) {
let stake_amount = system.staking.get_min_stake();
let delegated_amount = STRK_CONFIG.min_for_rewards;
let staker = system.new_staker(amount: stake_amount);
let commission = 200;
let delegator = system.new_delegator(amount: delegated_amount);

system.stake(:staker, amount: stake_amount, pool_enabled: true, :commission);
let pool = system.staking.get_pool(:staker);
system.delegate(:delegator, :pool, amount: delegated_amount);

system.set_pool_for_upgrade(pool_address: pool);
system.set_staker_for_migration(staker_address: staker.staker.address);
self.staker = Option::Some(staker);
self.stake_amount = Option::Some(stake_amount);
self.commission = Option::Some(commission);
self.delegator = Option::Some(delegator);
self.delegated_amount = Option::Some(delegated_amount);
self.pool = Option::Some(pool);
}

fn test(
self: UpdateRewardsWithOldDelegatorFlow, ref system: SystemState, version: ReleaseVersion,
) {
let staker = self.staker.unwrap();
let stake_amount = self.stake_amount.unwrap();
let commission = self.commission.unwrap();
let delegator = self.delegator.unwrap();
let delegated_amount = self.delegated_amount.unwrap();
let pool = self.pool.unwrap();

system.start_consensus_rewards();
system.update_rewards(:staker, disable_rewards: false);

// Calculate expected rewards.
let (expected_staker_rewards, expected_delegator_rewards) =
calculate_staker_strk_rewards_with_balances_v3(
amount_own: stake_amount,
pool_amount: delegated_amount,
:commission,
staking_contract: system.staking.address,
minting_curve_contract: system.minting_curve.address,
);
assert!(expected_staker_rewards.is_non_zero());
assert!(expected_delegator_rewards.is_non_zero());

// Test staker rewards.
let staker_rewards = system.staker_claim_rewards(:staker);
assert!(staker_rewards == expected_staker_rewards);

// Test delegator rewards.
system.advance_epoch();
let delegator_rewards = system.delegator_claim_rewards(delegator: delegator, pool: pool);
assert!(delegator_rewards == expected_delegator_rewards);
}
}
14 changes: 14 additions & 0 deletions src/flow_test/multi_version_tests.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -258,3 +258,17 @@ fn member_change_balance_claim_rewards_one_rewards_flow_test() {
};
test_multi_version_flow_mainnet(ref :flow);
}

#[test]
#[fork("MAINNET_LATEST")]
fn update_rewards_with_old_delegator_flow_test() {
let mut flow = flows::UpdateRewardsWithOldDelegatorFlow {
staker: Option::None,
stake_amount: Option::None,
commission: Option::None,
delegator: Option::None,
delegated_amount: Option::None,
pool: Option::None,
};
test_multi_version_flow_mainnet(ref :flow);
}
Loading