From 321b8d9b6d155e5881535df150559c58c095016d Mon Sep 17 00:00:00 2001 From: Andrew Ashikhmin <34320705+yperbasis@users.noreply.github.com> Date: Mon, 12 Feb 2024 08:50:33 +0100 Subject: [PATCH] Don't default to mainnet if NetworkID != 1 (#9408) In certain scenarios (e.g. Hive) genesis is initially populated by `erigon init` and afterwards erigon is started with some `networkid` flag, typically != 1. In that case it's incorrect to use default (mainnet) genesis instead of reading genesis from the DB. This PR fixes Issue #9191 and also some Hive tests that were incorrectly returning `SYNCING` because they were trying to download mainnet snapshots. --- cmd/utils/flags.go | 26 +++++++++---------- eth/backend.go | 46 +++++++++++++++++---------------- eth/stagedsync/stage_execute.go | 3 --- tests/bor/helper/miner.go | 2 +- turbo/app/import_cmd.go | 2 +- turbo/cli/flags.go | 14 ++++++++-- turbo/node/node.go | 2 +- 7 files changed, 51 insertions(+), 44 deletions(-) diff --git a/cmd/utils/flags.go b/cmd/utils/flags.go index 82c05daa1a2..7acb5e4aea4 100644 --- a/cmd/utils/flags.go +++ b/cmd/utils/flags.go @@ -1626,7 +1626,17 @@ func SetEthConfig(ctx *cli.Context, nodeConfig *nodecfg.Config, cfg *ethconfig.C cfg.SentinelPort = ctx.Uint64(SentinelPortFlag.Name) cfg.ForcePartialCommit = ctx.Bool(ForcePartialCommitFlag.Name) - cfg.Sync.UseSnapshots = ethconfig.UseSnapshotsByChainName(ctx.String(ChainFlag.Name)) + chain := ctx.String(ChainFlag.Name) // mainnet by default + if ctx.IsSet(NetworkIdFlag.Name) { + cfg.NetworkID = ctx.Uint64(NetworkIdFlag.Name) + if cfg.NetworkID != 1 && !ctx.IsSet(ChainFlag.Name) { + chain = "" // don't default to mainnet if NetworkID != 1 + } + } else { + cfg.NetworkID = params.NetworkIDByChainName(chain) + } + + cfg.Sync.UseSnapshots = ethconfig.UseSnapshotsByChainName(chain) if ctx.IsSet(SnapshotFlag.Name) { //force override default by cli cfg.Sync.UseSnapshots = ctx.Bool(SnapshotFlag.Name) } @@ -1653,7 +1663,6 @@ func SetEthConfig(ctx *cli.Context, nodeConfig *nodecfg.Config, cfg *ethconfig.C } logger.Info("torrent verbosity", "level", lvl.LogString()) version := "erigon: " + params.VersionWithCommit(params.GitCommit) - chain := ctx.String(ChainFlag.Name) webseedsList := libcommon.CliString2Array(ctx.String(WebSeedsFlag.Name)) if known, ok := snapcfg.KnownWebseeds[chain]; ok { webseedsList = append(webseedsList, known...) @@ -1689,9 +1698,6 @@ func SetEthConfig(ctx *cli.Context, nodeConfig *nodecfg.Config, cfg *ethconfig.C cfg.Ethstats = ctx.String(EthStatsURLFlag.Name) cfg.HistoryV3 = ctx.Bool(HistoryV3Flag.Name) - if ctx.IsSet(NetworkIdFlag.Name) { - cfg.NetworkID = ctx.Uint64(NetworkIdFlag.Name) - } if ctx.IsSet(RPCGlobalGasCapFlag.Name) { cfg.RPCGasCap = ctx.Uint64(RPCGlobalGasCapFlag.Name) @@ -1714,9 +1720,8 @@ func SetEthConfig(ctx *cli.Context, nodeConfig *nodecfg.Config, cfg *ethconfig.C cfg.EthDiscoveryURLs = libcommon.CliString2Array(urls) } } - // Override any default configs for hard coded networks. - chain := ctx.String(ChainFlag.Name) + // Override any default configs for hard coded networks. switch chain { default: genesis := core.GenesisBlockByChainName(chain) @@ -1726,19 +1731,12 @@ func SetEthConfig(ctx *cli.Context, nodeConfig *nodecfg.Config, cfg *ethconfig.C return } cfg.Genesis = genesis - if !ctx.IsSet(NetworkIdFlag.Name) { - cfg.NetworkID = params.NetworkIDByChainName(chain) - } SetDNSDiscoveryDefaults(cfg, *genesisHash) case "": if cfg.NetworkID == 1 { SetDNSDiscoveryDefaults(cfg, params.MainnetGenesisHash) } case networkname.DevChainName: - if !ctx.IsSet(NetworkIdFlag.Name) { - cfg.NetworkID = params.NetworkIDByChainName(chain) - } - // Create new developer account or reuse existing one developer := cfg.Miner.Etherbase if developer == (libcommon.Address{}) { diff --git a/eth/backend.go b/eth/backend.go index 9842cc85238..4cca97bf8d2 100644 --- a/eth/backend.go +++ b/eth/backend.go @@ -250,28 +250,10 @@ func New(ctx context.Context, stack *node.Node, config *ethconfig.Config, logger } config.HistoryV3, err = kvcfg.HistoryV3.WriteOnce(tx, config.HistoryV3) - if err != nil { - return err - } - - isCorrectSync, useSnapshots, err := snap.EnsureNotChanged(tx, config.Snapshot) - if err != nil { - return err - } - // if we are in the incorrect syncmode then we change it to the appropriate one - if !isCorrectSync { - config.Sync.UseSnapshots = useSnapshots - config.Snapshot.Enabled = ethconfig.UseSnapshotsByChainName(config.Genesis.Config.ChainName) && useSnapshots - } - return nil + return err }); err != nil { return nil, err } - if !config.Sync.UseSnapshots { - if err := downloader.CreateProhibitNewDownloadsFile(dirs.Snap); err != nil { - return nil, err - } - } ctx, ctxCancel := context.WithCancel(context.Background()) @@ -317,6 +299,26 @@ func New(ctx context.Context, stack *node.Node, config *ethconfig.Config, logger backend.genesisBlock = genesis backend.genesisHash = genesis.Hash() + if err := chainKv.Update(context.Background(), func(tx kv.RwTx) error { + isCorrectSync, useSnapshots, err := snap.EnsureNotChanged(tx, config.Snapshot) + if err != nil { + return err + } + // if we are in the incorrect syncmode then we change it to the appropriate one + if !isCorrectSync { + config.Sync.UseSnapshots = useSnapshots + config.Snapshot.Enabled = ethconfig.UseSnapshotsByChainName(chainConfig.ChainName) && useSnapshots + } + return nil + }); err != nil { + return nil, err + } + if !config.Sync.UseSnapshots { + if err := downloader.CreateProhibitNewDownloadsFile(dirs.Snap); err != nil { + return nil, err + } + } + logger.Info("Initialised chain configuration", "config", chainConfig, "genesis", genesis.Hash()) // Check if we have an already initialized chain and fall back to @@ -840,14 +842,14 @@ func New(ctx context.Context, stack *node.Node, config *ethconfig.Config, logger return backend, nil } -func (s *Ethereum) Init(stack *node.Node, config *ethconfig.Config) error { +func (s *Ethereum) Init(stack *node.Node, config *ethconfig.Config, chainConfig *chain.Config) error { ethBackendRPC, miningRPC, stateDiffClient := s.ethBackendRPC, s.miningRPC, s.stateChangesClient blockReader := s.blockReader ctx := s.sentryCtx chainKv := s.chainDB var err error - if config.Genesis.Config.Bor == nil { + if chainConfig.Bor == nil { s.sentriesClient.Hd.StartPoSDownloader(s.sentryCtx, s.sentriesClient.SendHeaderRequest, s.sentriesClient.Penalize) } @@ -903,7 +905,7 @@ func (s *Ethereum) Init(stack *node.Node, config *ethconfig.Config) error { }() } - if config.Genesis.Config.Bor == nil { + if chainConfig.Bor == nil { go s.engineBackendRPC.Start(&httpRpcCfg, s.chainDB, s.blockReader, ff, stateCache, s.agg, s.engine, ethRpcClient, txPoolRpcClient, miningRpcClient) } diff --git a/eth/stagedsync/stage_execute.go b/eth/stagedsync/stage_execute.go index 4429aad9cd2..072de8e5b5d 100644 --- a/eth/stagedsync/stage_execute.go +++ b/eth/stagedsync/stage_execute.go @@ -112,9 +112,6 @@ func StageExecuteBlocksCfg( agg *libstate.AggregatorV3, silkworm *silkworm.Silkworm, ) ExecuteBlockCfg { - if genesis == nil { - panic("assert: nil genesis") - } return ExecuteBlockCfg{ db: db, prune: pm, diff --git a/tests/bor/helper/miner.go b/tests/bor/helper/miner.go index 592146c4b5b..a3a4537adf4 100644 --- a/tests/bor/helper/miner.go +++ b/tests/bor/helper/miner.go @@ -168,7 +168,7 @@ func InitMiner(ctx context.Context, genesis *types.Genesis, privKey *ecdsa.Priva return nil, nil, err } - err = ethBackend.Init(stack, ethCfg) + err = ethBackend.Init(stack, ethCfg, ethCfg.Genesis.Config) if err != nil { return nil, nil, err } diff --git a/turbo/app/import_cmd.go b/turbo/app/import_cmd.go index 085b2dbb53c..3bbce26bc17 100644 --- a/turbo/app/import_cmd.go +++ b/turbo/app/import_cmd.go @@ -70,7 +70,7 @@ func importChain(cliCtx *cli.Context) error { if err != nil { return err } - err = ethereum.Init(stack, ethCfg) + err = ethereum.Init(stack, ethCfg, ethCfg.Genesis.Config) if err != nil { return err } diff --git a/turbo/cli/flags.go b/turbo/cli/flags.go index 88ab5ca946a..4c17c135ae8 100644 --- a/turbo/cli/flags.go +++ b/turbo/cli/flags.go @@ -242,8 +242,13 @@ var ( ) func ApplyFlagsForEthConfig(ctx *cli.Context, cfg *ethconfig.Config, logger log.Logger) { + chainId := cfg.NetworkID + if cfg.Genesis != nil { + chainId = cfg.Genesis.Config.ChainID.Uint64() + } + mode, err := prune.FromCli( - cfg.Genesis.Config.ChainID.Uint64(), + chainId, ctx.String(PruneFlag.Name), ctx.Uint64(PruneHistoryFlag.Name), ctx.Uint64(PruneReceiptFlag.Name), @@ -376,7 +381,12 @@ func ApplyFlagsForEthConfigCobra(f *pflag.FlagSet, cfg *ethconfig.Config) { beforeC = *v } - mode, err := prune.FromCli(cfg.Genesis.Config.ChainID.Uint64(), *v, exactH, exactR, exactT, exactC, beforeH, beforeR, beforeT, beforeC, experiments) + chainId := cfg.NetworkID + if cfg.Genesis != nil { + chainId = cfg.Genesis.Config.ChainID.Uint64() + } + + mode, err := prune.FromCli(chainId, *v, exactH, exactR, exactT, exactC, beforeH, beforeR, beforeT, beforeC, experiments) if err != nil { utils.Fatalf(fmt.Sprintf("error while parsing mode: %v", err)) } diff --git a/turbo/node/node.go b/turbo/node/node.go index 6bb3e38c5a4..6bbc9901308 100644 --- a/turbo/node/node.go +++ b/turbo/node/node.go @@ -125,7 +125,7 @@ func New( if err != nil { return nil, err } - err = ethereum.Init(node, ethConfig) + err = ethereum.Init(node, ethConfig, ethereum.ChainConfig()) if err != nil { return nil, err }