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:
parent
97130dd6fe
commit
9d5684162a
|
@ -783,6 +783,37 @@ func (b *GethStatusBackend) AppStateChange(state string) {
|
||||||
b.log.Info("App State changed", "new-state", s)
|
b.log.Info("App State changed", "new-state", s)
|
||||||
b.appState = 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)
|
// TODO: put node in low-power mode if the app is in background (or inactive)
|
||||||
// and normal mode if the app is in foreground.
|
// and normal mode if the app is in foreground.
|
||||||
}
|
}
|
||||||
|
|
|
@ -149,3 +149,7 @@ func mapToList(m map[common.Address]struct{}) []common.Address {
|
||||||
}
|
}
|
||||||
return rst
|
return rst
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *Service) IsStarted() bool {
|
||||||
|
return s.group != nil
|
||||||
|
}
|
||||||
|
|
|
@ -7,6 +7,7 @@ import (
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
"github.com/stretchr/testify/suite"
|
"github.com/stretchr/testify/suite"
|
||||||
|
|
||||||
"github.com/ethereum/go-ethereum/common"
|
"github.com/ethereum/go-ethereum/common"
|
||||||
|
@ -108,3 +109,27 @@ func (s *ReactorChangesSuite) TestWatchNewAccounts() {
|
||||||
return nil
|
return nil
|
||||||
}, 5*time.Second, 500*time.Millisecond))
|
}, 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())
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue