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

Commit

Permalink
evm, rpc: fix parameters and block gas limit in getBlockByHeight an…
Browse files Browse the repository at this point in the history
…d `getBlockByHash` (#312)

* fix evm set parameters

* recompute header hash only if its not set

* Update x/evm/keeper/state_transition.go

Co-authored-by: Federico Kunze Küllmer <31522760+fedekunze@users.noreply.github.com>
  • Loading branch information
thomas-nguy and fedekunze authored Jul 19, 2021
1 parent 0a8f02e commit c8b88a3
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 5 deletions.
2 changes: 1 addition & 1 deletion docs/architecture/adr-001-state.md
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ func (k *Keeper) NewEVM(msg core.Message, config *params.ChainConfig) *vm.EVM {
Transfer: core.Transfer,
GetHash: k.GetHashFn(),
Coinbase: common.Address{}, // there's no beneficiary since we're not mining
GasLimit: blockGasMeter.Limit(),
GasLimit: gasLimit,
BlockNumber: blockHeight,
Time: blockTime,
Difficulty: 0, // unused. Only required in PoW context
Expand Down
7 changes: 6 additions & 1 deletion ethereum/rpc/backend/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,12 @@ func (e *EVMBackend) EthBlockFromTendermint(
validatorAddr := common.BytesToAddress(addr)

bloom := ethtypes.BytesToBloom(blockBloomResp.Bloom)
formattedBlock := types.FormatBlock(block.Header, block.Size(), ethermint.DefaultRPCGasLimit, new(big.Int).SetUint64(gasUsed), ethRPCTxs, bloom, validatorAddr)

gasLimit, err := types.BlockMaxGasFromConsensusParams(types.ContextWithHeight(block.Height), e.clientCtx)
if err != nil {
e.logger.Error("failed to query consensus params", "error", err.Error())
}
formattedBlock := types.FormatBlock(block.Header, block.Size(), gasLimit, new(big.Int).SetUint64(gasUsed), ethRPCTxs, bloom, validatorAddr)
return formattedBlock, nil
}

Expand Down
2 changes: 1 addition & 1 deletion ethereum/rpc/types/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ func EthTransactionsFromTendermint(clientCtx client.Context, txs []tmtypes.Tx) (
func BlockMaxGasFromConsensusParams(ctx context.Context, clientCtx client.Context) (int64, error) {
resConsParams, err := clientCtx.Client.ConsensusParams(ctx, nil)
if err != nil {
return 0, err
return int64(^uint32(0)), err
}

gasLimit := resConsParams.ConsensusParams.Block.MaxGas
Expand Down
3 changes: 3 additions & 0 deletions init.sh
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ cat $HOME/.ethermintd/config/genesis.json | jq '.app_state["mint"]["params"]["mi
# increase block time (?)
cat $HOME/.ethermintd/config/genesis.json | jq '.consensus_params["block"]["time_iota_ms"]="30000"' > $HOME/.ethermintd/config/tmp_genesis.json && mv $HOME/.ethermintd/config/tmp_genesis.json $HOME/.ethermintd/config/genesis.json

# Set gas limit in genesis
cat $HOME/.ethermintd/config/genesis.json | jq '.consensus_params["block"]["max_gas"]="10000000"' > $HOME/.ethermintd/config/tmp_genesis.json && mv $HOME/.ethermintd/config/tmp_genesis.json $HOME/.ethermintd/config/genesis.json

# disable produce empty block
if [[ "$OSTYPE" == "darwin"* ]]; then
sed -i '' 's/create_empty_blocks = true/create_empty_blocks = false/g' $HOME/.ethermintd/config/config.toml
Expand Down
3 changes: 3 additions & 0 deletions tests/solidity/init-test-node.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ echo $USER2_MNEMONIC | ethermintd keys add $USER2_KEY --recover --keyring-backen

ethermintd init $MONIKER --chain-id $CHAINID

# Set gas limit in genesis
cat $HOME/.ethermintd/config/genesis.json | jq '.consensus_params["block"]["max_gas"]="10000000"' > $HOME/.ethermintd/config/tmp_genesis.json && mv $HOME/.ethermintd/config/tmp_genesis.json $HOME/.ethermintd/config/genesis.json

# Allocate genesis accounts (cosmos formatted addresses)
ethermintd add-genesis-account "$(ethermintd keys show $VAL_KEY -a --keyring-backend test)" 1000000000000000000000aphoton,1000000000000000000stake --keyring-backend test
ethermintd add-genesis-account "$(ethermintd keys show $USER1_KEY -a --keyring-backend test)" 1000000000000000000000aphoton,1000000000000000000stake --keyring-backend test
Expand Down
5 changes: 4 additions & 1 deletion types/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@ import sdk "github.com/cosmos/cosmos-sdk/types"
// NOTE: see https://github.com/cosmos/cosmos-sdk/issues/9514 for full reference
func BlockGasLimit(ctx sdk.Context) uint64 {
blockGasMeter := ctx.BlockGasMeter()
if blockGasMeter != nil {

// Get the limit from the gas meter only if its not null and not an InfiniteGasMeter
if blockGasMeter != nil && blockGasMeter.Limit() != 0 {
return blockGasMeter.Limit()
}

// Otherwise get from the consensus parameters
cp := ctx.ConsensusParams()
if cp == nil || cp.Block == nil {
return 0
Expand Down
16 changes: 15 additions & 1 deletion x/evm/keeper/state_transition.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,21 @@ func (k Keeper) GetHashFn() vm.GetHashFunc {
case k.ctx.BlockHeight() == h:
// Case 1: The requested height matches the one from the context so we can retrieve the header
// hash directly from the context.
return common.BytesToHash(k.ctx.HeaderHash())
// Note: The headerHash is only set at begin block, it will be nil in case of a query context
headerHash := k.ctx.HeaderHash()
if len(headerHash) != 0 {
return common.BytesToHash(headerHash)
}

// only recompute the hash if not set
contextBlockHeader := k.ctx.BlockHeader()
header, err := tmtypes.HeaderFromProto(&contextBlockHeader)
if err != nil {
k.Logger(k.ctx).Error("failed to cast tendermint header from proto", "error", err)
return common.Hash{}
}
headerHash = header.Hash()
return common.BytesToHash(headerHash)

case k.ctx.BlockHeight() > h:
// Case 2: if the chain is not the current height we need to retrieve the hash from the store for the
Expand Down

0 comments on commit c8b88a3

Please sign in to comment.