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

[wip] refactor: remove staking from testutil #20887

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
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
9 changes: 8 additions & 1 deletion testutil/network/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ import (
pruningtypes "cosmossdk.io/store/pruning/types"
authtypes "cosmossdk.io/x/auth/types"
banktypes "cosmossdk.io/x/bank/types"
stakingtypes "cosmossdk.io/x/staking/types"

stakingtypes "cosmossdk.io/x/staking/types"
"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
Expand Down Expand Up @@ -483,6 +483,13 @@ func New(l Logger, baseDir string, cfg Config) (NetworkI, error) {
return nil, err
}

// createValMsg, err := sims.FakeStakingMsgCreateValidator(
// sdk.ValAddress(addr).String(),
// valPubKeys[i],
// sdk.NewCoin(cfg.BondDenom, cfg.BondedTokens),
// commission, sdkmath.LegacyOneDec(), sdkmath.LegacyOneDec(),
// sdkmath.OneInt(),
// )
createValMsg, err := stakingtypes.NewMsgCreateValidator(
sdk.ValAddress(addr).String(),
valPubKeys[i],
Expand Down
42 changes: 29 additions & 13 deletions testutil/sims/app_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
sdkmath "cosmossdk.io/math"
authtypes "cosmossdk.io/x/auth/types"
banktypes "cosmossdk.io/x/bank/types"
stakingtypes "cosmossdk.io/x/staking/types"

"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/codec"
Expand All @@ -25,7 +24,9 @@
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
"github.com/cosmos/cosmos-sdk/runtime"
servertypes "github.com/cosmos/cosmos-sdk/server/types"
"github.com/cosmos/cosmos-sdk/testutil"
"github.com/cosmos/cosmos-sdk/testutil/mock"
"github.com/cosmos/cosmos-sdk/testutil/testdata/testpb"
sdk "github.com/cosmos/cosmos-sdk/types"
)

Expand Down Expand Up @@ -223,8 +224,8 @@
authGenesis := authtypes.NewGenesisState(authtypes.DefaultParams(), genAccs)
genesisState[authtypes.ModuleName] = codec.MustMarshalJSON(authGenesis)

validators := make([]stakingtypes.Validator, 0, len(valSet.Validators))
delegations := make([]stakingtypes.Delegation, 0, len(valSet.Validators))
validators := make([]testpb.StakingValidator, 0, len(valSet.Validators))

Check failure on line 227 in testutil/sims/app_helpers.go

View workflow job for this annotation

GitHub Actions / dependency-review

undefined: testpb.StakingValidator

Check failure on line 227 in testutil/sims/app_helpers.go

View workflow job for this annotation

GitHub Actions / tests (00)

undefined: testpb.StakingValidator

Check failure on line 227 in testutil/sims/app_helpers.go

View workflow job for this annotation

GitHub Actions / tests (00)

undefined: testpb.StakingValidator

Check failure on line 227 in testutil/sims/app_helpers.go

View workflow job for this annotation

GitHub Actions / tests (02)

undefined: testpb.StakingValidator

Check failure on line 227 in testutil/sims/app_helpers.go

View workflow job for this annotation

GitHub Actions / tests (03)

undefined: testpb.StakingValidator

Check failure on line 227 in testutil/sims/app_helpers.go

View workflow job for this annotation

GitHub Actions / tests (03)

undefined: testpb.StakingValidator

Check failure on line 227 in testutil/sims/app_helpers.go

View workflow job for this annotation

GitHub Actions / golangci-lint

undefined: testpb.StakingValidator

Check failure on line 227 in testutil/sims/app_helpers.go

View workflow job for this annotation

GitHub Actions / golangci-lint

undefined: testpb.StakingValidator
delegations := make([]testpb.StakingDelegation, 0, len(valSet.Validators))

Check failure on line 228 in testutil/sims/app_helpers.go

View workflow job for this annotation

GitHub Actions / dependency-review

undefined: testpb.StakingDelegation

Check failure on line 228 in testutil/sims/app_helpers.go

View workflow job for this annotation

GitHub Actions / tests (00)

undefined: testpb.StakingDelegation

Check failure on line 228 in testutil/sims/app_helpers.go

View workflow job for this annotation

GitHub Actions / tests (00)

undefined: testpb.StakingDelegation

Check failure on line 228 in testutil/sims/app_helpers.go

View workflow job for this annotation

GitHub Actions / tests (02)

undefined: testpb.StakingDelegation

Check failure on line 228 in testutil/sims/app_helpers.go

View workflow job for this annotation

GitHub Actions / tests (03)

undefined: testpb.StakingDelegation

Check failure on line 228 in testutil/sims/app_helpers.go

View workflow job for this annotation

GitHub Actions / tests (03)

undefined: testpb.StakingDelegation

Check failure on line 228 in testutil/sims/app_helpers.go

View workflow job for this annotation

GitHub Actions / golangci-lint

undefined: testpb.StakingDelegation

bondAmt := sdk.DefaultPowerReduction

Expand All @@ -239,27 +240,42 @@
return nil, fmt.Errorf("failed to create new any: %w", err)
}

