From a2a7ddba9c23d2d82051eb3c0098a35b86701e1f Mon Sep 17 00:00:00 2001 From: buddh0 Date: Wed, 25 Sep 2024 14:13:07 +0800 Subject: [PATCH] BEP-440: add logic for deployment --- consensus/parlia/parlia.go | 8 +--- core/chain_makers.go | 4 +- core/state_processor.go | 6 +-- core/systemcontracts/upgrade.go | 21 ++++++++++- core/systemcontracts/upgrade_test.go | 4 +- eth/state_accessor.go | 9 ++--- eth/tracers/api.go | 56 ++++++++++------------------ miner/worker.go | 6 +-- params/config.go | 9 +++++ 9 files changed, 61 insertions(+), 62 deletions(-) diff --git a/consensus/parlia/parlia.go b/consensus/parlia/parlia.go index 79ff3f93af..7c4a12a79d 100644 --- a/consensus/parlia/parlia.go +++ b/consensus/parlia/parlia.go @@ -1275,9 +1275,7 @@ func (p *Parlia) Finalize(chain consensus.ChainHeaderReader, header *types.Heade return errors.New("parent not found") } - if p.chainConfig.IsFeynman(header.Number, header.Time) { - systemcontracts.UpgradeBuildInSystemContract(p.chainConfig, header.Number, parent.Time, header.Time, state) - } + systemcontracts.UpgradeBuildInContract(p.chainConfig, header.Number, parent.Time, header.Time, state, false) if p.chainConfig.IsOnFeynman(header.Number, parent.Time, header.Time) { err := p.initializeFeynmanContract(state, header, cx, txs, receipts, systemTxs, usedGas, false) @@ -1362,9 +1360,7 @@ func (p *Parlia) FinalizeAndAssemble(chain consensus.ChainHeaderReader, header * return nil, nil, errors.New("parent not found") } - if p.chainConfig.IsFeynman(header.Number, header.Time) { - systemcontracts.UpgradeBuildInSystemContract(p.chainConfig, header.Number, parent.Time, header.Time, state) - } + systemcontracts.UpgradeBuildInContract(p.chainConfig, header.Number, parent.Time, header.Time, state, false) if p.chainConfig.IsOnFeynman(header.Number, parent.Time, header.Time) { err := p.initializeFeynmanContract(state, header, cx, &txs, &receipts, nil, &header.GasUsed, true) diff --git a/core/chain_makers.go b/core/chain_makers.go index 29306c5222..ce135839f5 100644 --- a/core/chain_makers.go +++ b/core/chain_makers.go @@ -359,9 +359,7 @@ func GenerateChain(config *params.ChainConfig, parent *types.Block, engine conse misc.ApplyDAOHardFork(statedb) } - if !config.IsFeynman(b.header.Number, b.header.Time) { - systemcontracts.UpgradeBuildInSystemContract(config, b.header.Number, parent.Time(), b.header.Time, statedb) - } + systemcontracts.UpgradeBuildInContract(config, b.header.Number, parent.Time(), b.header.Time, statedb, true) // Execute any user modifications to the block if gen != nil { diff --git a/core/state_processor.go b/core/state_processor.go index a4e6511399..e685907075 100644 --- a/core/state_processor.go +++ b/core/state_processor.go @@ -78,10 +78,8 @@ func (p *StateProcessor) Process(block *types.Block, statedb *state.StateDB, cfg if lastBlock == nil { return statedb, nil, nil, 0, errors.New("could not get parent block") } - if !p.config.IsFeynman(block.Number(), block.Time()) { - // Handle upgrade build-in system contract code - systemcontracts.UpgradeBuildInSystemContract(p.config, blockNumber, lastBlock.Time(), block.Time(), statedb) - } + // Handle upgrade build-in system contract code + systemcontracts.UpgradeBuildInContract(p.config, blockNumber, lastBlock.Time(), block.Time(), statedb, true) var ( context = NewEVMBlockContext(header, p.bc, nil) diff --git a/core/systemcontracts/upgrade.go b/core/systemcontracts/upgrade.go index f9a776e245..bc91e63c68 100644 --- a/core/systemcontracts/upgrade.go +++ b/core/systemcontracts/upgrade.go @@ -1066,7 +1066,26 @@ func init() { } } -func UpgradeBuildInSystemContract(config *params.ChainConfig, blockNumber *big.Int, lastBlockTime uint64, blockTime uint64, statedb vm.StateDB) { +// TODO: `UpgradeBuildInContract` not suitable in a dir naming `systemcontracts`? +func UpgradeBuildInContract(config *params.ChainConfig, blockNumber *big.Int, lastBlockTime uint64, blockTime uint64, statedb vm.StateDB, atBlockBegin bool) { + if atBlockBegin { + if !config.IsFeynman(blockNumber, lastBlockTime) { + // Handle upgrade build-in system contract code + upgradeBuildInSystemContract(config, blockNumber, lastBlockTime, blockTime, statedb) + } + if config.IsOnPrague(blockNumber, lastBlockTime, blockTime) { + statedb.SetCode(params.HistoryStorageAddress, params.HistoryStorageCode) + statedb.SetNonce(params.HistoryStorageAddress, 1) + log.Info("Apply upgrade prague", "blockNumber", blockNumber.Int64(), "blockTime", blockTime) + } + } else { + if config.IsFeynman(blockNumber, lastBlockTime) { + upgradeBuildInSystemContract(config, blockNumber, lastBlockTime, blockTime, statedb) + } + } +} + +func upgradeBuildInSystemContract(config *params.ChainConfig, blockNumber *big.Int, lastBlockTime uint64, blockTime uint64, statedb vm.StateDB) { if config == nil || blockNumber == nil || statedb == nil || reflect.ValueOf(statedb).IsNil() { return } diff --git a/core/systemcontracts/upgrade_test.go b/core/systemcontracts/upgrade_test.go index 3f88d7687b..8074579729 100644 --- a/core/systemcontracts/upgrade_test.go +++ b/core/systemcontracts/upgrade_test.go @@ -55,7 +55,7 @@ func TestUpgradeBuildInSystemContractNilInterface(t *testing.T) { GenesisHash = params.BSCGenesisHash - UpgradeBuildInSystemContract(config, blockNumber, lastBlockTime, blockTime, statedb) + upgradeBuildInSystemContract(config, blockNumber, lastBlockTime, blockTime, statedb) } func TestUpgradeBuildInSystemContractNilValue(t *testing.T) { @@ -69,5 +69,5 @@ func TestUpgradeBuildInSystemContractNilValue(t *testing.T) { GenesisHash = params.BSCGenesisHash - UpgradeBuildInSystemContract(config, blockNumber, lastBlockTime, blockTime, statedb) + upgradeBuildInSystemContract(config, blockNumber, lastBlockTime, blockTime, statedb) } diff --git a/eth/state_accessor.go b/eth/state_accessor.go index e2fee88546..ccebbd78be 100644 --- a/eth/state_accessor.go +++ b/eth/state_accessor.go @@ -242,9 +242,8 @@ func (eth *Ethereum) stateAtTransaction(ctx context.Context, block *types.Block, return nil, vm.BlockContext{}, nil, nil, err } // upgrade build-in system contract before normal txs if Feynman is not enabled - if !eth.blockchain.Config().IsFeynman(block.Number(), block.Time()) { - systemcontracts.UpgradeBuildInSystemContract(eth.blockchain.Config(), block.Number(), parent.Time(), block.Time(), statedb) - } + systemcontracts.UpgradeBuildInContract(eth.blockchain.Config(), block.Number(), parent.Time(), block.Time(), statedb, true) + // If prague hardfork, insert parent block hash in the state as per EIP-2935. if eth.blockchain.Config().IsPrague(block.Number(), block.Time()) { context := core.NewEVMBlockContext(block.Header(), eth.blockchain, nil) @@ -270,9 +269,7 @@ func (eth *Ethereum) stateAtTransaction(ctx context.Context, block *types.Block, statedb.AddBalance(block.Header().Coinbase, balance) } - if eth.blockchain.Config().IsFeynman(block.Number(), block.Time()) { - systemcontracts.UpgradeBuildInSystemContract(eth.blockchain.Config(), block.Number(), parent.Time(), block.Time(), statedb) - } + systemcontracts.UpgradeBuildInContract(eth.blockchain.Config(), block.Number(), parent.Time(), block.Time(), statedb, false) beforeSystemTx = false } } diff --git a/eth/tracers/api.go b/eth/tracers/api.go index dba7c4916c..33bb080dc0 100644 --- a/eth/tracers/api.go +++ b/eth/tracers/api.go @@ -286,9 +286,7 @@ func (api *API) traceChain(start, end *types.Block, config *TraceConfig, closed task.statedb.AddBalance(blockCtx.Coinbase, balance) } - if api.backend.ChainConfig().IsFeynman(task.block.Number(), task.block.Time()) { - systemcontracts.UpgradeBuildInSystemContract(api.backend.ChainConfig(), task.block.Number(), task.parent.Time(), task.block.Time(), task.statedb) - } + systemcontracts.UpgradeBuildInContract(api.backend.ChainConfig(), task.block.Number(), task.parent.Time(), task.block.Time(), task.statedb, false) beforeSystemTx = false } } @@ -404,9 +402,8 @@ func (api *API) traceChain(start, end *types.Block, config *TraceConfig, closed } // upgrade build-in system contract before normal txs if Feynman is not enabled - if !api.backend.ChainConfig().IsFeynman(next.Number(), next.Time()) { - systemcontracts.UpgradeBuildInSystemContract(api.backend.ChainConfig(), next.Number(), block.Time(), next.Time(), statedb) - } + systemcontracts.UpgradeBuildInContract(api.backend.ChainConfig(), next.Number(), block.Time(), next.Time(), statedb, true) + // Insert parent hash in history contract. if api.backend.ChainConfig().IsPrague(next.Number(), next.Time()) { context := core.NewEVMBlockContext(next.Header(), api.chainContext(ctx), nil) @@ -556,9 +553,7 @@ func (api *API) IntermediateRoots(ctx context.Context, hash common.Hash, config defer release() // upgrade build-in system contract before normal txs if Feynman is not enabled - if !api.backend.ChainConfig().IsFeynman(block.Number(), block.Time()) { - systemcontracts.UpgradeBuildInSystemContract(api.backend.ChainConfig(), block.Number(), parent.Time(), block.Time(), statedb) - } + systemcontracts.UpgradeBuildInContract(api.backend.ChainConfig(), block.Number(), parent.Time(), block.Time(), statedb, true) var ( roots []common.Hash @@ -581,17 +576,18 @@ func (api *API) IntermediateRoots(ctx context.Context, hash common.Hash, config vmenv = vm.NewEVM(vmctx, txContext, statedb, chainConfig, vm.Config{}) ) - if posa, ok := api.backend.Engine().(consensus.PoSA); ok { - if isSystem, _ := posa.IsSystemTransaction(tx, block.Header()); isSystem { - balance := statedb.GetBalance(consensus.SystemAddress) - if balance.Cmp(common.U2560) > 0 { - statedb.SetBalance(consensus.SystemAddress, uint256.NewInt(0)) - statedb.AddBalance(vmctx.Coinbase, balance) - } + if beforeSystemTx { + if posa, ok := api.backend.Engine().(consensus.PoSA); ok { + if isSystem, _ := posa.IsSystemTransaction(tx, block.Header()); isSystem { + balance := statedb.GetBalance(consensus.SystemAddress) + if balance.Cmp(common.U2560) > 0 { + statedb.SetBalance(consensus.SystemAddress, uint256.NewInt(0)) + statedb.AddBalance(vmctx.Coinbase, balance) + } - if beforeSystemTx && api.backend.ChainConfig().IsFeynman(block.Number(), block.Time()) { - systemcontracts.UpgradeBuildInSystemContract(api.backend.ChainConfig(), block.Number(), parent.Time(), block.Time(), statedb) + systemcontracts.UpgradeBuildInContract(api.backend.ChainConfig(), block.Number(), parent.Time(), block.Time(), statedb, false) beforeSystemTx = false + } } } @@ -650,9 +646,7 @@ func (api *API) traceBlock(ctx context.Context, block *types.Block, config *Trac defer release() // upgrade build-in system contract before normal txs if Feynman is not enabled - if !api.backend.ChainConfig().IsFeynman(block.Number(), block.Time()) { - systemcontracts.UpgradeBuildInSystemContract(api.backend.ChainConfig(), block.Number(), parent.Time(), block.Time(), statedb) - } + systemcontracts.UpgradeBuildInContract(api.backend.ChainConfig(), block.Number(), parent.Time(), block.Time(), statedb, true) // JS tracers have high overhead. In this case run a parallel // process that generates states in one thread and traces txes @@ -688,9 +682,7 @@ func (api *API) traceBlock(ctx context.Context, block *types.Block, config *Trac statedb.AddBalance(blockCtx.Coinbase, balance) } - if api.backend.ChainConfig().IsFeynman(block.Number(), block.Time()) { - systemcontracts.UpgradeBuildInSystemContract(api.backend.ChainConfig(), block.Number(), parent.Time(), block.Time(), statedb) - } + systemcontracts.UpgradeBuildInContract(api.backend.ChainConfig(), block.Number(), parent.Time(), block.Time(), statedb, false) beforeSystemTx = false } } @@ -780,9 +772,7 @@ txloop: statedb.AddBalance(block.Header().Coinbase, balance) } - if api.backend.ChainConfig().IsFeynman(block.Number(), block.Time()) { - systemcontracts.UpgradeBuildInSystemContract(api.backend.ChainConfig(), block.Number(), parent.Time(), block.Time(), statedb) - } + systemcontracts.UpgradeBuildInContract(api.backend.ChainConfig(), block.Number(), parent.Time(), block.Time(), statedb, false) beforeSystemTx = false } } @@ -848,9 +838,7 @@ func (api *API) standardTraceBlockToFile(ctx context.Context, block *types.Block defer release() // upgrade build-in system contract before normal txs if Feynman is not enabled - if !api.backend.ChainConfig().IsFeynman(block.Number(), block.Time()) { - systemcontracts.UpgradeBuildInSystemContract(api.backend.ChainConfig(), block.Number(), parent.Time(), block.Time(), statedb) - } + systemcontracts.UpgradeBuildInContract(api.backend.ChainConfig(), block.Number(), parent.Time(), block.Time(), statedb, true) // Retrieve the tracing configurations, or use default values var ( @@ -897,9 +885,7 @@ func (api *API) standardTraceBlockToFile(ctx context.Context, block *types.Block statedb.AddBalance(vmctx.Coinbase, balance) } - if api.backend.ChainConfig().IsFeynman(block.Number(), block.Time()) { - systemcontracts.UpgradeBuildInSystemContract(api.backend.ChainConfig(), block.Number(), parent.Time(), block.Time(), statedb) - } + systemcontracts.UpgradeBuildInContract(api.backend.ChainConfig(), block.Number(), parent.Time(), block.Time(), statedb, false) beforeSystemTx = false } } @@ -1071,9 +1057,7 @@ func (api *API) TraceCall(ctx context.Context, args ethapi.TransactionArgs, bloc if err != nil { return nil, err } - if !api.backend.ChainConfig().IsFeynman(block.Number(), block.Time()) { - systemcontracts.UpgradeBuildInSystemContract(api.backend.ChainConfig(), block.Number(), parent.Time(), block.Time(), statedb) - } + systemcontracts.UpgradeBuildInContract(api.backend.ChainConfig(), block.Number(), parent.Time(), block.Time(), statedb, true) } vmctx := core.NewEVMBlockContext(block.Header(), api.chainContext(ctx), nil) diff --git a/miner/worker.go b/miner/worker.go index 4ec1145e24..8dce99f444 100644 --- a/miner/worker.go +++ b/miner/worker.go @@ -1061,10 +1061,8 @@ func (w *worker) prepareWork(genParams *generateParams) (*environment, error) { return nil, err } - if !w.chainConfig.IsFeynman(header.Number, header.Time) { - // Handle upgrade build-in system contract code - systemcontracts.UpgradeBuildInSystemContract(w.chainConfig, header.Number, parent.Time, header.Time, env.state) - } + // Handle upgrade build-in system contract code + systemcontracts.UpgradeBuildInContract(w.chainConfig, header.Number, parent.Time, header.Time, env.state, true) if header.ParentBeaconRoot != nil { context := core.NewEVMBlockContext(header, w.chain, nil) diff --git a/params/config.go b/params/config.go index 1c07b294cf..550ef60933 100644 --- a/params/config.go +++ b/params/config.go @@ -1018,6 +1018,15 @@ func (c *ChainConfig) IsPrague(num *big.Int, time uint64) bool { return c.IsLondon(num) && isTimestampForked(c.PragueTime, time) } +// IsOnPrague returns whether currentBlockTime is either equal to the Prague fork time or greater firstly. +func (c *ChainConfig) IsOnPrague(currentBlockNumber *big.Int, lastBlockTime uint64, currentBlockTime uint64) bool { + lastBlockNumber := new(big.Int) + if currentBlockNumber.Cmp(big.NewInt(1)) >= 0 { + lastBlockNumber.Sub(currentBlockNumber, big.NewInt(1)) + } + return !c.IsPrague(lastBlockNumber, lastBlockTime) && c.IsPrague(currentBlockNumber, currentBlockTime) +} + // IsVerkle returns whether num is either equal to the Verkle fork time or greater. func (c *ChainConfig) IsVerkle(num *big.Int, time uint64) bool { return c.IsLondon(num) && isTimestampForked(c.VerkleTime, time)