Skip to content

Commit

Permalink
manager integration
Browse files Browse the repository at this point in the history
  • Loading branch information
dusannosovic-ethernal committed May 28, 2024
1 parent ba4e9cb commit 146ad7a
Show file tree
Hide file tree
Showing 15 changed files with 42 additions and 26 deletions.
2 changes: 1 addition & 1 deletion accounts/event/feed_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func checkPanic(want error, fn func()) (err error) {
if panicErr == nil {
err = errors.New("didn't panic")
} else if !reflect.DeepEqual(panicErr, want) {
err = fmt.Errorf("panicked with wrong error: got %w, want %w", panicErr, want)
err = fmt.Errorf("panicked with wrong error: got %v, want %w", panicErr, want)
}
}()

Expand Down
2 changes: 1 addition & 1 deletion accounts/keystore/account_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ func TestUpdatedKeyfileContents(t *testing.T) {
t.Parallel()

// Create a temporary keystore to test with
dir, err := filepath.Abs(filepath.Join(fmt.Sprintf("eth-keystore-updatedkeyfilecontents-test-%d-%d", os.Getpid(), rand.Int())))
dir, err := filepath.Abs(filepath.Join(fmt.Sprintf("eth-keystore-updatedkeyfilecontents-test-%d-%d", os.Getpid(), rand.Int()))) //nolint:gocritic
require.NoError(t, err)

ks := NewKeyStore(dir, LightScryptN, LightScryptP, hclog.NewNullLogger())
Expand Down
6 changes: 3 additions & 3 deletions accounts/keystore/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ type cipherparamsJSON struct {

// TO DO marshall private key
func (k *Key) MarshalJSON() (j []byte, err error) {
privKey, err := crypto.MarshalECDSAPrivateKey(k.PrivateKey) //get more time for this
privKey, err := crypto.MarshalECDSAPrivateKey(k.PrivateKey) // get more time for this
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -135,7 +135,7 @@ func newKeyFromECDSA(privateKeyECDSA *ecdsa.PrivateKey) *Key {

key := &Key{
ID: id,
Address: crypto.PubKeyToAddress(&privateKeyECDSA.PublicKey), //TO DO get more time for this pointer
Address: crypto.PubKeyToAddress(&privateKeyECDSA.PublicKey), // TO DO get more time for this pointer
PrivateKey: privateKeyECDSA,
}

Expand Down Expand Up @@ -192,7 +192,7 @@ func writeTemporaryKeyFile(file string, content []byte) (string, error) {
}

func newKey(rand io.Reader) (*Key, error) {
privateKeyECDSA, err := crypto.GenerateECDSAPrivateKey() //TO DO maybe not valid
privateKeyECDSA, err := crypto.GenerateECDSAPrivateKey() // TO DO maybe not valid
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion accounts/keystore/keystore.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ var (
// already present in the keystore.
ErrAccountAlreadyExists = errors.New("account already exists")

DefaultStorage, _ = filepath.Abs(filepath.Join("data-storage"))
DefaultStorage, _ = filepath.Abs(filepath.Join("data-storage")) //nolint:gocritic
)

var KeyStoreType = reflect.TypeOf(&KeyStore{})
Expand Down
6 changes: 3 additions & 3 deletions accounts/keystore/plain.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ func (ks keyStorePlain) GetKey(addr types.Address, filename, auth string) (*Key,
return nil, err
}

key.Address = types.StringToAddress(dat["address"].(string))
key.Address = types.StringToAddress(dat["address"].(string)) //nolint:forcetypeassert

key.ID = uuid.MustParse(dat["id"].(string))
key.ID = uuid.MustParse(dat["id"].(string)) //nolint:forcetypeassert

key.PrivateKey, err = crypto.BytesToECDSAPrivateKey([]byte(dat["privatekey"].(string)))
key.PrivateKey, err = crypto.BytesToECDSAPrivateKey([]byte(dat["privatekey"].(string))) //nolint:forcetypeassert
if err != nil {
return nil, err
}
Expand Down
5 changes: 4 additions & 1 deletion accounts/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/0xPolygon/polygon-edge/accounts/event"
"github.com/0xPolygon/polygon-edge/types"
"github.com/hashicorp/go-hclog"
)

const managerSubBufferSize = 50
Expand All @@ -33,11 +34,13 @@ type Manager struct {

quit chan chan error

logger hclog.Logger

term chan struct{}
lock sync.RWMutex
}

func NewManager(config *Config, backends ...Backend) *Manager {
func NewManager(config *Config, logger hclog.Logger, backends ...Backend) *Manager {
var wallets []Wallet

for _, backend := range backends {
Expand Down
4 changes: 2 additions & 2 deletions command/accounts/import/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func runCommand(cmd *cobra.Command, _ []string) {
scryptP = keystore.LightScryptP
}

am := accounts.NewManager(&accounts.Config{})
am := accounts.NewManager(&accounts.Config{}, nil)

am.AddBackend(keystore.NewKeyStore(params.KeyDir, scryptN, scryptP, nil))

Expand All @@ -99,7 +99,7 @@ func runCommand(cmd *cobra.Command, _ []string) {
outputter.SetError(fmt.Errorf("keystore is not available"))
}

ks := backends[0].(*keystore.KeyStore)
ks := backends[0].(*keystore.KeyStore) //nolint:forcetypeassert

acct, err := ks.ImportECDSA(privKey, params.Passphrase)
if err != nil {
Expand Down
9 changes: 4 additions & 5 deletions crypto/crypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -370,13 +370,13 @@ func LatestSignerForChainID(chaidID uint64) TxSigner {
return NewLondonSigner(chaidID)

Check failure on line 370 in crypto/crypto.go

View workflow job for this annotation

GitHub Actions / Lint / Run Lint

return with no blank line before (nlreturn)
}

func DToECDSA(D []byte, strict bool) (*ecdsa.PrivateKey, error) {
func DToECDSA(d []byte, strict bool) (*ecdsa.PrivateKey, error) {
priv := new(ecdsa.PrivateKey)
priv.PublicKey.Curve = btcec.S256()
if strict && 8*len(D) != priv.Params().BitSize {
if strict && 8*len(d) != priv.Params().BitSize {
return nil, fmt.Errorf("invalid length, need %d bits", priv.Params().BitSize)
}
priv.D = new(big.Int).SetBytes(D)
priv.D = new(big.Int).SetBytes(d)

if priv.D.Cmp(secp256k1N) >= 0 {
return nil, errors.New("invalid private key, >= N")
Expand All @@ -386,11 +386,10 @@ func DToECDSA(D []byte, strict bool) (*ecdsa.PrivateKey, error) {
return nil, errors.New("invalid private key, zero or negative")
}

priv.PublicKey.X, priv.PublicKey.Y = btcec.S256().ScalarBaseMult(D)
priv.PublicKey.X, priv.PublicKey.Y = btcec.S256().ScalarBaseMult(d)
if priv.PublicKey.X == nil {
return nil, errors.New("invalid private key")
}

return priv, nil

}
8 changes: 5 additions & 3 deletions jsonrpc/dispatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"time"
"unicode"

"github.com/0xPolygon/polygon-edge/accounts"
"github.com/armon/go-metrics"
"github.com/hashicorp/go-hclog"
jsonIter "github.com/json-iterator/go"
Expand Down Expand Up @@ -75,7 +76,7 @@ func (dp dispatcherParams) isExceedingBatchLengthLimit(value uint64) bool {
func newDispatcher(
logger hclog.Logger,
store JSONRPCStore,
params *dispatcherParams,
params *dispatcherParams, manager *accounts.Manager,
) (*Dispatcher, error) {
d := &Dispatcher{
logger: logger.Named("dispatcher"),
Expand All @@ -87,20 +88,21 @@ func newDispatcher(
go d.filterManager.Run()
}

if err := d.registerEndpoints(store); err != nil {
if err := d.registerEndpoints(store, manager); err != nil {
return nil, err
}

return d, nil
}

func (d *Dispatcher) registerEndpoints(store JSONRPCStore) error {
func (d *Dispatcher) registerEndpoints(store JSONRPCStore, manager *accounts.Manager) error {
d.endpoints.Eth = &Eth{
d.logger,
store,
d.params.chainID,
d.filterManager,
d.params.priceLimit,
manager,
}
d.endpoints.Net = &Net{
store,
Expand Down
2 changes: 1 addition & 1 deletion jsonrpc/dispatcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,7 @@ func TestDispatcher_WebsocketConnection_Unsubscribe(t *testing.T) {
func newTestDispatcher(tb testing.TB, logger hclog.Logger, store JSONRPCStore, params *dispatcherParams) *Dispatcher {
tb.Helper()

d, err := newDispatcher(logger, store, params)
d, err := newDispatcher(logger, store, params, nil)
require.NoError(tb, err)

return d
Expand Down
2 changes: 2 additions & 0 deletions jsonrpc/eth_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/hashicorp/go-hclog"

"github.com/0xPolygon/polygon-edge/accounts"
"github.com/0xPolygon/polygon-edge/chain"
"github.com/0xPolygon/polygon-edge/gasprice"
"github.com/0xPolygon/polygon-edge/helper/common"
Expand Down Expand Up @@ -101,6 +102,7 @@ type Eth struct {
chainID uint64
filterManager *FilterManager
priceLimit uint64
accManager *accounts.Manager
}

// ChainId returns the chain id of the client
Expand Down
4 changes: 2 additions & 2 deletions jsonrpc/eth_endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,13 +288,13 @@ func TestEth_TxnType(t *testing.T) {

func newTestEthEndpoint(store testStore) *Eth {
return &Eth{
hclog.NewNullLogger(), store, 100, nil, 0,
hclog.NewNullLogger(), store, 100, nil, 0, nil,
}
}

func newTestEthEndpointWithPriceLimit(store testStore, priceLimit uint64) *Eth {
return &Eth{
hclog.NewNullLogger(), store, 100, nil, priceLimit,
hclog.NewNullLogger(), store, 100, nil, priceLimit, nil,
}
}

Expand Down
4 changes: 3 additions & 1 deletion jsonrpc/jsonrpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"sync"
"time"

"github.com/0xPolygon/polygon-edge/accounts"
"github.com/0xPolygon/polygon-edge/secrets"
"github.com/0xPolygon/polygon-edge/versioning"
"github.com/gorilla/websocket"
Expand Down Expand Up @@ -59,7 +60,7 @@ type Config struct {
}

// NewJSONRPC returns the JSONRPC http server
func NewJSONRPC(logger hclog.Logger, config *Config) (*JSONRPC, error) {
func NewJSONRPC(logger hclog.Logger, config *Config, manager *accounts.Manager) (*JSONRPC, error) {
d, err := newDispatcher(
logger,
config.Store,
Expand All @@ -71,6 +72,7 @@ func NewJSONRPC(logger hclog.Logger, config *Config) (*JSONRPC, error) {
blockRangeLimit: config.BlockRangeLimit,
concurrentRequestsDebug: config.ConcurrentRequestsDebug,
},
manager,
)

if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion jsonrpc/jsonrpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,5 +111,5 @@ func newTestJSONRPC(t *testing.T) (*JSONRPC, error) {
Addr: &net.TCPAddr{IP: net.ParseIP("127.0.0.1"), Port: port},
}

return NewJSONRPC(hclog.NewNullLogger(), config)
return NewJSONRPC(hclog.NewNullLogger(), config, nil)
}
10 changes: 9 additions & 1 deletion server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/prometheus/client_golang/prometheus/promhttp"
"google.golang.org/grpc"

"github.com/0xPolygon/polygon-edge/accounts"
"github.com/0xPolygon/polygon-edge/archive"
"github.com/0xPolygon/polygon-edge/blockchain"
"github.com/0xPolygon/polygon-edge/blockchain/storagev2"
Expand Down Expand Up @@ -76,6 +77,8 @@ type Server struct {
// libp2p network
network *network.Server

accManager *accounts.Manager

// transaction pool
txpool *txpool.TxPool

Expand Down Expand Up @@ -207,6 +210,11 @@ func NewServer(config *Config) (*Server, error) {
m.network = network
}

// setup account manager
{
m.accManager = accounts.NewManager(&accounts.Config{InsecureUnlockAllowed: true}, nil)
}

// start blockchain object
stateStorage, err := itrie.NewLevelDBStorage(filepath.Join(m.config.DataDir, "trie"), logger)
if err != nil {
Expand Down Expand Up @@ -899,7 +907,7 @@ func (s *Server) setupJSONRPC() error {
SecretsManager: s.secretsManager,
}

srv, err := jsonrpc.NewJSONRPC(s.logger, conf)
srv, err := jsonrpc.NewJSONRPC(s.logger, conf, s.accManager)
if err != nil {
return err
}
Expand Down

0 comments on commit 146ad7a

Please sign in to comment.