Skip to content

Conversation

@Yashk767
Copy link
Contributor

@Yashk767 Yashk767 commented Mar 20, 2024

Description

Previously, when fetching keys from the keystore, we used ks.Accounts(), which created a new goroutine every time. This approach led to goroutines waiting indefinitely, which was inefficient and could potentially cause resource exhaustion issues. To optimize this process and avoid unnecessary resource consumption, the solution is to use the same keystore instance every time to call ks.Accounts() will solves the issue of increasing go routines.

So this PR optimises keystore file fetching to prevent indefinite waiting by goroutines.

Fixes #1195

Solution
Creating and initialising the keystore instance once at the start and using the same to call ks.Accounts() at every fetching of pKey call doesnt increase the goroutine count and it also gives the updated keystore cache at the time of fetching accounts.

How Has This Been Tested?

  • Added realistic fetching of pKey from keystore file tests.
  • Account Imports: Tested the creation and importation of both new and old accounts to verify compatibility and functionality across different account types and ages.

@ashish10677
Copy link
Member

Tests failing @Yashk767

ashish10677
ashish10677 previously approved these changes Mar 21, 2024
@SkandaBhat
Copy link
Contributor

SkandaBhat commented Mar 28, 2024


import (
	"crypto/ecdsa"
	"github.com/ethereum/go-ethereum/accounts"
	"github.com/ethereum/go-ethereum/accounts/keystore"
	"razor/logger"
	"razor/path"
	"os"
	"strings"
)

type AccountManager struct {
	log *logger.Logger
	ks  *keystore.KeyStore
}

// NewAccountManager creates a new AccountManager instance.
func NewAccountManager() *AccountManager {
	return &AccountManager{
		log: logger.NewLogger(),
	}
}

// InitializeKeystore initializes the keystore instance.
func (am *AccountManager) InitializeKeystore(keystorePath string) {
	am.log.Info("Initialising keystoreInstance...")
	am.ks = keystore.NewKeyStore(keystorePath, keystore.StandardScryptN, keystore.StandardScryptP)
}

// CreateAccount creates a new account.
func (am *AccountManager) CreateAccount(keystorePath string, password string) accounts.Account {
	// Method implementation...
}

// GetPrivateKey retrieves the private key from the keystore.
func (am *AccountManager) GetPrivateKey(address string, password string, keystoreDirPath string) (*ecdsa.PrivateKey, error) {
	// Method implementation...
}

// SignData signs the data.
func (am *AccountManager) SignData(hash []byte, account types.Account, defaultPath string) ([]byte, error) {
	// Method implementation...
}

Please follow this pattern. cc: @ashish10677

@SkandaBhat SkandaBhat closed this Mar 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants