From 0b3e557324a7717b598ca877f7350d346e9c1ada Mon Sep 17 00:00:00 2001 From: redhdx <136775144+redhdx@users.noreply.github.com> Date: Tue, 8 Aug 2023 10:54:50 +0800 Subject: [PATCH] feat: add preDeployedContract hardfork (#5) * feat: add preDeployedContract hardfork to modify WBNB contract name/symbol and remove GovernanceToken * fix: remove useless IsPreContractBlock --- cmd/evm/internal/t8ntool/execution.go | 4 +++ consensus/misc/precontract.go | 42 +++++++++++++++++++++++++++ core/chain_makers.go | 3 ++ core/state_processor.go | 3 ++ miner/worker.go | 4 +++ params/config.go | 5 ++++ 6 files changed, 61 insertions(+) create mode 100644 consensus/misc/precontract.go diff --git a/cmd/evm/internal/t8ntool/execution.go b/cmd/evm/internal/t8ntool/execution.go index 5f796c1d66..b7e463bfd5 100644 --- a/cmd/evm/internal/t8ntool/execution.go +++ b/cmd/evm/internal/t8ntool/execution.go @@ -162,6 +162,10 @@ func (pre *Prestate) Apply(vmConfig vm.Config, chainConfig *params.ChainConfig, misc.ApplyDAOHardFork(statedb) } + if chainConfig.PreContractForkBlock != nil && chainConfig.PreContractForkBlock.Cmp(new(big.Int).SetUint64(pre.Env.Number)) == 0 { + misc.ApplyPreContractHardFork(statedb) + } + for i, tx := range txs { msg, err := core.TransactionToMessage(tx, signer, pre.Env.BaseFee) if err != nil { diff --git a/consensus/misc/precontract.go b/consensus/misc/precontract.go new file mode 100644 index 0000000000..3b381479e2 --- /dev/null +++ b/consensus/misc/precontract.go @@ -0,0 +1,42 @@ +// Copyright 2016 The go-ethereum Authors +// This file is part of the go-ethereum library. +// +// The go-ethereum library is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// The go-ethereum library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with the go-ethereum library. If not, see . + +package misc + +import ( + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/core/state" +) + +var ( + // WBNBContract WBNB preDeploy contract address + WBNBContract = common.HexToAddress("0x4200000000000000000000000000000000000006") + // GovernanceToken contract address + governanceToken = common.HexToAddress("4200000000000000000000000000000000000042") + nameSlot = common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000000") + symbolSlot = common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000001") + // Wrapped BNB + nameValue = common.HexToHash("0x5772617070656420424e42000000000000000000000000000000000000000016") + // WBNB + symbolValue = common.HexToHash("0x57424e4200000000000000000000000000000000000000000000000000000008") +) + +// ApplyPreContractHardFork modifies the state database according to the hard-fork rules +func ApplyPreContractHardFork(statedb *state.StateDB) { + statedb.SetState(WBNBContract, nameSlot, nameValue) + statedb.SetState(WBNBContract, symbolSlot, symbolValue) + statedb.Suicide(governanceToken) +} diff --git a/core/chain_makers.go b/core/chain_makers.go index 61d0098af3..0096a7b3cd 100644 --- a/core/chain_makers.go +++ b/core/chain_makers.go @@ -310,6 +310,9 @@ func GenerateChain(config *params.ChainConfig, parent *types.Block, engine conse if config.DAOForkSupport && config.DAOForkBlock != nil && config.DAOForkBlock.Cmp(b.header.Number) == 0 { misc.ApplyDAOHardFork(statedb) } + if config.PreContractForkBlock != nil && config.PreContractForkBlock.Cmp(b.header.Number) == 0 { + misc.ApplyPreContractHardFork(statedb) + } // Execute any user modifications to the block if gen != nil { gen(i, b) diff --git a/core/state_processor.go b/core/state_processor.go index 0310af96fb..c8dc50211a 100644 --- a/core/state_processor.go +++ b/core/state_processor.go @@ -70,6 +70,9 @@ func (p *StateProcessor) Process(block *types.Block, statedb *state.StateDB, cfg if p.config.DAOForkSupport && p.config.DAOForkBlock != nil && p.config.DAOForkBlock.Cmp(block.Number()) == 0 { misc.ApplyDAOHardFork(statedb) } + if p.config.PreContractForkBlock != nil && p.config.PreContractForkBlock.Cmp(block.Number()) == 0 { + misc.ApplyPreContractHardFork(statedb) + } blockContext := NewEVMBlockContext(header, p.bc, nil, p.config, statedb) vmenv := vm.NewEVM(blockContext, vm.TxContext{}, statedb, p.config, cfg) // Iterate over and process the individual transactions diff --git a/miner/worker.go b/miner/worker.go index 4651a5973e..6b28ea4d9b 100644 --- a/miner/worker.go +++ b/miner/worker.go @@ -1176,6 +1176,10 @@ func (w *worker) generateWork(genParams *generateParams) (*types.Block, *big.Int return nil, nil, nil, err } defer work.discard() + + if w.chainConfig.PreContractForkBlock != nil && work.header.Number.Cmp(w.chainConfig.PreContractForkBlock) == 0 { + misc.ApplyPreContractHardFork(work.state) + } if work.gasPool == nil { work.gasPool = new(core.GasPool).AddGas(work.header.GasLimit) } diff --git a/params/config.go b/params/config.go index 63ef1c46ae..7d5e5e82e2 100644 --- a/params/config.go +++ b/params/config.go @@ -476,6 +476,8 @@ type ChainConfig struct { // Optimism config, nil if not active Optimism *OptimismConfig `json:"optimism,omitempty"` + // PreContractForkBlock hard-fork switch block (nil = no fork, 0 = already on preContractForkBlock) + PreContractForkBlock *big.Int `json:"preContractForkBlock,omitempty"` } // EthashConfig is the consensus engine configs for proof-of-work based sealing. @@ -599,6 +601,9 @@ func (c *ChainConfig) Description() string { if c.RegolithTime != nil { banner += fmt.Sprintf(" - Regolith: @%-10v\n", *c.RegolithTime) } + if c.PreContractForkBlock != nil { + banner += fmt.Sprintf(" - PreContractForkBlock: @%-10v\n", *c.PreContractForkBlock) + } return banner }