From a4c62041f3b356a7d7a36327d7b607d8e88ded3c Mon Sep 17 00:00:00 2001 From: yperbasis Date: Wed, 7 Dec 2022 17:15:00 +0100 Subject: [PATCH] Shanghai & Cancun forks should be based on timestamp --- cl/clparams/config.go | 8 ++-- cmd/evm/internal/t8ntool/transition.go | 4 +- cmd/evm/runner.go | 2 +- cmd/rpcdaemon/commands/eth_block.go | 10 +++-- cmd/rpcdaemon/commands/eth_call.go | 2 +- cmd/rpcdaemon/commands/eth_callMany.go | 7 ++-- cmd/rpcdaemon/commands/eth_receipts.go | 5 ++- .../commands/otterscan_generic_tracer.go | 4 +- .../commands/otterscan_search_trace.go | 4 +- cmd/rpcdaemon/commands/trace_adhoc.go | 4 +- cmd/rpcdaemon/commands/trace_filtering.go | 8 ++-- cmd/rpcdaemon/commands/tracing.go | 9 ++--- cmd/state/commands/erigon2.go | 2 +- cmd/state/commands/erigon4.go | 2 +- cmd/state/commands/history22.go | 7 ++-- cmd/state/commands/opcode_tracer.go | 2 +- cmd/state/commands/replay_tx.go | 2 +- consensus/misc/eip1559_test.go | 4 +- consensus/parlia/parlia.go | 2 +- consensus/serenity/serenity.go | 2 +- core/blockchain.go | 6 +-- core/chain_makers.go | 2 +- core/vm/evm.go | 2 +- core/vm/gas_table_test.go | 4 +- core/vm/runtime/runtime.go | 10 ++--- eth/stagedsync/exec3.go | 4 +- params/config.go | 38 ++++++++----------- tests/state_test_util.go | 2 +- tests/transaction_test_util.go | 2 +- turbo/transactions/tracing.go | 2 +- 30 files changed, 78 insertions(+), 84 deletions(-) diff --git a/cl/clparams/config.go b/cl/clparams/config.go index 585bf5372a9..bf41e9998bc 100644 --- a/cl/clparams/config.go +++ b/cl/clparams/config.go @@ -430,7 +430,7 @@ func configForkNames(b *BeaconChainConfig) map[[VersionLength]byte]string { return fvn } -var mainnetBeaconConfig BeaconChainConfig = BeaconChainConfig{ +var MainnetBeaconConfig BeaconChainConfig = BeaconChainConfig{ // Constants (Non-configurable) FarFutureEpoch: math.MaxUint64, FarFutureSlot: math.MaxUint64, @@ -622,13 +622,13 @@ var mainnetBeaconConfig BeaconChainConfig = BeaconChainConfig{ } func mainnetConfig() BeaconChainConfig { - cfg := mainnetBeaconConfig + cfg := MainnetBeaconConfig cfg.InitializeForkSchedule() return cfg } func sepoliaConfig() BeaconChainConfig { - cfg := mainnetBeaconConfig + cfg := MainnetBeaconConfig cfg.MinGenesisTime = 1655647200 cfg.GenesisDelay = 86400 cfg.MinGenesisActiveValidatorCount = 1300 @@ -648,7 +648,7 @@ func sepoliaConfig() BeaconChainConfig { } func goerliConfig() BeaconChainConfig { - cfg := mainnetBeaconConfig + cfg := MainnetBeaconConfig cfg.MinGenesisTime = 1614588812 cfg.GenesisDelay = 1919188 cfg.ConfigName = "prater" diff --git a/cmd/evm/internal/t8ntool/transition.go b/cmd/evm/internal/t8ntool/transition.go index 0ac17991a66..e2769b5104e 100644 --- a/cmd/evm/internal/t8ntool/transition.go +++ b/cmd/evm/internal/t8ntool/transition.go @@ -235,7 +235,7 @@ func Main(ctx *cli.Context) error { return NewError(ErrorVMConfig, errors.New("can only apply RANDOM on top of London chain rules")) } - if chainConfig.IsShanghai(prestate.Env.Number) && prestate.Env.Withdrawals == nil { + if chainConfig.IsShanghai(prestate.Env.Timestamp) && prestate.Env.Withdrawals == nil { return NewError(ErrorVMConfig, errors.New("Shanghai config but missing 'withdrawals' in env section")) } @@ -286,7 +286,7 @@ func Main(ctx *cli.Context) error { } defer tx.Rollback() - reader, writer := MakePreState(chainConfig.Rules(0), tx, prestate.Pre) + reader, writer := MakePreState(chainConfig.Rules(0, 0), tx, prestate.Pre) engine := ethash.NewFaker() result, err := core.ExecuteBlockEphemerally(chainConfig, &vmConfig, getHash, engine, block, reader, writer, nil, nil, getTracer) diff --git a/cmd/evm/runner.go b/cmd/evm/runner.go index 081e0531fc3..ae1a831c7fb 100644 --- a/cmd/evm/runner.go +++ b/cmd/evm/runner.go @@ -286,7 +286,7 @@ func runCmd(ctx *cli.Context) error { if ctx.Bool(DumpFlag.Name) { rules := ¶ms.Rules{} if chainConfig != nil { - rules = chainConfig.Rules(runtimeConfig.BlockNumber.Uint64()) + rules = chainConfig.Rules(runtimeConfig.BlockNumber.Uint64(), runtimeConfig.Time.Uint64()) } if err = statedb.CommitBlock(rules, state.NewNoopWriter()); err != nil { fmt.Println("Could not commit state: ", err) diff --git a/cmd/rpcdaemon/commands/eth_block.go b/cmd/rpcdaemon/commands/eth_block.go index 5d44d3220b7..04e2fa573bb 100644 --- a/cmd/rpcdaemon/commands/eth_block.go +++ b/cmd/rpcdaemon/commands/eth_block.go @@ -7,6 +7,9 @@ import ( "time" "github.com/ledgerwatch/erigon-lib/kv" + "github.com/ledgerwatch/log/v3" + + "github.com/ledgerwatch/erigon/cl/clparams" "github.com/ledgerwatch/erigon/common" "github.com/ledgerwatch/erigon/common/hexutil" "github.com/ledgerwatch/erigon/common/math" @@ -20,7 +23,6 @@ import ( "github.com/ledgerwatch/erigon/turbo/adapter/ethapi" "github.com/ledgerwatch/erigon/turbo/rpchelper" "github.com/ledgerwatch/erigon/turbo/transactions" - "github.com/ledgerwatch/log/v3" ) func (api *APIImpl) CallBundle(ctx context.Context, txHashes []common.Hash, stateBlockNumberOrHash rpc.BlockNumberOrHash, timeoutMilliSecondsPtr *int64) (map[string]interface{}, error) { @@ -97,7 +99,7 @@ func (api *APIImpl) CallBundle(ctx context.Context, txHashes []common.Hash, stat blockNumber := stateBlockNumber + 1 - timestamp := parent.Time // Dont care about the timestamp + timestamp := parent.Time + clparams.MainnetBeaconConfig.SecondsPerSlot coinbase := parent.Coinbase header := &types.Header{ @@ -109,9 +111,8 @@ func (api *APIImpl) CallBundle(ctx context.Context, txHashes []common.Hash, stat Coinbase: coinbase, } - // Get a new instance of the EVM signer := types.MakeSigner(chainConfig, blockNumber) - rules := chainConfig.Rules(blockNumber) + rules := chainConfig.Rules(blockNumber, timestamp) firstMsg, err := txs[0].AsMessage(*signer, nil, rules) if err != nil { return nil, err @@ -119,6 +120,7 @@ func (api *APIImpl) CallBundle(ctx context.Context, txHashes []common.Hash, stat blockCtx := transactions.NewEVMBlockContext(engine, header, stateBlockNumberOrHash.RequireCanonical, tx, api._blockReader) txCtx := core.NewEVMTxContext(firstMsg) + // Get a new instance of the EVM evm := vm.NewEVM(blockCtx, txCtx, ibs, chainConfig, vm.Config{Debug: false}) timeoutMilliSeconds := int64(5000) diff --git a/cmd/rpcdaemon/commands/eth_call.go b/cmd/rpcdaemon/commands/eth_call.go index fca5f7a0028..1328fba562e 100644 --- a/cmd/rpcdaemon/commands/eth_call.go +++ b/cmd/rpcdaemon/commands/eth_call.go @@ -398,7 +398,7 @@ func (api *APIImpl) CreateAccessList(ctx context.Context, args ethapi2.CallArgs, } // Retrieve the precompiles since they don't need to be added to the access list - precompiles := vm.ActivePrecompiles(chainConfig.Rules(blockNumber)) + precompiles := vm.ActivePrecompiles(chainConfig.Rules(blockNumber, header.Time)) // Create an initial tracer prevTracer := logger.NewAccessListTracer(nil, *args.From, to, precompiles) diff --git a/cmd/rpcdaemon/commands/eth_callMany.go b/cmd/rpcdaemon/commands/eth_callMany.go index 8b5473cf0b9..079aae896ae 100644 --- a/cmd/rpcdaemon/commands/eth_callMany.go +++ b/cmd/rpcdaemon/commands/eth_callMany.go @@ -144,10 +144,6 @@ func (api *APIImpl) CallMany(ctx context.Context, bundles []Bundle, simulateCont return nil, fmt.Errorf("block %d(%x) not found", blockNum, hash) } - // Get a new instance of the EVM - signer := types.MakeSigner(chainConfig, blockNum) - rules := chainConfig.Rules(blockNum) - getHash := func(i uint64) common.Hash { if hash, ok := overrideBlockHash[i]; ok { return hash @@ -175,7 +171,10 @@ func (api *APIImpl) CallMany(ctx context.Context, bundles []Bundle, simulateCont BaseFee: &baseFee, } + // Get a new instance of the EVM evm = vm.NewEVM(blockCtx, txCtx, st, chainConfig, vm.Config{Debug: false}) + signer := types.MakeSigner(chainConfig, blockNum) + rules := chainConfig.Rules(blockNum, blockCtx.Time) timeoutMilliSeconds := int64(5000) diff --git a/cmd/rpcdaemon/commands/eth_receipts.go b/cmd/rpcdaemon/commands/eth_receipts.go index 9ee3c074aeb..49aac18de79 100644 --- a/cmd/rpcdaemon/commands/eth_receipts.go +++ b/cmd/rpcdaemon/commands/eth_receipts.go @@ -13,6 +13,8 @@ import ( "github.com/ledgerwatch/erigon-lib/kv" "github.com/ledgerwatch/erigon-lib/kv/bitmapdb" libstate "github.com/ledgerwatch/erigon-lib/state" + "github.com/ledgerwatch/log/v3" + "github.com/ledgerwatch/erigon/common" "github.com/ledgerwatch/erigon/common/dbutils" "github.com/ledgerwatch/erigon/common/hexutil" @@ -28,7 +30,6 @@ import ( "github.com/ledgerwatch/erigon/rpc" "github.com/ledgerwatch/erigon/turbo/rpchelper" "github.com/ledgerwatch/erigon/turbo/transactions" - "github.com/ledgerwatch/log/v3" ) func (api *BaseAPI) getReceipts(ctx context.Context, tx kv.Tx, chainConfig *params.ChainConfig, block *types.Block, senders []common.Address) (types.Receipts, error) { @@ -381,7 +382,7 @@ func (api *APIImpl) getLogsV3(ctx context.Context, tx kv.Tx, begin, end uint64, lastBlockNum = blockNum blockHash = header.Hash() signer = types.MakeSigner(chainConfig, blockNum) - rules = chainConfig.Rules(blockNum) + rules = chainConfig.Rules(blockNum, header.Time) vmConfig.SkipAnalysis = core.SkipAnalysis(chainConfig, blockNum) minTxNumInBlock, err = rawdb.TxNums.Min(tx, blockNum) diff --git a/cmd/rpcdaemon/commands/otterscan_generic_tracer.go b/cmd/rpcdaemon/commands/otterscan_generic_tracer.go index a0111915399..dfa5ea8317a 100644 --- a/cmd/rpcdaemon/commands/otterscan_generic_tracer.go +++ b/cmd/rpcdaemon/commands/otterscan_generic_tracer.go @@ -48,7 +48,7 @@ func (api *OtterscanAPIImpl) genericTracer(dbtx kv.Tx, ctx context.Context, bloc engine := api.engine() header := block.Header() - rules := chainConfig.Rules(block.NumberU64()) + rules := chainConfig.Rules(block.NumberU64(), header.Time) for idx, tx := range block.Transactions() { ibs.Prepare(tx.Hash(), block.Hash(), idx) @@ -61,7 +61,7 @@ func (api *OtterscanAPIImpl) genericTracer(dbtx kv.Tx, ctx context.Context, bloc if _, err := core.ApplyMessage(vmenv, msg, new(core.GasPool).AddGas(tx.GetGas()), true /* refunds */, false /* gasBailout */); err != nil { return err } - _ = ibs.FinalizeTx(vmenv.ChainConfig().Rules(block.NumberU64()), cachedWriter) + _ = ibs.FinalizeTx(rules, cachedWriter) if tracer.Found() { tracer.SetTransaction(tx) diff --git a/cmd/rpcdaemon/commands/otterscan_search_trace.go b/cmd/rpcdaemon/commands/otterscan_search_trace.go index b4ad9849fb0..151c9411e98 100644 --- a/cmd/rpcdaemon/commands/otterscan_search_trace.go +++ b/cmd/rpcdaemon/commands/otterscan_search_trace.go @@ -72,7 +72,7 @@ func (api *OtterscanAPIImpl) traceBlock(dbtx kv.Tx, ctx context.Context, blockNu blockReceipts := rawdb.ReadReceipts(dbtx, block, senders) header := block.Header() - rules := chainConfig.Rules(block.NumberU64()) + rules := chainConfig.Rules(block.NumberU64(), header.Time) found := false for idx, tx := range block.Transactions() { ibs.Prepare(tx.Hash(), block.Hash(), idx) @@ -87,7 +87,7 @@ func (api *OtterscanAPIImpl) traceBlock(dbtx kv.Tx, ctx context.Context, blockNu if _, err := core.ApplyMessage(vmenv, msg, new(core.GasPool).AddGas(tx.GetGas()), true /* refunds */, false /* gasBailout */); err != nil { return false, nil, err } - _ = ibs.FinalizeTx(vmenv.ChainConfig().Rules(block.NumberU64()), cachedWriter) + _ = ibs.FinalizeTx(rules, cachedWriter) if tracer.Found { rpcTx := newRPCTransaction(tx, block.Hash(), blockNum, uint64(idx), block.BaseFee()) diff --git a/cmd/rpcdaemon/commands/trace_adhoc.go b/cmd/rpcdaemon/commands/trace_adhoc.go index 42d9c177772..152b4848122 100644 --- a/cmd/rpcdaemon/commands/trace_adhoc.go +++ b/cmd/rpcdaemon/commands/trace_adhoc.go @@ -734,7 +734,7 @@ func (api *TraceAPIImpl) ReplayTransaction(ctx context.Context, txHash common.Ha } // Returns an array of trace arrays, one trace array for each transaction - traces, err := api.callManyTransactions(ctx, tx, block.Transactions(), traceTypes, block.ParentHash(), rpc.BlockNumber(parentNr), block.Header(), int(txnIndex), types.MakeSigner(chainConfig, blockNum), chainConfig.Rules(blockNum)) + traces, err := api.callManyTransactions(ctx, tx, block.Transactions(), traceTypes, block.ParentHash(), rpc.BlockNumber(parentNr), block.Header(), int(txnIndex), types.MakeSigner(chainConfig, blockNum), chainConfig.Rules(blockNum, block.Time())) if err != nil { return nil, err } @@ -817,7 +817,7 @@ func (api *TraceAPIImpl) ReplayBlockTransactions(ctx context.Context, blockNrOrH } // Returns an array of trace arrays, one trace array for each transaction - traces, err := api.callManyTransactions(ctx, tx, block.Transactions(), traceTypes, block.ParentHash(), rpc.BlockNumber(parentNr), block.Header(), -1 /* all tx indices */, types.MakeSigner(chainConfig, blockNumber), chainConfig.Rules(blockNumber)) + traces, err := api.callManyTransactions(ctx, tx, block.Transactions(), traceTypes, block.ParentHash(), rpc.BlockNumber(parentNr), block.Header(), -1 /* all tx indices */, types.MakeSigner(chainConfig, blockNumber), chainConfig.Rules(blockNumber, block.Time())) if err != nil { return nil, err } diff --git a/cmd/rpcdaemon/commands/trace_filtering.go b/cmd/rpcdaemon/commands/trace_filtering.go index d1dbf68a61b..03cb4827a80 100644 --- a/cmd/rpcdaemon/commands/trace_filtering.go +++ b/cmd/rpcdaemon/commands/trace_filtering.go @@ -77,7 +77,7 @@ func (api *TraceAPIImpl) Transaction(ctx context.Context, txHash common.Hash) (P hash := block.Hash() // Returns an array of trace arrays, one trace array for each transaction - traces, err := api.callManyTransactions(ctx, tx, block.Transactions(), []string{TraceTypeTrace}, block.ParentHash(), rpc.BlockNumber(parentNr), block.Header(), txIndex, types.MakeSigner(chainConfig, blockNumber), chainConfig.Rules(blockNumber)) + traces, err := api.callManyTransactions(ctx, tx, block.Transactions(), []string{TraceTypeTrace}, block.ParentHash(), rpc.BlockNumber(parentNr), block.Header(), txIndex, types.MakeSigner(chainConfig, blockNumber), chainConfig.Rules(blockNumber, block.Time())) if err != nil { return nil, err } @@ -158,7 +158,7 @@ func (api *TraceAPIImpl) Block(ctx context.Context, blockNr rpc.BlockNumber) (Pa if err != nil { return nil, err } - traces, err := api.callManyTransactions(ctx, tx, block.Transactions(), []string{TraceTypeTrace}, block.ParentHash(), rpc.BlockNumber(parentNr), block.Header(), -1 /* all tx indices */, types.MakeSigner(chainConfig, blockNum), chainConfig.Rules(blockNum)) + traces, err := api.callManyTransactions(ctx, tx, block.Transactions(), []string{TraceTypeTrace}, block.ParentHash(), rpc.BlockNumber(parentNr), block.Header(), -1 /* all tx indices */, types.MakeSigner(chainConfig, blockNum), chainConfig.Rules(blockNum, block.Time())) if err != nil { return nil, err } @@ -378,7 +378,7 @@ func (api *TraceAPIImpl) Filter(ctx context.Context, req TraceFilterRequest, str isPos = header.Difficulty.Cmp(common.Big0) == 0 || header.Difficulty.Cmp(api._chainConfig.TerminalTotalDifficulty) >= 0 } txs := block.Transactions() - t, tErr := api.callManyTransactions(ctx, dbtx, txs, []string{TraceTypeTrace}, block.ParentHash(), rpc.BlockNumber(block.NumberU64()-1), block.Header(), -1 /* all tx indices */, types.MakeSigner(chainConfig, b), chainConfig.Rules(b)) + t, tErr := api.callManyTransactions(ctx, dbtx, txs, []string{TraceTypeTrace}, block.ParentHash(), rpc.BlockNumber(block.NumberU64()-1), block.Header(), -1 /* all tx indices */, types.MakeSigner(chainConfig, b), chainConfig.Rules(b, block.Time())) if tErr != nil { if first { first = false @@ -633,7 +633,7 @@ func (api *TraceAPIImpl) filterV3(ctx context.Context, dbtx kv.Tx, fromBlock, to lastBlockNum = blockNum lastBlockHash = lastHeader.Hash() lastSigner = types.MakeSigner(chainConfig, blockNum) - lastRules = chainConfig.Rules(blockNum) + lastRules = chainConfig.Rules(blockNum, lastHeader.Time) } maxTxNum, err := rawdb.TxNums.Max(dbtx, blockNum) if err != nil { diff --git a/cmd/rpcdaemon/commands/tracing.go b/cmd/rpcdaemon/commands/tracing.go index 57fc09d492d..6d1d566e567 100644 --- a/cmd/rpcdaemon/commands/tracing.go +++ b/cmd/rpcdaemon/commands/tracing.go @@ -84,7 +84,7 @@ func (api *PrivateDebugAPIImpl) traceBlock(ctx context.Context, blockNrOrHash rp } signer := types.MakeSigner(chainConfig, block.NumberU64()) - rules := chainConfig.Rules(block.NumberU64()) + rules := chainConfig.Rules(block.NumberU64(), block.Time()) stream.WriteArrayStart() for idx, txn := range block.Transactions() { select { @@ -332,10 +332,6 @@ func (api *PrivateDebugAPIImpl) TraceCallMany(ctx context.Context, bundles []Bun return fmt.Errorf("block %d(%x) not found", blockNum, hash) } - // Get a new instance of the EVM - signer := types.MakeSigner(chainConfig, blockNum) - rules := chainConfig.Rules(blockNum) - getHash := func(i uint64) common.Hash { if hash, ok := overrideBlockHash[i]; ok { return hash @@ -363,7 +359,10 @@ func (api *PrivateDebugAPIImpl) TraceCallMany(ctx context.Context, bundles []Bun BaseFee: &baseFee, } + // Get a new instance of the EVM evm = vm.NewEVM(blockCtx, txCtx, st, chainConfig, vm.Config{Debug: false}) + signer := types.MakeSigner(chainConfig, blockNum) + rules := chainConfig.Rules(blockNum, blockCtx.Time) // Setup the gas pool (also for unmetered requests) // and apply the message. diff --git a/cmd/state/commands/erigon2.go b/cmd/state/commands/erigon2.go index 359dded7802..70cf93a0f22 100644 --- a/cmd/state/commands/erigon2.go +++ b/cmd/state/commands/erigon2.go @@ -405,7 +405,7 @@ func processBlock(trace bool, txNumStart uint64, rw *ReaderWrapper, ww *WriterWr usedGas := new(uint64) var receipts types.Receipts daoBlock := chainConfig.DAOForkSupport && chainConfig.DAOForkBlock != nil && chainConfig.DAOForkBlock.Cmp(block.Number()) == 0 - rules := chainConfig.Rules(block.NumberU64()) + rules := chainConfig.Rules(block.NumberU64(), block.Time()) txNum := txNumStart for i, tx := range block.Transactions() { diff --git a/cmd/state/commands/erigon4.go b/cmd/state/commands/erigon4.go index e8cf1d7afc5..fb9c76e6417 100644 --- a/cmd/state/commands/erigon4.go +++ b/cmd/state/commands/erigon4.go @@ -336,7 +336,7 @@ func processBlock23(startTxNum uint64, trace bool, txNumStart uint64, rw *Reader gp := new(core.GasPool).AddGas(block.GasLimit()) usedGas := new(uint64) var receipts types.Receipts - rules := chainConfig.Rules(block.NumberU64()) + rules := chainConfig.Rules(block.NumberU64(), block.Time()) txNum := txNumStart ww.w.SetTxNum(txNum) ww.w.SetBlockNum(block.NumberU64()) diff --git a/cmd/state/commands/history22.go b/cmd/state/commands/history22.go index d1a9b65b297..25ec444f188 100644 --- a/cmd/state/commands/history22.go +++ b/cmd/state/commands/history22.go @@ -17,6 +17,9 @@ import ( "github.com/ledgerwatch/erigon-lib/kv/mdbx" kv2 "github.com/ledgerwatch/erigon-lib/kv/mdbx" libstate "github.com/ledgerwatch/erigon-lib/state" + "github.com/ledgerwatch/log/v3" + "github.com/spf13/cobra" + "github.com/ledgerwatch/erigon/common" "github.com/ledgerwatch/erigon/consensus/ethash" "github.com/ledgerwatch/erigon/consensus/misc" @@ -29,8 +32,6 @@ import ( "github.com/ledgerwatch/erigon/params" "github.com/ledgerwatch/erigon/turbo/services" "github.com/ledgerwatch/erigon/turbo/snapshotsync" - "github.com/ledgerwatch/log/v3" - "github.com/spf13/cobra" ) func init() { @@ -227,7 +228,7 @@ func runHistory22(trace bool, blockNum, txNumStart uint64, hw *state.HistoryRead gp := new(core.GasPool).AddGas(block.GasLimit()) usedGas := new(uint64) var receipts types.Receipts - rules := chainConfig.Rules(block.NumberU64()) + rules := chainConfig.Rules(block.NumberU64(), block.Time()) txNum := txNumStart hw.SetTxNum(txNum) daoFork := chainConfig.DAOForkSupport && chainConfig.DAOForkBlock != nil && chainConfig.DAOForkBlock.Cmp(block.Number()) == 0 diff --git a/cmd/state/commands/opcode_tracer.go b/cmd/state/commands/opcode_tracer.go index ea24432c748..d11dc174af0 100644 --- a/cmd/state/commands/opcode_tracer.go +++ b/cmd/state/commands/opcode_tracer.go @@ -679,7 +679,7 @@ func runBlock(engine consensus.Engine, ibs *state.IntraBlockState, txnWriter sta misc.ApplyDAOHardFork(ibs) } systemcontracts.UpgradeBuildInSystemContract(chainConfig, header.Number, ibs) - rules := chainConfig.Rules(block.NumberU64()) + rules := chainConfig.Rules(block.NumberU64(), block.Time()) for i, tx := range block.Transactions() { ibs.Prepare(tx.Hash(), block.Hash(), i) receipt, _, err := core.ApplyTransaction(chainConfig, core.GetHashFn(header, getHeader), engine, nil, gp, ibs, txnWriter, header, tx, usedGas, vmConfig) diff --git a/cmd/state/commands/replay_tx.go b/cmd/state/commands/replay_tx.go index 45ecc82047e..67d6b04edd4 100644 --- a/cmd/state/commands/replay_tx.go +++ b/cmd/state/commands/replay_tx.go @@ -147,7 +147,7 @@ func replayTxNum(ctx context.Context, allSnapshots *snapshotsync.RoSnapshots, bl stateReader.SetTxNum(txNum) stateWriter.SetTxNum(txNum) noop := state.NewNoopWriter() - rules := chainConfig.Rules(bn) + rules := chainConfig.Rules(bn, header.Time) for { stateReader.ResetError() ibs := state.New(stateReader) diff --git a/consensus/misc/eip1559_test.go b/consensus/misc/eip1559_test.go index 1d64ff833cf..649943be565 100644 --- a/consensus/misc/eip1559_test.go +++ b/consensus/misc/eip1559_test.go @@ -50,8 +50,8 @@ func copyConfig(original *params.ChainConfig) *params.ChainConfig { TerminalTotalDifficulty: original.TerminalTotalDifficulty, TerminalTotalDifficultyPassed: original.TerminalTotalDifficultyPassed, MergeNetsplitBlock: original.MergeNetsplitBlock, - ShanghaiBlock: original.ShanghaiBlock, - CancunBlock: original.CancunBlock, + ShanghaiTime: original.ShanghaiTime, + CancunTime: original.CancunTime, RamanujanBlock: original.RamanujanBlock, NielsBlock: original.NielsBlock, MirrorSyncBlock: original.MirrorSyncBlock, diff --git a/consensus/parlia/parlia.go b/consensus/parlia/parlia.go index 97b0ce992bb..8f40f4e3e46 100644 --- a/consensus/parlia/parlia.go +++ b/consensus/parlia/parlia.go @@ -1233,7 +1233,7 @@ func (p *Parlia) applyTransaction(from common.Address, to common.Address, value receipt := types.NewReceipt(false, *usedGas) receipt.TxHash = expectedTx.Hash() receipt.GasUsed = gasUsed - if err := ibs.FinalizeTx(p.chainConfig.Rules(header.Number.Uint64()), state.NewNoopWriter()); err != nil { + if err := ibs.FinalizeTx(p.chainConfig.Rules(header.Number.Uint64(), header.Time), state.NewNoopWriter()); err != nil { return nil, nil, nil, err } // Set the receipt logs and create a bloom for filtering diff --git a/consensus/serenity/serenity.go b/consensus/serenity/serenity.go index 1e0becbab5e..4d679606205 100644 --- a/consensus/serenity/serenity.go +++ b/consensus/serenity/serenity.go @@ -204,7 +204,7 @@ func (s *Serenity) verifyHeader(chain consensus.ChainHeaderReader, header, paren } // Verify existence / non-existence of withdrawalsHash - shanghai := chain.Config().IsShanghai(header.Number.Uint64()) + shanghai := chain.Config().IsShanghai(header.Time) if shanghai && header.WithdrawalsHash == nil { return fmt.Errorf("missing withdrawalsHash") } diff --git a/core/blockchain.go b/core/blockchain.go index 6abc9e0674f..653b413c512 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -197,7 +197,7 @@ func ExecuteBlockEphemerallyForBSC( } } - if err := ibs.CommitBlock(chainConfig.Rules(header.Number.Uint64()), stateWriter); err != nil { + if err := ibs.CommitBlock(chainConfig.Rules(header.Number.Uint64(), header.Time), stateWriter); err != nil { return nil, fmt.Errorf("committing block %d failed: %w", header.Number.Uint64(), err) } else if err := stateWriter.WriteChangeSets(); err != nil { return nil, fmt.Errorf("writing changesets for block %d failed: %w", header.Number.Uint64(), err) @@ -585,7 +585,7 @@ func FinalizeBlockExecution(engine consensus.Engine, stateReader state.StateRead return nil, nil, nil, err } - if err := ibs.CommitBlock(cc.Rules(header.Number.Uint64()), stateWriter); err != nil { + if err := ibs.CommitBlock(cc.Rules(header.Number.Uint64(), header.Time), stateWriter); err != nil { return nil, nil, nil, fmt.Errorf("committing block %d failed: %w", header.Number.Uint64(), err) } @@ -600,6 +600,6 @@ func InitializeBlockExecution(engine consensus.Engine, chain consensus.ChainHead return SysCallContract(contract, data, *cc, ibs, header, engine, false /* constCall */) }) noop := state.NewNoopWriter() - ibs.FinalizeTx(cc.Rules(header.Number.Uint64()), noop) + ibs.FinalizeTx(cc.Rules(header.Number.Uint64(), header.Time), noop) return nil } diff --git a/core/chain_makers.go b/core/chain_makers.go index f4ecfd9d671..02cec5a718f 100644 --- a/core/chain_makers.go +++ b/core/chain_makers.go @@ -333,7 +333,7 @@ func GenerateChain(config *params.ChainConfig, parent *types.Block, engine conse return nil, nil, fmt.Errorf("call to FinaliseAndAssemble: %w", err) } // Write state changes to db - if err := ibs.CommitBlock(config.Rules(b.header.Number.Uint64()), plainStateWriter); err != nil { + if err := ibs.CommitBlock(config.Rules(b.header.Number.Uint64(), b.header.Time), plainStateWriter); err != nil { return nil, nil, fmt.Errorf("call to CommitBlock to plainStateWriter: %w", err) } diff --git a/core/vm/evm.go b/core/vm/evm.go index bd76d2a2962..f998d088394 100644 --- a/core/vm/evm.go +++ b/core/vm/evm.go @@ -107,7 +107,7 @@ func NewEVM(blockCtx evmtypes.BlockContext, txCtx evmtypes.TxContext, state evmt intraBlockState: state, config: vmConfig, chainConfig: chainConfig, - chainRules: chainConfig.Rules(blockCtx.BlockNumber), + chainRules: chainConfig.Rules(blockCtx.BlockNumber, blockCtx.Time), } evm.interpreter = NewEVMInterpreter(evm, vmConfig) diff --git a/core/vm/gas_table_test.go b/core/vm/gas_table_test.go index 7af98526240..968e9ff90bb 100644 --- a/core/vm/gas_table_test.go +++ b/core/vm/gas_table_test.go @@ -95,7 +95,7 @@ func TestEIP2200(t *testing.T) { s.SetCode(address, hexutil.MustDecode(tt.input)) s.SetState(address, &common.Hash{}, *uint256.NewInt(uint64(tt.original))) - _ = s.CommitBlock(params.AllProtocolChanges.Rules(0), state.NewPlainStateWriter(tx, tx, 0)) + _ = s.CommitBlock(params.AllProtocolChanges.Rules(0, 0), state.NewPlainStateWriter(tx, tx, 0)) vmctx := evmtypes.BlockContext{ CanTransfer: func(evmtypes.IntraBlockState, common.Address, *uint256.Int) bool { return true }, Transfer: func(evmtypes.IntraBlockState, common.Address, common.Address, *uint256.Int, bool) {}, @@ -139,7 +139,7 @@ func TestCreateGas(t *testing.T) { s := state.New(state.NewPlainStateReader(tx)) s.CreateAccount(address, true) s.SetCode(address, hexutil.MustDecode(tt.code)) - _ = s.CommitBlock(params.TestChainConfig.Rules(0), state.NewPlainStateWriter(tx, tx, 0)) + _ = s.CommitBlock(params.TestChainConfig.Rules(0, 0), state.NewPlainStateWriter(tx, tx, 0)) vmctx := evmtypes.BlockContext{ CanTransfer: func(evmtypes.IntraBlockState, common.Address, *uint256.Int) bool { return true }, diff --git a/core/vm/runtime/runtime.go b/core/vm/runtime/runtime.go index 551101e3263..07bf1b41092 100644 --- a/core/vm/runtime/runtime.go +++ b/core/vm/runtime/runtime.go @@ -76,8 +76,8 @@ func setDefaults(cfg *Config) { LondonBlock: new(big.Int), ArrowGlacierBlock: new(big.Int), GrayGlacierBlock: new(big.Int), - ShanghaiBlock: new(big.Int), - CancunBlock: new(big.Int), + ShanghaiTime: new(big.Int), + CancunTime: new(big.Int), } } @@ -130,7 +130,7 @@ func Execute(code, input []byte, cfg *Config, blockNr uint64) ([]byte, *state.In vmenv = NewEnv(cfg) sender = vm.AccountRef(cfg.Origin) ) - if rules := cfg.ChainConfig.Rules(vmenv.Context().BlockNumber); rules.IsBerlin { + if rules := cfg.ChainConfig.Rules(vmenv.Context().BlockNumber, vmenv.Context().Time); rules.IsBerlin { cfg.State.PrepareAccessList(cfg.Origin, &address, vm.ActivePrecompiles(rules), nil) } cfg.State.CreateAccount(address, true) @@ -168,7 +168,7 @@ func Create(input []byte, cfg *Config, blockNr uint64) ([]byte, common.Address, vmenv = NewEnv(cfg) sender = vm.AccountRef(cfg.Origin) ) - if rules := cfg.ChainConfig.Rules(vmenv.Context().BlockNumber); rules.IsBerlin { + if rules := cfg.ChainConfig.Rules(vmenv.Context().BlockNumber, vmenv.Context().Time); rules.IsBerlin { cfg.State.PrepareAccessList(cfg.Origin, nil, vm.ActivePrecompiles(rules), nil) } @@ -194,7 +194,7 @@ func Call(address common.Address, input []byte, cfg *Config) ([]byte, uint64, er sender := cfg.State.GetOrNewStateObject(cfg.Origin) statedb := cfg.State - if rules := cfg.ChainConfig.Rules(vmenv.Context().BlockNumber); rules.IsBerlin { + if rules := cfg.ChainConfig.Rules(vmenv.Context().BlockNumber, vmenv.Context().Time); rules.IsBerlin { statedb.PrepareAccessList(cfg.Origin, &address, vm.ActivePrecompiles(rules), nil) } diff --git a/eth/stagedsync/exec3.go b/eth/stagedsync/exec3.go index e232ece2fab..a8557bf34da 100644 --- a/eth/stagedsync/exec3.go +++ b/eth/stagedsync/exec3.go @@ -447,7 +447,6 @@ Loop: t := time.Now() inputBlockNum.Store(blockNum) - rules := chainConfig.Rules(blockNum) b, err = blockWithSenders(chainDb, applyTx, blockReader, blockNum) if err != nil { return err @@ -485,6 +484,7 @@ Loop: } }() } + rules := chainConfig.Rules(blockNum, b.Time()) var gasUsed uint64 for txIndex := -1; txIndex <= len(txs); txIndex++ { // Do not oversend, wait for the result heap to go under certain size @@ -1044,7 +1044,6 @@ func ReconstituteState(ctx context.Context, s *StageState, dirs datadir.Dirs, wo } for bn = uint64(0); bn <= blockNum; bn++ { t = time.Now() - rules := chainConfig.Rules(bn) b, err = blockWithSenders(chainDb, nil, blockReader, bn) if err != nil { return err @@ -1062,6 +1061,7 @@ func ReconstituteState(ctx context.Context, s *StageState, dirs datadir.Dirs, wo return f(n) } blockContext := core.NewEVMBlockContext(header, getHashFn, engine, nil /* author */) + rules := chainConfig.Rules(blockNum, b.Time()) for txIndex := -1; txIndex <= len(txs); txIndex++ { if bitmap.Contains(inputTxNum) { diff --git a/params/config.go b/params/config.go index b7b8af0f289..b09cb22a3b1 100644 --- a/params/config.go +++ b/params/config.go @@ -134,7 +134,7 @@ var ( GrayGlacierBlock: big.NewInt(0), TerminalTotalDifficulty: big.NewInt(0), TerminalTotalDifficultyPassed: true, - ShanghaiBlock: big.NewInt(0), + ShanghaiTime: big.NewInt(0), Ethash: new(EthashConfig), } @@ -201,7 +201,7 @@ var ( Aura: &AuRaConfig{}, } - TestRules = TestChainConfig.Rules(0) + TestRules = TestChainConfig.Rules(0, 0) ) // ChainConfig is the core config which determines the blockchain settings. @@ -241,8 +241,8 @@ type ChainConfig struct { TerminalTotalDifficultyPassed bool `json:"terminalTotalDifficultyPassed,omitempty"` // Disable PoW sync for networks that have already passed through the Merge MergeNetsplitBlock *big.Int `json:"mergeNetsplitBlock,omitempty"` // Virtual fork after The Merge to use as a network splitter; see FORK_NEXT_VALUE in EIP-3675 - ShanghaiBlock *big.Int `json:"shanghaiBlock,omitempty"` // Shanghai switch block (nil = no fork, 0 = already activated) - CancunBlock *big.Int `json:"cancunBlock,omitempty"` // Cancun switch block (nil = no fork, 0 = already activated) + ShanghaiTime *big.Int `json:"shanghaiTime,omitempty"` // Shanghai switch time (nil = no fork, 0 = already activated) + CancunTime *big.Int `json:"cancunTime,omitempty"` // Cancun switch time (nil = no fork, 0 = already activated) // Parlia fork blocks RamanujanBlock *big.Int `json:"ramanujanBlock,omitempty" toml:",omitempty"` // ramanujanBlock switch block (nil = no fork, 0 = already activated) @@ -445,8 +445,8 @@ func (c *ChainConfig) String() string { c.GrayGlacierBlock, c.TerminalTotalDifficulty, c.MergeNetsplitBlock, - c.ShanghaiBlock, - c.CancunBlock, + c.ShanghaiTime, + c.CancunTime, engine, ) } @@ -621,14 +621,14 @@ func (c *ChainConfig) IsGrayGlacier(num uint64) bool { return isForked(c.GrayGlacierBlock, num) } -// IsShanghai returns whether num is either equal to the Shanghai fork block or greater. -func (c *ChainConfig) IsShanghai(num uint64) bool { - return isForked(c.ShanghaiBlock, num) +// IsShanghai returns whether time is either equal to the Shanghai fork time or greater. +func (c *ChainConfig) IsShanghai(time uint64) bool { + return isForked(c.ShanghaiTime, time) } -// IsCancun returns whether num is either equal to the Cancun fork block or greater. -func (c *ChainConfig) IsCancun(num uint64) bool { - return isForked(c.CancunBlock, num) +// IsCancun returns whether time is either equal to the Cancun fork time or greater. +func (c *ChainConfig) IsCancun(time uint64) bool { + return isForked(c.CancunTime, time) } func (c *ChainConfig) IsEip1559FeeCollector(num uint64) bool { @@ -682,8 +682,6 @@ func (c *ChainConfig) CheckConfigForkOrder() error { {name: "arrowGlacierBlock", block: c.ArrowGlacierBlock, optional: true}, {name: "grayGlacierBlock", block: c.GrayGlacierBlock, optional: true}, {name: "mergeNetsplitBlock", block: c.MergeNetsplitBlock, optional: true}, - {name: "shanghaiBlock", block: c.ShanghaiBlock}, - {name: "cancunBlock", block: c.CancunBlock}, } { if lastFork.name != "" { // Next one must be higher number @@ -760,12 +758,6 @@ func (c *ChainConfig) checkCompatible(newcfg *ChainConfig, head uint64) *ConfigC if isForkIncompatible(c.MergeNetsplitBlock, newcfg.MergeNetsplitBlock, head) { return newCompatError("Merge netsplit block", c.MergeNetsplitBlock, newcfg.MergeNetsplitBlock) } - if isForkIncompatible(c.ShanghaiBlock, newcfg.ShanghaiBlock, head) { - return newCompatError("Shanghai fork block", c.ShanghaiBlock, newcfg.ShanghaiBlock) - } - if isForkIncompatible(c.CancunBlock, newcfg.CancunBlock, head) { - return newCompatError("Cancun fork block", c.CancunBlock, newcfg.CancunBlock) - } // Parlia forks if isForkIncompatible(c.RamanujanBlock, newcfg.RamanujanBlock, head) { @@ -866,7 +858,7 @@ type Rules struct { } // Rules ensures c's ChainID is not nil. -func (c *ChainConfig) Rules(num uint64) *Rules { +func (c *ChainConfig) Rules(num uint64, time uint64) *Rules { chainID := c.ChainID if chainID == nil { chainID = new(big.Int) @@ -882,8 +874,8 @@ func (c *ChainConfig) Rules(num uint64) *Rules { IsIstanbul: c.IsIstanbul(num), IsBerlin: c.IsBerlin(num), IsLondon: c.IsLondon(num), - IsShanghai: c.IsShanghai(num), - IsCancun: c.IsCancun(num), + IsShanghai: c.IsShanghai(time), + IsCancun: c.IsCancun(time), IsNano: c.IsNano(num), IsMoran: c.IsMoran(num), IsEip1559FeeCollector: c.IsEip1559FeeCollector(num), diff --git a/tests/state_test_util.go b/tests/state_test_util.go index 8431501e999..b2a5f2a1c05 100644 --- a/tests/state_test_util.go +++ b/tests/state_test_util.go @@ -223,7 +223,7 @@ func (t *StateTest) RunNoVerify(tx kv.RwTx, subtest StateSubtest, vmconfig vm.Co if err != nil { return nil, common.Hash{}, err } - msg, err = txn.AsMessage(*types.MakeSigner(config, 0), baseFee, config.Rules(0)) + msg, err = txn.AsMessage(*types.MakeSigner(config, 0), baseFee, config.Rules(0, 0)) if err != nil { return nil, common.Hash{}, err } diff --git a/tests/transaction_test_util.go b/tests/transaction_test_util.go index 0d6398f32e8..5555ad550e2 100644 --- a/tests/transaction_test_util.go +++ b/tests/transaction_test_util.go @@ -118,7 +118,7 @@ func (tt *TransactionTest) Run(chainID *big.Int) error { {"Berlin", types.LatestSignerForChainID(chainID), tt.Forks.Berlin, Forks["Berlin"]}, {"London", types.LatestSignerForChainID(chainID), tt.Forks.London, Forks["London"]}, } { - sender, txhash, intrinsicGas, err := validateTx(tt.RLP, *testcase.signer, testcase.config.Rules(0)) + sender, txhash, intrinsicGas, err := validateTx(tt.RLP, *testcase.signer, testcase.config.Rules(0, 0)) if testcase.fork.Exception != "" { if err == nil { diff --git a/turbo/transactions/tracing.go b/turbo/transactions/tracing.go index 975f64b8aac..fe405377e36 100644 --- a/turbo/transactions/tracing.go +++ b/turbo/transactions/tracing.go @@ -50,7 +50,7 @@ func ComputeTxEnv(ctx context.Context, engine consensus.EngineReader, block *typ } txn := block.Transactions()[txIndex] signer := types.MakeSigner(cfg, block.NumberU64()) - msg, _ := txn.AsMessage(*signer, header.BaseFee, cfg.Rules(block.NumberU64())) + msg, _ := txn.AsMessage(*signer, header.BaseFee, cfg.Rules(block.NumberU64(), block.Time())) blockCtx := NewEVMBlockContext(engine, header, true /* requireCanonical */, dbtx, headerReader) txCtx := core.NewEVMTxContext(msg) return msg, blockCtx, txCtx, ibs, reader, nil