2016-12-07 21:07:08 +00:00
|
|
|
package geth
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"path/filepath"
|
2016-12-11 12:19:20 +00:00
|
|
|
"strings"
|
2016-12-07 21:07:08 +00:00
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/accounts"
|
2017-02-27 12:52:10 +00:00
|
|
|
"github.com/ethereum/go-ethereum/accounts/keystore"
|
2016-12-07 21:07:08 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/les"
|
|
|
|
"github.com/ethereum/go-ethereum/logger"
|
|
|
|
"github.com/ethereum/go-ethereum/logger/glog"
|
2016-12-11 12:19:20 +00:00
|
|
|
"github.com/ethereum/go-ethereum/node"
|
2016-12-07 21:07:08 +00:00
|
|
|
"github.com/ethereum/go-ethereum/p2p/discover"
|
|
|
|
"github.com/ethereum/go-ethereum/rpc"
|
|
|
|
whisper "github.com/ethereum/go-ethereum/whisper/whisperv2"
|
2017-03-14 13:17:32 +00:00
|
|
|
"github.com/status-im/status-go/geth/params"
|
2016-12-07 21:07:08 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// SelectedExtKey is a container for currently selected (logged in) account
|
|
|
|
type SelectedExtKey struct {
|
|
|
|
Address common.Address
|
2017-02-27 12:52:10 +00:00
|
|
|
AccountKey *keystore.Key
|
2016-12-07 21:07:08 +00:00
|
|
|
SubAccounts []accounts.Account
|
|
|
|
}
|
|
|
|
|
|
|
|
// NodeManager manages Status node (which abstracts contained geth node)
|
|
|
|
type NodeManager struct {
|
2016-12-11 12:19:20 +00:00
|
|
|
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
|
|
|
|
SelectedAccount *SelectedExtKey // account that was processed during the last call to SelectAccount()
|
2016-12-07 21:07:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NodeServiceStack contains "standard" node services (which are always available)
|
|
|
|
type NodeServiceStack struct {
|
|
|
|
lightEthereum *les.LightEthereum // LES service
|
|
|
|
whisperService *whisper.Whisper // Whisper service
|
|
|
|
rpcClient *rpc.Client // RPC client
|
|
|
|
jailedRequestQueue *JailedRequestQueue // bridge via which jail notifies node of incoming requests
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
ErrInvalidGethNode = errors.New("no running geth node detected")
|
|
|
|
ErrInvalidAccountManager = errors.New("could not retrieve account manager")
|
|
|
|
ErrInvalidWhisperService = errors.New("whisper service is unavailable")
|
|
|
|
ErrInvalidLightEthereumService = errors.New("can not retrieve LES service")
|
|
|
|
ErrInvalidClient = errors.New("RPC client is not properly initialized")
|
|
|
|
ErrInvalidJailedRequestQueue = errors.New("jailed request queue is not properly initialized")
|
|
|
|
ErrNodeMakeFailure = errors.New("error creating p2p node")
|
|
|
|
ErrNodeStartFailure = errors.New("error starting p2p node")
|
2016-12-11 12:19:20 +00:00
|
|
|
ErrInvalidNodeAPI = errors.New("no node API connected")
|
2017-02-27 12:52:10 +00:00
|
|
|
ErrAccountKeyStoreMissing = errors.New("account key store is not set")
|
2016-12-07 21:07:08 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
nodeManagerInstance *NodeManager
|
|
|
|
createOnce sync.Once
|
|
|
|
)
|
|
|
|
|
|
|
|
// CreateAndRunNode creates and starts running Geth node locally (exposing given RPC port along the way)
|
2017-03-15 21:03:01 +00:00
|
|
|
func CreateAndRunNode(config *params.NodeConfig) error {
|
2017-01-12 17:56:36 +00:00
|
|
|
defer HaltOnPanic()
|
|
|
|
|
2017-01-26 00:39:20 +00:00
|
|
|
nodeManager := NewNodeManager(config)
|
2016-12-07 21:07:08 +00:00
|
|
|
|
|
|
|
if nodeManager.NodeInited() {
|
|
|
|
nodeManager.RunNode()
|
2017-01-23 06:25:24 +00:00
|
|
|
nodeManager.WaitNodeStarted()
|
2016-12-07 21:07:08 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return ErrNodeStartFailure
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewNodeManager makes new instance of node manager
|
2017-03-15 21:03:01 +00:00
|
|
|
func NewNodeManager(config *params.NodeConfig) *NodeManager {
|
2016-12-07 21:07:08 +00:00
|
|
|
createOnce.Do(func() {
|
|
|
|
nodeManagerInstance = &NodeManager{
|
|
|
|
services: &NodeServiceStack{
|
|
|
|
jailedRequestQueue: NewJailedRequestsQueue(),
|
|
|
|
},
|
|
|
|
}
|
2017-01-26 00:39:20 +00:00
|
|
|
nodeManagerInstance.node = MakeNode(config)
|
2016-12-07 21:07:08 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
return nodeManagerInstance
|
|
|
|
}
|
|
|
|
|
|
|
|
// NodeManagerInstance exposes node manager instance
|
|
|
|
func NodeManagerInstance() *NodeManager {
|
|
|
|
return nodeManagerInstance
|
|
|
|
}
|
|
|
|
|
|
|
|
// RunNode starts Geth node
|
|
|
|
func (m *NodeManager) RunNode() {
|
|
|
|
go func() {
|
2017-01-12 17:56:36 +00:00
|
|
|
defer HaltOnPanic()
|
|
|
|
|
2016-12-07 21:07:08 +00:00
|
|
|
m.StartNode()
|
|
|
|
|
|
|
|
if _, err := m.AccountManager(); err != nil {
|
|
|
|
glog.V(logger.Warn).Infoln(ErrInvalidAccountManager)
|
|
|
|
}
|
|
|
|
if err := m.node.geth.Service(&m.services.whisperService); err != nil {
|
|
|
|
glog.V(logger.Warn).Infoln("cannot get whisper service:", err)
|
|
|
|
}
|
|
|
|
if err := m.node.geth.Service(&m.services.lightEthereum); err != nil {
|
|
|
|
glog.V(logger.Warn).Infoln("cannot get light ethereum service:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// setup handlers
|
|
|
|
lightEthereum, err := m.LightEthereumService()
|
|
|
|
if err != nil {
|
|
|
|
panic("service stack misses LES")
|
|
|
|
}
|
|
|
|
|
|
|
|
lightEthereum.StatusBackend.SetTransactionQueueHandler(onSendTransactionRequest)
|
|
|
|
lightEthereum.StatusBackend.SetAccountsFilterHandler(onAccountsListRequest)
|
|
|
|
lightEthereum.StatusBackend.SetTransactionReturnHandler(onSendTransactionReturn)
|
|
|
|
|
|
|
|
m.services.rpcClient, err = m.node.geth.Attach()
|
|
|
|
if err != nil {
|
|
|
|
glog.V(logger.Warn).Infoln("cannot get RPC client service:", ErrInvalidClient)
|
|
|
|
}
|
|
|
|
|
2016-12-11 12:19:20 +00:00
|
|
|
// expose API
|
|
|
|
m.api = node.NewPrivateAdminAPI(m.node.geth)
|
|
|
|
|
2017-03-10 00:16:32 +00:00
|
|
|
m.PopulateStaticPeers()
|
2016-12-07 21:07:08 +00:00
|
|
|
|
|
|
|
m.onNodeStarted() // node started, notify listeners
|
|
|
|
m.node.geth.Wait()
|
2017-01-23 06:25:24 +00:00
|
|
|
|
|
|
|
glog.V(logger.Info).Infoln("node stopped")
|
2016-12-07 21:07:08 +00:00
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
// StartNode starts running P2P node
|
|
|
|
func (m *NodeManager) StartNode() {
|
|
|
|
if m == nil || !m.NodeInited() {
|
|
|
|
panic(ErrInvalidGethNode)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := m.node.geth.Start(); err != nil {
|
|
|
|
panic(fmt.Sprintf("%v: %v", ErrNodeStartFailure, err))
|
|
|
|
}
|
|
|
|
|
|
|
|
// allow interrupting running nodes
|
|
|
|
go func() {
|
|
|
|
sigc := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(sigc, os.Interrupt)
|
|
|
|
defer signal.Stop(sigc)
|
|
|
|
<-sigc
|
|
|
|
glog.V(logger.Info).Infoln("Got interrupt, shutting down...")
|
|
|
|
go m.node.geth.Stop()
|
|
|
|
for i := 3; i > 0; i-- {
|
|
|
|
<-sigc
|
|
|
|
if i > 1 {
|
|
|
|
glog.V(logger.Info).Infof("Already shutting down, interrupt %d more times for panic.", i-1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
panic("interrupted!")
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2017-01-23 06:25:24 +00:00
|
|
|
// StopNode stops running P2P node
|
|
|
|
func (m *NodeManager) StopNode() error {
|
|
|
|
if m == nil || !m.NodeInited() {
|
|
|
|
return ErrInvalidGethNode
|
|
|
|
}
|
|
|
|
|
|
|
|
m.node.geth.Stop()
|
|
|
|
m.node.started = make(chan struct{})
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// RestartNode restarts P2P node
|
2016-12-11 12:19:20 +00:00
|
|
|
func (m *NodeManager) RestartNode() error {
|
|
|
|
if m == nil || !m.NodeInited() {
|
|
|
|
return ErrInvalidGethNode
|
|
|
|
}
|
|
|
|
|
2017-01-23 06:25:24 +00:00
|
|
|
m.StopNode()
|
|
|
|
m.RunNode()
|
|
|
|
m.WaitNodeStarted()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-01-24 18:42:55 +00:00
|
|
|
// ResumeNode resumes previously stopped P2P node
|
|
|
|
func (m *NodeManager) ResumeNode() error {
|
|
|
|
if m == nil || !m.NodeInited() {
|
|
|
|
return ErrInvalidGethNode
|
|
|
|
}
|
|
|
|
|
|
|
|
m.RunNode()
|
|
|
|
m.WaitNodeStarted()
|
|
|
|
|
|
|
|
// re-select the previously selected account
|
|
|
|
if err := ReSelectAccount(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-01-23 06:25:24 +00:00
|
|
|
// ResetChainData purges chain data (by removing data directory). Safe to apply on running P2P node.
|
|
|
|
func (m *NodeManager) ResetChainData() error {
|
|
|
|
if m == nil || !m.NodeInited() {
|
|
|
|
return ErrInvalidGethNode
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := m.StopNode(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-01-26 00:39:20 +00:00
|
|
|
chainDataDir := filepath.Join(m.node.gethConfig.DataDir, m.node.gethConfig.Name, "lightchaindata")
|
2017-01-23 06:25:24 +00:00
|
|
|
if _, err := os.Stat(chainDataDir); os.IsNotExist(err) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := os.RemoveAll(chainDataDir); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-01-24 18:42:55 +00:00
|
|
|
if err := m.ResumeNode(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-01-23 06:25:24 +00:00
|
|
|
|
|
|
|
return nil
|
2016-12-11 12:19:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *NodeManager) StartNodeRPCServer() (bool, error) {
|
|
|
|
if m == nil || !m.NodeInited() {
|
|
|
|
return false, ErrInvalidGethNode
|
|
|
|
}
|
|
|
|
|
|
|
|
if m.api == nil {
|
|
|
|
return false, ErrInvalidNodeAPI
|
|
|
|
}
|
|
|
|
|
2017-01-26 00:39:20 +00:00
|
|
|
config := m.node.gethConfig
|
2016-12-11 12:19:20 +00:00
|
|
|
modules := strings.Join(config.HTTPModules, ",")
|
|
|
|
|
2017-02-20 09:44:38 +00:00
|
|
|
return m.api.StartRPC(&config.HTTPHost, &config.HTTPPort, &config.HTTPCors, &modules)
|
2016-12-11 12:19:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// StopNodeRPCServer stops HTTP RPC service attached to node
|
|
|
|
func (m *NodeManager) StopNodeRPCServer() (bool, error) {
|
|
|
|
if m == nil || !m.NodeInited() {
|
|
|
|
return false, ErrInvalidGethNode
|
|
|
|
}
|
|
|
|
|
|
|
|
if m.api == nil {
|
|
|
|
return false, ErrInvalidNodeAPI
|
|
|
|
}
|
|
|
|
|
|
|
|
return m.api.StopRPC()
|
|
|
|
}
|
|
|
|
|
2016-12-07 21:07:08 +00:00
|
|
|
// HasNode checks whether manager has initialized node attached
|
|
|
|
func (m *NodeManager) NodeInited() bool {
|
|
|
|
if m == nil || !m.node.Inited() {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// AccountManager exposes reference to accounts manager
|
|
|
|
func (m *NodeManager) AccountManager() (*accounts.Manager, error) {
|
|
|
|
if m == nil || !m.NodeInited() {
|
|
|
|
return nil, ErrInvalidGethNode
|
|
|
|
}
|
|
|
|
|
|
|
|
return m.node.geth.AccountManager(), nil
|
|
|
|
}
|
|
|
|
|
2017-02-27 12:52:10 +00:00
|
|
|
// AccountKeyStore exposes reference to accounts key store
|
|
|
|
func (m *NodeManager) AccountKeyStore() (*keystore.KeyStore, error) {
|
|
|
|
if m == nil || !m.NodeInited() {
|
|
|
|
return nil, ErrInvalidGethNode
|
|
|
|
}
|
|
|
|
|
|
|
|
accountManager, err := m.AccountManager()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
backends := accountManager.Backends(keystore.KeyStoreType)
|
|
|
|
if len(backends) == 0 {
|
|
|
|
return nil, ErrAccountKeyStoreMissing
|
|
|
|
}
|
|
|
|
|
|
|
|
keyStore, ok := backends[0].(*keystore.KeyStore)
|
|
|
|
if !ok {
|
|
|
|
return nil, ErrAccountKeyStoreMissing
|
|
|
|
}
|
|
|
|
|
|
|
|
return keyStore, nil
|
|
|
|
}
|
|
|
|
|
2016-12-07 21:07:08 +00:00
|
|
|
// LightEthereumService exposes LES
|
|
|
|
func (m *NodeManager) LightEthereumService() (*les.LightEthereum, error) {
|
|
|
|
if m == nil || !m.NodeInited() {
|
|
|
|
return nil, ErrInvalidGethNode
|
|
|
|
}
|
|
|
|
|
|
|
|
if m.services.lightEthereum == nil {
|
|
|
|
return nil, ErrInvalidLightEthereumService
|
|
|
|
}
|
|
|
|
|
|
|
|
return m.services.lightEthereum, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// WhisperService exposes Whisper service
|
|
|
|
func (m *NodeManager) WhisperService() (*whisper.Whisper, error) {
|
|
|
|
if m == nil || !m.NodeInited() {
|
|
|
|
return nil, ErrInvalidGethNode
|
|
|
|
}
|
|
|
|
|
|
|
|
if m.services.whisperService == nil {
|
|
|
|
return nil, ErrInvalidWhisperService
|
|
|
|
}
|
|
|
|
|
|
|
|
return m.services.whisperService, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// RPCClient exposes Geth's RPC client
|
|
|
|
func (m *NodeManager) RPCClient() (*rpc.Client, error) {
|
|
|
|
if m == nil || !m.NodeInited() {
|
|
|
|
return nil, ErrInvalidGethNode
|
|
|
|
}
|
|
|
|
|
|
|
|
if m.services.rpcClient == nil {
|
|
|
|
return nil, ErrInvalidClient
|
|
|
|
}
|
|
|
|
|
|
|
|
return m.services.rpcClient, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// JailedRequestQueue exposes reference to queue of jailed requests
|
|
|
|
func (m *NodeManager) JailedRequestQueue() (*JailedRequestQueue, error) {
|
|
|
|
if m == nil || !m.NodeInited() {
|
|
|
|
return nil, ErrInvalidGethNode
|
|
|
|
}
|
|
|
|
|
|
|
|
if m.services.jailedRequestQueue == nil {
|
|
|
|
return nil, ErrInvalidJailedRequestQueue
|
|
|
|
}
|
|
|
|
|
|
|
|
return m.services.jailedRequestQueue, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddPeer adds new peer node
|
|
|
|
func (m *NodeManager) AddPeer(url string) (bool, error) {
|
|
|
|
if m == nil || !m.NodeInited() {
|
|
|
|
return false, ErrInvalidGethNode
|
|
|
|
}
|
|
|
|
|
|
|
|
server := m.node.geth.Server()
|
|
|
|
if server == nil {
|
|
|
|
return false, ErrInvalidGethNode
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to add the url as a static peer and return
|
|
|
|
parsedNode, err := discover.ParseNode(url)
|
|
|
|
if err != nil {
|
|
|
|
return false, fmt.Errorf("invalid enode: %v", err)
|
|
|
|
}
|
|
|
|
server.AddPeer(parsedNode)
|
|
|
|
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2017-01-23 06:25:24 +00:00
|
|
|
// WaitNodeStarted blocks until node is started (start channel gets notified)
|
|
|
|
func (m *NodeManager) WaitNodeStarted() {
|
|
|
|
<-m.node.started // block until node is started
|
|
|
|
}
|
|
|
|
|
2016-12-07 21:07:08 +00:00
|
|
|
// onNodeStarted sends upward notification letting the app know that Geth node is ready to be used
|
|
|
|
func (m *NodeManager) onNodeStarted() {
|
|
|
|
// notify local listener
|
|
|
|
m.node.started <- struct{}{}
|
|
|
|
close(m.node.started)
|
|
|
|
|
|
|
|
// send signal up to native app
|
2016-12-18 20:36:17 +00:00
|
|
|
SendSignal(SignalEnvelope{
|
2016-12-07 21:07:08 +00:00
|
|
|
Type: EventNodeStarted,
|
|
|
|
Event: struct{}{},
|
2016-12-18 20:36:17 +00:00
|
|
|
})
|
2016-12-07 21:07:08 +00:00
|
|
|
}
|
|
|
|
|
2017-03-10 00:16:32 +00:00
|
|
|
// PopulateStaticPeers connects current node with our publicly available LES/SHH/Swarm cluster
|
|
|
|
func (m *NodeManager) PopulateStaticPeers() {
|
|
|
|
for _, enode := range params.TestnetBootnodes {
|
2016-12-07 21:07:08 +00:00
|
|
|
m.AddPeer(enode)
|
|
|
|
}
|
|
|
|
}
|
2016-12-15 09:49:05 +00:00
|
|
|
|
|
|
|
func (k *SelectedExtKey) Hex() string {
|
|
|
|
if k == nil {
|
|
|
|
return "0x0"
|
|
|
|
}
|
|
|
|
|
|
|
|
return k.Address.Hex()
|
|
|
|
}
|