Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial implementation of IBC Precompile (branch forked from yevhenii/precompile-v2) #36

Merged
merged 1 commit into from
Feb 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
4 changes: 3 additions & 1 deletion x/evm/keeper/state_transition.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@
tracer vm.EVMLogger,
stateDB vm.StateDB,
) evm.EVM {
goCtx := sdk.WrapSDKContext(ctx)

Check failure on line 55 in x/evm/keeper/state_transition.go

View workflow job for this annotation

GitHub Actions / Run golangci-lint

goCtx declared and not used (typecheck)

blockCtx := vm.BlockContext{

Check failure on line 57 in x/evm/keeper/state_transition.go

View workflow job for this annotation

GitHub Actions / Run golangci-lint

blockCtx declared and not used (typecheck)
CanTransfer: core.CanTransfer,
Transfer: core.Transfer,
GetHash: k.GetHashFn(ctx),
Expand All @@ -65,12 +67,12 @@
Random: nil, // not supported
}

txCtx := core.NewEVMTxContext(msg)

Check failure on line 70 in x/evm/keeper/state_transition.go

View workflow job for this annotation

GitHub Actions / Run golangci-lint

txCtx declared and not used (typecheck)
if tracer == nil {
tracer = k.Tracer(ctx, msg, cfg.ChainConfig)
}
vmConfig := k.VMConfig(ctx, msg, cfg, tracer)

Check failure on line 74 in x/evm/keeper/state_transition.go

View workflow job for this annotation

GitHub Actions / Run golangci-lint

vmConfig declared and not used (typecheck)
return k.evmConstructor(blockCtx, txCtx, stateDB, cfg.ChainConfig, vmConfig, k.customPrecompiles)
return k.evmConstructor(goCtx, blockCtx, txCtx, stateDB, cfg.ChainConfig, vmConfig, k.customPrecompiles)
}

// GetHashFn implements vm.GetHashFunc for Ethermint. It handles 3 cases:
Expand Down
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
}
51 changes: 51 additions & 0 deletions x/evm/statedb/statedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,21 @@
package statedb

import (
"context"
"fmt"
"math/big"
"sort"

errorsmod "cosmossdk.io/errors"
"github.com/cosmos/cosmos-sdk/types"
sdk "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"
"github.com/ethereum/go-ethereum/common"
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 @@ -85,6 +90,10 @@ func (s *StateDB) Keeper() Keeper {
return s.keeper
}

func (s *StateDB) Context() context.Context {
return s.ctx
}

// AddLog adds a log, called by evm.
func (s *StateDB) AddLog(log *ethtypes.Log) {
s.journal.append(addLogChange{})
Expand Down Expand Up @@ -476,3 +485,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,
}
}
4 changes: 3 additions & 1 deletion x/evm/vm/geth/geth.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package geth

import (
"context"
"math/big"

"github.com/ethereum/go-ethereum/common"
Expand All @@ -40,6 +41,7 @@ type EVM struct {
// the default precompiled contracts and the EVM concrete implementation from
// geth.
func NewEVM(
goCtx context.Context,
blockCtx vm.BlockContext,
txCtx vm.TxContext,
stateDB vm.StateDB,
Expand All @@ -48,7 +50,7 @@ func NewEVM(
_ evm.PrecompiledContracts, // unused
) evm.EVM {
return &EVM{
EVM: vm.NewEVM(blockCtx, txCtx, stateDB, chainConfig, config),
EVM: vm.NewEVMWithContext(goCtx, blockCtx, txCtx, stateDB, chainConfig, config),
}
}

Expand Down
2 changes: 2 additions & 0 deletions x/evm/vm/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package vm

import (
"context"
"math/big"

"github.com/ethereum/go-ethereum/common"
Expand Down Expand Up @@ -75,6 +76,7 @@ type EVM interface {
// Constructor defines the function used to instantiate the EVM on
// each state transition.
type Constructor func(
goCtx context.Context,
blockCtx vm.BlockContext,
txCtx vm.TxContext,
stateDB vm.StateDB,
Expand Down
Loading