geth,cmd/status: WS-RPC server enabled

This commit is contained in:
Victor Farazdagi 2017-01-26 03:39:20 +03:00
parent fd36dcfdb4
commit cf7b8fb96c
4 changed files with 78 additions and 38 deletions

View File

@ -200,7 +200,13 @@ func DiscardTransactions(ids *C.char) *C.char {
//export StartNode
func StartNode(datadir *C.char) *C.char {
// This starts a geth node with the given datadir
err := geth.CreateAndRunNode(C.GoString(datadir), geth.RPCPort)
err := geth.CreateAndRunNode(&geth.NodeConfig{
DataDir: C.GoString(datadir),
HTTPPort: geth.HTTPPort,
WSEnabled: true,
WSPort: geth.WSPort,
TLSEnabled: false,
})
return makeJSONErrorResponse(err)
}
@ -223,6 +229,19 @@ func ResetChainData() *C.char {
return makeJSONErrorResponse(err)
}
//export StartTLSNode
func StartTLSNode(datadir *C.char) *C.char {
// This starts a geth node with the given datadir
err := geth.CreateAndRunNode(&geth.NodeConfig{
DataDir: C.GoString(datadir),
HTTPPort: geth.HTTPPort,
WSPort: geth.WSPort,
TLSEnabled: true,
})
return makeJSONErrorResponse(err)
}
//export StopNodeRPCServer
func StopNodeRPCServer() *C.char {
_, err := geth.NodeManagerInstance().StopNodeRPCServer()

View File

@ -34,7 +34,8 @@ const (
VersionPatch = 0 // Patch version component of the current release
VersionMeta = "unstable" // Version metadata to append to the version string
RPCPort = 8545 // RPC port (replaced in unit tests)
HTTPPort = 8545 // HTTP-RPC port (replaced in unit tests)
WSPort = 8546 // WS-RPC port (replaced in unit tests)
NetworkPort = 30303
MaxPeers = 25
MaxLightPeers = 20
@ -72,18 +73,26 @@ func init() {
// node-related errors
var (
ErrRLimitRaiseFailure = errors.New("failed to register the whisper service")
ErrDatabaseAccessFailure = errors.New("could not open database")
ErrChainConfigurationFailure = errors.New("could not make chain configuration")
ErrEthServiceRegistrationFailure = errors.New("failed to register the Ethereum service")
ErrSshServiceRegistrationFailure = errors.New("failed to register the Whisper service")
ErrLightEthRegistrationFailure = errors.New("failed to register the LES service")
)
// NodeConfig stores configuration options for a node
type NodeConfig struct {
DataDir string // base data directory
HTTPPort int // HTTP-RPC Server port
WSPort int // WS-RPC Server port
WSEnabled bool // whether WS-RPC Server is enabled or not
TLSEnabled bool // whether TLS support should be enabled on node or not
}
// Node represents running node (serves as a wrapper around P2P node)
type Node struct {
geth *node.Node // reference to the running Geth node
started chan struct{} // channel to wait for node to start
config *node.Config
config *NodeConfig // configuration used to create Status node
geth *node.Node // reference to the running Geth node
gethConfig *node.Config // configuration used to create P2P node
started chan struct{} // channel to wait for node to start
}
// Inited checks whether status node has been properly initialized
@ -92,16 +101,20 @@ func (n *Node) Inited() bool {
}
// MakeNode create a geth node entity
func MakeNode(dataDir string, rpcPort int) *Node {
func MakeNode(config *NodeConfig) *Node {
glog.CopyStandardLogTo("INFO")
glog.SetToStderr(true)
dataDir := config.DataDir
if UseTestnet {
dataDir = filepath.Join(dataDir, "testnet")
dataDir = filepath.Join(config.DataDir, "testnet")
}
// exposed RPC APIs
exposedAPIs := "db,eth,net,web3,shh,personal,admin" // TODO remove "admin" on main net
// configure required node (should you need to update node's config, e.g. add bootstrap nodes, see node.Config)
config := &node.Config{
stackConfig := &node.Config{
DataDir: dataDir,
UseLightweightKDF: true,
Name: ClientIdentifier,
@ -115,12 +128,16 @@ func MakeNode(dataDir string, rpcPort int) *Node {
MaxPeers: MaxPeers,
MaxPendingPeers: MaxPendingPeers,
HTTPHost: node.DefaultHTTPHost,
HTTPPort: rpcPort,
HTTPPort: config.HTTPPort,
HTTPCors: "*",
HTTPModules: strings.Split("db,eth,net,web3,shh,personal,admin", ","), // TODO remove "admin" on main net
HTTPModules: strings.Split(exposedAPIs, ","),
WSHost: makeWSHost(config.WSEnabled),
WSPort: config.WSPort,
WSOrigins: "*",
WSModules: strings.Split(exposedAPIs, ","),
}
stack, err := node.New(config)
stack, err := node.New(stackConfig)
if err != nil {
Fatalf(ErrNodeMakeFailure)
}
@ -136,9 +153,10 @@ func MakeNode(dataDir string, rpcPort int) *Node {
}
return &Node{
geth: stack,
started: make(chan struct{}),
config: config,
geth: stack,
gethConfig: stackConfig,
started: make(chan struct{}),
config: config,
}
}
@ -193,6 +211,15 @@ func activateShhService(stack *node.Node) error {
return nil
}
// makeWSHost returns WS-RPC Server host, given enabled/disabled flag
func makeWSHost(wsEnabled bool) string {
if !wsEnabled {
return ""
}
return node.DefaultWSHost
}
// makeChainConfig reads the chain configuration from the database in the datadir.
func makeChainConfig(stack *node.Node) *params.ChainConfig {
config := new(params.ChainConfig)

View File

@ -30,16 +30,8 @@ type SelectedExtKey struct {
SubAccounts []accounts.Account
}
// NodeManagerConfig stores configuration options passed to constructor
type NodeManagerConfig struct {
DataDir string
RPCPort int
TLSEnabled bool
}
// NodeManager manages Status node (which abstracts contained geth node)
type NodeManager struct {
config *NodeManagerConfig // reference to passed configuration variables
node *Node // reference to Status node
services *NodeServiceStack // default stack of services running on geth node
api *node.PrivateAdminAPI // exposes collection of administrative API methods
@ -74,10 +66,10 @@ var (
)
// CreateAndRunNode creates and starts running Geth node locally (exposing given RPC port along the way)
func CreateAndRunNode(dataDir string, rpcPort int) error {
func CreateAndRunNode(config *NodeConfig) error {
defer HaltOnPanic()
nodeManager := NewNodeManager(dataDir, rpcPort)
nodeManager := NewNodeManager(config)
if nodeManager.NodeInited() {
nodeManager.RunNode()
@ -89,19 +81,14 @@ func CreateAndRunNode(dataDir string, rpcPort int) error {
}
// NewNodeManager makes new instance of node manager
func NewNodeManager(dataDir string, rpcPort int) *NodeManager {
func NewNodeManager(config *NodeConfig) *NodeManager {
createOnce.Do(func() {
nodeManagerInstance = &NodeManager{
config: &NodeManagerConfig{
DataDir: dataDir,
RPCPort: rpcPort,
TLSEnabled: false,
},
services: &NodeServiceStack{
jailedRequestQueue: NewJailedRequestsQueue(),
},
}
nodeManagerInstance.node = MakeNode(dataDir, rpcPort)
nodeManagerInstance.node = MakeNode(config)
})
return nodeManagerInstance
@ -235,7 +222,7 @@ func (m *NodeManager) ResetChainData() error {
return err
}
chainDataDir := filepath.Join(m.node.config.DataDir, m.node.config.Name, "lightchaindata")
chainDataDir := filepath.Join(m.node.gethConfig.DataDir, m.node.gethConfig.Name, "lightchaindata")
if _, err := os.Stat(chainDataDir); os.IsNotExist(err) {
return err
}
@ -260,7 +247,7 @@ func (m *NodeManager) StartNodeRPCServer() (bool, error) {
return false, ErrInvalidNodeAPI
}
config := m.node.config
config := m.node.gethConfig
modules := strings.Join(config.HTTPModules, ",")
return m.api.StartRPC(&config.HTTPHost, &config.HTTPPort, &config.HTTPCors, &modules)

View File

@ -27,6 +27,8 @@ var muPrepareTestNode sync.Mutex
const (
TestDataDir = "../.ethereumtest"
TestNodeSyncSeconds = 30
TestNodeHTTPPort = 8645
TestNodeWSPort = 8646
)
type NodeNotificationHandler func(jsonEvent string)
@ -127,8 +129,13 @@ func PrepareTestNode() (err error) {
}
// start geth node and wait for it to initialize
// internally once.Do() is used, so call below is thread-safe
err = CreateAndRunNode(dataDir, 8546) // to avoid conflicts with running react-native app, run on different port
err = CreateAndRunNode(&NodeConfig{
DataDir: dataDir,
HTTPPort: TestNodeHTTPPort, // to avoid conflicts with running app, using different port in tests
WSEnabled: true,
WSPort: TestNodeWSPort, // ditto
TLSEnabled: false,
})
if err != nil {
panic(err)
}