Skip to content

Commit

Permalink
fix validate basic check (Stride-Labs#258)
Browse files Browse the repository at this point in the history
Co-authored-by: Aidan Salzmann <[email protected]>
  • Loading branch information
sampocs and asalzmann authored Sep 2, 2022
1 parent 566ce32 commit a5ba2a7
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 8 deletions.
7 changes: 7 additions & 0 deletions x/stakeibc/types/message_claim_undelegated_tokens.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,12 @@ func (msg *MsgClaimUndelegatedTokens) ValidateBasic() error {
if err != nil {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidAddress, "invalid sender address (%s)", err)
}
// validate host denom is not empty
if msg.HostZoneId == "" {
return sdkerrors.Wrapf(ErrRequiredFieldEmpty, "host zone id cannot be empty")
}
if !(msg.Epoch < (1<<63 - 1)) {
return sdkerrors.Wrapf(ErrInvalidAmount, "epoch must be less than math.MaxInt64 %d", 1<<63-1)
}
return nil
}
36 changes: 31 additions & 5 deletions x/stakeibc/types/message_claim_undelegated_tokens_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package types

import (
"math"
"testing"

"github.com/Stride-Labs/stride/testutil/sample"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/stretchr/testify/require"

"github.com/Stride-Labs/stride/testutil/sample"
)

func TestMsgClaimUndelegatedTokens_ValidateBasic(t *testing.T) {
Expand All @@ -14,19 +16,43 @@ func TestMsgClaimUndelegatedTokens_ValidateBasic(t *testing.T) {
msg MsgClaimUndelegatedTokens
err error
}{
{
name: "success",
msg: MsgClaimUndelegatedTokens{
Creator: sample.AccAddress(),
Sender: sample.StrideAddress(),
HostZoneId: "GAIA",
Epoch: uint64(1),
},
},
{
name: "invalid address",
msg: MsgClaimUndelegatedTokens{
Creator: "invalid_address",
Sender: sample.StrideAddress(),
Creator: "invalid_address",
Sender: sample.StrideAddress(),
HostZoneId: "GAIA",
Epoch: uint64(1),
},
err: sdkerrors.ErrInvalidAddress,
}, {
name: "valid address",
},
{
name: "no host zone",
msg: MsgClaimUndelegatedTokens{
Creator: sample.AccAddress(),
Sender: sample.StrideAddress(),
Epoch: uint64(1),
},
err: ErrRequiredFieldEmpty,
},
{
name: "epoch max int",
msg: MsgClaimUndelegatedTokens{
Creator: sample.AccAddress(),
Sender: sample.StrideAddress(),
HostZoneId: "GAIA",
Epoch: math.MaxUint64,
},
err: ErrInvalidAmount,
},
}
for _, tt := range tests {
Expand Down
6 changes: 3 additions & 3 deletions x/stakeibc/types/message_liquid_stake.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ func (msg *MsgLiquidStake) ValidateBasic() error {
if msg.HostDenom == "" {
return sdkerrors.Wrapf(ErrRequiredFieldEmpty, "host denom cannot be empty")
}
// Math.MaxUint32 == 1<<32 - 1
if !(msg.Amount < (1<<32 - 1)) {
return sdkerrors.Wrapf(ErrInvalidAmount, "amount liquid staked must be less than %d", 1<<32-1)
// math.MaxInt64 == 1<<63 - 1
if !(msg.Amount < (1<<63 - 1)) {
return sdkerrors.Wrapf(ErrInvalidAmount, "amount liquid staked must be less than math.MaxInt64 %d", 1<<63-1)
}
// host denom must be a valid asset denom
if err := sdk.ValidateDenom(msg.HostDenom); err != nil {
Expand Down
14 changes: 14 additions & 0 deletions x/stakeibc/types/message_redeem_stake.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,27 @@ func (msg *MsgRedeemStake) GetSignBytes() []byte {
}

func (msg *MsgRedeemStake) ValidateBasic() error {
// check valid creator address
_, err := sdk.AccAddressFromBech32(msg.Creator)
if err != nil {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidAddress, "invalid creator address (%s)", err)
}
// check valid receiver address
_, err = sdk.AccAddressFromBech32(msg.Receiver)
if err != nil {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidAddress, "invalid receiver address (%s)", err)
}
// ensure amount is a nonzero positive integer
if msg.Amount <= 0 {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "invalid amount (%d)", msg.Amount)
}
// validate host zone is not empty
if msg.HostZone == "" {
return sdkerrors.Wrapf(ErrRequiredFieldEmpty, "host zone cannot be empty")
}
// math.MaxInt64 == 1<<63 - 1
if !(msg.Amount < (1<<63 - 1)) {
return sdkerrors.Wrapf(ErrInvalidAmount, "amount liquid staked must be less than math.MaxInt64 %d", 1<<63-1)
}
return nil
}
78 changes: 78 additions & 0 deletions x/stakeibc/types/message_redeem_stake_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package types

import (
"math"
"testing"

sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/stretchr/testify/require"

"github.com/Stride-Labs/stride/testutil/sample"
)

func TestMsgRedeemStake_ValidateBasic(t *testing.T) {
tests := []struct {
name string
msg MsgRedeemStake
err error
}{
{
name: "success",
msg: MsgRedeemStake{
Creator: sample.AccAddress(),
HostZone: "GAIA",
Receiver: sample.AccAddress(),
Amount: uint64(1),
},
},
{
name: "invalid creator",
msg: MsgRedeemStake{
Creator: "invalid_address",
HostZone: "GAIA",
Receiver: sample.AccAddress(),
Amount: uint64(1),
},
err: sdkerrors.ErrInvalidAddress,
},
{
name: "no host zone",
msg: MsgRedeemStake{
Creator: sample.AccAddress(),
Receiver: sample.AccAddress(),
Amount: uint64(1),
},
err: ErrRequiredFieldEmpty,
},
{
name: "invalid receiver",
msg: MsgRedeemStake{
Creator: sample.AccAddress(),
Receiver: "invalid_address",
HostZone: "GAIA",
Amount: uint64(1),
},
err: sdkerrors.ErrInvalidAddress,
},
{
name: "amount max int",
msg: MsgRedeemStake{
Creator: sample.AccAddress(),
HostZone: "GAIA",
Receiver: sample.AccAddress(),
Amount: math.MaxUint64,
},
err: ErrInvalidAmount,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := tt.msg.ValidateBasic()
if tt.err != nil {
require.ErrorIs(t, err, tt.err)
return
}
require.NoError(t, err)
})
}
}

0 comments on commit a5ba2a7

Please sign in to comment.