Skip to content

Commit c5b2896

Browse files
committed
backend/account: fix bugs in account discovery
Recent commit 726289c made the account Initialization asynchronous, moving the `account.ensureAddresses()` call on a separate thread. This caused a regression inside `backend.checkAccountUsed()` where the method wasn't waiting for the complete account synchronization before marking it as used. This commit fixes the issue waiting for account sync before checking if it used. This also fixes the `backend.Accounts()` method, that was returning the original `backend.accounts` object instead of a copy. This caused the caller to read the account list without a proper lock, causing weird behaviors during account discovery.
1 parent b77cd13 commit c5b2896

File tree

2 files changed

+7
-6
lines changed

2 files changed

+7
-6
lines changed

backend/accounts.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -750,12 +750,16 @@ func (backend *Backend) addAccount(account accounts.Interface) {
750750
})
751751
if event.Subject == string(accountsTypes.EventSyncDone) {
752752
backend.notifyNewTxs(account)
753+
go backend.checkAccountUsed(account)
753754
}
754755
})
756+
if err := account.Initialize(); err != nil {
757+
backend.log.WithError(err).Error("error initializing account")
758+
return
759+
}
755760
if backend.onAccountInit != nil {
756761
backend.onAccountInit(account)
757762
}
758-
go backend.checkAccountUsed(account)
759763
}
760764

761765
// The accountsAndKeystoreLock must be held when calling this function.
@@ -1529,10 +1533,6 @@ func (backend *Backend) checkAccountUsed(account accounts.Interface) {
15291533
}
15301534
}
15311535
log := backend.log.WithField("accountCode", account.Config().Config.Code)
1532-
if err := account.Initialize(); err != nil {
1533-
log.WithError(err).Error("error initializing account")
1534-
return
1535-
}
15361536
txs, err := account.Transactions()
15371537
if err != nil {
15381538
log.WithError(err).Error("discoverAccount")

backend/backend.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"net/url"
2323
"os"
2424
"path/filepath"
25+
"slices"
2526
"strings"
2627
"time"
2728

@@ -590,7 +591,7 @@ func (backend *Backend) Testing() bool {
590591
// Accounts returns the current accounts of the backend.
591592
func (backend *Backend) Accounts() AccountsList {
592593
defer backend.accountsAndKeystoreLock.RLock()()
593-
return backend.accounts
594+
return slices.Clone(backend.accounts)
594595
}
595596

596597
// KeystoreTotalAmount represents the total balance amount of the accounts belonging to a keystore.

0 commit comments

Comments
 (0)