Skip to content

Commit

Permalink
refactor: skip 'from' account verification during contract query
Browse files Browse the repository at this point in the history
  • Loading branch information
zakir-code committed Oct 28, 2024
1 parent f8f47dc commit 67e4ec5
Show file tree
Hide file tree
Showing 10 changed files with 47 additions and 32 deletions.
6 changes: 3 additions & 3 deletions contract/contract.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package contract

import (
"context"
"math/big"
"strings"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
Expand Down Expand Up @@ -48,8 +48,8 @@ var (
)

type Caller interface {
QueryContract(ctx sdk.Context, from, contract common.Address, abi abi.ABI, method string, res interface{}, args ...interface{}) error
ApplyContract(ctx sdk.Context, from, contract common.Address, value *big.Int, abi abi.ABI, method string, constructorData ...interface{}) (*evmtypes.MsgEthereumTxResponse, error)
QueryContract(ctx context.Context, from, contract common.Address, abi abi.ABI, method string, res interface{}, args ...interface{}) error
ApplyContract(ctx context.Context, from, contract common.Address, value *big.Int, abi abi.ABI, method string, constructorData ...interface{}) (*evmtypes.MsgEthereumTxResponse, error)
}

type Contract struct {
Expand Down
2 changes: 1 addition & 1 deletion x/crosschain/keeper/bridge_call_in.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func (k Keeper) BridgeCallEvm(ctx sdk.Context, sender, refundAddr, to, receiverA
}

gasLimit := k.GetBridgeCallMaxGasLimit(ctx)
txResp, err := k.evmKeeper.CallEVM(ctx, callEvmSender, &to, value.BigInt(), gasLimit, args, true)
txResp, err := k.evmKeeper.ExecuteEVM(ctx, callEvmSender, &to, value.BigInt(), gasLimit, args)
if err != nil {
return err
}
Expand Down
12 changes: 6 additions & 6 deletions x/crosschain/mock/expected_keepers_mocks.go

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

2 changes: 1 addition & 1 deletion x/crosschain/types/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ type Erc20Keeper interface {

// EVMKeeper defines the expected EVM keeper interface used on crosschain
type EVMKeeper interface {
CallEVM(ctx sdk.Context, from common.Address, contract *common.Address, value *big.Int, gasLimit uint64, data []byte, commit bool) (*types.MsgEthereumTxResponse, error)
ExecuteEVM(ctx sdk.Context, from common.Address, contract *common.Address, value *big.Int, gasLimit uint64, data []byte) (*types.MsgEthereumTxResponse, error)
IsContract(ctx sdk.Context, account common.Address) bool
}

Expand Down
15 changes: 10 additions & 5 deletions x/evm/keeper/contract_code.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package keeper

import (
"bytes"
"context"
"encoding/hex"
"math/big"

Expand Down Expand Up @@ -79,7 +80,7 @@ func (k *Keeper) DeployContract(ctx sdk.Context, from common.Address, abi abi.AB
return common.Address{}, err
}

_, err = k.CallEVMWithoutGas(ctx, from, nil, nil, data, true)
_, err = k.callEvm(ctx, from, nil, nil, nonce, data, true)
if err != nil {
return common.Address{}, err
}
Expand Down Expand Up @@ -110,12 +111,12 @@ func (k *Keeper) DeployUpgradableContract(ctx sdk.Context, from, logic common.Ad
}

// QueryContract query contract with args and res
func (k *Keeper) QueryContract(ctx sdk.Context, from, contract common.Address, abi abi.ABI, method string, res interface{}, args ...interface{}) error {
func (k *Keeper) QueryContract(ctx context.Context, from, contract common.Address, abi abi.ABI, method string, res interface{}, args ...interface{}) error {
data, err := abi.Pack(method, args...)
if err != nil {
return types.ErrABIPack.Wrap(err.Error())
}
resp, err := k.CallEVMWithoutGas(ctx, from, &contract, nil, data, false)
resp, err := k.callEvm(sdk.UnwrapSDKContext(ctx), from, &contract, nil, 0, data, false)
if err != nil {
return err
}
Expand All @@ -126,12 +127,16 @@ func (k *Keeper) QueryContract(ctx sdk.Context, from, contract common.Address, a
}

// ApplyContract apply contract with args
func (k *Keeper) ApplyContract(ctx sdk.Context, from, contract common.Address, value *big.Int, abi abi.ABI, method string, constructorData ...interface{}) (*evmtypes.MsgEthereumTxResponse, error) {
func (k *Keeper) ApplyContract(ctx context.Context, from, contract common.Address, value *big.Int, abi abi.ABI, method string, constructorData ...interface{}) (*evmtypes.MsgEthereumTxResponse, error) {
args, err := abi.Pack(method, constructorData...)
if err != nil {
return nil, types.ErrABIPack.Wrap(err.Error())
}
resp, err := k.CallEVMWithoutGas(ctx, from, &contract, value, args, true)
nonce, err := k.accountKeeper.GetSequence(ctx, from.Bytes())
if err != nil {
return nil, err
}
resp, err := k.callEvm(sdk.UnwrapSDKContext(ctx), from, &contract, value, nonce, args, true)
if err != nil {
return nil, err
}
Expand Down
28 changes: 17 additions & 11 deletions x/evm/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func NewKeeper(ek *evmkeeper.Keeper, ak fxevmtypes.AccountKeeper) *Keeper {
}
}

// CallEVMWithoutGas performs a smart contract method call using contract data without gas
// Deprecated: please use callEvm todo: remove this
func (k *Keeper) CallEVMWithoutGas(
ctx sdk.Context,
from common.Address,
Expand All @@ -52,13 +52,24 @@ func (k *Keeper) CallEVMWithoutGas(
data []byte,
commit bool,
) (*types.MsgEthereumTxResponse, error) {
gasMeter := ctx.GasMeter()
ctx = ctx.WithGasMeter(storetypes.NewInfiniteGasMeter())

nonce, err := k.accountKeeper.GetSequence(ctx, from.Bytes())
if err != nil {
return nil, err
}
return k.callEvm(ctx, from, contract, value, nonce, data, commit)
}

func (k *Keeper) callEvm(
ctx sdk.Context,
from common.Address,
contract *common.Address,
value *big.Int,
nonce uint64,
data []byte,
commit bool,
) (*types.MsgEthereumTxResponse, error) {
gasMeter := ctx.GasMeter()
ctx = ctx.WithGasMeter(storetypes.NewInfiniteGasMeter())

gasLimit := fxcontract.DefaultGasCap
params := ctx.ConsensusParams()
Expand Down Expand Up @@ -103,14 +114,13 @@ func (k *Keeper) CallEVMWithoutGas(
return res, nil
}

func (k *Keeper) CallEVM(
func (k *Keeper) ExecuteEVM(
ctx sdk.Context,
from common.Address,
contract *common.Address,
value *big.Int,
gasLimit uint64,
data []byte,
commit bool,
) (*types.MsgEthereumTxResponse, error) {
gasMeter := ctx.GasMeter()
ctx = ctx.WithGasMeter(storetypes.NewInfiniteGasMeter())
Expand All @@ -119,10 +129,6 @@ func (k *Keeper) CallEVM(
if err != nil {
return nil, err
}
params := ctx.ConsensusParams()
if params.Block != nil && params.Block.MaxGas > 0 {
gasLimit = uint64(params.Block.MaxGas)
}

if value == nil {
value = big.NewInt(0)
Expand All @@ -141,7 +147,7 @@ func (k *Keeper) CallEVM(
SkipAccountChecks: false,
}

res, err := k.ApplyMessage(ctx, msg, types.NewNoOpTracer(), commit)
res, err := k.ApplyMessage(ctx, msg, types.NewNoOpTracer(), true)
if err != nil {
return nil, err
}
Expand Down
6 changes: 5 additions & 1 deletion x/evm/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ func (k *Keeper) CallContract(goCtx context.Context, msg *fxevmtypes.MsgCallCont
if account == nil || !account.IsContract() {
return nil, govtypes.ErrInvalidProposalMsg.Wrapf("contract %s not found", contract.Hex())
}
_, err := k.CallEVMWithoutGas(ctx, k.module, &contract, nil, common.Hex2Bytes(msg.Data), true)
nonce, err := k.accountKeeper.GetSequence(ctx, k.module.Bytes())
if err != nil {
return nil, err
}
_, err = k.callEvm(ctx, k.module, &contract, nil, nonce, common.Hex2Bytes(msg.Data), true)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion x/evm/testutil/evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func (s *EVMSuite) Call(abi abi.ABI, method string, res interface{}, args ...int
}

func (s *EVMSuite) CallEVM(data []byte, gasLimit uint64) *evmtypes.MsgEthereumTxResponse {
tx, err := s.evmKeeper.CallEVM(s.ctx, s.GetFrom(), &s.contractAddr, nil, gasLimit, data, false)
tx, err := s.evmKeeper.ExecuteEVM(s.ctx, s.GetFrom(), &s.contractAddr, nil, gasLimit, data)
s.NoError(err)
return tx
}
Expand Down
4 changes: 2 additions & 2 deletions x/ibc/middleware/keeper/ibc_call.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ func (k Keeper) HandlerIbcCallEvm(ctx sdk.Context, sender common.Address, evmPac
}
ctx.EventManager().EmitEvent(sdk.NewEvent(types.EventTypeIBCCall, attrs...))
}()
txResp, err := k.evmKeeper.CallEVM(ctx, sender,
evmPacket.GetToAddress(), evmPacket.Value.BigInt(), uint64(limit), evmPacket.MustGetData(), true)
txResp, err := k.evmKeeper.ExecuteEVM(ctx, sender,
evmPacket.GetToAddress(), evmPacket.Value.BigInt(), uint64(limit), evmPacket.MustGetData())
if err != nil {
evmErrCause = err.Error()
return err
Expand Down
2 changes: 1 addition & 1 deletion x/ibc/middleware/types/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

type EvmKeeper interface {
CallEVM(ctx sdk.Context, from common.Address, contract *common.Address, value *big.Int, gasLimit uint64, data []byte, commit bool) (*evmtypes.MsgEthereumTxResponse, error)
ExecuteEVM(ctx sdk.Context, from common.Address, contract *common.Address, value *big.Int, gasLimit uint64, data []byte) (*evmtypes.MsgEthereumTxResponse, error)
}

type CrosschainKeeper interface {
Expand Down

0 comments on commit 67e4ec5

Please sign in to comment.