From 10c214ffd5952a46da0de073372c1373bfde4842 Mon Sep 17 00:00:00 2001 From: Marius van der Wijden Date: Tue, 14 Apr 2020 13:13:41 +0200 Subject: [PATCH] accounts/keystore: fix race in Import/ImportECDSA --- accounts/keystore/keystore.go | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/accounts/keystore/keystore.go b/accounts/keystore/keystore.go index 5b55175b1f..09aa8e7834 100644 --- a/accounts/keystore/keystore.go +++ b/accounts/keystore/keystore.go @@ -67,7 +67,8 @@ type KeyStore struct { updateScope event.SubscriptionScope // Subscription scope tracking current live listeners updating bool // Whether the event notification loop is running - mu sync.RWMutex + mu sync.RWMutex + impMu sync.RWMutex // Import Mutex locks the import to prevent two insertions from racing } type unlocked struct { @@ -443,12 +444,19 @@ func (ks *KeyStore) Import(keyJSON []byte, passphrase, newPassphrase string) (ac if err != nil { return accounts.Account{}, err } + ks.impMu.Lock() + defer ks.impMu.Unlock() + if ks.cache.hasAddress(key.Address) { + return accounts.Account{}, fmt.Errorf("account already exists") + } return ks.importKey(key, newPassphrase) } // ImportECDSA stores the given key into the key directory, encrypting it with the passphrase. func (ks *KeyStore) ImportECDSA(priv *ecdsa.PrivateKey, passphrase string) (accounts.Account, error) { key := newKeyFromECDSA(priv) + ks.impMu.Lock() + defer ks.impMu.Unlock() if ks.cache.hasAddress(key.Address) { return accounts.Account{}, fmt.Errorf("account already exists") }