From d57c28b92b87f81174a60046adef9d2e8c4114e6 Mon Sep 17 00:00:00 2001 From: skosito Date: Wed, 20 Mar 2024 20:36:21 +0100 Subject: [PATCH] Use authority keeper and fix tests --- .../keeper/msg_server_reset_chain_nonces.go | 3 +- .../msg_server_reset_chain_nonces_test.go | 43 +++++++++++-------- 2 files changed, 26 insertions(+), 20 deletions(-) diff --git a/x/observer/keeper/msg_server_reset_chain_nonces.go b/x/observer/keeper/msg_server_reset_chain_nonces.go index 48a9f59eb4..b3d1731645 100644 --- a/x/observer/keeper/msg_server_reset_chain_nonces.go +++ b/x/observer/keeper/msg_server_reset_chain_nonces.go @@ -5,6 +5,7 @@ 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" ) @@ -12,7 +13,7 @@ import ( // 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 } diff --git a/x/observer/keeper/msg_server_reset_chain_nonces_test.go b/x/observer/keeper/msg_server_reset_chain_nonces_test.go index 9a0c8cd696..46da328fe8 100644 --- a/x/observer/keeper/msg_server_reset_chain_nonces_test.go +++ b/x/observer/keeper/msg_server_reset_chain_nonces_test.go @@ -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, @@ -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{ @@ -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, @@ -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()