Skip to content

Commit

Permalink
Apptesting: Change Interface to Struct (#1046)
Browse files Browse the repository at this point in the history
* Initial Commit

* Delete unnecessary lines

* Embed suite and keeper helper
  • Loading branch information
mattverse authored Mar 7, 2022
1 parent bff6f64 commit b27b5b2
Show file tree
Hide file tree
Showing 10 changed files with 270 additions and 271 deletions.
68 changes: 34 additions & 34 deletions app/apptesting/test_suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,70 +19,70 @@ import (
"github.com/stretchr/testify/suite"
)

// TODO: Consider an embedded struct here rather than an interface
type SuiteI interface {
GetSuite() *suite.Suite
GetCtx() sdk.Context
SetCtx(sdk.Context)
GetApp() *app.OsmosisApp
type KeeperTestHelper struct {
suite.Suite

App *app.OsmosisApp
Ctx sdk.Context
}

func SetupValidator(suite SuiteI, bondStatus stakingtypes.BondStatus) sdk.ValAddress {
func (keeperTestHelper *KeeperTestHelper) SetupValidator(bondStatus stakingtypes.BondStatus) sdk.ValAddress {
valPub := secp256k1.GenPrivKey().PubKey()
valAddr := sdk.ValAddress(valPub.Address())
bondDenom := suite.GetApp().StakingKeeper.GetParams(suite.GetCtx()).BondDenom
bondDenom := keeperTestHelper.App.StakingKeeper.GetParams(keeperTestHelper.Ctx).BondDenom
selfBond := sdk.NewCoins(sdk.Coin{Amount: sdk.NewInt(100), Denom: bondDenom})

err := simapp.FundAccount(suite.GetApp().BankKeeper, suite.GetCtx(), sdk.AccAddress(valAddr), selfBond)
suite.GetSuite().Require().NoError(err)
sh := teststaking.NewHelper(suite.GetSuite().T(), suite.GetCtx(), *suite.GetApp().StakingKeeper)
err := simapp.FundAccount(keeperTestHelper.App.BankKeeper, keeperTestHelper.Ctx, sdk.AccAddress(valAddr), selfBond)
keeperTestHelper.Require().NoError(err)

sh := teststaking.NewHelper(keeperTestHelper.Suite.T(), keeperTestHelper.Ctx, *keeperTestHelper.App.StakingKeeper)
msg := sh.CreateValidatorMsg(valAddr, valPub, selfBond[0].Amount)
sh.Handle(msg, true)
val, found := suite.GetApp().StakingKeeper.GetValidator(suite.GetCtx(), valAddr)
suite.GetSuite().Require().True(found)
val, found := keeperTestHelper.App.StakingKeeper.GetValidator(keeperTestHelper.Ctx, valAddr)
keeperTestHelper.Require().True(found)
val = val.UpdateStatus(bondStatus)
suite.GetApp().StakingKeeper.SetValidator(suite.GetCtx(), val)
keeperTestHelper.App.StakingKeeper.SetValidator(keeperTestHelper.Ctx, val)

consAddr, err := val.GetConsAddr()
suite.GetSuite().Require().NoError(err)
keeperTestHelper.Suite.Require().NoError(err)
signingInfo := slashingtypes.NewValidatorSigningInfo(
consAddr,
suite.GetCtx().BlockHeight(),
keeperTestHelper.Ctx.BlockHeight(),
0,
time.Unix(0, 0),
false,
0,
)
suite.GetApp().SlashingKeeper.SetValidatorSigningInfo(suite.GetCtx(), consAddr, signingInfo)
keeperTestHelper.App.SlashingKeeper.SetValidatorSigningInfo(keeperTestHelper.Ctx, consAddr, signingInfo)

return valAddr
}

func BeginNewBlock(suite SuiteI, executeNextEpoch bool) {
func (keeperTestHelper *KeeperTestHelper) BeginNewBlock(executeNextEpoch bool) {
valAddr := []byte(":^) at this distribution workaround")
validators := suite.GetApp().StakingKeeper.GetAllValidators(suite.GetCtx())
validators := keeperTestHelper.App.StakingKeeper.GetAllValidators(keeperTestHelper.Ctx)
if len(validators) >= 1 {
valAddrFancy, err := validators[0].GetConsAddr()
suite.GetSuite().Require().NoError(err)
keeperTestHelper.Require().NoError(err)
valAddr = valAddrFancy.Bytes()
} else {
valAddrFancy := SetupValidator(suite, stakingtypes.Bonded)
validator, _ := suite.GetApp().StakingKeeper.GetValidator(suite.GetCtx(), valAddrFancy)
valAddrFancy := keeperTestHelper.SetupValidator(stakingtypes.Bonded)
validator, _ := keeperTestHelper.App.StakingKeeper.GetValidator(keeperTestHelper.Ctx, valAddrFancy)
valAddr2, _ := validator.GetConsAddr()
valAddr = valAddr2.Bytes()
}

epochIdentifier := suite.GetApp().SuperfluidKeeper.GetEpochIdentifier(suite.GetCtx())
epoch := suite.GetApp().EpochsKeeper.GetEpochInfo(suite.GetCtx(), epochIdentifier)
newBlockTime := suite.GetCtx().BlockTime().Add(5 * time.Second)
epochIdentifier := keeperTestHelper.App.SuperfluidKeeper.GetEpochIdentifier(keeperTestHelper.Ctx)
epoch := keeperTestHelper.App.EpochsKeeper.GetEpochInfo(keeperTestHelper.Ctx, epochIdentifier)
newBlockTime := keeperTestHelper.Ctx.BlockTime().Add(5 * time.Second)
if executeNextEpoch {
endEpochTime := epoch.CurrentEpochStartTime.Add(epoch.Duration)
newBlockTime = endEpochTime.Add(time.Second)
}
// fmt.Println(executeNextEpoch, suite.ctx.BlockTime(), newBlockTime)
header := tmproto.Header{Height: suite.GetCtx().BlockHeight() + 1, Time: newBlockTime}
newCtx := suite.GetCtx().WithBlockTime(newBlockTime).WithBlockHeight(suite.GetCtx().BlockHeight() + 1)
suite.SetCtx(newCtx)
// 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},
Expand All @@ -91,11 +91,11 @@ func BeginNewBlock(suite SuiteI, executeNextEpoch bool) {
}
reqBeginBlock := abci.RequestBeginBlock{Header: header, LastCommitInfo: lastCommitInfo}

fmt.Println("beginning block ", suite.GetCtx().BlockHeight())
suite.GetApp().BeginBlocker(suite.GetCtx(), reqBeginBlock)
fmt.Println("beginning block ", keeperTestHelper.Ctx.BlockHeight())
keeperTestHelper.App.BeginBlocker(keeperTestHelper.Ctx, reqBeginBlock)
}

func EndBlock(suite SuiteI) {
reqEndBlock := abci.RequestEndBlock{Height: suite.GetCtx().BlockHeight()}
suite.GetApp().EndBlocker(suite.GetCtx(), reqEndBlock)
func (keeperTestHelper *KeeperTestHelper) EndBlock() {
reqEndBlock := abci.RequestEndBlock{Height: keeperTestHelper.Ctx.BlockHeight()}
keeperTestHelper.App.EndBlocker(keeperTestHelper.Ctx, reqEndBlock)
}
20 changes: 10 additions & 10 deletions x/superfluid/keeper/distribution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,19 @@ import (
)

func (suite *KeeperTestSuite) allocateRewardsToValidator(valAddr sdk.ValAddress) {
validator, found := suite.app.StakingKeeper.GetValidator(suite.ctx, valAddr)
validator, found := suite.App.StakingKeeper.GetValidator(suite.Ctx, valAddr)
suite.Require().True(found)

// allocate reward tokens to distribution module
coins := sdk.Coins{sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(20000))}
suite.app.BankKeeper.MintCoins(suite.ctx, minttypes.ModuleName, coins)
suite.app.BankKeeper.SendCoinsFromModuleToModule(suite.ctx, minttypes.ModuleName, distrtypes.ModuleName, coins)
suite.App.BankKeeper.MintCoins(suite.Ctx, minttypes.ModuleName, coins)
suite.App.BankKeeper.SendCoinsFromModuleToModule(suite.Ctx, minttypes.ModuleName, distrtypes.ModuleName, coins)

// allocate rewards to validator
suite.ctx = suite.ctx.WithBlockHeight(suite.ctx.BlockHeight() + 1)
suite.Ctx = suite.Ctx.WithBlockHeight(suite.Ctx.BlockHeight() + 1)
decTokens := sdk.DecCoins{{Denom: sdk.DefaultBondDenom, Amount: sdk.NewDec(20000)}}
suite.app.DistrKeeper.AllocateTokensToValidator(suite.ctx, validator, decTokens)
suite.app.DistrKeeper.IncrementValidatorPeriod(suite.ctx, validator)
suite.App.DistrKeeper.AllocateTokensToValidator(suite.Ctx, validator, decTokens)
suite.App.DistrKeeper.IncrementValidatorPeriod(suite.Ctx, validator)
}

func (suite *KeeperTestSuite) TestMoveSuperfluidDelegationRewardToGauges() {
Expand Down Expand Up @@ -88,19 +88,19 @@ func (suite *KeeperTestSuite) TestMoveSuperfluidDelegationRewardToGauges() {

// setup superfluid delegations
suite.SetupSuperfluidDelegations(delAddrs, valAddrs, tc.superDelegations)
unbondingDuration := suite.app.StakingKeeper.GetParams(suite.ctx).UnbondingTime
unbondingDuration := suite.App.StakingKeeper.GetParams(suite.Ctx).UnbondingTime

// allocate rewards to first validator
for _, valIndex := range tc.rewardedVals {
suite.allocateRewardsToValidator(valAddrs[valIndex])
}

// move intermediary account delegation rewards to gauges
suite.app.SuperfluidKeeper.MoveSuperfluidDelegationRewardToGauges(suite.ctx)
suite.App.SuperfluidKeeper.MoveSuperfluidDelegationRewardToGauges(suite.Ctx)

// check gauge balance
for _, gaugeCheck := range tc.gaugeChecks {
gauge, err := suite.app.IncentivesKeeper.GetGaugeByID(suite.ctx, gaugeCheck.gaugeId)
gauge, err := suite.App.IncentivesKeeper.GetGaugeByID(suite.Ctx, gaugeCheck.gaugeId)
suite.Require().NoError(err)
suite.Require().Equal(gauge.Id, gaugeCheck.gaugeId)
suite.Require().Equal(gauge.IsPerpetual, true)
Expand All @@ -114,7 +114,7 @@ func (suite *KeeperTestSuite) TestMoveSuperfluidDelegationRewardToGauges() {
} else {
suite.Require().True(gauge.Coins.AmountOf(sdk.DefaultBondDenom).IsZero())
}
suite.Require().Equal(gauge.StartTime, suite.ctx.BlockTime())
suite.Require().Equal(gauge.StartTime, suite.Ctx.BlockTime())
suite.Require().Equal(gauge.NumEpochsPaidOver, uint64(1))
suite.Require().Equal(gauge.FilledEpochs, uint64(0))
suite.Require().Equal(gauge.DistributedCoins, sdk.Coins(nil))
Expand Down
30 changes: 15 additions & 15 deletions x/superfluid/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

func (suite *KeeperTestSuite) TestGRPCParams() {
suite.SetupTest()
res, err := suite.app.SuperfluidKeeper.Params(sdk.WrapSDKContext(suite.ctx), &types.ParamsRequest{})
res, err := suite.App.SuperfluidKeeper.Params(sdk.WrapSDKContext(suite.Ctx), &types.ParamsRequest{})
suite.Require().NoError(err)
suite.Require().True(res.Params.MinimumRiskFactor.Equal(types.DefaultParams().MinimumRiskFactor))
}
Expand All @@ -18,22 +18,22 @@ func (suite *KeeperTestSuite) TestGRPCSuperfluidAsset() {
suite.SetupTest()

// initial check
assets := suite.app.SuperfluidKeeper.GetAllSuperfluidAssets(suite.ctx)
assets := suite.App.SuperfluidKeeper.GetAllSuperfluidAssets(suite.Ctx)
suite.Require().Len(assets, 0)

// set asset
suite.app.SuperfluidKeeper.SetSuperfluidAsset(suite.ctx, types.SuperfluidAsset{
suite.App.SuperfluidKeeper.SetSuperfluidAsset(suite.Ctx, types.SuperfluidAsset{
Denom: "gamm/pool/1",
AssetType: types.SuperfluidAssetTypeLPShare,
})

// get asset
res, err := suite.app.SuperfluidKeeper.AssetType(sdk.WrapSDKContext(suite.ctx), &types.AssetTypeRequest{Denom: "gamm/pool/1"})
res, err := suite.App.SuperfluidKeeper.AssetType(sdk.WrapSDKContext(suite.Ctx), &types.AssetTypeRequest{Denom: "gamm/pool/1"})
suite.Require().NoError(err)
suite.Require().Equal(res.AssetType, types.SuperfluidAssetTypeLPShare)

// check assets
resp, err := suite.app.SuperfluidKeeper.AllAssets(sdk.WrapSDKContext(suite.ctx), &types.AllAssetsRequest{})
resp, err := suite.App.SuperfluidKeeper.AllAssets(sdk.WrapSDKContext(suite.Ctx), &types.AllAssetsRequest{})
suite.Require().NoError(err)
suite.Require().Len(resp.Assets, 1)
}
Expand Down Expand Up @@ -65,7 +65,7 @@ func (suite *KeeperTestSuite) TestGRPCQuerySuperfluidDelegations() {

// for each superfluid delegation, query the amount and make sure it is 1000000
for _, delegation := range superfluidDelegations {
res, err := suite.queryClient.SuperfluidDelegationAmount(sdk.WrapSDKContext(suite.ctx), &types.SuperfluidDelegationAmountRequest{
res, err := suite.queryClient.SuperfluidDelegationAmount(sdk.WrapSDKContext(suite.Ctx), &types.SuperfluidDelegationAmountRequest{
DelegatorAddress: delAddrs[delegation.delIndex].String(),
ValidatorAddress: valAddrs[delegation.valIndex].String(),
Denom: delegation.lpDenom,
Expand All @@ -76,7 +76,7 @@ func (suite *KeeperTestSuite) TestGRPCQuerySuperfluidDelegations() {

// for each delegator, query all their superfluid delegations and make sure they have 2 delegations
for _, delegator := range delAddrs {
res, err := suite.queryClient.SuperfluidDelegationsByDelegator(sdk.WrapSDKContext(suite.ctx), &types.SuperfluidDelegationsByDelegatorRequest{
res, err := suite.queryClient.SuperfluidDelegationsByDelegator(sdk.WrapSDKContext(suite.Ctx), &types.SuperfluidDelegationsByDelegatorRequest{
DelegatorAddress: delegator.String(),
})
suite.Require().NoError(err)
Expand All @@ -90,15 +90,15 @@ func (suite *KeeperTestSuite) TestGRPCQuerySuperfluidDelegations() {
// for each validator denom pair, make sure they have 1 delegations
for _, validator := range valAddrs {
for _, denom := range denoms {
amountRes, err := suite.queryClient.EstimateSuperfluidDelegatedAmountByValidatorDenom(sdk.WrapSDKContext(suite.ctx), &types.EstimateSuperfluidDelegatedAmountByValidatorDenomRequest{
amountRes, err := suite.queryClient.EstimateSuperfluidDelegatedAmountByValidatorDenom(sdk.WrapSDKContext(suite.Ctx), &types.EstimateSuperfluidDelegatedAmountByValidatorDenomRequest{
ValidatorAddress: validator.String(),
Denom: denom,
})

suite.Require().NoError(err)
suite.Require().Equal(sdk.NewCoins(sdk.NewInt64Coin(denom, 1000000)), amountRes.TotalDelegatedCoins)

delegationsRes, err := suite.queryClient.SuperfluidDelegationsByValidatorDenom(sdk.WrapSDKContext(suite.ctx), &types.SuperfluidDelegationsByValidatorDenomRequest{
delegationsRes, err := suite.queryClient.SuperfluidDelegationsByValidatorDenom(sdk.WrapSDKContext(suite.Ctx), &types.SuperfluidDelegationsByValidatorDenomRequest{
ValidatorAddress: validator.String(),
Denom: denom,
})
Expand All @@ -107,7 +107,7 @@ func (suite *KeeperTestSuite) TestGRPCQuerySuperfluidDelegations() {
}
}

totalSuperfluidDelegationsRes, err := suite.queryClient.TotalSuperfluidDelegations(sdk.WrapSDKContext(suite.ctx), &types.TotalSuperfluidDelegationsRequest{})
totalSuperfluidDelegationsRes, err := suite.queryClient.TotalSuperfluidDelegations(sdk.WrapSDKContext(suite.Ctx), &types.TotalSuperfluidDelegationsRequest{})
suite.Require().NoError(err)
suite.Require().Equal(sdk.NewInt(40000000), totalSuperfluidDelegationsRes.TotalDelegations)

Expand Down Expand Up @@ -139,11 +139,11 @@ func (suite *KeeperTestSuite) TestGRPCQuerySuperfluidDelegationsDontIncludeUnbon
_, locks := suite.SetupSuperfluidDelegations(delAddrs, valAddrs, superfluidDelegations)

// start unbonding the superfluid delegations of denom0 from delegator0 to validator0
err := suite.app.SuperfluidKeeper.SuperfluidUndelegate(suite.ctx, locks[0].Owner, locks[0].ID)
err := suite.App.SuperfluidKeeper.SuperfluidUndelegate(suite.Ctx, locks[0].Owner, locks[0].ID)
suite.Require().NoError(err)

// query to make sure that the amount delegated for the now unbonding delegation is 0
res, err := suite.queryClient.SuperfluidDelegationAmount(sdk.WrapSDKContext(suite.ctx), &types.SuperfluidDelegationAmountRequest{
res, err := suite.queryClient.SuperfluidDelegationAmount(sdk.WrapSDKContext(suite.Ctx), &types.SuperfluidDelegationAmountRequest{
DelegatorAddress: delAddrs[0].String(),
ValidatorAddress: valAddrs[0].String(),
Denom: denoms[0],
Expand All @@ -152,7 +152,7 @@ func (suite *KeeperTestSuite) TestGRPCQuerySuperfluidDelegationsDontIncludeUnbon
suite.Require().Equal(res.Amount.AmountOf(denoms[0]).Int64(), int64(0))

// query to make sure that the unbonding delegation is not included in delegator query
res2, err := suite.queryClient.SuperfluidDelegationsByDelegator(sdk.WrapSDKContext(suite.ctx), &types.SuperfluidDelegationsByDelegatorRequest{
res2, err := suite.queryClient.SuperfluidDelegationsByDelegator(sdk.WrapSDKContext(suite.Ctx), &types.SuperfluidDelegationsByDelegatorRequest{
DelegatorAddress: delAddrs[0].String(),
})
suite.Require().NoError(err)
Expand All @@ -161,7 +161,7 @@ func (suite *KeeperTestSuite) TestGRPCQuerySuperfluidDelegationsDontIncludeUnbon
sdk.NewInt64Coin("gamm/pool/2", 1000000)), res2.TotalDelegatedCoins)

// query to make sure that the unbonding delegation is not included in the validator denom pair query
amountRes, err := suite.queryClient.EstimateSuperfluidDelegatedAmountByValidatorDenom(sdk.WrapSDKContext(suite.ctx), &types.EstimateSuperfluidDelegatedAmountByValidatorDenomRequest{
amountRes, err := suite.queryClient.EstimateSuperfluidDelegatedAmountByValidatorDenom(sdk.WrapSDKContext(suite.Ctx), &types.EstimateSuperfluidDelegatedAmountByValidatorDenomRequest{
ValidatorAddress: valAddrs[1].String(),
Denom: denoms[0],
})
Expand All @@ -171,7 +171,7 @@ func (suite *KeeperTestSuite) TestGRPCQuerySuperfluidDelegationsDontIncludeUnbon
sdk.NewInt64Coin(denoms[0], 1000000),
)))

delegationsRes, err := suite.queryClient.SuperfluidDelegationsByValidatorDenom(sdk.WrapSDKContext(suite.ctx), &types.SuperfluidDelegationsByValidatorDenomRequest{
delegationsRes, err := suite.queryClient.SuperfluidDelegationsByValidatorDenom(sdk.WrapSDKContext(suite.Ctx), &types.SuperfluidDelegationsByValidatorDenomRequest{
ValidatorAddress: valAddrs[1].String(),
Denom: denoms[0],
})
Expand Down
Loading

0 comments on commit b27b5b2

Please sign in to comment.