status-go/geth/api/backend.go

317 lines
9.6 KiB
Go
Raw Normal View History

package api
import (
"context"
2018-03-22 12:31:12 +00:00
"errors"
"fmt"
"sync"
gethcommon "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/log"
"github.com/status-im/status-go/geth/account"
"github.com/status-im/status-go/geth/common"
"github.com/status-im/status-go/geth/jail"
"github.com/status-im/status-go/geth/node"
"github.com/status-im/status-go/geth/notifications/push/fcm"
"github.com/status-im/status-go/geth/params"
"github.com/status-im/status-go/geth/signal"
"github.com/status-im/status-go/geth/transactions"
2017-10-10 15:30:56 +00:00
)
const (
//todo(jeka): should be removed
fcmServerKey = "AAAAxwa-r08:APA91bFtMIToDVKGAmVCm76iEXtA4dn9MPvLdYKIZqAlNpLJbd12EgdBI9DSDSXKdqvIAgLodepmRhGVaWvhxnXJzVpE6MoIRuKedDV3kfHSVBhWFqsyoLTwXY4xeufL9Sdzb581U-lx"
)
2018-03-22 12:31:12 +00:00
var (
// ErrWhisperClearIdentitiesFailure clearing whisper identities has failed.
ErrWhisperClearIdentitiesFailure = errors.New("failed to clear whisper identities")
// ErrWhisperIdentityInjectionFailure injecting whisper identities has failed.
ErrWhisperIdentityInjectionFailure = errors.New("failed to inject identity into Whisper")
)
// StatusBackend implements Status.im service
type StatusBackend struct {
mu sync.Mutex
nodeManager *node.NodeManager
2018-03-22 12:31:12 +00:00
accountManager *account.Manager
txQueueManager *transactions.Manager
jailManager jail.Manager
2018-03-23 08:55:05 +00:00
newNotification fcm.NotificationConstructor
connectionState ConnectionState
log log.Logger
}
// NewStatusBackend create a new NewStatusBackend instance
func NewStatusBackend() *StatusBackend {
defer log.Info("Status backend initialized")
nodeManager := node.NewNodeManager()
accountManager := account.NewManager(nodeManager)
txQueueManager := transactions.NewManager(nodeManager, accountManager)
jailManager := jail.New(nodeManager)
notificationManager := fcm.NewNotification(fcmServerKey)
return &StatusBackend{
2017-10-18 20:17:40 +00:00
nodeManager: nodeManager,
accountManager: accountManager,
jailManager: jailManager,
txQueueManager: txQueueManager,
newNotification: notificationManager,
log: log.New("package", "status-go/geth/api.StatusBackend"),
}
}
// NodeManager returns reference to node manager
func (b *StatusBackend) NodeManager() *node.NodeManager {
2018-02-14 16:32:36 +00:00
return b.nodeManager
}
// AccountManager returns reference to account manager
2018-03-22 12:31:12 +00:00
func (b *StatusBackend) AccountManager() *account.Manager {
2018-02-14 16:32:36 +00:00
return b.accountManager
}
// JailManager returns reference to jail
func (b *StatusBackend) JailManager() jail.Manager {
2018-02-14 16:32:36 +00:00
return b.jailManager
}
// TxQueueManager returns reference to transactions manager
2018-02-14 16:32:36 +00:00
func (b *StatusBackend) TxQueueManager() *transactions.Manager {
return b.txQueueManager
}
// IsNodeRunning confirm that node is running
2018-02-14 16:32:36 +00:00
func (b *StatusBackend) IsNodeRunning() bool {
return b.nodeManager.IsNodeRunning()
}
// StartNode start Status node, fails if node is already started
2018-02-14 16:32:36 +00:00
func (b *StatusBackend) StartNode(config *params.NodeConfig) error {
b.mu.Lock()
defer b.mu.Unlock()
return b.startNode(config)
}
2018-02-14 16:32:36 +00:00
func (b *StatusBackend) startNode(config *params.NodeConfig) (err error) {
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("node crashed on start: %v", err)
}
}()
2018-02-14 16:32:36 +00:00
err = b.nodeManager.StartNode(config)
if err != nil {
switch err.(type) {
case node.RPCClientError:
2018-02-27 10:13:43 +00:00
err = fmt.Errorf("%v: %v", node.ErrRPCClient, err)
case node.EthNodeError:
err = fmt.Errorf("%v: %v", node.ErrNodeStartFailure, err)
}
signal.Send(signal.Envelope{
Type: signal.EventNodeCrashed,
Event: signal.NodeCrashEvent{
Error: err,
},
})
return err
}
signal.Send(signal.Envelope{Type: signal.EventNodeStarted})
// tx queue manager should be started after node is started, it depends
// on rpc client being created
2018-02-14 16:32:36 +00:00
b.txQueueManager.Start()
if err := b.registerHandlers(); err != nil {
b.log.Error("Handler registration failed", "err", err)
}
2018-03-22 12:31:12 +00:00
if err := b.ReSelectAccount(); err != nil {
b.log.Error("Reselect account failed", "err", err)
}
b.log.Info("Account reselected")
signal.Send(signal.Envelope{Type: signal.EventNodeReady})
return nil
}
// StopNode stop Status node. Stopped node cannot be resumed.
2018-02-14 16:32:36 +00:00
func (b *StatusBackend) StopNode() error {
b.mu.Lock()
defer b.mu.Unlock()
return b.stopNode()
}
2018-02-14 16:32:36 +00:00
func (b *StatusBackend) stopNode() error {
if !b.IsNodeRunning() {
return node.ErrNoRunningNode
}
2018-02-14 16:32:36 +00:00
b.txQueueManager.Stop()
b.jailManager.Stop()
defer signal.Send(signal.Envelope{Type: signal.EventNodeStopped})
return b.nodeManager.StopNode()
}
// RestartNode restart running Status node, fails if node is not running
2018-02-14 16:32:36 +00:00
func (b *StatusBackend) RestartNode() error {
if !b.IsNodeRunning() {
return node.ErrNoRunningNode
}
2018-02-14 16:32:36 +00:00
config, err := b.nodeManager.NodeConfig()
if err != nil {
return err
}
newcfg := *config
2018-02-14 16:32:36 +00:00
if err := b.stopNode(); err != nil {
return err
}
2018-02-14 16:32:36 +00:00
return b.startNode(&newcfg)
}
// ResetChainData remove chain data from data directory.
// Node is stopped, and new node is started, with clean data directory.
2018-02-14 16:32:36 +00:00
func (b *StatusBackend) ResetChainData() error {
b.mu.Lock()
defer b.mu.Unlock()
config, err := b.nodeManager.NodeConfig()
if err != nil {
return err
}
newcfg := *config
2018-02-14 16:32:36 +00:00
if err := b.stopNode(); err != nil {
return err
}
2018-02-14 16:32:36 +00:00
// config is cleaned when node is stopped
if err := b.nodeManager.ResetChainData(&newcfg); err != nil {
return err
}
signal.Send(signal.Envelope{Type: signal.EventChainDataRemoved})
2018-02-14 16:32:36 +00:00
return b.startNode(&newcfg)
}
// CallRPC executes RPC request on node's in-proc RPC server
2018-02-14 16:32:36 +00:00
func (b *StatusBackend) CallRPC(inputJSON string) string {
client := b.nodeManager.RPCClient()
return client.CallRaw(inputJSON)
}
// SendTransaction creates a new transaction and waits until it's complete.
2018-02-14 16:32:36 +00:00
func (b *StatusBackend) SendTransaction(ctx context.Context, args common.SendTxArgs) (hash gethcommon.Hash, err error) {
if ctx == nil {
ctx = context.Background()
}
tx := common.CreateTransaction(ctx, args)
2018-02-14 16:32:36 +00:00
if err = b.txQueueManager.QueueTransaction(tx); err != nil {
return hash, err
}
2018-02-14 16:32:36 +00:00
rst := b.txQueueManager.WaitForTransaction(tx)
if rst.Error != nil {
return hash, rst.Error
}
return rst.Hash, nil
}
// CompleteTransaction instructs backend to complete sending of a given transaction
2018-02-14 16:32:36 +00:00
func (b *StatusBackend) CompleteTransaction(id common.QueuedTxID, password string) (gethcommon.Hash, error) {
return b.txQueueManager.CompleteTransaction(id, password)
}
// CompleteTransactions instructs backend to complete sending of multiple transactions
2018-02-14 16:32:36 +00:00
func (b *StatusBackend) CompleteTransactions(ids []common.QueuedTxID, password string) map[common.QueuedTxID]common.TransactionResult {
return b.txQueueManager.CompleteTransactions(ids, password)
}
// DiscardTransaction discards a given transaction from transaction queue
2018-02-14 16:32:36 +00:00
func (b *StatusBackend) DiscardTransaction(id common.QueuedTxID) error {
return b.txQueueManager.DiscardTransaction(id)
}
// DiscardTransactions discards given multiple transactions from transaction queue
2018-02-14 16:32:36 +00:00
func (b *StatusBackend) DiscardTransactions(ids []common.QueuedTxID) map[common.QueuedTxID]common.RawDiscardTransactionResult {
return b.txQueueManager.DiscardTransactions(ids)
}
// registerHandlers attaches Status callback handlers to running node
2018-02-14 16:32:36 +00:00
func (b *StatusBackend) registerHandlers() error {
rpcClient := b.NodeManager().RPCClient()
2017-10-26 13:25:38 +00:00
if rpcClient == nil {
return node.ErrRPCClient
}
2018-03-22 12:31:12 +00:00
rpcClient.RegisterHandler("eth_accounts", func(context.Context, ...interface{}) (interface{}, error) {
return b.AccountManager().Accounts()
})
2018-02-14 16:32:36 +00:00
rpcClient.RegisterHandler("eth_sendTransaction", b.txQueueManager.SendTransactionRPCHandler)
return nil
}
// ConnectionChange handles network state changes logic.
func (b *StatusBackend) ConnectionChange(state ConnectionState) {
b.log.Info("Network state change", "old", b.connectionState, "new", state)
b.connectionState = state
// logic of handling state changes here
// restart node? force peers reconnect? etc
}
// AppStateChange handles app state changes (background/foreground).
func (b *StatusBackend) AppStateChange(state AppState) {
b.log.Info("App State changed.", "new-state", state)
// TODO: put node in low-power mode if the app is in background (or inactive)
// and normal mode if the app is in foreground.
}
2018-03-22 12:31:12 +00:00
// Logout clears whisper identities.
func (b *StatusBackend) Logout() error {
whisperService, err := b.nodeManager.WhisperService()
if err != nil {
return err
}
err = whisperService.DeleteKeyPairs()
if err != nil {
return fmt.Errorf("%s: %v", ErrWhisperClearIdentitiesFailure, err)
}
return b.AccountManager().Logout()
}
// ReSelectAccount selects previously selected account, often, after node restart.
func (b *StatusBackend) ReSelectAccount() error {
selectedAccount, err := b.AccountManager().SelectedAccount()
if selectedAccount == nil || err == account.ErrNoAccountSelected {
return nil
}
whisperService, err := b.nodeManager.WhisperService()
if err != nil {
return err
}
if err := whisperService.SelectKeyPair(selectedAccount.AccountKey.PrivateKey); err != nil {
return ErrWhisperIdentityInjectionFailure
}
return nil
}
// SelectAccount selects current account, by verifying that address has corresponding account which can be decrypted
// using provided password. Once verification is done, decrypted key is injected into Whisper (as a single identity,
// all previous identities are removed).
func (b *StatusBackend) SelectAccount(address, password string) error {
err := b.accountManager.SelectAccount(address, password)
if err != nil {
return err
}
acc, err := b.accountManager.SelectedAccount()
if err != nil {
return err
}
whisperService, err := b.nodeManager.WhisperService()
if err != nil {
return err
}
err = whisperService.SelectKeyPair(acc.AccountKey.PrivateKey)
if err != nil {
return ErrWhisperIdentityInjectionFailure
}
return nil
}