Merge pull request #2175 from karalabe/refactor-http-rpc

cmd, common, node, rpc: move HTTP RPC into node, drop singleton aspect
This commit is contained in:
Péter Szilágyi 2016-02-13 14:53:48 +02:00
commit 770b29fd80
19 changed files with 660 additions and 546 deletions

View File

@ -37,22 +37,21 @@ import (
"github.com/ethereum/go-ethereum/eth" "github.com/ethereum/go-ethereum/eth"
"github.com/ethereum/go-ethereum/ethdb" "github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/node" "github.com/ethereum/go-ethereum/node"
"github.com/ethereum/go-ethereum/cmd/utils"
) )
const ( const (
testSolcPath = "" testSolcPath = ""
solcVersion = "0.9.23" solcVersion = "0.9.23"
testKey = "e6fab74a43941f82d89cb7faa408e227cdad3153c4720e540e855c19b15e6674" testKey = "e6fab74a43941f82d89cb7faa408e227cdad3153c4720e540e855c19b15e6674"
testAddress = "0x8605cdbbdb6d264aa742e77020dcbc58fcdce182" testAddress = "0x8605cdbbdb6d264aa742e77020dcbc58fcdce182"
testBalance = "10000000000000000000" testBalance = "10000000000000000000"
// of empty string // of empty string
testHash = "0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470" testHash = "0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470"
) )
var ( var (
versionRE = regexp.MustCompile(strconv.Quote(`"compilerVersion":"` + solcVersion + `"`)) versionRE = regexp.MustCompile(strconv.Quote(`"compilerVersion":"` + solcVersion + `"`))
testNodeKey = crypto.ToECDSA(common.Hex2Bytes("4b50fa71f5c3eeb8fdc452224b2395af2fcc3d125e06c32c82e048c0559db03f")) testNodeKey = crypto.ToECDSA(common.Hex2Bytes("4b50fa71f5c3eeb8fdc452224b2395af2fcc3d125e06c32c82e048c0559db03f"))
testGenesis = `{"` + testAddress[2:] + `": {"balance": "` + testBalance + `"}}` testGenesis = `{"` + testAddress[2:] + `": {"balance": "` + testBalance + `"}}`
) )
@ -141,8 +140,10 @@ func testREPL(t *testing.T, config func(*eth.Config)) (string, *testjethre, *nod
stack.Service(&ethereum) stack.Service(&ethereum)
assetPath := filepath.Join(os.Getenv("GOPATH"), "src", "github.com", "ethereum", "go-ethereum", "cmd", "mist", "assets", "ext") assetPath := filepath.Join(os.Getenv("GOPATH"), "src", "github.com", "ethereum", "go-ethereum", "cmd", "mist", "assets", "ext")
//client := comms.NewInProcClient(codec.JSON) client, err := stack.Attach()
client := utils.NewInProcRPCClient(stack) if err != nil {
t.Fatalf("failed to attach to node: %v", err)
}
tf := &testjethre{client: ethereum.HTTPClient()} tf := &testjethre{client: ethereum.HTTPClient()}
repl := newJSRE(stack, assetPath, "", client, false) repl := newJSRE(stack, assetPath, "", client, false)
tf.jsre = repl tf.jsre = repl
@ -152,9 +153,6 @@ func testREPL(t *testing.T, config func(*eth.Config)) (string, *testjethre, *nod
func TestNodeInfo(t *testing.T) { func TestNodeInfo(t *testing.T) {
t.Skip("broken after p2p update") t.Skip("broken after p2p update")
tmp, repl, ethereum := testJEthRE(t) tmp, repl, ethereum := testJEthRE(t)
if err := ethereum.Start(); err != nil {
t.Fatalf("error starting ethereum: %v", err)
}
defer ethereum.Stop() defer ethereum.Stop()
defer os.RemoveAll(tmp) defer os.RemoveAll(tmp)
@ -167,8 +165,8 @@ func TestAccounts(t *testing.T) {
defer node.Stop() defer node.Stop()
defer os.RemoveAll(tmp) defer os.RemoveAll(tmp)
checkEvalJSON(t, repl, `eth.accounts`, `["` + testAddress + `"]`) checkEvalJSON(t, repl, `eth.accounts`, `["`+testAddress+`"]`)
checkEvalJSON(t, repl, `eth.coinbase`, `"` + testAddress + `"`) checkEvalJSON(t, repl, `eth.coinbase`, `"`+testAddress+`"`)
val, err := repl.re.Run(`jeth.newAccount("password")`) val, err := repl.re.Run(`jeth.newAccount("password")`)
if err != nil { if err != nil {
t.Errorf("expected no error, got %v", err) t.Errorf("expected no error, got %v", err)
@ -178,7 +176,7 @@ func TestAccounts(t *testing.T) {
t.Errorf("address not hex: %q", addr) t.Errorf("address not hex: %q", addr)
} }
checkEvalJSON(t, repl, `eth.accounts`, `["` + testAddress + `","` + addr + `"]`) checkEvalJSON(t, repl, `eth.accounts`, `["`+testAddress+`","`+addr+`"]`)
} }
@ -206,13 +204,13 @@ func TestBlockChain(t *testing.T) {
node.Service(&ethereum) node.Service(&ethereum)
ethereum.BlockChain().Reset() ethereum.BlockChain().Reset()
checkEvalJSON(t, repl, `admin.exportChain(` + tmpfileq + `)`, `true`) checkEvalJSON(t, repl, `admin.exportChain(`+tmpfileq+`)`, `true`)
if _, err := os.Stat(tmpfile); err != nil { if _, err := os.Stat(tmpfile); err != nil {
t.Fatal(err) t.Fatal(err)
} }
// check import, verify that dumpBlock gives the same result. // check import, verify that dumpBlock gives the same result.
checkEvalJSON(t, repl, `admin.importChain(` + tmpfileq + `)`, `true`) checkEvalJSON(t, repl, `admin.importChain(`+tmpfileq+`)`, `true`)
checkEvalJSON(t, repl, `debug.dumpBlock(eth.blockNumber)`, beforeExport) checkEvalJSON(t, repl, `debug.dumpBlock(eth.blockNumber)`, beforeExport)
} }
@ -240,7 +238,7 @@ func TestCheckTestAccountBalance(t *testing.T) {
defer os.RemoveAll(tmp) defer os.RemoveAll(tmp)
repl.re.Run(`primary = "` + testAddress + `"`) repl.re.Run(`primary = "` + testAddress + `"`)
checkEvalJSON(t, repl, `eth.getBalance(primary)`, `"` + testBalance + `"`) checkEvalJSON(t, repl, `eth.getBalance(primary)`, `"`+testBalance+`"`)
} }
func TestSignature(t *testing.T) { func TestSignature(t *testing.T) {
@ -301,11 +299,11 @@ func TestContract(t *testing.T) {
*/ */
source := `contract test {\n` + source := `contract test {\n` +
" /// @notice Will multiply `a` by 7." + `\n` + " /// @notice Will multiply `a` by 7." + `\n` +
` function multiply(uint a) returns(uint d) {\n` + ` function multiply(uint a) returns(uint d) {\n` +
` return a * 7;\n` + ` return a * 7;\n` +
` }\n` + ` }\n` +
`}\n` `}\n`
if checkEvalJSON(t, repl, `admin.stopNatSpec()`, `true`) != nil { if checkEvalJSON(t, repl, `admin.stopNatSpec()`, `true`) != nil {
return return
@ -315,10 +313,10 @@ func TestContract(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("%v", err) t.Fatalf("%v", err)
} }
if checkEvalJSON(t, repl, `primary = eth.accounts[0]`, `"` + testAddress + `"`) != nil { if checkEvalJSON(t, repl, `primary = eth.accounts[0]`, `"`+testAddress+`"`) != nil {
return return
} }
if checkEvalJSON(t, repl, `source = "` + source + `"`, `"` + source + `"`) != nil { if checkEvalJSON(t, repl, `source = "`+source+`"`, `"`+source+`"`) != nil {
return return
} }
@ -396,7 +394,7 @@ multiply7 = Multiply7.at(contractaddress);
var contentHash = `"0x86d2b7cf1e72e9a7a3f8d96601f0151742a2f780f1526414304fbe413dc7f9bd"` var contentHash = `"0x86d2b7cf1e72e9a7a3f8d96601f0151742a2f780f1526414304fbe413dc7f9bd"`
if sol != nil && solcVersion != sol.Version() { if sol != nil && solcVersion != sol.Version() {
modContractInfo := versionRE.ReplaceAll(contractInfo, []byte(`"compilerVersion":"` + sol.Version() + `"`)) modContractInfo := versionRE.ReplaceAll(contractInfo, []byte(`"compilerVersion":"`+sol.Version()+`"`))
fmt.Printf("modified contractinfo:\n%s\n", modContractInfo) fmt.Printf("modified contractinfo:\n%s\n", modContractInfo)
contentHash = `"` + common.ToHex(crypto.Sha3([]byte(modContractInfo))) + `"` contentHash = `"` + common.ToHex(crypto.Sha3([]byte(modContractInfo))) + `"`
} }
@ -481,7 +479,7 @@ func processTxs(repl *testjethre, t *testing.T, expTxc int) bool {
repl.wait <- height repl.wait <- height
select { select {
case <-timer.C: case <-timer.C:
// if times out make sure the xeth loop does not block // if times out make sure the xeth loop does not block
go func() { go func() {
select { select {
case repl.wait <- nil: case repl.wait <- nil:

View File

@ -399,7 +399,7 @@ func attach(ctx *cli.Context) {
// attach to a running geth instance // attach to a running geth instance
client, err := utils.NewRemoteRPCClient(ctx) client, err := utils.NewRemoteRPCClient(ctx)
if err != nil { if err != nil {
utils.Fatalf("Unable to attach to geth - %v", err) utils.Fatalf("Unable to attach to geth: %v", err)
} }
repl := newLightweightJSRE( repl := newLightweightJSRE(
@ -425,8 +425,10 @@ func console(ctx *cli.Context) {
startNode(ctx, node) startNode(ctx, node)
// Attach to the newly started node, and either execute script or become interactive // Attach to the newly started node, and either execute script or become interactive
client := utils.NewInProcRPCClient(node) client, err := node.Attach()
if err != nil {
utils.Fatalf("Failed to attach to the inproc geth: %v", err)
}
repl := newJSRE(node, repl := newJSRE(node,
ctx.GlobalString(utils.JSpathFlag.Name), ctx.GlobalString(utils.JSpathFlag.Name),
ctx.GlobalString(utils.RPCCORSDomainFlag.Name), ctx.GlobalString(utils.RPCCORSDomainFlag.Name),
@ -449,8 +451,10 @@ func execScripts(ctx *cli.Context) {
startNode(ctx, node) startNode(ctx, node)
// Attach to the newly started node and execute the given scripts // Attach to the newly started node and execute the given scripts
client := utils.NewInProcRPCClient(node) client, err := node.Attach()
if err != nil {
utils.Fatalf("Failed to attach to the inproc geth: %v", err)
}
repl := newJSRE(node, repl := newJSRE(node,
ctx.GlobalString(utils.JSpathFlag.Name), ctx.GlobalString(utils.JSpathFlag.Name),
ctx.GlobalString(utils.RPCCORSDomainFlag.Name), ctx.GlobalString(utils.RPCCORSDomainFlag.Name),
@ -503,16 +507,6 @@ func startNode(ctx *cli.Context, stack *node.Node) {
} }
} }
// Start auxiliary services if enabled // Start auxiliary services if enabled
if ctx.GlobalBool(utils.RPCEnabledFlag.Name) {
if err := utils.StartRPC(stack, ctx); err != nil {
utils.Fatalf("Failed to start RPC: %v", err)
}
}
if ctx.GlobalBool(utils.WSEnabledFlag.Name) {
if err := utils.StartWS(stack, ctx); err != nil {
utils.Fatalf("Failed to start WS: %v", err)
}
}
if ctx.GlobalBool(utils.MiningEnabledFlag.Name) { if ctx.GlobalBool(utils.MiningEnabledFlag.Name) {
if err := ethereum.StartMining(ctx.GlobalInt(utils.MinerThreadsFlag.Name), ctx.GlobalString(utils.MiningGPUFlag.Name)); err != nil { if err := ethereum.StartMining(ctx.GlobalInt(utils.MinerThreadsFlag.Name), ctx.GlobalString(utils.MiningGPUFlag.Name)); err != nil {
utils.Fatalf("Failed to start mining: %v", err) utils.Fatalf("Failed to start mining: %v", err)

View File

@ -36,7 +36,7 @@ import (
var ( var (
monitorCommandAttachFlag = cli.StringFlag{ monitorCommandAttachFlag = cli.StringFlag{
Name: "attach", Name: "attach",
Value: "ipc:" + node.DefaultIpcEndpoint(), Value: "ipc:" + node.DefaultIPCEndpoint(),
Usage: "API endpoint to attach to", Usage: "API endpoint to attach to",
} }
monitorCommandRowsFlag = cli.IntFlag{ monitorCommandRowsFlag = cli.IntFlag{

View File

@ -18,7 +18,6 @@
package main package main
import ( import (
"errors"
"flag" "flag"
"io/ioutil" "io/ioutil"
"log" "log"
@ -26,10 +25,10 @@ import (
"os/signal" "os/signal"
"github.com/ethereum/go-ethereum/accounts" "github.com/ethereum/go-ethereum/accounts"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/eth" "github.com/ethereum/go-ethereum/eth"
"github.com/ethereum/go-ethereum/ethdb" "github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/logger"
"github.com/ethereum/go-ethereum/logger/glog" "github.com/ethereum/go-ethereum/logger/glog"
"github.com/ethereum/go-ethereum/node" "github.com/ethereum/go-ethereum/node"
"github.com/ethereum/go-ethereum/tests" "github.com/ethereum/go-ethereum/tests"
@ -84,12 +83,6 @@ func main() {
} }
log.Println("Initial test suite passed...") log.Println("Initial test suite passed...")
// Start the RPC interface and wait until terminated
if err := StartRPC(stack); err != nil {
log.Fatalf("Failed to start RPC interface: %v", err)
}
log.Println("RPC Interface started, accepting requests...")
quit := make(chan os.Signal, 1) quit := make(chan os.Signal, 1)
signal.Notify(quit, os.Interrupt) signal.Notify(quit, os.Interrupt)
<-quit <-quit
@ -99,7 +92,16 @@ func main() {
// keystore path and initial pre-state. // keystore path and initial pre-state.
func MakeSystemNode(keydir string, privkey string, test *tests.BlockTest) (*node.Node, error) { func MakeSystemNode(keydir string, privkey string, test *tests.BlockTest) (*node.Node, error) {
// Create a networkless protocol stack // Create a networkless protocol stack
stack, err := node.New(&node.Config{IpcPath: node.DefaultIpcEndpoint(), NoDiscovery: true}) stack, err := node.New(&node.Config{
IPCPath: node.DefaultIPCEndpoint(),
HTTPHost: common.DefaultHTTPHost,
HTTPPort: common.DefaultHTTPPort,
HTTPModules: []string{"admin", "db", "eth", "debug", "miner", "net", "shh", "txpool", "personal", "web3"},
WSHost: common.DefaultWSHost,
WSPort: common.DefaultWSPort,
WSModules: []string{"admin", "db", "eth", "debug", "miner", "net", "shh", "txpool", "personal", "web3"},
NoDiscovery: true,
})
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -164,23 +166,3 @@ func RunTest(stack *node.Node, test *tests.BlockTest) error {
} }
return nil return nil
} }
// StartRPC initializes an RPC interface to the given protocol stack.
func StartRPC(stack *node.Node) error {
/*
web3 := NewPublicWeb3API(stack)
server.RegisterName("web3", web3)
net := NewPublicNetAPI(stack.Server(), ethereum.NetVersion())
server.RegisterName("net", net)
*/
for _, api := range stack.APIs() {
if adminApi, ok := api.Service.(*node.PrivateAdminAPI); ok {
_, err := adminApi.StartRPC("127.0.0.1", 8545, "", "admin,db,eth,debug,miner,net,shh,txpool,personal,web3")
return err
}
}
glog.V(logger.Error).Infof("Unable to start RPC-HTTP interface, could not find admin API")
return errors.New("Unable to start RPC-HTTP interface")
}

View File

@ -17,132 +17,14 @@
package utils package utils
import ( import (
"encoding/json"
"fmt" "fmt"
"strings" "strings"
"github.com/codegangsta/cli" "github.com/codegangsta/cli"
"github.com/ethereum/go-ethereum/eth"
"github.com/ethereum/go-ethereum/logger"
"github.com/ethereum/go-ethereum/logger/glog"
"github.com/ethereum/go-ethereum/node" "github.com/ethereum/go-ethereum/node"
"github.com/ethereum/go-ethereum/rpc" "github.com/ethereum/go-ethereum/rpc"
) )
// NewInProcRPCClient will start a new RPC server for the given node and returns a client to interact with it.
func NewInProcRPCClient(stack *node.Node) *inProcClient {
server := rpc.NewServer()
offered := stack.APIs()
for _, api := range offered {
server.RegisterName(api.Namespace, api.Service)
}
web3 := node.NewPublicWeb3API(stack)
server.RegisterName("web3", web3)
var ethereum *eth.Ethereum
if err := stack.Service(&ethereum); err == nil {
net := eth.NewPublicNetAPI(stack.Server(), ethereum.NetVersion())
server.RegisterName("net", net)
} else {
glog.V(logger.Warn).Infof("%v\n", err)
}
buf := &buf{
requests: make(chan []byte),
responses: make(chan []byte),
}
client := &inProcClient{
server: server,
buf: buf,
}
go func() {
server.ServeCodec(rpc.NewJSONCodec(client.buf))
}()
return client
}
// buf represents the connection between the RPC server and console
type buf struct {
readBuf []byte // store remaining request bytes after a partial read
requests chan []byte // list with raw serialized requests
responses chan []byte // list with raw serialized responses
}
// will read the next request in json format
func (b *buf) Read(p []byte) (int, error) {
// last read didn't read entire request, return remaining bytes
if len(b.readBuf) > 0 {
n := copy(p, b.readBuf)
if n < len(b.readBuf) {
b.readBuf = b.readBuf[:n]
} else {
b.readBuf = b.readBuf[:0]
}
return n, nil
}
// read next request
req := <-b.requests
n := copy(p, req)
if n < len(req) {
// buf too small, store remaining chunk for next read
b.readBuf = req[n:]
}
return n, nil
}
// Write send the given buffer to the backend
func (b *buf) Write(p []byte) (n int, err error) {
b.responses <- p
return len(p), nil
}
// Close cleans up obtained resources.
func (b *buf) Close() error {
close(b.requests)
close(b.responses)
return nil
}
// inProcClient starts a RPC server and uses buf to communicate with it.
type inProcClient struct {
server *rpc.Server
buf *buf
}
// Close will stop the RPC server
func (c *inProcClient) Close() {
c.server.Stop()
}
// Send a msg to the endpoint
func (c *inProcClient) Send(msg interface{}) error {
d, err := json.Marshal(msg)
if err != nil {
return err
}
c.buf.requests <- d
return nil
}
// Recv reads a message and tries to parse it into the given msg
func (c *inProcClient) Recv(msg interface{}) error {
data := <-c.buf.responses
return json.Unmarshal(data, &msg)
}
// Returns the collection of modules the RPC server offers.
func (c *inProcClient) SupportedModules() (map[string]string, error) {
return rpc.SupportedModules(c)
}
// NewRemoteRPCClient returns a RPC client which connects to a running geth instance. // NewRemoteRPCClient returns a RPC client which connects to a running geth instance.
// Depending on the given context this can either be a IPC or a HTTP client. // Depending on the given context this can either be a IPC or a HTTP client.
func NewRemoteRPCClient(ctx *cli.Context) (rpc.Client, error) { func NewRemoteRPCClient(ctx *cli.Context) (rpc.Client, error) {
@ -151,7 +33,7 @@ func NewRemoteRPCClient(ctx *cli.Context) (rpc.Client, error) {
return NewRemoteRPCClientFromString(endpoint) return NewRemoteRPCClientFromString(endpoint)
} }
// use IPC by default // use IPC by default
return rpc.NewIPCClient(node.DefaultIpcEndpoint()) return rpc.NewIPCClient(node.DefaultIPCEndpoint())
} }
// NewRemoteRPCClientFromString returns a RPC client which connects to the given // NewRemoteRPCClientFromString returns a RPC client which connects to the given
@ -169,6 +51,5 @@ func NewRemoteRPCClientFromString(endpoint string) (rpc.Client, error) {
if strings.HasPrefix(endpoint, "ws:") { if strings.HasPrefix(endpoint, "ws:") {
return rpc.NewWSClient(endpoint) return rpc.NewWSClient(endpoint)
} }
return nil, fmt.Errorf("invalid endpoint") return nil, fmt.Errorf("invalid endpoint")
} }

View File

@ -18,7 +18,6 @@ package utils
import ( import (
"crypto/ecdsa" "crypto/ecdsa"
"errors"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"math" "math"
@ -233,12 +232,12 @@ var (
RPCListenAddrFlag = cli.StringFlag{ RPCListenAddrFlag = cli.StringFlag{
Name: "rpcaddr", Name: "rpcaddr",
Usage: "HTTP-RPC server listening interface", Usage: "HTTP-RPC server listening interface",
Value: "127.0.0.1", Value: common.DefaultHTTPHost,
} }
RPCPortFlag = cli.IntFlag{ RPCPortFlag = cli.IntFlag{
Name: "rpcport", Name: "rpcport",
Usage: "HTTP-RPC server listening port", Usage: "HTTP-RPC server listening port",
Value: 8545, Value: common.DefaultHTTPPort,
} }
RPCCORSDomainFlag = cli.StringFlag{ RPCCORSDomainFlag = cli.StringFlag{
Name: "rpccorsdomain", Name: "rpccorsdomain",
@ -248,7 +247,7 @@ var (
RPCApiFlag = cli.StringFlag{ RPCApiFlag = cli.StringFlag{
Name: "rpcapi", Name: "rpcapi",
Usage: "API's offered over the HTTP-RPC interface", Usage: "API's offered over the HTTP-RPC interface",
Value: rpc.DefaultHttpRpcApis, Value: rpc.DefaultHTTPApis,
} }
IPCDisabledFlag = cli.BoolFlag{ IPCDisabledFlag = cli.BoolFlag{
Name: "ipcdisable", Name: "ipcdisable",
@ -257,12 +256,12 @@ var (
IPCApiFlag = cli.StringFlag{ IPCApiFlag = cli.StringFlag{
Name: "ipcapi", Name: "ipcapi",
Usage: "API's offered over the IPC-RPC interface", Usage: "API's offered over the IPC-RPC interface",
Value: rpc.DefaultIpcApis, Value: rpc.DefaultIPCApis,
} }
IPCPathFlag = DirectoryFlag{ IPCPathFlag = DirectoryFlag{
Name: "ipcpath", Name: "ipcpath",
Usage: "Filename for IPC socket/pipe within the datadir (explicit paths escape it)", Usage: "Filename for IPC socket/pipe within the datadir (explicit paths escape it)",
Value: DirectoryString{common.DefaultIpcSocket()}, Value: DirectoryString{common.DefaultIPCSocket},
} }
WSEnabledFlag = cli.BoolFlag{ WSEnabledFlag = cli.BoolFlag{
Name: "ws", Name: "ws",
@ -271,21 +270,21 @@ var (
WSListenAddrFlag = cli.StringFlag{ WSListenAddrFlag = cli.StringFlag{
Name: "wsaddr", Name: "wsaddr",
Usage: "WS-RPC server listening interface", Usage: "WS-RPC server listening interface",
Value: "127.0.0.1", Value: common.DefaultWSHost,
} }
WSPortFlag = cli.IntFlag{ WSPortFlag = cli.IntFlag{
Name: "wsport", Name: "wsport",
Usage: "WS-RPC server listening port", Usage: "WS-RPC server listening port",
Value: 8546, Value: common.DefaultWSPort,
} }
WSApiFlag = cli.StringFlag{ WSApiFlag = cli.StringFlag{
Name: "wsapi", Name: "wsapi",
Usage: "API's offered over the WS-RPC interface", Usage: "API's offered over the WS-RPC interface",
Value: rpc.DefaultHttpRpcApis, Value: rpc.DefaultHTTPApis,
} }
WSAllowedDomainsFlag = cli.StringFlag{ WSAllowedDomainsFlag = cli.StringFlag{
Name: "wsdomains", Name: "wsdomains",
Usage: "Domains from which to accept websockets requests", Usage: "Domains from which to accept websockets requests (can be spoofed)",
Value: "", Value: "",
} }
ExecFlag = cli.StringFlag{ ExecFlag = cli.StringFlag{
@ -394,9 +393,9 @@ func MustMakeDataDir(ctx *cli.Context) string {
return "" return ""
} }
// MakeIpcPath creates an IPC path configuration from the set command line flags, // MakeIPCPath creates an IPC path configuration from the set command line flags,
// returning an empty string if IPC was explicitly disabled, or the set path. // returning an empty string if IPC was explicitly disabled, or the set path.
func MakeIpcPath(ctx *cli.Context) string { func MakeIPCPath(ctx *cli.Context) string {
if ctx.GlobalBool(IPCDisabledFlag.Name) { if ctx.GlobalBool(IPCDisabledFlag.Name) {
return "" return ""
} }
@ -482,6 +481,24 @@ func MakeNAT(ctx *cli.Context) nat.Interface {
return natif return natif
} }
// MakeHTTPRpcHost creates the HTTP RPC listener interface string from the set
// command line flags, returning empty if the HTTP endpoint is disabled.
func MakeHTTPRpcHost(ctx *cli.Context) string {
if !ctx.GlobalBool(RPCEnabledFlag.Name) {
return ""
}
return ctx.GlobalString(RPCListenAddrFlag.Name)
}
// MakeWSRpcHost creates the WebSocket RPC listener interface string from the set
// command line flags, returning empty if the HTTP endpoint is disabled.
func MakeWSRpcHost(ctx *cli.Context) string {
if !ctx.GlobalBool(WSEnabledFlag.Name) {
return ""
}
return ctx.GlobalString(WSListenAddrFlag.Name)
}
// MakeGenesisBlock loads up a genesis block from an input file specified in the // MakeGenesisBlock loads up a genesis block from an input file specified in the
// command line, or returns the empty string if none set. // command line, or returns the empty string if none set.
func MakeGenesisBlock(ctx *cli.Context) string { func MakeGenesisBlock(ctx *cli.Context) string {
@ -591,7 +608,6 @@ func MakeSystemNode(name, version string, extra []byte, ctx *cli.Context) *node.
// Configure the node's service container // Configure the node's service container
stackConf := &node.Config{ stackConf := &node.Config{
DataDir: MustMakeDataDir(ctx), DataDir: MustMakeDataDir(ctx),
IpcPath: MakeIpcPath(ctx),
PrivateKey: MakeNodeKey(ctx), PrivateKey: MakeNodeKey(ctx),
Name: MakeNodeName(name, version, ctx), Name: MakeNodeName(name, version, ctx),
NoDiscovery: ctx.GlobalBool(NoDiscoverFlag.Name), NoDiscovery: ctx.GlobalBool(NoDiscoverFlag.Name),
@ -600,6 +616,15 @@ func MakeSystemNode(name, version string, extra []byte, ctx *cli.Context) *node.
NAT: MakeNAT(ctx), NAT: MakeNAT(ctx),
MaxPeers: ctx.GlobalInt(MaxPeersFlag.Name), MaxPeers: ctx.GlobalInt(MaxPeersFlag.Name),
MaxPendingPeers: ctx.GlobalInt(MaxPendingPeersFlag.Name), MaxPendingPeers: ctx.GlobalInt(MaxPendingPeersFlag.Name),
IPCPath: MakeIPCPath(ctx),
HTTPHost: MakeHTTPRpcHost(ctx),
HTTPPort: ctx.GlobalInt(RPCPortFlag.Name),
HTTPCors: ctx.GlobalString(RPCCORSDomainFlag.Name),
HTTPModules: strings.Split(ctx.GlobalString(RPCApiFlag.Name), ","),
WSHost: MakeWSRpcHost(ctx),
WSPort: ctx.GlobalInt(WSPortFlag.Name),
WSDomains: ctx.GlobalString(WSAllowedDomainsFlag.Name),
WSModules: strings.Split(ctx.GlobalString(WSApiFlag.Name), ","),
} }
// Configure the Ethereum service // Configure the Ethereum service
accman := MakeAccountManager(ctx) accman := MakeAccountManager(ctx)
@ -740,48 +765,5 @@ func MakeChain(ctx *cli.Context) (chain *core.BlockChain, chainDb ethdb.Database
if err != nil { if err != nil {
Fatalf("Could not start chainmanager: %v", err) Fatalf("Could not start chainmanager: %v", err)
} }
return chain, chainDb return chain, chainDb
} }
// StartRPC starts a HTTP JSON-RPC API server.
func StartRPC(stack *node.Node, ctx *cli.Context) error {
for _, api := range stack.APIs() {
if adminApi, ok := api.Service.(*node.PrivateAdminAPI); ok {
address := ctx.GlobalString(RPCListenAddrFlag.Name)
port := ctx.GlobalInt(RPCPortFlag.Name)
cors := ctx.GlobalString(RPCCORSDomainFlag.Name)
apiStr := ""
if ctx.GlobalIsSet(RPCApiFlag.Name) {
apiStr = ctx.GlobalString(RPCApiFlag.Name)
}
_, err := adminApi.StartRPC(address, port, cors, apiStr)
return err
}
}
glog.V(logger.Error).Infof("Unable to start RPC-HTTP interface, could not find admin API")
return errors.New("Unable to start RPC-HTTP interface")
}
// StartWS starts a websocket JSON-RPC API server.
func StartWS(stack *node.Node, ctx *cli.Context) error {
for _, api := range stack.APIs() {
if adminApi, ok := api.Service.(*node.PrivateAdminAPI); ok {
address := ctx.GlobalString(WSListenAddrFlag.Name)
port := ctx.GlobalInt(WSAllowedDomainsFlag.Name)
allowedDomains := ctx.GlobalString(WSAllowedDomainsFlag.Name)
apiStr := ""
if ctx.GlobalIsSet(WSApiFlag.Name) {
apiStr = ctx.GlobalString(WSApiFlag.Name)
}
_, err := adminApi.StartWS(address, port, allowedDomains, apiStr)
return err
}
}
glog.V(logger.Error).Infof("Unable to start RPC-WS interface, could not find admin API")
return errors.New("Unable to start RPC-WS interface")
}

48
common/defaults.go Normal file
View File

@ -0,0 +1,48 @@
// Copyright 2016 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
package common
import (
"path/filepath"
"runtime"
)
const (
DefaultIPCSocket = "geth.ipc" // Default (relative) name of the IPC RPC socket
DefaultHTTPHost = "localhost" // Default host interface for the HTTP RPC server
DefaultHTTPPort = 8545 // Default TCP port for the HTTP RPC server
DefaultWSHost = "localhost" // Default host interface for the websocket RPC server
DefaultWSPort = 8546 // Default TCP port for the websocket RPC server
)
// DefaultDataDir is the default data directory to use for the databases and other
// persistence requirements.
func DefaultDataDir() string {
// Try to place the data folder in the user's home dir
home := HomeDir()
if home != "" {
if runtime.GOOS == "darwin" {
return filepath.Join(home, "Library", "Ethereum")
} else if runtime.GOOS == "windows" {
return filepath.Join(home, "AppData", "Roaming", "Ethereum")
} else {
return filepath.Join(home, ".ethereum")
}
}
// As we cannot guess a stable location, return empty and handle later
return ""
}

View File

@ -72,25 +72,3 @@ func HomeDir() string {
} }
return "" return ""
} }
func DefaultDataDir() string {
// Try to place the data folder in the user's home dir
home := HomeDir()
if home != "" {
if runtime.GOOS == "darwin" {
return filepath.Join(home, "Library", "Ethereum")
} else if runtime.GOOS == "windows" {
return filepath.Join(home, "AppData", "Roaming", "Ethereum")
} else {
return filepath.Join(home, ".ethereum")
}
}
// As we cannot guess a stable location, return empty and handle later
return ""
}
// DefaultIpcSocket returns the relative name of the default IPC socket. The path
// resolution is done by a node with other contextual infos.
func DefaultIpcSocket() string {
return "geth.ipc"
}

View File

@ -25,10 +25,7 @@ import (
"github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/p2p" "github.com/ethereum/go-ethereum/p2p"
"github.com/ethereum/go-ethereum/p2p/discover" "github.com/ethereum/go-ethereum/p2p/discover"
"github.com/ethereum/go-ethereum/rpc"
"github.com/rcrowley/go-metrics" "github.com/rcrowley/go-metrics"
"gopkg.in/fatih/set.v0"
) )
// PrivateAdminAPI is the collection of administrative API methods exposed only // PrivateAdminAPI is the collection of administrative API methods exposed only
@ -61,83 +58,55 @@ func (api *PrivateAdminAPI) AddPeer(url string) (bool, error) {
} }
// StartRPC starts the HTTP RPC API server. // StartRPC starts the HTTP RPC API server.
func (api *PrivateAdminAPI) StartRPC(address string, port int, cors string, apis string) (bool, error) { func (api *PrivateAdminAPI) StartRPC(host string, port int, cors string, apis string) (bool, error) {
var offeredAPIs []rpc.API api.node.lock.Lock()
if len(apis) > 0 { defer api.node.lock.Unlock()
namespaces := set.New()
for _, a := range strings.Split(apis, ",") {
namespaces.Add(strings.TrimSpace(a))
}
for _, api := range api.node.APIs() {
if namespaces.Has(api.Namespace) {
offeredAPIs = append(offeredAPIs, api)
}
}
} else { // use by default all public API's
for _, api := range api.node.APIs() {
if api.Public {
offeredAPIs = append(offeredAPIs, api)
}
}
}
if address == "" { if api.node.httpHandler != nil {
address = "127.0.0.1" return false, fmt.Errorf("HTTP RPC already running on %s", api.node.httpEndpoint)
} }
if port == 0 { if err := api.node.startHTTP(fmt.Sprintf("%s:%d", host, port), api.node.rpcAPIs, strings.Split(apis, ","), cors); err != nil {
port = 8545 return false, err
} }
return true, nil
corsDomains := strings.Split(cors, " ")
err := rpc.StartHTTP(address, port, corsDomains, offeredAPIs)
return err == nil, err
} }
// StopRPC terminates an already running HTTP RPC API endpoint. // StopRPC terminates an already running HTTP RPC API endpoint.
func (api *PrivateAdminAPI) StopRPC() (bool, error) { func (api *PrivateAdminAPI) StopRPC() (bool, error) {
err := rpc.StopHTTP() api.node.lock.Lock()
return err == nil, err defer api.node.lock.Unlock()
if api.node.httpHandler == nil {
return false, fmt.Errorf("HTTP RPC not running")
}
api.node.stopHTTP()
return true, nil
} }
// StartWS starts the websocket RPC API server. // StartWS starts the websocket RPC API server.
func (api *PrivateAdminAPI) StartWS(address string, port int, cors string, apis string) (bool, error) { func (api *PrivateAdminAPI) StartWS(host string, port int, cors string, apis string) (bool, error) {
var offeredAPIs []rpc.API api.node.lock.Lock()
if len(apis) > 0 { defer api.node.lock.Unlock()
namespaces := set.New()
for _, a := range strings.Split(apis, ",") {
namespaces.Add(strings.TrimSpace(a))
}
for _, api := range api.node.APIs() {
if namespaces.Has(api.Namespace) {
offeredAPIs = append(offeredAPIs, api)
}
}
} else {
// use by default all public API's
for _, api := range api.node.APIs() {
if api.Public {
offeredAPIs = append(offeredAPIs, api)
}
}
}
if address == "" { if api.node.wsHandler != nil {
address = "127.0.0.1" return false, fmt.Errorf("WebSocket RPC already running on %s", api.node.wsEndpoint)
} }
if port == 0 { if err := api.node.startWS(fmt.Sprintf("%s:%d", host, port), api.node.rpcAPIs, strings.Split(apis, ","), cors); err != nil {
port = 8546 return false, err
} }
return true, nil
corsDomains := strings.Split(cors, " ")
err := rpc.StartWS(address, port, corsDomains, offeredAPIs)
return err == nil, err
} }
// StopRPC terminates an already running websocket RPC API endpoint. // StopRPC terminates an already running websocket RPC API endpoint.
func (api *PrivateAdminAPI) StopWS() (bool, error) { func (api *PrivateAdminAPI) StopWS() (bool, error) {
err := rpc.StopWS() api.node.lock.Lock()
return err == nil, err defer api.node.lock.Unlock()
if api.node.wsHandler == nil {
return false, fmt.Errorf("WebSocket RPC not running")
}
api.node.stopWS()
return true, nil
} }
// PublicAdminAPI is the collection of administrative API methods exposed over // PublicAdminAPI is the collection of administrative API methods exposed over

View File

@ -19,6 +19,7 @@ package node
import ( import (
"crypto/ecdsa" "crypto/ecdsa"
"encoding/json" "encoding/json"
"fmt"
"io/ioutil" "io/ioutil"
"net" "net"
"os" "os"
@ -52,11 +53,11 @@ type Config struct {
// in memory. // in memory.
DataDir string DataDir string
// IpcPath is the requested location to place the IPC endpoint. If the path is // IPCPath is the requested location to place the IPC endpoint. If the path is
// a simple file name, it is placed inside the data directory (or on the root // a simple file name, it is placed inside the data directory (or on the root
// pipe path on Windows), whereas if it's a resolvable path name (absolute or // pipe path on Windows), whereas if it's a resolvable path name (absolute or
// relative), then that specific path is enforced. An empty path disables IPC. // relative), then that specific path is enforced. An empty path disables IPC.
IpcPath string IPCPath string
// This field should be a valid secp256k1 private key that will be used for both // This field should be a valid secp256k1 private key that will be used for both
// remote peer identification as well as network traffic encryption. If no key // remote peer identification as well as network traffic encryption. If no key
@ -97,37 +98,105 @@ type Config struct {
// handshake phase, counted separately for inbound and outbound connections. // handshake phase, counted separately for inbound and outbound connections.
// Zero defaults to preset values. // Zero defaults to preset values.
MaxPendingPeers int MaxPendingPeers int
// HTTPHost is the host interface on which to start the HTTP RPC server. If this
// field is empty, no HTTP API endpoint will be started.
HTTPHost string
// HTTPPort is the TCP port number on which to start the HTTP RPC server. The
// default zero value is/ valid and will pick a port number randomly (useful
// for ephemeral nodes).
HTTPPort int
// HTTPCors is the Cross-Origin Resource Sharing header to send to requesting
// clients. Please be aware that CORS is a browser enforced security, it's fully
// useless for custom HTTP clients.
HTTPCors string
// HTTPModules is a list of API modules to expose via the HTTP RPC interface.
// If the module list is empty, all RPC API endpoints designated public will be
// exposed.
HTTPModules []string
// WSHost is the host interface on which to start the websocket RPC server. If
// this field is empty, no websocket API endpoint will be started.
WSHost string
// WSPort is the TCP port number on which to start the websocket RPC server. The
// default zero value is/ valid and will pick a port number randomly (useful for
// ephemeral nodes).
WSPort int
// WSDomains is the list of domain to accept websocket requests from. Please be
// aware that the server can only act upon the HTTP request the client sends and
// cannot verify the validity of the request header.
WSDomains string
// WSModules is a list of API modules to expose via the websocket RPC interface.
// If the module list is empty, all RPC API endpoints designated public will be
// exposed.
WSModules []string
} }
// IpcEndpoint resolves an IPC endpoint based on a configured value, taking into // IPCEndpoint resolves an IPC endpoint based on a configured value, taking into
// account the set data folders as well as the designated platform we're currently // account the set data folders as well as the designated platform we're currently
// running on. // running on.
func (c *Config) IpcEndpoint() string { func (c *Config) IPCEndpoint() string {
// Short circuit if IPC has not been enabled // Short circuit if IPC has not been enabled
if c.IpcPath == "" { if c.IPCPath == "" {
return "" return ""
} }
// On windows we can only use plain top-level pipes // On windows we can only use plain top-level pipes
if runtime.GOOS == "windows" { if runtime.GOOS == "windows" {
if strings.HasPrefix(c.IpcPath, `\\.\pipe\`) { if strings.HasPrefix(c.IPCPath, `\\.\pipe\`) {
return c.IpcPath return c.IPCPath
} }
return `\\.\pipe\` + c.IpcPath return `\\.\pipe\` + c.IPCPath
} }
// Resolve names into the data directory full paths otherwise // Resolve names into the data directory full paths otherwise
if filepath.Base(c.IpcPath) == c.IpcPath { if filepath.Base(c.IPCPath) == c.IPCPath {
if c.DataDir == "" { if c.DataDir == "" {
return filepath.Join(os.TempDir(), c.IpcPath) return filepath.Join(os.TempDir(), c.IPCPath)
} }
return filepath.Join(c.DataDir, c.IpcPath) return filepath.Join(c.DataDir, c.IPCPath)
} }
return c.IpcPath return c.IPCPath
} }
// DefaultIpcEndpoint returns the IPC path used by default. // DefaultIPCEndpoint returns the IPC path used by default.
func DefaultIpcEndpoint() string { func DefaultIPCEndpoint() string {
config := &Config{DataDir: common.DefaultDataDir(), IpcPath: common.DefaultIpcSocket()} config := &Config{DataDir: common.DefaultDataDir(), IPCPath: common.DefaultIPCSocket}
return config.IpcEndpoint() return config.IPCEndpoint()
}
// HTTPEndpoint resolves an HTTP endpoint based on the configured host interface
// and port parameters.
func (c *Config) HTTPEndpoint() string {
if c.HTTPHost == "" {
return ""
}
return fmt.Sprintf("%s:%d", c.HTTPHost, c.HTTPPort)
}
// DefaultHTTPEndpoint returns the HTTP endpoint used by default.
func DefaultHTTPEndpoint() string {
config := &Config{HTTPHost: common.DefaultHTTPHost, HTTPPort: common.DefaultHTTPPort}
return config.HTTPEndpoint()
}
// WSEndpoint resolves an websocket endpoint based on the configured host interface
// and port parameters.
func (c *Config) WSEndpoint() string {
if c.WSHost == "" {
return ""
}
return fmt.Sprintf("%s:%d", c.WSHost, c.WSPort)
}
// DefaultWSEndpoint returns the websocket endpoint used by default.
func DefaultWSEndpoint() string {
config := &Config{WSHost: common.DefaultWSHost, WSPort: common.DefaultWSPort}
return config.WSEndpoint()
} }
// NodeKey retrieves the currently configured private key of the node, checking // NodeKey retrieves the currently configured private key of the node, checking

View File

@ -63,10 +63,10 @@ func TestDatadirCreation(t *testing.T) {
// Tests that IPC paths are correctly resolved to valid endpoints of different // Tests that IPC paths are correctly resolved to valid endpoints of different
// platforms. // platforms.
func TestIpcPathResolution(t *testing.T) { func TestIPCPathResolution(t *testing.T) {
var tests = []struct { var tests = []struct {
DataDir string DataDir string
IpcPath string IPCPath string
Windows bool Windows bool
Endpoint string Endpoint string
}{ }{
@ -85,7 +85,7 @@ func TestIpcPathResolution(t *testing.T) {
for i, test := range tests { for i, test := range tests {
// Only run when platform/test match // Only run when platform/test match
if (runtime.GOOS == "windows") == test.Windows { if (runtime.GOOS == "windows") == test.Windows {
if endpoint := (&Config{DataDir: test.DataDir, IpcPath: test.IpcPath}).IpcEndpoint(); endpoint != test.Endpoint { if endpoint := (&Config{DataDir: test.DataDir, IPCPath: test.IPCPath}).IPCEndpoint(); endpoint != test.Endpoint {
t.Errorf("test %d: IPC endpoint mismatch: have %s, want %s", i, endpoint, test.Endpoint) t.Errorf("test %d: IPC endpoint mismatch: have %s, want %s", i, endpoint, test.Endpoint)
} }
} }

View File

@ -55,10 +55,25 @@ type Node struct {
serviceFuncs []ServiceConstructor // Service constructors (in dependency order) serviceFuncs []ServiceConstructor // Service constructors (in dependency order)
services map[reflect.Type]Service // Currently running services services map[reflect.Type]Service // Currently running services
rpcAPIs []rpc.API // List of APIs currently provided by the node
inprocHandler *rpc.Server // In-process RPC request handler to process the API requests
ipcEndpoint string // IPC endpoint to listen at (empty = IPC disabled) ipcEndpoint string // IPC endpoint to listen at (empty = IPC disabled)
ipcListener net.Listener // IPC RPC listener socket to serve API requests ipcListener net.Listener // IPC RPC listener socket to serve API requests
ipcHandler *rpc.Server // IPC RPC request handler to process the API requests ipcHandler *rpc.Server // IPC RPC request handler to process the API requests
httpEndpoint string // HTTP endpoint (interface + port) to listen at (empty = HTTP disabled)
httpWhitelist []string // HTTP RPC modules to allow through this endpoint
httpCors string // HTTP RPC Cross-Origin Resource Sharing header
httpListener net.Listener // HTTP RPC listener socket to server API requests
httpHandler *rpc.Server // HTTP RPC request handler to process the API requests
wsEndpoint string // Websocket endpoint (interface + port) to listen at (empty = websocket disabled)
wsWhitelist []string // Websocket RPC modules to allow through this endpoint
wsDomains string // Websocket RPC allowed origin domains
wsListener net.Listener // Websocket RPC listener socket to server API requests
wsHandler *rpc.Server // Websocket RPC request handler to process the API requests
stop chan struct{} // Channel to wait for termination notifications stop chan struct{} // Channel to wait for termination notifications
lock sync.RWMutex lock sync.RWMutex
} }
@ -93,9 +108,15 @@ func New(conf *Config) (*Node, error) {
MaxPeers: conf.MaxPeers, MaxPeers: conf.MaxPeers,
MaxPendingPeers: conf.MaxPendingPeers, MaxPendingPeers: conf.MaxPendingPeers,
}, },
serviceFuncs: []ServiceConstructor{}, serviceFuncs: []ServiceConstructor{},
ipcEndpoint: conf.IpcEndpoint(), ipcEndpoint: conf.IPCEndpoint(),
eventmux: new(event.TypeMux), httpEndpoint: conf.HTTPEndpoint(),
httpWhitelist: conf.HTTPModules,
httpCors: conf.HTTPCors,
wsEndpoint: conf.WSEndpoint(),
wsWhitelist: conf.WSModules,
wsDomains: conf.WSDomains,
eventmux: new(event.TypeMux),
}, nil }, nil
} }
@ -188,58 +209,234 @@ func (n *Node) Start() error {
return nil return nil
} }
// startRPC initializes and starts the IPC RPC endpoints. // startRPC is a helper method to start all the various RPC endpoint during node
// startup. It's not meant to be called at any time afterwards as it makes certain
// assumptions about the state of the node.
func (n *Node) startRPC(services map[reflect.Type]Service) error { func (n *Node) startRPC(services map[reflect.Type]Service) error {
// Gather and register all the APIs exposed by the services // Gather all the possible APIs to surface
apis := n.apis() apis := n.apis()
for _, service := range services { for _, service := range services {
apis = append(apis, service.APIs()...) apis = append(apis, service.APIs()...)
} }
ipcHandler := rpc.NewServer() // Start the various API endpoints, terminating all in case of errors
for _, api := range apis { if err := n.startInProc(apis); err != nil {
if err := ipcHandler.RegisterName(api.Namespace, api.Service); err != nil { return err
return err
}
glog.V(logger.Debug).Infof("Register %T under namespace '%s'", api.Service, api.Namespace)
} }
// All APIs registered, start the IPC and HTTP listeners if err := n.startIPC(apis); err != nil {
var ( n.stopInProc()
ipcListener net.Listener return err
err error }
) if err := n.startHTTP(n.httpEndpoint, apis, n.httpWhitelist, n.httpCors); err != nil {
if n.ipcEndpoint != "" { n.stopIPC()
if ipcListener, err = rpc.CreateIPCListener(n.ipcEndpoint); err != nil { n.stopInProc()
return err return err
} }
go func() { if err := n.startWS(n.wsEndpoint, apis, n.wsWhitelist, n.wsDomains); err != nil {
glog.V(logger.Info).Infof("IPC endpoint opened: %s", n.ipcEndpoint) n.stopHTTP()
defer glog.V(logger.Info).Infof("IPC endpoint closed: %s", n.ipcEndpoint) n.stopIPC()
n.stopInProc()
return err
}
// All API endpoints started successfully
n.rpcAPIs = apis
return nil
}
for { // startInProc initializes an in-process RPC endpoint.
conn, err := ipcListener.Accept() func (n *Node) startInProc(apis []rpc.API) error {
if err != nil { // Register all the APIs exposed by the services
// Terminate if the listener was closed handler := rpc.NewServer()
n.lock.RLock() for _, api := range apis {
closed := n.ipcListener == nil if err := handler.RegisterName(api.Namespace, api.Service); err != nil {
n.lock.RUnlock() return err
if closed { }
return glog.V(logger.Debug).Infof("InProc registered %T under '%s'", api.Service, api.Namespace)
}
// Not closed, just some error; report and continue
glog.V(logger.Error).Infof("IPC accept failed: %v", err)
continue
}
go ipcHandler.ServeCodec(rpc.NewJSONCodec(conn))
}
}()
} }
n.inprocHandler = handler
return nil
}
// stopInProc terminates the in-process RPC endpoint.
func (n *Node) stopInProc() {
if n.inprocHandler != nil {
n.inprocHandler.Stop()
n.inprocHandler = nil
}
}
// startIPC initializes and starts the IPC RPC endpoint.
func (n *Node) startIPC(apis []rpc.API) error {
// Short circuit if the IPC endpoint isn't being exposed
if n.ipcEndpoint == "" {
return nil
}
// Register all the APIs exposed by the services
handler := rpc.NewServer()
for _, api := range apis {
if err := handler.RegisterName(api.Namespace, api.Service); err != nil {
return err
}
glog.V(logger.Debug).Infof("IPC registered %T under '%s'", api.Service, api.Namespace)
}
// All APIs registered, start the IPC listener
var (
listener net.Listener
err error
)
if listener, err = rpc.CreateIPCListener(n.ipcEndpoint); err != nil {
return err
}
go func() {
glog.V(logger.Info).Infof("IPC endpoint opened: %s", n.ipcEndpoint)
for {
conn, err := listener.Accept()
if err != nil {
// Terminate if the listener was closed
n.lock.RLock()
closed := n.ipcListener == nil
n.lock.RUnlock()
if closed {
return
}
// Not closed, just some error; report and continue
glog.V(logger.Error).Infof("IPC accept failed: %v", err)
continue
}
go handler.ServeCodec(rpc.NewJSONCodec(conn))
}
}()
// All listeners booted successfully // All listeners booted successfully
n.ipcListener = ipcListener n.ipcListener = listener
n.ipcHandler = ipcHandler n.ipcHandler = handler
return nil return nil
} }
// stopIPC terminates the IPC RPC endpoint.
func (n *Node) stopIPC() {
if n.ipcListener != nil {
n.ipcListener.Close()
n.ipcListener = nil
glog.V(logger.Info).Infof("IPC endpoint closed: %s", n.ipcEndpoint)
}
if n.ipcHandler != nil {
n.ipcHandler.Stop()
n.ipcHandler = nil
}
}
// startHTTP initializes and starts the HTTP RPC endpoint.
func (n *Node) startHTTP(endpoint string, apis []rpc.API, modules []string, cors string) error {
// Short circuit if the HTTP endpoint isn't being exposed
if endpoint == "" {
return nil
}
// Generate the whitelist based on the allowed modules
whitelist := make(map[string]bool)
for _, module := range modules {
whitelist[module] = true
}
// Register all the APIs exposed by the services
handler := rpc.NewServer()
for _, api := range apis {
if whitelist[api.Namespace] || (len(whitelist) == 0 && api.Public) {
if err := handler.RegisterName(api.Namespace, api.Service); err != nil {
return err
}
glog.V(logger.Debug).Infof("HTTP registered %T under '%s'", api.Service, api.Namespace)
}
}
// All APIs registered, start the HTTP listener
var (
listener net.Listener
err error
)
if listener, err = net.Listen("tcp", endpoint); err != nil {
return err
}
go rpc.NewHTTPServer(cors, handler).Serve(listener)
glog.V(logger.Info).Infof("HTTP endpoint opened: http://%s", endpoint)
// All listeners booted successfully
n.httpEndpoint = endpoint
n.httpListener = listener
n.httpHandler = handler
n.httpCors = cors
return nil
}
// stopHTTP terminates the HTTP RPC endpoint.
func (n *Node) stopHTTP() {
if n.httpListener != nil {
n.httpListener.Close()
n.httpListener = nil
glog.V(logger.Info).Infof("HTTP endpoint closed: http://%s", n.httpEndpoint)
}
if n.httpHandler != nil {
n.httpHandler.Stop()
n.httpHandler = nil
}
}
// startWS initializes and starts the websocket RPC endpoint.
func (n *Node) startWS(endpoint string, apis []rpc.API, modules []string, cors string) error {
// Short circuit if the WS endpoint isn't being exposed
if endpoint == "" {
return nil
}
// Generate the whitelist based on the allowed modules
whitelist := make(map[string]bool)
for _, module := range modules {
whitelist[module] = true
}
// Register all the APIs exposed by the services
handler := rpc.NewServer()
for _, api := range apis {
if whitelist[api.Namespace] || (len(whitelist) == 0 && api.Public) {
if err := handler.RegisterName(api.Namespace, api.Service); err != nil {
return err
}
glog.V(logger.Debug).Infof("WebSocket registered %T under '%s'", api.Service, api.Namespace)
}
}
// All APIs registered, start the HTTP listener
var (
listener net.Listener
err error
)
if listener, err = net.Listen("tcp", endpoint); err != nil {
return err
}
go rpc.NewWSServer(cors, handler).Serve(listener)
glog.V(logger.Info).Infof("WebSocket endpoint opened: ws://%s", endpoint)
// All listeners booted successfully
n.wsEndpoint = endpoint
n.wsListener = listener
n.wsHandler = handler
n.wsDomains = cors
return nil
}
// stopWS terminates the websocket RPC endpoint.
func (n *Node) stopWS() {
if n.wsListener != nil {
n.wsListener.Close()
n.wsListener = nil
glog.V(logger.Info).Infof("WebSocket endpoint closed: ws://%s", n.wsEndpoint)
}
if n.wsHandler != nil {
n.wsHandler.Stop()
n.wsHandler = nil
}
}
// Stop terminates a running node along with all it's services. In the node was // Stop terminates a running node along with all it's services. In the node was
// not started, an error is returned. // not started, an error is returned.
func (n *Node) Stop() error { func (n *Node) Stop() error {
@ -251,14 +448,11 @@ func (n *Node) Stop() error {
return ErrNodeStopped return ErrNodeStopped
} }
// Otherwise terminate the API, all services and the P2P server too // Otherwise terminate the API, all services and the P2P server too
if n.ipcListener != nil { n.stopWS()
n.ipcListener.Close() n.stopHTTP()
n.ipcListener = nil n.stopIPC()
} n.rpcAPIs = nil
if n.ipcHandler != nil {
n.ipcHandler.Stop()
n.ipcHandler = nil
}
failure := &StopError{ failure := &StopError{
Services: make(map[reflect.Type]error), Services: make(map[reflect.Type]error),
} }
@ -304,6 +498,19 @@ func (n *Node) Restart() error {
return nil return nil
} }
// Attach creates an RPC client attached to an in-process API handler.
func (n *Node) Attach() (rpc.Client, error) {
n.lock.RLock()
defer n.lock.RUnlock()
// Short circuit if the node's not running
if n.server == nil {
return nil, ErrNodeStopped
}
// Otherwise attach to the API and return
return rpc.NewInProcRPCClient(n.inprocHandler), nil
}
// Server retrieves the currently running P2P network layer. This method is meant // Server retrieves the currently running P2P network layer. This method is meant
// only to inspect fields of the currently running server, life cycle management // only to inspect fields of the currently running server, life cycle management
// should be left to this Node entity. // should be left to this Node entity.
@ -337,11 +544,21 @@ func (n *Node) DataDir() string {
return n.datadir return n.datadir
} }
// IpcEndpoint retrieves the current IPC endpoint used by the protocol stack. // IPCEndpoint retrieves the current IPC endpoint used by the protocol stack.
func (n *Node) IpcEndpoint() string { func (n *Node) IPCEndpoint() string {
return n.ipcEndpoint return n.ipcEndpoint
} }
// HTTPEndpoint retrieves the current HTTP endpoint used by the protocol stack.
func (n *Node) HTTPEndpoint() string {
return n.httpEndpoint
}
// WSEndpoint retrieves the current WS endpoint used by the protocol stack.
func (n *Node) WSEndpoint() string {
return n.wsEndpoint
}
// EventMux retrieves the event multiplexer used by all the network services in // EventMux retrieves the event multiplexer used by all the network services in
// the current protocol stack. // the current protocol stack.
func (n *Node) EventMux() *event.TypeMux { func (n *Node) EventMux() *event.TypeMux {
@ -377,14 +594,3 @@ func (n *Node) apis() []rpc.API {
}, },
} }
} }
// APIs returns the collection of RPC descriptor this node offers. This method
// is just a quick placeholder passthrough for the RPC update, which in the next
// step will be fully integrated into the node itself.
func (n *Node) APIs() []rpc.API {
apis := n.apis()
for _, api := range n.services {
apis = append(apis, api.APIs()...)
}
return apis
}

View File

@ -18,9 +18,7 @@ package node
import ( import (
"errors" "errors"
"fmt"
"io/ioutil" "io/ioutil"
"math/rand"
"os" "os"
"reflect" "reflect"
"testing" "testing"
@ -37,7 +35,6 @@ var (
func testNodeConfig() *Config { func testNodeConfig() *Config {
return &Config{ return &Config{
IpcPath: fmt.Sprintf("test-%d.ipc", rand.Int63()),
PrivateKey: testNodeKey, PrivateKey: testNodeKey,
Name: "test node", Name: "test node",
} }
@ -541,10 +538,11 @@ func TestAPIGather(t *testing.T) {
defer stack.Stop() defer stack.Stop()
// Connect to the RPC server and verify the various registered endpoints // Connect to the RPC server and verify the various registered endpoints
ipcClient, err := rpc.NewIPCClient(stack.IpcEndpoint()) client, err := stack.Attach()
if err != nil { if err != nil {
t.Fatalf("failed to connect to the IPC API server: %v", err) t.Fatalf("failed to connect to the inproc API server: %v", err)
} }
defer client.Close()
tests := []struct { tests := []struct {
Method string Method string
@ -556,11 +554,11 @@ func TestAPIGather(t *testing.T) {
{"multi.v2.nested_theOneMethod", "multi.v2.nested"}, {"multi.v2.nested_theOneMethod", "multi.v2.nested"},
} }
for i, test := range tests { for i, test := range tests {
if err := ipcClient.Send(rpc.JSONRequest{Id: new(int64), Version: "2.0", Method: test.Method}); err != nil { if err := client.Send(rpc.JSONRequest{Id: new(int64), Version: "2.0", Method: test.Method}); err != nil {
t.Fatalf("test %d: failed to send API request: %v", i, err) t.Fatalf("test %d: failed to send API request: %v", i, err)
} }
reply := new(rpc.JSONSuccessResponse) reply := new(rpc.JSONSuccessResponse)
if err := ipcClient.Recv(reply); err != nil { if err := client.Recv(reply); err != nil {
t.Fatalf("test %d: failed to read API reply: %v", i, err) t.Fatalf("test %d: failed to read API reply: %v", i, err)
} }
select { select {

View File

@ -20,7 +20,6 @@ import (
"bufio" "bufio"
"bytes" "bytes"
"encoding/json" "encoding/json"
"errors"
"fmt" "fmt"
"io" "io"
"io/ioutil" "io/ioutil"
@ -29,7 +28,6 @@ import (
"net/url" "net/url"
"strconv" "strconv"
"strings" "strings"
"sync"
"time" "time"
"github.com/ethereum/go-ethereum/logger" "github.com/ethereum/go-ethereum/logger"
@ -41,12 +39,6 @@ const (
httpReadDeadLine = 60 * time.Second // wait max httpReadDeadeline for next request httpReadDeadLine = 60 * time.Second // wait max httpReadDeadeline for next request
) )
var (
httpServerMu sync.Mutex // prevent concurrent access to the httpListener and httpServer
httpListener net.Listener // listener for the http server
httpRPCServer *Server // the node can only start 1 HTTP RPC server instance
)
// httpMessageStream is the glue between a HTTP connection which is message based // httpMessageStream is the glue between a HTTP connection which is message based
// and the RPC codecs that expect json requests to be read from a stream. It will // and the RPC codecs that expect json requests to be read from a stream. It will
// parse HTTP messages and offer the bodies of these requests as a stream through // parse HTTP messages and offer the bodies of these requests as a stream through
@ -249,53 +241,14 @@ func (h *httpConnHijacker) ServeHTTP(w http.ResponseWriter, req *http.Request) {
go h.rpcServer.ServeCodec(codec) go h.rpcServer.ServeCodec(codec)
} }
// StartHTTP will start the JSONRPC HTTP RPC interface when its not yet running. // NewHTTPServer creates a new HTTP RPC server around an API provider.
func StartHTTP(address string, port int, corsdomains []string, apis []API) error { func NewHTTPServer(cors string, handler *Server) *http.Server {
httpServerMu.Lock() return &http.Server{
defer httpServerMu.Unlock() Handler: &httpConnHijacker{
corsdomains: strings.Split(cors, ","),
if httpRPCServer != nil { rpcServer: handler,
return fmt.Errorf("HTTP RPC interface already started on %s", httpListener.Addr()) },
} }
rpcServer := NewServer()
for _, api := range apis {
if err := rpcServer.RegisterName(api.Namespace, api.Service); err != nil {
return err
}
}
listener, err := net.Listen("tcp", fmt.Sprintf("%s:%d", address, port))
if err != nil {
return err
}
httpServer := http.Server{Handler: &httpConnHijacker{corsdomains, rpcServer}}
go httpServer.Serve(listener)
httpListener = listener
httpRPCServer = rpcServer
return nil
}
// StopHTTP will stop the running HTTP interface. If it is not running an error will be returned.
func StopHTTP() error {
httpServerMu.Lock()
defer httpServerMu.Unlock()
if httpRPCServer == nil {
return errors.New("HTTP RPC interface not started")
}
httpListener.Close()
httpRPCServer.Stop()
httpRPCServer = nil
httpListener = nil
return nil
} }
// httpClient connects to a geth RPC server over HTTP. // httpClient connects to a geth RPC server over HTTP.
@ -306,7 +259,7 @@ type httpClient struct {
// NewHTTPClient create a new RPC clients that connection to a geth RPC server // NewHTTPClient create a new RPC clients that connection to a geth RPC server
// over HTTP. // over HTTP.
func NewHTTPClient(endpoint string) (*httpClient, error) { func NewHTTPClient(endpoint string) (Client, error) {
url, err := url.Parse(endpoint) url, err := url.Parse(endpoint)
if err != nil { if err != nil {
return nil, err return nil, err

111
rpc/inproc.go Normal file
View File

@ -0,0 +1,111 @@
// Copyright 2016 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
package rpc
import "encoding/json"
// NewInProcRPCClient creates an in-process buffer stream attachment to a given
// RPC server.
func NewInProcRPCClient(handler *Server) Client {
buffer := &inprocBuffer{
requests: make(chan []byte, 16),
responses: make(chan []byte, 16),
}
client := &inProcClient{
server: handler,
buffer: buffer,
}
go handler.ServeCodec(NewJSONCodec(client.buffer))
return client
}
// inProcClient is an in-process buffer stream attached to an RPC server.
type inProcClient struct {
server *Server
buffer *inprocBuffer
}
// Close tears down the request channel of the in-proc client.
func (c *inProcClient) Close() {
c.buffer.Close()
}
// Send marshals a message into a json format and injects in into the client
// request channel.
func (c *inProcClient) Send(msg interface{}) error {
d, err := json.Marshal(msg)
if err != nil {
return err
}
c.buffer.requests <- d
return nil
}
// Recv reads a message from the response channel and tries to parse it into the
// given msg interface.
func (c *inProcClient) Recv(msg interface{}) error {
data := <-c.buffer.responses
return json.Unmarshal(data, &msg)
}
// Returns the collection of modules the RPC server offers.
func (c *inProcClient) SupportedModules() (map[string]string, error) {
return SupportedModules(c)
}
// inprocBuffer represents the connection between the RPC server and console
type inprocBuffer struct {
readBuf []byte // store remaining request bytes after a partial read
requests chan []byte // list with raw serialized requests
responses chan []byte // list with raw serialized responses
}
// Read will read the next request in json format.
func (b *inprocBuffer) Read(p []byte) (int, error) {
// last read didn't read entire request, return remaining bytes
if len(b.readBuf) > 0 {
n := copy(p, b.readBuf)
if n < len(b.readBuf) {
b.readBuf = b.readBuf[:n]
} else {
b.readBuf = b.readBuf[:0]
}
return n, nil
}
// read next request
req := <-b.requests
n := copy(p, req)
if n < len(req) {
// inprocBuffer too small, store remaining chunk for next read
b.readBuf = req[n:]
}
return n, nil
}
// Write sends the given buffer to the backend.
func (b *inprocBuffer) Write(p []byte) (n int, err error) {
b.responses <- p
return len(p), nil
}
// Close cleans up obtained resources.
func (b *inprocBuffer) Close() error {
close(b.requests)
close(b.responses)
return nil
}

View File

@ -38,7 +38,7 @@ type ipcClient struct {
// NewIPCClient create a new IPC client that will connect on the given endpoint. Messages are JSON encoded and encoded. // NewIPCClient create a new IPC client that will connect on the given endpoint. Messages are JSON encoded and encoded.
// On Unix it assumes the endpoint is the full path to a unix socket, and Windows the endpoint is an identifier for a // On Unix it assumes the endpoint is the full path to a unix socket, and Windows the endpoint is an identifier for a
// named pipe. // named pipe.
func NewIPCClient(endpoint string) (*ipcClient, error) { func NewIPCClient(endpoint string) (Client, error) {
conn, err := newIPCConnection(endpoint) conn, err := newIPCConnection(endpoint)
if err != nil { if err != nil {
return nil, err return nil, err

View File

@ -33,8 +33,8 @@ import (
const ( const (
stopPendingRequestTimeout = 3 * time.Second // give pending requests stopPendingRequestTimeout the time to finish when the server is stopped stopPendingRequestTimeout = 3 * time.Second // give pending requests stopPendingRequestTimeout the time to finish when the server is stopped
DefaultIpcApis = "admin,eth,debug,miner,net,shh,txpool,personal,web3" DefaultIPCApis = "admin,eth,debug,miner,net,shh,txpool,personal,web3"
DefaultHttpRpcApis = "eth,net,web3" DefaultHTTPApis = "eth,net,web3"
) )
// NewServer will create a new server instance with no registered handlers. // NewServer will create a new server instance with no registered handlers.

View File

@ -20,13 +20,12 @@ import (
"crypto/rand" "crypto/rand"
"encoding/hex" "encoding/hex"
"errors" "errors"
"fmt"
"math/big" "math/big"
"reflect" "reflect"
"unicode" "unicode"
"unicode/utf8" "unicode/utf8"
"fmt"
"golang.org/x/net/context" "golang.org/x/net/context"
) )

View File

@ -17,13 +17,11 @@
package rpc package rpc
import ( import (
"errors"
"fmt" "fmt"
"net"
"net/http" "net/http"
"sync"
"os" "os"
"strings"
"sync"
"github.com/ethereum/go-ethereum/logger" "github.com/ethereum/go-ethereum/logger"
"github.com/ethereum/go-ethereum/logger/glog" "github.com/ethereum/go-ethereum/logger/glog"
@ -31,12 +29,6 @@ import (
"gopkg.in/fatih/set.v0" "gopkg.in/fatih/set.v0"
) )
var (
wsServerMu sync.Mutex
wsRPCServer *Server
wsListener net.Listener
)
// wsReaderWriterCloser reads and write payloads from and to a websocket connection. // wsReaderWriterCloser reads and write payloads from and to a websocket connection.
type wsReaderWriterCloser struct { type wsReaderWriterCloser struct {
c *websocket.Conn c *websocket.Conn
@ -57,14 +49,6 @@ func (rw *wsReaderWriterCloser) Close() error {
return rw.c.Close() return rw.c.Close()
} }
// wsHandler accepts a websocket connection and handles incoming RPC requests.
// Will return when the websocket connection is closed, either by the client or
// server.
func wsHandler(conn *websocket.Conn) {
rwc := &wsReaderWriterCloser{conn}
wsRPCServer.ServeCodec(NewJSONCodec(rwc))
}
// wsHandshakeValidator returns a handler that verifies the origin during the // wsHandshakeValidator returns a handler that verifies the origin during the
// websocket upgrade process. When a '*' is specified as an allowed origins all // websocket upgrade process. When a '*' is specified as an allowed origins all
// connections are accepted. // connections are accepted.
@ -103,54 +87,16 @@ func wsHandshakeValidator(allowedOrigins []string) func(*websocket.Config, *http
return f return f
} }
// StartWS will start a websocket RPC server on the given address and port. // NewWSServer creates a new websocket RPC server around an API provider.
func StartWS(address string, port int, corsdomains []string, apis []API) error { func NewWSServer(cors string, handler *Server) *http.Server {
wsServerMu.Lock() return &http.Server{
defer wsServerMu.Unlock() Handler: websocket.Server{
Handshake: wsHandshakeValidator(strings.Split(cors, ",")),
if wsRPCServer != nil { Handler: func(conn *websocket.Conn) {
return fmt.Errorf("WS RPC interface already started on %s", wsListener.Addr()) handler.ServeCodec(NewJSONCodec(&wsReaderWriterCloser{conn}))
},
},
} }
rpcServer := NewServer()
for _, api := range apis {
if err := rpcServer.RegisterName(api.Namespace, api.Service); err != nil {
return err
}
}
listener, err := net.Listen("tcp", fmt.Sprintf("%s:%d", address, port))
if err != nil {
return err
}
wsServer := websocket.Server{Handshake: wsHandshakeValidator(corsdomains), Handler: wsHandler}
wsHTTPServer := http.Server{Handler: wsServer}
go wsHTTPServer.Serve(listener)
wsListener = listener
wsRPCServer = rpcServer
return nil
}
// StopWS stops the running websocket RPC server.
func StopWS() error {
wsServerMu.Lock()
defer wsServerMu.Unlock()
if wsRPCServer == nil {
return errors.New("HTTP RPC interface not started")
}
wsListener.Close()
wsRPCServer.Stop()
wsRPCServer = nil
wsListener = nil
return nil
} }
// wsClient represents a RPC client that communicates over websockets with a // wsClient represents a RPC client that communicates over websockets with a
@ -163,7 +109,7 @@ type wsClient struct {
// NewWSClientj creates a new RPC client that communicates with a RPC server // NewWSClientj creates a new RPC client that communicates with a RPC server
// that is listening on the given endpoint using JSON encoding. // that is listening on the given endpoint using JSON encoding.
func NewWSClient(endpoint string) (*wsClient, error) { func NewWSClient(endpoint string) (Client, error) {
return &wsClient{endpoint: endpoint}, nil return &wsClient{endpoint: endpoint}, nil
} }