Stop fetching new blocks while app is not active

Saves some traffic (blocks with transactions are heavy) and infura
requests.
This commit is contained in:
Roman Volosovskyi 2020-02-28 18:05:37 +02:00
parent 97130dd6fe
commit 9d5684162a
No known key found for this signature in database
GPG Key ID: 0238A4B5ECEE70DE
3 changed files with 60 additions and 0 deletions

View File

@ -783,6 +783,37 @@ func (b *GethStatusBackend) AppStateChange(state string) {
b.log.Info("App State changed", "new-state", s)
b.appState = s
if s == appStateForeground {
wallet, err := b.statusNode.WalletService()
if err != nil {
b.log.Error("Retrieving of wallet service failed on app state change to active", "error", err)
return
}
if !wallet.IsStarted() {
err = wallet.Start(b.statusNode.Server())
if err != nil {
b.log.Error("Wallet service start failed on app state change to active", "error", err)
return
}
err = b.startWallet()
if err != nil {
b.log.Error("Wallet reactor start failed on app state change to active", "error", err)
return
}
}
} else if s == appStateBackground {
wallet, err := b.statusNode.WalletService()
if err != nil {
b.log.Error("Retrieving of wallet service failed on app state change to background", "error", err)
return
}
err = wallet.Stop()
if err != nil {
b.log.Error("Wallet service stop failed on app state change to background", "error", err)
return
}
}
// TODO: put node in low-power mode if the app is in background (or inactive)
// and normal mode if the app is in foreground.
}

View File

@ -149,3 +149,7 @@ func mapToList(m map[common.Address]struct{}) []common.Address {
}
return rst
}
func (s *Service) IsStarted() bool {
return s.group != nil
}

View File

@ -7,6 +7,7 @@ import (
"testing"
"time"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
"github.com/ethereum/go-ethereum/common"
@ -108,3 +109,27 @@ func (s *ReactorChangesSuite) TestWatchNewAccounts() {
return nil
}, 5*time.Second, 500*time.Millisecond))
}
func TestServiceStartStop(t *testing.T) {
db, stop := setupTestDB(t)
defer stop()
backend, err := testchain.NewBackend()
require.NoError(t, err)
s := NewService(db, &event.Feed{})
require.NoError(t, s.Start(nil))
account, err := crypto.GenerateKey()
require.NoError(t, err)
err = s.StartReactor(backend.Client, []common.Address{crypto.PubkeyToAddress(account.PublicKey)}, big.NewInt(1337))
require.NoError(t, err)
require.NoError(t, s.Stop())
require.NoError(t, s.Start(nil))
err = s.StartReactor(backend.Client, []common.Address{crypto.PubkeyToAddress(account.PublicKey)}, big.NewInt(1337))
require.NoError(t, err)
require.NoError(t, s.Stop())
}