Skip to content

Commit

Permalink
add params validation to bridge message validation and add non-empty …
Browse files Browse the repository at this point in the history
…authority unit test (#570)
  • Loading branch information
tqin7 authored Oct 11, 2023
1 parent 0a98a8d commit 6375de1
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 23 deletions.
5 changes: 3 additions & 2 deletions protocol/x/bridge/types/msg_update_event_params.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package types

import (
errorsmod "cosmossdk.io/errors"
"fmt"

errorsmod "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
)

Expand All @@ -22,5 +23,5 @@ func (msg *MsgUpdateEventParams) ValidateBasic() error {
),
)
}
return nil
return msg.Params.Validate()
}
38 changes: 33 additions & 5 deletions protocol/x/bridge/types/msg_update_event_params_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func TestMsgUpdateEventParams_GetSigners(t *testing.T) {
func TestMsgUpdateEventParams_ValidateBasic(t *testing.T) {
tests := map[string]struct {
msg types.MsgUpdateEventParams
expectedErr error
expectedErr string
}{
"Success": {
msg: types.MsgUpdateEventParams{
Expand All @@ -36,21 +36,49 @@ func TestMsgUpdateEventParams_ValidateBasic(t *testing.T) {
},
},
},
"Failure: Invalid authority": {
"Failure: invalid Denom param": {
msg: types.MsgUpdateEventParams{
Authority: validAuthority,
Params: types.EventParams{
Denom: "2test-denom", // cannot start with number
EthChainId: 0,
EthAddress: "test",
},
},
expectedErr: "invalid denom",
},
"Failure: invalid EthAddress param": {
msg: types.MsgUpdateEventParams{
Authority: validAuthority,
Params: types.EventParams{
Denom: "test-denom",
EthChainId: 0,
EthAddress: "", // cannot be empty
},
},
expectedErr: types.ErrInvalidEthAddress.Error(),
},
"Failure: empty authority": {
msg: types.MsgUpdateEventParams{
Authority: "",
},
expectedErr: types.ErrInvalidAuthority,
expectedErr: types.ErrInvalidAuthority.Error(),
},
"Failure: invalid authority": {
msg: types.MsgUpdateEventParams{
Authority: "dydx1abc",
},
expectedErr: types.ErrInvalidAuthority.Error(),
},
}

for name, tc := range tests {
t.Run(name, func(t *testing.T) {
err := tc.msg.ValidateBasic()
if tc.expectedErr == nil {
if tc.expectedErr == "" {
require.NoError(t, err)
} else {
require.ErrorIs(t, err, tc.expectedErr)
require.ErrorContains(t, err, tc.expectedErr)
}
})
}
Expand Down
5 changes: 3 additions & 2 deletions protocol/x/bridge/types/msg_update_propose_params.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package types

import (
errorsmod "cosmossdk.io/errors"
"fmt"

errorsmod "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
)

Expand All @@ -22,5 +23,5 @@ func (msg *MsgUpdateProposeParams) ValidateBasic() error {
),
)
}
return nil
return msg.Params.Validate()
}
57 changes: 50 additions & 7 deletions protocol/x/bridge/types/msg_update_propose_params_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package types_test

import (
"testing"
"time"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/dydxprotocol/v4-chain/protocol/testutil/constants"
Expand All @@ -19,34 +20,76 @@ func TestMsgUpdateProposeParams_GetSigners(t *testing.T) {
func TestMsgUpdateProposeParams_ValidateBasic(t *testing.T) {
tests := map[string]struct {
msg types.MsgUpdateProposeParams
expectedErr error
expectedErr string
}{
"Success": {
msg: types.MsgUpdateProposeParams{
Authority: validAuthority,
Params: types.ProposeParams{
MaxBridgesPerBlock: 5,
ProposeDelayDuration: 10_000,
ProposeDelayDuration: time.Second,
SkipRatePpm: 800_000,
SkipIfBlockDelayedByDuration: 5_000,
SkipIfBlockDelayedByDuration: time.Minute,
},
},
},
"Failure: Invalid authority": {
"Failure: negative propose delay duration": {
msg: types.MsgUpdateProposeParams{
Authority: validAuthority,
Params: types.ProposeParams{
MaxBridgesPerBlock: 5,
ProposeDelayDuration: -time.Second,
SkipRatePpm: 800_000,
SkipIfBlockDelayedByDuration: time.Minute,
},
},
expectedErr: types.ErrNegativeDuration.Error(),
},
"Failure: negative skip if blocked delayed by duration": {
msg: types.MsgUpdateProposeParams{
Authority: validAuthority,
Params: types.ProposeParams{
MaxBridgesPerBlock: 5,
ProposeDelayDuration: time.Second,
SkipRatePpm: 800_000,
SkipIfBlockDelayedByDuration: -time.Minute,
},
},
expectedErr: types.ErrNegativeDuration.Error(),
},
"Failure: out-of-bound skip rate ppm": {
msg: types.MsgUpdateProposeParams{
Authority: validAuthority,
Params: types.ProposeParams{
MaxBridgesPerBlock: 5,
ProposeDelayDuration: time.Second,
SkipRatePpm: 1_000_001,
SkipIfBlockDelayedByDuration: time.Minute,
},
},
expectedErr: types.ErrRateOutOfBounds.Error(),
},
"Failure: empty authority": {
msg: types.MsgUpdateProposeParams{
Authority: "",
},
expectedErr: types.ErrInvalidAuthority,
expectedErr: types.ErrInvalidAuthority.Error(),
},
"Failure: invalid authority": {
msg: types.MsgUpdateProposeParams{
Authority: "dydx1abc",
},
expectedErr: types.ErrInvalidAuthority.Error(),
},
}

for name, tc := range tests {
t.Run(name, func(t *testing.T) {
err := tc.msg.ValidateBasic()
if tc.expectedErr == nil {
if tc.expectedErr == "" {
require.NoError(t, err)
} else {
require.ErrorIs(t, err, tc.expectedErr)
require.ErrorContains(t, err, tc.expectedErr)
}
})
}
Expand Down
5 changes: 3 additions & 2 deletions protocol/x/bridge/types/msg_update_safety_params.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package types

import (
errorsmod "cosmossdk.io/errors"
"fmt"

errorsmod "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
)

Expand All @@ -22,5 +23,5 @@ func (msg *MsgUpdateSafetyParams) ValidateBasic() error {
),
)
}
return nil
return msg.Params.Validate()
}
17 changes: 12 additions & 5 deletions protocol/x/bridge/types/msg_update_safety_params_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func TestMsgUpdateSafetyParams_GetSigners(t *testing.T) {
func TestMsgUpdateSafetyParams_ValidateBasic(t *testing.T) {
tests := map[string]struct {
msg types.MsgUpdateSafetyParams
expectedErr error
expectedErr string
}{
"Success": {
msg: types.MsgUpdateSafetyParams{
Expand All @@ -30,20 +30,27 @@ func TestMsgUpdateSafetyParams_ValidateBasic(t *testing.T) {
},
},
},
"Failure: Invalid authority": {
"Failure: empty authority": {
msg: types.MsgUpdateSafetyParams{
Authority: "",
},
expectedErr: types.ErrInvalidAuthority,
expectedErr: types.ErrInvalidAuthority.Error(),
},
"Failure: invalid authority": {
msg: types.MsgUpdateSafetyParams{
Authority: "dydx1abc",
},
expectedErr: types.ErrInvalidAuthority.Error(),
},
}

for name, tc := range tests {
t.Run(name, func(t *testing.T) {
err := tc.msg.ValidateBasic()
if tc.expectedErr == nil {
if tc.expectedErr == "" {
require.NoError(t, err)
} else {
require.ErrorIs(t, err, tc.expectedErr)
require.ErrorContains(t, err, tc.expectedErr)
}
})
}
Expand Down

0 comments on commit 6375de1

Please sign in to comment.