Skip to content

Commit

Permalink
Merge branch 'add-msg-reset-chain-nonces' into release/v15
Browse files Browse the repository at this point in the history
  • Loading branch information
lumtis authored Mar 20, 2024
2 parents 741ae6b + 4020a71 commit 455aa6e
Show file tree
Hide file tree
Showing 9 changed files with 151 additions and 100 deletions.
4 changes: 2 additions & 2 deletions docs/spec/observer/messages.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@ Authorized: admin policy group 2 (admin update)
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;
}
```

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 {}
4 changes: 2 additions & 2 deletions typescript/observer/tx_pb.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -465,12 +465,12 @@ export declare class MsgResetChainNonces extends Message<MsgResetChainNonces> {
chainId: bigint;

/**
* @generated from field: uint64 chain_nonce_low = 3;
* @generated from field: int64 chain_nonce_low = 3;
*/
chainNonceLow: bigint;

/**
* @generated from field: uint64 chain_nonce_high = 4;
* @generated from field: int64 chain_nonce_high = 4;
*/
chainNonceHigh: bigint;

Expand Down
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 455aa6e

Please sign in to comment.