This commit is contained in:
Halimao 2024-11-25 09:58:41 +07:00 committed by GitHub
commit 37846ed2e7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 13 additions and 12 deletions

View File

@ -283,9 +283,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

@ -17,6 +17,7 @@
package keystore
import (
"fmt"
"math/rand"
"os"
"runtime"
@ -339,8 +340,8 @@ func TestWalletNotifications(t *testing.T) {
for ev := range updates {
events = append(events, walletEvent{ev, ev.Wallet.Accounts()[0]})
}
checkAccounts(t, live, ks.Wallets())
checkEvents(t, wantEvents, events)
checkAccounts(t, live, ks.Wallets())
}
// TestImportECDSA tests the import functionality of a keystore.
@ -443,15 +444,12 @@ func checkAccounts(t *testing.T, live map[common.Address]accounts.Account, walle
// checkEvents checks that all events in 'want' are present in 'have'. Events may be present multiple times.
func checkEvents(t *testing.T, want []walletEvent, have []walletEvent) {
filter := make(map[string]struct{})
for _, haveEv := range have {
filter[fmt.Sprintf("%s-%d", haveEv.a.Address.String(), haveEv.Kind)] = struct{}{}
}
for _, wantEv := range want {
nmatch := 0
for ; len(have) > 0; nmatch++ {
if have[0].Kind != wantEv.Kind || have[0].a != wantEv.a {
break
}
have = have[1:]
}
if nmatch == 0 {
if _, ok := filter[fmt.Sprintf("%s-%d", wantEv.a.Address.String(), wantEv.Kind)]; !ok {
t.Fatalf("can't find event with Kind=%v for %x", wantEv.Kind, wantEv.a.Address)
}
}