Skip to content

Commit

Permalink
rebase
Browse files Browse the repository at this point in the history
  • Loading branch information
codchen committed Jun 19, 2024
1 parent 29a5a1c commit fed14b5
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 5 deletions.
2 changes: 1 addition & 1 deletion x/evm/keeper/evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func (k *Keeper) CallEVM(ctx sdk.Context, from common.Address, to *common.Addres
}
k.AppendToEvmTxDeferredInfo(ctx, ethtypes.Bloom{}, ethtypes.EmptyTxsHash, surplus)
ctx.EVMEventManager().EmitEvents(stateDB.GetAllLogs())
return ctx, res.ReturnData, nil
return stateDB.Ctx(), res.ReturnData, nil
}

func (k *Keeper) StaticCallEVM(ctx sdk.Context, from sdk.AccAddress, to *common.Address, data []byte) ([]byte, error) {
Expand Down
8 changes: 5 additions & 3 deletions x/evm/keeper/evm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,15 @@ func TestInternalCall(t *testing.T) {
Data: contractData,
}
ctx = ctx.WithIsEVM(false)
_, ret, err := k.HandleInternalEVMCall(ctx, req)
resCtx, ret, err := k.HandleInternalEVMCall(ctx, req)
require.Nil(t, err)
contractAddr := crypto.CreateAddress(senderEvmAddr, 0)
require.NotEmpty(t, k.GetCode(ctx, contractAddr))
require.Equal(t, ret.Data, k.GetCode(ctx, contractAddr))
k.SetERC20NativePointer(ctx, "test", contractAddr)

ctx = resCtx
require.NotNil(t, types.GetCtxEVM(ctx))
receiverAddr, evmAddr := testkeeper.MockAddressPair()
k.SetAddressMapping(ctx, receiverAddr, evmAddr)
args, err = abi.Pack("transfer", evmAddr, big.NewInt(1000))
Expand All @@ -80,9 +82,9 @@ func TestInternalCall(t *testing.T) {
Data: args,
Value: &val,
}
_, _, err = k.HandleInternalEVMCall(ctx, req)
resCtx, _, err = k.HandleInternalEVMCall(ctx, req)
require.Nil(t, err)
require.Equal(t, int64(1000), testkeeper.EVMTestApp.BankKeeper.GetBalance(ctx, receiverAddr, "test").Amount.Int64())
require.Equal(t, int64(1000), testkeeper.EVMTestApp.BankKeeper.GetBalance(resCtx, receiverAddr, "test").Amount.Int64())
}

func TestStaticCall(t *testing.T) {
Expand Down
14 changes: 14 additions & 0 deletions x/evm/state/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/tracing"
"github.com/ethereum/go-ethereum/core/vm"
testkeeper "github.com/sei-protocol/sei-chain/testutil/keeper"
"github.com/sei-protocol/sei-chain/x/evm/state"
"github.com/sei-protocol/sei-chain/x/evm/types"
Expand Down Expand Up @@ -166,3 +167,16 @@ func TestSnapshot(t *testing.T) {
require.Equal(t, common.Hash{}, newStateDB.GetTransientState(evmAddr, tkey))
require.Equal(t, val, newStateDB.GetState(evmAddr, key))
}

func TestSetEVM(t *testing.T) {
k, ctx := testkeeper.MockEVMKeeper()
statedb := state.NewDBImpl(ctx, k, false)
rev1 := statedb.Snapshot()
statedb.SetEVM(&vm.EVM{})
rev2 := statedb.Snapshot()
require.NotNil(t, types.GetCtxEVM(statedb.Ctx()))
statedb.RevertToSnapshot(rev2)
require.NotNil(t, types.GetCtxEVM(statedb.Ctx()))
statedb.RevertToSnapshot(rev1)
require.NotNil(t, types.GetCtxEVM(statedb.Ctx()))
}
3 changes: 2 additions & 1 deletion x/evm/state/statedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ func (s *DBImpl) SetLogger(logger *tracing.Hooks) {
s.logger = logger
}

// for interface compliance
func (s *DBImpl) SetEVM(evm *vm.EVM) {
s.ctx = types.SetCtxEVM(s.ctx, evm)
s.snapshottedCtxs = utils.Map(s.snapshottedCtxs, func(ctx sdk.Context) sdk.Context { return types.SetCtxEVM(ctx, evm) })
Expand Down Expand Up @@ -109,6 +108,8 @@ func (s *DBImpl) Finalize() (surplus sdk.Int, err error) {
for i := len(s.snapshottedCtxs) - 1; i > 0; i-- {
s.flushCtx(s.snapshottedCtxs[i])
}
s.ctx = s.snapshottedCtxs[0]
s.snapshottedCtxs = []sdk.Context{}

surplus = s.tempStateCurrent.surplus
for _, ts := range s.tempStatesHist {
Expand Down
20 changes: 20 additions & 0 deletions x/evm/types/context_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package types_test

import (
"context"
"testing"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/ethereum/go-ethereum/core/vm"
"github.com/sei-protocol/sei-chain/x/evm/types"
"github.com/stretchr/testify/require"
)

func TestCtxEvm(t *testing.T) {
ctx := sdk.Context{}.WithContext(context.Background())
require.Nil(t, types.GetCtxEVM(ctx))
ctx = types.SetCtxEVM(ctx, &vm.EVM{})
require.NotNil(t, types.GetCtxEVM(ctx))
ctx = ctx.WithContext(context.WithValue(ctx.Context(), types.CtxEVMKey, 123))
require.Nil(t, types.GetCtxEVM(ctx))
}

0 comments on commit fed14b5

Please sign in to comment.