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

Commit

Permalink
pass chain-id to state transition
Browse files Browse the repository at this point in the history
  • Loading branch information
yihuang committed Oct 28, 2022
1 parent 879178b commit b9e748f
Show file tree
Hide file tree
Showing 8 changed files with 232 additions and 181 deletions.
6 changes: 3 additions & 3 deletions docs/api/proto-docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -563,7 +563,7 @@ EthCallRequest defines EthCall request
| `args` | [bytes](#bytes) | | same json format as the json rpc api. |
| `gas_cap` | [uint64](#uint64) | | the default gas cap to be used |
| `proposer_address` | [bytes](#bytes) | | the proposer of the requested block |
| `chain_id` | [string](#string) | | the full chain id in the requested block header |
| `chain_id` | [string](#string) | | the eip155 chain id parsed from the requested block header |



Expand Down Expand Up @@ -794,7 +794,7 @@ QueryTraceBlockRequest defines TraceTx request
| `block_hash` | [string](#string) | | block hex hash |
| `block_time` | [google.protobuf.Timestamp](#google.protobuf.Timestamp) | | block time |
| `proposer_address` | [bytes](#bytes) | | the proposer of the requested block |
| `chain_id` | [string](#string) | | the full chain id in the requested block header |
| `chain_id` | [string](#string) | | the eip155 chain id parsed from the requested block header |



Expand Down Expand Up @@ -831,7 +831,7 @@ QueryTraceTxRequest defines TraceTx request
| `block_hash` | [string](#string) | | block hex hash of requested transaction |
| `block_time` | [google.protobuf.Timestamp](#google.protobuf.Timestamp) | | block time of requested transaction |
| `proposer_address` | [bytes](#bytes) | | the proposer of the requested block |
| `chain_id` | [string](#string) | | the full chain id in the requested block header |
| `chain_id` | [string](#string) | | the eip155 chain id parsed from the requested block header |



Expand Down
12 changes: 6 additions & 6 deletions proto/ethermint/evm/v1/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -223,8 +223,8 @@ message EthCallRequest {
uint64 gas_cap = 2;
// the proposer of the requested block
bytes proposer_address = 3 [(gogoproto.casttype) = "github.com/cosmos/cosmos-sdk/types.ConsAddress"];
// the full chain id in the requested block header
string chain_id = 4;
// the eip155 chain id parsed from the requested block header
string chain_id = 4 [(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int"];
}

// EstimateGasResponse defines EstimateGas response
Expand Down Expand Up @@ -253,8 +253,8 @@ message QueryTraceTxRequest {
google.protobuf.Timestamp block_time = 7 [(gogoproto.nullable) = false, (gogoproto.stdtime) = true];
// the proposer of the requested block
bytes proposer_address = 8 [(gogoproto.casttype) = "github.com/cosmos/cosmos-sdk/types.ConsAddress"];
// the full chain id in the requested block header
string chain_id = 9;
// the eip155 chain id parsed from the requested block header
string chain_id = 9 [(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int"];
}

// QueryTraceTxResponse defines TraceTx response
Expand All @@ -277,8 +277,8 @@ message QueryTraceBlockRequest {
google.protobuf.Timestamp block_time = 7 [(gogoproto.nullable) = false, (gogoproto.stdtime) = true];
// the proposer of the requested block
bytes proposer_address = 8 [(gogoproto.casttype) = "github.com/cosmos/cosmos-sdk/types.ConsAddress"];
// the full chain id in the requested block header
string chain_id = 9;
// the eip155 chain id parsed from the requested block header
string chain_id = 9 [(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int"];
}

// QueryTraceBlockResponse defines TraceBlock response
Expand Down
19 changes: 17 additions & 2 deletions rpc/backend/call_tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"fmt"
"math/big"

sdkmath "cosmossdk.io/math"

"github.com/cosmos/cosmos-sdk/client/flags"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
Expand Down Expand Up @@ -305,11 +307,17 @@ func (b *Backend) EstimateGas(args evmtypes.TransactionArgs, blockNrOptional *rp
return 0, errors.New("header not found")
}

tmp, err := ethermint.ParseChainID(header.Block.ChainID)
if err != nil {
return 0, sdkerrors.Wrapf(err, "failed to parse chainID: %s", header.Block.ChainID)
}
chainID := sdkmath.NewIntFromBigInt(tmp)

req := evmtypes.EthCallRequest{
Args: bz,
GasCap: b.RPCGasCap(),
ProposerAddress: sdk.ConsAddress(header.Block.ProposerAddress),
ChainId: header.Block.ChainID,
ChainId: &chainID,
}

// From ContextWithHeight: if the provided height is 0,
Expand All @@ -336,11 +344,18 @@ func (b *Backend) DoCall(
// the error message imitates geth behavior
return nil, errors.New("header not found")
}

tmp, err := ethermint.ParseChainID(header.Block.ChainID)
if err != nil {
return nil, sdkerrors.Wrapf(err, "failed to parse chainID: %s", header.Block.ChainID)
}
chainID := sdkmath.NewIntFromBigInt(tmp)

req := evmtypes.EthCallRequest{
Args: bz,
GasCap: b.RPCGasCap(),
ProposerAddress: sdk.ConsAddress(header.Block.ProposerAddress),
ChainId: header.Block.ChainID,
ChainId: &chainID,
}

// From ContextWithHeight: if the provided height is 0,
Expand Down
20 changes: 18 additions & 2 deletions rpc/backend/tracing.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@ import (
"encoding/json"
"fmt"

sdkmath "cosmossdk.io/math"

sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/ethereum/go-ethereum/common"
rpctypes "github.com/evmos/ethermint/rpc/types"
ethermint "github.com/evmos/ethermint/types"
evmtypes "github.com/evmos/ethermint/x/evm/types"
"github.com/pkg/errors"
tmrpctypes "github.com/tendermint/tendermint/rpc/core/types"
Expand Down Expand Up @@ -77,14 +81,20 @@ func (b *Backend) TraceTransaction(hash common.Hash, config *evmtypes.TraceConfi
return nil, fmt.Errorf("invalid transaction type %T", tx)
}

tmp, err := ethermint.ParseChainID(blk.Block.ChainID)
if err != nil {
return nil, sdkerrors.Wrapf(err, "failed to parse chainID: %s", blk.Block.ChainID)
}
chainID := sdkmath.NewIntFromBigInt(tmp)

traceTxRequest := evmtypes.QueryTraceTxRequest{
Msg: ethMessage,
Predecessors: predecessors,
BlockNumber: blk.Block.Height,
BlockTime: blk.Block.Time,
BlockHash: common.Bytes2Hex(blk.BlockID.Hash),
ProposerAddress: sdk.ConsAddress(blk.Block.ProposerAddress),
ChainId: blk.Block.ChainID,
ChainId: &chainID,
}

if config != nil {
Expand Down Expand Up @@ -156,14 +166,20 @@ func (b *Backend) TraceBlock(height rpctypes.BlockNumber,
}
ctxWithHeight := rpctypes.ContextWithHeight(int64(contextHeight))

tmp, err := ethermint.ParseChainID(block.Block.ChainID)
if err != nil {
return nil, sdkerrors.Wrapf(err, "failed to parse chainID: %s", block.Block.ChainID)
}
chainID := sdkmath.NewIntFromBigInt(tmp)

traceBlockRequest := &evmtypes.QueryTraceBlockRequest{
Txs: txsMessages,
TraceConfig: config,
BlockNumber: block.Block.Height,
BlockTime: block.Block.Time,
BlockHash: common.Bytes2Hex(block.BlockID.Hash),
ProposerAddress: sdk.ConsAddress(block.Block.ProposerAddress),
ChainId: block.Block.ChainID,
ChainId: &chainID,
}

res, err := b.queryClient.TraceBlock(ctxWithHeight, traceBlockRequest)
Expand Down
49 changes: 28 additions & 21 deletions x/evm/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,18 +217,17 @@ func (k Keeper) EthCall(c context.Context, req *types.EthCallRequest) (*types.Ms
}

ctx := sdk.UnwrapSDKContext(c)
if len(req.ChainId) > 0 {
ctx = ctx.WithChainID(req.ChainId)
k.WithChainID(ctx)
}

var args types.TransactionArgs
err := json.Unmarshal(req.Args, &args)
if err != nil {
return nil, status.Error(codes.InvalidArgument, err.Error())
}

cfg, err := k.EVMConfig(ctx, GetProposerAddress(ctx, req.ProposerAddress))
chainID, err := getChainID(ctx, req.ChainId.BigInt())
if err != nil {
return nil, status.Error(codes.InvalidArgument, err.Error())
}
cfg, err := k.EVMConfig(ctx, GetProposerAddress(ctx, req.ProposerAddress), chainID)
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
Expand Down Expand Up @@ -260,17 +259,17 @@ func (k Keeper) EstimateGas(c context.Context, req *types.EthCallRequest) (*type
}

ctx := sdk.UnwrapSDKContext(c)
if len(req.ChainId) > 0 {
ctx = ctx.WithChainID(req.ChainId)
k.WithChainID(ctx)
chainID, err := getChainID(ctx, req.ChainId.BigInt())
if err != nil {
return nil, status.Error(codes.InvalidArgument, err.Error())
}

if req.GasCap < ethparams.TxGas {
return nil, status.Error(codes.InvalidArgument, "gas cap cannot be lower than 21,000")
}

var args types.TransactionArgs
err := json.Unmarshal(req.Args, &args)
err = json.Unmarshal(req.Args, &args)
if err != nil {
return nil, status.Error(codes.InvalidArgument, err.Error())
}
Expand Down Expand Up @@ -302,7 +301,7 @@ func (k Keeper) EstimateGas(c context.Context, req *types.EthCallRequest) (*type
hi = req.GasCap
}
cap = hi
cfg, err := k.EVMConfig(ctx, GetProposerAddress(ctx, req.ProposerAddress))
cfg, err := k.EVMConfig(ctx, GetProposerAddress(ctx, req.ProposerAddress), chainID)
if err != nil {
return nil, status.Error(codes.Internal, "failed to load evm config")
}
Expand Down Expand Up @@ -382,12 +381,11 @@ func (k Keeper) TraceTx(c context.Context, req *types.QueryTraceTxRequest) (*typ
ctx = ctx.WithBlockHeight(contextHeight)
ctx = ctx.WithBlockTime(req.BlockTime)
ctx = ctx.WithHeaderHash(common.Hex2Bytes(req.BlockHash))
if len(req.ChainId) > 0 {
ctx = ctx.WithChainID(req.ChainId)
k.WithChainID(ctx)
chainID, err := getChainID(ctx, req.ChainId.BigInt())
if err != nil {
return nil, status.Error(codes.InvalidArgument, err.Error())
}

cfg, err := k.EVMConfig(ctx, GetProposerAddress(ctx, req.ProposerAddress))
cfg, err := k.EVMConfig(ctx, GetProposerAddress(ctx, req.ProposerAddress), chainID)
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to load evm config: %s", err.Error())
}
Expand Down Expand Up @@ -454,12 +452,12 @@ func (k Keeper) TraceBlock(c context.Context, req *types.QueryTraceBlockRequest)
ctx = ctx.WithBlockHeight(contextHeight)
ctx = ctx.WithBlockTime(req.BlockTime)
ctx = ctx.WithHeaderHash(common.Hex2Bytes(req.BlockHash))
if len(req.ChainId) > 0 {
ctx = ctx.WithChainID(req.ChainId)
k.WithChainID(ctx)
chainID, err := getChainID(ctx, req.ChainId.BigInt())
if err != nil {
return nil, status.Error(codes.InvalidArgument, err.Error())
}

cfg, err := k.EVMConfig(ctx, GetProposerAddress(ctx, req.ProposerAddress))
cfg, err := k.EVMConfig(ctx, GetProposerAddress(ctx, req.ProposerAddress), chainID)
if err != nil {
return nil, status.Error(codes.Internal, "failed to load evm config")
}
Expand Down Expand Up @@ -584,7 +582,8 @@ func (k Keeper) BaseFee(c context.Context, _ *types.QueryBaseFeeRequest) (*types
ctx := sdk.UnwrapSDKContext(c)

params := k.GetParams(ctx)
ethCfg := params.ChainConfig.EthereumConfig(k.eip155ChainID)
// the chainID parameter is not used in this case, just set to nil
ethCfg := params.ChainConfig.EthereumConfig(nil)
baseFee := k.GetBaseFee(ctx, ethCfg)

res := &types.QueryBaseFeeResponse{}
Expand All @@ -595,3 +594,11 @@ func (k Keeper) BaseFee(c context.Context, _ *types.QueryBaseFeeRequest) (*types

return res, nil
}

// getChainID parse chainID from current context if not provided
func getChainID(ctx sdk.Context, chainID *big.Int) (*big.Int, error) {
if chainID == nil {
return ethermint.ParseChainID(ctx.ChainID())
}
return chainID, nil
}
17 changes: 13 additions & 4 deletions x/evm/keeper/state_transition.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ func GasToRefund(availableRefund, gasConsumed, refundQuotient uint64) uint64 {
}

// EVMConfig creates the EVMConfig based on current state
func (k *Keeper) EVMConfig(ctx sdk.Context, proposerAddress sdk.ConsAddress) (*types.EVMConfig, error) {
func (k *Keeper) EVMConfig(ctx sdk.Context, proposerAddress sdk.ConsAddress, chainID *big.Int) (*types.EVMConfig, error) {
params := k.GetParams(ctx)
ethCfg := params.ChainConfig.EthereumConfig(k.eip155ChainID)
ethCfg := params.ChainConfig.EthereumConfig(chainID)

// get the coinbase address from the block proposer
coinbase, err := k.GetCoinbaseAddress(ctx, proposerAddress)
Expand Down Expand Up @@ -199,7 +199,11 @@ func (k *Keeper) ApplyTransaction(ctx sdk.Context, tx *ethtypes.Transaction) (*t
bloomReceipt ethtypes.Bloom
)

cfg, err := k.EVMConfig(ctx, sdk.ConsAddress(ctx.BlockHeader().ProposerAddress))
chainID, err := ethermint.ParseChainID(ctx.ChainID())
if err != nil {
return nil, sdkerrors.Wrapf(err, "invalid chain-id %s", ctx.ChainID())
}
cfg, err := k.EVMConfig(ctx, sdk.ConsAddress(ctx.BlockHeader().ProposerAddress), chainID)
if err != nil {
return nil, sdkerrors.Wrap(err, "failed to load evm config")
}
Expand Down Expand Up @@ -464,7 +468,12 @@ func (k *Keeper) ApplyMessageWithConfig(ctx sdk.Context,

// ApplyMessage calls ApplyMessageWithConfig with default EVMConfig
func (k *Keeper) ApplyMessage(ctx sdk.Context, msg core.Message, tracer vm.EVMLogger, commit bool) (*types.MsgEthereumTxResponse, error) {
cfg, err := k.EVMConfig(ctx, sdk.ConsAddress(ctx.BlockHeader().ProposerAddress))
chainID, err := ethermint.ParseChainID(ctx.ChainID())
if err != nil {
return nil, sdkerrors.Wrapf(err, "invalid chain-id %s", ctx.ChainID())
}

cfg, err := k.EVMConfig(ctx, sdk.ConsAddress(ctx.BlockHeader().ProposerAddress), chainID)
if err != nil {
return nil, sdkerrors.Wrap(err, "failed to load evm config")
}
Expand Down
6 changes: 2 additions & 4 deletions x/evm/simulation/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,10 +244,8 @@ func EstimateGas(ctx *simulateContext, from, to *common.Address, data *hexutil.B
}

res, err := ctx.keeper.EstimateGas(sdk.WrapSDKContext(ctx.context), &types.EthCallRequest{
Args: args,
GasCap: gasCap,
ProposerAddress: ctx.context.BlockHeader().ProposerAddress,
ChainId: ctx.context.ChainID(),
Args: args,
GasCap: gasCap,
})
if err != nil {
return 0, err
Expand Down
Loading

0 comments on commit b9e748f

Please sign in to comment.