Skip to content

feat: centralize chain config and harden genesis startup - #2329

Merged
benjamin202410 merged 1 commit into
XinFinOrg:dev-upgradefrom
gzliudan:move-common-global-var
May 27, 2026
Merged

feat: centralize chain config and harden genesis startup#2329
benjamin202410 merged 1 commit into
XinFinOrg:dev-upgradefrom
gzliudan:move-common-global-var

Conversation

@gzliudan

@gzliudan gzliudan commented Apr 24, 2026

Copy link
Copy Markdown
Collaborator

Proposed changes

Consolidate per-network constants into common/constants.go and common/constants.shared.go, dropping the build-tag constant matrix (constants.{mainnet,testnet,devnet,local,all}.go).

Move gas pricing helpers from common to params and make them ChainConfig-driven, so fork-aware pricing is derived from the active config instead of process-wide globals.

Split params/config.go into focused files (config_networks, config_forks, config_xdpos, config_backfill, config_compat) and
extend ChainConfig with XDC fork switches and system-contract addresses. Add clone and semantic-equality helpers, and preserve explicit-zero JSON fields via presence-aware marshal/unmarshal.

Introduce core/startup with an explicit decision flow, and rework genesis loading/recovery to:

  • hydrate and validate config by network class,
  • backfill only missing fields,
  • preserve compatibility errors,
  • allow same-hash custom override only in guarded recovery mode.

Refresh bundled genesis JSON files to the new schema and thread ChainConfig through consensus, core, eth, miner, txpool, tracers, the simulated backend and related helpers.

Add regression coverage for built-in / local / custom / same-hash startup paths and denylist validation, and document the operator upgrade/repair workflow in docs/upgrade.md.

Types of changes

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

  • build: Changes that affect the build system or external dependencies
  • ci: Changes to CI configuration files and scripts
  • chore: Changes that don't change source code or tests
  • docs: Documentation only changes
  • feat: A new feature
  • fix: A bug fix
  • perf: A code change that improves performance
  • refactor: A code change that neither fixes a bug nor adds a feature
  • revert: Revert something
  • style: Changes that do not affect the meaning of the code
  • test: Adding missing tests or correcting existing tests

Impacted Components

Which parts of the codebase does this PR touch?
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
  • Tested on a private network from the genesis block and monitored the chain operating correctly for multiple epochs.
  • 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 April 24, 2026 07:28
@coderabbitai

coderabbitai Bot commented Apr 24, 2026

Copy link
Copy Markdown

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.

⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: fac219f6-7537-4cec-af0a-54c1023cedcb

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

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.

@gzliudan gzliudan changed the title fix(common,params,core,eth): make fork switches chainconfig-driven refactor(common,params,core,eth): make fork switches chainconfig-driven Apr 24, 2026
@gzliudan gzliudan changed the title refactor(common,params,core,eth): make fork switches chainconfig-driven refactor(common,core,eth,params): make fork switches chainconfig-driven Apr 24, 2026
@gzliudan
gzliudan force-pushed the move-common-global-var branch from c0ca61f to b87a72c Compare April 24, 2026 07:29

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

This PR refactors fork activation to be fully params.ChainConfig-driven (removing reliance on common fallback globals), normalizes XDC network configs to explicit per-network fork block values (using nil for unscheduled forks), and updates multiple call sites/tests to operate without global fallback behavior.

Changes:

  • Move Berlin/London/Merge/Shanghai/EIP-1559/Cancun/Prague/Osaka/DynamicGasLimit activation to params.ChainConfig and remove common fallback usage.
  • Normalize XDC chain configs (mainnet/testnet/devnet/localnet) with explicit fork block declarations and update signer/genesis/state logic to rely on ChainConfig only.
  • Update and extend tests to assert explicit fork declarations and avoid relying on implicit global fallback activation.

Reviewed changes

