diff options
author | obscuren <geffobscura@gmail.com> | 2014-06-27 01:54:09 +0800 |
---|---|---|
committer | obscuren <geffobscura@gmail.com> | 2014-06-27 01:54:09 +0800 |
commit | 3777ead25e1acedc0571a48a485976eb5c36fb30 (patch) | |
tree | 38419590b42a20a51773e178acbbb3178ee89bf2 /ethereum | |
parent | cba47963113d8041281278d75ee0dad046798e82 (diff) | |
parent | a68bfd215f7b1859c1b14b0df59f3260b35df828 (diff) | |
download | dexon-3777ead25e1acedc0571a48a485976eb5c36fb30.tar.gz dexon-3777ead25e1acedc0571a48a485976eb5c36fb30.tar.zst dexon-3777ead25e1acedc0571a48a485976eb5c36fb30.zip |
Merge branch 'release/0.5.15'
Diffstat (limited to 'ethereum')
-rw-r--r-- | ethereum/cmd.go | 32 | ||||
-rw-r--r-- | ethereum/ethereum.go | 193 | ||||
-rw-r--r-- | ethereum/flags.go (renamed from ethereum/config.go) | 32 | ||||
-rw-r--r-- | ethereum/javascript_runtime.go | 24 | ||||
-rw-r--r-- | ethereum/main.go | 53 | ||||
-rw-r--r-- | ethereum/repl.go | 41 | ||||
-rw-r--r-- | ethereum/repl_darwin.go | 4 |
7 files changed, 173 insertions, 206 deletions
diff --git a/ethereum/cmd.go b/ethereum/cmd.go new file mode 100644 index 000000000..08147824d --- /dev/null +++ b/ethereum/cmd.go @@ -0,0 +1,32 @@ +package main + +import ( + "github.com/ethereum/eth-go" + "github.com/ethereum/go-ethereum/utils" + "io/ioutil" + "os" +) + +func InitJsConsole(ethereum *eth.Ethereum) { + repl := NewJSRepl(ethereum) + go repl.Start() + utils.RegisterInterrupt(func(os.Signal) { + repl.Stop() + }) +} + +func ExecJsFile(ethereum *eth.Ethereum, InputFile string) { + file, err := os.Open(InputFile) + if err != nil { + logger.Fatalln(err) + } + content, err := ioutil.ReadAll(file) + if err != nil { + logger.Fatalln(err) + } + re := NewJSRE(ethereum) + utils.RegisterInterrupt(func(os.Signal) { + re.Stop() + }) + re.Run(string(content)) +} diff --git a/ethereum/ethereum.go b/ethereum/ethereum.go deleted file mode 100644 index 8812e0a60..000000000 --- a/ethereum/ethereum.go +++ /dev/null @@ -1,193 +0,0 @@ -package main - -import ( - "fmt" - "github.com/ethereum/eth-go" - "github.com/ethereum/eth-go/ethutil" - "github.com/ethereum/go-ethereum/utils" - "github.com/rakyll/globalconf" - "io/ioutil" - "log" - "os" - "os/signal" - "path" - "runtime" - "strings" -) - -const Debug = true - -func RegisterInterrupt(cb func(os.Signal)) { - go func() { - // Buffered chan of one is enough - c := make(chan os.Signal, 1) - // Notify about interrupts for now - signal.Notify(c, os.Interrupt) - - for sig := range c { - cb(sig) - } - }() -} - -func confirm(message string) bool { - fmt.Println(message, "Are you sure? (y/n)") - var r string - fmt.Scanln(&r) - for ; ; fmt.Scanln(&r) { - if r == "n" || r == "y" { - break - } else { - fmt.Printf("Yes or no?", r) - } - } - return r == "y" -} - -func main() { - Init() - - runtime.GOMAXPROCS(runtime.NumCPU()) - - // set logger - var logSys *log.Logger - flags := log.LstdFlags - - var lt ethutil.LoggerType - if StartJsConsole || len(InputFile) > 0 { - lt = ethutil.LogFile - } else { - lt = ethutil.LogFile | ethutil.LogStd - } - - g, err := globalconf.NewWithOptions(&globalconf.Options{ - Filename: path.Join(ethutil.ApplicationFolder(Datadir), "conf.ini"), - }) - if err != nil { - fmt.Println(err) - } else { - g.ParseAll() - } - ethutil.ReadConfig(Datadir, lt, g, Identifier) - - logger := ethutil.Config.Log - - if LogFile != "" { - logfile, err := os.OpenFile(LogFile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666) - if err != nil { - panic(fmt.Sprintf("error opening log file '%s': %v", LogFile, err)) - } - defer logfile.Close() - log.SetOutput(logfile) - logSys = log.New(logfile, "", flags) - logger.AddLogSystem(logSys) - } else { - logSys = log.New(os.Stdout, "", flags) - } - - // Instantiated a eth stack - ethereum, err := eth.New(eth.CapDefault, UseUPnP) - if err != nil { - log.Println("eth start err:", err) - return - } - ethereum.Port = OutboundPort - - // bookkeeping tasks - switch { - case GenAddr: - if NonInteractive || confirm("This action overwrites your old private key.") { - utils.CreateKeyPair(true) - } - os.Exit(0) - case len(ImportKey) > 0: - if NonInteractive || confirm("This action overwrites your old private key.") { - mnemonic := strings.Split(ImportKey, " ") - if len(mnemonic) == 24 { - logSys.Println("Got mnemonic key, importing.") - key := ethutil.MnemonicDecode(mnemonic) - utils.ImportPrivateKey(key) - } else if len(mnemonic) == 1 { - logSys.Println("Got hex key, importing.") - utils.ImportPrivateKey(ImportKey) - } else { - logSys.Println("Did not recognise format, exiting.") - } - } - os.Exit(0) - case ExportKey: - keyPair := ethutil.GetKeyRing().Get(0) - fmt.Printf(` -Generating new address and keypair. -Please keep your keys somewhere save. - -++++++++++++++++ KeyRing +++++++++++++++++++ -addr: %x -prvk: %x -pubk: %x -++++++++++++++++++++++++++++++++++++++++++++ -save these words so you can restore your account later: %s -`, keyPair.Address(), keyPair.PrivateKey, keyPair.PublicKey) - - os.Exit(0) - case ShowGenesis: - logSys.Println(ethereum.BlockChain().Genesis()) - os.Exit(0) - default: - // Creates a keypair if non exists - utils.CreateKeyPair(false) - } - - // client - logger.Infoln(fmt.Sprintf("Starting Ethereum v%s", ethutil.Config.Ver)) - - // Set the max peers - ethereum.MaxPeers = MaxPeer - - // Set Mining status - ethereum.Mining = StartMining - - if StartMining { - utils.DoMining(ethereum) - } - - if StartJsConsole { - repl := NewJSRepl(ethereum) - - go repl.Start() - - RegisterInterrupt(func(os.Signal) { - repl.Stop() - }) - } else if len(InputFile) > 0 { - file, err := os.Open(InputFile) - if err != nil { - ethutil.Config.Log.Fatal(err) - } - - content, err := ioutil.ReadAll(file) - if err != nil { - ethutil.Config.Log.Fatal(err) - } - - re := NewJSRE(ethereum) - RegisterInterrupt(func(os.Signal) { - re.Stop() - }) - re.Run(string(content)) - } - - if StartRpc { - utils.DoRpc(ethereum, RpcPort) - } - - RegisterInterrupt(func(sig os.Signal) { - fmt.Printf("Shutting down (%v) ... \n", sig) - ethereum.Stop() - }) - - ethereum.Start(UseSeed) - - // Wait for shutdown - ethereum.WaitForShutdown() -} diff --git a/ethereum/config.go b/ethereum/flags.go index a80b47a8e..8fb87cf34 100644 --- a/ethereum/config.go +++ b/ethereum/flags.go @@ -3,11 +3,13 @@ package main import ( "flag" "fmt" + "github.com/ethereum/eth-go/ethlog" "os" + "os/user" + "path" ) var Identifier string -var StartMining bool var StartRpc bool var RpcPort int var UseUPnP bool @@ -19,16 +21,28 @@ var GenAddr bool var UseSeed bool var ImportKey string var ExportKey bool -var LogFile string var NonInteractive bool +var Datadir string +var LogFile string +var ConfigFile string +var DebugFile string +var LogLevel int + +// flags specific to cli client +var StartMining bool var StartJsConsole bool var InputFile string -var Datadir string +func defaultDataDir() string { + usr, _ := user.Current() + return path.Join(usr.HomeDir, ".ethereum") +} + +var defaultConfigFile = path.Join(defaultDataDir(), "conf.ini") func Init() { flag.Usage = func() { - fmt.Fprintf(os.Stderr, "%s [options] [filename]:\n", os.Args[0]) + fmt.Fprintf(os.Stderr, "%s [options] [filename]:\noptions precedence: default < config file < environment variables < command line\n", os.Args[0]) flag.PrintDefaults() } @@ -38,17 +52,19 @@ func Init() { flag.IntVar(&MaxPeer, "maxpeer", 10, "maximum desired peers") flag.IntVar(&RpcPort, "rpcport", 8080, "port to start json-rpc server on") flag.BoolVar(&StartRpc, "rpc", false, "start rpc server") - flag.BoolVar(&StartJsConsole, "js", false, "exp") - - flag.BoolVar(&StartMining, "mine", false, "start dagger mining") flag.BoolVar(&NonInteractive, "y", false, "non-interactive mode (say yes to confirmations)") flag.BoolVar(&UseSeed, "seed", true, "seed peers") flag.BoolVar(&GenAddr, "genaddr", false, "create a new priv/pub key") flag.BoolVar(&ExportKey, "export", false, "export private key") flag.StringVar(&LogFile, "logfile", "", "log file (defaults to standard output)") flag.StringVar(&ImportKey, "import", "", "imports the given private key (hex)") + flag.StringVar(&Datadir, "datadir", defaultDataDir(), "specifies the datadir to use") + flag.StringVar(&ConfigFile, "conf", defaultConfigFile, "config file") + flag.StringVar(&DebugFile, "debug", "", "debug file (no debugging if not set)") + flag.IntVar(&LogLevel, "loglevel", int(ethlog.InfoLevel), "loglevel: 0-5: silent,error,warn,info,debug,debug detail)") - flag.StringVar(&Datadir, "datadir", ".ethereum", "specifies the datadir to use. Takes precedence over config file.") + flag.BoolVar(&StartMining, "mine", false, "start dagger mining") + flag.BoolVar(&StartJsConsole, "js", false, "launches javascript console") flag.Parse() diff --git a/ethereum/javascript_runtime.go b/ethereum/javascript_runtime.go index b05d39232..0dfe07a54 100644 --- a/ethereum/javascript_runtime.go +++ b/ethereum/javascript_runtime.go @@ -4,6 +4,7 @@ import ( "fmt" "github.com/ethereum/eth-go" "github.com/ethereum/eth-go/ethchain" + "github.com/ethereum/eth-go/ethlog" "github.com/ethereum/eth-go/ethpub" "github.com/ethereum/eth-go/ethutil" "github.com/ethereum/go-ethereum/utils" @@ -14,6 +15,8 @@ import ( "path/filepath" ) +var jsrelogger = ethlog.NewLogger("JSRE") + type JSRE struct { ethereum *eth.Ethereum vm *otto.Otto @@ -31,7 +34,7 @@ func (jsre *JSRE) LoadExtFile(path string) { if err == nil { jsre.vm.Run(result) } else { - ethutil.Config.Log.Debugln("Could not load file:", path) + jsrelogger.Debugln("Could not load file:", path) } } @@ -65,6 +68,8 @@ func NewJSRE(ethereum *eth.Ethereum) *JSRE { re.initStdFuncs() + jsrelogger.Infoln("started") + return re } @@ -99,6 +104,7 @@ func (self *JSRE) Stop() { close(self.blockChan) close(self.quitChan) close(self.changeChan) + jsrelogger.Infoln("stopped") } func (self *JSRE) mainLoop() { @@ -138,6 +144,7 @@ func (self *JSRE) initStdFuncs() { eth.Set("require", self.require) eth.Set("stopMining", self.stopMining) eth.Set("startMining", self.startMining) + eth.Set("execBlock", self.execBlock) } /* @@ -207,3 +214,18 @@ func (self *JSRE) require(call otto.FunctionCall) otto.Value { return t } + +func (self *JSRE) execBlock(call otto.FunctionCall) otto.Value { + hash, err := call.Argument(0).ToString() + if err != nil { + return otto.UndefinedValue() + } + + err = utils.BlockDo(self.ethereum, ethutil.FromHex(hash)) + if err != nil { + fmt.Println(err) + return otto.FalseValue() + } + + return otto.TrueValue() +} diff --git a/ethereum/main.go b/ethereum/main.go new file mode 100644 index 000000000..6b1995eec --- /dev/null +++ b/ethereum/main.go @@ -0,0 +1,53 @@ +package main + +import ( + "github.com/ethereum/eth-go/ethlog" + "github.com/ethereum/go-ethereum/utils" + "runtime" +) + +var logger = ethlog.NewLogger("CLI") + +func main() { + runtime.GOMAXPROCS(runtime.NumCPU()) + + utils.HandleInterrupt() + + // precedence: code-internal flag default < config file < environment variables < command line + Init() // parsing command line + utils.InitConfig(ConfigFile, Datadir, Identifier, "ETH") + + utils.InitDataDir(Datadir) + + utils.InitLogging(Datadir, LogFile, LogLevel, DebugFile) + + ethereum := utils.NewEthereum(UseUPnP, OutboundPort, MaxPeer) + + // create, import, export keys + utils.KeyTasks(GenAddr, ImportKey, ExportKey, NonInteractive) + + if ShowGenesis { + utils.ShowGenesis(ethereum) + } + + if StartMining { + utils.StartMining(ethereum) + } + + // better reworked as cases + if StartJsConsole { + InitJsConsole(ethereum) + } else if len(InputFile) > 0 { + ExecJsFile(ethereum, InputFile) + } + + if StartRpc { + utils.StartRpc(ethereum, RpcPort) + } + + utils.StartEthereum(ethereum, UseSeed) + + // this blocks the thread + ethereum.WaitForShutdown() + ethlog.Flush() +} diff --git a/ethereum/repl.go b/ethereum/repl.go index 0208459ad..34380a06f 100644 --- a/ethereum/repl.go +++ b/ethereum/repl.go @@ -1,10 +1,15 @@ package main import ( + "bufio" "fmt" "github.com/ethereum/eth-go" "github.com/ethereum/eth-go/ethpub" + "github.com/ethereum/eth-go/ethutil" "github.com/obscuren/otto" + "io" + "os" + "path" ) type Repl interface { @@ -16,18 +21,48 @@ type JSRepl struct { re *JSRE prompt string + + history *os.File + + running bool } func NewJSRepl(ethereum *eth.Ethereum) *JSRepl { - return &JSRepl{re: NewJSRE(ethereum), prompt: "> "} + hist, err := os.OpenFile(path.Join(ethutil.Config.ExecPath, "history"), os.O_RDWR|os.O_CREATE, os.ModePerm) + if err != nil { + panic(err) + } + + return &JSRepl{re: NewJSRE(ethereum), prompt: "> ", history: hist} } func (self *JSRepl) Start() { - self.read() + if !self.running { + self.running = true + logger.Infoln("init JS Console") + reader := bufio.NewReader(self.history) + for { + line, err := reader.ReadString('\n') + if err != nil && err == io.EOF { + break + } else if err != nil { + fmt.Println("error reading history", err) + break + } + + addHistory(line[:len(line)-1]) + } + self.read() + } } func (self *JSRepl) Stop() { - self.re.Stop() + if self.running { + self.running = false + self.re.Stop() + logger.Infoln("exit JS Console") + self.history.Close() + } } func (self *JSRepl) parseInput(code string) { diff --git a/ethereum/repl_darwin.go b/ethereum/repl_darwin.go index b61d4edd7..62b40059a 100644 --- a/ethereum/repl_darwin.go +++ b/ethereum/repl_darwin.go @@ -102,7 +102,9 @@ L: break L } - addHistory(str[:len(str)-1]) //allow user to recall this line + hist := str[:len(str)-1] + addHistory(hist) //allow user to recall this line + self.history.WriteString(str) self.parseInput(str) |