cmd, core, eth, params: implement flags to control dao fork blocks

This commit is contained in:
Péter Szilágyi
2016-07-07 16:04:34 +03:00
parent aa1e052cb4
commit 6060e098c9
7 changed files with 354 additions and 46 deletions

View File

@ -126,10 +126,6 @@ var (
Name: "dev",
Usage: "Developer mode: pre-configured private network with several debugging flags",
}
GenesisFileFlag = cli.StringFlag{
Name: "genesis",
Usage: "Insert/overwrite the genesis block (JSON format)",
}
IdentityFlag = cli.StringFlag{
Name: "identity",
Usage: "Custom node name",
@ -161,6 +157,15 @@ var (
Name: "lightkdf",
Usage: "Reduce key-derivation RAM & CPU usage at some expense of KDF strength",
}
// Fork settings
SupportDAOFork = cli.BoolFlag{
Name: "support-dao-fork",
Usage: "Updates the chain rules to support the DAO hard-fork",
}
OpposeDAOFork = cli.BoolFlag{
Name: "oppose-dao-fork",
Usage: "Updates the chain rules to oppose the DAO hard-fork",
}
// Miner settings
// TODO: refactor CPU vs GPU mining flags
MiningEnabledFlag = cli.BoolFlag{
@ -534,20 +539,6 @@ func MakeWSRpcHost(ctx *cli.Context) string {
return ctx.GlobalString(WSListenAddrFlag.Name)
}
// MakeGenesisBlock loads up a genesis block from an input file specified in the
// command line, or returns the empty string if none set.
func MakeGenesisBlock(ctx *cli.Context) string {
genesis := ctx.GlobalString(GenesisFileFlag.Name)
if genesis == "" {
return ""
}
data, err := ioutil.ReadFile(genesis)
if err != nil {
Fatalf("Failed to load custom genesis file: %v", err)
}
return string(data)
}
// MakeDatabaseHandles raises out the number of allowed file handles per process
// for Geth and returns half of the allowance to assign to the database.
func MakeDatabaseHandles() int {
@ -689,7 +680,6 @@ func MakeSystemNode(name, version string, relconf release.Config, extra []byte,
ethConf := &eth.Config{
ChainConfig: MustMakeChainConfig(ctx),
Genesis: MakeGenesisBlock(ctx),
FastSync: ctx.GlobalBool(FastSyncFlag.Name),
BlockChainVersion: ctx.GlobalInt(BlockchainVersionFlag.Name),
DatabaseCache: ctx.GlobalInt(CacheFlag.Name),
@ -722,17 +712,13 @@ func MakeSystemNode(name, version string, relconf release.Config, extra []byte,
if !ctx.GlobalIsSet(NetworkIdFlag.Name) {
ethConf.NetworkId = 1
}
if !ctx.GlobalIsSet(GenesisFileFlag.Name) {
ethConf.Genesis = core.OlympicGenesisBlock()
}
ethConf.Genesis = core.OlympicGenesisBlock()
case ctx.GlobalBool(TestNetFlag.Name):
if !ctx.GlobalIsSet(NetworkIdFlag.Name) {
ethConf.NetworkId = 2
}
if !ctx.GlobalIsSet(GenesisFileFlag.Name) {
ethConf.Genesis = core.TestNetGenesisBlock()
}
ethConf.Genesis = core.TestNetGenesisBlock()
state.StartingNonce = 1048576 // (2**20)
case ctx.GlobalBool(DevModeFlag.Name):
@ -747,9 +733,7 @@ func MakeSystemNode(name, version string, relconf release.Config, extra []byte,
stackConf.ListenAddr = ":0"
}
// Override the Ethereum protocol configs
if !ctx.GlobalIsSet(GenesisFileFlag.Name) {
ethConf.Genesis = core.OlympicGenesisBlock()
}
ethConf.Genesis = core.OlympicGenesisBlock()
if !ctx.GlobalIsSet(GasPriceFlag.Name) {
ethConf.GasPrice = new(big.Int)
}
@ -813,24 +797,44 @@ func MustMakeChainConfig(ctx *cli.Context) *core.ChainConfig {
// MustMakeChainConfigFromDb reads the chain configuration from the given database.
func MustMakeChainConfigFromDb(ctx *cli.Context, db ethdb.Database) *core.ChainConfig {
genesis := core.GetBlock(db, core.GetCanonicalHash(db, 0), 0)
if genesis != nil {
// Existing genesis block, use stored config if available.
// If the chain is already initialized, use any existing chain configs
if genesis := core.GetBlock(db, core.GetCanonicalHash(db, 0), 0); genesis != nil {
storedConfig, err := core.GetChainConfig(db, genesis.Hash())
if err == nil {
// Force override any existing configs if explicitly requested
switch {
case storedConfig.DAOForkBlock == nil && ctx.GlobalBool(SupportDAOFork.Name) && ctx.GlobalBool(TestNetFlag.Name):
storedConfig.DAOForkBlock = params.TestNetDAOForkBlock
case storedConfig.DAOForkBlock == nil && ctx.GlobalBool(SupportDAOFork.Name):
storedConfig.DAOForkBlock = params.MainNetDAOForkBlock
case ctx.GlobalBool(OpposeDAOFork.Name):
storedConfig.DAOForkBlock = nil
}
return storedConfig
} else if err != core.ChainConfigNotFoundErr {
Fatalf("Could not make chain configuration: %v", err)
}
}
var homesteadBlockNo *big.Int
// If the chain is uninitialized nor no configs are present, create one
var homesteadBlock *big.Int
if ctx.GlobalBool(TestNetFlag.Name) {
homesteadBlockNo = params.TestNetHomesteadBlock
homesteadBlock = params.TestNetHomesteadBlock
} else {
homesteadBlockNo = params.MainNetHomesteadBlock
homesteadBlock = params.MainNetHomesteadBlock
}
var daoForkBlock *big.Int
switch {
case ctx.GlobalBool(SupportDAOFork.Name) && ctx.GlobalBool(TestNetFlag.Name):
daoForkBlock = params.TestNetDAOForkBlock
case ctx.GlobalBool(SupportDAOFork.Name):
daoForkBlock = params.MainNetDAOForkBlock
case ctx.GlobalBool(OpposeDAOFork.Name):
daoForkBlock = nil
}
return &core.ChainConfig{
HomesteadBlock: homesteadBlock,
DAOForkBlock: daoForkBlock,
}
return &core.ChainConfig{HomesteadBlock: homesteadBlockNo}
}
// MakeChainDatabase open an LevelDB using the flags passed to the client and will hard crash if it fails.