Copilot reviewed 18 out of 18 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
params/config.go Adds explicit fork block fields to XDC configs; removes fallback logic in Description() and fork checks; introduces Localnet config and Localnet V2 config map.
params/config_test.go Adds tests ensuring XDC configs explicitly declare fork blocks and that activation ignores removed common fallbacks.
eth/filters/filter_test.go Adjusts test chain config to avoid Cancun/Prague/Osaka activation via config defaults.
eth/downloader/testchain_test.go Introduces a local test chain config with Cancun/Prague/Osaka disabled; uses it in genesis/signer creation.
eth/downloader/downloader_test.go Uses the shared testChainConfig for downloader tests.
eth/backend.go Switches common.CopyConstants call to use chainConfig.ChainID directly.
core/vm/gas_table_test.go Forces legacy fork behavior by nil-ing post-London fork blocks in test config copy.
core/types/transaction_signing.go Makes LatestSigner selection depend solely on ChainConfig fork pointers (no global fallback).
core/state_processor.go Removes fallback to common.PragueBlock when checking Prague activation.
core/genesis.go Canonicalizes localnet config by chain ID and expands logging around resolved configs; removes fallback-related comment text.
core/genesis_test.go Adds test asserting localnet config normalization and persistence.
contracts/randomize/randomize_test.go Moves to NewXDCSimulatedBackend with a legacy config to preserve legacy-tx signing path.
common/constants.all.go Removes fork block fields/globals (Berlin/London/Merge/Shanghai/EIP-1559/Cancun/Prague/Osaka/DynamicGasLimit) from constant set/copy paths.
common/constants.mainnet.go Removes fork block constants from mainnet constant set.
common/constants.testnet.go Removes fork block constants from testnet constant set.
common/constants.devnet.go Removes fork block constants from devnet constant set.
common/constants.local.go Removes fork block constants from local constant set.
cmd/XDC/chaincmd.go Adds nil-check for genesis.Config; canonicalizes localnet config when chain ID matches.

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

Comment thread params/config_test.go Outdated
Comment thread params/config.go Outdated
@gzliudan
gzliudan force-pushed the move-common-global-var branch 3 times, most recently from 298f45c to a732dbb Compare April 24, 2026 09:48
@gzliudan
gzliudan requested a review from Copilot April 24, 2026 09:49

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 36 out of 36 changed files in this pull request and generated 4 comments.


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

Comment thread params/config.go Outdated
Comment thread core/genesis.go Outdated
Comment thread cmd/XDC/chaincmd.go Outdated
Comment thread params/config_test.go Outdated
@gzliudan
gzliudan force-pushed the move-common-global-var branch 3 times, most recently from 0efd748 to 32c289c Compare April 24, 2026 12:06
@gzliudan
gzliudan requested a review from Copilot April 24, 2026 12:38

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 36 out of 36 changed files in this pull request and generated 3 comments.


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

Comment thread contracts/utils.go Outdated
Comment thread contracts/utils.go Outdated
Comment thread params/config.go Outdated
@gzliudan
gzliudan force-pushed the move-common-global-var branch 4 times, most recently from f6109e8 to 15b851a Compare April 24, 2026 16:55
@gzliudan
gzliudan requested a review from Copilot April 24, 2026 16:57

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 94 out of 94 changed files in this pull request and generated 1 comment.


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

Comment thread core/genesis.go Outdated

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 94 out of 94 changed files in this pull request and generated no new comments.

Comments suppressed due to low confidence (2)

core/token_validator.go:112

  • CallContractWithState now relies on StateDB.GetTRC21FeeCapacityFromState(), which in turn uses StateDB.TRC21IssuerSMC() (derived from StateDB.chainConfig). CallContractWithState never sets statedb's chain config, so if the caller passes a StateDB without SetChainConfig having been invoked, feeCapacity will be read from the zero address and token-fee behavior becomes incorrect. Consider setting statedb.SetChainConfig(chain.Config()) at the start of CallContractWithState (or otherwise ensuring the chain config is always attached before accessing TRC21 fee state).
    eth/tracers/api.go:285
  • TransactionToMessage can now return an error (e.g., missing token-fee fork fields in chainConfig, signer/sender issues). In traceChain the error is ignored (msg, _ := ...), but msg is then passed to traceTx without nil checks. If an error occurs this can lead to a nil dereference/panic inside traceTx/ApplyTransactionWithEVM. Please handle the error and record it into task.results[i] (similar to the traceTx error path), or abort tracing the block gracefully.

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

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 94 out of 94 changed files in this pull request and generated 1 comment.


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

Comment thread core/state/trc21_reader.go

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 103 out of 103 changed files in this pull request and generated 3 comments.


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

Comment thread contracts/trc21issuer/simulation/test/main.go Outdated
Comment thread contracts/trc21issuer/simulation/test/main.go Outdated
Comment thread contracts/trc21issuer/simulation/test/main.go Outdated

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 103 out of 104 changed files in this pull request and generated no new comments.

Comments suppressed due to low confidence (5)

