github.com/cosmos/cosmos-sdk@v0.50.10/x/gov/common_test.go (about) 1 package gov_test 2 3 import ( 4 "bytes" 5 "log" 6 "sort" 7 "testing" 8 9 "github.com/stretchr/testify/require" 10 11 "cosmossdk.io/depinject" 12 sdklog "cosmossdk.io/log" 13 "cosmossdk.io/math" 14 15 "github.com/cosmos/cosmos-sdk/crypto/keys/ed25519" 16 cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" 17 "github.com/cosmos/cosmos-sdk/runtime" 18 "github.com/cosmos/cosmos-sdk/testutil/configurator" 19 simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims" 20 sdk "github.com/cosmos/cosmos-sdk/types" 21 _ "github.com/cosmos/cosmos-sdk/x/auth" 22 authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper" 23 authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" 24 _ "github.com/cosmos/cosmos-sdk/x/bank" 25 bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper" 26 _ "github.com/cosmos/cosmos-sdk/x/consensus" 27 _ "github.com/cosmos/cosmos-sdk/x/distribution" 28 distrkeeper "github.com/cosmos/cosmos-sdk/x/distribution/keeper" 29 "github.com/cosmos/cosmos-sdk/x/gov/keeper" 30 "github.com/cosmos/cosmos-sdk/x/gov/types" 31 v1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1" 32 "github.com/cosmos/cosmos-sdk/x/gov/types/v1beta1" 33 _ "github.com/cosmos/cosmos-sdk/x/params" 34 _ "github.com/cosmos/cosmos-sdk/x/staking" 35 stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper" 36 stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" 37 ) 38 39 var ( 40 valTokens = sdk.TokensFromConsensusPower(42, sdk.DefaultPowerReduction) 41 TestProposal = v1beta1.NewTextProposal("Test", "description") 42 TestDescription = stakingtypes.NewDescription("T", "E", "S", "T", "Z") 43 TestCommissionRates = stakingtypes.NewCommissionRates(math.LegacyZeroDec(), math.LegacyZeroDec(), math.LegacyZeroDec()) 44 ) 45 46 // mkTestLegacyContent creates a MsgExecLegacyContent for testing purposes. 47 func mkTestLegacyContent(t *testing.T) *v1.MsgExecLegacyContent { 48 msgContent, err := v1.NewLegacyContent(TestProposal, authtypes.NewModuleAddress(types.ModuleName).String()) 49 require.NoError(t, err) 50 51 return msgContent 52 } 53 54 // SortAddresses - Sorts Addresses 55 func SortAddresses(addrs []sdk.AccAddress) { 56 byteAddrs := make([][]byte, len(addrs)) 57 58 for i, addr := range addrs { 59 byteAddrs[i] = addr.Bytes() 60 } 61 62 SortByteArrays(byteAddrs) 63 64 for i, byteAddr := range byteAddrs { 65 addrs[i] = byteAddr 66 } 67 } 68 69 // implement `Interface` in sort package. 70 type sortByteArrays [][]byte 71 72 func (b sortByteArrays) Len() int { 73 return len(b) 74 } 75 76 func (b sortByteArrays) Less(i, j int) bool { 77 // bytes package already implements Comparable for []byte. 78 switch bytes.Compare(b[i], b[j]) { 79 case -1: 80 return true 81 case 0, 1: 82 return false 83 default: 84 log.Panic("not fail-able with `bytes.Comparable` bounded [-1, 1].") 85 return false 86 } 87 } 88 89 func (b sortByteArrays) Swap(i, j int) { 90 b[j], b[i] = b[i], b[j] 91 } 92 93 // SortByteArrays - sorts the provided byte array 94 func SortByteArrays(src [][]byte) [][]byte { 95 sorted := sortByteArrays(src) 96 sort.Sort(sorted) 97 return sorted 98 } 99 100 var pubkeys = []cryptotypes.PubKey{ 101 ed25519.GenPrivKey().PubKey(), 102 ed25519.GenPrivKey().PubKey(), 103 ed25519.GenPrivKey().PubKey(), 104 } 105 106 type suite struct { 107 AccountKeeper authkeeper.AccountKeeper 108 BankKeeper bankkeeper.Keeper 109 GovKeeper *keeper.Keeper 110 StakingKeeper *stakingkeeper.Keeper 111 DistributionKeeper distrkeeper.Keeper 112 App *runtime.App 113 } 114 115 func createTestSuite(t *testing.T) suite { 116 res := suite{} 117 118 app, err := simtestutil.SetupWithConfiguration( 119 depinject.Configs( 120 configurator.NewAppConfig( 121 configurator.ParamsModule(), 122 configurator.AuthModule(), 123 configurator.StakingModule(), 124 configurator.BankModule(), 125 configurator.GovModule(), 126 configurator.ConsensusModule(), 127 configurator.DistributionModule(), 128 ), 129 depinject.Supply(sdklog.NewNopLogger()), 130 ), 131 simtestutil.DefaultStartUpConfig(), 132 &res.AccountKeeper, &res.BankKeeper, &res.GovKeeper, &res.DistributionKeeper, &res.StakingKeeper, 133 ) 134 require.NoError(t, err) 135 136 res.App = app 137 return res 138 }