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

feat(app)!: enforce min commission by changing default and genesis validation #1613

Merged
merged 4 commits into from
Sep 29, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* [#xxx](https://github.com/NibiruChain/nibiru/pull/xxx) - epic(tokenfactory):
State transitions, collections, genesis import and export, and app wiring

### State Machine Breaking

* [#1613](https://github.com/NibiruChain/nibiru/pull/1613) - feat(app)!: enforce min commission by changing default and genesis validation

## [v0.21.10]

### Features
Expand All @@ -59,6 +63,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* [#1559](https://github.com/NibiruChain/nibiru/pull/1559) - feat: add versions to markets to allow to disable them
* [#1585](https://github.com/NibiruChain/nibiru/pull/1585) - feat: include flag versioned in query markets to allow to query disabled markets
* [#1594](https://github.com/NibiruChain/nibiru/pull/1594) - feat: add user discounts

### Improvements

* [#1466](https://github.com/NibiruChain/nibiru/pull/1466) - refactor(perp): `PositionLiquidatedEvent`
Expand Down
26 changes: 26 additions & 0 deletions app/modules.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ package app

import (
"encoding/json"
"fmt"

sdkclient "github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
"github.com/cosmos/cosmos-sdk/x/bank"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
"github.com/cosmos/cosmos-sdk/x/crisis"
Expand Down Expand Up @@ -57,13 +60,36 @@ type StakingModule struct {
staking.AppModuleBasic
}

var _ module.HasGenesisBasics = (*StakingModule)(nil)

// DefaultGenesis returns custom Nibiru x/staking module genesis state.
func (StakingModule) DefaultGenesis(cdc codec.JSONCodec) json.RawMessage {
genState := stakingtypes.DefaultGenesisState()
genState.Params.BondDenom = BondDenom
genState.Params.MinCommissionRate = sdk.MustNewDecFromStr("0.05")
return cdc.MustMarshalJSON(genState)
}

// ValidateGenesis: Verifies that the provided staking genesis state holds
// expected invariants. I.e., params in correct bounds, no duplicate validators.
// This implements the module.HasGenesisBasics interface and gets called during
// the setup of the chain's module.BasicManager.
func (StakingModule) ValidateGenesis(
cdc codec.JSONCodec, txConfig sdkclient.TxEncodingConfig, bz json.RawMessage,
) error {
gen := new(stakingtypes.GenesisState)
if err := cdc.UnmarshalJSON(bz, gen); err != nil {
return fmt.Errorf("failed to unmarshal %s genesis state: %s", stakingtypes.ModuleName, bz)
}
if !gen.Params.MinCommissionRate.IsPositive() {
return fmt.Errorf(
"staking.params.min_commission must be positive (preferably >= 0.05): found value of %s",
gen.Params.MinCommissionRate.String(),
)
}
return staking.ValidateGenesis(gen)
}

// CrisisModule defines a custom wrapper around the x/crisis module's
// AppModuleBasic implementation to provide custom default genesis state.
type CrisisModule struct {
Expand Down
76 changes: 76 additions & 0 deletions app/modules_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package app_test

import (
"testing"

sdk "github.com/cosmos/cosmos-sdk/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
"github.com/stretchr/testify/suite"

"github.com/NibiruChain/nibiru/app"
)

type TestSuite struct {
suite.Suite

encCfg app.EncodingConfig
}

func TestAppTestSuite(t *testing.T) {
suite.Run(t, new(TestSuite))
}

func (s *TestSuite) SetupSuite() {
s.encCfg = app.MakeEncodingConfigAndRegister()
}

func (s *TestSuite) DefaultGenesisCopy() app.GenesisState {
return app.NewDefaultGenesisState(s.encCfg.Marshaler)
}

func (s *TestSuite) TestGenesis() {
getDefaultStakingGenesis := func() *stakingtypes.GenesisState {
genStaking := new(stakingtypes.GenesisState)
s.encCfg.Marshaler.MustUnmarshalJSON(
app.StakingModule{}.DefaultGenesis(s.encCfg.Marshaler),
genStaking,
)
return genStaking
}

gens := []*stakingtypes.GenesisState{}
gens = append(gens, getDefaultStakingGenesis())

genStaking := getDefaultStakingGenesis()
genStaking.Params.MinCommissionRate = sdk.ZeroDec()
gens = append(gens, genStaking)

for _, tc := range []struct {
name string
gen *stakingtypes.GenesisState
wantErr string
}{
{
name: "default should work fine",
gen: gens[0]},
{
name: "zero commission should fail",
gen: gens[1],
wantErr: "min_commission must be positive",
},
} {
s.T().Run(tc.name, func(t *testing.T) {
genStakingJson := s.encCfg.Marshaler.MustMarshalJSON(tc.gen)
err := app.StakingModule{}.ValidateGenesis(
s.encCfg.Marshaler,
s.encCfg.TxConfig,
genStakingJson,
)
if tc.wantErr != "" {
s.ErrorContains(err, tc.wantErr)
return
}
s.NoError(err)
})
}
}
2 changes: 1 addition & 1 deletion app/config.go → app/sim/config.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package app
package sim

import (
"flag"
Expand Down
3 changes: 2 additions & 1 deletion cmd/nibid/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,11 +264,12 @@ func (a appCreator) appExport(
return servertypes.ExportedApp{}, errors.New("application home is not set")
}

loadLatestHeight := height == -1
nibiruApp = app.NewNibiruApp(
logger,
db,
traceStore,
height == -1,
loadLatestHeight,
a.encCfg,
appOpts,
)
Expand Down
3 changes: 2 additions & 1 deletion simapp/sim_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@ import (
"github.com/stretchr/testify/require"

"github.com/NibiruChain/nibiru/app"
appsim "github.com/NibiruChain/nibiru/app/sim"
"github.com/NibiruChain/nibiru/x/common/testutil/testapp"
)

// SimAppChainID hardcoded chainID for simulation
const SimAppChainID = "simulation-app"

func init() {
app.GetSimulatorFlags()
appsim.GetSimulatorFlags()
}

func TestFullAppSimulation(tb *testing.T) {
Expand Down
7 changes: 4 additions & 3 deletions simapp/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"

"github.com/NibiruChain/nibiru/app"
appsim "github.com/NibiruChain/nibiru/app/sim"
)

// AppStateFn returns the initial application state using a genesis or the simulation parameters.
Expand All @@ -28,10 +29,10 @@ import (
func AppStateFn(cdc codec.JSONCodec, simManager *module.SimulationManager) simtypes.AppStateFn {
return func(r *rand.Rand, accs []simtypes.Account, config simtypes.Config,
) (appState json.RawMessage, simAccs []simtypes.Account, chainID string, genesisTimestamp time.Time) {
if app.FlagGenesisTimeValue == 0 {
if appsim.FlagGenesisTimeValue == 0 {
genesisTimestamp = simtypes.RandTimestamp(r)
} else {
genesisTimestamp = time.Unix(app.FlagGenesisTimeValue, 0)
genesisTimestamp = time.Unix(appsim.FlagGenesisTimeValue, 0)
}

chainID = config.ChainID
Expand All @@ -43,7 +44,7 @@ func AppStateFn(cdc codec.JSONCodec, simManager *module.SimulationManager) simty
// override the default chain-id from simapp to set it later to the config
genesisDoc, accounts := app.AppStateFromGenesisFileFn(r, cdc, config.GenesisFile)

if app.FlagGenesisTimeValue == 0 {
if appsim.FlagGenesisTimeValue == 0 {
// use genesis timestamp if no custom timestamp is provided (i.e no random timestamp)
genesisTimestamp = genesisDoc.GenesisTime
}
Expand Down