Fetching private key every time increases goroutine count
Yashk767 opened this issue · 0 comments
Problem
We use GetPrivateKeyFromKeystore(address, password, keystorePath) to fetch the private key from the keystore.
func (AccountUtils) GetPrivateKey(address string, password string, keystorePath string) (*ecdsa.PrivateKey, error) {
allAccounts := AccountUtilsInterface.Accounts(keystorePath)
for _, account := range allAccounts {
if strings.EqualFold(account.Address.Hex(), address) {
return AccountUtilsInterface.GetPrivateKeyFromKeystore(account.URL.Path, password)
}
}
return nil, errors.New("no keystore file found")
}
where we iterate over all the accounts keystore files using ks.Accounts and try to match the desired keystore file for the node.
So here the KeyStore uses file watching mechanisms to automatically detect changes in the keystore directory (adding or removing key files). This involves goroutines waiting for filesystem events.
So when we do ks.Accounnts every time it creates a new goroutine which keep waiting infinitely.
Solution
Either keep reloading ks.Accounts() in a separate go routine in a separate cache and add a getter function in GetPrivateKeyFromKeystore() which will just get the whole list of accounts keystore files. [Check security measures]
Find a way to skip using ks.Accounts call to fetch private key.
Store private key in memory at the start and reuse it when needed. [Check security measures]