Explorar o código

ethutil: remove Config variable

Various functions throughout the codebase used it to grab settings. This
has to stop because I want to use them without reading the config file.

These functions can now be used without reading the config first:

* ethdb.NewLDBDatabase
* ethrepl.NewJSRepl
* vm.New
Felix Lange %!s(int64=10) %!d(string=hai) anos
pai
achega
bdba044a80
Modificáronse 7 ficheiros con 29 adicións e 37 borrados
  1. 1 1
      cmd/ethereum/repl/repl.go
  2. 2 2
      cmd/mist/bindings.go
  3. 1 1
      cmd/mist/gui.go
  4. 5 2
      eth/backend.go
  5. 3 8
      ethdb/database.go
  6. 16 19
      ethutil/config.go
  7. 1 4
      vm/vm.go

+ 1 - 1
cmd/ethereum/repl/repl.go

@@ -54,7 +54,7 @@ type JSRepl struct {
 }
 
 func NewJSRepl(ethereum *eth.Ethereum) *JSRepl {
-	hist, err := os.OpenFile(path.Join(ethutil.Config.ExecPath, "history"), os.O_RDWR|os.O_CREATE, os.ModePerm)
+	hist, err := os.OpenFile(path.Join(ethereum.DataDir, "history"), os.O_RDWR|os.O_CREATE, os.ModePerm)
 	if err != nil {
 		panic(err)
 	}

+ 2 - 2
cmd/mist/bindings.go

@@ -79,14 +79,14 @@ func (self *Gui) AddPlugin(pluginPath string) {
 	self.plugins[pluginPath] = plugin{Name: pluginPath, Path: pluginPath}
 
 	json, _ := json.MarshalIndent(self.plugins, "", "    ")
-	ethutil.WriteFile(ethutil.Config.ExecPath+"/plugins.json", json)
+	ethutil.WriteFile(self.eth.DataDir+"/plugins.json", json)
 }
 
 func (self *Gui) RemovePlugin(pluginPath string) {
 	delete(self.plugins, pluginPath)
 
 	json, _ := json.MarshalIndent(self.plugins, "", "    ")
-	ethutil.WriteFile(ethutil.Config.ExecPath+"/plugins.json", json)
+	ethutil.WriteFile(self.eth.DataDir+"/plugins.json", json)
 }
 
 // this extra function needed to give int typecast value to gui widget

+ 1 - 1
cmd/mist/gui.go

@@ -100,7 +100,7 @@ func NewWindow(ethereum *eth.Ethereum, config *ethutil.ConfigManager, session st
 		plugins:       make(map[string]plugin),
 		serviceEvents: make(chan ServEv, 1),
 	}
-	data, _ := ethutil.ReadAllFile(path.Join(ethutil.Config.ExecPath, "plugins.json"))
+	data, _ := ethutil.ReadAllFile(path.Join(ethereum.DataDir, "plugins.json"))
 	json.Unmarshal([]byte(data), &gui.plugins)
 
 	return gui

+ 5 - 2
eth/backend.go

@@ -132,13 +132,15 @@ type Ethereum struct {
 
 	logger logger.LogSystem
 
-	Mining bool
+	Mining  bool
+	DataDir string
 }
 
 func New(config *Config) (*Ethereum, error) {
 	// Boostrap database
 	ethlogger := logger.New(config.DataDir, config.LogFile, config.LogLevel, config.LogFormat)
-	db, err := ethdb.NewLDBDatabase("blockchain")
+
+	db, err := ethdb.NewLDBDatabase(path.Join(config.DataDir, "blockchain"))
 	if err != nil {
 		return nil, err
 	}
@@ -175,6 +177,7 @@ func New(config *Config) (*Ethereum, error) {
 		blacklist:    p2p.NewBlacklist(),
 		eventMux:     &event.TypeMux{},
 		logger:       ethlogger,
+		DataDir:      config.DataDir,
 	}
 
 	eth.chainManager = core.NewChainManager(db, eth.EventMux())

+ 3 - 8
ethdb/database.go

@@ -1,11 +1,10 @@
 package ethdb
 
 import (
-	"path"
 	"fmt"
 
-	"github.com/ethereum/go-ethereum/ethutil"
 	"github.com/ethereum/go-ethereum/compression/rle"
+	"github.com/ethereum/go-ethereum/ethutil"
 	"github.com/syndtr/goleveldb/leveldb"
 	"github.com/syndtr/goleveldb/leveldb/iterator"
 )
@@ -15,17 +14,13 @@ type LDBDatabase struct {
 	comp bool
 }
 
-func NewLDBDatabase(name string) (*LDBDatabase, error) {
-	dbPath := path.Join(ethutil.Config.ExecPath, name)
-
+func NewLDBDatabase(file string) (*LDBDatabase, error) {
 	// Open the db
-	db, err := leveldb.OpenFile(dbPath, nil)
+	db, err := leveldb.OpenFile(file, nil)
 	if err != nil {
 		return nil, err
 	}
-
 	database := &LDBDatabase{db: db, comp: true}
-
 	return database, nil
 }
 

+ 16 - 19
ethutil/config.go

@@ -20,30 +20,27 @@ type ConfigManager struct {
 	conf *globalconf.GlobalConf
 }
 
-var Config *ConfigManager
-
 // Read config
 //
 // Initialize Config from Config File
 func ReadConfig(ConfigFile string, Datadir string, EnvPrefix string) *ConfigManager {
-	if Config == nil {
-		// create ConfigFile if does not exist, otherwise globalconf panic when trying to persist flags
-		if !FileExist(ConfigFile) {
-			fmt.Printf("config file '%s' doesn't exist, creating it\n", ConfigFile)
-			os.Create(ConfigFile)
-		}
-		g, err := globalconf.NewWithOptions(&globalconf.Options{
-			Filename:  ConfigFile,
-			EnvPrefix: EnvPrefix,
-		})
-		if err != nil {
-			fmt.Println(err)
-		} else {
-			g.ParseAll()
-		}
-		Config = &ConfigManager{ExecPath: Datadir, Debug: true, conf: g, Paranoia: true}
+	if !FileExist(ConfigFile) {
+		// create ConfigFile if it does not exist, otherwise
+		// globalconf will panic when trying to persist flags.
+		fmt.Printf("config file '%s' doesn't exist, creating it\n", ConfigFile)
+		os.Create(ConfigFile)
+	}
+	g, err := globalconf.NewWithOptions(&globalconf.Options{
+		Filename:  ConfigFile,
+		EnvPrefix: EnvPrefix,
+	})
+	if err != nil {
+		fmt.Println(err)
+	} else {
+		g.ParseAll()
 	}
-	return Config
+	cfg := &ConfigManager{ExecPath: Datadir, Debug: true, conf: g, Paranoia: true}
+	return cfg
 }
 
 // provides persistence for flags

+ 1 - 4
vm/vm.go

@@ -30,10 +30,7 @@ type Vm struct {
 
 func New(env Environment) *Vm {
 	lt := LogTyPretty
-	if ethutil.Config.Diff {
-		lt = LogTyDiff
-	}
-
+	// lt = LogTyDiff
 	return &Vm{debug: true, env: env, logTy: lt, Recoverable: true}
 }