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

cmd/utils: if dev-mode is enabled and db is pre-existing, validate that genesis configuration is compatible #28468

Merged
merged 6 commits into from
Nov 22, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
48 changes: 48 additions & 0 deletions cmd/utils/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -1646,6 +1646,45 @@ func CheckExclusive(ctx *cli.Context, args ...interface{}) {
}
}

// validateDeveloperGenesis asserts that a provided genesis can be used with dev mode:
// it requires pre-merge hard-forks and proof-of-stake to be activated at block 0
func validateDeveloperGenesis(genesis *core.Genesis) error {
checkForkIsBlock0 := func(forkName string, hfBlockNumber *big.Int) error {
if hfBlockNumber == nil {
return fmt.Errorf("%s: hard-fork not enabled", forkName)
} else if hfBlockNumber.Cmp(big.NewInt(0)) != 0 {
return fmt.Errorf("%s: hard-fork not activated in block 0", forkName)
}
return nil
}

config := genesis.Config
if err := checkForkIsBlock0("Homestead", config.HomesteadBlock); err != nil {
return err
} else if config.DAOForkBlock != nil {
return errors.New("DAO hardfork cannot be enabled")
} else if err := checkForkIsBlock0("EIP150", config.EIP150Block); err != nil {
return err
} else if err := checkForkIsBlock0("EIP155", config.EIP155Block); err != nil {
return err
} else if err := checkForkIsBlock0("EIP158", config.EIP158Block); err != nil {
return err
} else if err := checkForkIsBlock0("Byzantium", config.ByzantiumBlock); err != nil {
return err
} else if err := checkForkIsBlock0("Constantinople", config.ConstantinopleBlock); err != nil {
return err
} else if err := checkForkIsBlock0("Istanbul", config.IstanbulBlock); err != nil {
return err
} else if err := checkForkIsBlock0("Berlin", config.BerlinBlock); err != nil {
return err
} else if err := checkForkIsBlock0("London", config.LondonBlock); err != nil {
return err
} else if !config.TerminalTotalDifficultyPassed {
return errors.New("terminal total difficulty must be passed")
}
return nil
}

// SetEthConfig applies eth-related command line flags to the config.
func SetEthConfig(ctx *cli.Context, stack *node.Node, cfg *ethconfig.Config) {
// Avoid conflicting network flags
Expand Down Expand Up @@ -1870,6 +1909,15 @@ func SetEthConfig(ctx *cli.Context, stack *node.Node, cfg *ethconfig.Config) {
chaindb := tryMakeReadOnlyDatabase(ctx, stack)
if rawdb.ReadCanonicalHash(chaindb, 0) != (common.Hash{}) {
cfg.Genesis = nil // fallback to db content

var genesis *core.Genesis
var err error
if genesis, err = core.ReadGenesis(chaindb); err != nil {
Fatalf("could not read genesis from database: %v", err)
}
if err = validateDeveloperGenesis(genesis); err != nil {
Fatalf("genesis configuration incompatible with development mode: %v", err)
}
jwasinger marked this conversation as resolved.
Show resolved Hide resolved
}
chaindb.Close()
}
Expand Down
4 changes: 0 additions & 4 deletions eth/catalyst/simulated_beacon.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,6 @@ type SimulatedBeacon struct {
}

func NewSimulatedBeacon(period uint64, eth *eth.Ethereum) (*SimulatedBeacon, error) {
chainConfig := eth.APIBackend.ChainConfig()
if !chainConfig.IsDevMode {
return nil, errors.New("incompatible pre-existing chain configuration")
}
block := eth.BlockChain().CurrentBlock()
current := engine.ForkchoiceStateV1{
HeadBlockHash: block.Hash(),
Expand Down
6 changes: 2 additions & 4 deletions params/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,6 @@ var (
ShanghaiTime: newUint64(0),
TerminalTotalDifficulty: big.NewInt(0),
TerminalTotalDifficultyPassed: true,
IsDevMode: true,
}

// AllCliqueProtocolChanges contains every protocol change (EIPs) introduced
Expand Down Expand Up @@ -329,9 +328,8 @@ type ChainConfig struct {
TerminalTotalDifficultyPassed bool `json:"terminalTotalDifficultyPassed,omitempty"`

// Various consensus engines
Ethash *EthashConfig `json:"ethash,omitempty"`
Clique *CliqueConfig `json:"clique,omitempty"`
IsDevMode bool `json:"isDev,omitempty"`
Ethash *EthashConfig `json:"ethash,omitempty"`
Clique *CliqueConfig `json:"clique,omitempty"`
}

// EthashConfig is the consensus engine configs for proof-of-work based sealing.
Expand Down