From 8b79166f810ca245f3b1448929635d3eefa5106f Mon Sep 17 00:00:00 2001 From: Puneet <59960662+puneet2019@users.noreply.github.com> Date: Mon, 22 Jan 2024 19:53:00 +0530 Subject: [PATCH] add validation for redelegation delta (#735) --- x/liquidstakeibc/genesis_test.go | 14 ++-- x/liquidstakeibc/keeper/msg_server.go | 9 +- x/liquidstakeibc/types/genesis_test.go | 13 +-- x/liquidstakeibc/types/liquidstakeibc.go | 3 + x/liquidstakeibc/types/liquidstakeibc_test.go | 83 ++++++++++++------- 5 files changed, 74 insertions(+), 48 deletions(-) diff --git a/x/liquidstakeibc/genesis_test.go b/x/liquidstakeibc/genesis_test.go index 9470e0de2..ceca1d8f6 100644 --- a/x/liquidstakeibc/genesis_test.go +++ b/x/liquidstakeibc/genesis_test.go @@ -21,12 +21,14 @@ func TestGenesis(t *testing.T) { 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), + RedelegationAcceptableDelta: sdk.ZeroInt(), + MaxEntries: 7, }, HostDenom: "uatom", ChannelId: "channel-1", diff --git a/x/liquidstakeibc/keeper/msg_server.go b/x/liquidstakeibc/keeper/msg_server.go index 1ccbfc89c..92114fac2 100644 --- a/x/liquidstakeibc/keeper/msg_server.go +++ b/x/liquidstakeibc/keeper/msg_server.go @@ -56,10 +56,11 @@ func (k msgServer) RegisterHostChain( // build the host chain params hostChainParams := &types.HostChainLSParams{ - DepositFee: msg.DepositFee, - RestakeFee: msg.RestakeFee, - UnstakeFee: msg.UnstakeFee, - RedemptionFee: msg.RedemptionFee, + DepositFee: msg.DepositFee, + RestakeFee: msg.RestakeFee, + UnstakeFee: msg.UnstakeFee, + RedemptionFee: msg.RedemptionFee, + RedelegationAcceptableDelta: sdktypes.ZeroInt(), } hc := &types.HostChain{ diff --git a/x/liquidstakeibc/types/genesis_test.go b/x/liquidstakeibc/types/genesis_test.go index b4aadc9e0..6ea55dec9 100644 --- a/x/liquidstakeibc/types/genesis_test.go +++ b/x/liquidstakeibc/types/genesis_test.go @@ -275,12 +275,13 @@ func ValidGenesis() *types.GenesisState { 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), + RedelegationAcceptableDelta: sdk.OneInt(), }, HostDenom: "uatom", ChannelId: "channel-1", diff --git a/x/liquidstakeibc/types/liquidstakeibc.go b/x/liquidstakeibc/types/liquidstakeibc.go index 68aa48b90..7f99fa394 100644 --- a/x/liquidstakeibc/types/liquidstakeibc.go +++ b/x/liquidstakeibc/types/liquidstakeibc.go @@ -172,6 +172,9 @@ func (params *HostChainLSParams) Validate() error { if params.UnstakeFee.LT(sdk.ZeroDec()) || params.UnstakeFee.GT(sdk.OneDec()) { return fmt.Errorf("host chain lsparams has invalid unstake fee, should be 0<=fee<=1\"") } + if params.RedelegationAcceptableDelta.LT(sdk.ZeroInt()) { + return fmt.Errorf("host chain has invalid redelegation acceptable delta expected >= 0") + } return nil } diff --git a/x/liquidstakeibc/types/liquidstakeibc_test.go b/x/liquidstakeibc/types/liquidstakeibc_test.go index bfc2592b1..fbfa36bc2 100644 --- a/x/liquidstakeibc/types/liquidstakeibc_test.go +++ b/x/liquidstakeibc/types/liquidstakeibc_test.go @@ -238,10 +238,11 @@ func TestHostChain_Validate(t *testing.T) { ChainId: "chain-1", ConnectionId: "connection-1", Params: &types.HostChainLSParams{ - DepositFee: sdk.ZeroDec(), - RestakeFee: sdk.ZeroDec(), - UnstakeFee: sdk.ZeroDec(), - RedemptionFee: sdk.ZeroDec(), + DepositFee: sdk.ZeroDec(), + RestakeFee: sdk.ZeroDec(), + UnstakeFee: sdk.ZeroDec(), + RedemptionFee: sdk.ZeroDec(), + RedelegationAcceptableDelta: sdk.OneInt(), }, HostDenom: "uatom", ChannelId: "channel-1", @@ -635,10 +636,11 @@ func TestValidatorUnbonding_Validate(t *testing.T) { func TestHostChainLSParams_Validate(t *testing.T) { type fields struct { - DepositFee sdk.Dec - RestakeFee sdk.Dec - UnstakeFee sdk.Dec - RedemptionFee sdk.Dec + DepositFee sdk.Dec + RestakeFee sdk.Dec + UnstakeFee sdk.Dec + RedemptionFee sdk.Dec + RedelegationDelta math.Int } tests := []struct { name string @@ -648,50 +650,66 @@ func TestHostChainLSParams_Validate(t *testing.T) { { name: "valid", fields: fields{ - DepositFee: sdk.ZeroDec(), - RestakeFee: sdk.ZeroDec(), - UnstakeFee: sdk.ZeroDec(), - RedemptionFee: sdk.ZeroDec(), + DepositFee: sdk.ZeroDec(), + RestakeFee: sdk.ZeroDec(), + UnstakeFee: sdk.ZeroDec(), + RedemptionFee: sdk.ZeroDec(), + RedelegationDelta: sdk.OneInt(), }, wantErr: false, }, { name: "invalid deposit fee", fields: fields{ - DepositFee: sdk.MustNewDecFromStr("-1"), - RestakeFee: sdk.ZeroDec(), - UnstakeFee: sdk.ZeroDec(), - RedemptionFee: sdk.ZeroDec(), + DepositFee: sdk.MustNewDecFromStr("-1"), + RestakeFee: sdk.ZeroDec(), + UnstakeFee: sdk.ZeroDec(), + RedemptionFee: sdk.ZeroDec(), + RedelegationDelta: sdk.OneInt(), }, wantErr: true, }, { name: "invalid restake fee", fields: fields{ - DepositFee: sdk.ZeroDec(), - RestakeFee: sdk.MustNewDecFromStr("1.1"), - UnstakeFee: sdk.ZeroDec(), - RedemptionFee: sdk.ZeroDec(), + DepositFee: sdk.ZeroDec(), + RestakeFee: sdk.MustNewDecFromStr("1.1"), + UnstakeFee: sdk.ZeroDec(), + RedemptionFee: sdk.ZeroDec(), + RedelegationDelta: sdk.OneInt(), }, wantErr: true, }, { name: "invalid unstake fee", fields: fields{ - DepositFee: sdk.ZeroDec(), - RestakeFee: sdk.ZeroDec(), - UnstakeFee: sdk.MustNewDecFromStr("-1"), - RedemptionFee: sdk.ZeroDec(), + DepositFee: sdk.ZeroDec(), + RestakeFee: sdk.ZeroDec(), + UnstakeFee: sdk.MustNewDecFromStr("-1"), + RedemptionFee: sdk.ZeroDec(), + RedelegationDelta: sdk.OneInt(), }, wantErr: true, }, { name: "invalid redemption fee", fields: fields{ - DepositFee: sdk.ZeroDec(), - RestakeFee: sdk.ZeroDec(), - UnstakeFee: sdk.ZeroDec(), - RedemptionFee: sdk.MustNewDecFromStr("1.2"), + DepositFee: sdk.ZeroDec(), + RestakeFee: sdk.ZeroDec(), + UnstakeFee: sdk.ZeroDec(), + RedemptionFee: sdk.MustNewDecFromStr("1.2"), + RedelegationDelta: sdk.OneInt(), + }, + wantErr: true, + }, + { + name: "invalid redelegation delta", + fields: fields{ + DepositFee: sdk.ZeroDec(), + RestakeFee: sdk.ZeroDec(), + UnstakeFee: sdk.ZeroDec(), + RedemptionFee: sdk.ZeroDec(), + RedelegationDelta: sdk.NewInt(-1), }, wantErr: true, }, @@ -699,10 +717,11 @@ func TestHostChainLSParams_Validate(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { params := &types.HostChainLSParams{ - DepositFee: tt.fields.DepositFee, - RestakeFee: tt.fields.RestakeFee, - UnstakeFee: tt.fields.UnstakeFee, - RedemptionFee: tt.fields.RedemptionFee, + DepositFee: tt.fields.DepositFee, + RestakeFee: tt.fields.RestakeFee, + UnstakeFee: tt.fields.UnstakeFee, + RedemptionFee: tt.fields.RedemptionFee, + RedelegationAcceptableDelta: tt.fields.RedelegationDelta, } if err := params.Validate(); (err != nil) != tt.wantErr { t.Errorf("Validate() error = %v, wantErr %v", err, tt.wantErr)