Skip to content

Commit

Permalink
PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
skosito committed Mar 19, 2024
1 parent 18b30b9 commit 9aacf38
Show file tree
Hide file tree
Showing 7 changed files with 147 additions and 96 deletions.
4 changes: 2 additions & 2 deletions proto/observer/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ message MsgUpdateKeygenResponse {}
message MsgResetChainNonces {
string creator = 1;
int64 chain_id = 2;
uint64 chain_nonce_low = 3;
uint64 chain_nonce_high = 4;
int64 chain_nonce_low = 3;
int64 chain_nonce_high = 4;
}

message MsgResetChainNoncesResponse {}
8 changes: 4 additions & 4 deletions x/observer/client/cli/tx_reset_chain_nonces.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ func CmdResetChainNonces() *cobra.Command {
return err
}

// get chainNonceLow as uint64
chainNonceLow, err := strconv.ParseUint(args[1], 10, 64)
// get chainNonceLow as int64
chainNonceLow, err := strconv.ParseInt(args[1], 10, 64)
if err != nil {
return err
}

// get chainNonceHigh as uint64
chainNonceHigh, err := strconv.ParseUint(args[2], 10, 64)
// get chainNonceHigh as int64
chainNonceHigh, err := strconv.ParseInt(args[2], 10, 64)
if err != nil {
return err
}
Expand Down
9 changes: 4 additions & 5 deletions x/observer/keeper/msg_server_reset_chain_nonces.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,17 @@ func (k msgServer) ResetChainNonces(goCtx context.Context, msg *types.MsgResetCh
chainNonce := types.ChainNonces{
Index: chain.ChainName.String(),
ChainId: chain.ChainId,
Nonce: msg.ChainNonceHigh,
// #nosec G701 always positive
Nonce: uint64(msg.ChainNonceHigh),
// #nosec G701 always positive
FinalizedHeight: uint64(ctx.BlockHeight()),
}
k.SetChainNonces(ctx, chainNonce)

// set pending nonces
p := types.PendingNonces{
// #nosec G701 always in the range
NonceLow: int64(msg.ChainNonceLow),
// #nosec G701 always in the range
NonceHigh: int64(msg.ChainNonceHigh),
NonceLow: msg.ChainNonceLow,
NonceHigh: msg.ChainNonceHigh,
ChainId: chain.ChainId,
Tss: tss.TssPubkey,
}
Expand Down
8 changes: 4 additions & 4 deletions x/observer/keeper/msg_server_reset_chain_nonces_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ func TestMsgServer_ResetChainNonces(t *testing.T) {
_, err := srv.ResetChainNonces(sdk.WrapSDKContext(ctx), &types.MsgResetChainNonces{
Creator: admin,
ChainId: chainId,
ChainNonceLow: uint64(nonceLow),
ChainNonceHigh: uint64(nonceHigh),
ChainNonceLow: int64(nonceLow),
ChainNonceHigh: int64(nonceHigh),
})
require.NoError(t, err)

Expand All @@ -121,8 +121,8 @@ func TestMsgServer_ResetChainNonces(t *testing.T) {
_, err = srv.ResetChainNonces(sdk.WrapSDKContext(ctx), &types.MsgResetChainNonces{
Creator: admin,
ChainId: chainId,
ChainNonceLow: uint64(0),
ChainNonceHigh: uint64(0),
ChainNonceLow: 0,
ChainNonceHigh: 0,
})
require.NoError(t, err)

Expand Down
16 changes: 15 additions & 1 deletion x/observer/types/message_reset_chain_nonces.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package types

import (
"errors"

cosmoserrors "cosmossdk.io/errors"
"github.com/zeta-chain/zetacore/common"

Expand All @@ -12,7 +14,7 @@ const TypeMsgResetChainNonces = "reset_chain_nonces"

var _ sdk.Msg = &MsgResetChainNonces{}

func NewMsgResetChainNonces(creator string, chainID int64, chainNonceLow uint64, chainNonceHigh uint64) *MsgResetChainNonces {
func NewMsgResetChainNonces(creator string, chainID int64, chainNonceLow int64, chainNonceHigh int64) *MsgResetChainNonces {
return &MsgResetChainNonces{
Creator: creator,
ChainId: chainID,
Expand Down Expand Up @@ -54,5 +56,17 @@ func (msg *MsgResetChainNonces) ValidateBasic() error {
return cosmoserrors.Wrapf(sdkerrors.ErrInvalidChainID, "invalid chain id (%d)", msg.ChainId)
}

if msg.ChainNonceLow < 0 {
return errors.New("chain nonce low must be greater or equal 0")
}

if msg.ChainNonceHigh < 0 {
return errors.New("chain nonce high must be greater or equal 0")
}

if msg.ChainNonceLow > msg.ChainNonceHigh {
return errors.New("chain nonce low must be less or equal than chain nonce high")
}

return nil
}
58 changes: 48 additions & 10 deletions x/observer/types/message_reset_chain_nonces_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package types_test
import (
"testing"

sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/stretchr/testify/require"
"github.com/zeta-chain/zetacore/common"
"github.com/zeta-chain/zetacore/testutil/sample"
Expand All @@ -12,42 +11,81 @@ import (

func TestMsgResetChainNonces_ValidateBasic(t *testing.T) {
tests := []struct {
name string
msg types.MsgResetChainNonces
err error
name string
msg types.MsgResetChainNonces
wantErr bool
}{
{
name: "valid message",
name: "valid message chain nonce high greater than nonce low",
msg: types.MsgResetChainNonces{
Creator: sample.AccAddress(),
ChainId: common.ExternalChainList()[0].ChainId,
ChainNonceLow: 1,
ChainNonceHigh: 5,
},
wantErr: false,
},
{
name: "valid message chain nonce high same as nonce low",
msg: types.MsgResetChainNonces{
Creator: sample.AccAddress(),
ChainId: common.ExternalChainList()[0].ChainId,
ChainNonceLow: 1,
ChainNonceHigh: 1,
},
wantErr: false,
},
{
name: "invalid address",
msg: types.MsgResetChainNonces{
Creator: "invalid_address",
ChainId: common.ExternalChainList()[0].ChainId,
},
err: sdkerrors.ErrInvalidAddress,
wantErr: true,
},

{
name: "invalid chain ID",
msg: types.MsgResetChainNonces{
Creator: sample.AccAddress(),
ChainId: 999,
},
err: sdkerrors.ErrInvalidChainID,
wantErr: true,
},
{
name: "invalid chain nonce low",
msg: types.MsgResetChainNonces{
Creator: sample.AccAddress(),
ChainId: common.ExternalChainList()[0].ChainId,
ChainNonceLow: -1,
},
wantErr: true,
},
{
name: "invalid chain nonce high",
msg: types.MsgResetChainNonces{
Creator: sample.AccAddress(),
ChainId: common.ExternalChainList()[0].ChainId,
ChainNonceLow: 1,
ChainNonceHigh: -1,
},
wantErr: true,
},
{
name: "invalid chain nonce low greater than chain nonce high",
msg: types.MsgResetChainNonces{
Creator: sample.AccAddress(),
ChainId: common.ExternalChainList()[0].ChainId,
ChainNonceLow: 1,
ChainNonceHigh: 0,
},
wantErr: true,
},
}
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)
if tt.wantErr {
require.Error(t, err)
return
}
require.NoError(t, err)
Expand Down
Loading

0 comments on commit 9aacf38

Please sign in to comment.