From ce549458fe945a7fb896264a622fd669bbf10c20 Mon Sep 17 00:00:00 2001 From: Goran Rojovic Date: Tue, 26 Dec 2023 11:00:01 +0100 Subject: [PATCH] small optimization --- jsonrpc/dispatcher.go | 14 +++++++++----- jsonrpc/eth_endpoint.go | 42 ++++++++++++++++++++++++++++------------- 2 files changed, 38 insertions(+), 18 deletions(-) diff --git a/jsonrpc/dispatcher.go b/jsonrpc/dispatcher.go index ceb28c21ee..10c502f974 100644 --- a/jsonrpc/dispatcher.go +++ b/jsonrpc/dispatcher.go @@ -97,14 +97,20 @@ func newDispatcher( } func (d *Dispatcher) registerEndpoints(store JSONRPCStore) error { - d.endpoints.Eth = &Eth{ + var err error + + d.endpoints.Eth, err = NewEth( d.logger, store, - d.params.chainID, d.filterManager, - d.params.priceLimit, d.params.secretsManager, + d.params.chainID, + d.params.priceLimit, + ) + if err != nil { + return err } + d.endpoints.Net = &Net{ store, d.params.chainID, @@ -120,8 +126,6 @@ func (d *Dispatcher) registerEndpoints(store JSONRPCStore) error { } d.endpoints.Debug = NewDebug(store, d.params.concurrentRequestsDebug) - var err error - if err = d.registerService("eth", d.endpoints.Eth); err != nil { return err } diff --git a/jsonrpc/eth_endpoint.go b/jsonrpc/eth_endpoint.go index 71b5ba8f49..1859123f77 100644 --- a/jsonrpc/eth_endpoint.go +++ b/jsonrpc/eth_endpoint.go @@ -95,12 +95,34 @@ type ethStore interface { // Eth is the eth jsonrpc endpoint type Eth struct { - logger hclog.Logger - store ethStore - chainID uint64 - filterManager *FilterManager - priceLimit uint64 - secretsManager secrets.SecretsManager + logger hclog.Logger + store ethStore + chainID uint64 + filterManager *FilterManager + priceLimit uint64 + account *wallet.Account +} + +func NewEth( + logger hclog.Logger, + store ethStore, + filterManager *FilterManager, + secretsManager secrets.SecretsManager, + chainID uint64, + priceLimit uint64) (*Eth, error) { + account, err := wallet.NewAccountFromSecret(secretsManager) + if err != nil { + return nil, fmt.Errorf("failed to read account data: %w", err) + } + + return &Eth{ + store: store, + logger: logger, + chainID: chainID, + account: account, + priceLimit: priceLimit, + filterManager: filterManager, + }, nil } var ( @@ -115,13 +137,7 @@ func (e *Eth) ChainId() (interface{}, error) { } func (e *Eth) Accounts() (interface{}, error) { - // read account - account, err := wallet.NewAccountFromSecret(e.secretsManager) - if err != nil { - return nil, fmt.Errorf("failed to read account data: %w", err) - } - - return []types.Address{types.Address(wallet.NewKey(account).Address())}, nil + return []types.Address{types.Address(wallet.NewKey(e.account).Address())}, nil } func (e *Eth) Syncing() (interface{}, error) {