Skip to content

Commit f0cbebb

Browse files
committed
core: added basic chain configuration
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.
1 parent 10d3466 commit f0cbebb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+732
-431
lines changed

accounts/abi/bind/backends/simulated.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,15 @@ import (
2424
"github.com/ethereum/go-ethereum/core"
2525
"github.com/ethereum/go-ethereum/core/state"
2626
"github.com/ethereum/go-ethereum/core/types"
27+
"github.com/ethereum/go-ethereum/core/vm"
2728
"github.com/ethereum/go-ethereum/ethdb"
2829
"github.com/ethereum/go-ethereum/event"
30+
"github.com/ethereum/go-ethereum/params"
2931
)
3032

33+
// Default chain configuration which sets homestead phase at block 0 (i.e. no frontier)
34+
var chainConfig = &core.ChainConfig{HomesteadBlock: params.MainNetHomesteadBlock}
35+
3136
// This nil assignment ensures compile time that SimulatedBackend implements bind.ContractBackend.
3237
var _ bind.ContractBackend = (*SimulatedBackend)(nil)
3338

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

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

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

151156
_, gas, err := core.ApplyMessage(vmenv, msg, gaspool)

cmd/ethtest/main.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727

2828
"github.com/codegangsta/cli"
2929
"github.com/ethereum/go-ethereum/logger/glog"
30+
"github.com/ethereum/go-ethereum/params"
3031
"github.com/ethereum/go-ethereum/tests"
3132
)
3233

