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

Add minor comment for superfluid reward distr #1104

Merged
merged 9 commits into from
Mar 30, 2022
Merged
Show file tree
Hide file tree
Changes from 6 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
21 changes: 15 additions & 6 deletions app/apptesting/test_suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,18 @@ func (keeperTestHelper *KeeperTestHelper) BeginNewBlock(executeNextEpoch bool) {
valAddr2, _ := validator.GetConsAddr()
valAddr = valAddr2.Bytes()
}
keeperTestHelper.BeginNewBlockWithProposer(executeNextEpoch, valAddr)
}

func (keeperTestHelper *KeeperTestHelper) BeginNewBlockWithProposer(executeNextEpoch bool, proposer sdk.ValAddress) {
valAddr := []byte(":^) at this distribution workaround")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the context behind this, just curious?

Copy link
Member

@ValarDragon ValarDragon Mar 28, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh it was referring to it being hard to simply simulate an app.go begin block for tests (thats what all this function is doing) It was a super late night for superfluid staking, trying to get this test working lol, we should probably delete the message.

e.g. this + the last commit info was really confusing to get working (distribution would panic w/o a clear message if it wasn't present)

valAddr := []byte(":^) at this distribution workaround") //nolint
	validators := keeperTestHelper.App.StakingKeeper.GetAllValidators(keeperTestHelper.Ctx)
	if len(validators) >= 1 {
		valAddrFancy, err := validators[0].GetConsAddr()
		keeperTestHelper.Require().NoError(err)
		valAddr = valAddrFancy.Bytes()
	} else {
		valAddrFancy := keeperTestHelper.SetupValidator(stakingtypes.Bonded)
		validator, _ := keeperTestHelper.App.StakingKeeper.GetValidator(keeperTestHelper.Ctx, valAddrFancy)
		valAddr2, _ := validator.GetConsAddr()
		valAddr = valAddr2.Bytes()
	}valAddr := []byte(":^) at this distribution workaround") //nolint
	validators := keeperTestHelper.App.StakingKeeper.GetAllValidators(keeperTestHelper.Ctx)
	if len(validators) >= 1 {
		valAddrFancy, err := validators[0].GetConsAddr()
		keeperTestHelper.Require().NoError(err)
		valAddr = valAddrFancy.Bytes()
	} else {
		valAddrFancy := keeperTestHelper.SetupValidator(stakingtypes.Bonded)
		validator, _ := keeperTestHelper.App.StakingKeeper.GetValidator(keeperTestHelper.Ctx, valAddrFancy)
		valAddr2, _ := validator.GetConsAddr()
		valAddr = valAddr2.Bytes()
	}

validator, found := keeperTestHelper.App.StakingKeeper.GetValidator(keeperTestHelper.Ctx, proposer)
if !found {
panic("given validator not found")
mattverse marked this conversation as resolved.
Show resolved Hide resolved
}
valConsAddr, err := validator.GetConsAddr()
keeperTestHelper.Require().NoError(err)
valAddr = valConsAddr.Bytes()

epochIdentifier := keeperTestHelper.App.SuperfluidKeeper.GetEpochIdentifier(keeperTestHelper.Ctx)
epoch := keeperTestHelper.App.EpochsKeeper.GetEpochInfo(keeperTestHelper.Ctx, epochIdentifier)
Expand All @@ -86,16 +98,13 @@ func (keeperTestHelper *KeeperTestHelper) BeginNewBlock(executeNextEpoch bool) {
endEpochTime := epoch.CurrentEpochStartTime.Add(epoch.Duration)
newBlockTime = endEpochTime.Add(time.Second)
}
// fmt.Println(executeNextEpoch, keeperTestHelper.Ctx.BlockTime(), newBlockTime)
header := tmproto.Header{Height: keeperTestHelper.Ctx.BlockHeight() + 1, Time: newBlockTime}
newCtx := keeperTestHelper.Ctx.WithBlockTime(newBlockTime).WithBlockHeight(keeperTestHelper.Ctx.BlockHeight() + 1)
keeperTestHelper.Ctx = newCtx
lastCommitInfo := abci.LastCommitInfo{
Votes: []abci.VoteInfo{
{
Validator: abci.Validator{Address: valAddr, Power: 1000},
SignedLastBlock: true,
},
Votes: []abci.VoteInfo{{
Validator: abci.Validator{Address: valAddr, Power: 1000},
SignedLastBlock: true},
mattverse marked this conversation as resolved.
Show resolved Hide resolved
},
}
reqBeginBlock := abci.RequestBeginBlock{Header: header, LastCommitInfo: lastCommitInfo}
Expand Down
66 changes: 46 additions & 20 deletions x/superfluid/keeper/hooks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,35 @@ func (suite *KeeperTestSuite) TestSuperfluidAfterEpochEnd() {
testCases := []struct {
name string
validatorStats []stakingtypes.BondStatus
delegatorNumber int
superDelegations []superfluidDelegation
expRewards sdk.Coins
expRewards []sdk.Coins
}{
{
"happy path with single validator and delegator",
[]stakingtypes.BondStatus{stakingtypes.Bonded},
1,
[]superfluidDelegation{{0, 0, 0, 1000000}},
sdk.Coins{{Amount: sdk.NewInt(999990), Denom: "stake"}},
// bond denom staked in pool = 15_000_000
// with risk adjustment, the actual bond denom staked via superfluid would be 15_000_000 * (1 - 0.5) = 7_500_000
// we do an arbitrary swap to set spot price, which adjusts superfluid staked equivilent base denom 20_000_000 * (1 - 0.5) = 10_000_000 during begin block
// delegation rewards are calculated using the equation (current period cumulative reward ratio - last period cumulative reward ratio) * asset amount
// in this test case, the calculation for expected reward would be the following (0.99999 - 0) * 10_000_000
// thus we expect 999_990 stake as rewards
[]sdk.Coins{{sdk.NewCoin("stake", sdk.NewInt(999990))}},
},
{
"happy path with two validator and delegator each",
[]stakingtypes.BondStatus{stakingtypes.Bonded, stakingtypes.Bonded},
2,
[]superfluidDelegation{{0, 0, 0, 1000000}, {1, 1, 0, 1000000}},
// reward for the first block propser / lock 0 that has been superfluid staked would be equivilent to calculations done above
// 999_990 stake as rewards.
// reward for the second delegation is expected to be different. Amount superfluid staked would be equivilently 7_500_000 stake.
// This would be the first block propsed by the second validator, current period cumulative reward ratio being 999_86.66684,
// last period cumulative reward ratio being 0
// Thus as rewards, we expect 999986stake, calculted using the following equation: (999_86.66684 - 0) * 7_500_000
[]sdk.Coins{{sdk.NewCoin("stake", sdk.NewInt(999990))}, {sdk.NewCoin("stake", sdk.NewInt(999986))}},
},
}

Expand All @@ -25,11 +46,10 @@ func (suite *KeeperTestSuite) TestSuperfluidAfterEpochEnd() {
suite.SetupTest()
valAddrs := suite.SetupValidators(tc.validatorStats)

// we create two additional pools: total three pools, 10 gauges
denoms, poolIds := suite.SetupGammPoolsAndSuperfluidAssets([]sdk.Dec{sdk.NewDec(20), sdk.NewDec(20)})
denoms, poolIds := suite.SetupGammPoolsAndSuperfluidAssets([]sdk.Dec{sdk.NewDec(20)})

// Generate delegator addresses
delAddrs := CreateRandomAccounts(1)
delAddrs := CreateRandomAccounts(tc.delegatorNumber)
intermediaryAccs, locks := suite.SetupSuperfluidDelegations(delAddrs, valAddrs, tc.superDelegations, denoms)
suite.checkIntermediaryAccountDelegations(intermediaryAccs)

Expand All @@ -40,24 +60,26 @@ func (suite *KeeperTestSuite) TestSuperfluidAfterEpochEnd() {
suite.SwapAndSetSpotPrice(poolIds[0], coins[1], coins[0])

// run epoch actions
suite.BeginNewBlock(true)
// run begin block for each validator so that both validator gets block rewards
for _, valAddr := range valAddrs {
suite.BeginNewBlockWithProposer(true, valAddr)
}

// check lptoken twap value set
newEpochMultiplier := suite.App.SuperfluidKeeper.GetOsmoEquivalentMultiplier(suite.Ctx, denoms[0])
suite.Require().Equal(newEpochMultiplier, sdk.NewDec(15))

// check gauge creation in new block
intermediaryAccAddr := suite.App.SuperfluidKeeper.GetLockIdIntermediaryAccountConnection(suite.Ctx, locks[0].ID)
intermediaryAcc := suite.App.SuperfluidKeeper.GetIntermediaryAccount(suite.Ctx, intermediaryAccAddr)
gauge, err := suite.App.IncentivesKeeper.GetGaugeByID(suite.Ctx, intermediaryAcc.GaugeId)

suite.Require().NoError(err)
suite.Require().Equal(gauge.Id, intermediaryAcc.GaugeId)
suite.Require().Equal(gauge.IsPerpetual, true)
suite.Require().Equal(gauge.Coins, tc.expRewards)
suite.Require().Equal(gauge.NumEpochsPaidOver, uint64(1))
suite.Require().Equal(gauge.FilledEpochs, uint64(1))
suite.Require().Equal(gauge.DistributedCoins, tc.expRewards)
for index, lock := range locks {
// check gauge creation in new block
intermediaryAccAddr := suite.App.SuperfluidKeeper.GetLockIdIntermediaryAccountConnection(suite.Ctx, lock.ID)
intermediaryAcc := suite.App.SuperfluidKeeper.GetIntermediaryAccount(suite.Ctx, intermediaryAccAddr)
gauge, err := suite.App.IncentivesKeeper.GetGaugeByID(suite.Ctx, intermediaryAcc.GaugeId)
suite.Require().NoError(err)
suite.Require().Equal(gauge.Id, intermediaryAcc.GaugeId)
suite.Require().Equal(gauge.IsPerpetual, true)
suite.Require().Equal(gauge.Coins, tc.expRewards[index])
suite.Require().Equal(gauge.DistributedCoins, tc.expRewards[index])
}

// check delegation changes
for _, acc := range intermediaryAccs {
Expand All @@ -67,8 +89,12 @@ func (suite *KeeperTestSuite) TestSuperfluidAfterEpochEnd() {
suite.Require().True(found)
suite.Require().Equal(sdk.NewDec(7500000), delegation.Shares)
}
balance := suite.App.BankKeeper.GetAllBalances(suite.Ctx, delAddrs[0])
suite.Require().Equal(tc.expRewards, balance)

// check balances
mattverse marked this conversation as resolved.
Show resolved Hide resolved
for index, delAddr := range delAddrs {
balance := suite.App.BankKeeper.GetAllBalances(suite.Ctx, delAddr)
suite.Require().Equal(tc.expRewards[index], balance)
}
})
}
}
Expand Down
1 change: 0 additions & 1 deletion x/superfluid/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ func (suite *KeeperTestSuite) SetupTest() {
queryHelper := baseapp.NewQueryServerTestHelper(suite.Ctx, suite.App.InterfaceRegistry())
types.RegisterQueryServer(queryHelper, suite.App.SuperfluidKeeper)
suite.queryClient = types.NewQueryClient(queryHelper)
suite.SetupDefaultPool()
ValarDragon marked this conversation as resolved.
Show resolved Hide resolved

unbondingDuration := suite.App.StakingKeeper.GetParams(suite.Ctx).UnbondingTime

Expand Down