Skip to content

Commit

Permalink
Fixed contract initialization bug (branch forked from yevhenii/precom…
Browse files Browse the repository at this point in the history
…pile-v2)
  • Loading branch information
evgeniy-scherbina committed Feb 15, 2024
1 parent 5d6946c commit ced8cbb
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 2 deletions.
2 changes: 1 addition & 1 deletion app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ func NewEthermintApp(
app.EvmKeeper = evmkeeper.NewKeeper(
appCodec, keys[evmtypes.StoreKey], tkeys[evmtypes.TransientKey],
authtypes.NewModuleAddress(govtypes.ModuleName),
app.AccountKeeper, app.BankKeeper, app.StakingKeeper, app.FeeMarketKeeper,
app.AccountKeeper, app.BankKeeper, app.StakingKeeper, app.FeeMarketKeeper, nil,
nil, geth.NewEVM, tracer, evmSs,
)

Expand Down
2 changes: 1 addition & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -901,6 +901,7 @@ github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6So
github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
github.com/rogpeppe/go-internal v1.8.1/go.mod h1:JeRgkft04UBgHMgCIwADu4Pn6Mtm5d4nPKWu0nJ5d+o=
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
github.com/rs/cors v1.7.0/go.mod h1:gFx+x8UowdsKA9AchylcLynDq+nNFfI8FkUZdN/jGCU=
Expand Down Expand Up @@ -978,7 +979,6 @@ github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.2/go.mod h1:R6va5+xMeoiuVRoj+gSkQ7d3FALtqAAGI1FQKckRals=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
Expand Down
8 changes: 8 additions & 0 deletions x/evm/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ type Keeper struct {
// fetch EIP1559 base fee and parameters
feeMarketKeeper types.FeeMarketKeeper

ibcTransferKeeper statedb.IBCTransferKeeper

// chain ID number obtained from the context's chain id
eip155ChainID *big.Int

Expand Down Expand Up @@ -90,6 +92,7 @@ func NewKeeper(
bankKeeper types.BankKeeper,
sk types.StakingKeeper,
fmk types.FeeMarketKeeper,
ibcTransferKeeper statedb.IBCTransferKeeper,
customPrecompiles evm.PrecompiledContracts,
evmConstructor evm.Constructor,
tracer string,
Expand Down Expand Up @@ -117,6 +120,7 @@ func NewKeeper(
bankKeeper: bankKeeper,
stakingKeeper: sk,
feeMarketKeeper: fmk,
ibcTransferKeeper: ibcTransferKeeper,
storeKey: storeKey,
transientKey: transientKey,
customPrecompiles: customPrecompiles,
Expand Down Expand Up @@ -398,3 +402,7 @@ func (k Keeper) AddTransientGasUsed(ctx sdk.Context, gasUsed uint64) (uint64, er
k.SetTransientGasUsed(ctx, result)
return result, nil
}

func (k Keeper) IBCTransferKeeper() statedb.IBCTransferKeeper {
return k.ibcTransferKeeper
}
9 changes: 9 additions & 0 deletions x/evm/statedb/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@
package statedb

import (
"context"

sdk "github.com/cosmos/cosmos-sdk/types"
ibctransfertypes "github.com/cosmos/ibc-go/v6/modules/apps/transfer/types"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/vm"
)
Expand All @@ -31,6 +34,10 @@ type ExtStateDB interface {
AppendJournalEntry(JournalEntry)
}

type IBCTransferKeeper interface {
Transfer(goCtx context.Context, msg *ibctransfertypes.MsgTransfer) (*ibctransfertypes.MsgTransferResponse, error)
}

// Keeper provide underlying storage of StateDB
type Keeper interface {
// Read methods
Expand All @@ -45,4 +52,6 @@ type Keeper interface {
SetState(ctx sdk.Context, addr common.Address, key common.Hash, value []byte)
SetCode(ctx sdk.Context, codeHash []byte, code []byte)
DeleteAccount(ctx sdk.Context, addr common.Address) error

IBCTransferKeeper() IBCTransferKeeper
}
48 changes: 48 additions & 0 deletions x/evm/statedb/statedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@
package statedb

import (
"context"
"fmt"
"github.com/cosmos/cosmos-sdk/types"
ibctransfertypes "github.com/cosmos/ibc-go/v6/modules/apps/transfer/types"
ibcclienttypes "github.com/cosmos/ibc-go/v6/modules/core/02-client/types"
"math/big"
"sort"

Expand All @@ -26,6 +30,8 @@ import (
ethtypes "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/core/vm"
"github.com/ethereum/go-ethereum/crypto"

"github.com/ethereum/go-ethereum/precompile/contract"
)

// revision is the identifier of a version of state.
Expand Down Expand Up @@ -476,3 +482,45 @@ func (s *StateDB) Commit() error {
}
return nil
}

func (s *StateDB) IBCTransfer(goCtx context.Context, msg *contract.MsgTransfer) (*contract.MsgTransferResponse, error) {
resp, err := s.keeper.IBCTransferKeeper().Transfer(goCtx, newMsgTransfer(msg))
if err != nil {
return nil, err
}

return newMsgTransferResponse(resp), nil
}

func newMsgTransfer(m *contract.MsgTransfer) *ibctransfertypes.MsgTransfer {
return &ibctransfertypes.MsgTransfer{
SourcePort: m.SourcePort,
SourceChannel: m.SourceChannel,
Token: newCoin(m.Token),
Sender: m.Sender,
Receiver: m.Receiver,
TimeoutHeight: newHeight(m.TimeoutHeight),
TimeoutTimestamp: m.TimeoutTimestamp,
Memo: m.Memo,
}
}

func newCoin(c contract.Coin) types.Coin {
return types.Coin{
Denom: c.Denom,
Amount: c.Amount,
}
}

func newHeight(h contract.Height) ibcclienttypes.Height {
return ibcclienttypes.Height{
RevisionNumber: h.RevisionNumber,
RevisionHeight: h.RevisionHeight,
}
}

func newMsgTransferResponse(r *ibctransfertypes.MsgTransferResponse) *contract.MsgTransferResponse {
return &contract.MsgTransferResponse{
Sequence: r.Sequence,
}
}

0 comments on commit ced8cbb

Please sign in to comment.