From 47661d4df7f9029345df4c3de447889eac3dccdb Mon Sep 17 00:00:00 2001
From: skosito <skostic9242@gmail.com>
Date: Fri, 5 Apr 2024 21:28:46 +0100
Subject: [PATCH] test: remove setupKeeper from crosschain module (#1992)

* Remove setupKeeper from crosschain module

* Changelog

---------

Co-authored-by: Lucas Bertrand <lucas.bertrand.22@gmail.com>
---
 changelog.md                                  |  1 +
 x/crosschain/keeper/gas_price_test.go         | 27 ++++----
 .../keeper/grpc_query_gas_price_test.go       | 21 +++---
 .../grpc_query_last_block_height_test.go      | 33 +++++-----
 x/crosschain/keeper/keeper_test.go            | 65 -------------------
 x/crosschain/keeper/last_block_height_test.go | 26 ++++----
 6 files changed, 58 insertions(+), 115 deletions(-)
 delete mode 100644 x/crosschain/keeper/keeper_test.go

diff --git a/changelog.md b/changelog.md
index 73e8e04e28..da05d7b9b4 100644
--- a/changelog.md
+++ b/changelog.md
@@ -58,6 +58,7 @@
 * [1955](https://github.com/zeta-chain/node/pull/1955) - improve emissions module coverage
 * [1941](https://github.com/zeta-chain/node/pull/1941) - add unit tests for zetabridge package
 * [1985](https://github.com/zeta-chain/node/pull/1985) - improve fungible module coverage
+* [1992](https://github.com/zeta-chain/node/pull/1992) - remove setupKeeper from crosschain module
 
 ### Fixes
 
diff --git a/x/crosschain/keeper/gas_price_test.go b/x/crosschain/keeper/gas_price_test.go
index 67aeb95cd6..bdd867081e 100644
--- a/x/crosschain/keeper/gas_price_test.go
+++ b/x/crosschain/keeper/gas_price_test.go
@@ -1,4 +1,4 @@
-package keeper
+package keeper_test
 
 import (
 	"strconv"
@@ -6,11 +6,13 @@ import (
 
 	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/x/crosschain/keeper"
 	"github.com/zeta-chain/zetacore/x/crosschain/types"
 )
 
 // Keeper Tests
-func createNGasPrice(keeper *Keeper, ctx sdk.Context, n int) []types.GasPrice {
+func createNGasPrice(keeper *keeper.Keeper, ctx sdk.Context, n int) []types.GasPrice {
 	items := make([]types.GasPrice, n)
 	for i := range items {
 		items[i].Creator = "any"
@@ -22,26 +24,27 @@ func createNGasPrice(keeper *Keeper, ctx sdk.Context, n int) []types.GasPrice {
 }
 
 func TestGasPriceGet(t *testing.T) {
-	keeper, ctx := setupKeeper(t)
-	items := createNGasPrice(keeper, ctx, 10)
+	k, ctx, _, _ := keepertest.CrosschainKeeper(t)
+	items := createNGasPrice(k, ctx, 10)
 	for _, item := range items {
-		rst, found := keeper.GetGasPrice(ctx, item.ChainId)
+		rst, found := k.GetGasPrice(ctx, item.ChainId)
 		require.True(t, found)
 		require.Equal(t, item, rst)
 	}
 }
+
 func TestGasPriceRemove(t *testing.T) {
-	keeper, ctx := setupKeeper(t)
-	items := createNGasPrice(keeper, ctx, 10)
+	k, ctx, _, _ := keepertest.CrosschainKeeper(t)
+	items := createNGasPrice(k, ctx, 10)
 	for _, item := range items {
-		keeper.RemoveGasPrice(ctx, item.Index)
-		_, found := keeper.GetGasPrice(ctx, item.ChainId)
+		k.RemoveGasPrice(ctx, item.Index)
+		_, found := k.GetGasPrice(ctx, item.ChainId)
 		require.False(t, found)
 	}
 }
 
 func TestGasPriceGetAll(t *testing.T) {
-	keeper, ctx := setupKeeper(t)
-	items := createNGasPrice(keeper, ctx, 10)
-	require.Equal(t, items, keeper.GetAllGasPrice(ctx))
+	k, ctx, _, _ := keepertest.CrosschainKeeper(t)
+	items := createNGasPrice(k, ctx, 10)
+	require.Equal(t, items, k.GetAllGasPrice(ctx))
 }
diff --git a/x/crosschain/keeper/grpc_query_gas_price_test.go b/x/crosschain/keeper/grpc_query_gas_price_test.go
index b8e9f0606e..bba8f9ae74 100644
--- a/x/crosschain/keeper/grpc_query_gas_price_test.go
+++ b/x/crosschain/keeper/grpc_query_gas_price_test.go
@@ -1,4 +1,4 @@
-package keeper
+package keeper_test
 
 import (
 	"fmt"
@@ -7,15 +7,16 @@ import (
 	sdk "github.com/cosmos/cosmos-sdk/types"
 	"github.com/cosmos/cosmos-sdk/types/query"
 	"github.com/stretchr/testify/require"
+	keepertest "github.com/zeta-chain/zetacore/testutil/keeper"
 	"github.com/zeta-chain/zetacore/x/crosschain/types"
 	"google.golang.org/grpc/codes"
 	"google.golang.org/grpc/status"
 )
 
 func TestGasPriceQuerySingle(t *testing.T) {
-	keeper, ctx := setupKeeper(t)
+	k, ctx, _, _ := keepertest.CrosschainKeeper(t)
 	wctx := sdk.WrapSDKContext(ctx)
-	msgs := createNGasPrice(keeper, ctx, 2)
+	msgs := createNGasPrice(k, ctx, 2)
 	for _, tc := range []struct {
 		desc     string
 		request  *types.QueryGetGasPriceRequest
@@ -49,7 +50,7 @@ func TestGasPriceQuerySingle(t *testing.T) {
 	} {
 		tc := tc
 		t.Run(tc.desc, func(t *testing.T) {
-			response, err := keeper.GasPrice(wctx, tc.request)
+			response, err := k.GasPrice(wctx, tc.request)
 			if tc.err != nil {
 				require.Error(t, err)
 			} else {
@@ -60,9 +61,9 @@ func TestGasPriceQuerySingle(t *testing.T) {
 }
 
 func TestGasPriceQueryPaginated(t *testing.T) {
-	keeper, ctx := setupKeeper(t)
+	k, ctx, _, _ := keepertest.CrosschainKeeper(t)
 	wctx := sdk.WrapSDKContext(ctx)
-	msgs := createNGasPrice(keeper, ctx, 5)
+	msgs := createNGasPrice(k, ctx, 5)
 
 	request := func(next []byte, offset, limit uint64, total bool) *types.QueryAllGasPriceRequest {
 		return &types.QueryAllGasPriceRequest{
@@ -77,7 +78,7 @@ func TestGasPriceQueryPaginated(t *testing.T) {
 	t.Run("ByOffset", func(t *testing.T) {
 		step := 2
 		for i := 0; i < len(msgs); i += step {
-			resp, err := keeper.GasPriceAll(wctx, request(nil, uint64(i), uint64(step), false))
+			resp, err := k.GasPriceAll(wctx, request(nil, uint64(i), uint64(step), false))
 			require.NoError(t, err)
 			for j := i; j < len(msgs) && j < i+step; j++ {
 				require.Equal(t, &msgs[j], resp.GasPrice[j-i])
@@ -88,7 +89,7 @@ func TestGasPriceQueryPaginated(t *testing.T) {
 		step := 2
 		var next []byte
 		for i := 0; i < len(msgs); i += step {
-			resp, err := keeper.GasPriceAll(wctx, request(next, 0, uint64(step), false))
+			resp, err := k.GasPriceAll(wctx, request(next, 0, uint64(step), false))
 			require.NoError(t, err)
 			for j := i; j < len(msgs) && j < i+step; j++ {
 				require.Equal(t, &msgs[j], resp.GasPrice[j-i])
@@ -97,12 +98,12 @@ func TestGasPriceQueryPaginated(t *testing.T) {
 		}
 	})
 	t.Run("Total", func(t *testing.T) {
-		resp, err := keeper.GasPriceAll(wctx, request(nil, 0, 0, true))
+		resp, err := k.GasPriceAll(wctx, request(nil, 0, 0, true))
 		require.NoError(t, err)
 		require.Equal(t, len(msgs), int(resp.Pagination.Total))
 	})
 	t.Run("InvalidRequest", func(t *testing.T) {
-		_, err := keeper.GasPriceAll(wctx, nil)
+		_, err := k.GasPriceAll(wctx, nil)
 		require.ErrorIs(t, err, status.Error(codes.InvalidArgument, "invalid request"))
 	})
 }
diff --git a/x/crosschain/keeper/grpc_query_last_block_height_test.go b/x/crosschain/keeper/grpc_query_last_block_height_test.go
index 057fe120b9..81e3c7d2d9 100644
--- a/x/crosschain/keeper/grpc_query_last_block_height_test.go
+++ b/x/crosschain/keeper/grpc_query_last_block_height_test.go
@@ -1,4 +1,4 @@
-package keeper
+package keeper_test
 
 import (
 	"math"
@@ -7,15 +7,16 @@ import (
 	sdk "github.com/cosmos/cosmos-sdk/types"
 	"github.com/cosmos/cosmos-sdk/types/query"
 	"github.com/stretchr/testify/require"
+	keepertest "github.com/zeta-chain/zetacore/testutil/keeper"
 	"github.com/zeta-chain/zetacore/x/crosschain/types"
 	"google.golang.org/grpc/codes"
 	"google.golang.org/grpc/status"
 )
 
 func TestLastBlockHeightQuerySingle(t *testing.T) {
-	keeper, ctx := setupKeeper(t)
+	k, ctx, _, _ := keepertest.CrosschainKeeper(t)
 	wctx := sdk.WrapSDKContext(ctx)
-	msgs := createNLastBlockHeight(keeper, ctx, 2)
+	msgs := createNLastBlockHeight(k, ctx, 2)
 	for _, tc := range []struct {
 		desc     string
 		request  *types.QueryGetLastBlockHeightRequest
@@ -44,7 +45,7 @@ func TestLastBlockHeightQuerySingle(t *testing.T) {
 	} {
 		tc := tc
 		t.Run(tc.desc, func(t *testing.T) {
-			response, err := keeper.LastBlockHeight(wctx, tc.request)
+			response, err := k.LastBlockHeight(wctx, tc.request)
 			if tc.err != nil {
 				require.ErrorIs(t, err, tc.err)
 			} else {
@@ -56,14 +57,14 @@ func TestLastBlockHeightQuerySingle(t *testing.T) {
 
 func TestLastBlockHeightLimits(t *testing.T) {
 	t.Run("should err if last send height is max int", func(t *testing.T) {
-		keeper, ctx := setupKeeper(t)
+		k, ctx, _, _ := keepertest.CrosschainKeeper(t)
 		wctx := sdk.WrapSDKContext(ctx)
-		keeper.SetLastBlockHeight(ctx, types.LastBlockHeight{
+		k.SetLastBlockHeight(ctx, types.LastBlockHeight{
 			Index:          "index",
 			LastSendHeight: math.MaxInt64,
 		})
 
-		res, err := keeper.LastBlockHeight(wctx, &types.QueryGetLastBlockHeightRequest{
+		res, err := k.LastBlockHeight(wctx, &types.QueryGetLastBlockHeightRequest{
 			Index: "index",
 		})
 		require.Nil(t, res)
@@ -71,15 +72,15 @@ func TestLastBlockHeightLimits(t *testing.T) {
 	})
 
 	t.Run("should err if last receive height is max int", func(t *testing.T) {
-		keeper, ctx := setupKeeper(t)
+		k, ctx, _, _ := keepertest.CrosschainKeeper(t)
 		wctx := sdk.WrapSDKContext(ctx)
-		keeper.SetLastBlockHeight(ctx, types.LastBlockHeight{
+		k.SetLastBlockHeight(ctx, types.LastBlockHeight{
 			Index:             "index",
 			LastSendHeight:    10,
 			LastReceiveHeight: math.MaxInt64,
 		})
 
-		res, err := keeper.LastBlockHeight(wctx, &types.QueryGetLastBlockHeightRequest{
+		res, err := k.LastBlockHeight(wctx, &types.QueryGetLastBlockHeightRequest{
 			Index: "index",
 		})
 		require.Nil(t, res)
@@ -88,9 +89,9 @@ func TestLastBlockHeightLimits(t *testing.T) {
 }
 
 func TestLastBlockHeightQueryPaginated(t *testing.T) {
-	keeper, ctx := setupKeeper(t)
+	k, ctx, _, _ := keepertest.CrosschainKeeper(t)
 	wctx := sdk.WrapSDKContext(ctx)
-	msgs := createNLastBlockHeight(keeper, ctx, 5)
+	msgs := createNLastBlockHeight(k, ctx, 5)
 
 	request := func(next []byte, offset, limit uint64, total bool) *types.QueryAllLastBlockHeightRequest {
 		return &types.QueryAllLastBlockHeightRequest{
@@ -105,7 +106,7 @@ func TestLastBlockHeightQueryPaginated(t *testing.T) {
 	t.Run("ByOffset", func(t *testing.T) {
 		step := 2
 		for i := 0; i < len(msgs); i += step {
-			resp, err := keeper.LastBlockHeightAll(wctx, request(nil, uint64(i), uint64(step), false))
+			resp, err := k.LastBlockHeightAll(wctx, request(nil, uint64(i), uint64(step), false))
 			require.NoError(t, err)
 			for j := i; j < len(msgs) && j < i+step; j++ {
 				require.Equal(t, &msgs[j], resp.LastBlockHeight[j-i])
@@ -116,7 +117,7 @@ func TestLastBlockHeightQueryPaginated(t *testing.T) {
 		step := 2
 		var next []byte
 		for i := 0; i < len(msgs); i += step {
-			resp, err := keeper.LastBlockHeightAll(wctx, request(next, 0, uint64(step), false))
+			resp, err := k.LastBlockHeightAll(wctx, request(next, 0, uint64(step), false))
 			require.NoError(t, err)
 			for j := i; j < len(msgs) && j < i+step; j++ {
 				require.Equal(t, &msgs[j], resp.LastBlockHeight[j-i])
@@ -125,12 +126,12 @@ func TestLastBlockHeightQueryPaginated(t *testing.T) {
 		}
 	})
 	t.Run("Total", func(t *testing.T) {
-		resp, err := keeper.LastBlockHeightAll(wctx, request(nil, 0, 0, true))
+		resp, err := k.LastBlockHeightAll(wctx, request(nil, 0, 0, true))
 		require.NoError(t, err)
 		require.Equal(t, len(msgs), int(resp.Pagination.Total))
 	})
 	t.Run("InvalidRequest", func(t *testing.T) {
-		_, err := keeper.LastBlockHeightAll(wctx, nil)
+		_, err := k.LastBlockHeightAll(wctx, nil)
 		require.ErrorIs(t, err, status.Error(codes.InvalidArgument, "invalid request"))
 	})
 }
diff --git a/x/crosschain/keeper/keeper_test.go b/x/crosschain/keeper/keeper_test.go
deleted file mode 100644
index 58cc10896e..0000000000
--- a/x/crosschain/keeper/keeper_test.go
+++ /dev/null
@@ -1,65 +0,0 @@
-package keeper
-
-import (
-	"testing"
-
-	"github.com/cosmos/cosmos-sdk/codec"
-	codectypes "github.com/cosmos/cosmos-sdk/codec/types"
-	"github.com/cosmos/cosmos-sdk/store"
-	storetypes "github.com/cosmos/cosmos-sdk/store/types"
-	sdk "github.com/cosmos/cosmos-sdk/types"
-	authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper"
-	bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
-	typesparams "github.com/cosmos/cosmos-sdk/x/params/types"
-	stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper"
-	"github.com/stretchr/testify/require"
-	"github.com/tendermint/tendermint/libs/log"
-	tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
-	tmdb "github.com/tendermint/tm-db"
-	authoritykeeper "github.com/zeta-chain/zetacore/x/authority/keeper"
-	"github.com/zeta-chain/zetacore/x/crosschain/types"
-	fungiblekeeper "github.com/zeta-chain/zetacore/x/fungible/keeper"
-	"github.com/zeta-chain/zetacore/x/observer/keeper"
-)
-
-func setupKeeper(t testing.TB) (*Keeper, sdk.Context) {
-	storeKey := sdk.NewKVStoreKey(types.StoreKey)
-	memStoreKey := storetypes.NewMemoryStoreKey(types.MemStoreKey)
-
-	db := tmdb.NewMemDB()
-	stateStore := store.NewCommitMultiStore(db)
-	stateStore.MountStoreWithDB(storeKey, storetypes.StoreTypeIAVL, db)
-	stateStore.MountStoreWithDB(memStoreKey, storetypes.StoreTypeMemory, nil)
-	require.NoError(t, stateStore.LoadLatestVersion())
-
-	registry := codectypes.NewInterfaceRegistry()
-	cdc := codec.NewProtoCodec(registry)
-
-	paramsSubspace := typesparams.NewSubspace(cdc,
-		types.Amino,
-		storeKey,
-		memStoreKey,
-		"ZetacoreParams",
-	)
-	bankKeeper := bankkeeper.BaseKeeper{}
-	authKeeper := authkeeper.AccountKeeper{}
-	observerKeeper := keeper.Keeper{}
-	fungibleKeeper := fungiblekeeper.Keeper{}
-	authorityKeeper := authoritykeeper.Keeper{}
-
-	k := NewKeeper(
-		codec.NewProtoCodec(registry),
-		storeKey,
-		memStoreKey,
-		stakingkeeper.Keeper{}, // custom
-		paramsSubspace,
-		authKeeper,
-		bankKeeper,
-		observerKeeper,
-		&fungibleKeeper,
-		authorityKeeper,
-	)
-
-	ctx := sdk.NewContext(stateStore, tmproto.Header{}, false, log.NewNopLogger())
-	return k, ctx
-}
diff --git a/x/crosschain/keeper/last_block_height_test.go b/x/crosschain/keeper/last_block_height_test.go
index 8a284f6f97..707b89ff02 100644
--- a/x/crosschain/keeper/last_block_height_test.go
+++ b/x/crosschain/keeper/last_block_height_test.go
@@ -1,4 +1,4 @@
-package keeper
+package keeper_test
 
 import (
 	"fmt"
@@ -6,10 +6,12 @@ import (
 
 	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/x/crosschain/keeper"
 	"github.com/zeta-chain/zetacore/x/crosschain/types"
 )
 
-func createNLastBlockHeight(keeper *Keeper, ctx sdk.Context, n int) []types.LastBlockHeight {
+func createNLastBlockHeight(keeper *keeper.Keeper, ctx sdk.Context, n int) []types.LastBlockHeight {
 	items := make([]types.LastBlockHeight, n)
 	for i := range items {
 		items[i].Creator = "any"
@@ -20,26 +22,26 @@ func createNLastBlockHeight(keeper *Keeper, ctx sdk.Context, n int) []types.Last
 }
 
 func TestLastBlockHeightGet(t *testing.T) {
-	keeper, ctx := setupKeeper(t)
-	items := createNLastBlockHeight(keeper, ctx, 10)
+	k, ctx, _, _ := keepertest.CrosschainKeeper(t)
+	items := createNLastBlockHeight(k, ctx, 10)
 	for _, item := range items {
-		rst, found := keeper.GetLastBlockHeight(ctx, item.Index)
+		rst, found := k.GetLastBlockHeight(ctx, item.Index)
 		require.True(t, found)
 		require.Equal(t, item, rst)
 	}
 }
 func TestLastBlockHeightRemove(t *testing.T) {
-	keeper, ctx := setupKeeper(t)
-	items := createNLastBlockHeight(keeper, ctx, 10)
+	k, ctx, _, _ := keepertest.CrosschainKeeper(t)
+	items := createNLastBlockHeight(k, ctx, 10)
 	for _, item := range items {
-		keeper.RemoveLastBlockHeight(ctx, item.Index)
-		_, found := keeper.GetLastBlockHeight(ctx, item.Index)
+		k.RemoveLastBlockHeight(ctx, item.Index)
+		_, found := k.GetLastBlockHeight(ctx, item.Index)
 		require.False(t, found)
 	}
 }
 
 func TestLastBlockHeightGetAll(t *testing.T) {
-	keeper, ctx := setupKeeper(t)
-	items := createNLastBlockHeight(keeper, ctx, 10)
-	require.Equal(t, items, keeper.GetAllLastBlockHeight(ctx))
+	k, ctx, _, _ := keepertest.CrosschainKeeper(t)
+	items := createNLastBlockHeight(k, ctx, 10)
+	require.Equal(t, items, k.GetAllLastBlockHeight(ctx))
 }