Skip to content

Commit

Permalink
all: disallow launching Geth in legacy PoW mode
Browse files Browse the repository at this point in the history
  • Loading branch information
karalabe committed May 3, 2023
1 parent 04a1702 commit 003cce1
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 32 deletions.
1 change: 1 addition & 0 deletions cmd/devp2p/internal/ethtest/testdata/genesis.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"eip155Block": 0,
"eip158Block": 0,
"byzantiumBlock": 0,
"terminalTotalDifficultyPassed": true,
"ethash": {}
},
"nonce": "0xdeadbeefdeadbeef",
Expand Down
17 changes: 11 additions & 6 deletions cmd/geth/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ var customGenesisTests = []struct {
"mixhash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
"parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
"timestamp" : "0x00",
"config" : {}
"config" : {
"terminalTotalDifficultyPassed": true
}
}`,
query: "eth.getBlock(0).nonce",
result: "0x0000000000001338",
Expand All @@ -59,9 +61,10 @@ var customGenesisTests = []struct {
"parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
"timestamp" : "0x00",
"config" : {
"homesteadBlock" : 42,
"daoForkBlock" : 141,
"daoForkSupport" : true
"homesteadBlock" : 42,
"daoForkBlock" : 141,
"daoForkSupport" : true,
"terminalTotalDifficultyPassed" : true
}
}`,
query: "eth.getBlock(0).nonce",
Expand Down Expand Up @@ -111,8 +114,10 @@ func TestCustomBackend(t *testing.T) {
"mixhash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
"parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
"timestamp" : "0x00",
"config" : {}
}`
"config" : {
"terminalTotalDifficultyPassed": true
}
}`
type backendTest struct {
initArgs []string
initExpect string
Expand Down
7 changes: 5 additions & 2 deletions cmd/utils/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -2116,11 +2116,14 @@ func MakeChain(ctx *cli.Context, stack *node.Node, readonly bool) (*core.BlockCh
gspec = MakeGenesis(ctx)
chainDb = MakeChainDatabase(ctx, stack, readonly)
)
cliqueConfig, err := core.LoadCliqueConfig(chainDb, gspec)
config, err := core.LoadChainConfig(chainDb, gspec)
if err != nil {
Fatalf("%v", err)
}
engine, err := ethconfig.CreateConsensusEngine(config, chainDb)
if err != nil {
Fatalf("%v", err)
}
engine := ethconfig.CreateConsensusEngine(cliqueConfig, chainDb)
if gcmode := ctx.String(GCModeFlag.Name); gcmode != "full" && gcmode != "archive" {
Fatalf("--%s must be either 'full' or 'archive'", GCModeFlag.Name)
}
Expand Down
19 changes: 8 additions & 11 deletions core/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -379,22 +379,20 @@ func SetupGenesisBlockWithOverride(db ethdb.Database, triedb *trie.Database, gen
return newcfg, stored, nil
}

// LoadCliqueConfig loads the stored clique config if the chain config
// is already present in database, otherwise, return the config in the
// provided genesis specification. Note the returned clique config can
// be nil if we are not in the clique network.
func LoadCliqueConfig(db ethdb.Database, genesis *Genesis) (*params.CliqueConfig, error) {
// LoadChainConfig loads the stored chain config if it is already present in
// database, otherwise, return the config in the provided genesis specification.
func LoadChainConfig(db ethdb.Database, genesis *Genesis) (*params.ChainConfig, error) {
// Load the stored chain config from the database. It can be nil
// in case the database is empty. Notably, we only care about the
// chain config corresponds to the canonical chain.
stored := rawdb.ReadCanonicalHash(db, 0)
if stored != (common.Hash{}) {
storedcfg := rawdb.ReadChainConfig(db, stored)
if storedcfg != nil {
return storedcfg.Clique, nil
return storedcfg, nil
}
}
// Load the clique config from the provided genesis specification.
// Load the config from the provided genesis specification
if genesis != nil {
// Reject invalid genesis spec without valid chain config
if genesis.Config == nil {
Expand All @@ -407,12 +405,11 @@ func LoadCliqueConfig(db ethdb.Database, genesis *Genesis) (*params.CliqueConfig
if stored != (common.Hash{}) && genesis.ToBlock().Hash() != stored {
return nil, &GenesisMismatchError{stored, genesis.ToBlock().Hash()}
}
return genesis.Config.Clique, nil
return genesis.Config, nil
}
// There is no stored chain config and no new config provided,
// In this case the default chain config(mainnet) will be used,
// namely ethash is the specified consensus engine, return nil.
return nil, nil
// In this case the default chain config(mainnet) will be used
return params.MainnetChainConfig, nil
}

func (g *Genesis) configOrDefault(ghash common.Hash) *params.ChainConfig {
Expand Down
8 changes: 5 additions & 3 deletions eth/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,14 @@ func New(stack *node.Node, config *ethconfig.Config) (*Ethereum, error) {
log.Error("Failed to recover state", "error", err)
}
// Transfer mining-related config to the ethash config.
cliqueConfig, err := core.LoadCliqueConfig(chainDb, config.Genesis)
chainConfig, err := core.LoadChainConfig(chainDb, config.Genesis)
if err != nil {
return nil, err
}
engine, err := ethconfig.CreateConsensusEngine(chainConfig, chainDb)
if err != nil {
return nil, err
}
engine := ethconfig.CreateConsensusEngine(cliqueConfig, chainDb)

eth := &Ethereum{
config: config,
merger: consensus.NewMerger(chainDb),
Expand Down
22 changes: 14 additions & 8 deletions eth/ethconfig/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package ethconfig

import (
"errors"
"time"

"github.com/ethereum/go-ethereum/common"
Expand Down Expand Up @@ -163,14 +164,19 @@ type Config struct {
OverrideCancun *uint64 `toml:",omitempty"`
}

// CreateConsensusEngine creates a consensus engine for the given chain configuration.
func CreateConsensusEngine(cliqueConfig *params.CliqueConfig, db ethdb.Database) consensus.Engine {
// CreateConsensusEngine creates a consensus engine for the given chain config.
// Clique is allowed for now to live standalone, but ethash is forbidden and can
// only exist on already merged networks.
func CreateConsensusEngine(config *params.ChainConfig, db ethdb.Database) (consensus.Engine, error) {
// If proof-of-authority is requested, set it up
var engine consensus.Engine
if cliqueConfig != nil {
engine = clique.New(cliqueConfig, db)
} else {
engine = ethash.NewFaker()
if config.Clique != nil {
return beacon.New(clique.New(config.Clique, db)), nil
}
return beacon.New(engine)
// If defaulting to proof-of-work, enforce an already merged network since
// we cannot run PoW algorithms and more, so we cannot even follow a chain
// not coordinated by a beacon node.
if !config.TerminalTotalDifficultyPassed {
return nil, errors.New("ethash is only supported as a historical component of already merged networks")
}
return beacon.New(ethash.NewFaker()), nil
}
6 changes: 5 additions & 1 deletion les/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ func New(stack *node.Node, config *ethconfig.Config) (*LightEthereum, error) {
if _, isCompat := genesisErr.(*params.ConfigCompatError); genesisErr != nil && !isCompat {
return nil, genesisErr
}
engine, err := ethconfig.CreateConsensusEngine(chainConfig, chainDb)
if err != nil {
return nil, err
}
log.Info("")
log.Info(strings.Repeat("-", 153))
for _, line := range strings.Split(chainConfig.Description(), "\n") {
Expand All @@ -126,7 +130,7 @@ func New(stack *node.Node, config *ethconfig.Config) (*LightEthereum, error) {
reqDist: newRequestDistributor(peers, &mclock.System{}),
accountManager: stack.AccountManager(),
merger: merger,
engine: ethconfig.CreateConsensusEngine(chainConfig.Clique, chainDb),
engine: engine,
bloomRequests: make(chan chan *bloombits.Retrieval),
bloomIndexer: core.NewBloomIndexer(chainDb, params.BloomBitsBlocksClient, params.HelperTrieConfirmations),
p2pServer: stack.Server(),
Expand Down
2 changes: 1 addition & 1 deletion params/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ var (
CancunTime: nil,
PragueTime: nil,
TerminalTotalDifficulty: nil,
TerminalTotalDifficultyPassed: false,
TerminalTotalDifficultyPassed: true,
Ethash: new(EthashConfig),
Clique: nil,
}
Expand Down

0 comments on commit 003cce1

Please sign in to comment.