validator := stakingtypes.Validator{
validator := testpb.StakingValidator{

Check failure on line 243 in testutil/sims/app_helpers.go

View workflow job for this annotation

GitHub Actions / dependency-review

undefined: testpb.StakingValidator

Check failure on line 243 in testutil/sims/app_helpers.go

View workflow job for this annotation

GitHub Actions / tests (00)

undefined: testpb.StakingValidator

Check failure on line 243 in testutil/sims/app_helpers.go

View workflow job for this annotation

GitHub Actions / tests (00)

undefined: testpb.StakingValidator

Check failure on line 243 in testutil/sims/app_helpers.go

View workflow job for this annotation

GitHub Actions / tests (02)

undefined: testpb.StakingValidator

Check failure on line 243 in testutil/sims/app_helpers.go

View workflow job for this annotation

GitHub Actions / tests (03)

undefined: testpb.StakingValidator

Check failure on line 243 in testutil/sims/app_helpers.go

View workflow job for this annotation

GitHub Actions / tests (03)

undefined: testpb.StakingValidator

Check failure on line 243 in testutil/sims/app_helpers.go

View workflow job for this annotation

GitHub Actions / golangci-lint

undefined: testpb.StakingValidator
OperatorAddress: sdk.ValAddress(val.Address).String(),
ConsensusPubkey: pkAny,
Jailed: false,
Status: stakingtypes.Bonded,
Status: 3,
Tokens: bondAmt,
DelegatorShares: sdkmath.LegacyOneDec(),
Description: stakingtypes.Description{},
UnbondingHeight: int64(0),
UnbondingTime: time.Unix(0, 0).UTC(),
Commission: stakingtypes.NewCommission(sdkmath.LegacyZeroDec(), sdkmath.LegacyZeroDec(), sdkmath.LegacyZeroDec()),
MinSelfDelegation: sdkmath.ZeroInt(),
}

validators = append(validators, validator)
delegations = append(delegations, stakingtypes.NewDelegation(genAccs[0].GetAddress().String(), sdk.ValAddress(val.Address).String(), sdkmath.LegacyOneDec()))

delegation := testpb.StakingDelegation{

Check failure on line 255 in testutil/sims/app_helpers.go

View workflow job for this annotation

GitHub Actions / dependency-review

undefined: testpb.StakingDelegation

Check failure on line 255 in testutil/sims/app_helpers.go

View workflow job for this annotation

GitHub Actions / tests (00)

undefined: testpb.StakingDelegation

Check failure on line 255 in testutil/sims/app_helpers.go

View workflow job for this annotation

GitHub Actions / tests (02)

undefined: testpb.StakingDelegation

Check failure on line 255 in testutil/sims/app_helpers.go

View workflow job for this annotation

GitHub Actions / tests (03)

undefined: testpb.StakingDelegation

Check failure on line 255 in testutil/sims/app_helpers.go

View workflow job for this annotation

GitHub Actions / golangci-lint

undefined: testpb.StakingDelegation
DelegatorAddress: genAccs[0].GetAddress().String(),
ValidatorAddress: sdk.ValAddress(val.Address).String(),
Shares: sdkmath.LegacyOneDec(),
}
delegations = append(delegations, delegation)
}

