Skip to content

Commit

Permalink
Problem: can't support prevrando opcode (backport #443) (#444)
Browse files Browse the repository at this point in the history
* Problem: can't support prevrando opcode (#443)

* Problem: no proper error msg for unsupported opcode

* panic specific

* fix test

* update deps

* Apply suggestions from code review

* Apply suggestions from code review

* keep default nil

* clean doc
  • Loading branch information
mmsqe authored Apr 2, 2024
1 parent 0cbadd0 commit 2740d1f
Show file tree
Hide file tree
Showing 10 changed files with 54 additions and 5 deletions.
6 changes: 4 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,14 @@ Ref: https://keepachangelog.com/en/1.0.0/

### Bug Fixes


* (rpc) [#434](https://github.com/crypto-org-chain/ethermint/pull/434) No need gasPrice when patch gasUsed for `eth_getTransactionReceipt`.
* (feemarket) [#433](https://github.com/crypto-org-chain/ethermint/pull/433) Fix sdk int conversion panic with baseFee.
* (rpc) [#439](https://github.com/crypto-org-chain/ethermint/pull/439), [#441](https://github.com/crypto-org-chain/ethermint/pull/441) Align trace response for failed tx with go-ethereum.

### Features
### State Machine Breaking

* (rpc) [#443](https://github.com/crypto-org-chain/ethermint/pull/443) Keep behavior of random opcode as before.


## v0.21.x-cronos

Expand Down
1 change: 1 addition & 0 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,7 @@ func NewEthermintApp(
tracer,
evmSs, nil,
allKeys,
nil,
)

// Create IBC Keeper
Expand Down
2 changes: 1 addition & 1 deletion tests/integration_tests/expected_constants.py

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions tests/integration_tests/hardhat/contracts/Random.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.15;

contract Random {
function randomTokenId() public returns (uint256) {
return uint256(keccak256(abi.encodePacked(block.prevrandao)));
}
}
9 changes: 9 additions & 0 deletions tests/integration_tests/hardhat/hardhat.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@ import "@nomiclabs/hardhat-ethers";
const config: HardhatUserConfig = {
solidity: {
compilers: [
{
version: "0.8.21",
settings: {
optimizer: {
enabled: true
},
evmVersion: "shanghai"
}
},
{
version: "0.8.10",
settings: {
Expand Down
11 changes: 11 additions & 0 deletions tests/integration_tests/test_call.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import json

import pytest
from hexbytes import HexBytes
from web3 import Web3
from web3._utils.contracts import encode_transaction_data
Expand Down Expand Up @@ -95,3 +96,13 @@ def test_override_state(ethermint):
},
)
assert ("",) == w3.codec.decode(("string",), result)


def test_opcode(ethermint):
contract, _ = deploy_contract(
ethermint.w3,
CONTRACTS["Random"],
)
with pytest.raises(ValueError) as e_info:
contract.caller.randomTokenId()
assert "invalid memory address or nil pointer dereference" in str(e_info.value)
1 change: 1 addition & 0 deletions tests/integration_tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"TestMessageCall": "TestMessageCall.sol",
"Calculator": "Calculator.sol",
"Caller": "Caller.sol",
"Random": "Random.sol",
}


Expand Down
10 changes: 10 additions & 0 deletions x/evm/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ type Keeper struct {
// a set of store keys that should cover all the precompile use cases,
// or ideally just pass the application's all stores.
keys map[string]storetypes.StoreKey

forkEnabledFunc func(sdk.Context) bool
}

// NewKeeper generates new evm module keeper
Expand All @@ -94,6 +96,7 @@ func NewKeeper(
ss paramstypes.Subspace,
customContractFns []CustomContractFn,
keys map[string]storetypes.StoreKey,
forkEnabledFunc func(sdk.Context) bool,
) *Keeper {
// ensure evm module account is set
if addr := ak.GetModuleAddress(types.ModuleName); addr == nil {
Expand All @@ -105,6 +108,12 @@ func NewKeeper(
panic(err)
}

if forkEnabledFunc == nil {
forkEnabledFunc = func(sdk.Context) bool {
return false
}
}

// NOTE: we pass in the parameter space to the CommitStateDB in order to use custom denominations for the EVM operations
return &Keeper{
cdc: cdc,
Expand All @@ -119,6 +128,7 @@ func NewKeeper(
ss: ss,
customContractFns: customContractFns,
keys: keys,
forkEnabledFunc: forkEnabledFunc,
}
}

Expand Down
10 changes: 8 additions & 2 deletions x/evm/keeper/state_transition.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ func (k *Keeper) NewEVM(
msg core.Message,
cfg *EVMConfig,
stateDB vm.StateDB,
random *common.Hash,
) *vm.EVM {
blockCtx := vm.BlockContext{
CanTransfer: core.CanTransfer,
Expand All @@ -62,7 +63,7 @@ func (k *Keeper) NewEVM(
Time: uint64(ctx.BlockHeader().Time.Unix()),
Difficulty: big.NewInt(0), // unused. Only required in PoW context
BaseFee: cfg.BaseFee,
Random: nil, // not supported
Random: random, // not supported
}
if cfg.BlockOverrides != nil {
cfg.BlockOverrides.Apply(&blockCtx)
Expand Down Expand Up @@ -361,7 +362,12 @@ func (k *Keeper) ApplyMessageWithConfig(
return nil, errorsmod.Wrap(err, "failed to apply state override")
}
}
evm = k.NewEVM(ctx, msg, cfg, stateDB)
var random *common.Hash
if k.forkEnabledFunc(ctx) {
zero := common.BigToHash(big.NewInt(0))
random = &zero
}
evm = k.NewEVM(ctx, msg, cfg, stateDB, random)
leftoverGas := msg.GasLimit
sender := vm.AccountRef(msg.From)
// Allow the tracer captures the tx level events, mainly the gas consumption.
Expand Down
1 change: 1 addition & 0 deletions x/evm/statedb/statedb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -825,6 +825,7 @@ func newTestKeeper(t *testing.T, cms sdk.MultiStore) (sdk.Context, *evmkeeper.Ke
"",
paramstypes.Subspace{}, nil,
allKeys,
nil,
)

ctx := sdk.NewContext(cms, tmproto.Header{}, false, log.NewNopLogger())
Expand Down

0 comments on commit 2740d1f

Please sign in to comment.