From ee381b2451fb5b7c20fc3fc7888dfe42ff8092a5 Mon Sep 17 00:00:00 2001 From: Nazarii Denha Date: Fri, 9 Feb 2024 13:46:33 +0100 Subject: [PATCH] feat: setup configuration for upcoming hard fork and re-enable sha256 precompile (#562) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * setup configuration for upcoming hard fork * bump version * bump version * enable sha256 * rename to kepler * update * update lint * rename Kepler to Banach * goimports * reorder precompile forks * typo --------- Co-authored-by: Péter Garamvölgyi --- core/vm/contracts.go | 27 ++++++++++++++++++++++++--- core/vm/evm.go | 2 ++ core/vm/runtime/runtime.go | 1 + params/config.go | 26 +++++++++++++++++++++----- params/version.go | 2 +- tests/init.go | 17 +++++++++++++++++ 6 files changed, 66 insertions(+), 9 deletions(-) diff --git a/core/vm/contracts.go b/core/vm/contracts.go index 278746e02c..ac9624ee12 100644 --- a/core/vm/contracts.go +++ b/core/vm/contracts.go @@ -111,6 +111,20 @@ var PrecompiledContractsArchimedes = map[common.Address]PrecompiledContract{ common.BytesToAddress([]byte{9}): &blake2FDisabled{}, } +// PrecompiledContractsBanach contains the default set of pre-compiled Ethereum +// contracts used in the Banach release. Same as Archimedes but with sha256hash enabled again +var PrecompiledContractsBanach = map[common.Address]PrecompiledContract{ + common.BytesToAddress([]byte{1}): &ecrecover{}, + common.BytesToAddress([]byte{2}): &sha256hash{}, + common.BytesToAddress([]byte{3}): &ripemd160hashDisabled{}, + common.BytesToAddress([]byte{4}): &dataCopy{}, + common.BytesToAddress([]byte{5}): &bigModExp{eip2565: true}, + common.BytesToAddress([]byte{6}): &bn256AddIstanbul{}, + common.BytesToAddress([]byte{7}): &bn256ScalarMulIstanbul{}, + common.BytesToAddress([]byte{8}): &bn256PairingIstanbul{}, + common.BytesToAddress([]byte{9}): &blake2FDisabled{}, +} + // PrecompiledContractsBLS contains the set of pre-compiled Ethereum // contracts specified in EIP-2537. These are exported for testing purposes. var PrecompiledContractsBLS = map[common.Address]PrecompiledContract{ @@ -126,6 +140,7 @@ var PrecompiledContractsBLS = map[common.Address]PrecompiledContract{ } var ( + PrecompiledAddressesBanach []common.Address PrecompiledAddressesArchimedes []common.Address PrecompiledAddressesBerlin []common.Address PrecompiledAddressesIstanbul []common.Address @@ -149,11 +164,16 @@ func init() { for k := range PrecompiledContractsArchimedes { PrecompiledAddressesArchimedes = append(PrecompiledAddressesArchimedes, k) } + for k := range PrecompiledContractsBanach { + PrecompiledAddressesBanach = append(PrecompiledAddressesBanach, k) + } } // ActivePrecompiles returns the precompiles enabled with the current configuration. func ActivePrecompiles(rules params.Rules) []common.Address { switch { + case rules.IsBanach: + return PrecompiledAddressesBanach case rules.IsArchimedes: return PrecompiledAddressesArchimedes case rules.IsBerlin: @@ -309,9 +329,10 @@ var ( // modexpMultComplexity implements bigModexp multComplexity formula, as defined in EIP-198 // // def mult_complexity(x): -// if x <= 64: return x ** 2 -// elif x <= 1024: return x ** 2 // 4 + 96 * x - 3072 -// else: return x ** 2 // 16 + 480 * x - 199680 +// +// if x <= 64: return x ** 2 +// elif x <= 1024: return x ** 2 // 4 + 96 * x - 3072 +// else: return x ** 2 // 16 + 480 * x - 199680 // // where is x is max(length_of_MODULUS, length_of_BASE) func modexpMultComplexity(x *big.Int) *big.Int { diff --git a/core/vm/evm.go b/core/vm/evm.go index 1e8689aa02..d4d9fbf58b 100644 --- a/core/vm/evm.go +++ b/core/vm/evm.go @@ -46,6 +46,8 @@ type ( func (evm *EVM) precompile(addr common.Address) (PrecompiledContract, bool) { var precompiles map[common.Address]PrecompiledContract switch { + case evm.chainRules.IsBanach: + precompiles = PrecompiledContractsBanach case evm.chainRules.IsArchimedes: precompiles = PrecompiledContractsArchimedes case evm.chainRules.IsBerlin: diff --git a/core/vm/runtime/runtime.go b/core/vm/runtime/runtime.go index 48e9a29b34..a60927215e 100644 --- a/core/vm/runtime/runtime.go +++ b/core/vm/runtime/runtime.go @@ -69,6 +69,7 @@ func setDefaults(cfg *Config) { BerlinBlock: new(big.Int), LondonBlock: new(big.Int), ArchimedesBlock: new(big.Int), + BanachBlock: new(big.Int), } } diff --git a/params/config.go b/params/config.go index bce481a8cb..7d00e62808 100644 --- a/params/config.go +++ b/params/config.go @@ -279,6 +279,7 @@ var ( ArrowGlacierBlock: nil, ArchimedesBlock: big.NewInt(2646311), ShanghaiBlock: nil, + BanachBlock: nil, Clique: &CliqueConfig{ Period: 3, Epoch: 30000, @@ -317,6 +318,7 @@ var ( ArrowGlacierBlock: nil, ArchimedesBlock: big.NewInt(0), ShanghaiBlock: big.NewInt(0), + BanachBlock: nil, Clique: &CliqueConfig{ Period: 3, Epoch: 30000, @@ -355,6 +357,7 @@ var ( ArrowGlacierBlock: nil, ArchimedesBlock: big.NewInt(0), ShanghaiBlock: big.NewInt(0), + BanachBlock: nil, Clique: &CliqueConfig{ Period: 3, Epoch: 30000, @@ -380,7 +383,7 @@ var ( // // This configuration is intentionally not using keyed fields to force anyone // adding flags to the config to also have to set these fields. - AllEthashProtocolChanges = &ChainConfig{big.NewInt(1337), big.NewInt(0), nil, false, big.NewInt(0), common.Hash{}, big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), nil, nil, new(EthashConfig), nil, + AllEthashProtocolChanges = &ChainConfig{big.NewInt(1337), big.NewInt(0), nil, false, big.NewInt(0), common.Hash{}, big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), nil, nil, nil, new(EthashConfig), nil, ScrollConfig{ UseZktrie: false, FeeVaultAddress: nil, @@ -396,7 +399,7 @@ var ( // // This configuration is intentionally not using keyed fields to force anyone // adding flags to the config to also have to set these fields. - AllCliqueProtocolChanges = &ChainConfig{big.NewInt(1337), big.NewInt(0), nil, false, big.NewInt(0), common.Hash{}, big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), nil, nil, nil, nil, &CliqueConfig{Period: 0, Epoch: 30000}, + AllCliqueProtocolChanges = &ChainConfig{big.NewInt(1337), big.NewInt(0), nil, false, big.NewInt(0), common.Hash{}, big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), nil, nil, nil, nil, nil, &CliqueConfig{Period: 0, Epoch: 30000}, ScrollConfig{ UseZktrie: false, FeeVaultAddress: nil, @@ -407,7 +410,7 @@ var ( L1Config: &L1Config{5, common.HexToAddress("0x0000000000000000000000000000000000000000"), 0, common.HexToAddress("0x0000000000000000000000000000000000000000")}, }} - TestChainConfig = &ChainConfig{big.NewInt(1), big.NewInt(0), nil, false, big.NewInt(0), common.Hash{}, big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), nil, nil, new(EthashConfig), nil, + TestChainConfig = &ChainConfig{big.NewInt(1), big.NewInt(0), nil, false, big.NewInt(0), common.Hash{}, big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), nil, nil, nil, new(EthashConfig), nil, ScrollConfig{ UseZktrie: false, FeeVaultAddress: &common.Address{123}, @@ -419,7 +422,7 @@ var ( }} TestRules = TestChainConfig.Rules(new(big.Int)) - TestNoL1DataFeeChainConfig = &ChainConfig{big.NewInt(1), big.NewInt(0), nil, false, big.NewInt(0), common.Hash{}, big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), nil, nil, new(EthashConfig), nil, + TestNoL1DataFeeChainConfig = &ChainConfig{big.NewInt(1), big.NewInt(0), nil, false, big.NewInt(0), common.Hash{}, big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), nil, nil, nil, new(EthashConfig), nil, ScrollConfig{ UseZktrie: false, FeeVaultAddress: nil, @@ -509,6 +512,7 @@ type ChainConfig struct { ArrowGlacierBlock *big.Int `json:"arrowGlacierBlock,omitempty"` // Eip-4345 (bomb delay) switch block (nil = no fork, 0 = already activated) ArchimedesBlock *big.Int `json:"archimedesBlock,omitempty"` // Archimedes switch block (nil = no fork, 0 = already on archimedes) ShanghaiBlock *big.Int `json:"shanghaiBlock,omitempty"` // Shanghai switch block (nil = no fork, 0 = already on shanghai) + BanachBlock *big.Int `json:"banachBlock,omitempty"` // Banach switch block (nil = no fork, 0 = already on banach) // TerminalTotalDifficulty is the amount of total difficulty reached by // the network that triggers the consensus upgrade. @@ -634,7 +638,7 @@ func (c *ChainConfig) String() string { default: engine = "unknown" } - return fmt.Sprintf("{ChainID: %v Homestead: %v DAO: %v DAOSupport: %v EIP150: %v EIP155: %v EIP158: %v Byzantium: %v Constantinople: %v Petersburg: %v Istanbul: %v, Muir Glacier: %v, Berlin: %v, London: %v, Arrow Glacier: %v, Archimedes: %v, Shanghai: %v, Engine: %v, Scroll config: %v}", + return fmt.Sprintf("{ChainID: %v Homestead: %v DAO: %v DAOSupport: %v EIP150: %v EIP155: %v EIP158: %v Byzantium: %v Constantinople: %v Petersburg: %v Istanbul: %v, Muir Glacier: %v, Berlin: %v, London: %v, Arrow Glacier: %v, Archimedes: %v, Shanghai: %v, Banach: %v, Engine: %v, Scroll config: %v}", c.ChainID, c.HomesteadBlock, c.DAOForkBlock, @@ -652,6 +656,7 @@ func (c *ChainConfig) String() string { c.ArrowGlacierBlock, c.ArchimedesBlock, c.ShanghaiBlock, + c.BanachBlock, engine, c.Scroll, ) @@ -734,6 +739,11 @@ func (c *ChainConfig) IsShanghai(num *big.Int) bool { return isForked(c.ShanghaiBlock, num) } +// IsBanach returns whether num is either equal to the Banach fork block or greater. +func (c *ChainConfig) IsBanach(num *big.Int) bool { + return isForked(c.BanachBlock, num) +} + // IsTerminalPoWBlock returns whether the given block is the last block of PoW stage. func (c *ChainConfig) IsTerminalPoWBlock(parentTotalDiff *big.Int, totalDiff *big.Int) bool { if c.TerminalTotalDifficulty == nil { @@ -785,6 +795,7 @@ func (c *ChainConfig) CheckConfigForkOrder() error { {name: "arrowGlacierBlock", block: c.ArrowGlacierBlock, optional: true}, {name: "archimedesBlock", block: c.ArchimedesBlock, optional: true}, {name: "shanghaiBlock", block: c.ShanghaiBlock, optional: true}, + {name: "banachBlock", block: c.BanachBlock, optional: true}, } { if lastFork.name != "" { // Next one must be higher number @@ -863,6 +874,9 @@ func (c *ChainConfig) checkCompatible(newcfg *ChainConfig, head *big.Int) *Confi if isForkIncompatible(c.ShanghaiBlock, newcfg.ShanghaiBlock, head) { return newCompatError("Shanghai fork block", c.ShanghaiBlock, newcfg.ShanghaiBlock) } + if isForkIncompatible(c.BanachBlock, newcfg.BanachBlock, head) { + return newCompatError("Hard fork block", c.BanachBlock, newcfg.BanachBlock) + } return nil } @@ -931,6 +945,7 @@ type Rules struct { IsHomestead, IsEIP150, IsEIP155, IsEIP158 bool IsByzantium, IsConstantinople, IsPetersburg, IsIstanbul bool IsBerlin, IsLondon, IsArchimedes, IsShanghai bool + IsBanach bool } // Rules ensures c's ChainID is not nil. @@ -953,5 +968,6 @@ func (c *ChainConfig) Rules(num *big.Int) Rules { IsLondon: c.IsLondon(num), IsArchimedes: c.IsArchimedes(num), IsShanghai: c.IsShanghai(num), + IsBanach: c.IsBanach(num), } } diff --git a/params/version.go b/params/version.go index 2bf76cba46..4f095b93f2 100644 --- a/params/version.go +++ b/params/version.go @@ -24,7 +24,7 @@ import ( const ( VersionMajor = 5 // Major version component of the current release VersionMinor = 1 // Minor version component of the current release - VersionPatch = 15 // Patch version component of the current release + VersionPatch = 16 // Patch version component of the current release VersionMeta = "mainnet" // Version metadata to append to the version string ) diff --git a/tests/init.go b/tests/init.go index 5b726bb7e8..0b9b0895c2 100644 --- a/tests/init.go +++ b/tests/init.go @@ -213,6 +213,23 @@ var Forks = map[string]*params.ChainConfig{ ArrowGlacierBlock: big.NewInt(0), ArchimedesBlock: big.NewInt(0), }, + "Banach": { + ChainID: big.NewInt(1), + HomesteadBlock: big.NewInt(0), + EIP150Block: big.NewInt(0), + EIP155Block: big.NewInt(0), + EIP158Block: big.NewInt(0), + ByzantiumBlock: big.NewInt(0), + ConstantinopleBlock: big.NewInt(0), + PetersburgBlock: big.NewInt(0), + IstanbulBlock: big.NewInt(0), + MuirGlacierBlock: big.NewInt(0), + BerlinBlock: big.NewInt(0), + LondonBlock: big.NewInt(0), + ArrowGlacierBlock: big.NewInt(0), + ArchimedesBlock: big.NewInt(0), + BanachBlock: big.NewInt(0), + }, } // Returns the set of defined fork names