// set validators and delegations
stakingGenesis := stakingtypes.NewGenesisState(stakingtypes.DefaultParams(), validators, delegations)
genesisState[stakingtypes.ModuleName] = codec.MustMarshalJSON(stakingGenesis)
stakingGenesis := &testpb.StakingGenesisState{

Check failure on line 264 in testutil/sims/app_helpers.go

View workflow job for this annotation

GitHub Actions / dependency-review

undefined: testpb.StakingGenesisState

Check failure on line 264 in testutil/sims/app_helpers.go

View workflow job for this annotation

GitHub Actions / tests (00)

undefined: testpb.StakingGenesisState

Check failure on line 264 in testutil/sims/app_helpers.go

View workflow job for this annotation

GitHub Actions / tests (02)

undefined: testpb.StakingGenesisState

Check failure on line 264 in testutil/sims/app_helpers.go

View workflow job for this annotation

GitHub Actions / tests (03)

undefined: testpb.StakingGenesisState

Check failure on line 264 in testutil/sims/app_helpers.go

View workflow job for this annotation

GitHub Actions / golangci-lint

undefined: testpb.StakingGenesisState
Params: testpb.StakingParams{

Check failure on line 265 in testutil/sims/app_helpers.go

View workflow job for this annotation

GitHub Actions / dependency-review

undefined: testpb.StakingParams

Check failure on line 265 in testutil/sims/app_helpers.go

View workflow job for this annotation

GitHub Actions / tests (00)

undefined: testpb.StakingParams

Check failure on line 265 in testutil/sims/app_helpers.go

View workflow job for this annotation

GitHub Actions / tests (02)

undefined: testpb.StakingParams

Check failure on line 265 in testutil/sims/app_helpers.go

View workflow job for this annotation

GitHub Actions / tests (03)

undefined: testpb.StakingParams

Check failure on line 265 in testutil/sims/app_helpers.go

View workflow job for this annotation

GitHub Actions / golangci-lint

undefined: testpb.StakingParams
UnbondingTime: time.Hour * 24 * 7 * 3,
MaxValidators: 100,
MaxEntries: 7,
HistoricalEntries: 10000,
BondDenom: sdk.DefaultBondDenom,
MinCommissionRate: sdkmath.LegacyZeroDec(),
KeyRotationFee: sdk.NewCoin(sdk.DefaultBondDenom, sdkmath.NewInt(1000)),
},
Validators: validators,
Delegations: delegations,
}

genesisState[testutil.StakingModuleName] = codec.MustMarshalJSON(stakingGenesis)

