Skip to content
This repository has been archived by the owner on Apr 4, 2024. It is now read-only.

Commit

Permalink
avoid fork
Browse files Browse the repository at this point in the history
  • Loading branch information
mmsqe committed Feb 1, 2023
1 parent a98437d commit bb913f1
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 62 deletions.
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,6 @@ replace (
// use cosmos keyring
github.com/99designs/keyring => github.com/cosmos/keyring v1.1.7-0.20210622111912-ef00f8ac3d76
github.com/cosmos/cosmos-sdk => github.com/mmsqe/cosmos-sdk v0.46.6-0.20230110154545-70f48768ca49
github.com/ethereum/go-ethereum => github.com/mmsqe/go-ethereum v1.10.26-0.20230131012941-1ed879b1a4a9
// Fix upstream GHSA-h395-qcrw-5vmq vulnerability.
// TODO Remove it: https://github.com/cosmos/cosmos-sdk/issues/10409
github.com/gin-gonic/gin => github.com/gin-gonic/gin v1.7.0
Expand Down
65 changes: 42 additions & 23 deletions go.sum

Large diffs are not rendered by default.

5 changes: 2 additions & 3 deletions gomod2nix.toml
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,8 @@ schema = 3
version = "v1.0.0"
hash = "sha256-k1DYvCqO3BKNcGEve/nMW0RxzMkK2tGfXbUbycqcVSo="
[mod."github.com/ethereum/go-ethereum"]
version = "v1.10.26-0.20230131012941-1ed879b1a4a9"
hash = "sha256-5m+CG3syk9no51Okj0tSHJKCkvKGYDtpunF+SDqxhnc="
replaced = "github.com/mmsqe/go-ethereum"
version = "v1.10.26"
hash = "sha256-gkMEwJ4rOgn12amD4QpZ4th/10uyTTeoFmpseuKDQPs="
[mod."github.com/felixge/httpsnoop"]
version = "v1.0.1"
hash = "sha256-TNXnnC/ZGNY9lInAcES1cBGqIdEljKuh5LH/khVFjVk="
Expand Down
30 changes: 19 additions & 11 deletions x/evm/keeper/precompiles/bank.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
sdkmath "cosmossdk.io/math"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/vm"

sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
Expand Down Expand Up @@ -59,14 +58,28 @@ func EVMDenom(token common.Address) string {
type BankContract struct {
ctx sdk.Context
bankKeeper types.BankKeeper
stateDB evm.ExtStateDB
caller common.Address
value *big.Int
readonly bool
}

// NewBankContractCreator creates the precompiled contract to manage native tokens
func NewBankContractCreator(bankKeeper types.BankKeeper) evm.PrecompiledContractCreator {
return func(ctx sdk.Context) evm.StatefulPrecompiledContract {
return func(
ctx sdk.Context,
stateDB evm.ExtStateDB,
caller common.Address,
value *big.Int,
readonly bool,
) evm.StatefulPrecompiledContract {
return &BankContract{
ctx: ctx,
bankKeeper: bankKeeper,
stateDB: stateDB,
caller: caller,
value: value,
readonly: readonly,
}
}
}
Expand All @@ -77,17 +90,12 @@ func (bc *BankContract) RequiredGas(input []byte) uint64 {
return 0
}

func (bc *BankContract) Run(evm *vm.EVM, input []byte, caller common.Address, value *big.Int, readonly bool) ([]byte, error) {
stateDB, ok := evm.StateDB.(ExtStateDB)
if !ok {
return nil, errors.New("not run in ethermint")
}

func (bc *BankContract) Run(input []byte) ([]byte, error) {
// parse input
methodID := input[:4]
switch string(methodID) {
case string(MintMethod.ID):
if readonly {
if bc.readonly {
return nil, errors.New("the method is not readonly")
}
args, err := MintMethod.Inputs.Unpack(input[4:])
Expand All @@ -99,8 +107,8 @@ func (bc *BankContract) Run(evm *vm.EVM, input []byte, caller common.Address, va
if amount.Sign() <= 0 {
return nil, errors.New("invalid amount")
}
denom := EVMDenom(caller)
err = stateDB.ExecuteNativeAction(func(ctx sdk.Context) error {
denom := EVMDenom(bc.caller)
err = bc.stateDB.ExecuteNativeAction(func(ctx sdk.Context) error {
addr := sdk.AccAddress(recipient.Bytes())
amt := sdk.NewCoins(sdk.NewCoin(denom, sdkmath.NewIntFromBigInt(amount)))
if err := bc.bankKeeper.MintCoins(ctx, types.ModuleName, amt); err != nil {
Expand Down
8 changes: 0 additions & 8 deletions x/evm/keeper/precompiles/types.go

This file was deleted.

15 changes: 8 additions & 7 deletions x/evm/keeper/state_transition.go
Original file line number Diff line number Diff line change
Expand Up @@ -359,17 +359,20 @@ func (k *Keeper) ApplyMessageWithConfig(ctx sdk.Context,
return nil, errorsmod.Wrap(types.ErrCallDisabled, "failed to call contract")
}

contractCreation := msg.To() == nil
sender := vm.AccountRef(msg.From())
caller := sender.Address()
if !contractCreation {
caller = *msg.To()
}
// construct precompiles
contracts := make(map[common.Address]evm.StatefulPrecompiledContract, len(k.customPrecompiles))
stateDB := k.StateDB(ctx, txConfig)
for addr, creator := range k.customPrecompiles {
c := creator(ctx)
contracts[addr] = c
contracts[addr] = creator(ctx, stateDB, caller, msg.Value(), false)
}
stateDB := k.StateDB(ctx, txConfig)
evm := k.NewEVM(ctx, msg, cfg, tracer, stateDB, contracts)

leftoverGas := msg.Gas()

// Allow the tracer captures the tx level events, mainly the gas consumption.
vmCfg := evm.Config
if vmCfg.Debug {
Expand All @@ -379,8 +382,6 @@ func (k *Keeper) ApplyMessageWithConfig(ctx sdk.Context,
}()
}

sender := vm.AccountRef(msg.From())
contractCreation := msg.To() == nil
isLondon := cfg.ChainConfig.IsLondon(evm.Context.BlockNumber)

intrinsicGas, err := k.GetEthIntrinsicGas(ctx, msg, cfg.ChainConfig, contractCreation)
Expand Down
6 changes: 1 addition & 5 deletions x/evm/vm/geth/geth.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
package geth

import (
"math/big"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/vm"
"github.com/ethereum/go-ethereum/params"
Expand Down Expand Up @@ -85,10 +83,8 @@ func (EVM) ActivePrecompiles(rules params.Rules) []common.Address {
// value arguments. It uses the RunPrecompiledContract function from the geth vm package
func (e EVM) RunPrecompiledContract(
p evm.StatefulPrecompiledContract,
_ common.Address, // address arg is unused
input []byte,
suppliedGas uint64,
caller common.Address, value *big.Int, readonly bool,
) (ret []byte, remainingGas uint64, err error) {
return vm.RunPrecompiledContract(p, e.EVM, input, suppliedGas, caller, value, readonly)
return vm.RunPrecompiledContract(p, input, suppliedGas)
}
17 changes: 13 additions & 4 deletions x/evm/vm/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,24 @@ import (
// PrecompiledContracts defines a map of address -> precompiled contract
type PrecompiledContracts map[common.Address]vm.PrecompiledContract

type PrecompiledContractCreator func(sdk.Context) StatefulPrecompiledContract
type PrecompiledContractCreator func(
sdk.Context,
ExtStateDB,
common.Address,
*big.Int,
bool,
) StatefulPrecompiledContract

type StatefulPrecompiledContract interface {
vm.PrecompiledContract
// RunStateful(evm EVM, addr common.Address, input []byte, value *big.Int) (ret []byte, err error)
}

// ExtStateDB defines extra methods of statedb to support stateful precompiled contracts
type ExtStateDB interface {
ExecuteNativeAction(action func(ctx sdk.Context) error) error
}

// EVM defines the interface for the Ethereum Virtual Machine used by the EVM module.
type EVM interface {
Config() vm.Config
Expand Down Expand Up @@ -64,10 +75,8 @@ type EVM interface {
Precompile(addr common.Address) (vm.PrecompiledContract, bool)
RunPrecompiledContract(
p StatefulPrecompiledContract,
addr common.Address,
input []byte,
suppliedGas uint64,
caller common.Address, value *big.Int, readonly bool) (
suppliedGas uint64) (
ret []byte, remainingGas uint64, err error,
)
}
Expand Down

0 comments on commit bb913f1

Please sign in to comment.