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

This commit is contained in:
Halimao 2024-06-25 15:21:08 +08:00
parent 22a1da1c65
commit b9aff2d86e
4 changed files with 7 additions and 4 deletions

View File

@ -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)

View File

@ -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]()

View File

@ -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.

View File

@ -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("")