From b9aff2d86ef840e017c7fa3e5832d938a6ef967b Mon Sep 17 00:00:00 2001 From: Halimao <1065621723@qq.com> Date: Tue, 25 Jun 2024 15:21:08 +0800 Subject: [PATCH] fix: deleted account maybe recover by `scanAccounts` if process meanwhile --- accounts/keystore/account_cache.go | 2 ++ accounts/keystore/file_cache.go | 5 ++--- accounts/keystore/keystore.go | 2 ++ accounts/keystore/keystore_test.go | 2 +- 4 files changed, 7 insertions(+), 4 deletions(-) diff --git a/accounts/keystore/account_cache.go b/accounts/keystore/account_cache.go index f7cf688e62..fb7877ffba 100644 --- a/accounts/keystore/account_cache.go +++ b/accounts/keystore/account_cache.go @@ -284,9 +284,11 @@ func (ac *accountCache) scanAccounts() error { start := time.Now() for _, path := range creates.ToSlice() { + ac.fileC.mu.Lock() if a := readAccount(path); a != nil { ac.add(*a) } + ac.fileC.mu.Unlock() } for _, path := range deletes.ToSlice() { ac.deleteByFile(path) diff --git a/accounts/keystore/file_cache.go b/accounts/keystore/file_cache.go index 63eb850374..390551a1f1 100644 --- a/accounts/keystore/file_cache.go +++ b/accounts/keystore/file_cache.go @@ -39,6 +39,8 @@ type fileCache struct { func (fc *fileCache) scan(keyDir string) (mapset.Set[string], mapset.Set[string], mapset.Set[string], error) { t0 := time.Now() + fc.mu.Lock() + defer fc.mu.Unlock() // List all the files from the keystore folder files, err := os.ReadDir(keyDir) if err != nil { @@ -46,9 +48,6 @@ func (fc *fileCache) scan(keyDir string) (mapset.Set[string], mapset.Set[string] } t1 := time.Now() - fc.mu.Lock() - defer fc.mu.Unlock() - // Iterate all the files and gather their metadata all := mapset.NewThreadUnsafeSet[string]() mods := mapset.NewThreadUnsafeSet[string]() diff --git a/accounts/keystore/keystore.go b/accounts/keystore/keystore.go index df3dda60b6..13bcb670b1 100644 --- a/accounts/keystore/keystore.go +++ b/accounts/keystore/keystore.go @@ -228,6 +228,8 @@ func (ks *KeyStore) Accounts() []accounts.Account { // Delete deletes the key matched by account if the passphrase is correct. // If the account contains no filename, the address must match a unique key. func (ks *KeyStore) Delete(a accounts.Account, passphrase string) error { + ks.cache.fileC.mu.Lock() + defer ks.cache.fileC.mu.Unlock() // Decrypting the key isn't really necessary, but we do // it anyway to check the password and zero out the key // immediately afterwards. diff --git a/accounts/keystore/keystore_test.go b/accounts/keystore/keystore_test.go index d2d52b1b3f..7f541ddfed 100644 --- a/accounts/keystore/keystore_test.go +++ b/accounts/keystore/keystore_test.go @@ -311,7 +311,7 @@ func TestWalletNotifications(t *testing.T) { live = make(map[common.Address]accounts.Account) wantEvents []walletEvent ) - for i := 0; i < 500; i++ { + for i := 0; i < 1024; i++ { if create := len(live) == 0 || rand.Int()%4 > 0; create { // Add a new account and ensure wallet notifications arrives account, err := ks.NewAccount("")