Skip to content

Commit

Permalink
Use authority keeper and fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
skosito committed Mar 20, 2024
1 parent 4a3c832 commit d57c28b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 20 deletions.
3 changes: 2 additions & 1 deletion x/observer/keeper/msg_server_reset_chain_nonces.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ import (

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/zeta-chain/zetacore/common"
authoritytypes "github.com/zeta-chain/zetacore/x/authority/types"
"github.com/zeta-chain/zetacore/x/observer/types"
)

// ResetChainNonces handles resetting chain nonces
// Authorized: admin policy group 2 (admin update)
func (k msgServer) ResetChainNonces(goCtx context.Context, msg *types.MsgResetChainNonces) (*types.MsgResetChainNoncesResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
if msg.Creator != k.GetParams(ctx).GetAdminPolicyAccount(types.Policy_Type_group2) {
if !k.GetAuthorityKeeper().IsAuthorized(ctx, msg.Creator, authoritytypes.PolicyType_groupAdmin) {
return &types.MsgResetChainNoncesResponse{}, types.ErrNotAuthorizedPolicy
}

Expand Down
43 changes: 24 additions & 19 deletions x/observer/keeper/msg_server_reset_chain_nonces_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,25 @@ import (
"github.com/zeta-chain/zetacore/common"
keepertest "github.com/zeta-chain/zetacore/testutil/keeper"
"github.com/zeta-chain/zetacore/testutil/sample"
authoritytypes "github.com/zeta-chain/zetacore/x/authority/types"
"github.com/zeta-chain/zetacore/x/observer/keeper"
"github.com/zeta-chain/zetacore/x/observer/types"
)

func TestMsgServer_ResetChainNonces(t *testing.T) {
t.Run("cannot reset chain nonces if not authorized", func(t *testing.T) {
k, ctx := keepertest.ObserverKeeper(t)
k, ctx, _, _ := keepertest.ObserverKeeperWithMocks(t, keepertest.ObserverMockOptions{
UseAuthorityMock: true,
})
srv := keeper.NewMsgServerImpl(*k)

chainId := common.GoerliLocalnetChain().ChainId
_, err := srv.ResetChainNonces(sdk.WrapSDKContext(ctx), &types.MsgResetChainNonces{
Creator: sample.AccAddress(),
ChainId: chainId,
ChainNonceLow: 1,
ChainNonceHigh: 5,
})
require.ErrorIs(t, err, types.ErrNotAuthorizedPolicy)

// group 1 should not be able to reset chain nonces
admin := sample.AccAddress()
setAdminCrossChainFlags(ctx, k, admin, types.Policy_Type_group1)
authorityMock := keepertest.GetObserverAuthorityMock(t, k)
keepertest.MockIsAuthorized(&authorityMock.Mock, admin, authoritytypes.PolicyType_groupAdmin, false)

_, err = srv.ResetChainNonces(sdk.WrapSDKContext(ctx), &types.MsgResetChainNonces{
Creator: sample.AccAddress(),
_, err := srv.ResetChainNonces(sdk.WrapSDKContext(ctx), &types.MsgResetChainNonces{
Creator: admin,
ChainId: chainId,
ChainNonceLow: 1,
ChainNonceHigh: 5,
Expand All @@ -40,11 +35,14 @@ func TestMsgServer_ResetChainNonces(t *testing.T) {
})

t.Run("cannot reset chain nonces if tss not found", func(t *testing.T) {
k, ctx := keepertest.ObserverKeeper(t)
k, ctx, _, _ := keepertest.ObserverKeeperWithMocks(t, keepertest.ObserverMockOptions{
UseAuthorityMock: true,
})
srv := keeper.NewMsgServerImpl(*k)

admin := sample.AccAddress()
setAdminCrossChainFlags(ctx, k, admin, types.Policy_Type_group2)
authorityMock := keepertest.GetObserverAuthorityMock(t, k)
keepertest.MockIsAuthorized(&authorityMock.Mock, admin, authoritytypes.PolicyType_groupAdmin, true)

chainId := common.GoerliLocalnetChain().ChainId
_, err := srv.ResetChainNonces(sdk.WrapSDKContext(ctx), &types.MsgResetChainNonces{
Expand All @@ -57,13 +55,16 @@ func TestMsgServer_ResetChainNonces(t *testing.T) {
})

t.Run("cannot reset chain nonces if chain not supported", func(t *testing.T) {
k, ctx := keepertest.ObserverKeeper(t)
k, ctx, _, _ := keepertest.ObserverKeeperWithMocks(t, keepertest.ObserverMockOptions{
UseAuthorityMock: true,
})
srv := keeper.NewMsgServerImpl(*k)
tss := sample.Tss()
k.SetTSS(ctx, tss)

admin := sample.AccAddress()
setAdminCrossChainFlags(ctx, k, admin, types.Policy_Type_group2)
authorityMock := keepertest.GetObserverAuthorityMock(t, k)
keepertest.MockIsAuthorized(&authorityMock.Mock, admin, authoritytypes.PolicyType_groupAdmin, true)

_, err := srv.ResetChainNonces(sdk.WrapSDKContext(ctx), &types.MsgResetChainNonces{
Creator: admin,
Expand All @@ -75,13 +76,17 @@ func TestMsgServer_ResetChainNonces(t *testing.T) {
})

t.Run("can reset chain nonces", func(t *testing.T) {
k, ctx := keepertest.ObserverKeeper(t)
k, ctx, _, _ := keepertest.ObserverKeeperWithMocks(t, keepertest.ObserverMockOptions{
UseAuthorityMock: true,
})
srv := keeper.NewMsgServerImpl(*k)
tss := sample.Tss()
k.SetTSS(ctx, tss)

admin := sample.AccAddress()
setAdminCrossChainFlags(ctx, k, admin, types.Policy_Type_group2)
authorityMock := keepertest.GetObserverAuthorityMock(t, k)
keepertest.MockIsAuthorized(&authorityMock.Mock, admin, authoritytypes.PolicyType_groupAdmin, true)
keepertest.MockIsAuthorized(&authorityMock.Mock, admin, authoritytypes.PolicyType_groupAdmin, true)

chainId := common.GoerliLocalnetChain().ChainId
index := common.GoerliLocalnetChain().ChainName.String()
Expand Down

0 comments on commit d57c28b

Please sign in to comment.