Stop/start wallet methods

This commit is contained in:
Roman Volosovskyi 2020-07-29 12:13:47 +03:00 committed by Andrea Maria Piana
parent 19487da894
commit aa1b898fe4
3 changed files with 68 additions and 14 deletions

View File

@ -84,6 +84,7 @@ type GethStatusBackend struct {
selectedAccountKeyID string
log log.Logger
allowAllRPC bool // used only for tests, disables api method restrictions
forceStopWallet bool
}
// NewGethStatusBackend create a new GethStatusBackend instance
@ -269,9 +270,6 @@ func (b *GethStatusBackend) startNodeWithKey(acc multiaccounts.Account, password
if err != nil {
return err
}
if err := b.startWallet(); err != nil {
return err
}
err = b.multiaccountsDB.UpdateAccountTimestamp(acc.KeyUID, time.Now().Unix())
if err != nil {
return err
@ -856,7 +854,7 @@ func (b *GethStatusBackend) AppStateChange(state string) {
b.log.Info("App State changed", "new-state", s)
b.appState = s
if s == appStateForeground {
if s == appStateForeground && !b.forceStopWallet {
wallet, err := b.statusNode.WalletService()
if err != nil {
b.log.Error("Retrieving of wallet service failed on app state change to active", "error", err)
@ -875,7 +873,7 @@ func (b *GethStatusBackend) AppStateChange(state string) {
return
}
}
} else if s == appStateBackground {
} else if s == appStateBackground && !b.forceStopWallet {
wallet, err := b.statusNode.WalletService()
if err != nil {
b.log.Error("Retrieving of wallet service failed on app state change to background", "error", err)
@ -891,6 +889,50 @@ func (b *GethStatusBackend) AppStateChange(state string) {
// and normal mode if the app is in foreground.
}
func (b *GethStatusBackend) StopWallet() error {
wallet, err := b.statusNode.WalletService()
if err != nil {
b.log.Error("Retrieving of wallet service failed on StopWallet", "error", err)
return nil
}
if wallet.IsStarted() {
err = wallet.Stop()
if err != nil {
b.log.Error("Wallet service stop failed on StopWallet", "error", err)
return nil
}
}
b.forceStopWallet = true
return nil
}
func (b *GethStatusBackend) StartWallet() error {
wallet, err := b.statusNode.WalletService()
if err != nil {
b.log.Error("Retrieving of wallet service failed on StartWallet", "error", err)
return nil
}
if !wallet.IsStarted() {
err = wallet.Start(b.statusNode.Server())
if err != nil {
b.log.Error("Wallet service start failed on StartWallet", "error", err)
return nil
}
err = b.startWallet()
if err != nil {
b.log.Error("Wallet reactor start failed on StartWallet", "error", err)
return nil
}
}
b.forceStopWallet = false
return nil
}
// Logout clears whisper identities.
func (b *GethStatusBackend) Logout() error {
b.mu.Lock()
@ -940,10 +982,12 @@ func (b *GethStatusBackend) cleanupServices() error {
switch err {
case node.ErrServiceUnknown:
case nil:
err = wallet.StopReactor()
if wallet.IsStarted() {
err = wallet.Stop()
if err != nil {
return err
}
}
default:
return err
}
@ -981,10 +1025,6 @@ func (b *GethStatusBackend) SelectAccount(loginParams account.LoginParams) error
return err
}
if err := b.startWallet(); err != nil {
return err
}
return nil
}

View File

@ -624,6 +624,18 @@ func AppStateChange(state string) {
statusBackend.AppStateChange(state)
}
// StopWallet
func StopWallet() string {
err := statusBackend.StopWallet()
return makeJSONResponse(err)
}
// StartWallet
func StartWallet() string {
err := statusBackend.StartWallet()
return makeJSONResponse(err)
}
// SetMobileSignalHandler setup geth callback to notify about new signal
// used for gomobile builds
func SetMobileSignalHandler(handler SignalHandler) {

View File

@ -64,8 +64,10 @@ func (s *Service) StopReactor() error {
return nil
}
s.reactor.Stop()
if s.group != nil {
s.group.Stop()
s.group.Wait()
}
return nil
}