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

fix(perp): add proper stateless genesis validation #857

Merged
merged 13 commits into from
Aug 31, 2022
Prev Previous commit
Next Next commit
add: genesis tests
godismercilex committed Aug 29, 2022
commit d64e14a60685f1a6b40f2878aef7ef15a752851a
6 changes: 5 additions & 1 deletion x/perp/types/genesis.go
Original file line number Diff line number Diff line change
@@ -23,6 +23,10 @@ func DefaultGenesis() *GenesisState {
// Validate performs basic genesis state validation returning an error upon any
// failure.
func (gs GenesisState) Validate() error {
if err := gs.Params.Validate(); err != nil {
return err
}

for i, pos := range gs.Positions {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about the test of this one.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added, also found some other bugs which are fixed

if err := pos.Validate(); err != nil {
return fmt.Errorf("malformed genesis position %s at index %d: %w", pos, i, err)
@@ -47,5 +51,5 @@ func (gs GenesisState) Validate() error {
}
}

return gs.Params.Validate()
return nil
}
95 changes: 95 additions & 0 deletions x/perp/types/genesis_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package types

import (
"github.com/NibiruChain/nibiru/x/common"
"github.com/NibiruChain/nibiru/x/testutil/sample"
sdk "github.com/cosmos/cosmos-sdk/types"
"testing"
)

func TestGenesisState_Validate(t *testing.T) {
type test struct {
g *GenesisState
wantErr bool
}

cases := map[string]test{
"success": {
g: &GenesisState{
Params: DefaultParams(),
VaultBalance: sdk.NewCoins(),
PerpEfBalance: sdk.NewCoins(),
FeePoolBalance: sdk.NewCoins(),
PairMetadata: []*PairMetadata{
{
Pair: common.MustNewAssetPair("pair1:pair2"),
CumulativePremiumFractions: []sdk.Dec{sdk.MustNewDecFromStr("0.1")},
},
},
Positions: []*Position{
{
TraderAddress: sample.AccAddress().String(),
Pair: common.MustNewAssetPair("valid:pair"),
Size_: sdk.MustNewDecFromStr("1000"),
Margin: sdk.MustNewDecFromStr("1000"),
OpenNotional: sdk.MustNewDecFromStr("1000"),
LastUpdateCumulativePremiumFraction: sdk.MustNewDecFromStr("1"),
BlockNumber: 0,
},
},
PrepaidBadDebts: []*PrepaidBadDebt{
{
Denom: "pair",
Amount: sdk.NewInt(10),
},
},
WhitelistedAddresses: []string{sample.AccAddress().String()},
},
wantErr: false,
},
"bad params": {
g: &GenesisState{Params: Params{FeePoolFeeRatio: sdk.MustNewDecFromStr("-1.0")}},
wantErr: true,
},
"bad position": {
g: &GenesisState{
Params: DefaultParams(),
Positions: []*Position{
{
TraderAddress: sample.AccAddress().String(),
Pair: common.AssetPair{},
},
},
},
wantErr: true,
},
"bad whitelisted addr": {
g: &GenesisState{Params: DefaultParams(), WhitelistedAddresses: []string{"bad"}},
wantErr: true,
},
"bad pair metadata": {
g: &GenesisState{Params: DefaultParams(), PairMetadata: []*PairMetadata{{Pair: common.AssetPair{}}}},
wantErr: true,
},

"bad prepaid bad debt": {
g: &GenesisState{Params: DefaultParams(), PrepaidBadDebts: []*PrepaidBadDebt{{
Denom: ":invalid:Denom",
Amount: sdk.Int{},
}}},
wantErr: true,
},
}

for name, tc := range cases {
tc := tc
t.Run(name, func(t *testing.T) {
err := tc.g.Validate()
if tc.wantErr && err == nil {
t.Fatal("expected an error")
} else if !tc.wantErr && err != nil {
t.Fatalf("unexpected error: %s", err)
}
})
}
}
8 changes: 6 additions & 2 deletions x/perp/types/params.go
Original file line number Diff line number Diff line change
@@ -121,10 +121,14 @@ func validatePercentageRatio(i interface{}) error {
return fmt.Errorf("invalid parameter type: %T", i)
}

if ratio.IsNil() {
return fmt.Errorf("invalid nil decimal")
}

if ratio.GT(sdk.OneDec()) {
return fmt.Errorf("Ratio is above max value(1.00): %s", ratio.String())
return fmt.Errorf("ratio is above max value(1.00): %s", ratio.String())
} else if ratio.IsNegative() {
return fmt.Errorf("Ratio is negative: %s", ratio.String())
return fmt.Errorf("ratio is negative: %s", ratio.String())
}

return nil
5 changes: 4 additions & 1 deletion x/perp/types/state.go
Original file line number Diff line number Diff line change
@@ -49,5 +49,8 @@ func (m *PairMetadata) Validate() error {
}

func (m *PrepaidBadDebt) Validate() error {
return sdk.NewCoin(m.Denom, m.Amount).Validate()
return sdk.Coin{
Denom: m.Denom,
Amount: m.Amount,
}.Validate()
}