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

Commit b9e748f

Browse files
author
HuangYi
committed
pass chain-id to state transition
1 parent 879178b commit b9e748f

File tree

8 files changed

+232
-181
lines changed

8 files changed

+232
-181
lines changed

docs/api/proto-docs.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -563,7 +563,7 @@ EthCallRequest defines EthCall request
563563
| `args` | [bytes](#bytes) | | same json format as the json rpc api. |
564564
| `gas_cap` | [uint64](#uint64) | | the default gas cap to be used |
565565
| `proposer_address` | [bytes](#bytes) | | the proposer of the requested block |
566-
| `chain_id` | [string](#string) | | the full chain id in the requested block header |
566+
| `chain_id` | [string](#string) | | the eip155 chain id parsed from the requested block header |
567567

568568

569569

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

799799

800800

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

836836

837837

proto/ethermint/evm/v1/query.proto

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -223,8 +223,8 @@ message EthCallRequest {
223223
uint64 gas_cap = 2;
224224
// the proposer of the requested block
225225
bytes proposer_address = 3 [(gogoproto.casttype) = "github.com/cosmos/cosmos-sdk/types.ConsAddress"];
226-
// the full chain id in the requested block header
227-
string chain_id = 4;
226+
// the eip155 chain id parsed from the requested block header
227+
string chain_id = 4 [(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int"];
228228
}
229229

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

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

284284
// QueryTraceBlockResponse defines TraceBlock response

rpc/backend/call_tx.go

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import (
77
"fmt"
88
"math/big"
99

10+
sdkmath "cosmossdk.io/math"
11+
1012
"github.com/cosmos/cosmos-sdk/client/flags"
1113
sdk "github.com/cosmos/cosmos-sdk/types"
1214
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
@@ -305,11 +307,17 @@ func (b *Backend) EstimateGas(args evmtypes.TransactionArgs, blockNrOptional *rp
305307
return 0, errors.New("header not found")
306308
}
307309

310+
tmp, err := ethermint.ParseChainID(header.Block.ChainID)
311+
if err != nil {
312+
return 0, sdkerrors.Wrapf(err, "failed to parse chainID: %s", header.Block.ChainID)
313+
}
314+
chainID := sdkmath.NewIntFromBigInt(tmp)
315+
308316
req := evmtypes.EthCallRequest{
309317
Args: bz,
310318
GasCap: b.RPCGasCap(),
311319
ProposerAddress: sdk.ConsAddress(header.Block.ProposerAddress),
312-
ChainId: header.Block.ChainID,
320+
ChainId: &chainID,
313321
}
314322

315323
// From ContextWithHeight: if the provided height is 0,
@@ -336,11 +344,18 @@ func (b *Backend) DoCall(
336344
// the error message imitates geth behavior
337345
return nil, errors.New("header not found")
338346
}
347+
348+
tmp, err := ethermint.ParseChainID(header.Block.ChainID)
349+
if err != nil {
350+
return nil, sdkerrors.Wrapf(err, "failed to parse chainID: %s", header.Block.ChainID)
351+
}
352+
chainID := sdkmath.NewIntFromBigInt(tmp)
353+
339354
req := evmtypes.EthCallRequest{
340355
Args: bz,
341356
GasCap: b.RPCGasCap(),
342357
ProposerAddress: sdk.ConsAddress(header.Block.ProposerAddress),
343-
ChainId: header.Block.ChainID,
358+
ChainId: &chainID,
344359
}
345360

346361
// From ContextWithHeight: if the provided height is 0,

rpc/backend/tracing.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,13 @@ import (
44
"encoding/json"
55
"fmt"
66

7+
sdkmath "cosmossdk.io/math"
8+
79
sdk "github.com/cosmos/cosmos-sdk/types"
10+
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
811
"github.com/ethereum/go-ethereum/common"
912
rpctypes "github.com/evmos/ethermint/rpc/types"
13+
ethermint "github.com/evmos/ethermint/types"
1014
evmtypes "github.com/evmos/ethermint/x/evm/types"
1115
"github.com/pkg/errors"
1216
tmrpctypes "github.com/tendermint/tendermint/rpc/core/types"
@@ -77,14 +81,20 @@ func (b *Backend) TraceTransaction(hash common.Hash, config *evmtypes.TraceConfi
7781
return nil, fmt.Errorf("invalid transaction type %T", tx)
7882
}
7983

84+
tmp, err := ethermint.ParseChainID(blk.Block.ChainID)
85+
if err != nil {
86+
return nil, sdkerrors.Wrapf(err, "failed to parse chainID: %s", blk.Block.ChainID)
87+
}
88+
chainID := sdkmath.NewIntFromBigInt(tmp)
89+
8090
traceTxRequest := evmtypes.QueryTraceTxRequest{
8191
Msg: ethMessage,
8292
Predecessors: predecessors,
8393
BlockNumber: blk.Block.Height,
8494
BlockTime: blk.Block.Time,
8595
BlockHash: common.Bytes2Hex(blk.BlockID.Hash),
8696
ProposerAddress: sdk.ConsAddress(blk.Block.ProposerAddress),
87-
ChainId: blk.Block.ChainID,
97+
ChainId: &chainID,
8898
}
8999

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

169+
tmp, err := ethermint.ParseChainID(block.Block.ChainID)
170+
if err != nil {
171+
return nil, sdkerrors.Wrapf(err, "failed to parse chainID: %s", block.Block.ChainID)
172+
}
173+
chainID := sdkmath.NewIntFromBigInt(tmp)
174+
159175
traceBlockRequest := &evmtypes.QueryTraceBlockRequest{
160176
Txs: txsMessages,
161177
TraceConfig: config,
162178
BlockNumber: block.Block.Height,
163179
BlockTime: block.Block.Time,
164180
BlockHash: common.Bytes2Hex(block.BlockID.Hash),
165181
ProposerAddress: sdk.ConsAddress(block.Block.ProposerAddress),
166-
ChainId: block.Block.ChainID,
182+
ChainId: &chainID,
167183
}
168184

169185
res, err := b.queryClient.TraceBlock(ctxWithHeight, traceBlockRequest)

x/evm/keeper/grpc_query.go

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -217,18 +217,17 @@ func (k Keeper) EthCall(c context.Context, req *types.EthCallRequest) (*types.Ms
217217
}
218218

219219
ctx := sdk.UnwrapSDKContext(c)
220-
if len(req.ChainId) > 0 {
221-
ctx = ctx.WithChainID(req.ChainId)
222-
k.WithChainID(ctx)
223-
}
224220

225221
var args types.TransactionArgs
226222
err := json.Unmarshal(req.Args, &args)
227223
if err != nil {
228224
return nil, status.Error(codes.InvalidArgument, err.Error())
229225
}
230-
231-
cfg, err := k.EVMConfig(ctx, GetProposerAddress(ctx, req.ProposerAddress))
226+
chainID, err := getChainID(ctx, req.ChainId.BigInt())
227+
if err != nil {
228+
return nil, status.Error(codes.InvalidArgument, err.Error())
229+
}
230+
cfg, err := k.EVMConfig(ctx, GetProposerAddress(ctx, req.ProposerAddress), chainID)
232231
if err != nil {
233232
return nil, status.Error(codes.Internal, err.Error())
234233
}
@@ -260,17 +259,17 @@ func (k Keeper) EstimateGas(c context.Context, req *types.EthCallRequest) (*type
260259
}
261260

262261
ctx := sdk.UnwrapSDKContext(c)
263-
if len(req.ChainId) > 0 {
264-
ctx = ctx.WithChainID(req.ChainId)
265-
k.WithChainID(ctx)
262+
chainID, err := getChainID(ctx, req.ChainId.BigInt())
263+
if err != nil {
264+
return nil, status.Error(codes.InvalidArgument, err.Error())
266265
}
267266

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

272271
var args types.TransactionArgs
273-
err := json.Unmarshal(req.Args, &args)
272+
err = json.Unmarshal(req.Args, &args)
274273
if err != nil {
275274
return nil, status.Error(codes.InvalidArgument, err.Error())
276275
}
@@ -302,7 +301,7 @@ func (k Keeper) EstimateGas(c context.Context, req *types.EthCallRequest) (*type
302301
hi = req.GasCap
303302
}
304303
cap = hi
305-
cfg, err := k.EVMConfig(ctx, GetProposerAddress(ctx, req.ProposerAddress))
304+
cfg, err := k.EVMConfig(ctx, GetProposerAddress(ctx, req.ProposerAddress), chainID)
306305
if err != nil {
307306
return nil, status.Error(codes.Internal, "failed to load evm config")
308307
}
@@ -382,12 +381,11 @@ func (k Keeper) TraceTx(c context.Context, req *types.QueryTraceTxRequest) (*typ
382381
ctx = ctx.WithBlockHeight(contextHeight)
383382
ctx = ctx.WithBlockTime(req.BlockTime)
384383
ctx = ctx.WithHeaderHash(common.Hex2Bytes(req.BlockHash))
385-
if len(req.ChainId) > 0 {
386-
ctx = ctx.WithChainID(req.ChainId)
387-
k.WithChainID(ctx)
384+
chainID, err := getChainID(ctx, req.ChainId.BigInt())
385+
if err != nil {
386+
return nil, status.Error(codes.InvalidArgument, err.Error())
388387
}
389-
390-
cfg, err := k.EVMConfig(ctx, GetProposerAddress(ctx, req.ProposerAddress))
388+
cfg, err := k.EVMConfig(ctx, GetProposerAddress(ctx, req.ProposerAddress), chainID)
391389
if err != nil {
392390
return nil, status.Errorf(codes.Internal, "failed to load evm config: %s", err.Error())
393391
}
@@ -454,12 +452,12 @@ func (k Keeper) TraceBlock(c context.Context, req *types.QueryTraceBlockRequest)
454452
ctx = ctx.WithBlockHeight(contextHeight)
455453
ctx = ctx.WithBlockTime(req.BlockTime)
456454
ctx = ctx.WithHeaderHash(common.Hex2Bytes(req.BlockHash))
457-
if len(req.ChainId) > 0 {
458-
ctx = ctx.WithChainID(req.ChainId)
459-
k.WithChainID(ctx)
455+
chainID, err := getChainID(ctx, req.ChainId.BigInt())
456+
if err != nil {
457+
return nil, status.Error(codes.InvalidArgument, err.Error())
460458
}
461459

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

586584
params := k.GetParams(ctx)
587-
ethCfg := params.ChainConfig.EthereumConfig(k.eip155ChainID)
585+
// the chainID parameter is not used in this case, just set to nil
586+
ethCfg := params.ChainConfig.EthereumConfig(nil)
588587
baseFee := k.GetBaseFee(ctx, ethCfg)
589588

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

596595
return res, nil
597596
}
597+
598+
// getChainID parse chainID from current context if not provided
599+
func getChainID(ctx sdk.Context, chainID *big.Int) (*big.Int, error) {
600+
if chainID == nil {
601+
return ethermint.ParseChainID(ctx.ChainID())
602+
}
603+
return chainID, nil
604+
}

x/evm/keeper/state_transition.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ func GasToRefund(availableRefund, gasConsumed, refundQuotient uint64) uint64 {
3939
}
4040

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

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

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

465469
// ApplyMessage calls ApplyMessageWithConfig with default EVMConfig
466470
func (k *Keeper) ApplyMessage(ctx sdk.Context, msg core.Message, tracer vm.EVMLogger, commit bool) (*types.MsgEthereumTxResponse, error) {
467-
cfg, err := k.EVMConfig(ctx, sdk.ConsAddress(ctx.BlockHeader().ProposerAddress))
471+
chainID, err := ethermint.ParseChainID(ctx.ChainID())
472+
if err != nil {
473+
return nil, sdkerrors.Wrapf(err, "invalid chain-id %s", ctx.ChainID())
474+
}
475+
476+
cfg, err := k.EVMConfig(ctx, sdk.ConsAddress(ctx.BlockHeader().ProposerAddress), chainID)
468477
if err != nil {
469478
return nil, sdkerrors.Wrap(err, "failed to load evm config")
470479
}

x/evm/simulation/operations.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -244,10 +244,8 @@ func EstimateGas(ctx *simulateContext, from, to *common.Address, data *hexutil.B
244244
}
245245

246246
res, err := ctx.keeper.EstimateGas(sdk.WrapSDKContext(ctx.context), &types.EthCallRequest{
247-
Args: args,
248-
GasCap: gasCap,
249-
ProposerAddress: ctx.context.BlockHeader().ProposerAddress,
250-
ChainId: ctx.context.ChainID(),
247+
Args: args,
248+
GasCap: gasCap,
251249
})
252250
if err != nil {
253251
return 0, err

0 commit comments

Comments
 (0)