Skip to content

Commit d07d35f

Browse files
authored
feat: decouple x/distribution from simapp (#12334)
* feat: decouple `x/distribution` from simapp * updates
1 parent 6559c7a commit d07d35f

39 files changed

+915
-575
lines changed

CHANGELOG.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,10 @@ Ref: https://keepachangelog.com/en/1.0.0/
4949

5050
### API Breaking Changes
5151

52-
* (simapp) [#12312](https:12278//github.com/cosmos/cosmos-sdk/pull/12312) Move `simapp.EmptyAppOptions` to `simtestutil.EmptyAppOptions` (`testutil/sims`)
53-
* (simapp) [#12312](https:12278//github.com/cosmos/cosmos-sdk/pull/12312) Remove `skipUpgradeHeights map[int64]bool` and `homePath string` from `NewSimApp` constructor as per migration of `x/upgrade` to app-wiring.
54-
* (testutil) [#12278](https:12278//github.com/cosmos/cosmos-sdk/pull/12278) Move all functions from `simapp/helpers` to `testutil/sims`
52+
* (simapp) [#XXXXX](https://github.com/cosmos/cosmos-sdk/pull/XXXXX) Move `simapp.ConvertAddrsToValAddrs` and `simapp.CreateTestPubKeys ` to respectively `simtestutil.ConvertAddrsToValAddrs` and `simtestutil.CreateTestPubKeys` (`testutil/sims`)
53+
* (simapp) [#12312](https://github.com/cosmos/cosmos-sdk/pull/12312) Move `simapp.EmptyAppOptions` to `simtestutil.EmptyAppOptions` (`testutil/sims`)
54+
* (simapp) [#12312](https://github.com/cosmos/cosmos-sdk/pull/12312) Remove `skipUpgradeHeights map[int64]bool` and `homePath string` from `NewSimApp` constructor as per migration of `x/upgrade` to app-wiring.
55+
* (testutil) [#12278](https://github.com/cosmos/cosmos-sdk/pull/12278) Move all functions from `simapp/helpers` to `testutil/sims`
5556
* (testutil) [#12233](https://github.com/cosmos/cosmos-sdk/pull/12233) Move `simapp.TestAddr` to `simtestutil.TestAddr` (`testutil/sims`)
5657
* (x/staking) [#12102](https://github.com/cosmos/cosmos-sdk/pull/12102) Staking keeper now is passed by reference instead of copy. Keeper's SetHooks no longer returns keeper. It updates the keeper in place instead.
5758
* (linting) [#12141](https://github.com/cosmos/cosmos-sdk/pull/12141) Fix usability related linting for database. This means removing the infix Prefix from `prefix.NewPrefixWriter` and such so that it is `prefix.NewWriter` and making `db.DBConnection` and such into `db.Connection`

simapp/params/weights.go

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,18 @@ package params
22

33
// Default simulation operation weights for messages and gov proposals
44
const (
5-
DefaultWeightMsgSend int = 100
6-
DefaultWeightMsgMultiSend int = 10
7-
DefaultWeightMsgSetWithdrawAddress int = 50
8-
DefaultWeightMsgWithdrawDelegationReward int = 50
9-
DefaultWeightMsgWithdrawValidatorCommission int = 50
10-
DefaultWeightMsgFundCommunityPool int = 50
11-
DefaultWeightMsgDeposit int = 100
12-
DefaultWeightMsgVote int = 67
13-
DefaultWeightMsgVoteWeighted int = 33
14-
DefaultWeightMsgUnjail int = 100
15-
DefaultWeightMsgCreateValidator int = 100
16-
DefaultWeightMsgEditValidator int = 5
17-
DefaultWeightMsgDelegate int = 100
18-
DefaultWeightMsgUndelegate int = 100
19-
DefaultWeightMsgBeginRedelegate int = 100
20-
DefaultWeightMsgCancelUnbondingDelegation int = 100
21-
22-
DefaultWeightCommunitySpendProposal int = 5
23-
DefaultWeightTextProposal int = 5
5+
DefaultWeightMsgSend int = 100
6+
DefaultWeightMsgMultiSend int = 10
7+
DefaultWeightMsgDeposit int = 100
8+
DefaultWeightMsgVote int = 67
9+
DefaultWeightMsgVoteWeighted int = 33
10+
DefaultWeightMsgUnjail int = 100
11+
DefaultWeightMsgCreateValidator int = 100
12+
DefaultWeightMsgEditValidator int = 5
13+
DefaultWeightMsgDelegate int = 100
14+
DefaultWeightMsgUndelegate int = 100
15+
DefaultWeightMsgBeginRedelegate int = 100
16+
DefaultWeightMsgCancelUnbondingDelegation int = 100
2417

2518
// feegrant
2619
DefaultWeightGrantAllowance int = 100

simapp/test_helpers.go

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
package simapp
22

33
import (
4-
"bytes"
54
"context"
6-
"encoding/hex"
75
"encoding/json"
8-
"strconv"
96
"testing"
107

118
"github.com/stretchr/testify/require"
@@ -19,7 +16,6 @@ import (
1916
"cosmossdk.io/math"
2017
bam "github.com/cosmos/cosmos-sdk/baseapp"
2118
"github.com/cosmos/cosmos-sdk/client"
22-
"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
2319
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
2420
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
2521
"github.com/cosmos/cosmos-sdk/depinject"
@@ -28,7 +24,6 @@ import (
2824
"github.com/cosmos/cosmos-sdk/testutil/mock"
2925
simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims"
3026
sdk "github.com/cosmos/cosmos-sdk/types"
31-
"github.com/cosmos/cosmos-sdk/types/errors"
3227
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
3328
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
3429
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
@@ -247,17 +242,6 @@ func initAccountWithCoins(app *SimApp, ctx sdk.Context, addr sdk.AccAddress, coi
247242
}
248243
}
249244

250-
// ConvertAddrsToValAddrs converts the provided addresses to ValAddress.
251-
func ConvertAddrsToValAddrs(addrs []sdk.AccAddress) []sdk.ValAddress {
252-
valAddrs := make([]sdk.ValAddress, len(addrs))
253-
254-
for i, addr := range addrs {
255-
valAddrs[i] = sdk.ValAddress(addr)
256-
}
257-
258-
return valAddrs
259-
}
260-
261245
// CheckBalance checks the balance of an account.
262246
func CheckBalance(t *testing.T, app *SimApp, addr sdk.AccAddress, balances sdk.Coins) {
263247
ctxCheck := app.BaseApp.NewContext(true, tmproto.Header{})
@@ -347,35 +331,6 @@ func incrementAllSequenceNumbers(initSeqNums []uint64) {
347331
}
348332
}
349333

350-
// CreateTestPubKeys returns a total of numPubKeys public keys in ascending order.
351-
func CreateTestPubKeys(numPubKeys int) []cryptotypes.PubKey {
352-
var publicKeys []cryptotypes.PubKey
353-
var buffer bytes.Buffer
354-
355-
// start at 10 to avoid changing 1 to 01, 2 to 02, etc
356-
for i := 100; i < (numPubKeys + 100); i++ {
357-
numString := strconv.Itoa(i)
358-
buffer.WriteString("0B485CFC0EECC619440448436F8FC9DF40566F2369E72400281454CB552AF") // base pubkey string
359-
buffer.WriteString(numString) // adding on final two digits to make pubkeys unique
360-
publicKeys = append(publicKeys, NewPubKeyFromHex(buffer.String()))
361-
buffer.Reset()
362-
}
363-
364-
return publicKeys
365-
}
366-
367-
// NewPubKeyFromHex returns a PubKey from a hex string.
368-
func NewPubKeyFromHex(pk string) (res cryptotypes.PubKey) {
369-
pkBytes, err := hex.DecodeString(pk)
370-
if err != nil {
371-
panic(err)
372-
}
373-
if len(pkBytes) != ed25519.PubKeySize {
374-
panic(errors.Wrap(errors.ErrInvalidPubKey, "invalid pubkey size"))
375-
}
376-
return &ed25519.PubKey{Key: pkBytes}
377-
}
378-
379334
// ModuleAccountAddrs provides a list of blocked module accounts from configuration in app.yaml
380335
//
381336
// Ported from SimApp

testutil/sims/address_helpers.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,28 @@ package sims
22

33
import (
44
"bytes"
5+
"encoding/hex"
56
"fmt"
67
"strconv"
78

89
"cosmossdk.io/math"
910
"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
11+
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
1012
sdk "github.com/cosmos/cosmos-sdk/types"
13+
"github.com/cosmos/cosmos-sdk/types/errors"
1114
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
1215
minttypes "github.com/cosmos/cosmos-sdk/x/mint/types"
1316
stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper"
1417
)
1518

1619
type GenerateAccountStrategy func(int) []sdk.AccAddress
1720

21+
// AddTestAddrs constructs and returns accNum amount of accounts with an
22+
// initial balance of accAmt in random order
23+
func AddTestAddrs(bankKeeper bankkeeper.Keeper, stakingKeeper *stakingkeeper.Keeper, ctx sdk.Context, accNum int, accAmt math.Int) []sdk.AccAddress {
24+
return addTestAddrs(bankKeeper, stakingKeeper, ctx, accNum, accAmt, CreateRandomAccounts)
25+
}
26+
1827
// AddTestAddrsIncremental constructs and returns accNum amount of accounts with an initial balance of accAmt in random order
1928
func AddTestAddrsIncremental(bankKeeper bankkeeper.Keeper, stakingKeeper *stakingkeeper.Keeper, ctx sdk.Context, accNum int, accAmt math.Int) []sdk.AccAddress {
2029
return addTestAddrs(bankKeeper, stakingKeeper, ctx, accNum, accAmt, CreateIncrementalAccounts)
@@ -94,3 +103,43 @@ func TestAddr(addr string, bech string) (sdk.AccAddress, error) {
94103

95104
return res, nil
96105
}
106+
107+
// ConvertAddrsToValAddrs converts the provided addresses to ValAddress.
108+
func ConvertAddrsToValAddrs(addrs []sdk.AccAddress) []sdk.ValAddress {
109+
valAddrs := make([]sdk.ValAddress, len(addrs))
110+
111+
for i, addr := range addrs {
112+
valAddrs[i] = sdk.ValAddress(addr)
113+
}
114+
115+
return valAddrs
116+
}
117+
118+
// CreateTestPubKeys returns a total of numPubKeys public keys in ascending order.
119+
func CreateTestPubKeys(numPubKeys int) []cryptotypes.PubKey {
120+
var publicKeys []cryptotypes.PubKey
121+
var buffer bytes.Buffer
122+
123+
// start at 10 to avoid changing 1 to 01, 2 to 02, etc
124+
for i := 100; i < (numPubKeys + 100); i++ {
125+
numString := strconv.Itoa(i)
126+
buffer.WriteString("0B485CFC0EECC619440448436F8FC9DF40566F2369E72400281454CB552AF") // base pubkey string
127+
buffer.WriteString(numString) // adding on final two digits to make pubkeys unique
128+
publicKeys = append(publicKeys, NewPubKeyFromHex(buffer.String()))
129+
buffer.Reset()
130+
}
131+
132+
return publicKeys
133+
}
134+
135+
// NewPubKeyFromHex returns a PubKey from a hex string.
136+
func NewPubKeyFromHex(pk string) (res cryptotypes.PubKey) {
137+
pkBytes, err := hex.DecodeString(pk)
138+
if err != nil {
139+
panic(err)
140+
}
141+
if len(pkBytes) != ed25519.PubKeySize {
142+
panic(errors.Wrap(errors.ErrInvalidPubKey, "invalid pubkey size"))
143+
}
144+
return &ed25519.PubKey{Key: pkBytes}
145+
}

x/auth/migrations/v043/store_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
1010

1111
"github.com/cosmos/cosmos-sdk/simapp"
12+
simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims"
1213
sdk "github.com/cosmos/cosmos-sdk/types"
1314
authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper"
1415
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
@@ -648,8 +649,8 @@ func dirtyTrackingFields(ctx sdk.Context, vesting exported.VestingAccount, app *
648649
func createValidator(t *testing.T, ctx sdk.Context, app *simapp.SimApp, powers int64) (sdk.AccAddress, sdk.ValAddress) {
649650
valTokens := sdk.TokensFromConsensusPower(powers, sdk.DefaultPowerReduction)
650651
addrs := simapp.AddTestAddrsIncremental(app, ctx, 1, valTokens)
651-
valAddrs := simapp.ConvertAddrsToValAddrs(addrs)
652-
pks := simapp.CreateTestPubKeys(1)
652+
valAddrs := simtestutil.ConvertAddrsToValAddrs(addrs)
653+
pks := simtestutil.CreateTestPubKeys(1)
653654
cdc := simapp.MakeTestEncodingConfig().Codec
654655

655656
app.StakingKeeper = stakingkeeper.NewKeeper(

x/distribution/abci_test.go

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,12 @@ import (
55

66
"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
77
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
8-
"github.com/cosmos/cosmos-sdk/simapp"
8+
simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims"
99
sdk "github.com/cosmos/cosmos-sdk/types"
10+
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
11+
"github.com/cosmos/cosmos-sdk/x/distribution/keeper"
12+
"github.com/cosmos/cosmos-sdk/x/distribution/testutil"
13+
stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper"
1014
"github.com/cosmos/cosmos-sdk/x/staking/teststaking"
1115
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
1216
"github.com/stretchr/testify/require"
@@ -33,10 +37,23 @@ type validator struct {
3337

3438
// Context in https://github.com/cosmos/cosmos-sdk/issues/9161
3539
func TestVerifyProposerRewardAssignement(t *testing.T) {
36-
app := simapp.Setup(t, false)
40+
41+
var (
42+
bankKeeper bankkeeper.Keeper
43+
stakingKeeper *stakingkeeper.Keeper
44+
distrKeeper keeper.Keeper
45+
)
46+
47+
app, err := simtestutil.Setup(testutil.AppConfig,
48+
&bankKeeper,
49+
&stakingKeeper,
50+
&distrKeeper,
51+
)
52+
require.NoError(t, err)
53+
3754
ctx := app.BaseApp.NewContext(false, tmproto.Header{})
38-
addrs := simapp.AddTestAddrsIncremental(app, ctx, totalValidators, valTokens)
39-
tstaking := teststaking.NewHelper(t, ctx, app.StakingKeeper)
55+
addrs := simtestutil.AddTestAddrsIncremental(bankKeeper, stakingKeeper, ctx, totalValidators, valTokens)
56+
tstaking := teststaking.NewHelper(t, ctx, stakingKeeper)
4057
tstaking.Commission = validatorCommissionRates
4158

4259
// create validators
@@ -51,10 +68,10 @@ func TestVerifyProposerRewardAssignement(t *testing.T) {
5168
require.NotEmpty(t, app.Commit())
5269

5370
// verify validators lists
54-
require.Len(t, app.StakingKeeper.GetAllValidators(ctx), totalValidators)
71+
require.Len(t, stakingKeeper.GetAllValidators(ctx), totalValidators)
5572
for i, val := range validators {
5673
// verify all validator exists
57-
require.NotNil(t, app.StakingKeeper.ValidatorByConsAddr(ctx, sdk.GetConsAddress(val.pubkey)))
74+
require.NotNil(t, stakingKeeper.ValidatorByConsAddr(ctx, sdk.GetConsAddress(val.pubkey)))
5875

5976
// populate last commit info
6077
voteInfos := []abci.VoteInfo{}
@@ -99,9 +116,9 @@ func TestVerifyProposerRewardAssignement(t *testing.T) {
99116
})
100117
require.NotEmpty(t, app.Commit())
101118

102-
rewardsValidatorBeforeLazyValidator := app.DistrKeeper.GetValidatorOutstandingRewardsCoins(ctx, validators[lazyValidatorIdx+1].addr)
103-
rewardsLazyValidator := app.DistrKeeper.GetValidatorOutstandingRewardsCoins(ctx, validators[lazyValidatorIdx].addr)
104-
rewardsValidatorAfterLazyValidator := app.DistrKeeper.GetValidatorOutstandingRewardsCoins(ctx, validators[lazyValidatorIdx+1].addr)
119+
rewardsValidatorBeforeLazyValidator := distrKeeper.GetValidatorOutstandingRewardsCoins(ctx, validators[lazyValidatorIdx+1].addr)
120+
rewardsLazyValidator := distrKeeper.GetValidatorOutstandingRewardsCoins(ctx, validators[lazyValidatorIdx].addr)
121+
rewardsValidatorAfterLazyValidator := distrKeeper.GetValidatorOutstandingRewardsCoins(ctx, validators[lazyValidatorIdx+1].addr)
105122
require.True(t, rewardsLazyValidator[0].Amount.LT(rewardsValidatorAfterLazyValidator[0].Amount))
106123
require.Equal(t, rewardsValidatorBeforeLazyValidator, rewardsValidatorAfterLazyValidator)
107124
}

x/distribution/client/cli/tx_test.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,14 @@ import (
55

66
"github.com/spf13/pflag"
77

8+
"github.com/cosmos/cosmos-sdk/codec"
9+
"github.com/cosmos/cosmos-sdk/codec/types"
810
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
9-
"github.com/cosmos/cosmos-sdk/simapp/params"
10-
"github.com/cosmos/cosmos-sdk/testutil"
11+
sdktestutil "github.com/cosmos/cosmos-sdk/testutil"
1112
"github.com/cosmos/cosmos-sdk/testutil/testdata"
1213

1314
"github.com/stretchr/testify/require"
1415

15-
"github.com/stretchr/testify/assert"
16-
1716
"github.com/cosmos/cosmos-sdk/client"
1817
sdk "github.com/cosmos/cosmos-sdk/types"
1918
)
@@ -22,7 +21,7 @@ func Test_splitAndCall_NoMessages(t *testing.T) {
2221
clientCtx := client.Context{}
2322

2423
err := newSplitAndApply(nil, clientCtx, nil, nil, 10)
25-
assert.NoError(t, err, "")
24+
require.NoError(t, err, "")
2625
}
2726

2827
func Test_splitAndCall_Splitting(t *testing.T) {
@@ -47,27 +46,28 @@ func Test_splitAndCall_Splitting(t *testing.T) {
4746
func(clientCtx client.Context, fs *pflag.FlagSet, msgs ...sdk.Msg) error {
4847
callCount++
4948

50-
assert.NotNil(t, clientCtx)
51-
assert.NotNil(t, msgs)
49+
require.NotNil(t, clientCtx)
50+
require.NotNil(t, msgs)
5251

5352
if callCount < 3 {
54-
assert.Equal(t, len(msgs), 2)
53+
require.Equal(t, len(msgs), 2)
5554
} else {
56-
assert.Equal(t, len(msgs), 1)
55+
require.Equal(t, len(msgs), 1)
5756
}
5857

5958
return nil
6059
},
6160
clientCtx, nil, msgs, chunkSize)
6261

63-
assert.NoError(t, err, "")
64-
assert.Equal(t, 3, callCount)
62+
require.NoError(t, err, "")
63+
require.Equal(t, 3, callCount)
6564
}
6665

6766
func TestParseProposal(t *testing.T) {
68-
encodingConfig := params.MakeTestEncodingConfig()
67+
interfaceRegistry := types.NewInterfaceRegistry()
68+
cdc := codec.NewProtoCodec(interfaceRegistry)
6969

70-
okJSON := testutil.WriteToNewTempFile(t, `
70+
okJSON := sdktestutil.WriteToNewTempFile(t, `
7171
{
7272
"title": "Community Pool Spend",
7373
"description": "Pay me some Atoms!",
@@ -77,7 +77,7 @@ func TestParseProposal(t *testing.T) {
7777
}
7878
`)
7979

80-
proposal, err := ParseCommunityPoolSpendProposalWithDeposit(encodingConfig.Codec, okJSON.Name())
80+
proposal, err := ParseCommunityPoolSpendProposalWithDeposit(cdc, okJSON.Name())
8181
require.NoError(t, err)
8282

8383
require.Equal(t, "Community Pool Spend", proposal.Title)

0 commit comments

Comments
 (0)