Skip to content

Commit

Permalink
🎨 fix lints from staticcheck
Browse files Browse the repository at this point in the history
  • Loading branch information
cairoeth committed Mar 24, 2024
1 parent 4a22ae3 commit d4c7873
Show file tree
Hide file tree
Showing 42 changed files with 168 additions and 348 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ lint:
gofmt -d -s .
gofumpt -d -extra .
go vet ./...
staticcheck ./...
go list ./... | grep -F -e contracts/ -v | xargs staticcheck
golangci-lint run

fmt:
Expand Down
4 changes: 2 additions & 2 deletions preconf-operator/cmd/operator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ func main() {
}
}

func operatorMain(ctx_cli *cli.Context) error {
configPath := ctx_cli.GlobalString(config.ConfigFileFlag.Name)
func operatorMain(ctxCli *cli.Context) error {
configPath := ctxCli.GlobalString(config.ConfigFileFlag.Name)
nodeConfig := types.NodeConfig{}
err := sdkutils.ReadYamlConfig(configPath, &nodeConfig)
if err != nil {
Expand Down
9 changes: 5 additions & 4 deletions preconf-operator/core/chainio/avs_reader.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Package chainio contains the logic to interact with the AVS contracts onchain.
package chainio

import (
Expand Down Expand Up @@ -28,15 +29,15 @@ type AvsReader struct {
var _ AvsReaderer = (*AvsReader)(nil)

func BuildAvsReaderFromConfig(c *config.Config) (*AvsReader, error) {
return BuildAvsReader(c.IncredibleSquaringRegistryCoordinatorAddr, c.OperatorStateRetrieverAddr, c.EthHttpClient, c.Logger)
return BuildAvsReader(c.IncredibleSquaringRegistryCoordinatorAddr, c.OperatorStateRetrieverAddr, c.EthHTTPClient, c.Logger)
}

func BuildAvsReader(registryCoordinatorAddr, operatorStateRetrieverAddr gethcommon.Address, ethHttpClient eth.EthClient, logger logging.Logger) (*AvsReader, error) {
avsManagersBindings, err := NewAvsManagersBindings(registryCoordinatorAddr, operatorStateRetrieverAddr, ethHttpClient, logger)
func BuildAvsReader(registryCoordinatorAddr, operatorStateRetrieverAddr gethcommon.Address, ethHTTPClient eth.EthClient, logger logging.Logger) (*AvsReader, error) {
avsManagersBindings, err := NewAvsManagersBindings(registryCoordinatorAddr, operatorStateRetrieverAddr, ethHTTPClient, logger)
if err != nil {
return nil, err
}
avsRegistryReader, err := sdkavsregistry.BuildAvsRegistryChainReader(registryCoordinatorAddr, operatorStateRetrieverAddr, ethHttpClient, logger)
avsRegistryReader, err := sdkavsregistry.BuildAvsRegistryChainReader(registryCoordinatorAddr, operatorStateRetrieverAddr, ethHTTPClient, logger)
if err != nil {
return nil, err
}
Expand Down
3 changes: 1 addition & 2 deletions preconf-operator/core/chainio/avs_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,12 @@ type AvsWriter struct {
AvsContractBindings *AvsManagersBindings
logger logging.Logger
TxMgr txmgr.TxManager
client eth.EthClient
}

var _ AvsWriterer = (*AvsWriter)(nil)

func BuildAvsWriterFromConfig(c *config.Config) (*AvsWriter, error) {
return BuildAvsWriter(c.TxMgr, c.IncredibleSquaringRegistryCoordinatorAddr, c.OperatorStateRetrieverAddr, c.EthHttpClient, c.Logger)
return BuildAvsWriter(c.TxMgr, c.IncredibleSquaringRegistryCoordinatorAddr, c.OperatorStateRetrieverAddr, c.EthHTTPClient, c.Logger)
}

func BuildAvsWriter(txMgr txmgr.TxManager, registryCoordinatorAddr, operatorStateRetrieverAddr gethcommon.Address, ethHTTPClient eth.EthClient, logger logging.Logger) (*AvsWriter, error) {
Expand Down
33 changes: 17 additions & 16 deletions preconf-operator/core/config/config.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Package config contains the config types.
package config

import (
Expand Down Expand Up @@ -25,30 +26,30 @@ type Config struct {
EcdsaPrivateKey *ecdsa.PrivateKey
BlsPrivateKey *bls.PrivateKey
Logger sdklogging.Logger
EigenMetricsIpPortAddress string
EigenMetricsIPPortAddress string
// we need the url for the eigensdk currently... eventually standardize api so as to
// only take an ethclient or an rpcUrl (and build the ethclient at each constructor site)
EthHttpRpcUrl string
EthHttpClient eth.EthClient
EthHTTPRPCURL string
EthHTTPClient eth.EthClient
OperatorStateRetrieverAddr common.Address
IncredibleSquaringRegistryCoordinatorAddr common.Address
AggregatorServerIpPortAddr string
AggregatorServerIPPortAddr string
RegisterOperatorOnStartup bool
// json:"-" skips this field when marshaling (only used for logging to stdout), since SignerFn doesnt implement marshalJson
SignerFn signerv2.SignerFn `json:"-"`
TxMgr txmgr.TxManager
AggregatorAddress common.Address
}

// These are read from ConfigFileFlag
// ConfigRaw These are read from ConfigFileFlag
type ConfigRaw struct {
Environment sdklogging.LogLevel `yaml:"environment"`
EthRpcUrl string `yaml:"eth_rpc_url"`
AggregatorServerIpPortAddr string `yaml:"aggregator_server_ip_port_address"`
EthRPCURL string `yaml:"eth_rpc_url"`
AggregatorServerIPPortAddr string `yaml:"aggregator_server_ip_port_address"`
RegisterOperatorOnStartup bool `yaml:"register_operator_on_startup"`
}

// These are read from CredibleSquaringDeploymentFileFlag
// IncredibleSquaringDeploymentRaw These are read from CredibleSquaringDeploymentFileFlag
type IncredibleSquaringDeploymentRaw struct {
Addresses IncredibleSquaringContractsRaw `json:"addresses"`
}
Expand Down Expand Up @@ -79,7 +80,7 @@ func NewConfig(ctx *cli.Context) (*Config, error) {
return nil, err
}

ethRpcClient, err := eth.NewClient(configRaw.EthRpcUrl)
ethRPCClient, err := eth.NewClient(configRaw.EthRPCURL)
if err != nil {
logger.Errorf("Cannot create http ethclient", "err", err)
return nil, err
Expand All @@ -101,26 +102,26 @@ func NewConfig(ctx *cli.Context) (*Config, error) {
return nil, err
}

chainId, err := ethRpcClient.ChainID(context.Background())
chainID, err := ethRPCClient.ChainID(context.Background())
if err != nil {
logger.Error("Cannot get chainId", "err", err)
logger.Error("Cannot get chainID", "err", err)
return nil, err
}

signerV2, _, err := signerv2.SignerFromConfig(signerv2.Config{PrivateKey: ecdsaPrivateKey}, chainId)
signerV2, _, err := signerv2.SignerFromConfig(signerv2.Config{PrivateKey: ecdsaPrivateKey}, chainID)
if err != nil {
panic(err)
}
txMgr := txmgr.NewSimpleTxManager(ethRpcClient, logger, signerV2, aggregatorAddr)
txMgr := txmgr.NewSimpleTxManager(ethRPCClient, logger, signerV2, aggregatorAddr)

config := &Config{
EcdsaPrivateKey: ecdsaPrivateKey,
Logger: logger,
EthHttpRpcUrl: configRaw.EthRpcUrl,
EthHttpClient: ethRpcClient,
EthHTTPRPCURL: configRaw.EthRPCURL,
EthHTTPClient: ethRPCClient,
OperatorStateRetrieverAddr: common.HexToAddress(credibleSquaringDeploymentRaw.Addresses.OperatorStateRetrieverAddr),
IncredibleSquaringRegistryCoordinatorAddr: common.HexToAddress(credibleSquaringDeploymentRaw.Addresses.RegistryCoordinatorAddr),
AggregatorServerIpPortAddr: configRaw.AggregatorServerIpPortAddr,
AggregatorServerIPPortAddr: configRaw.AggregatorServerIPPortAddr,
RegisterOperatorOnStartup: configRaw.RegisterOperatorOnStartup,
SignerFn: signerV2,
TxMgr: txMgr,
Expand Down
1 change: 0 additions & 1 deletion preconf-operator/core/utils.go

This file was deleted.

96 changes: 47 additions & 49 deletions preconf-operator/operator.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Package operator AVS operator logic.
package operator

import (
Expand All @@ -22,7 +23,6 @@ import (
"github.com/Layr-Labs/eigensdk-go/crypto/bls"
sdkecdsa "github.com/Layr-Labs/eigensdk-go/crypto/ecdsa"
"github.com/Layr-Labs/eigensdk-go/logging"
sdklogging "github.com/Layr-Labs/eigensdk-go/logging"
sdkmetrics "github.com/Layr-Labs/eigensdk-go/metrics"
"github.com/Layr-Labs/eigensdk-go/metrics/collectors/economic"
rpccalls "github.com/Layr-Labs/eigensdk-go/metrics/collectors/rpc_calls"
Expand All @@ -36,8 +36,8 @@ import (
)

const (
AVS_NAME = "preconfirmations"
SEM_VER = "0.0.1"
AvsName = "preconfirmations"
SemVer = "0.0.1"
)

type Operator struct {
Expand All @@ -51,62 +51,60 @@ type Operator struct {
// writing to the chain should be done via the cli only
metricsReg *prometheus.Registry
metrics metrics.Metrics
nodeApi *nodeapi.NodeApi
receiveApi *receiverapi.ReceiveApi
nodeAPI *nodeapi.NodeApi
receiveAPI *receiverapi.ReceiveAPI
avsWriter *chainio.AvsWriter
avsReader chainio.AvsReaderer
eigenlayerReader sdkelcontracts.ELReader
eigenlayerWriter sdkelcontracts.ELWriter
blsKeypair *bls.KeyPair
operatorId bls.OperatorId
operatorID bls.OperatorId
operatorAddr common.Address
// ip address of aggregator
aggregatorServerIpPortAddr string
aggregatorServerIPPortAddr string
// rpc client to send signed task responses to aggregator
aggregatorRpcClient AggregatorRpcClienter
aggregatorRPCClient AggregatorRPCClienter
// needed when opting in to avs (allow this service manager contract to slash operator)
credibleSquaringServiceManagerAddr common.Address
}

// TODO(samlaf): config is a mess right now, since the chainio client constructors
//
// take the config in core (which is shared with aggregator and challenger)
// NewOperatorFromConfig the config in core (which is shared with aggregator and challenger)
func NewOperatorFromConfig(c types.NodeConfig) (*Operator, error) {
var logLevel logging.LogLevel
if c.Production {
logLevel = sdklogging.Production
logLevel = logging.Production
} else {
logLevel = sdklogging.Development
logLevel = logging.Development
}
logger, err := sdklogging.NewZapLogger(logLevel)
logger, err := logging.NewZapLogger(logLevel)
if err != nil {
return nil, err
}
reg := prometheus.NewRegistry()
eigenMetrics := sdkmetrics.NewEigenMetrics(AVS_NAME, c.EigenMetricsIpPortAddress, reg, logger)
avsAndEigenMetrics := metrics.NewAvsAndEigenMetrics(AVS_NAME, eigenMetrics, reg)
eigenMetrics := sdkmetrics.NewEigenMetrics(AvsName, c.EigenMetricsIPPortAddress, reg, logger)
avsAndEigenMetrics := metrics.NewAvsAndEigenMetrics(AvsName, eigenMetrics, reg)

// Setup Node Api
nodeApi := nodeapi.NewNodeApi(AVS_NAME, SEM_VER, c.NodeApiIpPortAddress, logger)
nodeAPI := nodeapi.NewNodeApi(AvsName, SemVer, c.NodeAPIIPPortAddress, logger)

var ethRpcClient eth.EthClient
var ethRPCClient eth.EthClient
if c.EnableMetrics {
rpcCallsCollector := rpccalls.NewCollector(AVS_NAME, reg)
ethRpcClient, err = eth.NewInstrumentedClient(c.EthRpcUrl, rpcCallsCollector)
rpcCallsCollector := rpccalls.NewCollector(AvsName, reg)
ethRPCClient, err = eth.NewInstrumentedClient(c.EthRPCURL, rpcCallsCollector)
if err != nil {
logger.Errorf("Cannot create http ethclient", "err", err)
return nil, err
}
} else {
ethRpcClient, err = eth.NewClient(c.EthRpcUrl)
ethRPCClient, err = eth.NewClient(c.EthRPCURL)
if err != nil {
logger.Errorf("Cannot create http ethclient", "err", err)
return nil, err
}
}

// Setup Receive Api
receiveApi := receiverapi.NewReceiveApi("localhost:8000", logger, ethRpcClient)
receiveAPI := receiverapi.NewReceiveAPI("localhost:8000", logger, ethRPCClient)

blsKeyPassword, ok := os.LookupEnv("OPERATOR_BLS_KEY_PASSWORD")
if !ok {
Expand All @@ -117,12 +115,12 @@ func NewOperatorFromConfig(c types.NodeConfig) (*Operator, error) {
logger.Errorf("Cannot parse bls private key", "err", err)
return nil, err
}
// TODO(samlaf): should we add the chainId to the config instead?
// TODO(samlaf): should we add the chainID to the config instead?
// this way we can prevent creating a signer that signs on mainnet by mistake
// if the config says chainId=5, then we can only create a goerli signer
chainId, err := ethRpcClient.ChainID(context.Background())
// if the config says chainID=5, then we can only create a goerli signer
chainID, err := ethRPCClient.ChainID(context.Background())
if err != nil {
logger.Error("Cannot get chainId", "err", err)
logger.Error("Cannot get chainID", "err", err)
return nil, err
}

Expand All @@ -134,29 +132,29 @@ func NewOperatorFromConfig(c types.NodeConfig) (*Operator, error) {
signerV2, _, err := signerv2.SignerFromConfig(signerv2.Config{
KeystorePath: c.EcdsaPrivateKeyStorePath,
Password: ecdsaKeyPassword,
}, chainId)
}, chainID)
if err != nil {
panic(err)
}
chainioConfig := clients.BuildAllConfig{
EthHttpUrl: c.EthRpcUrl,
EthWsUrl: c.EthRpcUrl,
EthHttpUrl: c.EthRPCURL,
EthWsUrl: c.EthRPCURL,
RegistryCoordinatorAddr: c.AVSRegistryCoordinatorAddress,
OperatorStateRetrieverAddr: c.OperatorStateRetrieverAddress,
AvsName: AVS_NAME,
PromMetricsIpPortAddress: c.EigenMetricsIpPortAddress,
AvsName: AvsName,
PromMetricsIpPortAddress: c.EigenMetricsIPPortAddress,
}

sdkClients, err := clients.BuildAll(chainioConfig, common.HexToAddress(c.OperatorAddress), signerV2, logger)
if err != nil {
panic(err)
}

txMgr := txmgr.NewSimpleTxManager(ethRpcClient, logger, signerV2, common.HexToAddress(c.OperatorAddress))
txMgr := txmgr.NewSimpleTxManager(ethRPCClient, logger, signerV2, common.HexToAddress(c.OperatorAddress))

avsWriter, err := chainio.BuildAvsWriter(
txMgr, common.HexToAddress(c.AVSRegistryCoordinatorAddress),
common.HexToAddress(c.OperatorStateRetrieverAddress), ethRpcClient, logger,
common.HexToAddress(c.OperatorStateRetrieverAddress), ethRPCClient, logger,
)
if err != nil {
logger.Error("Cannot create AvsWriter", "err", err)
Expand All @@ -166,7 +164,7 @@ func NewOperatorFromConfig(c types.NodeConfig) (*Operator, error) {
avsReader, err := chainio.BuildAvsReader(
common.HexToAddress(c.AVSRegistryCoordinatorAddress),
common.HexToAddress(c.OperatorStateRetrieverAddress),
ethRpcClient, logger)
ethRPCClient, logger)
if err != nil {
logger.Error("Cannot create AvsReader", "err", err)
return nil, err
Expand All @@ -179,12 +177,12 @@ func NewOperatorFromConfig(c types.NodeConfig) (*Operator, error) {
}
economicMetricsCollector := economic.NewCollector(
sdkClients.ElChainReader, sdkClients.AvsRegistryChainReader,
AVS_NAME, logger, common.HexToAddress(c.OperatorAddress), quorumNames)
AvsName, logger, common.HexToAddress(c.OperatorAddress), quorumNames)
reg.MustRegister(economicMetricsCollector)

aggregatorRpcClient, err := NewAggregatorRpcClient(c.AggregatorServerIpPortAddress, logger, avsAndEigenMetrics)
aggregatorRPCClient, err := NewAggregatorRPCClient(c.AggregatorServerIPPortAddress, logger, avsAndEigenMetrics)
if err != nil {
logger.Error("Cannot create AggregatorRpcClient. Is aggregator running?", "err", err)
logger.Error("Cannot create AggregatorRPCClient. Is aggregator running?", "err", err)
return nil, err
}

Expand All @@ -193,19 +191,19 @@ func NewOperatorFromConfig(c types.NodeConfig) (*Operator, error) {
logger: logger,
metricsReg: reg,
metrics: avsAndEigenMetrics,
nodeApi: nodeApi,
receiveApi: receiveApi,
ethClient: ethRpcClient,
nodeAPI: nodeAPI,
receiveAPI: receiveAPI,
ethClient: ethRPCClient,
avsWriter: avsWriter,
avsReader: avsReader,
eigenlayerReader: sdkClients.ElChainReader,
eigenlayerWriter: sdkClients.ElChainWriter,
blsKeypair: blsKeyPair,
operatorAddr: common.HexToAddress(c.OperatorAddress),
aggregatorServerIpPortAddr: c.AggregatorServerIpPortAddress,
aggregatorRpcClient: aggregatorRpcClient,
aggregatorServerIPPortAddr: c.AggregatorServerIPPortAddress,
aggregatorRPCClient: aggregatorRPCClient,
credibleSquaringServiceManagerAddr: common.HexToAddress(c.AVSRegistryCoordinatorAddress),
operatorId: [32]byte{0}, // this is set below
operatorID: [32]byte{0}, // this is set below

}

Expand All @@ -221,14 +219,14 @@ func NewOperatorFromConfig(c types.NodeConfig) (*Operator, error) {
}

// OperatorId is set in contract during registration so we get it after registering operator.
operatorId, err := sdkClients.AvsRegistryChainReader.GetOperatorId(&bind.CallOpts{}, operator.operatorAddr)
operatorID, err := sdkClients.AvsRegistryChainReader.GetOperatorId(&bind.CallOpts{}, operator.operatorAddr)
if err != nil {
logger.Error("Cannot get operator id", "err", err)
return nil, err
}
operator.operatorId = operatorId
operator.operatorID = operatorID
logger.Info("Operator info",
"operatorId", operatorId,
"operatorID", operatorID,
"operatorAddr", c.OperatorAddress,
"operatorG1Pubkey", operator.blsKeypair.GetPubKeyG1(),
"operatorG2Pubkey", operator.blsKeypair.GetPubKeyG2(),
Expand All @@ -251,11 +249,11 @@ func (o *Operator) Start(ctx context.Context) error {

o.logger.Infof("Starting operator.")

if o.config.EnableNodeApi {
o.nodeApi.Start()
if o.config.EnableNodeAPI {
o.nodeAPI.Start()
}

o.receiveApi.Start()
o.receiveAPI.Start()

var metricsErrChan <-chan error
if o.config.EnableMetrics {
Expand Down
Loading

0 comments on commit d4c7873

Please sign in to comment.