From 3f5b348451a8acd4c22be1c320808dd4eadc38d3 Mon Sep 17 00:00:00 2001 From: obscuren Date: Wed, 21 May 2014 23:36:55 +0200 Subject: [PATCH 1/3] Fixes #50 --- ethereum/repl_darwin.go | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/ethereum/repl_darwin.go b/ethereum/repl_darwin.go index 1b98c2150..cb11adfc7 100644 --- a/ethereum/repl_darwin.go +++ b/ethereum/repl_darwin.go @@ -1,17 +1,37 @@ package main +// #cgo darwin CFLAGS: -I/usr/local/opt/readline/include +// #cgo darwin LDFLAGS: -L/usr/local/opt/readline/lib // #cgo LDFLAGS: -lreadline // #include // #include // #include // #include import "C" - import ( + "os" + "os/signal" "strings" + "syscall" "unsafe" ) +func initReadLine() { + C.rl_catch_sigwinch = 0 + c := make(chan os.Signal, 1) + signal.Notify(c, syscall.SIGWINCH) + go func() { + for sig := range c { + switch sig { + case syscall.SIGWINCH: + C.rl_resize_terminal() + default: + + } + } + }() +} + func readLine(prompt *string) *string { var p *C.char @@ -59,6 +79,7 @@ func (self *JSRepl) setIndent() { } func (self *JSRepl) read() { + initReadLine() L: for { switch result := readLine(&self.prompt); true { From b902de20c7119ec521a28bba986a0cc9d14354c0 Mon Sep 17 00:00:00 2001 From: obscuren Date: Wed, 21 May 2014 23:46:16 +0200 Subject: [PATCH 2/3] Fixes #49 --- ethereum/repl_darwin.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/ethereum/repl_darwin.go b/ethereum/repl_darwin.go index cb11adfc7..fa36b0d52 100644 --- a/ethereum/repl_darwin.go +++ b/ethereum/repl_darwin.go @@ -18,13 +18,18 @@ import ( func initReadLine() { C.rl_catch_sigwinch = 0 + C.rl_catch_signals = 0 c := make(chan os.Signal, 1) signal.Notify(c, syscall.SIGWINCH) + signal.Notify(c, os.Interrupt) go func() { for sig := range c { switch sig { case syscall.SIGWINCH: C.rl_resize_terminal() + + case os.Interrupt: + C.rl_cleanup_after_signal() default: } From 01b833146f3afa214586a1ffb710546a5e4cc90a Mon Sep 17 00:00:00 2001 From: obscuren Date: Thu, 22 May 2014 00:25:48 +0200 Subject: [PATCH 3/3] Added mining stop and start --- ethereum/javascript_runtime.go | 15 +++++++++++++- utils/cmd.go | 38 +++++++++++++++++++++++++++------- 2 files changed, 44 insertions(+), 9 deletions(-) diff --git a/ethereum/javascript_runtime.go b/ethereum/javascript_runtime.go index fa01c7005..93297f604 100644 --- a/ethereum/javascript_runtime.go +++ b/ethereum/javascript_runtime.go @@ -6,6 +6,7 @@ import ( "github.com/ethereum/eth-go/ethchain" "github.com/ethereum/eth-go/ethpub" "github.com/ethereum/eth-go/ethutil" + "github.com/ethereum/go-ethereum/utils" "github.com/obscuren/otto" "io/ioutil" "os" @@ -116,14 +117,26 @@ func (self *JSRE) initStdFuncs() { eth.Set("watch", self.watch) eth.Set("addPeer", self.addPeer) eth.Set("require", self.require) + eth.Set("stopMining", self.stopMining) + eth.Set("startMining", self.startMining) } /* * The following methods are natively implemented javascript functions */ +func (self *JSRE) stopMining(call otto.FunctionCall) otto.Value { + v, _ := self.vm.ToValue(utils.StopMining(self.ethereum)) + return v +} + +func (self *JSRE) startMining(call otto.FunctionCall) otto.Value { + v, _ := self.vm.ToValue(utils.StartMining(self.ethereum)) + return v +} + // eth.watch -func (self JSRE) watch(call otto.FunctionCall) otto.Value { +func (self *JSRE) watch(call otto.FunctionCall) otto.Value { addr, _ := call.Argument(0).ToString() var storageAddr string var cb otto.Value diff --git a/utils/cmd.go b/utils/cmd.go index f163575da..98005d7de 100644 --- a/utils/cmd.go +++ b/utils/cmd.go @@ -19,6 +19,8 @@ func DoRpc(ethereum *eth.Ethereum, RpcPort int) { } } +var miner ethminer.Miner + func DoMining(ethereum *eth.Ethereum) { // Set Mining status ethereum.Mining = true @@ -31,17 +33,37 @@ func DoMining(ethereum *eth.Ethereum) { addr := keyPair.Address() go func() { + ethutil.Config.Log.Infoln("Miner started") + + miner = ethminer.NewDefaultMiner(addr, ethereum) + // Give it some time to connect with peers time.Sleep(3 * time.Second) - /* - for ethereum.IsUpToDate() == false { - time.Sleep(5 * time.Second) - } - */ - ethutil.Config.Log.Infoln("Miner started") - - miner := ethminer.NewDefaultMiner(addr, ethereum) miner.Start() }() } + +func StopMining(ethereum *eth.Ethereum) bool { + if ethereum.Mining { + miner.Stop() + + ethutil.Config.Log.Infoln("Miner stopped") + + ethereum.Mining = false + + return true + } + + return false +} + +func StartMining(ethereum *eth.Ethereum) bool { + if !ethereum.Mining { + DoMining(ethereum) + + return true + } + + return false +}