core/state_transition.go:1

  • This can panic if st.evm.ChainConfig() is nil or if TIPTRC21FeeBlock is nil (nil pointer dereference during .Cmp). Consider guarding the comparison (e.g., check cfg != nil && cfg.TIPTRC21FeeBlock != nil) or using a ChainConfig helper method that encapsulates the nil-safe fork check.
    core/state_transition.go:1
  • These are newly introduced error strings that callers/tests appear to match via substring checks. To make this easier to handle robustly across the codebase, consider defining package-level sentinel errors (similar to txpool.ErrMissingChainConfig) and wrapping them (so callers can use errors.Is) rather than relying on string matching.
    core/genesis.go:1
  • This adds an unconditional Info log on every SetupGenesisBlock call (including tests and any code paths that call config loading/setup multiple times). Consider lowering this to Debug, or logging only when the config is written/changed or when compatErr/err is non-nil, to avoid noisy startup logs and large log volume in repeated setup/load scenarios.
    contracts/trc21issuer/trc21issuer_test.go:1
  • This test constructs and uses a cloned chainConfig (with at least TRC21IssuerSMC overridden) when creating the simulated backend, but computes expected fees using params.TestXDPoSMockChainConfig instead of the actual chainConfig in use. To avoid false positives/negatives if fee fork blocks differ in future edits, compute fees using the same config instance passed into the backend.
    contracts/trc21issuer/trc21issuer_test.go:1
  • This test constructs and uses a cloned chainConfig (with at least TRC21IssuerSMC overridden) when creating the simulated backend, but computes expected fees using params.TestXDPoSMockChainConfig instead of the actual chainConfig in use. To avoid false positives/negatives if fee fork blocks differ in future edits, compute fees using the same config instance passed into the backend.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 109 out of 110 changed files in this pull request and generated 3 comments.

Comments suppressed due to low confidence (1)

core/state/trc21_reader.go:161

  • UpdateTRC21Fee writes issuer token-capacity state and subtracts balance from the TRC21 issuer address. If TRC21IssuerSMC resolves to the zero address (e.g., chain config not set on StateDB), this will mutate account(0) and corrupt state. Add a guard to no-op or error when issuer is the zero address (or when chain config is missing).

Comment thread core/state/trc21_reader.go
Comment thread XDCxlending/lendingstate/relayer.go Outdated
Comment thread XDCx/tradingstate/relayer_state.go Outdated

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 112 out of 115 changed files in this pull request and generated 2 comments.

Comments suppressed due to low confidence (5)

eth/tracers/api.go:545

  • core.TransactionToMessage now returns an error in some cases. Ignoring it here (msg, _ := ...) can result in a nil msg and a panic in ApplyMessage when tracing blocks whose chain config is missing required fork fields. Capture and handle the error (return it, or short-circuit with roots collected so far).
    eth/tracers/api.go:628
  • core.TransactionToMessage can fail now, but the error is ignored here. If it returns an error, msg will be nil and traceTx will likely panic. Please handle the error and return it (or store it in results[i] like other trace failures).
    eth/tracers/api.go:683
  • core.TransactionToMessage now returns an error for some chain-config dependent cases; this worker path ignores it. A nil msg can panic inside traceTx. Handle the error and set results[task.index] to an error result (or propagate the error to abort tracing).
    eth/tracers/api.go:726
  • core.TransactionToMessage errors are ignored here (msg, _ := ...). If it returns an error, the subsequent ApplyMessage will dereference a nil msg and panic. Handle the error and abort the trace loop cleanly (e.g., set failed and break).
    eth/state_accessor.go:250
  • core.TransactionToMessage now returns an error in some cases. This call ignores the error, which can leave msg nil and cause ApplyMessage to panic while replaying transactions to reach txIndex. Please handle and return the error (or wrap it with tx/hash context).

Comment thread eth/tracers/api.go
Comment thread eth/backend.go Outdated

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 112 out of 115 changed files in this pull request and generated 3 comments.

Comment thread common/gas.go Outdated
Comment thread core/state/trc21_reader.go Outdated
Comment thread core/blockchain.go

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 112 out of 115 changed files in this pull request and generated 4 comments.

Comment thread core/types/transaction.go
Comment thread core/types/transaction.go
Comment thread core/state_transition.go
Comment thread common/gas.go Outdated

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 113 out of 116 changed files in this pull request and generated 6 comments.

Comment thread core/vm/runtime/runtime.go Outdated
Comment thread core/chain_makers.go Outdated
Comment thread core/state/trc21_reader.go
Comment thread cmd/utils/flags.go Outdated
Comment thread core/blockchain.go Outdated
Comment thread eth/backend.go

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 115 out of 118 changed files in this pull request and generated 3 comments.

