Skip to content

Commit

Permalink
v1.2.0-dev release (#13)
Browse files Browse the repository at this point in the history
* refactor: used the same keystore instance to fetch pKey everytime  (razor-network#1200)

* refactor: used regex instead of ks.Accounts to match keystore file

* refactor: added more extensive realistic testing for fetching of private key

* ci: ignore test-accounts directory in coverage

* refactor: added test when multiple timestamps keystore file present for same account

* refactor: covered lower and upper case addresses

* revert: optimised fetching key from keystore dir

* fix: reused same keystore instance for fetching pkey every time

* refactor: added realistic tests for fetching of pKey

* refactor: removed unused accounts() from interface

* refactor: used AccountManager struct with a keystore as a method caller

* refactor: fix tests

* refactor: removed commented test code

* refactor: returned interface instead of type struct instance from AccountManagerForKeystore()

* refactor: fixed create tests

* refactor: compared expected and returned pKey values in tests

* refactor: requested changes

* fix: changed keyword to const for defining constants (razor-network#1206)

* fix: changed keyword to const for defining constants

* fix: fixed lint error by adding explicit type for ProcessRequestRetryDelay

* refactor: updated RPC to op sepolia in testing post request

* refactor: used a common http client for all the requests (razor-network#1201)

* refactor: used a common http client throughout for all the APIs

* fix: handled goroutine exit

* refactor: golangci-lint fix

* refactor: defined http client fields in constants.go

* refactor: defined http client as a field to HttpClient struct used as a method caller

* refactor: revert InvokeFunctionWithTimeout() changes

* refator: fixed tests

* refactor: moved httpClient as the first parameter in api.go functions signature

* refactor: removed exporting of client field from HttpClient struct

* refactor: changed var to const keyword for http client constants

* refactor: Optimised number of `GetBlockNumber` and `GetStakerId` eth calls (razor-network#1204)

* refactor: Used getStakerId() only once

* refactor: used exisiting blockNumber from Vote() in dependent functions

* refactor: fixed tests after header being passed as a parameter

* fix: removed continous retries if error in fetching block number in logger

* refactor: added tests when context is cancelled for Vote()

* refactor: fixed vote tests

* fix: updated default config values to support both 20 min and shorter 5 min epoch  (razor-network#1209)

* fix: updated config default max, min values to support longer epoch

* fix: updated default config values in config.sh if builded from source

* feat: Implemented batching of multiple getStakeSnapshot RPC calls (razor-network#1218)

* feat: Implemented batching of multiple getStakeSnapshot RPC calls

* refactor: added method caller to batch call functions and added it to interface

* refactor: added new and fixed exisiting tests

* refactor: fixed log with correct staker Id associated for stake

* refactor: moved getStakeSnapshot string to constants

* refactor: implemented generic batch call function

* refactor: used generic batch call for getStakeSnapshot

* refactor: removed timeout and address parameter from GetBiggestStakeAnId()

* refactor: fetching jobs and collections from asset caches (razor-network#1207)

* feat: added jobs/collections cache structs

* feat: added support for using jobs/collections caches

* refactor: added time constants required for job/collection cache updates

* fix: fixed resetting of cache every expiry interval

* refactor: fixed error log

* refactor: fix utils tests

* refactor: added httpClient instance in commitParams struct

* refactor: removed resetting of cache as its not required

* feat: checked for job/collection events at the start of commit state in main go routine

* refactor: used FilterLogswithRetry and fixed other tests

* refactor: fixed benchmark

* refactor: moved all the event names to constants

* refactor: requested changes

* refactor: removed custom http client struct and directly used inbuilt http.Client

* refcator: updated default logFileMaxAge and logFileMaxBackups (razor-network#1221)

* chore: updated default deployment parameters and version for `v1.2.0` (razor-network#1222)

* chore: updated default addresses and chainId to mainnet addresses and chainId

* chore: updated testnet addresses to staging europa 20 min epoch

* chore: updated version to v1.2.0

* refactor: fixed tests due to state length change to 1200
  • Loading branch information
Yashk767 authored Aug 9, 2024
1 parent 12d57a1 commit e6a80ed
Show file tree
Hide file tree
Showing 93 changed files with 2,572 additions and 1,489 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ docker exec -it razor-go razor setConfig --provider <rpc_provider> --alternatePr
Example:

```
$ ./razor setConfig --provider https://mainnet.skalenodes.com/v1/turbulent-unique-scheat --alternateProvider https://ce2m-skale.chainode.tech:10200/ --gasmultiplier 1 --buffer 20 --wait 30 --gasprice 0 --logLevel debug --gasLimit 2 --rpcTimeout 10 --httpTimeout 10 --logFileMaxSize 200 --logFileMaxBackups 52 --logFileMaxAge 365
$ ./razor setConfig --provider https://mainnet.skalenodes.com/v1/turbulent-unique-scheat --alternateProvider https://ce2m-skale.chainode.tech:10200/ --gasmultiplier 1 --buffer 20 --wait 30 --gasprice 0 --logLevel debug --gasLimit 2 --rpcTimeout 10 --httpTimeout 10 --logFileMaxSize 200 --logFileMaxBackups 10 --logFileMaxAge 60
```

Besides, setting these parameters in the config, you can use different values for these parameters in various commands. Just add the same flag to any command you want to use and the new config changes will appear for that command.
Expand Down
58 changes: 14 additions & 44 deletions accounts/accountUtils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,56 +2,26 @@
package accounts

import (
"crypto/ecdsa"
"github.com/ethereum/go-ethereum/accounts"
"github.com/ethereum/go-ethereum/accounts/keystore"
"github.com/ethereum/go-ethereum/crypto"
"os"
"razor/core/types"
)

//go:generate mockery --name AccountInterface --output ./mocks/ --case=underscore

var AccountUtilsInterface AccountInterface

type AccountInterface interface {
CreateAccount(path string, password string) accounts.Account
GetPrivateKeyFromKeystore(keystorePath string, password string) (*ecdsa.PrivateKey, error)
GetPrivateKey(address string, password string, keystorePath string) (*ecdsa.PrivateKey, error)
SignData(hash []byte, account types.Account, defaultPath string) ([]byte, error)
Accounts(path string) []accounts.Account
NewAccount(path string, passphrase string) (accounts.Account, error)
DecryptKey(jsonBytes []byte, password string) (*keystore.Key, error)
Sign(digestHash []byte, prv *ecdsa.PrivateKey) ([]byte, error)
ReadFile(filename string) ([]byte, error)
}

type AccountUtils struct{}

//This function returns all the accounts in form of array
func (accountUtils AccountUtils) Accounts(path string) []accounts.Account {
ks := keystore.NewKeyStore(path, keystore.StandardScryptN, keystore.StandardScryptP)
return ks.Accounts()
}

//This function takes path and pass phrase as input and returns the new account
func (accountUtils AccountUtils) NewAccount(path string, passphrase string) (accounts.Account, error) {
ks := keystore.NewKeyStore(path, keystore.StandardScryptN, keystore.StandardScryptP)
accounts.NewManager(&accounts.Config{InsecureUnlockAllowed: false}, ks)
return ks.NewAccount(passphrase)
}

//This function takes json bytes array and password as input and returns the decrypted key
func (accountUtils AccountUtils) DecryptKey(jsonBytes []byte, password string) (*keystore.Key, error) {
return keystore.DecryptKey(jsonBytes, password)
type AccountManager struct {
Keystore *keystore.KeyStore
}

//This function takes hash in form of byte array and private key as input and returns signature as byte array
func (accountUtils AccountUtils) Sign(digestHash []byte, prv *ecdsa.PrivateKey) (sig []byte, err error) {
return crypto.Sign(digestHash, prv)
func NewAccountManager(keystorePath string) *AccountManager {
ks := keystore.NewKeyStore(keystorePath, keystore.StandardScryptN, keystore.StandardScryptP)
return &AccountManager{
Keystore: ks,
}
}

//This function takes name of the file as input and returns the file data as byte array
func (accountUtils AccountUtils) ReadFile(filename string) ([]byte, error) {
return os.ReadFile(filename)
// InitAccountStruct initializes an Account struct with provided details.
func InitAccountStruct(address, password string, accountManager types.AccountManagerInterface) types.Account {
return types.Account{
Address: address,
Password: password,
AccountManager: accountManager,
}
}
49 changes: 29 additions & 20 deletions accounts/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import (
"crypto/ecdsa"
"errors"
"github.com/ethereum/go-ethereum/accounts"
"razor/core/types"
"github.com/ethereum/go-ethereum/accounts/keystore"
"github.com/ethereum/go-ethereum/crypto"
"os"
"razor/logger"
"razor/path"
"strings"
Expand All @@ -14,51 +16,58 @@ import (
var log = logger.NewLogger()

//This function takes path and password as input and returns new account
func (AccountUtils) CreateAccount(keystorePath string, password string) accounts.Account {
func (am *AccountManager) CreateAccount(keystorePath string, password string) accounts.Account {
if _, err := path.OSUtilsInterface.Stat(keystorePath); path.OSUtilsInterface.IsNotExist(err) {
mkdirErr := path.OSUtilsInterface.Mkdir(keystorePath, 0700)
if mkdirErr != nil {
log.Fatal("Error in creating directory: ", mkdirErr)
}
}
newAcc, err := AccountUtilsInterface.NewAccount(keystorePath, password)
newAcc, err := am.NewAccount(password)
if err != nil {
log.Fatal("Error in creating account: ", err)
}
return newAcc
}

//This function takes path and pass phrase as input and returns the new account
func (am *AccountManager) NewAccount(passphrase string) (accounts.Account, error) {
ks := am.Keystore
accounts.NewManager(&accounts.Config{InsecureUnlockAllowed: false}, ks)
return ks.NewAccount(passphrase)
}

//This function takes address of account, password and keystore path as input and returns private key of account
func (am *AccountManager) GetPrivateKey(address string, password string) (*ecdsa.PrivateKey, error) {
allAccounts := am.Keystore.Accounts()
for _, account := range allAccounts {
if strings.EqualFold(account.Address.Hex(), address) {
return getPrivateKeyFromKeystore(account.URL.Path, password)
}
}
return nil, errors.New("no keystore file found")
}

//This function takes and path of keystore and password as input and returns private key of account
func (AccountUtils) GetPrivateKeyFromKeystore(keystorePath string, password string) (*ecdsa.PrivateKey, error) {
jsonBytes, err := AccountUtilsInterface.ReadFile(keystorePath)
func getPrivateKeyFromKeystore(keystoreFilePath string, password string) (*ecdsa.PrivateKey, error) {
jsonBytes, err := os.ReadFile(keystoreFilePath)
if err != nil {
log.Error("Error in reading keystore: ", err)
return nil, err
}
key, err := AccountUtilsInterface.DecryptKey(jsonBytes, password)
key, err := keystore.DecryptKey(jsonBytes, password)
if err != nil {
log.Error("Error in fetching private key: ", err)
return nil, err
}
return key.PrivateKey, nil
}

//This function takes address of account, password and keystore path as input and returns private key of account
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")
}

//This function takes hash, account and path as input and returns the signed data as array of byte
func (AccountUtils) SignData(hash []byte, account types.Account, defaultPath string) ([]byte, error) {
privateKey, err := AccountUtilsInterface.GetPrivateKey(account.Address, account.Password, defaultPath)
func (am *AccountManager) SignData(hash []byte, address string, password string) ([]byte, error) {
privateKey, err := am.GetPrivateKey(address, password)
if err != nil {
return nil, err
}
return AccountUtilsInterface.Sign(hash, privateKey)
return crypto.Sign(hash, privateKey)
}
Loading

0 comments on commit e6a80ed

Please sign in to comment.