Skip to content

Commit

Permalink
feat: Move c value bounds to per-chain params (#732)
Browse files Browse the repository at this point in the history
* move c value bounds to per-chain params

* changelog

* linter

* check negative lower limit

* add comment on removed protos

* migration

* version fix

* update lower bound cosmoshub-4

* Update app/app.go

Co-authored-by: Puneet <[email protected]>

* Update app/app.go

Co-authored-by: Puneet <[email protected]>

* Update app/app.go

Co-authored-by: Puneet <[email protected]>

* Update app/app.go

Co-authored-by: Puneet <[email protected]>

* Update app/app.go

Co-authored-by: Puneet <[email protected]>

* Update app/app.go

Co-authored-by: Puneet <[email protected]>

* just check for negative values

---------

Co-authored-by: Puneet <[email protected]>
  • Loading branch information
kruspy and puneet2019 authored Jan 22, 2024
1 parent 83091c3 commit 633526e
Show file tree
Hide file tree
Showing 21 changed files with 411 additions and 362 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ Ref: https://keepachangelog.com/en/1.0.0/
### Features

- [736](https://github.com/persistenceOne/pstake-native/pull/736) Check for host denom duplicates.
- [733](https://github.com/persistenceOne/pstake-native/pull/733) Add more validation for host-chain
- [733](https://github.com/persistenceOne/pstake-native/pull/733) Add more validation for host-chain.
- [732](https://github.com/persistenceOne/pstake-native/pull/732) Move c value bounds to per-chain params.
- [729](https://github.com/persistenceOne/pstake-native/pull/729) Add rewards account query (hence autocompound)
OnChanOpenAck.
- [727](https://github.com/persistenceOne/pstake-native/pull/727) Send LSM redeem messages in chunks.
Expand Down
67 changes: 45 additions & 22 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -1019,28 +1019,51 @@ func (app *PstakeApp) RegisterUpgradeHandler() {
app.UpgradeKeeper.SetUpgradeHandler(
UpgradeName,
func(ctx sdk.Context, _ upgradetypes.Plan, fromVM module.VersionMap) (module.VersionMap, error) {
// stuck unbonding epoch numbers
RemovableUnbondings := map[string]map[int64]any{"cosmoshub-4": {312: nil}, "osmosis-1": {429: nil, 432: nil}}

// get the stuck unbondings from the store
unbondings := app.LiquidStakeIBCKeeper.FilterUnbondings(
ctx,
func(u liquidstakeibctypes.Unbonding) bool {
_, chain := RemovableUnbondings[u.ChainId]
if chain {
_, epoch := RemovableUnbondings[u.ChainId][u.EpochNumber]
if epoch {
return true
}
}
return false
},
)

// mark the stuck unbondings as failed, so they can be processed
for _, unbonding := range unbondings {
unbonding.State = liquidstakeibctypes.Unbonding_UNBONDING_FAILED
app.LiquidStakeIBCKeeper.SetUnbonding(ctx, unbonding)
for _, hc := range app.LiquidStakeIBCKeeper.GetAllHostChains(ctx) {
switch hc.ChainId {
case "cosmoshub-4":
upperLimit, _ := sdk.NewDecFromStr("1.01")
lowerLimit, _ := sdk.NewDecFromStr("0.80")
hc.Params.UpperCValueLimit = upperLimit
hc.Params.LowerCValueLimit = lowerLimit
app.LiquidStakeIBCKeeper.SetHostChain(ctx, hc)

Check failure on line 1030 in app/app.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gci`-ed with --skip-generated -s standard -s default -s prefix(github.com/persistenceOne/pstake-native) --custom-order (gci)
case "osmosis-1":
upperLimit, _ := sdk.NewDecFromStr("1.01")
lowerLimit, _ := sdk.NewDecFromStr("0.97")
hc.Params.UpperCValueLimit = upperLimit
hc.Params.LowerCValueLimit = lowerLimit
app.LiquidStakeIBCKeeper.SetHostChain(ctx, hc)

Check failure on line 1037 in app/app.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gci`-ed with --skip-generated -s standard -s default -s prefix(github.com/persistenceOne/pstake-native) --custom-order (gci)
case "theta-testnet-001":
upperLimit, _ := sdk.NewDecFromStr("1.01")
lowerLimit, _ := sdk.NewDecFromStr("0.9")
hc.Params.UpperCValueLimit = upperLimit
hc.Params.LowerCValueLimit = lowerLimit
app.LiquidStakeIBCKeeper.SetHostChain(ctx, hc)

Check failure on line 1044 in app/app.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gci`-ed with --skip-generated -s standard -s default -s prefix(github.com/persistenceOne/pstake-native) --custom-order (gci)
case "osmo-test-5":
upperLimit, _ := sdk.NewDecFromStr("1.01")
lowerLimit, _ := sdk.NewDecFromStr("0.95")
hc.Params.UpperCValueLimit = upperLimit
hc.Params.LowerCValueLimit = lowerLimit
app.LiquidStakeIBCKeeper.SetHostChain(ctx, hc)

Check failure on line 1051 in app/app.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gci`-ed with --skip-generated -s standard -s default -s prefix(github.com/persistenceOne/pstake-native) --custom-order (gci)
case "dydx-test-4":
upperLimit, _ := sdk.NewDecFromStr("1.01")
lowerLimit, _ := sdk.NewDecFromStr("0.95")
hc.Params.UpperCValueLimit = upperLimit
hc.Params.LowerCValueLimit = lowerLimit
app.LiquidStakeIBCKeeper.SetHostChain(ctx, hc)

Check failure on line 1058 in app/app.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gci`-ed with --skip-generated -s standard -s default -s prefix(github.com/persistenceOne/pstake-native) --custom-order (gci)
case "gaia-1":
upperLimit, _ := sdk.NewDecFromStr("1.01")
lowerLimit, _ := sdk.NewDecFromStr("0.95")
hc.Params.UpperCValueLimit = upperLimit
hc.Params.LowerCValueLimit = lowerLimit
app.LiquidStakeIBCKeeper.SetHostChain(ctx, hc)

Check failure on line 1065 in app/app.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gci`-ed with --skip-generated -s standard -s default -s prefix(github.com/persistenceOne/pstake-native) --custom-order (gci)
}
}

return app.mm.RunMigrations(ctx, app.configurator, fromVM)
Expand Down
2 changes: 1 addition & 1 deletion app/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ package app

const (
appName = "pStake"
UpgradeName = "v2.8.2"
UpgradeName = "v2.9.0"
)
11 changes: 11 additions & 0 deletions proto/pstake/liquidstakeibc/v1beta1/liquidstakeibc.proto
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,17 @@ message HostChainLSParams {
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
(gogoproto.nullable) = false
];
string upper_c_value_limit = 10 [
(cosmos_proto.scalar) = "cosmos.Dec",
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = false
]; // upper limit for the c value of the host chain

string lower_c_value_limit = 11 [
(cosmos_proto.scalar) = "cosmos.Dec",
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = false
]; // lower limit for the c value of the host chain
}

message ICAAccount {
Expand Down
15 changes: 4 additions & 11 deletions proto/pstake/liquidstakeibc/v1beta1/params.proto
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,8 @@ message Params {
string fee_address = 2 [ (cosmos_proto.scalar) =
"cosmos.AddressString" ]; // protocol fee address

string upper_c_value_limit = 3 [
(cosmos_proto.scalar) = "cosmos.Dec",
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = false
]; // upper limit for the c value of a host chain

string lower_c_value_limit = 4 [
(cosmos_proto.scalar) = "cosmos.Dec",
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = false
]; // lower limit for the c value of a host chain
// fields 3 and 4 were migrated to on-chain params.
// check https://github.com/persistenceOne/pstake-native/pull/732.
reserved 3; // upper_c_value_limit
reserved 4; // lower_c_value_limit
}
17 changes: 11 additions & 6 deletions x/liquidstakeibc/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,23 @@ import (
)

func TestGenesis(t *testing.T) {
upperCValueLimit, _ := sdk.NewDecFromStr("1.05")
lowerCValueLimit, _ := sdk.NewDecFromStr("0.95")

genesisState := &types.GenesisState{
Params: types.DefaultParams(),
HostChains: []*types.HostChain{{
ChainId: "chainA-1",
ConnectionId: "connection-1",
Params: &types.HostChainLSParams{
DepositFee: sdk.ZeroDec(),
RestakeFee: sdk.ZeroDec(),
UnstakeFee: sdk.ZeroDec(),
RedemptionFee: sdk.ZeroDec(),
LsmValidatorCap: sdk.NewDec(1),
LsmBondFactor: sdk.NewDec(-1),
DepositFee: sdk.ZeroDec(),
RestakeFee: sdk.ZeroDec(),
UnstakeFee: sdk.ZeroDec(),
RedemptionFee: sdk.ZeroDec(),
LsmValidatorCap: sdk.NewDec(1),
LsmBondFactor: sdk.NewDec(-1),
UpperCValueLimit: upperCValueLimit,
LowerCValueLimit: lowerCValueLimit,
RedelegationAcceptableDelta: sdk.ZeroInt(),
MaxEntries: 7,
},
Expand Down
6 changes: 2 additions & 4 deletions x/liquidstakeibc/keeper/grpc_querier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,8 @@ func (suite *IntegrationTestSuite) TestQueryParams() {
req: &types.QueryParamsRequest{},
resp: &types.QueryParamsResponse{
Params: types.Params{
AdminAddress: "persistence1gztc3y3k52hjds5nqvl7h9jvfnc33spz47zcjy",
FeeAddress: "persistence1gztc3y3k52hjds5nqvl7h9jvfnc33spz47zcjy",
UpperCValueLimit: decFromStr("1.1"),
LowerCValueLimit: decFromStr("0.85"),
AdminAddress: "persistence1gztc3y3k52hjds5nqvl7h9jvfnc33spz47zcjy",
FeeAddress: "persistence1gztc3y3k52hjds5nqvl7h9jvfnc33spz47zcjy",
},
},
},
Expand Down
2 changes: 1 addition & 1 deletion x/liquidstakeibc/keeper/invariants.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func CValueLimits(k Keeper) sdk.Invariant {
str := ""
broken := false
for _, hc := range hostChains {
if !k.CValueWithinLimits(ctx, hc) {
if !k.CValueWithinLimits(hc) {
str = fmt.Sprintf("chainID: %s, cValue: %s \n", hc.ChainId, hc.CValue)
}
}
Expand Down
6 changes: 3 additions & 3 deletions x/liquidstakeibc/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ func (k *Keeper) UpdateCValues(ctx sdk.Context) {
}()

// if the c value is out of bounds, disable the chain
if !k.CValueWithinLimits(ctx, hc) {
if !k.CValueWithinLimits(hc) {
hc.Active = false
k.SetHostChain(ctx, hc)

Expand All @@ -350,8 +350,8 @@ func (k *Keeper) UpdateCValues(ctx sdk.Context) {
}
}

func (k *Keeper) CValueWithinLimits(ctx sdk.Context, hc *types.HostChain) bool {
return hc.CValue.LT(k.GetParams(ctx).UpperCValueLimit) && hc.CValue.GT(k.GetParams(ctx).LowerCValueLimit)
func (k *Keeper) CValueWithinLimits(hc *types.HostChain) bool {
return hc.CValue.LT(hc.Params.UpperCValueLimit) && hc.CValue.GT(hc.Params.LowerCValueLimit)
}

func (k *Keeper) CalculateAutocompoundLimit(autocompoundFactor sdk.Dec) sdk.Dec {
Expand Down
17 changes: 6 additions & 11 deletions x/liquidstakeibc/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,12 @@ func (suite *IntegrationTestSuite) TestGetSetParams() {
{
name: "normal params",
params: types.Params{
AdminAddress: "persistence10khgeppewe4rgfrcy809r9h00aquwxxxrk6glr",
FeeAddress: "persistence1xruvjju28j0a5ud5325rfdak8f5a04h0s30mld",
UpperCValueLimit: decFromStr("1.1"),
LowerCValueLimit: decFromStr("0.9"),
AdminAddress: "persistence10khgeppewe4rgfrcy809r9h00aquwxxxrk6glr",
FeeAddress: "persistence1xruvjju28j0a5ud5325rfdak8f5a04h0s30mld",
},
expected: types.Params{
AdminAddress: "persistence10khgeppewe4rgfrcy809r9h00aquwxxxrk6glr",
FeeAddress: "persistence1xruvjju28j0a5ud5325rfdak8f5a04h0s30mld",
UpperCValueLimit: decFromStr("1.1"),
LowerCValueLimit: decFromStr("0.9"),
AdminAddress: "persistence10khgeppewe4rgfrcy809r9h00aquwxxxrk6glr",
FeeAddress: "persistence1xruvjju28j0a5ud5325rfdak8f5a04h0s30mld",
},
},
}
Expand Down Expand Up @@ -274,9 +270,8 @@ func (suite *IntegrationTestSuite) TestUpdateCValues() {
suite.Require().NotPanics(func() { pstakeApp.LiquidStakeIBCKeeper.UpdateCValues(ctx) })

// lower limits so that chain goes out of limits
params := pstakeApp.LiquidStakeIBCKeeper.GetParams(ctx)
params.UpperCValueLimit = sdk.MustNewDecFromStr("0.5")
pstakeApp.LiquidStakeIBCKeeper.SetParams(ctx, params)
hc.Params.UpperCValueLimit = sdk.MustNewDecFromStr("0.5")
pstakeApp.LiquidStakeIBCKeeper.SetHostChain(ctx, hc)
suite.Require().NotPanics(func() { pstakeApp.LiquidStakeIBCKeeper.UpdateCValues(ctx) })
hc, _ = pstakeApp.LiquidStakeIBCKeeper.GetHostChain(ctx, suite.chainB.ChainID)
suite.Require().Equal(false, hc.Active)
Expand Down
22 changes: 22 additions & 0 deletions x/liquidstakeibc/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,28 @@ func (k msgServer) UpdateHostChain(
return nil, err
}
hc.Params.MaxEntries = uint32(entries)
case types.KeyUpperCValueLimit:
limit, err := sdktypes.NewDecFromStr(update.Value)
if err != nil {
return nil, fmt.Errorf("unable to parse string to sdk.Dec: %w", err)
}

if limit.LTE(hc.Params.LowerCValueLimit) {
return nil, fmt.Errorf("upper c value limit can't be less than lower c value limit: %w", err)
}

hc.Params.UpperCValueLimit = limit
case types.KeyLowerCValueLimit:
limit, err := sdktypes.NewDecFromStr(update.Value)
if err != nil {
return nil, fmt.Errorf("unable to parse string to sdk.Dec: %w", err)
}

if limit.GTE(hc.Params.UpperCValueLimit) {
return nil, fmt.Errorf("lower c value limit can't be higher than upper c value limit: %w", err)
}

hc.Params.LowerCValueLimit = limit
case types.KeyRedelegationAcceptableDelta:
redelegationAcceptableDelta, ok := sdktypes.NewIntFromString(update.Value)
if !ok {
Expand Down
6 changes: 2 additions & 4 deletions x/liquidstakeibc/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -588,10 +588,8 @@ func (suite *IntegrationTestSuite) Test_msgServer_UpdateParams() {
msg: &types.MsgUpdateParams{
Authority: suite.chainA.SenderAccount.GetAddress().String(),
Params: types.Params{
AdminAddress: suite.chainA.SenderAccount.GetAddress().String(),
FeeAddress: suite.chainA.SenderAccount.GetAddress().String(),
UpperCValueLimit: sdk.OneDec(),
LowerCValueLimit: sdk.ZeroDec(),
AdminAddress: suite.chainA.SenderAccount.GetAddress().String(),
FeeAddress: suite.chainA.SenderAccount.GetAddress().String(),
},
},
},
Expand Down
20 changes: 14 additions & 6 deletions x/liquidstakeibc/keeper/setup_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,13 +155,21 @@ func (suite *IntegrationTestSuite) SetupHostChainAB() {
lsmBondFactor, err := sdk.NewDecFromStr("50")
suite.NoError(err)

upperCValueLimit, err := sdk.NewDecFromStr("1.05")
suite.NoError(err)

lowerCValueLimit, err := sdk.NewDecFromStr("0.95")
suite.NoError(err)

hostChainLSParams := &types.HostChainLSParams{
DepositFee: depositFee,
RestakeFee: restakeFee,
UnstakeFee: unstakeFee,
RedemptionFee: redemptionFee,
LsmValidatorCap: lsmValidatorCap,
LsmBondFactor: lsmBondFactor,
DepositFee: depositFee,
RestakeFee: restakeFee,
UnstakeFee: unstakeFee,
RedemptionFee: redemptionFee,
LsmValidatorCap: lsmValidatorCap,
LsmBondFactor: lsmBondFactor,
UpperCValueLimit: upperCValueLimit,
LowerCValueLimit: lowerCValueLimit,
}

validators := make([]*types.Validator, 0)
Expand Down
17 changes: 11 additions & 6 deletions x/liquidstakeibc/types/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,18 +269,23 @@ func TestGenesisState_Validate(t *testing.T) {
}

func ValidGenesis() *types.GenesisState {
upperCValueLimit, _ := sdk.NewDecFromStr("1.05")
lowerCValueLimit, _ := sdk.NewDecFromStr("0.95")

return &types.GenesisState{
Params: types.DefaultParams(),
HostChains: []*types.HostChain{{
ChainId: "chainA-1",
ConnectionId: "connection-1",
Params: &types.HostChainLSParams{
DepositFee: sdk.ZeroDec(),
RestakeFee: sdk.ZeroDec(),
UnstakeFee: sdk.ZeroDec(),
RedemptionFee: sdk.ZeroDec(),
LsmValidatorCap: sdk.NewDec(1),
LsmBondFactor: sdk.NewDec(-1),
DepositFee: sdk.ZeroDec(),
RestakeFee: sdk.ZeroDec(),
UnstakeFee: sdk.ZeroDec(),
RedemptionFee: sdk.ZeroDec(),
LsmValidatorCap: sdk.NewDec(1),
LsmBondFactor: sdk.NewDec(-1),
UpperCValueLimit: upperCValueLimit,
LowerCValueLimit: lowerCValueLimit,
RedelegationAcceptableDelta: sdk.OneInt(),
},
HostDenom: "uatom",
Expand Down
2 changes: 2 additions & 0 deletions x/liquidstakeibc/types/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ const (
KeyLSMValidatorCap string = "lsm_validator_cap"
KeyLSMBondFactor string = "lsm_bond_factor"
KeyMaxEntries string = "max_entries"
KeyUpperCValueLimit string = "upper_c_value_limit"
KeyLowerCValueLimit string = "lower_c_value_limit"
KeyRedelegationAcceptableDelta string = "redelegation_acceptable_delta"
KeyMinimumDeposit string = "min_deposit"
KeyActive string = "active"
Expand Down
Loading

0 comments on commit 633526e

Please sign in to comment.