fix: deleted account maybe recover by `scanAccounts` if process meanwhile

pull/30053/head
Halimao 5 months ago
parent 22a1da1c65
commit b9aff2d86e
  1. 2
      accounts/keystore/account_cache.go
  2. 5
      accounts/keystore/file_cache.go
  3. 2
      accounts/keystore/keystore.go
  4. 2
      accounts/keystore/keystore_test.go

@ -284,9 +284,11 @@ func (ac *accountCache) scanAccounts() error {
start := time.Now() start := time.Now()
for _, path := range creates.ToSlice() { for _, path := range creates.ToSlice() {
ac.fileC.mu.Lock()
if a := readAccount(path); a != nil { if a := readAccount(path); a != nil {
ac.add(*a) ac.add(*a)
} }
ac.fileC.mu.Unlock()
} }
for _, path := range deletes.ToSlice() { for _, path := range deletes.ToSlice() {
ac.deleteByFile(path) ac.deleteByFile(path)

@ -39,6 +39,8 @@ type fileCache struct {
func (fc *fileCache) scan(keyDir string) (mapset.Set[string], mapset.Set[string], mapset.Set[string], error) { func (fc *fileCache) scan(keyDir string) (mapset.Set[string], mapset.Set[string], mapset.Set[string], error) {
t0 := time.Now() t0 := time.Now()
fc.mu.Lock()
defer fc.mu.Unlock()
// List all the files from the keystore folder // List all the files from the keystore folder
files, err := os.ReadDir(keyDir) files, err := os.ReadDir(keyDir)
if err != nil { if err != nil {
@ -46,9 +48,6 @@ func (fc *fileCache) scan(keyDir string) (mapset.Set[string], mapset.Set[string]
} }
t1 := time.Now() t1 := time.Now()
fc.mu.Lock()
defer fc.mu.Unlock()
// Iterate all the files and gather their metadata // Iterate all the files and gather their metadata
all := mapset.NewThreadUnsafeSet[string]() all := mapset.NewThreadUnsafeSet[string]()
mods := mapset.NewThreadUnsafeSet[string]() mods := mapset.NewThreadUnsafeSet[string]()

@ -228,6 +228,8 @@ func (ks *KeyStore) Accounts() []accounts.Account {
// Delete deletes the key matched by account if the passphrase is correct. // 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. // If the account contains no filename, the address must match a unique key.
func (ks *KeyStore) Delete(a accounts.Account, passphrase string) error { 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 // Decrypting the key isn't really necessary, but we do
// it anyway to check the password and zero out the key // it anyway to check the password and zero out the key
// immediately afterwards. // immediately afterwards.

@ -311,7 +311,7 @@ func TestWalletNotifications(t *testing.T) {
live = make(map[common.Address]accounts.Account) live = make(map[common.Address]accounts.Account)
wantEvents []walletEvent wantEvents []walletEvent
) )
for i := 0; i < 500; i++ { for i := 0; i < 1024; i++ {
if create := len(live) == 0 || rand.Int()%4 > 0; create { if create := len(live) == 0 || rand.Int()%4 > 0; create {
// Add a new account and ensure wallet notifications arrives // Add a new account and ensure wallet notifications arrives
account, err := ks.NewAccount("") account, err := ks.NewAccount("")

Loading…
Cancel
Save