Skip to content

Commit

Permalink
Remove unnecessary data from config
Browse files Browse the repository at this point in the history
  • Loading branch information
taco-paco committed Feb 29, 2024
1 parent cf0182b commit f591083
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 129 deletions.
63 changes: 42 additions & 21 deletions aggregator/aggregator.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package aggregator

import (
"context"
"github.com/Layr-Labs/eigensdk-go/chainio/clients/eth"
"github.com/Layr-Labs/eigensdk-go/chainio/txmgr"
"github.com/Layr-Labs/eigensdk-go/signerv2"
"sync"
"time"

Expand Down Expand Up @@ -89,49 +92,67 @@ type Aggregator struct {
// NewAggregator creates a new Aggregator with the provided config.
// TODO: Remove this context once OperatorPubkeysServiceInMemory's API is
// changed and we can gracefully exit otherwise
func NewAggregator(ctx context.Context, c *config.Config) (*Aggregator, error) {
avsReader, err := chainio.BuildAvsReaderFromConfig(c)
func NewAggregator(config *config.Config, logger logging.Logger, ctx context.Context) (*Aggregator, error) {
ethHttpClient, err := eth.NewClient(config.EthHttpRpcUrl)
if err != nil {
c.Logger.Error("Cannot create avsReader", "err", err)
logger.Errorf("Cannot create http ethclient", "err", err)
return nil, err
}

avsWriter, err := chainio.BuildAvsWriterFromConfig(c)
avsReader, err := chainio.BuildAvsReader(config.SFFLRegistryCoordinatorAddr, config.OperatorStateRetrieverAddr, ethHttpClient, logger)
if err != nil {
c.Logger.Errorf("Cannot create avsWriter", "err", err)
logger.Error("Cannot create avsReader", "err", err)
return nil, err
}

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

signerV2, _, err := signerv2.SignerFromConfig(signerv2.Config{PrivateKey: config.EcdsaPrivateKey}, chainId)
if err != nil {
panic(err)
}
txMgr := txmgr.NewSimpleTxManager(ethHttpClient, logger, signerV2, config.AggregatorAddress)

avsWriter, err := chainio.BuildAvsWriter(txMgr, config.SFFLRegistryCoordinatorAddr, config.OperatorStateRetrieverAddr, ethHttpClient, logger)
if err != nil {
logger.Errorf("Cannot create avsWriter", "err", err)
return nil, err
}

chainioConfig := sdkclients.BuildAllConfig{
EthHttpUrl: c.EthHttpRpcUrl,
EthWsUrl: c.EthWsRpcUrl,
RegistryCoordinatorAddr: c.SFFLRegistryCoordinatorAddr.String(),
OperatorStateRetrieverAddr: c.OperatorStateRetrieverAddr.String(),
EthHttpUrl: config.EthHttpRpcUrl,
EthWsUrl: config.EthWsRpcUrl,
RegistryCoordinatorAddr: config.SFFLRegistryCoordinatorAddr.String(),
OperatorStateRetrieverAddr: config.OperatorStateRetrieverAddr.String(),
AvsName: avsName,
PromMetricsIpPortAddress: ":9090",
}
clients, err := clients.BuildAll(chainioConfig, c.AggregatorAddress, c.SignerFn, c.Logger)
clients, err := clients.BuildAll(chainioConfig, config.AggregatorAddress, signerV2, logger)
if err != nil {
c.Logger.Errorf("Cannot create sdk clients", "err", err)
logger.Errorf("Cannot create sdk clients", "err", err)
return nil, err
}

msgDb, err := NewMessageDatabase(c.AggregatorDatabasePath)
msgDb, err := NewMessageDatabase(config.AggregatorDatabasePath)
if err != nil {
c.Logger.Errorf("Cannot create database", "err", err)
logger.Errorf("Cannot create database", "err", err)
return nil, err
}

operatorPubkeysService := oppubkeysserv.NewOperatorPubkeysServiceInMemory(ctx, clients.AvsRegistryChainSubscriber, clients.AvsRegistryChainReader, c.Logger)
avsRegistryService := avsregistry.NewAvsRegistryServiceChainCaller(avsReader, operatorPubkeysService, c.Logger)
taskBlsAggregationService := blsagg.NewBlsAggregatorService(avsRegistryService, c.Logger)
stateRootUpdateBlsAggregationService := NewMessageBlsAggregatorService(avsRegistryService, clients.EthHttpClient, c.Logger)
operatorSetUpdateBlsAggregationService := NewMessageBlsAggregatorService(avsRegistryService, clients.EthHttpClient, c.Logger)
operatorPubkeysService := oppubkeysserv.NewOperatorPubkeysServiceInMemory(ctx, clients.AvsRegistryChainSubscriber, clients.AvsRegistryChainReader, logger)
avsRegistryService := avsregistry.NewAvsRegistryServiceChainCaller(avsReader, operatorPubkeysService, logger)
taskBlsAggregationService := blsagg.NewBlsAggregatorService(avsRegistryService, logger)
stateRootUpdateBlsAggregationService := NewMessageBlsAggregatorService(avsRegistryService, clients.EthHttpClient, logger)
operatorSetUpdateBlsAggregationService := NewMessageBlsAggregatorService(avsRegistryService, clients.EthHttpClient, logger)

return &Aggregator{
logger: c.Logger,
serverIpPortAddr: c.AggregatorServerIpPortAddr,
restServerIpPortAddr: c.AggregatorRestServerIpPortAddr,
logger: logger,
serverIpPortAddr: config.AggregatorServerIpPortAddr,
restServerIpPortAddr: config.AggregatorRestServerIpPortAddr,
avsWriter: avsWriter,
avsReader: avsReader,
taskBlsAggregationService: taskBlsAggregationService,
Expand Down
23 changes: 17 additions & 6 deletions aggregator/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
sdklogging "github.com/Layr-Labs/eigensdk-go/logging"
"log"
"os"

Expand Down Expand Up @@ -37,19 +38,29 @@ func main() {

func aggregatorMain(ctx *cli.Context) error {
log.Println("Initializing Aggregator")
config, err := config.NewConfig(ctx)

rawConfig := config.NewRawConfig(ctx)
logger, err := sdklogging.NewZapLogger(rawConfig.Environment)
if err != nil {
return err
}
configJson, err := json.MarshalIndent(config, "", " ")

config, err := config.NewConfig(rawConfig, ctx)
if err != nil {
config.Logger.Fatalf(err.Error())
logger.Error("Error creating config", "err", err)
return err
}
fmt.Println("Config:", string(configJson))

bgCtx := context.Background()
{
configJson, err := json.MarshalIndent(config, "", " ")
if err != nil {
logger.Fatalf(err.Error())
}
fmt.Println("Config:", string(configJson))
}

agg, err := aggregator.NewAggregator(bgCtx, config)
bgCtx := context.Background()
agg, err := aggregator.NewAggregator(config, logger, bgCtx)
if err != nil {
return err
}
Expand Down
4 changes: 0 additions & 4 deletions core/chainio/avs_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
erc20mock "github.com/NethermindEth/near-sffl/contracts/bindings/ERC20Mock"
opsetupdatereg "github.com/NethermindEth/near-sffl/contracts/bindings/SFFLOperatorSetUpdateRegistry"
taskmanager "github.com/NethermindEth/near-sffl/contracts/bindings/SFFLTaskManager"
"github.com/NethermindEth/near-sffl/core/config"
)

type AvsReaderer interface {
Expand All @@ -37,9 +36,6 @@ type AvsReader struct {

var _ AvsReaderer = (*AvsReader)(nil)

func BuildAvsReaderFromConfig(c *config.Config) (*AvsReader, error) {
return BuildAvsReader(c.SFFLRegistryCoordinatorAddr, 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)
if err != nil {
Expand Down
10 changes: 0 additions & 10 deletions core/chainio/avs_subscriber.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (

opsetupdatereg "github.com/NethermindEth/near-sffl/contracts/bindings/SFFLOperatorSetUpdateRegistry"
taskmanager "github.com/NethermindEth/near-sffl/contracts/bindings/SFFLTaskManager"
"github.com/NethermindEth/near-sffl/core/config"
)

type AvsSubscriberer interface {
Expand All @@ -30,15 +29,6 @@ type AvsSubscriber struct {
logger sdklogging.Logger
}

func BuildAvsSubscriberFromConfig(config *config.Config) (*AvsSubscriber, error) {
return BuildAvsSubscriber(
config.SFFLRegistryCoordinatorAddr,
config.OperatorStateRetrieverAddr,
config.EthWsClient,
config.Logger,
)
}

func BuildAvsSubscriber(registryCoordinatorAddr, blsOperatorStateRetrieverAddr gethcommon.Address, ethclient eth.EthClient, logger sdklogging.Logger) (*AvsSubscriber, error) {
avsContractBindings, err := NewAvsManagersBindings(registryCoordinatorAddr, blsOperatorStateRetrieverAddr, ethclient, logger)
if err != nil {
Expand Down
5 changes: 0 additions & 5 deletions core/chainio/avs_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
logging "github.com/Layr-Labs/eigensdk-go/logging"

taskmanager "github.com/NethermindEth/near-sffl/contracts/bindings/SFFLTaskManager"
"github.com/NethermindEth/near-sffl/core/config"
)

type AvsWriterer interface {
Expand Down Expand Up @@ -49,10 +48,6 @@ type AvsWriter struct {

var _ AvsWriterer = (*AvsWriter)(nil)

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

func BuildAvsWriter(txMgr txmgr.TxManager, registryCoordinatorAddr, operatorStateRetrieverAddr gethcommon.Address, ethHttpClient eth.EthClient, logger logging.Logger) (*AvsWriter, error) {
avsServiceBindings, err := NewAvsManagersBindings(registryCoordinatorAddr, operatorStateRetrieverAddr, ethHttpClient, logger)
if err != nil {
Expand Down
71 changes: 16 additions & 55 deletions core/config/config.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
package config

import (
"context"
"crypto/ecdsa"
"errors"
"fmt"
"os"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/urfave/cli"

"github.com/Layr-Labs/eigensdk-go/chainio/clients/eth"
"github.com/Layr-Labs/eigensdk-go/chainio/txmgr"
"github.com/Layr-Labs/eigensdk-go/crypto/bls"
sdklogging "github.com/Layr-Labs/eigensdk-go/logging"
"github.com/Layr-Labs/eigensdk-go/signerv2"
sdkutils "github.com/Layr-Labs/eigensdk-go/utils"
)

Expand All @@ -23,23 +20,18 @@ import (
type Config struct {
EcdsaPrivateKey *ecdsa.PrivateKey
BlsPrivateKey *bls.PrivateKey
Logger sdklogging.Logger
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
EthWsRpcUrl string
EthHttpClient eth.EthClient
EthWsClient eth.EthClient
OperatorStateRetrieverAddr common.Address
SFFLRegistryCoordinatorAddr common.Address
AggregatorServerIpPortAddr string
AggregatorRestServerIpPortAddr string
AggregatorDatabasePath 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
}

Expand All @@ -63,93 +55,62 @@ type SFFLContractsRaw struct {
OperatorStateRetrieverAddr string `json:"operatorStateRetriever"`
}

// NewConfig parses config file to read from from flags or environment variables
// Note: This config is shared by challenger and aggregator and so we put in the core.
// Operator has a different config and is meant to be used by the operator CLI.
func NewConfig(ctx *cli.Context) (*Config, error) {

var configRaw ConfigRaw
func NewRawConfig(ctx *cli.Context) ConfigRaw {
var rawConfig ConfigRaw
configFilePath := ctx.GlobalString(ConfigFileFlag.Name)
if configFilePath != "" {
sdkutils.ReadYamlConfig(configFilePath, &configRaw)
sdkutils.ReadYamlConfig(configFilePath, &rawConfig)
}

return rawConfig
}

// NewConfig parses config file to read from from flags or environment variables
// Note: This config is shared by challenger and aggregator and so we put in the core.
// Operator has a different config and is meant to be used by the operator CLI.
func NewConfig(configRaw ConfigRaw, ctx *cli.Context) (*Config, error) {
var sfflDeploymentRaw SFFLDeploymentRaw
sfflDeploymentFilePath := ctx.GlobalString(SFFLDeploymentFileFlag.Name)
if _, err := os.Stat(sfflDeploymentFilePath); errors.Is(err, os.ErrNotExist) {
panic("Path " + sfflDeploymentFilePath + " does not exist")
}
sdkutils.ReadJsonConfig(sfflDeploymentFilePath, &sfflDeploymentRaw)

logger, err := sdklogging.NewZapLogger(configRaw.Environment)
if err != nil {
return nil, err
}

ethRpcClient, err := eth.NewClient(configRaw.EthRpcUrl)
if err != nil {
logger.Errorf("Cannot create http ethclient", "err", err)
return nil, err
}

ethWsClient, err := eth.NewClient(configRaw.EthWsUrl)
if err != nil {
logger.Errorf("Cannot create ws ethclient", "err", err)
return nil, err
}

ecdsaPrivateKeyString := ctx.GlobalString(EcdsaPrivateKeyFlag.Name)
if ecdsaPrivateKeyString[:2] == "0x" {
ecdsaPrivateKeyString = ecdsaPrivateKeyString[2:]
}

ecdsaPrivateKey, err := crypto.HexToECDSA(ecdsaPrivateKeyString)
if err != nil {
logger.Errorf("Cannot parse ecdsa private key", "err", err)
return nil, err
return nil, fmt.Errorf("Cannot parse ecdsa private key: %w", err)
}

aggregatorAddr, err := sdkutils.EcdsaPrivateKeyToAddress(ecdsaPrivateKey)
if err != nil {
logger.Error("Cannot get operator address", "err", err)
return nil, err
return nil, fmt.Errorf("cannot parse ecdsa private key: %w", err)
}

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

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

config := &Config{
EcdsaPrivateKey: ecdsaPrivateKey,
Logger: logger,
EthWsRpcUrl: configRaw.EthWsUrl,
EthHttpRpcUrl: configRaw.EthRpcUrl,
EthHttpClient: ethRpcClient,
EthWsClient: ethWsClient,
OperatorStateRetrieverAddr: common.HexToAddress(sfflDeploymentRaw.Addresses.OperatorStateRetrieverAddr),
SFFLRegistryCoordinatorAddr: common.HexToAddress(sfflDeploymentRaw.Addresses.RegistryCoordinatorAddr),
AggregatorServerIpPortAddr: configRaw.AggregatorServerIpPortAddr,
RegisterOperatorOnStartup: configRaw.RegisterOperatorOnStartup,
SignerFn: signerV2,
TxMgr: txMgr,
AggregatorAddress: aggregatorAddr,
}
config.validate()

return config, nil
}

func (c *Config) validate() {
// TODO: make sure every pointer is non-nil
if c.OperatorStateRetrieverAddr == common.HexToAddress("") {
panic("Config: BLSOperatorStateRetrieverAddr is required")
}

if c.SFFLRegistryCoordinatorAddr == common.HexToAddress("") {
panic("Config: SFFLRegistryCoordinatorAddr is required")
}
Expand Down
Loading

0 comments on commit f591083

Please sign in to comment.