totalSupply := sdk.NewCoins()
for _, b := range balances {
Expand All @@ -274,7 +290,7 @@

// add bonded amount to bonded pool module account
balances = append(balances, banktypes.Balance{
Address: authtypes.NewModuleAddress(stakingtypes.BondedPoolName).String(),
Address: authtypes.NewModuleAddress(StakingBondedPoolName).String(),
Coins: sdk.Coins{sdk.NewCoin(sdk.DefaultBondDenom, bondAmt)},
})

Expand Down
66 changes: 66 additions & 0 deletions testutil/sims/staking.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package sims

import (
"github.com/cosmos/gogoproto/proto"
any "github.com/cosmos/gogoproto/types/any"

"cosmossdk.io/math"

codectypes "github.com/cosmos/cosmos-sdk/codec/types"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
sdk "github.com/cosmos/cosmos-sdk/types"
)

const (
StakingNotBondedPoolName = "not_bonded_tokens_pool"
StakingBondedPoolName = "bonded_tokens_pool"
)

func FakeStakingMsgCreateValidator(
valAddr string, pubKey cryptotypes.PubKey, selfDelegation sdk.Coin,
rate, maxRate, maxChangeRate math.LegacyDec, minSelfDelegation math.Int,
) (proto.Message, error) {
var pkAny *codectypes.Any
if pubKey != nil {
var err error
if pkAny, err = codectypes.NewAnyWithValue(pubKey); err != nil {
return nil, err
}
}

_ = &StakingMsgCreateValidator{
Commission: StakingValidatorCommission{
Rate: rate,
MaxRate: maxRate,
MaxChangeRate: maxChangeRate,
},
MinSelfDelegation: minSelfDelegation,
DelegatorAddress: "",
ValidatorAddress: valAddr,
Pubkey: pkAny,
Value: selfDelegation,
}

return nil, nil // TODO
}

type StakingMsgCreateValidator struct {
Commission StakingValidatorCommission `protobuf:"bytes,2,opt,name=commission,proto3" json:"commission"`
MinSelfDelegation math.Int `protobuf:"bytes,3,opt,name=min_self_delegation,json=minSelfDelegation,proto3,customtype=cosmossdk.io/math.Int" json:"min_self_delegation"`
// Deprecated: Use of Delegator Address in MsgCreateValidator is deprecated.
// The validator address bytes and delegator address bytes refer to the same account while creating validator (defer
// only in bech32 notation).
DelegatorAddress string `protobuf:"bytes,4,opt,name=delegator_address,json=delegatorAddress,proto3" json:"delegator_address,omitempty"` // Deprecated: Do not use.
ValidatorAddress string `protobuf:"bytes,5,opt,name=validator_address,json=validatorAddress,proto3" json:"validator_address,omitempty"`
Pubkey *any.Any `protobuf:"bytes,6,opt,name=pubkey,proto3" json:"pubkey,omitempty"`
Value sdk.Coin `protobuf:"bytes,7,opt,name=value,proto3" json:"value"`
}

type StakingValidatorCommission struct {
// rate is the commission rate charged to delegators, as a fraction.
Rate math.LegacyDec `protobuf:"bytes,1,opt,name=rate,proto3,customtype=cosmossdk.io/math.LegacyDec" json:"rate"`
// max_rate defines the maximum commission rate which validator can ever charge, as a fraction.
MaxRate math.LegacyDec `protobuf:"bytes,2,opt,name=max_rate,json=maxRate,proto3,customtype=cosmossdk.io/math.LegacyDec" json:"max_rate"`
// max_change_rate defines the maximum daily increase of the validator commission, as a fraction.
MaxChangeRate math.LegacyDec `protobuf:"bytes,3,opt,name=max_change_rate,json=maxChangeRate,proto3,customtype=cosmossdk.io/math.LegacyDec" json:"max_change_rate"`
}
23 changes: 15 additions & 8 deletions testutil/sims/state_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@
"cosmossdk.io/math"
authtypes "cosmossdk.io/x/auth/types"
banktypes "cosmossdk.io/x/bank/types"
stakingtypes "cosmossdk.io/x/staking/types"

"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
"github.com/cosmos/cosmos-sdk/testutil"
"github.com/cosmos/cosmos-sdk/testutil/testdata/testpb"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
Expand Down Expand Up @@ -124,22 +124,28 @@
panic(err)
}

stakingStateBz, ok := rawState[stakingtypes.ModuleName]
stakingStateBz, ok := rawState[testutil.StakingModuleName]
if !ok {
panic("staking genesis state is missing")
}

stakingState := new(stakingtypes.GenesisState)
stakingState := new(testpb.StakingGenesisState)

Check failure on line 132 in testutil/sims/state_helpers.go

View workflow job for this annotation

GitHub Actions / dependency-review

undefined: testpb.StakingGenesisState

Check failure on line 132 in testutil/sims/state_helpers.go

View workflow job for this annotation

GitHub Actions / tests (00)

undefined: testpb.StakingGenesisState

Check failure on line 132 in testutil/sims/state_helpers.go

View workflow job for this annotation

GitHub Actions / tests (02)

undefined: testpb.StakingGenesisState

Check failure on line 132 in testutil/sims/state_helpers.go

View workflow job for this annotation

GitHub Actions / tests (03)

undefined: testpb.StakingGenesisState

Check failure on line 132 in testutil/sims/state_helpers.go

View workflow job for this annotation

GitHub Actions / golangci-lint

undefined: testpb.StakingGenesisState) (typecheck)
if err = cdc.UnmarshalJSON(stakingStateBz, stakingState); err != nil {
panic(err)
}
// compute not bonded balance
notBondedTokens := math.ZeroInt()
for _, val := range stakingState.Validators {
if val.Status != stakingtypes.Unbonded {
if val.Status != 1 { // unbonded
continue
}
notBondedTokens = notBondedTokens.Add(val.GetTokens())

tkn, err := sdk.ParseCoinNormalized(val.GetTokens())
if err != nil {
panic(err)
}

notBondedTokens = notBondedTokens.Add(tkn.Amount)
}
notBondedCoins := sdk.NewCoin(stakingState.Params.BondDenom, notBondedTokens)
// edit bank state to make it have the not bonded pool tokens
Expand All @@ -152,7 +158,7 @@
panic(err)
}

