-
Notifications
You must be signed in to change notification settings - Fork 110
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
114 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
package keeper_test | ||
|
||
import ( | ||
"testing" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/stretchr/testify/require" | ||
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 createNNodeAccount(keeper *types.Keeper, ctx sdk.Context, n int) []types.NodeAccount { | ||
// items := make([]types.NodeAccount, n) | ||
// for i := range items { | ||
// items[i].Operator = fmt.Sprintf("%d", i) | ||
// keeper.SetNodeAccount(ctx, items[i]) | ||
// } | ||
// return items | ||
// } | ||
|
||
func TestMsgServer_UpdateKeygen(t *testing.T) { | ||
t.Run("should error if not authorized", func(t *testing.T) { | ||
k, ctx, _, _ := keepertest.ObserverKeeperWithMocks(t, keepertest.ObserverMockOptions{ | ||
UseAuthorityMock: true, | ||
}) | ||
authorityMock := keepertest.GetObserverAuthorityMock(t, k) | ||
admin := sample.AccAddress() | ||
keepertest.MockIsAuthorized(&authorityMock.Mock, admin, authoritytypes.PolicyType_groupEmergency, false) | ||
wctx := sdk.WrapSDKContext(ctx) | ||
|
||
srv := keeper.NewMsgServerImpl(*k) | ||
res, err := srv.UpdateKeygen(wctx, &types.MsgUpdateKeygen{ | ||
Creator: admin, | ||
}) | ||
require.Error(t, err) | ||
require.Equal(t, &types.MsgUpdateKeygenResponse{}, res) | ||
}) | ||
|
||
t.Run("should error if keygen not found", func(t *testing.T) { | ||
k, ctx, _, _ := keepertest.ObserverKeeperWithMocks(t, keepertest.ObserverMockOptions{ | ||
UseAuthorityMock: true, | ||
}) | ||
authorityMock := keepertest.GetObserverAuthorityMock(t, k) | ||
admin := sample.AccAddress() | ||
keepertest.MockIsAuthorized(&authorityMock.Mock, admin, authoritytypes.PolicyType_groupEmergency, true) | ||
wctx := sdk.WrapSDKContext(ctx) | ||
|
||
srv := keeper.NewMsgServerImpl(*k) | ||
res, err := srv.UpdateKeygen(wctx, &types.MsgUpdateKeygen{ | ||
Creator: admin, | ||
}) | ||
require.Error(t, err) | ||
require.Nil(t, res) | ||
}) | ||
|
||
t.Run("should error if msg block too low", func(t *testing.T) { | ||
k, ctx, _, _ := keepertest.ObserverKeeperWithMocks(t, keepertest.ObserverMockOptions{ | ||
UseAuthorityMock: true, | ||
}) | ||
authorityMock := keepertest.GetObserverAuthorityMock(t, k) | ||
admin := sample.AccAddress() | ||
keepertest.MockIsAuthorized(&authorityMock.Mock, admin, authoritytypes.PolicyType_groupEmergency, true) | ||
wctx := sdk.WrapSDKContext(ctx) | ||
item := types.Keygen{ | ||
BlockNumber: 10, | ||
} | ||
k.SetKeygen(ctx, item) | ||
srv := keeper.NewMsgServerImpl(*k) | ||
res, err := srv.UpdateKeygen(wctx, &types.MsgUpdateKeygen{ | ||
Creator: admin, | ||
Block: 2, | ||
}) | ||
require.Error(t, err) | ||
require.Nil(t, res) | ||
}) | ||
|
||
t.Run("should update", func(t *testing.T) { | ||
k, ctx, _, _ := keepertest.ObserverKeeperWithMocks(t, keepertest.ObserverMockOptions{ | ||
UseAuthorityMock: true, | ||
}) | ||
authorityMock := keepertest.GetObserverAuthorityMock(t, k) | ||
admin := sample.AccAddress() | ||
keepertest.MockIsAuthorized(&authorityMock.Mock, admin, authoritytypes.PolicyType_groupEmergency, true) | ||
wctx := sdk.WrapSDKContext(ctx) | ||
item := types.Keygen{ | ||
BlockNumber: 10, | ||
} | ||
k.SetKeygen(ctx, item) | ||
srv := keeper.NewMsgServerImpl(*k) | ||
|
||
granteePubKey := sample.PubKeySet() | ||
k.SetNodeAccount(ctx, types.NodeAccount{ | ||
Operator: "operator", | ||
GranteePubkey: granteePubKey, | ||
}) | ||
|
||
res, err := srv.UpdateKeygen(wctx, &types.MsgUpdateKeygen{ | ||
Creator: admin, | ||
Block: ctx.BlockHeight() + 30, | ||
}) | ||
require.NoError(t, err) | ||
require.Equal(t, &types.MsgUpdateKeygenResponse{}, res) | ||
|
||
keygen, found := k.GetKeygen(ctx) | ||
require.True(t, found) | ||
require.Equal(t, 1, len(keygen.GranteePubkeys)) | ||
require.Equal(t, granteePubKey.Secp256k1.String(), keygen.GranteePubkeys[0]) | ||
require.Equal(t, ctx.BlockHeight()+30, keygen.BlockNumber) | ||
require.Equal(t, types.KeygenStatus_PendingKeygen, keygen.Status) | ||
}) | ||
} |