Comment thread XDCx/token.go Outdated
Comment thread consensus/XDPoS/engines/engine_v1/utils_test.go Outdated
Comment thread core/txpool/validation.go

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 118 out of 121 changed files in this pull request and generated 2 comments.

Comment thread eth/hooks/engine_v2_hooks.go Outdated
Comment thread core/token_validator.go

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 122 out of 125 changed files in this pull request and generated 2 comments.

Comment thread docs/xdc/admin/admin_nodeInfo_response.json
Comment thread eth/hooks/engine_v2_hooks.go Outdated

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 122 out of 125 changed files in this pull request and generated 2 comments.

Comment thread core/txpool/validation.go
Comment thread eth/backend.go Outdated

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 122 out of 125 changed files in this pull request and generated 2 comments.

Comment thread core/txpool/validation.go Outdated
Comment thread core/types/transaction.go

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 122 out of 125 changed files in this pull request and generated no new comments.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 122 out of 125 changed files in this pull request and generated 5 comments.

Comments suppressed due to low confidence (2)

ethclient/simulated/backend.go:1037

  • state.New can fail, but the returned error is ignored and the newly added SetChainConfig call dereferences b.pendingState unconditionally. Handle the error before using the StateDB so SendTransaction returns an error instead of panicking if the pending state cannot be reopened.
    ethclient/simulated/backend.go:1172
  • This ignores the error from state.New and then immediately dereferences b.pendingState. If rebuilding the adjusted pending state fails, AdjustTime will panic rather than returning the failure to the caller. Check and return the error before setting the chain config.

Comment thread docs/upgrade.md Outdated
Comment thread consensus/XDPoS/engines/engine_v1/engine.go Outdated
Comment thread docs/upgrade.md Outdated
Comment thread core/rawdb/accessors_metadata.go Outdated
Comment thread ethclient/simulated/backend.go Outdated

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot wasn't able to review this pull request because it exceeds the maximum number of lines (20,000). Try reducing the number of changed lines and requesting a review from Copilot again.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot wasn't able to review this pull request because it exceeds the maximum number of lines (20,000). Try reducing the number of changed lines and requesting a review from Copilot again.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 132 out of 135 changed files in this pull request and generated 10 comments.

Comments suppressed due to low confidence (1)

core/state/statedb.go:265

  • The comment says this accessor panics when the state/config/address is missing, but the implementation returns an error in those cases. Please update the doc comment so callers know they must handle the returned error rather than expect a panic.

Comment thread params/gas.go Outdated
Comment thread params/gas.go Outdated
Comment thread ethclient/simulated/backend.go Outdated
Comment thread core/state/statedb.go
Comment thread core/types/transaction.go
Comment thread core/types/transaction.go
Comment thread core/types/transaction.go
Comment thread core/state_transition.go
Comment thread core/blockchain.go Outdated
Comment thread core/genesis.go Outdated

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 135 out of 138 changed files in this pull request and generated 2 comments.

Comment thread core/txpool/legacypool/list.go Outdated
Comment thread core/txpool/legacypool/list_test.go Outdated

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot wasn't able to review this pull request because it exceeds the maximum number of lines (20,000). Try reducing the number of changed lines and requesting a review from Copilot again.

@gzliudan gzliudan mentioned this pull request May 20, 2026
25 tasks
Consolidate per-network constants into common/constants.go and
common/constants.shared.go, dropping the build-tag constant matrix
(constants.{mainnet,testnet,devnet,local,all}.go).

Move gas pricing helpers from common to params and make them
ChainConfig-driven, so fork-aware pricing is derived from the active
config instead of process-wide globals.

Split params/config.go into focused files (config_networks,
config_forks, config_xdpos, config_backfill, config_compat) and
extend ChainConfig with XDC fork switches and system-contract
addresses. Add clone and semantic-equality helpers, and preserve
explicit-zero JSON fields via presence-aware marshal/unmarshal.

Introduce core/startup with an explicit decision flow, and rework
genesis loading/recovery to:
  - hydrate and validate config by network class,
  - backfill only missing fields,
  - preserve compatibility errors,
  - allow same-hash custom override only in guarded recovery mode.

Refresh bundled genesis JSON files to the new schema and thread
ChainConfig through consensus, core, eth, miner, txpool, tracers,
the simulated backend and related helpers.

Add regression coverage for built-in / local / custom / same-hash
startup paths and denylist validation, and document the operator
upgrade/repair workflow in docs/upgrade.md.
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.

5 participants