Skip to content

Commit

Permalink
test: remaining incentive module unit tests (#2061)
Browse files Browse the repository at this point in the history
* genesis validate tests

* ++

* ++

* genesis struct tests

* test++

* validate genesis on init

* comment out test due to experimental

* genesis test

* grpc query tests

* hooks test and fix

* verify donate collateral in MsgEmergencyUnbond

* todo--

* suggestion++

Co-authored-by: Robert Zaremba <[email protected]>

* suggestion++

Co-authored-by: Robert Zaremba <[email protected]>

* update to v5

* use coin fixtures

* remove repeated string

* remove experimental flag from incentive, thus fix tests

* add parallel

* lint

---------

Co-authored-by: Robert Zaremba <[email protected]>
  • Loading branch information
toteki and robert-zaremba authored Jun 19, 2023
1 parent 8e9cd5d commit 8e7fc9d
Show file tree
Hide file tree
Showing 19 changed files with 779 additions and 93 deletions.
44 changes: 14 additions & 30 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,10 +196,7 @@ func init() {
uibcmodule.AppModuleBasic{},
ugovmodule.AppModuleBasic{},
wasm.AppModuleBasic{},
}

if Experimental {
moduleBasics = append(moduleBasics, incentivemodule.AppModuleBasic{})
incentivemodule.AppModuleBasic{},
}

ModuleBasics = module.NewBasicManager(moduleBasics...)
Expand Down Expand Up @@ -337,10 +334,7 @@ func New(
leveragetypes.StoreKey, oracletypes.StoreKey,
bech32ibctypes.StoreKey, uibc.StoreKey, ugov.StoreKey,
wasm.StoreKey,
}

if Experimental {
storeKeys = append(storeKeys, incentive.StoreKey)
incentive.StoreKey,
}

keys := sdk.NewKVStoreKeys(storeKeys...)
Expand Down Expand Up @@ -478,15 +472,13 @@ func New(

app.LeverageKeeper.SetTokenHooks(app.OracleKeeper.Hooks())

if Experimental {
app.IncentiveKeeper = incentivekeeper.NewKeeper(
appCodec,
keys[incentive.StoreKey],
app.BankKeeper,
app.LeverageKeeper,
)
app.LeverageKeeper.SetBondHooks(app.IncentiveKeeper.BondHooks())
}
app.IncentiveKeeper = incentivekeeper.NewKeeper(
appCodec,
keys[incentive.StoreKey],
app.BankKeeper,
app.LeverageKeeper,
)
app.LeverageKeeper.SetBondHooks(app.IncentiveKeeper.BondHooks())

app.UGovKeeperB = ugovkeeper.NewKeeperBuilder(appCodec, keys[ugov.ModuleName])

Expand Down Expand Up @@ -720,12 +712,7 @@ func New(
uibcmodule.NewAppModule(appCodec, app.UIbcQuotaKeeperB),
ugovmodule.NewAppModule(appCodec, app.UGovKeeperB),
wasm.NewAppModule(app.appCodec, &app.WasmKeeper, app.StakingKeeper, app.AccountKeeper, app.BankKeeper),
}
if Experimental {
appModules = append(
appModules,
incentivemodule.NewAppModule(appCodec, app.IncentiveKeeper, app.BankKeeper, app.LeverageKeeper),
)
incentivemodule.NewAppModule(appCodec, app.IncentiveKeeper, app.BankKeeper, app.LeverageKeeper),
}

app.mm = module.NewManager(appModules...)
Expand All @@ -752,6 +739,7 @@ func New(
uibc.ModuleName,
ugov.ModuleName,
wasm.ModuleName,
incentive.ModuleName,
}

endBlockers := []string{
Expand All @@ -771,6 +759,7 @@ func New(
uibc.ModuleName,
ugov.ModuleName,
wasm.ModuleName,
incentive.ModuleName,
}

// NOTE: The genutils module must occur after staking so that pools are
Expand All @@ -795,6 +784,7 @@ func New(
uibc.ModuleName,
ugov.ModuleName,
wasm.ModuleName,
incentive.ModuleName,
}

orderMigrations := []string{
Expand All @@ -812,13 +802,7 @@ func New(
uibc.ModuleName,
ugov.ModuleName,
wasm.ModuleName,
}

if Experimental {
beginBlockers = append(beginBlockers, incentive.ModuleName)
endBlockers = append(endBlockers, incentive.ModuleName)
initGenesis = append(initGenesis, incentive.ModuleName)
orderMigrations = append(orderMigrations, incentive.ModuleName)
incentive.ModuleName,
}

app.mm.SetOrderBeginBlockers(beginBlockers...)
Expand Down
8 changes: 4 additions & 4 deletions app/upgrades.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,8 @@ func (app UmeeApp) RegisterUpgradeHandlers(bool) {
app.registerUpgrade4_3(upgradeInfo)
app.registerUpgrade("v4.4", upgradeInfo)
app.registerUpgrade("v5.0", upgradeInfo, ugov.ModuleName, wasm.ModuleName)
if Experimental {
app.registerUpgrade("v4.5-alpha1", upgradeInfo, incentive.ModuleName) // TODO: set correct name
}
app.registerUpgrade("v5.1-alpha1", upgradeInfo, incentive.ModuleName)
// TODO: set correct 5.1 name and add borrowFactor migration
}

// performs upgrade from v4.2 to v4.3
Expand Down Expand Up @@ -260,6 +259,7 @@ func (app *UmeeApp) registerUpgrade(planName string, upgradeInfo upgradetypes.Pl

if len(newStores) > 0 {
app.storeUpgrade(planName, upgradeInfo, storetypes.StoreUpgrades{
Added: newStores})
Added: newStores,
})
}
}
2 changes: 1 addition & 1 deletion tools/tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ import (
_ "mvdan.cc/gofumpt"

// unnamed import of statik for swagger UI support
_ "github.com/umee-network/umee/v4/swagger/statik"
_ "github.com/umee-network/umee/v5/swagger/statik"
)
69 changes: 63 additions & 6 deletions x/incentive/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package incentive

import (
"encoding/json"
"fmt"

"cosmossdk.io/errors"
"github.com/cosmos/cosmos-sdk/codec"
Expand Down Expand Up @@ -58,54 +59,98 @@ func (gs GenesisState) Validate() error {
return ErrDecreaseLastRewardTime.Wrap("last reward time must not be negative")
}

// TODO: enforce no duplicate (account,denom)
m := map[string]bool{}
for _, rt := range gs.RewardTrackers {
// enforce no duplicate (account,denom)
s := rt.Account + rt.UToken
if err := noDuplicateString(m, s, "reward trackers"); err != nil {
return err
}
if err := rt.Validate(); err != nil {
return err
}
}

// TODO: enforce no duplicate denoms
m = map[string]bool{}
for _, ra := range gs.RewardAccumulators {
if err := ra.Validate(); err != nil {
return err
}
// enforce no duplicate denoms
s := ra.UToken
if err := noDuplicateString(m, s, "reward accumulators"); err != nil {
return err
}
}

// TODO: enforce no duplicate program IDs
m = map[string]bool{}
// enforce no duplicate program IDs
for _, up := range gs.UpcomingPrograms {
if err := up.ValidatePassed(); err != nil {
return err
}
s := fmt.Sprintf("%d", up.ID)
if err := noDuplicateString(m, s, "upcoming program ID"); err != nil {
return err
}
}
for _, op := range gs.OngoingPrograms {
if err := op.ValidatePassed(); err != nil {
return err
}
s := fmt.Sprintf("%d", op.ID)
if err := noDuplicateString(m, s, "ongoing program ID"); err != nil {
return err
}
}
for _, cp := range gs.CompletedPrograms {
if err := cp.ValidatePassed(); err != nil {
return err
}
s := fmt.Sprintf("%d", cp.ID)
if err := noDuplicateString(m, s, "completed program ID"); err != nil {
return err
}
}

// TODO: enforce no duplicate (account,denom)
m = map[string]bool{}
for _, b := range gs.Bonds {
if err := b.Validate(); err != nil {
return err
}
// enforce no duplicate (account,denom)
s := b.Account + b.UToken.Denom
if err := noDuplicateString(m, s, "bonds"); err != nil {
return err
}
}

// TODO: enforce no duplicate (account,denom)
m = map[string]bool{}
for _, au := range gs.AccountUnbondings {
if err := au.Validate(); err != nil {
return err
}
// enforce no duplicate (account,denom)
s := au.Account + au.UToken
if err := noDuplicateString(m, s, "account unbondings"); err != nil {
return err
}
}

return nil
}

// noDuplicateString checks to see if a string is already present in a map
// and then adds it to the map. If it was already present, an error is returned.
// used to check all uniqueness requirements in genesis state.
func noDuplicateString(m map[string]bool, s, errMsg string) error {
if _, found := m[s]; found {
return fmt.Errorf("duplicate %s: %s", errMsg, s)
}
m[s] = true
return nil
}

// GetGenesisStateFromAppState returns x/incentive GenesisState given raw application
// genesis state.
func GetGenesisStateFromAppState(cdc codec.JSONCodec, appState map[string]json.RawMessage) *GenesisState {
Expand Down Expand Up @@ -174,7 +219,7 @@ func (ip IncentiveProgram) Validate() error {
return errors.Wrapf(ErrInvalidProgramDuration, "%d", ip.Duration)
}
if ip.StartTime <= 0 {
return errors.Wrapf(ErrInvalidProgramStart, "%d", ip.Duration)
return ErrInvalidProgramStart.Wrapf("%d", ip.StartTime)
}

return nil
Expand Down Expand Up @@ -238,6 +283,9 @@ func (rt RewardTracker) Validate() error {
if _, err := sdk.AccAddressFromBech32(rt.Account); err != nil {
return err
}
if err := sdk.ValidateDenom(rt.UToken); err != nil {
return err
}
if !leveragetypes.HasUTokenPrefix(rt.UToken) {
return leveragetypes.ErrNotUToken.Wrap(rt.UToken)
}
Expand All @@ -262,6 +310,9 @@ func NewRewardAccumulator(uDenom string, exponent uint32, coins sdk.DecCoins) Re
}

func (ra RewardAccumulator) Validate() error {
if err := sdk.ValidateDenom(ra.UToken); err != nil {
return err
}
if !leveragetypes.HasUTokenPrefix(ra.UToken) {
return leveragetypes.ErrNotUToken.Wrap(ra.UToken)
}
Expand Down Expand Up @@ -289,6 +340,9 @@ func (u Unbonding) Validate() error {
if u.End < u.Start {
return ErrInvalidUnbonding.Wrap("start time > end time")
}
if !leveragetypes.HasUTokenPrefix(u.UToken.Denom) {
return leveragetypes.ErrNotUToken.Wrap(u.UToken.Denom)
}
return u.UToken.Validate()
}

Expand All @@ -305,6 +359,9 @@ func (au AccountUnbondings) Validate() error {
if _, err := sdk.AccAddressFromBech32(au.Account); err != nil {
return err
}
if err := sdk.ValidateDenom(au.UToken); err != nil {
return err
}
if !leveragetypes.HasUTokenPrefix(au.UToken) {
return leveragetypes.ErrNotUToken.Wrap(au.UToken)
}
Expand Down
Loading

0 comments on commit 8e7fc9d

Please sign in to comment.