Skip to content

Commit

Permalink
[protocol] remove config.EVMNetworkID() from EVM/execution
Browse files Browse the repository at this point in the history
  • Loading branch information
dustinxie committed Jun 3, 2022
1 parent 46602a9 commit 0ff0c80
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 20 deletions.
4 changes: 3 additions & 1 deletion action/protocol/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,10 @@ type (
BlockchainCtx struct {
// Tip is the information of tip block
Tip TipInfo
//ChainID of the node
//ChainID is the native chain ID
ChainID uint32
// EvmNetworkID is the EVM network ID
EvmNetworkID uint32
}

// BlockCtx provides block auxiliary information.
Expand Down
10 changes: 8 additions & 2 deletions action/protocol/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,17 @@ func TestWithBlockchainCtx(t *testing.T) {

func TestGetBlockchainCtx(t *testing.T) {
require := require.New(t)
bcCtx := BlockchainCtx{}
bcCtx := BlockchainCtx{
Tip: TipInfo{
Height: 1024,
Timestamp: time.Now(),
},
}
ctx := WithBlockchainCtx(context.Background(), bcCtx)
require.NotNil(ctx)
_, ok := GetBlockchainCtx(ctx)
bcCtx1, ok := GetBlockchainCtx(ctx)
require.True(ok)
require.Equal(bcCtx, bcCtx1)
}

func TestMustGetBlockchainCtx(t *testing.T) {
Expand Down
9 changes: 5 additions & 4 deletions action/protocol/execution/evm/evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import (
"github.com/iotexproject/iotex-core/action"
"github.com/iotexproject/iotex-core/action/protocol"
"github.com/iotexproject/iotex-core/blockchain/genesis"
"github.com/iotexproject/iotex-core/config"
"github.com/iotexproject/iotex-core/pkg/log"
"github.com/iotexproject/iotex-core/pkg/tracer"
"github.com/iotexproject/iotex-core/pkg/util/byteutil"
Expand Down Expand Up @@ -74,6 +73,7 @@ type (
context vm.BlockContext
txCtx vm.TxContext
nonce uint64
evmNetworkID uint32
executorRawAddress string
amount *big.Int
contract *common.Address
Expand Down Expand Up @@ -165,6 +165,7 @@ func newParams(
GasPrice: execution.GasPrice(),
},
execution.Nonce(),
protocol.MustGetBlockchainCtx(ctx).EvmNetworkID,
actionCtx.Caller.String(),
execution.Amount(),
contractAddrPointer,
Expand Down Expand Up @@ -321,7 +322,7 @@ func prepareStateDB(ctx context.Context, sm protocol.StateManager) *StateDBAdapt
)
}

func getChainConfig(g genesis.Blockchain, height uint64) *params.ChainConfig {
func getChainConfig(g genesis.Blockchain, height uint64, id uint32) *params.ChainConfig {
var chainConfig params.ChainConfig
chainConfig.ConstantinopleBlock = new(big.Int).SetUint64(0) // Constantinople switch block (nil = no fork, 0 = already activated)
chainConfig.BeringBlock = new(big.Int).SetUint64(g.BeringBlockHeight)
Expand All @@ -331,7 +332,7 @@ func getChainConfig(g genesis.Blockchain, height uint64) *params.ChainConfig {
chainConfig.IstanbulBlock = new(big.Int).SetUint64(g.IcelandBlockHeight)
chainConfig.MuirGlacierBlock = new(big.Int).SetUint64(g.IcelandBlockHeight)
if g.IsIceland(height) {
chainConfig.ChainID = new(big.Int).SetUint64(uint64(config.EVMNetworkID()))
chainConfig.ChainID = new(big.Int).SetUint64(uint64(id))
}
// enable Berlin and London
chainConfig.BerlinBlock = new(big.Int).SetUint64(g.ToBeEnabledBlockHeight)
Expand All @@ -353,7 +354,7 @@ func executeInEVM(ctx context.Context, evmParams *Params, stateDB *StateDBAdapte
if vmCfg, ok := protocol.GetVMConfigCtx(ctx); ok {
config = vmCfg
}
chainConfig := getChainConfig(g, blockHeight)
chainConfig := getChainConfig(g, blockHeight, evmParams.evmNetworkID)
evm := vm.NewEVM(evmParams.context, evmParams.txCtx, stateDB, chainConfig, config)
if g.IsToBeEnabled(blockHeight) {
accessList = evmParams.accessList
Expand Down
24 changes: 15 additions & 9 deletions action/protocol/execution/evm/evm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"github.com/iotexproject/iotex-core/action"
"github.com/iotexproject/iotex-core/action/protocol"
"github.com/iotexproject/iotex-core/blockchain/genesis"
"github.com/iotexproject/iotex-core/config"
"github.com/iotexproject/iotex-core/state"
"github.com/iotexproject/iotex-core/test/identityset"
"github.com/iotexproject/iotex-core/test/mock/mock_chainmanager"
Expand Down Expand Up @@ -57,7 +56,10 @@ func TestExecuteContractFailure(t *testing.T) {
})
ctx = genesis.WithGenesisContext(ctx, genesis.Default)

ctx = protocol.WithFeatureCtx(ctx)
ctx = protocol.WithBlockchainCtx(protocol.WithFeatureCtx(ctx), protocol.BlockchainCtx{
ChainID: 1,
EvmNetworkID: 100,
})
retval, receipt, err := ExecuteContract(ctx, sm, e,
func(uint64) (hash.Hash256, error) {
return hash.ZeroHash256, nil
Expand All @@ -80,8 +82,12 @@ func TestConstantinople(t *testing.T) {
Caller: identityset.Address(27),
})

evmNetworkID := uint32(100)
g := genesis.Default
ctx = genesis.WithGenesisContext(ctx, g)
ctx = protocol.WithBlockchainCtx(genesis.WithGenesisContext(ctx, g), protocol.BlockchainCtx{
ChainID: 1,
EvmNetworkID: evmNetworkID,
})

execHeights := []struct {
contract string
Expand Down Expand Up @@ -210,19 +216,19 @@ func TestConstantinople(t *testing.T) {
}
stateDB := NewStateDBAdapter(sm, e.height, hash.ZeroHash256, opt...)

ctx = protocol.WithBlockCtx(ctx, protocol.BlockCtx{
fCtx := protocol.WithBlockCtx(ctx, protocol.BlockCtx{
Producer: identityset.Address(27),
GasLimit: testutil.TestGasLimit,
BlockHeight: e.height,
})
ctx = protocol.WithFeatureCtx(ctx)
ps, err := newParams(ctx, ex, stateDB, func(uint64) (hash.Hash256, error) {
fCtx = protocol.WithFeatureCtx(fCtx)
ps, err := newParams(fCtx, ex, stateDB, func(uint64) (hash.Hash256, error) {
return hash.ZeroHash256, nil
})
require.NoError(err)

var evmConfig vm.Config
chainConfig := getChainConfig(g.Blockchain, e.height)
chainConfig := getChainConfig(g.Blockchain, e.height, ps.evmNetworkID)
evm := vm.NewEVM(ps.context, ps.txCtx, stateDB, chainConfig, evmConfig)

evmChainConfig := evm.ChainConfig()
Expand Down Expand Up @@ -253,7 +259,7 @@ func TestConstantinople(t *testing.T) {
// iceland = support chainID + enable Istanbul and Muir Glacier
isIceland := g.IsIceland(e.height)
if isIceland {
require.EqualValues(config.EVMNetworkID(), evmChainConfig.ChainID.Uint64())
require.EqualValues(evmNetworkID, evmChainConfig.ChainID.Uint64())
} else {
require.Nil(evmChainConfig.ChainID)
}
Expand Down Expand Up @@ -342,7 +348,7 @@ func TestGasEstimate(t *testing.T) {
for _, v := range []struct {
gas, consume, refund, size uint64
}{
{config.Default.Genesis.BlockGasLimit, 8200300, 1000000, 20000},
{genesis.Default.BlockGasLimit, 8200300, 1000000, 20000},
{1000000, 245600, 100000, 5600},
{500000, 21000, 10000, 36},
} {
Expand Down
2 changes: 0 additions & 2 deletions action/protocol/execution/protocol_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -941,8 +941,6 @@ func TestMaxTime(t *testing.T) {
}

func TestIstanbulEVM(t *testing.T) {
cfg := config.Default
config.SetEVMNetworkID(cfg.Chain.EVMNetworkID)
t.Run("ArrayReturn", func(t *testing.T) {
NewSmartContractTest(t, "testdata-istanbul/array-return.json")
})
Expand Down
11 changes: 9 additions & 2 deletions blockchain/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ type (
BlockFooterByHeight(height uint64) (*block.Footer, error)
// ChainID returns the chain ID
ChainID() uint32
// EvmNetworkID returns the evm network ID
EvmNetworkID() uint32
// ChainAddress returns chain address on parent chain, the root chain return empty.
ChainAddress() string
// TipHash returns tip block's hash
Expand Down Expand Up @@ -214,6 +216,10 @@ func (bc *blockchain) ChainID() uint32 {
return atomic.LoadUint32(&bc.config.Chain.ID)
}

func (bc *blockchain) EvmNetworkID() uint32 {
return atomic.LoadUint32(&bc.config.Chain.EVMNetworkID)
}

func (bc *blockchain) ChainAddress() string {
return bc.config.Chain.Address
}
Expand Down Expand Up @@ -365,8 +371,9 @@ func (bc *blockchain) context(ctx context.Context, tipInfoFlag bool) (context.Co
protocol.WithBlockchainCtx(
ctx,
protocol.BlockchainCtx{
Tip: tip,
ChainID: bc.ChainID(),
Tip: tip,
ChainID: bc.ChainID(),
EvmNetworkID: bc.EvmNetworkID(),
},
),
bc.config.Genesis,
Expand Down
14 changes: 14 additions & 0 deletions test/mock/mock_blockchain/mock_blockchain.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 0ff0c80

Please sign in to comment.