Skip to content

Commit

Permalink
core: added basic chain configuration
Browse files Browse the repository at this point in the history
Added chain configuration options and write out during genesis database
insertion. If no "config" was found, nothing is written to the database.

Configurations are written on a per genesis base. This means
that any chain (which is identified by it's genesis hash) can have their
own chain settings.
  • Loading branch information
obscuren committed Mar 31, 2016
1 parent 10d3466 commit f0cbebb
Show file tree
Hide file tree
Showing 55 changed files with 732 additions and 431 deletions.
11 changes: 8 additions & 3 deletions accounts/abi/bind/backends/simulated.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,15 @@ import (
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/state"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/core/vm"
"github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/event"
"github.com/ethereum/go-ethereum/params"
)

// Default chain configuration which sets homestead phase at block 0 (i.e. no frontier)
var chainConfig = &core.ChainConfig{HomesteadBlock: params.MainNetHomesteadBlock}

// This nil assignment ensures compile time that SimulatedBackend implements bind.ContractBackend.
var _ bind.ContractBackend = (*SimulatedBackend)(nil)

Expand All @@ -46,7 +51,7 @@ type SimulatedBackend struct {
func NewSimulatedBackend(accounts ...core.GenesisAccount) *SimulatedBackend {
database, _ := ethdb.NewMemDatabase()
core.WriteGenesisBlockForTesting(database, accounts...)
blockchain, _ := core.NewBlockChain(database, new(core.FakePow), new(event.TypeMux))
blockchain, _ := core.NewBlockChain(database, chainConfig, new(core.FakePow), new(event.TypeMux))

backend := &SimulatedBackend{
database: database,
Expand Down Expand Up @@ -102,7 +107,7 @@ func (b *SimulatedBackend) ContractCall(contract common.Address, data []byte, pe
data: data,
}
// Execute the call and return
vmenv := core.NewEnv(statedb, b.blockchain, msg, block.Header(), nil)
vmenv := core.NewEnv(statedb, chainConfig, b.blockchain, msg, block.Header(), vm.Config{})
gaspool := new(core.GasPool).AddGas(common.MaxBig)

out, _, err := core.ApplyMessage(vmenv, msg, gaspool)
Expand Down Expand Up @@ -145,7 +150,7 @@ func (b *SimulatedBackend) EstimateGasLimit(sender common.Address, contract *com
data: data,
}
// Execute the call and return
vmenv := core.NewEnv(statedb, b.blockchain, msg, block.Header(), nil)
vmenv := core.NewEnv(statedb, chainConfig, b.blockchain, msg, block.Header(), vm.Config{})
gaspool := new(core.GasPool).AddGas(common.MaxBig)

_, gas, err := core.ApplyMessage(vmenv, msg, gaspool)
Expand Down
5 changes: 3 additions & 2 deletions cmd/ethtest/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (

"github.com/codegangsta/cli"
"github.com/ethereum/go-ethereum/logger/glog"
"github.com/ethereum/go-ethereum/params"
"github.com/ethereum/go-ethereum/tests"
)

Expand Down Expand Up @@ -73,9 +74,9 @@ func runTestWithReader(test string, r io.Reader) error {
var err error
switch strings.ToLower(test) {
case "bk", "block", "blocktest", "blockchaintest", "blocktests", "blockchaintests":
err = tests.RunBlockTestWithReader(r, skipTests)
err = tests.RunBlockTestWithReader(params.MainNetHomesteadBlock, r, skipTests)
case "st", "state", "statetest", "statetests":
err = tests.RunStateTestWithReader(r, skipTests)
err = tests.RunStateTestWithReader(tests.RuleSet{params.MainNetHomesteadBlock}, r, skipTests)
case "tx", "transactiontest", "transactiontests":
err = tests.RunTransactionTestsWithReader(r, skipTests)
case "vm", "vmtest", "vmtests":
Expand Down
19 changes: 11 additions & 8 deletions cmd/evm/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ import (
"github.com/ethereum/go-ethereum/core/vm"
"github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/logger/glog"
"github.com/ethereum/go-ethereum/params"
)

var (
Expand Down Expand Up @@ -106,9 +105,6 @@ func init() {
}

func run(ctx *cli.Context) {
vm.ForceJit = ctx.GlobalBool(ForceJitFlag.Name)
vm.EnableJit = !ctx.GlobalBool(DisableJitFlag.Name)

glog.SetToStderr(true)
glog.SetV(ctx.GlobalInt(VerbosityFlag.Name))

Expand All @@ -118,8 +114,10 @@ func run(ctx *cli.Context) {
receiver := statedb.CreateAccount(common.StringToAddress("receiver"))
receiver.SetCode(common.Hex2Bytes(ctx.GlobalString(CodeFlag.Name)))

vmenv := NewEnv(statedb, common.StringToAddress("evmuser"), common.Big(ctx.GlobalString(ValueFlag.Name)), &vm.Config{
Debug: ctx.GlobalBool(DebugFlag.Name),
vmenv := NewEnv(statedb, common.StringToAddress("evmuser"), common.Big(ctx.GlobalString(ValueFlag.Name)), vm.Config{
Debug: ctx.GlobalBool(DebugFlag.Name),
ForceJit: ctx.GlobalBool(ForceJitFlag.Name),
EnableJit: !ctx.GlobalBool(DisableJitFlag.Name),
})

tstart := time.Now()
Expand Down Expand Up @@ -180,8 +178,7 @@ type VMEnv struct {
evm *vm.EVM
}

func NewEnv(state *state.StateDB, transactor common.Address, value *big.Int, cfg *vm.Config) *VMEnv {
params.HomesteadBlock = new(big.Int)
func NewEnv(state *state.StateDB, transactor common.Address, value *big.Int, cfg vm.Config) *VMEnv {
env := &VMEnv{
state: state,
transactor: &transactor,
Expand All @@ -194,6 +191,12 @@ func NewEnv(state *state.StateDB, transactor common.Address, value *big.Int, cfg
return env
}

// ruleSet implements vm.RuleSet and will always default to the homestead rule set.
type ruleSet struct{}

func (ruleSet) IsHomestead(*big.Int) bool { return true }

func (self *VMEnv) RuleSet() vm.RuleSet { return ruleSet{} }
func (self *VMEnv) Vm() vm.Vm { return self.evm }
func (self *VMEnv) Db() vm.Database { return self.state }
func (self *VMEnv) MakeSnapshot() vm.Database { return self.state.Copy() }
Expand Down
1 change: 1 addition & 0 deletions cmd/geth/js_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ func testREPL(t *testing.T, config func(*eth.Config)) (string, *testjethre, *nod
core.WriteGenesisBlockForTesting(db, core.GenesisAccount{common.HexToAddress(testAddress), common.String2Big(testBalance)})

ethConf := &eth.Config{
ChainConfig: &core.ChainConfig{HomesteadBlock: new(big.Int)},
TestGenesisState: db,
AccountManager: accman,
DocRoot: "/",
Expand Down
42 changes: 39 additions & 3 deletions cmd/geth/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ import (
"github.com/ethereum/go-ethereum/accounts"
"github.com/ethereum/go-ethereum/cmd/utils"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/eth"
"github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/internal/debug"
"github.com/ethereum/go-ethereum/logger"
"github.com/ethereum/go-ethereum/logger/glog"
Expand Down Expand Up @@ -108,7 +110,6 @@ Runs quick benchmark on first GPU found.
The output of this command is supposed to be machine-readable.
`,
},

{
Name: "wallet",
Usage: "ethereum presale wallet",
Expand Down Expand Up @@ -247,6 +248,16 @@ nodes.
},
},
},
{
Action: initGenesis,
Name: "init",
Usage: "bootstraps and initialises a new genesis block (JSON)",
Description: `
The init command initialises a new genesis block and definition for the network.
This is a destructive action and changes the network in which you will be
participating.
`,
},
{
Action: console,
Name: "console",
Expand All @@ -255,7 +266,8 @@ nodes.
The Geth console is an interactive shell for the JavaScript runtime environment
which exposes a node admin interface as well as the Ðapp JavaScript API.
See https://github.com/ethereum/go-ethereum/wiki/Javascipt-Console
`},
`,
},
{
Action: attach,
Name: "attach",
Expand Down Expand Up @@ -347,7 +359,6 @@ JavaScript API. See https://github.com/ethereum/go-ethereum/wiki/Javascipt-Conso
go metrics.CollectProcessMetrics(3 * time.Second)

utils.SetupNetwork(ctx)
utils.SetupVM(ctx)
return nil
}

Expand Down Expand Up @@ -417,6 +428,31 @@ func attach(ctx *cli.Context) {
}
}

// initGenesis will initialise the given JSON format genesis file and writes it as
// the zero'd block (i.e. genesis) or will fail hard if it can't succeed.
func initGenesis(ctx *cli.Context) {
genesisPath := ctx.Args().First()
if len(genesisPath) == 0 {
utils.Fatalf("must supply path to genesis JSON file")
}

chainDb, err := ethdb.NewLDBDatabase(filepath.Join(utils.MustMakeDataDir(ctx), "chaindata"), 0, 0)
if err != nil {
utils.Fatalf("could not open database: %v", err)
}

genesisFile, err := os.Open(genesisPath)
if err != nil {
utils.Fatalf("failed to read genesis file: %v", err)
}

block, err := core.WriteGenesisBlock(chainDb, genesisFile)
if err != nil {
utils.Fatalf("failed to write genesis block: %v", err)
}
glog.V(logger.Info).Infof("successfully wrote genesis block and/or chain rule set: %x", block.Hash())
}

// console starts a new geth node, attaching a JavaScript console to it at the
// same time.
func console(ctx *cli.Context) {
Expand Down
1 change: 1 addition & 0 deletions cmd/geth/usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ var AppHelpFlagGroups = []flagGroup{
Flags: []cli.Flag{
utils.MiningEnabledFlag,
utils.MinerThreadsFlag,
utils.TargetGasLimitFlag,
utils.MiningGPUFlag,
utils.AutoDAGFlag,
utils.EtherbaseFlag,
Expand Down
72 changes: 53 additions & 19 deletions cmd/utils/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/state"
"github.com/ethereum/go-ethereum/core/vm"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/eth"
"github.com/ethereum/go-ethereum/ethdb"
Expand Down Expand Up @@ -173,6 +172,11 @@ var (
Name: "minergpus",
Usage: "List of GPUs to use for mining (e.g. '0,1' will use the first two GPUs found)",
}
TargetGasLimitFlag = cli.StringFlag{
Name: "targetgaslimit",
Usage: "Target gas limit sets the artificial target gas floor for the blocks to mine",
Value: params.GenesisGasLimit.String(),
}
AutoDAGFlag = cli.BoolFlag{
Name: "autodag",
Usage: "Enable automatic DAG pregeneration",
Expand Down Expand Up @@ -656,6 +660,7 @@ func MakeSystemNode(name, version string, extra []byte, ctx *cli.Context) *node.
accman := MakeAccountManager(ctx)

ethConf := &eth.Config{
ChainConfig: MustMakeChainConfig(ctx),
Genesis: MakeGenesisBlock(ctx),
FastSync: ctx.GlobalBool(FastSyncFlag.Name),
BlockChainVersion: ctx.GlobalInt(BlockchainVersionFlag.Name),
Expand Down Expand Up @@ -701,8 +706,6 @@ func MakeSystemNode(name, version string, extra []byte, ctx *cli.Context) *node.
ethConf.Genesis = core.TestNetGenesisBlock()
}
state.StartingNonce = 1048576 // (2**20)
// overwrite homestead block
params.HomesteadBlock = params.TestNetHomesteadBlock

case ctx.GlobalBool(DevModeFlag.Name):
// Override the base network stack configs
Expand Down Expand Up @@ -758,36 +761,67 @@ func SetupNetwork(ctx *cli.Context) {
core.BlockReward = big.NewInt(1.5e+18)
core.ExpDiffPeriod = big.NewInt(math.MaxInt64)
}
params.TargetGasLimit = common.String2Big(ctx.GlobalString(TargetGasLimitFlag.Name))
}

// SetupVM configured the VM package's global settings
func SetupVM(ctx *cli.Context) {
vm.EnableJit = ctx.GlobalBool(VMEnableJitFlag.Name)
vm.ForceJit = ctx.GlobalBool(VMForceJitFlag.Name)
vm.SetJITCacheSize(ctx.GlobalInt(VMJitCacheFlag.Name))
// MustMakeChainConfig reads the chain configuration from the given database.
func MustMakeChainConfig(ctx *cli.Context) *core.ChainConfig {
var (
db = MakeChainDatabase(ctx)
genesis = core.GetBlock(db, core.GetCanonicalHash(db, 0))
)
defer db.Close()

chainConfig, err := core.GetChainConfig(db, genesis.Hash())
if err != nil {
if err != core.ChainConfigNotFoundErr {
Fatalf("Could not make chain configuration: %v", err)
}
var homesteadBlockNo *big.Int
if ctx.GlobalBool(TestNetFlag.Name) {
homesteadBlockNo = params.TestNetHomesteadBlock
} else {
homesteadBlockNo = params.MainNetHomesteadBlock
}

chainConfig = &core.ChainConfig{
HomesteadBlock: homesteadBlockNo,
}
}
return chainConfig
}

// MakeChain creates a chain manager from set command line flags.
func MakeChain(ctx *cli.Context) (chain *core.BlockChain, chainDb ethdb.Database) {
datadir := MustMakeDataDir(ctx)
cache := ctx.GlobalInt(CacheFlag.Name)
handles := MakeDatabaseHandles()
// MakeChainDatabase open an LevelDB using the flags passed to the client and will hard crash if it fails.
func MakeChainDatabase(ctx *cli.Context) ethdb.Database {
var (
datadir = MustMakeDataDir(ctx)
cache = ctx.GlobalInt(CacheFlag.Name)
handles = MakeDatabaseHandles()
)

var err error
if chainDb, err = ethdb.NewLDBDatabase(filepath.Join(datadir, "chaindata"), cache, handles); err != nil {
chainDb, err := ethdb.NewLDBDatabase(filepath.Join(datadir, "chaindata"), cache, handles)
if err != nil {
Fatalf("Could not open database: %v", err)
}
return chainDb
}

// MakeChain creates a chain manager from set command line flags.
func MakeChain(ctx *cli.Context) (chain *core.BlockChain, chainDb ethdb.Database) {
var err error
chainDb = MakeChainDatabase(ctx)

if ctx.GlobalBool(OlympicFlag.Name) {
_, err := core.WriteTestNetGenesisBlock(chainDb)
if err != nil {
glog.Fatalln(err)
}
}

eventMux := new(event.TypeMux)
pow := ethash.New()
//genesis := core.GenesisBlock(uint64(ctx.GlobalInt(GenesisNonceFlag.Name)), blockDB)
chain, err = core.NewBlockChain(chainDb, pow, eventMux)
chainConfig := MustMakeChainConfig(ctx)

var eventMux event.TypeMux
chain, err = core.NewBlockChain(chainDb, chainConfig, ethash.New(), &eventMux)
if err != nil {
Fatalf("Could not start chainmanager: %v", err)
}
Expand Down
16 changes: 16 additions & 0 deletions common/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ import (
"os"
"runtime"
"runtime/debug"
"strings"
)

// Report gives off a warning requesting the user to submit an issue to the github tracker.
func Report(extra ...interface{}) {
fmt.Fprintln(os.Stderr, "You've encountered a sought after, hard to reproduce bug. Please report this to the developers <3 https://github.com/ethereum/go-ethereum/issues")
fmt.Fprintln(os.Stderr, extra...)
Expand All @@ -34,3 +36,17 @@ func Report(extra ...interface{}) {

fmt.Fprintln(os.Stderr, "#### BUG! PLEASE REPORT ####")
}

// PrintDepricationWarning prinst the given string in a box using fmt.Println.
func PrintDepricationWarning(str string) {
line := strings.Repeat("#", len(str)+4)
emptyLine := strings.Repeat(" ", len(str))
fmt.Printf(`
%s
# %s #
# %s #
# %s #
%s
`, line, emptyLine, str, emptyLine, line)
}
Loading

0 comments on commit f0cbebb

Please sign in to comment.