-
Notifications
You must be signed in to change notification settings - Fork 208
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
**summary** add inner bounds **tests** - unittests - dockernet - verify bounds are set upon registration - verify hz halts if the inner bound is crossed ``` ➜ stride git:(tight-bounds) ✗ stridedl q stakeibc show-host-zone GAIA host_zone: ... last_redemption_rate: "1.000000000000000000" max_inner_redemption_rate: "1.500000000000000000" max_redemption_rate: "1.500000000000000000" min_inner_redemption_rate: "0.900000000000000000" min_redemption_rate: "0.900000000000000000" redemption_rate: "1.000000000000000000" ``` update bounds ``` $STRIDE_MAIN_CMD tx stakeibc set-redemption-rate-bounds GAIA 1 1.4 --from $STRIDE_ADMIN_ACCT -y | TRIM_TX ``` and query them ``` max_inner_redemption_rate: "1.400000000000000000" max_redemption_rate: "1.500000000000000000" min_inner_redemption_rate: "1.000000000000000000" min_redemption_rate: "0.900000000000000000" ``` try setting from the wrong account ``` $STRIDE_MAIN_CMD tx stakeibc set-redemption-rate-bounds GAIA 1.1 1.3 --from val1 -y | TRIM_TX ``` and the error ``` ➜ stride git:(tight-bounds) ✗ bash dockernet/scripts/set_bound.sh Error: invalid creator address (stride1uk4ze0x4nvh4fk0xm4jdud58eqn4yxhrt52vv7): invalid address Usage: strided tx stakeibc set-redemption-rate-bounds [chainid] [min-bound] [max-bound] [flags] Flags: ``` Set tight bound and observe halt ``` $STRIDE_MAIN_CMD tx stakeibc set-redemption-rate-bounds GAIA 1 1.000001 --from $STRIDE_ADMIN_ACCT -y | TRIM_TX ``` halt ``` dockernet-stride1-1 | 2:11AM ERR IsRedemptionRateWithinSafetyBounds check failed 1.001028241739306108 is outside inner safety bounds [1.000000000000000000, 1.000001000000000000] module=x/stakeibc dockernet-stride1-1 | 2:11AM ERR [INVARIANT BROKEN!!!] GAIA's RR is 1.001028241739306108. ERR: IsRedemptionRateWithinSafetyBounds check failed 1.001028241739306108 is outside inner safety bounds [1.000000000000000000, 1.000001000000000000]: redemption rate outside safety bounds module=x/stakeibc ```
- Loading branch information
Showing
16 changed files
with
1,108 additions
and
181 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
### IBC TRANSFER | ||
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) | ||
source ${SCRIPT_DIR}/../config.sh | ||
|
||
## Set the redemption rate bounds | ||
# Set bounds from the correct account (success) | ||
# $STRIDE_MAIN_CMD tx stakeibc set-redemption-rate-bounds GAIA 1 1.4 --from $STRIDE_ADMIN_ACCT -y | TRIM_TX | ||
# Set bounds from the wrong account (fail) | ||
# $STRIDE_MAIN_CMD tx stakeibc set-redemption-rate-bounds GAIA 1.1 1.3 --from val1 -y | TRIM_TX | ||
# Set tight bound and observe halt | ||
$STRIDE_MAIN_CMD tx stakeibc set-redemption-rate-bounds GAIA 1 1.000001 --from $STRIDE_ADMIN_ACCT -y | TRIM_TX |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
x/stakeibc/client/cli/tx_update_inner_redemption_rate_bounds.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package cli | ||
|
||
import ( | ||
"github.com/cosmos/cosmos-sdk/client" | ||
"github.com/cosmos/cosmos-sdk/client/flags" | ||
"github.com/cosmos/cosmos-sdk/client/tx" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/spf13/cobra" | ||
|
||
"github.com/Stride-Labs/stride/v14/x/stakeibc/types" | ||
) | ||
|
||
func CmdUpdateInnerRedemptionRateBounds() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "set-redemption-rate-bounds [chainid] [min-bound] [max-bound]", | ||
Short: "Broadcast message set-redemption-rate-bounds", | ||
Args: cobra.ExactArgs(3), | ||
RunE: func(cmd *cobra.Command, args []string) (err error) { | ||
argChainId := args[0] | ||
minInnerRedemptionRate := sdk.MustNewDecFromStr(args[1]) | ||
maxInnerRedemptionRate := sdk.MustNewDecFromStr(args[2]) | ||
|
||
clientCtx, err := client.GetClientTxContext(cmd) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
msg := types.NewMsgUpdateInnerRedemptionRateBounds( | ||
clientCtx.GetFromAddress().String(), | ||
argChainId, | ||
minInnerRedemptionRate, | ||
maxInnerRedemptionRate, | ||
) | ||
if err := msg.ValidateBasic(); err != nil { | ||
return err | ||
} | ||
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg) | ||
}, | ||
} | ||
|
||
flags.AddTxFlagsToCmd(cmd) | ||
|
||
return cmd | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
x/stakeibc/keeper/msg_server_update_inner_redemption_rate_bounds.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package keeper | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
errorsmod "cosmossdk.io/errors" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
|
||
"github.com/Stride-Labs/stride/v14/x/stakeibc/types" | ||
) | ||
|
||
func (k msgServer) UpdateInnerRedemptionRateBounds(goCtx context.Context, msg *types.MsgUpdateInnerRedemptionRateBounds) (*types.MsgUpdateInnerRedemptionRateBoundsResponse, error) { | ||
ctx := sdk.UnwrapSDKContext(goCtx) | ||
|
||
// Confirm host zone exists | ||
zone, found := k.GetHostZone(ctx, msg.ChainId) | ||
if !found { | ||
k.Logger(ctx).Error(fmt.Sprintf("Host Zone not found: %s", msg.ChainId)) | ||
return nil, types.ErrInvalidHostZone | ||
} | ||
|
||
// Get the wide bounds | ||
outerMinSafetyThreshold, outerMaxSafetyThreshold := k.GetOuterSafetyBounds(ctx, zone) | ||
|
||
innerMinSafetyThreshold := msg.MinInnerRedemptionRate | ||
innerMaxSafetyThreshold := msg.MaxInnerRedemptionRate | ||
|
||
// Confirm the inner bounds are within the outer bounds | ||
if innerMinSafetyThreshold.LT(outerMinSafetyThreshold) { | ||
errMsg := fmt.Sprintf("inner min safety threshold (%s) is less than outer min safety threshold (%s)", innerMinSafetyThreshold, outerMinSafetyThreshold) | ||
k.Logger(ctx).Error(errMsg) | ||
return nil, errorsmod.Wrapf(types.ErrInvalidBounds, errMsg) | ||
} | ||
|
||
if innerMaxSafetyThreshold.GT(outerMaxSafetyThreshold) { | ||
errMsg := fmt.Sprintf("inner max safety threshold (%s) is greater than outer max safety threshold (%s)", innerMaxSafetyThreshold, outerMaxSafetyThreshold) | ||
k.Logger(ctx).Error(errMsg) | ||
return nil, errorsmod.Wrapf(types.ErrInvalidBounds, errMsg) | ||
} | ||
|
||
// Set the inner bounds on the host zone | ||
zone.MinInnerRedemptionRate = innerMinSafetyThreshold | ||
zone.MaxInnerRedemptionRate = innerMaxSafetyThreshold | ||
k.SetHostZone(ctx, zone) | ||
|
||
return &types.MsgUpdateInnerRedemptionRateBoundsResponse{}, nil | ||
} |
111 changes: 111 additions & 0 deletions
111
x/stakeibc/keeper/msg_server_update_inner_redemption_rate_bounds_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
package keeper_test | ||
|
||
import ( | ||
"fmt" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
_ "github.com/stretchr/testify/suite" | ||
|
||
stakeibctypes "github.com/Stride-Labs/stride/v14/x/stakeibc/types" | ||
) | ||
|
||
type UpdateInnerRedemptionRateBoundsTestCase struct { | ||
validMsg stakeibctypes.MsgUpdateInnerRedemptionRateBounds | ||
zone stakeibctypes.HostZone | ||
} | ||
|
||
func (s *KeeperTestSuite) SetupUpdateInnerRedemptionRateBounds() UpdateInnerRedemptionRateBoundsTestCase { | ||
// Register a host zone | ||
hostZone := stakeibctypes.HostZone{ | ||
ChainId: HostChainId, | ||
HostDenom: Atom, | ||
IbcDenom: IbcAtom, | ||
RedemptionRate: sdk.NewDec(1.0), | ||
MinRedemptionRate: sdk.NewDec(9).Quo(sdk.NewDec(10)), | ||
MaxRedemptionRate: sdk.NewDec(15).Quo(sdk.NewDec(10)), | ||
} | ||
|
||
s.App.StakeibcKeeper.SetHostZone(s.Ctx, hostZone) | ||
|
||
defaultMsg := stakeibctypes.MsgUpdateInnerRedemptionRateBounds{ | ||
// TODO: does this need to be the admin address? | ||
Creator: s.TestAccs[0].String(), | ||
ChainId: HostChainId, | ||
MinInnerRedemptionRate: sdk.NewDec(1), | ||
MaxInnerRedemptionRate: sdk.NewDec(11).Quo(sdk.NewDec(10)), | ||
} | ||
|
||
return UpdateInnerRedemptionRateBoundsTestCase{ | ||
validMsg: defaultMsg, | ||
zone: hostZone, | ||
} | ||
} | ||
|
||
// Verify that bounds can be set successfully | ||
func (s *KeeperTestSuite) TestUpdateInnerRedemptionRateBounds_Success() { | ||
tc := s.SetupUpdateInnerRedemptionRateBounds() | ||
|
||
// Set the inner bounds on the host zone | ||
_, err := s.GetMsgServer().UpdateInnerRedemptionRateBounds(s.Ctx, &tc.validMsg) | ||
s.Require().NoError(err, "should not throw an error") | ||
|
||
// Confirm the inner bounds were set | ||
zone, found := s.App.StakeibcKeeper.GetHostZone(s.Ctx, HostChainId) | ||
s.Require().True(found, "host zone should be in the store") | ||
s.Require().Equal(tc.validMsg.MinInnerRedemptionRate, zone.MinInnerRedemptionRate, "min inner redemption rate should be set") | ||
s.Require().Equal(tc.validMsg.MaxInnerRedemptionRate, zone.MaxInnerRedemptionRate, "max inner redemption rate should be set") | ||
} | ||
|
||
// Setting inner bounds outside of outer bounds should throw an error | ||
func (s *KeeperTestSuite) TestUpdateInnerRedemptionRateBounds_OutOfBounds() { | ||
tc := s.SetupUpdateInnerRedemptionRateBounds() | ||
|
||
// Set the min inner bound to be less than the min outer bound | ||
tc.validMsg.MinInnerRedemptionRate = sdk.NewDec(0) | ||
|
||
// Set the inner bounds on the host zone | ||
_, err := s.GetMsgServer().UpdateInnerRedemptionRateBounds(s.Ctx, &tc.validMsg) | ||
// verify it throws an error | ||
errMsg := fmt.Sprintf("inner min safety threshold (%s) is less than outer min safety threshold (%s)", tc.validMsg.MinInnerRedemptionRate, sdk.NewDec(9).Quo(sdk.NewDec(10))) | ||
s.Require().ErrorContains(err, errMsg) | ||
|
||
// Set the min inner bound to be valid, but the max inner bound to be greater than the max outer bound | ||
tc.validMsg.MinInnerRedemptionRate = sdk.NewDec(1) | ||
tc.validMsg.MaxInnerRedemptionRate = sdk.NewDec(3) | ||
// Set the inner bounds on the host zone | ||
_, err = s.GetMsgServer().UpdateInnerRedemptionRateBounds(s.Ctx, &tc.validMsg) | ||
// verify it throws an error | ||
errMsg = fmt.Sprintf("inner max safety threshold (%s) is greater than outer max safety threshold (%s)", tc.validMsg.MaxInnerRedemptionRate, sdk.NewDec(15).Quo(sdk.NewDec(10))) | ||
s.Require().ErrorContains(err, errMsg) | ||
} | ||
|
||
// Validate basic tests | ||
func (s *KeeperTestSuite) TestUpdateInnerRedemptionRateBounds_InvalidMsg() { | ||
tc := s.SetupUpdateInnerRedemptionRateBounds() | ||
|
||
// Set the min inner bound to be greater than than the max inner bound | ||
invalidMsg := tc.validMsg | ||
invalidMsg.MinInnerRedemptionRate = sdk.NewDec(2) | ||
|
||
err := invalidMsg.ValidateBasic() | ||
|
||
// Verify the error | ||
errMsg := fmt.Sprintf("Inner max safety threshold (%s) is less than inner min safety threshold (%s)", invalidMsg.MaxInnerRedemptionRate, invalidMsg.MinInnerRedemptionRate) | ||
s.Require().ErrorContains(err, errMsg) | ||
} | ||
|
||
// Verify that if inner bounds end up outside of outer bounds (somehow), the outer bounds are returned | ||
func (s *KeeperTestSuite) TestGetInnerSafetyBounds() { | ||
tc := s.SetupUpdateInnerRedemptionRateBounds() | ||
|
||
// Set the inner bounds outside the outer bounds on the host zone directly | ||
tc.zone.MinInnerRedemptionRate = sdk.NewDec(0) | ||
tc.zone.MaxInnerRedemptionRate = sdk.NewDec(3) | ||
// Set the host zone | ||
s.App.StakeibcKeeper.SetHostZone(s.Ctx, tc.zone) | ||
|
||
// Get the inner bounds and verify the outer bounds are used | ||
innerMinSafetyThreshold, innerMaxSafetyThreshold := s.App.StakeibcKeeper.GetInnerSafetyBounds(s.Ctx, tc.zone) | ||
s.Require().Equal(tc.zone.MinRedemptionRate, innerMinSafetyThreshold, "min inner redemption rate should be set") | ||
s.Require().Equal(tc.zone.MaxRedemptionRate, innerMaxSafetyThreshold, "max inner redemption rate should be set") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.