Skip to content

Commit

Permalink
test(evmengine): add test cases for genesis (#134)
Browse files Browse the repository at this point in the history
increase coverage to 83.3%
moved common function to keeper_internal_test.go
  • Loading branch information
zsystm authored Sep 24, 2024
1 parent 25583ff commit dd832fe
Show file tree
Hide file tree
Showing 3 changed files with 157 additions and 28 deletions.
28 changes: 0 additions & 28 deletions client/x/evmengine/keeper/db_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,11 @@ package keeper
import (
"testing"

sdk "github.com/cosmos/cosmos-sdk/types"
authtx "github.com/cosmos/cosmos-sdk/x/auth/tx"
"github.com/ethereum/go-ethereum/beacon/engine"
"github.com/ethereum/go-ethereum/common"
"github.com/stretchr/testify/require"

moduletestutil "github.com/piplabs/story/client/x/evmengine/testutil"
"github.com/piplabs/story/lib/ethclient/mock"

"go.uber.org/mock/gomock"
)

func createTestKeeper(t *testing.T) (sdk.Context, *Keeper) {
t.Helper()
cdc := getCodec(t)
txConfig := authtx.NewTxConfig(cdc, nil)

mockEngine, err := newMockEngineAPI(0)
require.NoError(t, err)

ctrl := gomock.NewController(t)
mockClient := mock.NewMockClient(ctrl)
ak := moduletestutil.NewMockAccountKeeper(ctrl)
esk := moduletestutil.NewMockEvmStakingKeeper(ctrl)
uk := moduletestutil.NewMockUpgradeKeeper(ctrl)
ctx, storeService := setupCtxStore(t, nil)
ctx = ctx.WithExecMode(sdk.ExecModeFinalize)
keeper, err := NewKeeper(cdc, storeService, &mockEngine, mockClient, txConfig, ak, esk, uk)
require.NoError(t, err)

return ctx, keeper
}

func TestKeeper_InsertGenesisHead(t *testing.T) {
t.Parallel()

Expand Down
131 changes: 131 additions & 0 deletions client/x/evmengine/keeper/genesis_internal_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
package keeper

import (
"context"
"testing"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/ethereum/go-ethereum/common"
"github.com/stretchr/testify/require"

"github.com/piplabs/story/client/x/evmengine/types"
)

func TestKeeper_InitGenesis(t *testing.T) {
t.Parallel()
dummyExecutionHead := common.HexToHash("0x047e24c3455107d87c68dffa307b3b7fa1877f3e9d7f30c7ee359f2eff3a75d9")
validParams := types.NewParams(dummyExecutionHead.Bytes())

tcs := []struct {
name string
gs func() *types.GenesisState
setup func(c context.Context, k *Keeper)
postStateCheck func(c context.Context, k *Keeper)
expectedError string
requirePanic bool
}{
{
name: "pass",
gs: func() *types.GenesisState {
return &types.GenesisState{
Params: validParams,
}
},
postStateCheck: func(c context.Context, k *Keeper) {
params, err := k.GetParams(c)
require.NoError(t, err)
require.Equal(t, validParams, params)
},
},
{
name: "fail: invalid execution block hash",
gs: func() *types.GenesisState {
invalidParams := validParams
invalidParams.ExecutionBlockHash = []byte("invalid")

return &types.GenesisState{
Params: invalidParams,
}
},
expectedError: "invalid execution block hash length",
},
{
name: "panic: execution head already exists",
setup: func(c context.Context, k *Keeper) {
require.NoError(t, k.InsertGenesisHead(c, dummyExecutionHead.Bytes()))
},
gs: func() *types.GenesisState {
return &types.GenesisState{
Params: validParams,
}
},
expectedError: "insert genesis head: unexpected genesis head id",
requirePanic: true,
},
}

for _, tc := range tcs {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
ctx, keeper := createTestKeeper(t)
if tc.setup != nil {
tc.setup(ctx, keeper)
}
if tc.requirePanic {
require.PanicsWithError(t, tc.expectedError, func() {
_ = keeper.InitGenesis(ctx, tc.gs())
})
} else {
err := keeper.InitGenesis(ctx, tc.gs())
if tc.expectedError != "" {
require.Error(t, err)
require.Contains(t, err.Error(), tc.expectedError)
} else {
require.NoError(t, err)
tc.postStateCheck(ctx, keeper)
}
}
})
}
}

func TestKeeper_ExportGenesis(t *testing.T) {
t.Parallel()
dummyExecutionHead := common.HexToHash("0x047e24c3455107d87c68dffa307b3b7fa1877f3e9d7f30c7ee359f2eff3a75d9")
validParams := types.NewParams(dummyExecutionHead.Bytes())

tcs := []struct {
name string
setup func(c context.Context, k *Keeper)
postStateCheck func(c context.Context, k *Keeper)
}{
{
name: "pass",
setup: func(c context.Context, k *Keeper) {
require.NoError(t, k.SetParams(c, validParams))
},
postStateCheck: func(c context.Context, k *Keeper) {
gs := k.ExportGenesis(sdk.UnwrapSDKContext(c))
require.Equal(t, validParams, gs.Params)
},
},
{
name: "pass: default params",
postStateCheck: func(c context.Context, k *Keeper) {
gs := k.ExportGenesis(sdk.UnwrapSDKContext(c))
require.Equal(t, types.DefaultParams(), gs.Params)
},
},
}

for _, tc := range tcs {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
ctx, keeper := createTestKeeper(t)
if tc.setup != nil {
tc.setup(ctx, keeper)
}
tc.postStateCheck(ctx, keeper)
})
}
}
26 changes: 26 additions & 0 deletions client/x/evmengine/keeper/keeper_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,32 @@ type args struct {
header func(height int64, address []byte) cmtproto.Header
}

func createTestKeeper(t *testing.T) (context.Context, *Keeper) {
t.Helper()

cdc := getCodec(t)
txConfig := authtx.NewTxConfig(cdc, nil)
mockEngine, err := newMockEngineAPI(0)
require.NoError(t, err)

cmtAPI := newMockCometAPI(t, nil)
header := cmtproto.Header{Height: 1}

ctrl := gomock.NewController(t)
mockClient := mock.NewMockClient(ctrl)
ak := moduletestutil.NewMockAccountKeeper(ctrl)
esk := moduletestutil.NewMockEvmStakingKeeper(ctrl)
uk := moduletestutil.NewMockUpgradeKeeper(ctrl)

ctx, storeService := setupCtxStore(t, &header)

keeper, err := NewKeeper(cdc, storeService, &mockEngine, mockClient, txConfig, ak, esk, uk)
require.NoError(t, err)
keeper.SetCometAPI(cmtAPI)

return ctx, keeper
}

func createKeeper(t *testing.T, args args) (sdk.Context, *mockCometAPI, *Keeper) {
t.Helper()

Expand Down

0 comments on commit dd832fe

Please sign in to comment.