aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorobscuren <geffobscura@gmail.com>2015-04-21 03:57:00 +0800
committerobscuren <geffobscura@gmail.com>2015-04-21 03:57:00 +0800
commit79bef9eb164a1040a254be5e8a8844eeb37daf8e (patch)
treeab5452c23886fdb309c87d59dec5f8183c9c8097
parentb8160cc6d495678815738d46414e77ec89933f16 (diff)
parent99e825ad96e7b3f655f170a52ad6e408b6feb311 (diff)
downloadgo-tangerine-79bef9eb164a1040a254be5e8a8844eeb37daf8e.tar
go-tangerine-79bef9eb164a1040a254be5e8a8844eeb37daf8e.tar.gz
go-tangerine-79bef9eb164a1040a254be5e8a8844eeb37daf8e.tar.bz2
go-tangerine-79bef9eb164a1040a254be5e8a8844eeb37daf8e.tar.lz
go-tangerine-79bef9eb164a1040a254be5e8a8844eeb37daf8e.tar.xz
go-tangerine-79bef9eb164a1040a254be5e8a8844eeb37daf8e.tar.zst
go-tangerine-79bef9eb164a1040a254be5e8a8844eeb37daf8e.zip
Merge branch 'develop' of github.com-obscure:obscuren/go-ethereum into develop
-rw-r--r--cmd/geth/admin.go8
-rw-r--r--cmd/geth/main.go17
-rw-r--r--cmd/utils/flags.go53
-rw-r--r--rpc/http.go25
-rw-r--r--rpc/types.go99
5 files changed, 176 insertions, 26 deletions
diff --git a/cmd/geth/admin.go b/cmd/geth/admin.go
index defbb43ec..bd09291bf 100644
--- a/cmd/geth/admin.go
+++ b/cmd/geth/admin.go
@@ -28,6 +28,7 @@ func (js *jsre) adminBindings() {
admin := t.Object()
admin.Set("suggestPeer", js.suggestPeer)
admin.Set("startRPC", js.startRPC)
+ admin.Set("stopRPC", js.stopRPC)
admin.Set("nodeInfo", js.nodeInfo)
admin.Set("peers", js.peers)
admin.Set("newAccount", js.newAccount)
@@ -226,6 +227,13 @@ func (js *jsre) startRPC(call otto.FunctionCall) otto.Value {
return otto.TrueValue()
}
+func (js *jsre) stopRPC(call otto.FunctionCall) otto.Value {
+ if rpc.Stop() == nil {
+ return otto.TrueValue()
+ }
+ return otto.FalseValue()
+}
+
func (js *jsre) suggestPeer(call otto.FunctionCall) otto.Value {
nodeURL, err := call.Argument(0).ToString()
if err != nil {
diff --git a/cmd/geth/main.go b/cmd/geth/main.go
index 97d358407..ddbd1f129 100644
--- a/cmd/geth/main.go
+++ b/cmd/geth/main.go
@@ -24,8 +24,6 @@ import (
"bufio"
"fmt"
"io/ioutil"
- "log"
- "net/http"
"os"
"runtime"
"strconv"
@@ -237,6 +235,7 @@ JavaScript API. See https://github.com/ethereum/go-ethereum/wiki/Javascipt-Conso
utils.RPCEnabledFlag,
utils.RPCListenAddrFlag,
utils.RPCPortFlag,
+ utils.WhisperEnabledFlag,
utils.VMDebugFlag,
utils.ProtocolVersionFlag,
utils.NetworkIdFlag,
@@ -247,6 +246,14 @@ JavaScript API. See https://github.com/ethereum/go-ethereum/wiki/Javascipt-Conso
utils.LogVModuleFlag,
utils.LogFileFlag,
utils.LogJSONFlag,
+ utils.PProfEanbledFlag,
+ utils.PProfPortFlag,
+ }
+ app.Before = func(ctx *cli.Context) error {
+ if ctx.GlobalBool(utils.PProfEanbledFlag.Name) {
+ utils.StartPProf(ctx)
+ }
+ return nil
}
// missing:
@@ -261,11 +268,6 @@ JavaScript API. See https://github.com/ethereum/go-ethereum/wiki/Javascipt-Conso
}
func main() {
- // Start up the default http server for pprof
- go func() {
- log.Println(http.ListenAndServe("localhost:6060", nil))
- }()
-
fmt.Printf("Welcome to the FRONTIER\n")
runtime.GOMAXPROCS(runtime.NumCPU())
defer logger.Flush()
@@ -337,6 +339,7 @@ func unlockAccount(ctx *cli.Context, am *accounts.Manager, account string) (pass
}
func startEth(ctx *cli.Context, eth *eth.Ethereum) {
+ // Start Ethereum itself
utils.StartEthereum(eth)
am := eth.AccountManager()
diff --git a/cmd/utils/flags.go b/cmd/utils/flags.go
index f70f4243e..b8f3982e2 100644
--- a/cmd/utils/flags.go
+++ b/cmd/utils/flags.go
@@ -2,6 +2,9 @@ package utils
import (
"crypto/ecdsa"
+ "fmt"
+ "log"
+ "net/http"
"os"
"path"
"runtime"
@@ -140,10 +143,33 @@ var (
Usage: "Send json structured log output to a file or '-' for standard output (default: no json output)",
Value: "",
}
+ LogToStdErrFlag = cli.BoolFlag{
+ Name: "logtostderr",
+ Usage: "Logs are written to standard error instead of to files.",
+ }
+ LogVModuleFlag = cli.GenericFlag{
+ Name: "vmodule",
+ Usage: "The syntax of the argument is a comma-separated list of pattern=N, where pattern is a literal file name (minus the \".go\" suffix) or \"glob\" pattern and N is a V level.",
+ Value: glog.GetVModule(),
+ }
VMDebugFlag = cli.BoolFlag{
Name: "vmdebug",
Usage: "Virtual Machine debug output",
}
+ BacktraceAtFlag = cli.GenericFlag{
+ Name: "backtrace_at",
+ Usage: "When set to a file and line number holding a logging statement a stack trace will be written to the Info log",
+ Value: glog.GetTraceLocation(),
+ }
+ PProfEanbledFlag = cli.BoolFlag{
+ Name: "pprof",
+ Usage: "Whether the profiling server should be enabled",
+ }
+ PProfPortFlag = cli.IntFlag{
+ Name: "pprofport",
+ Usage: "Port on which the profiler should listen",
+ Value: 6060,
+ }
// RPC settings
RPCEnabledFlag = cli.BoolFlag{
@@ -194,25 +220,15 @@ var (
Usage: "Port mapping mechanism (any|none|upnp|pmp|extip:<IP>)",
Value: "any",
}
+ WhisperEnabledFlag = cli.BoolFlag{
+ Name: "shh",
+ Usage: "Whether the whisper sub-protocol is enabled",
+ }
JSpathFlag = cli.StringFlag{
Name: "jspath",
Usage: "JS library path to be used with console and js subcommands",
Value: ".",
}
- BacktraceAtFlag = cli.GenericFlag{
- Name: "backtrace_at",
- Usage: "When set to a file and line number holding a logging statement a stack trace will be written to the Info log",
- Value: glog.GetTraceLocation(),
- }
- LogToStdErrFlag = cli.BoolFlag{
- Name: "logtostderr",
- Usage: "Logs are written to standard error instead of to files.",
- }
- LogVModuleFlag = cli.GenericFlag{
- Name: "vmodule",
- Usage: "The syntax of the argument is a comma-separated list of pattern=N, where pattern is a literal file name (minus the \".go\" suffix) or \"glob\" pattern and N is a V level.",
- Value: glog.GetVModule(),
- }
)
func GetNAT(ctx *cli.Context) nat.Interface {
@@ -274,7 +290,7 @@ func MakeEthConfig(clientID, version string, ctx *cli.Context) *eth.Config {
NAT: GetNAT(ctx),
NatSpec: ctx.GlobalBool(NatspecEnabledFlag.Name),
NodeKey: GetNodeKey(ctx),
- Shh: true,
+ Shh: ctx.GlobalBool(WhisperEnabledFlag.Name),
Dial: true,
BootNodes: ctx.GlobalString(BootnodesFlag.Name),
}
@@ -324,3 +340,10 @@ func StartRPC(eth *eth.Ethereum, ctx *cli.Context) {
xeth := xeth.New(eth, nil)
_ = rpc.Start(xeth, config)
}
+
+func StartPProf(ctx *cli.Context) {
+ address := fmt.Sprintf("localhost:%d", ctx.GlobalInt(PProfPortFlag.Name))
+ go func() {
+ log.Println(http.ListenAndServe(address, nil))
+ }()
+}
diff --git a/rpc/http.go b/rpc/http.go
index 790442a28..f9c646908 100644
--- a/rpc/http.go
+++ b/rpc/http.go
@@ -5,7 +5,6 @@ import (
"fmt"
"io"
"io/ioutil"
- "net"
"net/http"
"github.com/ethereum/go-ethereum/logger"
@@ -15,6 +14,7 @@ import (
)
var rpclogger = logger.NewLogger("RPC")
+var rpclistener *stoppableTCPListener
const (
jsonrpcver = "2.0"
@@ -22,11 +22,19 @@ const (
)
func Start(pipe *xeth.XEth, config RpcConfig) error {
- l, err := net.Listen("tcp", fmt.Sprintf("%s:%d", config.ListenAddress, config.ListenPort))
+ if rpclistener != nil {
+ if fmt.Sprintf("%s:%d", config.ListenAddress, config.ListenPort) != rpclistener.Addr().String() {
+ return fmt.Errorf("RPC service already running on %s ", rpclistener.Addr().String())
+ }
+ return nil // RPC service already running on given host/port
+ }
+
+ l, err := newStoppableTCPListener(fmt.Sprintf("%s:%d", config.ListenAddress, config.ListenPort))
if err != nil {
rpclogger.Errorf("Can't listen on %s:%d: %v", config.ListenAddress, config.ListenPort, err)
return err
}
+ rpclistener = l
var handler http.Handler
if len(config.CorsDomain) > 0 {
@@ -35,9 +43,9 @@ func Start(pipe *xeth.XEth, config RpcConfig) error {
opts.AllowedOrigins = []string{config.CorsDomain}
c := cors.New(opts)
- handler = c.Handler(JSONRPC(pipe))
+ handler = newStoppableHandler(c.Handler(JSONRPC(pipe)), l.stop)
} else {
- handler = JSONRPC(pipe)
+ handler = newStoppableHandler(JSONRPC(pipe), l.stop)
}
go http.Serve(l, handler)
@@ -45,6 +53,15 @@ func Start(pipe *xeth.XEth, config RpcConfig) error {
return nil
}
+func Stop() error {
+ if rpclistener != nil {
+ rpclistener.Stop()
+ rpclistener = nil
+ }
+
+ return nil
+}
+
// JSONRPC returns a handler that implements the Ethereum JSON-RPC API.
func JSONRPC(pipe *xeth.XEth) http.Handler {
api := NewEthereumApi(pipe)
diff --git a/rpc/types.go b/rpc/types.go
index bc9a46ed5..1784759a4 100644
--- a/rpc/types.go
+++ b/rpc/types.go
@@ -23,6 +23,13 @@ import (
"math/big"
"strings"
+ "errors"
+ "net"
+ "net/http"
+ "time"
+
+ "io"
+
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
)
@@ -257,3 +264,95 @@ type RpcErrorObject struct {
Message string `json:"message"`
// Data interface{} `json:"data"`
}
+
+type listenerHasStoppedError struct {
+ msg string
+}
+
+func (self listenerHasStoppedError) Error() string {
+ return self.msg
+}
+
+var listenerStoppedError = listenerHasStoppedError{"Listener stopped"}
+
+// When https://github.com/golang/go/issues/4674 is fixed this could be replaced
+type stoppableTCPListener struct {
+ *net.TCPListener
+ stop chan struct{} // closed when the listener must stop
+}
+
+// Wraps the default handler and checks if the RPC service was stopped. In that case it returns an
+// error indicating that the service was stopped. This will only happen for connections which are
+// kept open (HTTP keep-alive) when the RPC service was shutdown.
+func newStoppableHandler(h http.Handler, stop chan struct{}) http.Handler {
+ return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ select {
+ case <-stop:
+ w.Header().Set("Content-Type", "application/json")
+ jsonerr := &RpcErrorObject{-32603, "RPC service stopped"}
+ send(w, &RpcErrorResponse{Jsonrpc: jsonrpcver, Id: nil, Error: jsonerr})
+ default:
+ h.ServeHTTP(w, r)
+ }
+ })
+}
+
+// Stop the listener and all accepted and still active connections.
+func (self *stoppableTCPListener) Stop() {
+ close(self.stop)
+}
+
+func newStoppableTCPListener(addr string) (*stoppableTCPListener, error) {
+ wl, err := net.Listen("tcp", addr)
+ if err != nil {
+ return nil, err
+ }
+
+ if tcpl, ok := wl.(*net.TCPListener); ok {
+ stop := make(chan struct{})
+ l := &stoppableTCPListener{tcpl, stop}
+ return l, nil
+ }
+
+ return nil, errors.New("Unable to create TCP listener for RPC service")
+}
+
+func (self *stoppableTCPListener) Accept() (net.Conn, error) {
+ for {
+ self.SetDeadline(time.Now().Add(time.Duration(1 * time.Second)))
+ c, err := self.TCPListener.AcceptTCP()
+
+ select {
+ case <-self.stop:
+ if c != nil { // accept timeout
+ c.Close()
+ }
+ self.TCPListener.Close()
+ return nil, listenerStoppedError
+ default:
+ }
+
+ if err != nil {
+ if netErr, ok := err.(net.Error); ok && netErr.Timeout() && netErr.Temporary() {
+ continue // regular timeout
+ }
+ }
+
+ return &closableConnection{c, self.stop}, err
+ }
+}
+
+type closableConnection struct {
+ *net.TCPConn
+ closed chan struct{}
+}
+
+func (self *closableConnection) Read(b []byte) (n int, err error) {
+ select {
+ case <-self.closed:
+ self.TCPConn.Close()
+ return 0, io.EOF
+ default:
+ return self.TCPConn.Read(b)
+ }
+}