Skip to content

Commit 94e1a7c

Browse files
committed
backend: do not re-initialize watch-only accounts
When disconnecting a keystore, previously all accounts were removed, because we had no watch-only accounts. When registering, all keystore accounts were (re-)loaded, because they were the only ones to be shown. With watch-only, we don't want to reinit accounts that are being watched. This allows a user to connect a keystore e.g. in the middle of an action inside an account, like displaying an address, without being disrupted. The force flag is needed to reinitialize also watch-only accounts if e.g. the activeTokens field changes for an ETH account. Ideally we can always dynamically fix the loaded accounts to match the persisted config, but that is for the future.
1 parent 73c8b0d commit 94e1a7c

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

backend/accounts.go

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,10 @@ func (backend *Backend) addAccount(account accounts.Interface) {
502502

503503
// The accountsAndKeystoreLock must be held when calling this function.
504504
func (backend *Backend) createAndAddAccount(coin coinpkg.Coin, persistedConfig *config.Account) {
505+
if backend.accounts.lookup(persistedConfig.Code) != nil {
506+
// Do not create/load account if it is already loaded.
507+
return
508+
}
505509
var account accounts.Interface
506510
accountConfig := &accounts.AccountConfig{
507511
Config: persistedConfig,
@@ -933,9 +937,10 @@ func (backend *Backend) updatePersistedAccounts(
933937
}
934938

935939
// The accountsAndKeystoreLock must be held when calling this function.
936-
func (backend *Backend) initAccounts() {
940+
// if force is true, all accounts are uninitialized first, even if they are watch-only.
941+
func (backend *Backend) initAccounts(force bool) {
937942
// Since initAccounts replaces all previous accounts, we need to properly close them first.
938-
backend.uninitAccounts()
943+
backend.uninitAccounts(force)
939944

940945
backend.initPersistedAccounts()
941946

@@ -955,19 +960,26 @@ func (backend *Backend) ReinitializeAccounts() {
955960
defer backend.accountsAndKeystoreLock.Lock()()
956961

957962
backend.log.Info("Reinitializing accounts")
958-
backend.initAccounts()
963+
backend.initAccounts(true)
959964
}
960965

961966
// The accountsAndKeystoreLock must be held when calling this function.
962-
func (backend *Backend) uninitAccounts() {
967+
// if force is true, all accounts are uninitialized, even if they are watch-only.
968+
func (backend *Backend) uninitAccounts(force bool) {
969+
keep := []accounts.Interface{}
963970
for _, account := range backend.accounts {
964971
account := account
972+
if !force && account.Config().Config.IsWatch() {
973+
// Do not uninit/remove account that is being watched.
974+
keep = append(keep, account)
975+
continue
976+
}
965977
if backend.onAccountUninit != nil {
966978
backend.onAccountUninit(account)
967979
}
968980
account.Close()
969981
}
970-
backend.accounts = []accounts.Interface{}
982+
backend.accounts = keep
971983
}
972984

973985
// maybeAddHiddenUnusedAccounts adds a hidden account for scanning to facilitate accounts discovery.

backend/backend.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -556,7 +556,7 @@ func (backend *Backend) registerKeystore(keystore keystore.Keystore) {
556556
log.WithError(err).Error("Could not persist default accounts")
557557
}
558558

559-
backend.initAccounts()
559+
backend.initAccounts(false)
560560

561561
backend.aoppKeystoreRegistered()
562562
}
@@ -578,7 +578,7 @@ func (backend *Backend) DeregisterKeystore() {
578578
Action: action.Reload,
579579
})
580580

581-
backend.uninitAccounts()
581+
backend.uninitAccounts(false)
582582
// TODO: classify accounts by keystore, remove only the ones belonging to the deregistered
583583
// keystore. For now we just remove all, then re-add the rest.
584584
backend.initPersistedAccounts()
@@ -721,7 +721,7 @@ func (backend *Backend) Close() error {
721721

722722
backend.ratesUpdater.Stop()
723723

724-
backend.uninitAccounts()
724+
backend.uninitAccounts(true)
725725

726726
for _, coin := range backend.coins {
727727
if err := coin.Close(); err != nil {

0 commit comments

Comments
 (0)