Skip to content

Commit

Permalink
Temporary Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
evgeniy-scherbina committed Mar 20, 2024
1 parent bda4959 commit da37a62
Show file tree
Hide file tree
Showing 5 changed files with 191 additions and 39 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ require (
github.com/evmos/ethermint v0.21.0
github.com/go-kit/kit v0.12.0
github.com/gogo/protobuf v1.3.3
github.com/golang/mock v1.6.0
github.com/golang/protobuf v1.5.3
github.com/gorilla/mux v1.8.0
github.com/grpc-ecosystem/grpc-gateway v1.16.0
Expand Down
3 changes: 3 additions & 0 deletions x/evmutil/migrations/v3/generate.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
mockgen -source=precompile.go \
-package v3_test \
-destination=precompile_mock_test.go
7 changes: 3 additions & 4 deletions x/evmutil/migrations/v3/precompile.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,13 @@ package v3
import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/ethereum/go-ethereum/common"
evmkeeper "github.com/evmos/ethermint/x/evm/keeper"
"github.com/evmos/ethermint/x/evm/statedb"
"math/big"
)

var ContractAddress = common.HexToAddress("0x0300000000000000000000000000000000000002")

// Keeper provide underlying storage of StateDB
type Keeper interface {
type EvmKeeper interface {
// Read methods
GetAccount(ctx sdk.Context, addr common.Address) *statedb.Account
GetState(ctx sdk.Context, addr common.Address, key common.Hash) common.Hash
Expand All @@ -29,7 +27,7 @@ type Keeper interface {

func Migrate(
ctx sdk.Context,
evmKeeper *evmkeeper.Keeper,
evmKeeper statedb.Keeper,
) error {
txConfig := statedb.TxConfig{
BlockHash: common.Hash{},
Expand All @@ -38,6 +36,7 @@ func Migrate(
LogIndex: 0,
}
stateDB := statedb.New(ctx, evmKeeper, txConfig)
_ = stateDB

// Set the nonce of the precompile's address (as is done when a contract is created) to ensure
// that it is marked as non-empty and will not be cleaned up when the statedb is finalized.
Expand Down
158 changes: 158 additions & 0 deletions x/evmutil/migrations/v3/precompile_mock_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

61 changes: 26 additions & 35 deletions x/evmutil/migrations/v3/precompile_test.go
Original file line number Diff line number Diff line change
@@ -1,47 +1,38 @@
package v3_test

import (
"testing"

"github.com/cosmos/cosmos-sdk/testutil"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/kava-labs/kava/app"
"github.com/ethereum/go-ethereum/common"
"github.com/evmos/ethermint/x/evm/statedb"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/require"

v3 "github.com/kava-labs/kava/x/evmutil/migrations/v3"
"testing"
)

func getAccountCallback(ctx sdk.Context, addr common.Address) {}

func mkSetAccountCallback(t *testing.T) func(ctx sdk.Context, addr common.Address, account statedb.Account) error {
return func(ctx sdk.Context, addr common.Address, account statedb.Account) error {
require.True(t, account.Nonce >= 0 && account.Nonce <= 1)
return nil
}
}

func TestMigratePrecompiles(t *testing.T) {
tApp := app.NewTestApp()
//cdc := tApp.AppCodec()
storeKey := sdk.NewKVStoreKey("evmutil")
ctx := testutil.DefaultContext(storeKey, sdk.NewTransientStoreKey("transient_test"))
//store := ctx.KVStore(storeKey)

//ctx sdk.Context,
//evmKeeper *evmkeeper.Keeper,
v3.Migrate(ctx)

//require.Nil(
// t,
// store.Get(types.ParamsKey),
// "params shouldn't exist in store before migration",
//)
//
//require.NoError(t, v2.Migrate(ctx, store, cdc))
//
//paramsBytes := store.Get(types.ParamsKey)
//require.NotNil(t, paramsBytes, "params should be in store after migration")
//
//var params types.Params
//cdc.MustUnmarshal(paramsBytes, &params)
//
//t.Logf("params: %+v", params)
//
//require.Equal(
// t,
// types.NewParams(
// time.Date(2023, 11, 1, 0, 0, 0, 0, time.UTC),
// sdk.NewInt(744191),
// ),
// params,
// "params should be correct after migration",
//)

ctrl := gomock.NewController(t)
evmKeeperMock := NewMockEvmKeeper(ctrl)
evmKeeperMock.EXPECT().GetAccount(gomock.Any(), v3.ContractAddress).Do(getAccountCallback).Times(2)
evmKeeperMock.EXPECT().SetAccount(gomock.Any(), v3.ContractAddress, gomock.Any()).DoAndReturn(mkSetAccountCallback(t)).Times(2)

evmKeeperMock.EXPECT().SetCode(gomock.Any(), gomock.Any(), gomock.Any()).Times(1)

err := v3.Migrate(ctx, evmKeeperMock)
require.NoError(t, err)
}

0 comments on commit da37a62

Please sign in to comment.