Skip to content

Commit

Permalink
Don't default to mainnet if NetworkID != 1 (erigontech#9408)
Browse files Browse the repository at this point in the history
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 erigontech#9191 and also some Hive tests that were incorrectly
returning `SYNCING` because they were trying to download mainnet
snapshots.
  • Loading branch information
yperbasis authored Feb 12, 2024
1 parent 24d4f1c commit 321b8d9
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 44 deletions.
26 changes: 12 additions & 14 deletions cmd/utils/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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...)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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{}) {
Expand Down
46 changes: 24 additions & 22 deletions eth/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
}

Expand Down Expand Up @@ -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)
}

Expand Down
3 changes: 0 additions & 3 deletions eth/stagedsync/stage_execute.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion tests/bor/helper/miner.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion turbo/app/import_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
14 changes: 12 additions & 2 deletions turbo/cli/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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))
}
Expand Down
2 changes: 1 addition & 1 deletion turbo/node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit 321b8d9

Please sign in to comment.