stakingAddr := authtypes.NewModuleAddress(stakingtypes.NotBondedPoolName).String()
stakingAddr := authtypes.NewModuleAddress(StakingNotBondedPoolName).String()
var found bool
for _, balance := range bankState.Balances {
if balance.Address == stakingAddr {
Expand All @@ -169,12 +175,13 @@

// change appState back
for name, state := range map[string]proto.Message{
stakingtypes.ModuleName: stakingState,
testutil.BankModuleName: bankState,
testutil.StakingModuleName: stakingState,
testutil.BankModuleName: bankState,
} {
if moduleStateCb != nil {
moduleStateCb(name, state)
}

rawState[name] = cdc.MustMarshalJSON(state)
}

Expand Down
3 changes: 1 addition & 2 deletions testutil/testdata/buf.gen.gogo.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,4 @@ version: v1
plugins:
- name: gocosmos
out: ../..
opt:
- plugins=grpc,Mgoogle/protobuf/any.proto=github.com/cosmos/gogoproto/types/any
opt: plugins=grpc,Mgoogle/protobuf/any.proto=github.com/cosmos/gogoproto/types/any
22 changes: 16 additions & 6 deletions testutil/testdata/buf.lock
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,25 @@ deps:
- remote: buf.build
owner: cosmos
repository: cosmos-sdk
commit: 9d547dbea90f47afbe1898388fcebffb
digest: shake256:63237398fb2043153c81bbe91ce52a832bca02d4307334b62fcc9914ce6f12fea59388eb5102949255054973f7022f581e02f97ed1f69a6585d2d00fb1da5833
commit: 05419252bcc241ea8023acf1ed4cadc5
digest: shake256:1e54a48c19a8b59d35e0a7efa76402939f515f2d8005df099856f24c37c20a52800308f025abb8cffcd014d437b49707388aaca4865d9d063d8f25d5d4eb77d5
- remote: buf.build
owner: cosmos
repository: gogo-proto
commit: 5e5b9fdd01804356895f8f79a6f1ddc1
digest: shake256:0b85da49e2e5f9ebc4806eae058e2f56096ff3b1c59d1fb7c190413dd15f45dd456f0b69ced9059341c80795d2b6c943de15b120a9e0308b499e43e4b5fc2952
commit: 88ef6483f90f478fb938c37dde52ece3
digest: shake256:89c45df2aa11e0cff97b0d695436713db3d993d76792e9f8dc1ae90e6ab9a9bec55503d48ceedd6b86069ab07d3041b32001b2bfe0227fa725dd515ff381e5ba
- remote: buf.build
owner: googleapis
repository: googleapis
commit: cc916c31859748a68fd229a3c8d7a2e8
digest: shake256:469b049d0eb04203d5272062636c078decefc96fec69739159c25d85349c50c34c7706918a8b216c5c27f76939df48452148cff8c5c3ae77fa6ba5c25c1b8bf8
commit: f0e53af8f2fc4556b94f482688b57223
digest: shake256:de26a277fc28b8b411ecf58729d78d32fcf15090ffd998a4469225b17889bfb51442eaab04bb7a8d88d203ecdf0a9febd4ffd52c18ed1c2229160c7bd353ca95
- remote: buf.build
owner: protocolbuffers
repository: wellknowntypes
commit: ee20af7d5b6044139bb9051283720274
digest: shake256:030e02269b2b3c080f716a133798cdaf2ca847192c63303cae27e996a26a0e433d0845c14063023ef98bf5750d0884987bc07b8babe903163d07ecbc3a30adb7
- remote: buf.build
owner: tendermint
repository: tendermint
commit: 33ed361a90514289beabf3189e1d7665
digest: shake256:038267e06294714fd883610626554b04a127b576b4e253befb4206cb72d5d3c1eeccacd4b9ec8e3fb891f7c14e1cb0f770c077d2989638995b0a61c85afedb1d
2 changes: 2 additions & 0 deletions testutil/testdata/buf.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ deps:
- buf.build/cosmos/cosmos-sdk
- buf.build/cosmos/gogo-proto
- buf.build/cosmos/cosmos-proto
- buf.build/googleapis/googleapis
- buf.build/protocolbuffers/wellknowntypes
Loading
Loading