Example #1
0
// MustMakeChainConfigFromDb reads the chain configuration from the given database.
func MustMakeChainConfigFromDb(ctx *cli.Context, db ethdb.Database) *core.ChainConfig {
	// If the chain is already initialized, use any existing chain configs
	config := new(core.ChainConfig)

	genesis := core.GetBlock(db, core.GetCanonicalHash(db, 0))
	if genesis != nil {
		storedConfig, err := core.GetChainConfig(db, genesis.Hash())
		switch err {
		case nil:
			config = storedConfig
		case core.ChainConfigNotFoundErr:
			// No configs found, use empty, will populate below
		default:
			Fatalf("Could not make chain configuration: %v", err)
		}
	}
	// Set any missing fields due to them being unset or system upgrade
	if config.HomesteadBlock == nil {
		if ctx.GlobalBool(TestNetFlag.Name) {
			config.HomesteadBlock = params.TestNetHomesteadBlock
		} else {
			config.HomesteadBlock = params.MainNetHomesteadBlock
		}
	}
	if config.DAOForkBlock == nil {
		if ctx.GlobalBool(TestNetFlag.Name) {
			config.DAOForkBlock = params.TestNetDAOForkBlock
		} else {
			config.DAOForkBlock = params.MainNetDAOForkBlock
		}
		config.DAOForkSupport = true
	}
	// Force override any existing configs if explicitly requested
	switch {
	case ctx.GlobalBool(SupportDAOFork.Name):
		config.DAOForkSupport = true
	case ctx.GlobalBool(OpposeDAOFork.Name):
		config.DAOForkSupport = false
	}
	// Temporarilly display a proper message so the user knows which fork its on
	if !ctx.GlobalBool(TestNetFlag.Name) && (genesis == nil || genesis.Hash() == common.HexToHash("0xd4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3")) {
		choice := "SUPPORT"
		if !config.DAOForkSupport {
			choice = "OPPOSE"
		}
		current := fmt.Sprintf(" is currently configured to %s the DAO hard-fork!", choice)
		howtoswap := fmt.Sprintf("You can change your choice prior to block #%v with --support-dao-fork or --oppose-dao-fork.", config.DAOForkBlock)
		howtosync := fmt.Sprintf("After the hard-fork block #%v passed, changing chains requires a resync from scratch!", config.DAOForkBlock)
		separator := strings.Repeat("-", len(howtoswap))

		glog.V(logger.Warn).Info(separator)
		glog.V(logger.Warn).Info(current)
		glog.V(logger.Warn).Info(howtoswap)
		glog.V(logger.Warn).Info(howtosync)
		glog.V(logger.Warn).Info(separator)
	}
	return config
}
Example #2
0
func testDAOForkBlockNewChain(t *testing.T, testnet bool, genesis string, votes [][2]bool, expectBlock *big.Int, expectVote bool) {
	// Create a temporary data directory to use and inspect later
	datadir := tmpdir(t)
	defer os.RemoveAll(datadir)

	// Start a  instance with the requested flags set and immediately terminate
	if genesis != "" {
		json := filepath.Join(datadir, "genesis.json")
		if err := ioutil.WriteFile(json, []byte(genesis), 0600); err != nil {
			t.Fatalf("failed to write genesis file: %v", err)
		}
		runGeth(t, "--datadir", datadir, "init", json).cmd.Wait()
	}
	for _, vote := range votes {
		args := []string{"--port", "0", "--maxpeers", "0", "--nodiscover", "--nat", "none", "--ipcdisable", "--datadir", datadir}
		if testnet {
			args = append(args, "--testnet")
		}
		if vote[0] {
			args = append(args, "--support-dao-fork")
		}
		if vote[1] {
			args = append(args, "--oppose-dao-fork")
		}
		geth := runGeth(t, append(args, []string{"--exec", "2+2", "console"}...)...)
		geth.cmd.Wait()
	}
	// Retrieve the DAO config flag from the database
	path := filepath.Join(datadir, "chaindata")
	if testnet && genesis == "" {
		path = filepath.Join(datadir, "testnet", "chaindata")
	}
	db, err := ethdb.NewLDBDatabase(path, 0, 0)
	if err != nil {
		t.Fatalf("failed to open test database: %v", err)
	}
	defer db.Close()

	genesisHash := common.HexToHash("0xd4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3")
	if testnet {
		genesisHash = common.HexToHash("0x0cd786a2425d16f152c658316c423e6ce1181e15c3295826d7c9904cba9ce303")
	}
	if genesis != "" {
		genesisHash = daoGenesisHash
	}
	config, err := core.GetChainConfig(db, genesisHash)
	if err != nil {
		t.Fatalf("failed to retrieve chain config: %v", err)
	}
	// Validate the DAO hard-fork block number against the expected value
	if config.DAOForkBlock == nil {
		if expectBlock != nil {
			t.Errorf("dao hard-fork block mismatch: have nil, want %v", expectBlock)
		}
	} else if expectBlock == nil {
		t.Errorf("dao hard-fork block mismatch: have %v, want nil", config.DAOForkBlock)
	} else if config.DAOForkBlock.Cmp(expectBlock) != 0 {
		t.Errorf("dao hard-fork block mismatch: have %v, want %v", config.DAOForkBlock, expectBlock)
	}
	if config.DAOForkSupport != expectVote {
		t.Errorf("dao hard-fork support mismatch: have %v, want %v", config.DAOForkSupport, expectVote)
	}
}