Skip to content

Commit

Permalink
[evm] enable Random opcode (EIP-4399) at Redsea height
Browse files Browse the repository at this point in the history
  • Loading branch information
dustinxie committed Oct 18, 2023
1 parent a2c0285 commit 65c83df
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
8 changes: 7 additions & 1 deletion action/protocol/execution/evm/evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,12 @@ func executeInEVM(ctx context.Context, evmParams *Params, stateDB *StateDBAdapte
config = vmCfg
}
chainConfig := getChainConfig(g, blockHeight, evmParams.evmNetworkID)
if g.IsRedsea(blockHeight) {
// Merge enabled at Redsea height
tipHash := protocol.MustGetBlockchainCtx(ctx).Tip.Hash
h := common.BytesToHash(tipHash[:])
evmParams.context.Random = &h
}
evm := vm.NewEVM(evmParams.context, evmParams.txCtx, stateDB, chainConfig, config)
if g.IsOkhotsk(blockHeight) {
accessList = evmParams.accessList
Expand All @@ -432,7 +438,7 @@ func executeInEVM(ctx context.Context, evmParams *Params, stateDB *StateDBAdapte
remainingGas -= intriGas

// Set up the initial access list
rules := chainConfig.Rules(evm.Context.BlockNumber, g.IsRedsea(blockHeight)) // Merge enabled at Redsea height
rules := chainConfig.Rules(evm.Context.BlockNumber, evm.Context.Random != nil)
if rules.IsBerlin {
stateDB.PrepareAccessList(evmParams.txCtx.Origin, evmParams.contract, vm.ActivePrecompiles(rules), evmParams.accessList)
}
Expand Down
14 changes: 13 additions & 1 deletion action/protocol/execution/evm/evm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"math/big"
"testing"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/vm"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -229,6 +230,10 @@ func TestConstantinople(t *testing.T) {
34838200,
},
// after Redsea
{
action.EmptyAddress,
34838201,
},
{
"io1pcg2ja9krrhujpazswgz77ss46xgt88afqlk6y",
math.MaxUint64,
Expand Down Expand Up @@ -260,7 +265,13 @@ func TestConstantinople(t *testing.T) {

var evmConfig vm.Config
chainConfig := getChainConfig(g.Blockchain, e.height, ps.evmNetworkID)
if g.IsRedsea(e.height) {
// Merge enabled at Redsea height
h := common.BytesToHash(hash.ZeroHash256[:])
ps.context.Random = &h
}
evm := vm.NewEVM(ps.context, ps.txCtx, stateDB, chainConfig, evmConfig)
require.Equal(e.height, evm.Context.BlockNumber.Uint64())

evmChainConfig := evm.ChainConfig()
require.Equal(g.IsGreenland(e.height), evmChainConfig.IsHomestead(evm.Context.BlockNumber))
Expand All @@ -273,7 +284,7 @@ func TestConstantinople(t *testing.T) {
require.True(evmChainConfig.IsPetersburg(evm.Context.BlockNumber))

// verify chainRules
chainRules := evmChainConfig.Rules(ps.context.BlockNumber, g.IsRedsea(e.height))
chainRules := evmChainConfig.Rules(ps.context.BlockNumber, evm.Context.Random != nil)
require.Equal(g.IsGreenland(e.height), chainRules.IsHomestead)
require.Equal(g.IsGreenland(e.height), chainRules.IsEIP150)
require.Equal(g.IsGreenland(e.height), chainRules.IsEIP158)
Expand Down Expand Up @@ -316,6 +327,7 @@ func TestConstantinople(t *testing.T) {
require.Equal(isRedsea, evmChainConfig.IsArrowGlacier(evm.Context.BlockNumber))
require.Equal(isRedsea, evmChainConfig.IsGrayGlacier(evm.Context.BlockNumber))
require.Equal(isRedsea, chainRules.IsMerge)
require.Equal(isRedsea, evm.Context.Random != nil)

// Shanghai and Cancun not yet enabled
require.False(chainRules.IsShanghai)
Expand Down
15 changes: 15 additions & 0 deletions action/protocol/execution/protocol_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,21 @@ func TestProtocol_Handle(t *testing.T) {
cfg.Genesis.EnableGravityChainVoting = false
cfg.ActPool.MinGasPriceStr = "0"
cfg.Genesis.InitBalanceMap[identityset.Address(27).String()] = unit.ConvertIotxToRau(1000000000).String()
// cfg.Genesis.BeringBlockHeight = 1
// cfg.Genesis.CookBlockHeight = 1
// cfg.Genesis.DardanellesBlockHeight = 1
// cfg.Genesis.DaytonaBlockHeight = 1
// cfg.Genesis.EasterBlockHeight = 1
// cfg.Genesis.FbkMigrationBlockHeight = 1
// cfg.Genesis.FairbankBlockHeight = 1
// cfg.Genesis.GreenlandBlockHeight = 1
// cfg.Genesis.IcelandBlockHeight = 1
// cfg.Genesis.JutlandBlockHeight = 1
// cfg.Genesis.KamchatkaBlockHeight = 1
// cfg.Genesis.LordHoweBlockHeight = 1
// cfg.Genesis.MidwayBlockHeight = 1
// cfg.Genesis.NewfoundlandBlockHeight = 1
// cfg.Genesis.OkhotskBlockHeight = 1
ctx := genesis.WithGenesisContext(context.Background(), cfg.Genesis)

registry := protocol.NewRegistry()
Expand Down

0 comments on commit 65c83df

Please sign in to comment.