Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
9 changes: 8 additions & 1 deletion cmd/geth/chaincmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ var (
ArgsUsage: "<genesisPath>",
Flags: []cli.Flag{
utils.DataDirFlag,
utils.OverrideMorph203TimeFlag,
},
Category: "BLOCKCHAIN COMMANDS",
Description: `
Expand Down Expand Up @@ -208,12 +209,18 @@ func initGenesis(ctx *cli.Context) error {
stack, _ := makeConfigNode(ctx)
defer stack.Close()

var overrides core.ChainOverrides
if ctx.IsSet(utils.OverrideMorph203TimeFlag.Name) {
v := ctx.Uint64(utils.OverrideMorph203TimeFlag.Name)
overrides.Morph203Time = &v
}

for _, name := range []string{"chaindata", "lightchaindata"} {
chaindb, err := stack.OpenDatabase(name, 0, 0, "", false)
if err != nil {
utils.Fatalf("Failed to open database: %v", err)
}
_, hash, err := core.SetupGenesisBlock(chaindb, genesis)
_, hash, err := core.SetupGenesisBlockWithOverride(chaindb, genesis, &overrides)
if err != nil {
utils.Fatalf("Failed to write genesis block: %v", err)
}
Expand Down
5 changes: 5 additions & 0 deletions cmd/geth/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,11 @@ func makeFullNode(ctx *cli.Context) (*node.Node, ethapi.Backend) {
if ctx.GlobalIsSet(utils.OverrideArrowGlacierFlag.Name) {
cfg.Eth.OverrideArrowGlacier = new(big.Int).SetUint64(ctx.GlobalUint64(utils.OverrideArrowGlacierFlag.Name))
}
if ctx.GlobalIsSet(utils.OverrideMorph203TimeFlag.Name) {
v := ctx.Uint64(utils.OverrideMorph203TimeFlag.Name)
cfg.Eth.OverrideMorph203Time = &v
}

backend, _ := utils.RegisterEthService(stack, &cfg.Eth)

// Configure log filter RPC API.
Expand Down
1 change: 1 addition & 0 deletions cmd/geth/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ var (
utils.USBFlag,
utils.SmartCardDaemonPathFlag,
utils.OverrideArrowGlacierFlag,
utils.OverrideMorph203TimeFlag,
utils.EthashCacheDirFlag,
utils.EthashCachesInMemoryFlag,
utils.EthashCachesOnDiskFlag,
Expand Down
4 changes: 4 additions & 0 deletions cmd/utils/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,10 @@ var (
Name: "override.arrowglacier",
Usage: "Manually specify Arrow Glacier fork-block, overriding the bundled setting",
}
OverrideMorph203TimeFlag = &cli.Uint64Flag{
Name: "override.morph203time",
Usage: "Manually specify the Morph203 fork timestamp, overriding the bundled setting",
}
// Light server and client settings
LightServeFlag = cli.IntFlag{
Name: "light.serve",
Expand Down
46 changes: 38 additions & 8 deletions core/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,23 @@ func (e *GenesisMismatchError) Error() string {
return fmt.Sprintf("database contains incompatible genesis (have %x, new %x)", e.Stored, e.New)
}

// ChainOverrides contains the changes to chain config
// Typically, these modifications involve hardforks that are not enabled on the BSC mainnet, intended for testing purposes.
type ChainOverrides struct {
Morph203Time *uint64
}

// apply applies the chain overrides on the supplied chain config.
func (o *ChainOverrides) apply(cfg *params.ChainConfig) error {
if o == nil || cfg == nil {
return nil
}
if o.Morph203Time != nil {
cfg.Morph203Time = o.Morph203Time
}
return cfg.CheckConfigForkOrder()
}

// SetupGenesisBlock writes or updates the genesis block in db.
// The block that will be used is:
//
Expand All @@ -159,7 +176,7 @@ func SetupGenesisBlock(db ethdb.Database, genesis *Genesis) (*params.ChainConfig
return SetupGenesisBlockWithOverride(db, genesis, nil)
}

func SetupGenesisBlockWithOverride(db ethdb.Database, genesis *Genesis, overrideArrowGlacier *big.Int) (*params.ChainConfig, common.Hash, error) {
func SetupGenesisBlockWithOverride(db ethdb.Database, genesis *Genesis, overrides *ChainOverrides) (*params.ChainConfig, common.Hash, error) {
if genesis != nil && genesis.Config == nil {
return params.AllEthashProtocolChanges, common.Hash{}, errGenesisNoConfig
}
Expand All @@ -172,6 +189,10 @@ func SetupGenesisBlockWithOverride(db ethdb.Database, genesis *Genesis, override
} else {
log.Info("Writing custom genesis block")
}
if err := overrides.apply(genesis.Config); err != nil {
return nil, common.Hash{}, err
}

block, err := genesis.Commit(db)
if err != nil {
return genesis.Config, common.Hash{}, err
Expand Down Expand Up @@ -232,13 +253,18 @@ func SetupGenesisBlockWithOverride(db ethdb.Database, genesis *Genesis, override
}
// Get the existing chain configuration.
newcfg := genesis.configOrDefault(stored)
if overrideArrowGlacier != nil {
newcfg.ArrowGlacierBlock = overrideArrowGlacier
if err := overrides.apply(newcfg); err != nil {
return nil, common.Hash{}, err
}
if err := newcfg.CheckConfigForkOrder(); err != nil {
return newcfg, common.Hash{}, err
}

storedcfg := rawdb.ReadChainConfig(db, stored)
if err := overrides.apply(storedcfg); err != nil {
return nil, common.Hash{}, err
}

if storedcfg == nil {
log.Warn("Found genesis block without chain config")
rawdb.WriteChainConfig(db, stored, newcfg)
Expand All @@ -262,12 +288,12 @@ func SetupGenesisBlockWithOverride(db ethdb.Database, genesis *Genesis, override
}
// Check config compatibility and write the config. Compatibility errors
// are returned to the caller unless we're already at block zero.
height := rawdb.ReadHeaderNumber(db, rawdb.ReadHeadHeaderHash(db))
if height == nil {
return newcfg, stored, fmt.Errorf("missing block number for head header hash")
head := rawdb.ReadHeadHeader(db)
if head == nil {
return nil, common.Hash{}, errors.New("missing head header")
}
compatErr := storedcfg.CheckCompatible(newcfg, *height)
if compatErr != nil && *height != 0 && compatErr.RewindTo != 0 {
compatErr := storedcfg.CheckCompatible(newcfg, head.Number.Uint64(), head.Time)
if compatErr != nil && ((head.Number.Uint64() != 0 && compatErr.RewindToBlock != 0) || (head.Time != 0 && compatErr.RewindToTime != 0)) {
return newcfg, stored, compatErr
}
rawdb.WriteChainConfig(db, stored, newcfg)
Expand All @@ -288,6 +314,10 @@ func (g *Genesis) configOrDefault(ghash common.Hash) *params.ChainConfig {
return params.RinkebyChainConfig
case ghash == params.GoerliGenesisHash:
return params.GoerliChainConfig
case ghash == params.MorphMainnetGenesisHash:
return params.MorphMainnetChainConfig
case ghash == params.MorphHoleskyGenesisHash:
return params.MorphHoleskyChainConfig
default:
return params.AllEthashProtocolChanges
}
Expand Down
8 changes: 4 additions & 4 deletions core/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,10 @@ func TestSetupGenesis(t *testing.T) {
wantHash: customghash,
wantConfig: customg.Config,
wantErr: &params.ConfigCompatError{
What: "Homestead fork block",
StoredConfig: big.NewInt(2),
NewConfig: big.NewInt(3),
RewindTo: 1,
What: "Homestead fork block",
StoredBlock: big.NewInt(2),
NewBlock: big.NewInt(3),
RewindToBlock: 1,
},
},
}
Expand Down
10 changes: 8 additions & 2 deletions eth/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,13 @@ func New(stack *node.Node, config *ethconfig.Config) (*Ethereum, error) {
if err != nil {
return nil, err
}
chainConfig, genesisHash, genesisErr := core.SetupGenesisBlockWithOverride(chainDb, config.Genesis, config.OverrideArrowGlacier)

// Override the chain config with provided settings.
var overrides core.ChainOverrides
if config.OverrideMorph203Time != nil {
overrides.Morph203Time = config.OverrideMorph203Time
}
chainConfig, genesisHash, genesisErr := core.SetupGenesisBlockWithOverride(chainDb, config.Genesis, &overrides)
if _, ok := genesisErr.(*params.ConfigCompatError); genesisErr != nil && !ok {
return nil, genesisErr
}
Expand Down Expand Up @@ -200,7 +206,7 @@ func New(stack *node.Node, config *ethconfig.Config) (*Ethereum, error) {
// Rewind the chain in case of an incompatible config upgrade.
if compat, ok := genesisErr.(*params.ConfigCompatError); ok {
log.Warn("Rewinding chain to upgrade configuration", "err", compat)
eth.blockchain.SetHead(compat.RewindTo)
eth.blockchain.SetHead(compat.RewindToBlock)
rawdb.WriteChainConfig(chainDb, genesisHash, chainConfig)
}
eth.bloomIndexer.Start(eth.blockchain)
Expand Down
3 changes: 3 additions & 0 deletions eth/ethconfig/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,9 @@ type Config struct {

// Max block range for eth_getLogs api method
MaxBlockRange int64

// Morph203Time override
OverrideMorph203Time *uint64 `toml:",omitempty"`
}

// CreateConsensusEngine creates a consensus engine for the given chain config.
Expand Down
12 changes: 12 additions & 0 deletions eth/ethconfig/gen_config.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 8 additions & 2 deletions les/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,13 @@ func New(stack *node.Node, config *ethconfig.Config) (*LightEthereum, error) {
if err != nil {
return nil, err
}
chainConfig, genesisHash, genesisErr := core.SetupGenesisBlockWithOverride(chainDb, config.Genesis, config.OverrideArrowGlacier)

// Override the chain config with provided settings.
var overrides core.ChainOverrides
if config.OverrideMorph203Time != nil {
overrides.Morph203Time = config.OverrideMorph203Time
}
chainConfig, genesisHash, genesisErr := core.SetupGenesisBlockWithOverride(chainDb, config.Genesis, &overrides)
if _, isCompat := genesisErr.(*params.ConfigCompatError); genesisErr != nil && !isCompat {
return nil, genesisErr
}
Expand Down Expand Up @@ -161,7 +167,7 @@ func New(stack *node.Node, config *ethconfig.Config) (*LightEthereum, error) {
// Rewind the chain in case of an incompatible config upgrade.
if compat, ok := genesisErr.(*params.ConfigCompatError); ok {
log.Warn("Rewinding chain to upgrade configuration", "err", compat)
leth.blockchain.SetHead(compat.RewindTo)
leth.blockchain.SetHead(compat.RewindToBlock)
rawdb.WriteChainConfig(chainDb, genesisHash, chainConfig)
}

Expand Down
Loading