Skip to content

refactor(all): move genesis initialization to blockchain #25523#2018

Merged
AnilChinchawale merged 1 commit intoXinFinOrg:dev-upgradefrom
gzliudan:move-genesis-state-init
Feb 10, 2026
Merged

refactor(all): move genesis initialization to blockchain #25523#2018
AnilChinchawale merged 1 commit intoXinFinOrg:dev-upgradefrom
gzliudan:move-genesis-state-init

Conversation

@gzliudan
Copy link
Collaborator

Proposed changes

This PR is the minimal version for moving genesis initialization logic into core.Blockchain.

Rerf: ethereum#25523

Types of changes

What types of changes does your code introduce to XDC network?
Put an in the boxes that apply

  • Bugfix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation Update (if none of the other choices apply)
  • Regular KTLO or any of the maintaince work. e.g code style
  • CICD Improvement

Impacted Components

Which part of the codebase this PR will touch base on,

Put an in the boxes that apply

  • Consensus
  • Account
  • Network
  • Geth
  • Smart Contract
  • External components
  • Not sure (Please specify below)

Checklist

Put an in the boxes once you have confirmed below actions (or provide reasons on not doing so) that

  • This PR has sufficient test coverage (unit/integration test) OR I have provided reason in the PR description for not having test coverage
  • Provide an end-to-end test plan in the PR description on how to manually test it on the devnet/testnet.
  • Tested the backwards compatibility.
  • Tested with XDC nodes running this version co-exist with those running the previous version.
  • Relevant documentation has been updated as part of this PR
  • N/A

Copilot AI review requested due to automatic review settings January 30, 2026 06:21
@coderabbitai
Copy link

coderabbitai bot commented Jan 30, 2026

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
✨ Finishing touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Moves genesis initialization responsibilities into core.BlockChain (instead of callers), updating related APIs and tests accordingly.

Changes:

  • Updated core.NewBlockChain / core.NewBlockChainEx to accept a *core.Genesis and run SetupGenesisBlock internally.
  • Refactored core.Genesis creation/commit paths (new ToBlock() behavior, persisted genesis state spec) and adjusted call sites.
  • Updated many tests/helpers/CLIs to pass genesis specs (vs chain config) and to use MustCommit where needed.

Reviewed changes

Copilot reviewed 28 out of 28 changed files in this pull request and generated 7 comments.

Show a summary per file
File Description
tests/state_test_util.go Updates genesis block creation to new ToBlock() signature.
tests/block_test_util.go Passes genesis spec to NewBlockChain instead of chain config.
params/config.go Comment tweak for MainnetChainConfig.
eth/tracers/api_test.go Updates test chain creation to use genesis spec in NewBlockChain.
eth/helper_test.go Updates test blockchain creation to pass genesis spec.
eth/handler_test.go Refactors DAO challenge test setup to use genesis spec for blockchain init.
eth/gasprice/gasprice_test.go Uses MustCommit and passes genesis spec to NewBlockChain.
eth/filters/filter_test.go Refactors genesis setup/commit and NewBlockChain calls for new API.
eth/filters/filter_system_test.go Updates blockchain creation to pass genesis spec.
eth/fetcher/fetcher_test.go Replaces GenesisBlockForTesting usage with explicit genesis spec + commit.
eth/downloader/testchain_test.go Replaces GenesisBlockForTesting with explicit genesis spec + commit; adjusts init.
eth/downloader/queue_test.go Removes local genesis DB setup and reuses shared test genesis/DB.
eth/backend.go Switches to LoadChainConfig before blockchain creation; passes genesis spec to NewBlockChainEx.
core/state_processor_test.go Updates test blockchain initialization to pass genesis spec.
core/rawdb/accessors_metadata.go Changes ReadChainConfig to return *ChainConfig without error; adds WriteGenesisStateSpec.
core/genesis_test.go Updates genesis ToBlock() calls and blockchain init signature in tests.
core/genesis.go Introduces alloc hashing/flushing helpers, adds LoadChainConfig, changes ToBlock/Commit flow, persists genesis alloc spec.
core/dao_test.go Refactors DAO fork tests to build blockchains from genesis specs.
core/chain_makers_test.go Updates example to pass genesis spec to NewBlockChain.
core/chain_makers.go Updates GenerateChainWithGenesis to use new ToBlock() signature.
core/blockchain_test.go Updates all test blockchain construction paths to pass genesis specs.
core/blockchain.go Changes NewBlockChain/NewBlockChainEx signatures; moves genesis setup + config logging inside blockchain init.
core/block_validator_test.go Updates test chain generation and blockchain init for new API.
core/bench_test.go Updates benchmarks to pass genesis spec; adapts bench DB setup for chain config storage.
cmd/utils/flags.go Updates MakeChain to use LoadChainConfig and passes genesis spec to NewBlockChain.
cmd/evm/runner.go Switches genesis handling from ToBlock(db) to MustCommit(db) for state initialization.
cmd/XDC/dao_test.go Updates for ReadChainConfig signature change (nil-check instead of error).
accounts/abi/bind/backends/simulated.go Updates simulated backend creation to pass genesis spec to NewBlockChain.
Comments suppressed due to low confidence (1)

