Skip to content

Commit

Permalink
Remove global variables and aggregate them in the test engine
Browse files Browse the repository at this point in the history
  • Loading branch information
Mdaiki0730 committed Jan 27, 2025
1 parent b4fc2d9 commit bb61f53
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 28 deletions.
18 changes: 5 additions & 13 deletions blockchain/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -2733,11 +2733,6 @@ func (bc *BlockChain) SaveTrieNodeCacheToDisk() error {
return nil
}

var (
GasLimitInExecutionSpecTest uint64
UseKaiaCancunExtCodeHashFee bool
)

// ApplyTransaction attempts to apply a transaction to the given state database
// and uses the input parameters for its environment. It returns the receipt
// for the transaction, gas used and an error if the transaction failed,
Expand Down Expand Up @@ -2766,19 +2761,16 @@ func (bc *BlockChain) ApplyTransaction(chainConfig *params.ChainConfig, author *
// Create a new context to be used in the EVM environment
blockContext := NewEVMBlockContext(header, bc, author)

// when execution spec test, we can insert test GasLimit to blockContext.
if GasLimitInExecutionSpecTest != 0 {
blockContext.GasLimit = GasLimitInExecutionSpecTest
}
txContext := NewEVMTxContext(msg, header, chainConfig)
// Create a new environment which holds all relevant information
// about the transaction and calling mechanisms.
vmenv := vm.NewEVM(blockContext, txContext, statedb, chainConfig, vmConfig)

// when execution spec test, we can insert test GasLimit to blockContext.
if UseKaiaCancunExtCodeHashFee && chainConfig.Rules(header.Number).IsCancun {
// EIP-1052 must be activated for backward compatibility on Kaia. But EIP-2929 is activated instead of it on Ethereum
vm.ChangeGasCostForTest(&vmenv.Config.JumpTable, vm.EXTCODEHASH, params.WarmStorageReadCostEIP2929)
// change evm and msg for eest
if e := bc.Engine().(interface {
BeforeApplyMessage(*vm.EVM, *types.Transaction)
}); e != nil {
e.BeforeApplyMessage(vmenv, msg)
}

// Apply the transaction to the current state (included in the env)
Expand Down
4 changes: 1 addition & 3 deletions blockchain/types/tx_internal_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -571,8 +571,6 @@ func toWordSize(size uint64) uint64 {
return (size + 31) / 32
}

var IsPragueInExecutionSpecTest bool

// Klaytn-TxTypes since genesis, and EthTxTypes since istanbul use this.
func IntrinsicGasPayload(gas uint64, data []byte, isContractCreation bool, rules params.Rules) (uint64, uint64, error) {
var tokens uint64
Expand All @@ -593,7 +591,7 @@ func IntrinsicGasPayload(gas uint64, data []byte, isContractCreation bool, rules
// Since the genesis block, a flat 100 gas is paid
// regardless of whether the value is zero or non-zero.
nonZeroGas, zeroGas := params.TxDataGas, params.TxDataGas
if rules.IsPrague || IsPragueInExecutionSpecTest {
if rules.IsPrague {
nonZeroGas = params.TxDataNonZeroGasEIP2028
zeroGas = params.TxDataZeroGas
}
Expand Down
5 changes: 0 additions & 5 deletions tests/block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
"testing"

"github.com/kaiachain/kaia/blockchain"
"github.com/kaiachain/kaia/blockchain/types"
"github.com/kaiachain/kaia/common"
"github.com/stretchr/testify/suite"
)
Expand Down Expand Up @@ -64,17 +63,13 @@ type ExecutionSpecBlockTestSuite struct {
func (suite *ExecutionSpecBlockTestSuite) SetupSuite() {
suite.originalIsPrecompiledContractAddress = common.IsPrecompiledContractAddress
common.IsPrecompiledContractAddress = isPrecompiledContractAddressForEthTest
blockchain.UseKaiaCancunExtCodeHashFee = true
blockchain.CreateContractWithCodeFormatInExecutionSpecTest = true
}

func (suite *ExecutionSpecBlockTestSuite) TearDownSuite() {
// Reset global variables for test
common.IsPrecompiledContractAddress = suite.originalIsPrecompiledContractAddress
blockchain.UseKaiaCancunExtCodeHashFee = false
blockchain.GasLimitInExecutionSpecTest = 0
blockchain.CreateContractWithCodeFormatInExecutionSpecTest = false
types.IsPragueInExecutionSpecTest = false
}

func (suite *ExecutionSpecBlockTestSuite) TestExecutionSpecBlock() {
Expand Down
26 changes: 19 additions & 7 deletions tests/block_test_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,28 @@ type btHeaderMarshaling struct {

type eestEngine struct {
*gxhash.Gxhash
baseFee *big.Int
baseFee *big.Int
gasLimit uint64
}

var _ consensus.Engine = &eestEngine{}

func (e *eestEngine) BeforeApplyMessage(evm *vm.EVM, msg *types.Transaction) {
evm.Context.GasLimit = e.gasLimit
if evm.ChainConfig().Rules(evm.Context.BlockNumber).IsCancun {
// EIP-1052 must be activated for backward compatibility on Kaia. But EIP-2929 is activated instead of it on Ethereum
vm.ChangeGasCostForTest(&evm.Config.JumpTable, vm.EXTCODEHASH, params.WarmStorageReadCostEIP2929)
}

r := evm.ChainConfig().Rules(evm.Context.BlockNumber)
if evm.ChainConfig().Rules(evm.Context.BlockNumber).IsIstanbul {
r.IsPrague = true
}
updatedIntrinsicGas, dataTokens, _ := types.IntrinsicGas(msg.Data(), msg.AccessList(), msg.AuthorizationList(), msg.To() == nil, r)
from, _ := msg.From()
msg = types.NewMessage(from, msg.To(), msg.Nonce(), msg.GetTxInternalData().GetAmount(), msg.Gas(), msg.GasPrice(), msg.GasFeeCap(), msg.GasTipCap(), msg.Data(), true, updatedIntrinsicGas, dataTokens, msg.AccessList(), msg.AuthorizationList())
}

func (e *eestEngine) Initialize(chain consensus.ChainReader, header *types.Header, state *state.StateDB) {
if chain.Config().IsPragueForkEnabled(header.Number) {
context := blockchain.NewEVMBlockContext(header, chain, nil)
Expand Down Expand Up @@ -151,6 +168,7 @@ func (e *eestEngine) Finalize(chain consensus.ChainReader, header *types.Header,

func (e *eestEngine) applyHeader(h TestHeader) {
e.baseFee = h.BaseFee
e.gasLimit = h.GasLimit
}

func (t *BlockTest) Run() error {
Expand Down Expand Up @@ -300,12 +318,6 @@ func (t *BlockTest) insertBlocksFromTx(bc *blockchain.BlockChain, gBlock types.B
e.applyHeader(header)
}

// The intrinsic gas calculation affects gas used, so we need to make some changes to the main code.
if bc.Config().IsIstanbulForkEnabled(bc.CurrentHeader().Number) {
types.IsPragueInExecutionSpecTest = true
}
blockchain.GasLimitInExecutionSpecTest = header.GasLimit

// var maxFeePerGas *big.Int
blocks, receiptsList := blockchain.GenerateChain(bc.Config(), preBlock, bc.Engine(), db, 1, func(i int, b *blockchain.BlockGen) {
b.SetRewardbase(common.Address(header.Coinbase))
Expand Down

0 comments on commit bb61f53

Please sign in to comment.