@@ -73,9 +74,9 @@ func runTestWithReader(test string, r io.Reader) error {
7374
var err error
7475
switch strings.ToLower(test) {
7576
case "bk", "block", "blocktest", "blockchaintest", "blocktests", "blockchaintests":
76-
err = tests.RunBlockTestWithReader(r, skipTests)
77+
err = tests.RunBlockTestWithReader(params.MainNetHomesteadBlock, r, skipTests)
7778
case "st", "state", "statetest", "statetests":
78-
err = tests.RunStateTestWithReader(r, skipTests)
79+
err = tests.RunStateTestWithReader(tests.RuleSet{params.MainNetHomesteadBlock}, r, skipTests)
7980
case "tx", "transactiontest", "transactiontests":
8081
err = tests.RunTransactionTestsWithReader(r, skipTests)
8182
case "vm", "vmtest", "vmtests":

cmd/evm/main.go

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ import (
3333
"github.com/ethereum/go-ethereum/core/vm"
3434
"github.com/ethereum/go-ethereum/ethdb"
3535
"github.com/ethereum/go-ethereum/logger/glog"
36-
"github.com/ethereum/go-ethereum/params"
3736
)
3837

3938
var (
@@ -106,9 +105,6 @@ func init() {
106105
}
107106

108107
func run(ctx *cli.Context) {
109-
vm.ForceJit = ctx.GlobalBool(ForceJitFlag.Name)
110-
vm.EnableJit = !ctx.GlobalBool(DisableJitFlag.Name)
111-
112108
glog.SetToStderr(true)
113109
glog.SetV(ctx.GlobalInt(VerbosityFlag.Name))
114110

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

121-
vmenv := NewEnv(statedb, common.StringToAddress("evmuser"), common.Big(ctx.GlobalString(ValueFlag.Name)), &vm.Config{
122-
Debug: ctx.GlobalBool(DebugFlag.Name),
117+
vmenv := NewEnv(statedb, common.StringToAddress("evmuser"), common.Big(ctx.GlobalString(ValueFlag.Name)), vm.Config{
118+
Debug: ctx.GlobalBool(DebugFlag.Name),
119+
ForceJit: ctx.GlobalBool(ForceJitFlag.Name),
120+
EnableJit: !ctx.GlobalBool(DisableJitFlag.Name),
123121
})
124122

125123
tstart := time.Now()
@@ -180,8 +178,7 @@ type VMEnv struct {
180178
evm *vm.EVM
181179
}
182180

183-
func NewEnv(state *state.StateDB, transactor common.Address, value *big.Int, cfg *vm.Config) *VMEnv {
184-
params.HomesteadBlock = new(big.Int)
181+
func NewEnv(state *state.StateDB, transactor common.Address, value *big.Int, cfg vm.Config) *VMEnv {
185182
env := &VMEnv{
186183
state: state,
187184
transactor: &transactor,
@@ -194,6 +191,12 @@ func NewEnv(state *state.StateDB, transactor common.Address, value *big.Int, cfg
194191
return env
195192
}
196193

194+
// ruleSet implements vm.RuleSet and will always default to the homestead rule set.
195+
type ruleSet struct{}
196+
197+
func (ruleSet) IsHomestead(*big.Int) bool { return true }
198+
199+
func (self *VMEnv) RuleSet() vm.RuleSet { return ruleSet{} }
197200
func (self *VMEnv) Vm() vm.Vm { return self.evm }
198201
func (self *VMEnv) Db() vm.Database { return self.state }
199202
func (self *VMEnv) MakeSnapshot() vm.Database { return self.state.Copy() }

cmd/geth/js_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ func testREPL(t *testing.T, config func(*eth.Config)) (string, *testjethre, *nod
106106
core.WriteGenesisBlockForTesting(db, core.GenesisAccount{common.HexToAddress(testAddress), common.String2Big(testBalance)})
107107

108108
ethConf := &eth.Config{
109+
ChainConfig: &core.ChainConfig{HomesteadBlock: new(big.Int)},
109110
TestGenesisState: db,
110111
AccountManager: accman,
111112
DocRoot: "/",

cmd/geth/main.go

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ import (
3232
"github.com/ethereum/go-ethereum/accounts"
3333
"github.com/ethereum/go-ethereum/cmd/utils"
3434
"github.com/ethereum/go-ethereum/common"
35+
"github.com/ethereum/go-ethereum/core"
3536
"github.com/ethereum/go-ethereum/eth"
37+
"github.com/ethereum/go-ethereum/ethdb"
3638
"github.com/ethereum/go-ethereum/internal/debug"
3739
"github.com/ethereum/go-ethereum/logger"
3840
"github.com/ethereum/go-ethereum/logger/glog"
@@ -108,7 +110,6 @@ Runs quick benchmark on first GPU found.
108110
The output of this command is supposed to be machine-readable.
109111
`,
110112
},
111-
112113
{
113114
Name: "wallet",
114115
Usage: "ethereum presale wallet",
@@ -247,6 +248,16 @@ nodes.
247248
},
248249
},
249250
},
251+
{
252+
Action: initGenesis,
253+
Name: "init",
254+
Usage: "bootstraps and initialises a new genesis block (JSON)",
255+
Description: `
256+
The init command initialises a new genesis block and definition for the network.
257+
This is a destructive action and changes the network in which you will be
258+
participating.
259+
`,
260+
},
250261
{
251262
Action: console,
252263
Name: "console",
@@ -255,7 +266,8 @@ nodes.
255266
The Geth console is an interactive shell for the JavaScript runtime environment
256267
which exposes a node admin interface as well as the Ðapp JavaScript API.
257268
See https://github.com/ethereum/go-ethereum/wiki/Javascipt-Console
258-
`},
269+
`,
270+
},
259271
{
260272
Action: attach,
261273
Name: "attach",
@@ -347,7 +359,6 @@ JavaScript API. See https://github.com/ethereum/go-ethereum/wiki/Javascipt-Conso
347359
go metrics.CollectProcessMetrics(3 * time.Second)
348360

349361
utils.SetupNetwork(ctx)
350-
utils.SetupVM(ctx)
351362
return nil
352363
}
353364

@@ -417,6 +428,31 @@ func attach(ctx *cli.Context) {
417428
}
418429
}
419430

431+
// initGenesis will initialise the given JSON format genesis file and writes it as
432+
// the zero'd block (i.e. genesis) or will fail hard if it can't succeed.
433+
func initGenesis(ctx *cli.Context) {
434+
genesisPath := ctx.Args().First()
435+
if len(genesisPath) == 0 {
436+
utils.Fatalf("must supply path to genesis JSON file")
437+
}
438+
439+
chainDb, err := ethdb.NewLDBDatabase(filepath.Join(utils.MustMakeDataDir(ctx), "chaindata"), 0, 0)
440+
if err != nil {
441+
utils.Fatalf("could not open database: %v", err)
442+
}
443+
444+
genesisFile, err := os.Open(genesisPath)
445+
if err != nil {
446+
utils.Fatalf("failed to read genesis file: %v", err)
447+
}
448+
449+
block, err := core.WriteGenesisBlock(chainDb, genesisFile)
450+
if err != nil {
451+
utils.Fatalf("failed to write genesis block: %v", err)
452+
}
453+
glog.V(logger.Info).Infof("successfully wrote genesis block and/or chain rule set: %x", block.Hash())
454+
}
455+
420456
// console starts a new geth node, attaching a JavaScript console to it at the
421457
// same time.
422458
func console(ctx *cli.Context) {

cmd/geth/usage.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ var AppHelpFlagGroups = []flagGroup{
121121
Flags: []cli.Flag{
122122
utils.MiningEnabledFlag,
123123
utils.MinerThreadsFlag,
124+
utils.TargetGasLimitFlag,
124125
utils.MiningGPUFlag,
125126
utils.AutoDAGFlag,
126127
utils.EtherbaseFlag,

cmd/utils/flags.go

Lines changed: 53 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ import (
3434
"github.com/ethereum/go-ethereum/common"
3535
"github.com/ethereum/go-ethereum/core"
3636
"github.com/ethereum/go-ethereum/core/state"
37-
"github.com/ethereum/go-ethereum/core/vm"
3837
"github.com/ethereum/go-ethereum/crypto"
3938
"github.com/ethereum/go-ethereum/eth"
4039
"github.com/ethereum/go-ethereum/ethdb"
@@ -173,6 +172,11 @@ var (
173172
Name: "minergpus",
174173
Usage: "List of GPUs to use for mining (e.g. '0,1' will use the first two GPUs found)",
175174
}
175+
TargetGasLimitFlag = cli.StringFlag{
176+
Name: "targetgaslimit",
177+
Usage: "Target gas limit sets the artificial target gas floor for the blocks to mine",
178+
Value: params.GenesisGasLimit.String(),
179+
}
176180
AutoDAGFlag = cli.BoolFlag{
177181
Name: "autodag",
178182
Usage: "Enable automatic DAG pregeneration",
@@ -656,6 +660,7 @@ func MakeSystemNode(name, version string, extra []byte, ctx *cli.Context) *node.
656660
accman := MakeAccountManager(ctx)
657661

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

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

763-
// SetupVM configured the VM package's global settings
764-
func SetupVM(ctx *cli.Context) {
765-
vm.EnableJit = ctx.GlobalBool(VMEnableJitFlag.Name)
766-
vm.ForceJit = ctx.GlobalBool(VMForceJitFlag.Name)
767-
vm.SetJITCacheSize(ctx.GlobalInt(VMJitCacheFlag.Name))
767+
// MustMakeChainConfig reads the chain configuration from the given database.
768+
func MustMakeChainConfig(ctx *cli.Context) *core.ChainConfig {
769+
var (
770+
db = MakeChainDatabase(ctx)
771+
genesis = core.GetBlock(db, core.GetCanonicalHash(db, 0))
772+
)
773+
defer db.Close()
774+
775+
chainConfig, err := core.GetChainConfig(db, genesis.Hash())
776+
if err != nil {
777+
if err != core.ChainConfigNotFoundErr {
778+
Fatalf("Could not make chain configuration: %v", err)
779+
}
780+
var homesteadBlockNo *big.Int
781+
if ctx.GlobalBool(TestNetFlag.Name) {
782+
homesteadBlockNo = params.TestNetHomesteadBlock
783+
} else {
784+
homesteadBlockNo = params.MainNetHomesteadBlock
785+
}
786+
787+
chainConfig = &core.ChainConfig{
788+
HomesteadBlock: homesteadBlockNo,
789+
}
790+
}
791+
return chainConfig
768792
}
769793

770-
// MakeChain creates a chain manager from set command line flags.
771-
func MakeChain(ctx *cli.Context) (chain *core.BlockChain, chainDb ethdb.Database) {
772-
datadir := MustMakeDataDir(ctx)
773-
cache := ctx.GlobalInt(CacheFlag.Name)
774-
handles := MakeDatabaseHandles()
794+
// MakeChainDatabase open an LevelDB using the flags passed to the client and will hard crash if it fails.
795+
func MakeChainDatabase(ctx *cli.Context) ethdb.Database {
796+
var (
797+
datadir = MustMakeDataDir(ctx)
798+
cache = ctx.GlobalInt(CacheFlag.Name)
799+
handles = MakeDatabaseHandles()
800+
)
775801

776-
var err error
777-
if chainDb, err = ethdb.NewLDBDatabase(filepath.Join(datadir, "chaindata"), cache, handles); err != nil {
802+
chainDb, err := ethdb.NewLDBDatabase(filepath.Join(datadir, "chaindata"), cache, handles)
803+
if err != nil {
778804
Fatalf("Could not open database: %v", err)
779805
}
806+
return chainDb
807+
}
808+
809+
// MakeChain creates a chain manager from set command line flags.
810+
func MakeChain(ctx *cli.Context) (chain *core.BlockChain, chainDb ethdb.Database) {
811+
var err error
812+
chainDb = MakeChainDatabase(ctx)
813+
780814
if ctx.GlobalBool(OlympicFlag.Name) {
781815
_, err := core.WriteTestNetGenesisBlock(chainDb)
782816
if err != nil {
783817
glog.Fatalln(err)
784818
}
785819
}
786820

787-
eventMux := new(event.TypeMux)
788-
pow := ethash.New()
789-
//genesis := core.GenesisBlock(uint64(ctx.GlobalInt(GenesisNonceFlag.Name)), blockDB)
790-
chain, err = core.NewBlockChain(chainDb, pow, eventMux)
821+
chainConfig := MustMakeChainConfig(ctx)
822+
823+
var eventMux event.TypeMux
824+
chain, err = core.NewBlockChain(chainDb, chainConfig, ethash.New(), &eventMux)
791825
if err != nil {
792826
Fatalf("Could not start chainmanager: %v", err)
793827
}

common/debug.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@ import (
2121
"os"
2222
"runtime"
2323
"runtime/debug"
24+
"strings"
2425
)
2526

27+
// Report gives off a warning requesting the user to submit an issue to the github tracker.
2628
func Report(extra ...interface{}) {
2729
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")
2830
fmt.Fprintln(os.Stderr, extra...)
@@ -34,3 +36,17 @@ func Report(extra ...interface{}) {
3436

3537
fmt.Fprintln(os.Stderr, "#### BUG! PLEASE REPORT ####")
3638
}
39+
40+
// PrintDepricationWarning prinst the given string in a box using fmt.Println.
41+
func PrintDepricationWarning(str string) {
42+
line := strings.Repeat("#", len(str)+4)
43+
emptyLine := strings.Repeat(" ", len(str))
44+
fmt.Printf(`
45+
%s
46+
# %s #
47+
# %s #
48+
# %s #
49+
%s
50+
51+
`, line, emptyLine, str, emptyLine, line)
52+
}

0 commit comments

Comments
 (0)