Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't default to mainnet if NetworkID != 1 #9408

Merged
merged 5 commits into from
Feb 12, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 12 additions & 14 deletions cmd/utils/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -1618,7 +1618,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 @@ -1645,7 +1655,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 @@ -1681,9 +1690,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 @@ -1706,9 +1712,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 @@ -1718,19 +1723,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
74 changes: 37 additions & 37 deletions eth/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,40 +239,6 @@ func New(ctx context.Context, stack *node.Node, config *ethconfig.Config, logger
}
latestBlockBuiltStore := builder.NewLatestBlockBuiltStore()

if err := chainKv.Update(context.Background(), func(tx kv.RwTx) error {
if err = stagedsync.UpdateMetrics(tx); err != nil {
return err
}

config.Prune, err = prune.EnsureNotChanged(tx, config.Prune)
if err != nil {
return err
}

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
}); 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())

// kv_remote architecture does blocks on stream.Send - means current architecture require unlimited amount of txs to provide good throughput
Expand Down Expand Up @@ -317,6 +283,40 @@ 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 {
if err = stagedsync.UpdateMetrics(tx); err != nil {
return err
}

config.Prune, err = prune.EnsureNotChanged(tx, config.Prune)
if err != nil {
return err
}

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(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 +840,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 +903,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
Loading