From f190c49252e04fa5820de2648dc2fa599a9ca452 Mon Sep 17 00:00:00 2001 From: Nathan Date: Tue, 21 May 2024 17:46:42 +0800 Subject: [PATCH] core/vm: add secp256r1 into PrecompiledContractsHaber (#2483) --- cmd/geth/chaincmd.go | 5 +++++ cmd/geth/config.go | 4 ++++ cmd/geth/main.go | 1 + cmd/utils/flags.go | 5 +++++ core/genesis.go | 4 ++++ core/vm/contracts.go | 30 ++++++++++++++++++++++++++++++ core/vm/evm.go | 2 ++ eth/backend.go | 4 ++++ eth/ethconfig/config.go | 3 +++ eth/ethconfig/gen_config.go | 6 ++++++ params/config.go | 24 +++++++++++++++++++++--- 11 files changed, 85 insertions(+), 3 deletions(-) diff --git a/cmd/geth/chaincmd.go b/cmd/geth/chaincmd.go index b7ff192334..7f810dbe20 100644 --- a/cmd/geth/chaincmd.go +++ b/cmd/geth/chaincmd.go @@ -63,6 +63,7 @@ var ( Flags: flags.Merge([]cli.Flag{ utils.CachePreimagesFlag, utils.OverrideCancun, + utils.OverrideHaber, utils.OverrideVerkle, }, utils.DatabaseFlags), Description: ` @@ -256,6 +257,10 @@ func initGenesis(ctx *cli.Context) error { v := ctx.Uint64(utils.OverrideCancun.Name) overrides.OverrideCancun = &v } + if ctx.IsSet(utils.OverrideHaber.Name) { + v := ctx.Uint64(utils.OverrideHaber.Name) + overrides.OverrideHaber = &v + } if ctx.IsSet(utils.OverrideVerkle.Name) { v := ctx.Uint64(utils.OverrideVerkle.Name) overrides.OverrideVerkle = &v diff --git a/cmd/geth/config.go b/cmd/geth/config.go index ca7e7810a7..d35e088ff2 100644 --- a/cmd/geth/config.go +++ b/cmd/geth/config.go @@ -189,6 +189,10 @@ func makeFullNode(ctx *cli.Context) (*node.Node, ethapi.Backend) { v := ctx.Uint64(utils.OverrideCancun.Name) cfg.Eth.OverrideCancun = &v } + if ctx.IsSet(utils.OverrideHaber.Name) { + v := ctx.Uint64(utils.OverrideHaber.Name) + cfg.Eth.OverrideHaber = &v + } if ctx.IsSet(utils.OverrideVerkle.Name) { v := ctx.Uint64(utils.OverrideVerkle.Name) cfg.Eth.OverrideVerkle = &v diff --git a/cmd/geth/main.go b/cmd/geth/main.go index d4be86c012..4689290d6b 100644 --- a/cmd/geth/main.go +++ b/cmd/geth/main.go @@ -73,6 +73,7 @@ var ( utils.SmartCardDaemonPathFlag, utils.RialtoHash, utils.OverrideCancun, + utils.OverrideHaber, utils.OverrideVerkle, utils.OverrideFullImmutabilityThreshold, utils.OverrideMinBlocksForBlobRequests, diff --git a/cmd/utils/flags.go b/cmd/utils/flags.go index c2653dd66e..e6faa8d0f8 100644 --- a/cmd/utils/flags.go +++ b/cmd/utils/flags.go @@ -310,6 +310,11 @@ var ( Usage: "Manually specify the Cancun fork timestamp, overriding the bundled setting", Category: flags.EthCategory, } + OverrideHaber = &cli.Uint64Flag{ + Name: "override.haber", + Usage: "Manually specify the Haber fork timestamp, overriding the bundled setting", + Category: flags.EthCategory, + } OverrideVerkle = &cli.Uint64Flag{ Name: "override.verkle", Usage: "Manually specify the Verkle fork timestamp, overriding the bundled setting", diff --git a/core/genesis.go b/core/genesis.go index 0e567f8231..b4833ac8a6 100644 --- a/core/genesis.go +++ b/core/genesis.go @@ -217,6 +217,7 @@ func (e *GenesisMismatchError) Error() string { // Typically, these modifications involve hardforks that are not enabled on the BSC mainnet, intended for testing purposes. type ChainOverrides struct { OverrideCancun *uint64 + OverrideHaber *uint64 OverrideVerkle *uint64 } @@ -246,6 +247,9 @@ func SetupGenesisBlockWithOverride(db ethdb.Database, triedb *triedb.Database, g if overrides != nil && overrides.OverrideCancun != nil { config.CancunTime = overrides.OverrideCancun } + if overrides != nil && overrides.OverrideHaber != nil { + config.HaberTime = overrides.OverrideHaber + } if overrides != nil && overrides.OverrideVerkle != nil { config.VerkleTime = overrides.OverrideVerkle } diff --git a/core/vm/contracts.go b/core/vm/contracts.go index b3dd51d7ea..38a6cac24d 100644 --- a/core/vm/contracts.go +++ b/core/vm/contracts.go @@ -248,6 +248,30 @@ var PrecompiledContractsCancun = map[common.Address]PrecompiledContract{ common.BytesToAddress([]byte{105}): &secp256k1SignatureRecover{}, } +// PrecompiledContractsHaber contains the default set of pre-compiled Ethereum +// contracts used in the Haber release. +var PrecompiledContractsHaber = map[common.Address]PrecompiledContract{ + common.BytesToAddress([]byte{1}): &ecrecover{}, + common.BytesToAddress([]byte{2}): &sha256hash{}, + common.BytesToAddress([]byte{3}): &ripemd160hash{}, + 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}): &blake2F{}, + common.BytesToAddress([]byte{0x0a}): &kzgPointEvaluation{}, + + common.BytesToAddress([]byte{100}): &tmHeaderValidate{}, + common.BytesToAddress([]byte{101}): &iavlMerkleProofValidatePlato{}, + common.BytesToAddress([]byte{102}): &blsSignatureVerify{}, + common.BytesToAddress([]byte{103}): &cometBFTLightBlockValidateHertz{}, + common.BytesToAddress([]byte{104}): &verifyDoubleSignEvidence{}, + common.BytesToAddress([]byte{105}): &secp256k1SignatureRecover{}, + + common.BytesToAddress([]byte{0x01, 0x00}): &p256Verify{}, +} + // PrecompiledContractsP256Verify contains the precompiled Ethereum // contract specified in EIP-7212. This is exported for testing purposes. var PrecompiledContractsP256Verify = map[common.Address]PrecompiledContract{ @@ -269,6 +293,7 @@ var PrecompiledContractsBLS = map[common.Address]PrecompiledContract{ } var ( + PrecompiledAddressesHaber []common.Address PrecompiledAddressesCancun []common.Address PrecompiledAddressesFeynman []common.Address PrecompiledAddressesHertz []common.Address @@ -320,11 +345,16 @@ func init() { for k := range PrecompiledContractsCancun { PrecompiledAddressesCancun = append(PrecompiledAddressesCancun, k) } + for k := range PrecompiledContractsHaber { + PrecompiledAddressesHaber = append(PrecompiledAddressesHaber, k) + } } // ActivePrecompiles returns the precompiles enabled with the current configuration. func ActivePrecompiles(rules params.Rules) []common.Address { switch { + case rules.IsHaber: + return PrecompiledAddressesHaber case rules.IsCancun: return PrecompiledAddressesCancun case rules.IsFeynman: diff --git a/core/vm/evm.go b/core/vm/evm.go index a24703342d..676b7cc3f2 100644 --- a/core/vm/evm.go +++ b/core/vm/evm.go @@ -48,6 +48,8 @@ type ( func (evm *EVM) precompile(addr common.Address) (PrecompiledContract, bool) { var precompiles map[common.Address]PrecompiledContract switch { + case evm.chainRules.IsHaber: + precompiles = PrecompiledContractsHaber case evm.chainRules.IsCancun: precompiles = PrecompiledContractsCancun case evm.chainRules.IsFeynman: diff --git a/eth/backend.go b/eth/backend.go index 860562eb4a..89ce49ad1d 100644 --- a/eth/backend.go +++ b/eth/backend.go @@ -183,6 +183,10 @@ func New(stack *node.Node, config *ethconfig.Config) (*Ethereum, error) { chainConfig.CancunTime = config.OverrideCancun overrides.OverrideCancun = config.OverrideCancun } + if config.OverrideHaber != nil { + chainConfig.HaberTime = config.OverrideHaber + overrides.OverrideHaber = config.OverrideHaber + } if config.OverrideVerkle != nil { chainConfig.VerkleTime = config.OverrideVerkle overrides.OverrideVerkle = config.OverrideVerkle diff --git a/eth/ethconfig/config.go b/eth/ethconfig/config.go index daa92e8c8b..0b69b62219 100644 --- a/eth/ethconfig/config.go +++ b/eth/ethconfig/config.go @@ -191,6 +191,9 @@ type Config struct { // OverrideCancun (TODO: remove after the fork) OverrideCancun *uint64 `toml:",omitempty"` + // OverrideHaber (TODO: remove after the fork) + OverrideHaber *uint64 `toml:",omitempty"` + // OverrideVerkle (TODO: remove after the fork) OverrideVerkle *uint64 `toml:",omitempty"` diff --git a/eth/ethconfig/gen_config.go b/eth/ethconfig/gen_config.go index 9f59076bcf..4ed328d632 100644 --- a/eth/ethconfig/gen_config.go +++ b/eth/ethconfig/gen_config.go @@ -71,6 +71,7 @@ func (c Config) MarshalTOML() (interface{}, error) { RPCEVMTimeout time.Duration RPCTxFeeCap float64 OverrideCancun *uint64 `toml:",omitempty"` + OverrideHaber *uint64 `toml:",omitempty"` OverrideVerkle *uint64 `toml:",omitempty"` BlobExtraReserve uint64 } @@ -129,6 +130,7 @@ func (c Config) MarshalTOML() (interface{}, error) { enc.RPCEVMTimeout = c.RPCEVMTimeout enc.RPCTxFeeCap = c.RPCTxFeeCap enc.OverrideCancun = c.OverrideCancun + enc.OverrideHaber = c.OverrideHaber enc.OverrideVerkle = c.OverrideVerkle enc.BlobExtraReserve = c.BlobExtraReserve return &enc, nil @@ -191,6 +193,7 @@ func (c *Config) UnmarshalTOML(unmarshal func(interface{}) error) error { RPCEVMTimeout *time.Duration RPCTxFeeCap *float64 OverrideCancun *uint64 `toml:",omitempty"` + OverrideHaber *uint64 `toml:",omitempty"` OverrideVerkle *uint64 `toml:",omitempty"` BlobExtraReserve *uint64 } @@ -360,6 +363,9 @@ func (c *Config) UnmarshalTOML(unmarshal func(interface{}) error) error { if dec.OverrideCancun != nil { c.OverrideCancun = dec.OverrideCancun } + if dec.OverrideHaber != nil { + c.OverrideHaber = dec.OverrideHaber + } if dec.OverrideVerkle != nil { c.OverrideVerkle = dec.OverrideVerkle } diff --git a/params/config.go b/params/config.go index 1c0b8fb175..610afc470f 100644 --- a/params/config.go +++ b/params/config.go @@ -152,6 +152,7 @@ var ( FeynmanTime: newUint64(1713419340), // 2024-04-18 05:49:00 AM UTC FeynmanFixTime: newUint64(1713419340), // 2024-04-18 05:49:00 AM UTC CancunTime: newUint64(1718863500), // 2024-06-20 06:05:00 AM UTC + HaberTime: newUint64(1718863500), // 2024-06-20 06:05:00 AM UTC Parlia: &ParliaConfig{ Period: 3, @@ -190,6 +191,7 @@ var ( FeynmanTime: newUint64(1710136800), // 2024-03-11 6:00:00 AM UTC FeynmanFixTime: newUint64(1711342800), // 2024-03-25 5:00:00 AM UTC CancunTime: newUint64(1713330442), // 2024-04-17 05:07:22 AM UTC + HaberTime: newUint64(1716962820), // 2024-05-29 06:07:00 AM UTC Parlia: &ParliaConfig{ Period: 3, @@ -229,6 +231,7 @@ var ( FeynmanTime: newUint64(0), FeynmanFixTime: newUint64(0), CancunTime: newUint64(0), + HaberTime: newUint64(0), Parlia: &ParliaConfig{ Period: 3, @@ -267,6 +270,7 @@ var ( FeynmanTime: newUint64(0), FeynmanFixTime: newUint64(0), CancunTime: newUint64(0), + Parlia: &ParliaConfig{ Period: 3, Epoch: 200, @@ -504,6 +508,7 @@ type ChainConfig struct { FeynmanTime *uint64 `json:"feynmanTime,omitempty"` // Feynman switch time (nil = no fork, 0 = already activated) FeynmanFixTime *uint64 `json:"feynmanFixTime,omitempty"` // FeynmanFix switch time (nil = no fork, 0 = already activated) CancunTime *uint64 `json:"cancunTime,omitempty"` // Cancun switch time (nil = no fork, 0 = already on cancun) + HaberTime *uint64 `json:"haberTime,omitempty"` // Haber switch time (nil = no fork, 0 = already on haber) PragueTime *uint64 `json:"pragueTime,omitempty"` // Prague switch time (nil = no fork, 0 = already on prague) VerkleTime *uint64 `json:"verkleTime,omitempty"` // Verkle switch time (nil = no fork, 0 = already on verkle) @@ -609,7 +614,12 @@ func (c *ChainConfig) String() string { CancunTime = big.NewInt(0).SetUint64(*c.CancunTime) } - 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, Ramanujan: %v, Niels: %v, MirrorSync: %v, Bruno: %v, Berlin: %v, YOLO v3: %v, CatalystBlock: %v, London: %v, ArrowGlacier: %v, MergeFork:%v, Euler: %v, Gibbs: %v, Nano: %v, Moran: %v, Planck: %v,Luban: %v, Plato: %v, Hertz: %v, Hertzfix: %v, ShanghaiTime: %v, KeplerTime: %v, FeynmanTime: %v, FeynmanFixTime: %v, CancunTime: %v, Engine: %v}", + var HaberTime *big.Int + if c.HaberTime != nil { + HaberTime = big.NewInt(0).SetUint64(*c.HaberTime) + } + + 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, Ramanujan: %v, Niels: %v, MirrorSync: %v, Bruno: %v, Berlin: %v, YOLO v3: %v, CatalystBlock: %v, London: %v, ArrowGlacier: %v, MergeFork:%v, Euler: %v, Gibbs: %v, Nano: %v, Moran: %v, Planck: %v,Luban: %v, Plato: %v, Hertz: %v, Hertzfix: %v, ShanghaiTime: %v, KeplerTime: %v, FeynmanTime: %v, FeynmanFixTime: %v, CancunTime: %v, HaberTime: %v, Engine: %v}", c.ChainID, c.HomesteadBlock, c.DAOForkBlock, @@ -646,6 +656,7 @@ func (c *ChainConfig) String() string { FeynmanTime, FeynmanFixTime, CancunTime, + HaberTime, engine, ) } @@ -913,6 +924,11 @@ func (c *ChainConfig) IsCancun(num *big.Int, time uint64) bool { return c.IsLondon(num) && isTimestampForked(c.CancunTime, time) } +// IsHaber returns whether time is either equal to the Haber fork time or greater. +func (c *ChainConfig) IsHaber(num *big.Int, time uint64) bool { + return c.IsLondon(num) && isTimestampForked(c.HaberTime, time) +} + // IsPrague returns whether num is either equal to the Prague fork time or greater. func (c *ChainConfig) IsPrague(num *big.Int, time uint64) bool { return c.IsLondon(num) && isTimestampForked(c.PragueTime, time) @@ -976,6 +992,7 @@ func (c *ChainConfig) CheckConfigForkOrder() error { {name: "feynmanTime", timestamp: c.FeynmanTime}, {name: "feynmanFixTime", timestamp: c.FeynmanFixTime}, {name: "cancunTime", timestamp: c.CancunTime}, + {name: "haberTime", timestamp: c.HaberTime}, {name: "pragueTime", timestamp: c.PragueTime, optional: true}, {name: "verkleTime", timestamp: c.VerkleTime, optional: true}, } { @@ -1305,8 +1322,8 @@ type Rules struct { IsPlato bool IsHertz bool IsHertzfix bool - IsShanghai, IsKepler, IsFeynman, IsCancun, IsPrague bool - IsVerkle bool + IsShanghai, IsKepler, IsFeynman, IsCancun, IsHaber bool + IsPrague, IsVerkle bool } // Rules ensures c's ChainID is not nil. @@ -1341,6 +1358,7 @@ func (c *ChainConfig) Rules(num *big.Int, isMerge bool, timestamp uint64) Rules IsKepler: c.IsKepler(num, timestamp), IsFeynman: c.IsFeynman(num, timestamp), IsCancun: c.IsCancun(num, timestamp), + IsHaber: c.IsHaber(num, timestamp), IsPrague: c.IsPrague(num, timestamp), IsVerkle: c.IsVerkle(num, timestamp), }