cmd/utils/flags.go:1775

  • Same config divergence risk as in eth/backend.go: MakeChain builds the consensus engine from core.LoadChainConfig, but core.NewBlockChain will run SetupGenesisBlock and may apply a different (upgraded) config. This can leave the engine using a stale config while the blockchain uses the updated one. Prefer obtaining the effective config via SetupGenesisBlock (or equivalent) before constructing the engine.
func MakeChain(ctx *cli.Context, stack *node.Node, readonly bool) (*core.BlockChain, ethdb.Database) {
	var (
		gspec   = MakeGenesis(ctx)
		chainDb = MakeChainDatabase(ctx, stack, readonly)
	)
	config, _, err := core.LoadChainConfig(chainDb, gspec)
	if err != nil {
		Fatalf("%v", err)
	}
	var engine consensus.Engine
	if config.XDPoS != nil {
		engine = XDPoS.New(config, chainDb)
	} else {
		Fatalf("Only support XDPoS consensus")
	}

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 44 to +55
proDb := rawdb.NewMemoryDatabase()
gspec.MustCommit(proDb)
proConf := *params.TestChainConfig
proConf.DAOForkBlock = forkBlock
proConf.DAOForkSupport = true
proBc, _ := NewBlockChain(proDb, nil, &proConf, ethash.NewFaker(), vm.Config{})
progspec := &Genesis{
BaseFee: big.NewInt(params.InitialBaseFee),
Config: &proConf,
}
gspec.MustCommit(proDb)

proBc, _ := NewBlockChain(proDb, nil, progspec, ethash.NewFaker(), vm.Config{})
Copy link

Copilot AI Jan 30, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this test setup, the genesis is committed to proDb using gspec (the non-fork config) and it’s done twice. This is redundant (since NewBlockChain(..., progspec, ...) will run SetupGenesisBlock anyway) and can accidentally exercise config-compat upgrade paths instead of starting the DB with the intended pro-fork proConf. Prefer removing these commits, or commit progspec once if you want the DB pre-seeded.

Copilot uses AI. Check for mistakes.
Comment on lines 52 to +69
conDb := rawdb.NewMemoryDatabase()
gspec.MustCommit(conDb)
conConf := *params.TestChainConfig
conConf.DAOForkBlock = forkBlock
conConf.DAOForkSupport = false
conBc, _ := NewBlockChain(conDb, nil, &conConf, ethash.NewFaker(), vm.Config{})
congspec := &Genesis{
BaseFee: big.NewInt(params.InitialBaseFee),
Config: &conConf,
}
gspec.MustCommit(conDb)

conBc, _ := NewBlockChain(conDb, nil, congspec, ethash.NewFaker(), vm.Config{})
Copy link

Copilot AI Jan 30, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same issue as the pro-fork DB: conDb is pre-seeded by committing gspec twice (non-fork config) even though NewBlockChain(..., congspec, ...) will initialize genesis. This is redundant and can mask mistakes by turning this into a config upgrade scenario. Remove these commits or commit congspec once if needed.

Copilot uses AI. Check for mistakes.
return nil, genesisErr
// Here we determine genesis hash and active ChainConfig.
// We need these to figure out the consensus parameters and to set up history pruning.
chainConfig, _, err := core.LoadChainConfig(chainDb, config.Genesis)
Copy link

Copilot AI Jan 30, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

core.LoadChainConfig returns the stored chain config without running the same compatibility/upgrade logic as SetupGenesisBlock. Since core.NewBlockChainEx will still call SetupGenesisBlock internally and may update/rewind to a different config, eth.chainConfig (and the consensus engine created from it) can diverge from eth.blockchain.Config(). For XDPoS this is dangerous because the engine is initialized with the pre-SetupGenesisBlock config. Consider calling SetupGenesisBlock here (as before), or enhancing LoadChainConfig to return the effective config that SetupGenesisBlock would apply.

Suggested change
chainConfig, _, err := core.LoadChainConfig(chainDb, config.Genesis)
chainConfig, _, _, err := core.SetupGenesisBlock(chainDb, config.Genesis)

Copilot uses AI. Check for mistakes.
@gzliudan gzliudan force-pushed the move-genesis-state-init branch from 2ca266d to 3893a4f Compare January 30, 2026 10:07
@gzliudan gzliudan changed the title all: move genesis initialization to blockchain #25523 refactor(all): move genesis initialization to blockchain #25523 Feb 5, 2026
@gzliudan gzliudan force-pushed the move-genesis-state-init branch from 3893a4f to 5753e89 Compare February 10, 2026 05:20
@gzliudan gzliudan force-pushed the move-genesis-state-init branch from 5753e89 to 1984320 Compare February 10, 2026 05:41
@AnilChinchawale AnilChinchawale merged commit 85f2beb into XinFinOrg:dev-upgrade Feb 10, 2026
13 checks passed
@gzliudan gzliudan deleted the move-genesis-state-init branch February 10, 2026 11:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants