From 32edc024b8d7d2e900499925f07498316cdcffed Mon Sep 17 00:00:00 2001 From: edwin <102215563+taco-paco@users.noreply.github.com> Date: Thu, 7 Mar 2024 17:59:33 +0300 Subject: [PATCH] Clean config (#33) * Some style edits * Remove unnecessary data from config * refactor: pr comment fixes * fix: cleanup execution order. Bug with break within selects * fix: revert order of initializtion * refactor: revert renaming + modified build from config. * refactor: Passing logger in NewConfig(issue #41). * fix: increased time limit for unit tests for stability. --- Makefile | 2 +- aggregator/aggregator.go | 64 ++++++++++++++++-------- aggregator/cmd/main.go | 30 +++++++---- aggregator/rpc_server.go | 1 - core/chainio/avs_reader.go | 6 ++- core/chainio/avs_subscriber.go | 10 ---- core/chainio/avs_writer.go | 6 +-- core/config/config.go | 71 +++++++-------------------- operator/attestor/attestor.go | 2 +- operator/consumer/consumer.go | 6 +-- operator/consumer/queues_listener.go | 2 +- operator/operator.go | 1 - tests/integration/integration_test.go | 60 +++++++++++----------- 13 files changed, 124 insertions(+), 137 deletions(-) diff --git a/Makefile b/Makefile index 2675e7d0..ab432fd9 100644 --- a/Makefile +++ b/Makefile @@ -110,7 +110,7 @@ mocks: ## generates mocks for tests go generate ./... tests-unit: ## runs all unit tests - go test $$(go list ./... | grep -v /integration) -coverprofile=coverage.out -covermode=atomic --timeout 18s + go test $$(go list ./... | grep -v /integration) -coverprofile=coverage.out -covermode=atomic --timeout 20s go tool cover -html=coverage.out -o coverage.html tests-contract: ## runs all forge tests diff --git a/aggregator/aggregator.go b/aggregator/aggregator.go index a378557b..f41b7c4a 100644 --- a/aggregator/aggregator.go +++ b/aggregator/aggregator.go @@ -7,10 +7,13 @@ import ( "github.com/Layr-Labs/eigensdk-go/chainio/clients" sdkclients "github.com/Layr-Labs/eigensdk-go/chainio/clients" + "github.com/Layr-Labs/eigensdk-go/chainio/clients/eth" + "github.com/Layr-Labs/eigensdk-go/chainio/txmgr" "github.com/Layr-Labs/eigensdk-go/logging" "github.com/Layr-Labs/eigensdk-go/services/avsregistry" blsagg "github.com/Layr-Labs/eigensdk-go/services/bls_aggregation" oppubkeysserv "github.com/Layr-Labs/eigensdk-go/services/operatorpubkeys" + "github.com/Layr-Labs/eigensdk-go/signerv2" sdktypes "github.com/Layr-Labs/eigensdk-go/types" "github.com/NethermindEth/near-sffl/aggregator/types" @@ -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(ctx context.Context, config *config.Config, logger logging.Logger) (*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(ctx) + 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.BuildAvsWriterFromConfig(txMgr, config, 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, @@ -295,6 +316,7 @@ func (agg *Aggregator) handleStateRootUpdateReachedQuorum(blsAggServiceResp type agg.logger.Error("Aggregator could not store message aggregation") return } + } func (agg *Aggregator) handleOperatorSetUpdateReachedQuorum(blsAggServiceResp types.MessageBlsAggregationServiceResponse) { diff --git a/aggregator/cmd/main.go b/aggregator/cmd/main.go index 6367cf62..265fdff2 100644 --- a/aggregator/cmd/main.go +++ b/aggregator/cmd/main.go @@ -7,6 +7,7 @@ import ( "log" "os" + sdklogging "github.com/Layr-Labs/eigensdk-go/logging" "github.com/urfave/cli" "github.com/NethermindEth/near-sffl/aggregator" @@ -21,7 +22,6 @@ var ( ) func main() { - app := cli.NewApp() app.Flags = config.Flags app.Version = fmt.Sprintf("%s-%s-%s", Version, GitCommit, GitDate) @@ -37,21 +37,34 @@ func main() { } func aggregatorMain(ctx *cli.Context) error { - log.Println("Initializing Aggregator") - config, err := config.NewConfig(ctx) + + configRaw, err := config.NewConfigRaw(ctx) if err != nil { return err } - configJson, err := json.MarshalIndent(config, "", " ") + + logger, err := sdklogging.NewZapLogger(configRaw.Environment) if err != nil { - config.Logger.Fatalf(err.Error()) + return err } - fmt.Println("Config:", string(configJson)) - bgCtx := context.Background() + config, err := config.NewConfig(ctx, *configRaw, logger) + if err != nil { + return err + } + + // Print config as JSON + { + 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(bgCtx, config, logger) if err != nil { return err } @@ -62,5 +75,4 @@ func aggregatorMain(ctx *cli.Context) error { } return nil - } diff --git a/aggregator/rpc_server.go b/aggregator/rpc_server.go index bd461be4..f81ced46 100644 --- a/aggregator/rpc_server.go +++ b/aggregator/rpc_server.go @@ -26,7 +26,6 @@ var ( ) func (agg *Aggregator) startServer() error { - err := rpc.Register(agg) if err != nil { agg.logger.Fatal("Format of service TaskManager isn't correct. ", "err", err) diff --git a/core/chainio/avs_reader.go b/core/chainio/avs_reader.go index bfffbbe2..447e71d2 100644 --- a/core/chainio/avs_reader.go +++ b/core/chainio/avs_reader.go @@ -37,9 +37,10 @@ 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 BuildAvsReaderFromConfig(config *config.Config, client eth.EthClient, logger logging.Logger) (*AvsReader, error) { + return BuildAvsReader(config.SFFLRegistryCoordinatorAddr, config.OperatorStateRetrieverAddr, client, 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 { @@ -51,6 +52,7 @@ func BuildAvsReader(registryCoordinatorAddr, operatorStateRetrieverAddr gethcomm } return NewAvsReader(avsRegistryReader, avsManagersBindings, logger) } + func NewAvsReader(avsRegistryReader sdkavsregistry.AvsRegistryReader, avsServiceBindings *AvsManagersBindings, logger logging.Logger) (*AvsReader, error) { return &AvsReader{ AvsRegistryReader: avsRegistryReader, diff --git a/core/chainio/avs_subscriber.go b/core/chainio/avs_subscriber.go index ed013506..ed032776 100644 --- a/core/chainio/avs_subscriber.go +++ b/core/chainio/avs_subscriber.go @@ -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 { @@ -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 { diff --git a/core/chainio/avs_writer.go b/core/chainio/avs_writer.go index d957df80..af3b5d0e 100644 --- a/core/chainio/avs_writer.go +++ b/core/chainio/avs_writer.go @@ -2,6 +2,7 @@ package chainio import ( "context" + "github.com/NethermindEth/near-sffl/core/config" gethcommon "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/types" @@ -12,7 +13,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 { @@ -49,8 +49,8 @@ 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 BuildAvsWriterFromConfig(txMgr txmgr.TxManager, config *config.Config, client eth.EthClient, logger logging.Logger) (*AvsWriter, error) { + return BuildAvsWriter(txMgr, config.SFFLRegistryCoordinatorAddr, config.OperatorStateRetrieverAddr, client, logger) } func BuildAvsWriter(txMgr txmgr.TxManager, registryCoordinatorAddr, operatorStateRetrieverAddr gethcommon.Address, ethHttpClient eth.EthClient, logger logging.Logger) (*AvsWriter, error) { diff --git a/core/config/config.go b/core/config/config.go index 165230dd..0559b402 100644 --- a/core/config/config.go +++ b/core/config/config.go @@ -1,7 +1,6 @@ package config import ( - "context" "crypto/ecdsa" "errors" "os" @@ -10,11 +9,8 @@ import ( "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" ) @@ -23,14 +19,11 @@ 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 @@ -38,8 +31,6 @@ type Config struct { 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 } @@ -63,48 +54,40 @@ 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) { - +func NewConfigRaw(ctx *cli.Context) (*ConfigRaw, error) { var configRaw ConfigRaw configFilePath := ctx.GlobalString(ConfigFileFlag.Name) - if configFilePath != "" { - sdkutils.ReadYamlConfig(configFilePath, &configRaw) - } - - var sfflDeploymentRaw SFFLDeploymentRaw - sfflDeploymentFilePath := ctx.GlobalString(SFFLDeploymentFileFlag.Name) - if _, err := os.Stat(sfflDeploymentFilePath); errors.Is(err, os.ErrNotExist) { - panic("Path " + sfflDeploymentFilePath + " does not exist") + if configFilePath == "" { + return nil, errors.New("config file path is empty") } - sdkutils.ReadJsonConfig(sfflDeploymentFilePath, &sfflDeploymentRaw) - logger, err := sdklogging.NewZapLogger(configRaw.Environment) + err := sdkutils.ReadYamlConfig(configFilePath, &configRaw) 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 - } + return &configRaw, nil +} - ethWsClient, err := eth.NewClient(configRaw.EthWsUrl) - if err != nil { - logger.Errorf("Cannot create ws ethclient", "err", err) - return nil, err +// 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, configRaw ConfigRaw, logger sdklogging.Logger) (*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) 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) + logger.Error("Cannot parse ecdsa private key", "err", err) return nil, err } @@ -114,44 +97,28 @@ func NewConfig(ctx *cli.Context) (*Config, error) { return nil, 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, AggregatorRestServerIpPortAddr: configRaw.AggregatorRestServerIpPortAddr, AggregatorDatabasePath: configRaw.AggregatorDatabasePath, - 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") } diff --git a/operator/attestor/attestor.go b/operator/attestor/attestor.go index b2a1a05d..88f303e1 100644 --- a/operator/attestor/attestor.go +++ b/operator/attestor/attestor.go @@ -68,7 +68,7 @@ type Attestor struct { rollupIdsToUrls map[uint32]string clients map[uint32]eth.EthClient notifier Notifier - consumer consumer.Consumer + consumer *consumer.Consumer registry *prometheus.Registry config *types.NodeConfig diff --git a/operator/consumer/consumer.go b/operator/consumer/consumer.go index 0ebc6ee6..3dc9e420 100644 --- a/operator/consumer/consumer.go +++ b/operator/consumer/consumer.go @@ -62,7 +62,7 @@ type Consumer struct { logger logging.Logger } -func NewConsumer(config ConsumerConfig, logger logging.Logger) Consumer { +func NewConsumer(config ConsumerConfig, logger logging.Logger) *Consumer { ctx, cancel := context.WithCancel(context.Background()) consumer := Consumer{ @@ -74,7 +74,7 @@ func NewConsumer(config ConsumerConfig, logger logging.Logger) Consumer { go consumer.Reconnect(config.Addr, ctx) - return consumer + return &consumer } func (consumer *Consumer) Reconnect(addr string, ctx context.Context) { @@ -106,7 +106,7 @@ func (consumer *Consumer) Reconnect(addr string, ctx context.Context) { case <-ctx.Done(): consumer.logger.Info("Consumer context canceled") // deref cancel smth? - break + return case err := <-consumer.connClosedErrC: if !err.Recover { diff --git a/operator/consumer/queues_listener.go b/operator/consumer/queues_listener.go index 2ffef573..8eb3a9ea 100644 --- a/operator/consumer/queues_listener.go +++ b/operator/consumer/queues_listener.go @@ -65,7 +65,7 @@ func (listener *QueuesListener) listen(rollupId uint32, rollupDataC <-chan rmq.D case <-ctx.Done(): listener.logger.Info("Consumer context canceled") // TODO: some closing and canceling here - break + return } } } diff --git a/operator/operator.go b/operator/operator.go index 608f1e46..5b0fa3bb 100644 --- a/operator/operator.go +++ b/operator/operator.go @@ -41,7 +41,6 @@ type Operator struct { config types.NodeConfig logger logging.Logger ethClient eth.EthClient - // TODO(samlaf): remove both avsWriter and eigenlayerWrite from operator // they are only used for registration, so we should make a special registration package // this way, auditing this operator code makes it obvious that operators don't need to // write to the chain during the course of their normal operations diff --git a/tests/integration/integration_test.go b/tests/integration/integration_test.go index 3903d189..0b421769 100644 --- a/tests/integration/integration_test.go +++ b/tests/integration/integration_test.go @@ -18,11 +18,9 @@ import ( "time" "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" sdkEcdsa "github.com/Layr-Labs/eigensdk-go/crypto/ecdsa" sdklogging "github.com/Layr-Labs/eigensdk-go/logging" - "github.com/Layr-Labs/eigensdk-go/signerv2" sdkutils "github.com/Layr-Labs/eigensdk-go/utils" "github.com/docker/go-connections/nat" "github.com/ethereum/go-ethereum/accounts/abi/bind" @@ -48,11 +46,12 @@ const TEST_DATA_DIR = "../../test_data" func TestIntegration(t *testing.T) { ctx, cancel := context.WithTimeout(context.Background(), 180*time.Second) + setup := setupTestEnv(t, ctx) t.Cleanup(func() { cancel() - }) - setup := setupTestEnv(t, ctx) + setup.cleanup() + }) time.Sleep(10 * time.Second) @@ -128,6 +127,7 @@ type testEnv struct { avsReader *chainio.AvsReader registryRollups []*registryrollup.ContractSFFLRegistryRollup registryRollupAuths []*bind.TransactOpts + cleanup func() } func setupTestEnv(t *testing.T, ctx context.Context) *testEnv { @@ -164,20 +164,26 @@ func setupTestEnv(t *testing.T, ctx context.Context) *testEnv { sfflDeploymentRaw := readSfflDeploymentRaw() - nodeConfig := genOperatorConfig(t, ctx, mainnetAnvil, rollupAnvils, rabbitMq) - config := buildConfig(t, sfflDeploymentRaw, mainnetAnvil) + configRaw := buildConfigRaw(mainnetAnvil) + logger, err := sdklogging.NewZapLogger(configRaw.Environment) + if err != nil { + t.Fatalf("Failed to create logger: %s", err.Error()) + } + nodeConfig := genOperatorConfig(t, ctx, mainnetAnvil, rollupAnvils, rabbitMq) operator := startOperator(t, ctx, nodeConfig) - aggregator := startAggregator(t, ctx, config) - avsReader, err := chainio.BuildAvsReaderFromConfig(config) + config := buildConfig(t, sfflDeploymentRaw, configRaw, logger) + aggregator := startAggregator(t, ctx, config, logger) + + avsReader, err := chainio.BuildAvsReaderFromConfig(config, mainnetAnvil.HttpClient, logger) if err != nil { t.Fatalf("Cannot create AVS Reader: %s", err.Error()) } registryRollups, registryRollupAuths := deployRegistryRollups(t, ctx, avsReader, rollupAnvils) - t.Cleanup(func() { + cleanup := func() { if err := os.RemoveAll(TEST_DATA_DIR); err != nil { t.Fatalf("Error cleaning test data dir: %s", err.Error()) } @@ -203,7 +209,7 @@ func setupTestEnv(t *testing.T, ctx context.Context) *testEnv { } cancelContainersCtx() - }) + } return &testEnv{ mainnetAnvil: mainnetAnvil, @@ -216,6 +222,7 @@ func setupTestEnv(t *testing.T, ctx context.Context) *testEnv { avsReader: avsReader, registryRollups: registryRollups, registryRollupAuths: registryRollupAuths, + cleanup: cleanup, } } @@ -233,10 +240,10 @@ func startOperator(t *testing.T, ctx context.Context, nodeConfig types.NodeConfi return operator } -func startAggregator(t *testing.T, ctx context.Context, config *config.Config) *aggregator.Aggregator { +func startAggregator(t *testing.T, ctx context.Context, config *config.Config, logger sdklogging.Logger) *aggregator.Aggregator { t.Log("starting aggregator for integration tests") - agg, err := aggregator.NewAggregator(ctx, config) + agg, err := aggregator.NewAggregator(ctx, config, logger) if err != nil { t.Fatalf("Failed to create aggregator: %s", err.Error()) } @@ -351,19 +358,18 @@ func genOperatorConfig(t *testing.T, ctx context.Context, mainnetAnvil *AnvilIns return nodeConfig } -func buildConfig(t *testing.T, sfflDeploymentRaw config.SFFLDeploymentRaw, mainnetAnvil *AnvilInstance) *config.Config { - var aggConfigRaw config.ConfigRaw +func buildConfigRaw(mainnetAnvil *AnvilInstance) config.ConfigRaw { + var configRaw config.ConfigRaw aggConfigFilePath := "../../config-files/aggregator.yaml" - sdkutils.ReadYamlConfig(aggConfigFilePath, &aggConfigRaw) - aggConfigRaw.EthRpcUrl = mainnetAnvil.HttpUrl - aggConfigRaw.EthWsUrl = mainnetAnvil.WsUrl - aggConfigRaw.AggregatorDatabasePath = "" + sdkutils.ReadYamlConfig(aggConfigFilePath, &configRaw) + configRaw.EthRpcUrl = mainnetAnvil.HttpUrl + configRaw.EthWsUrl = mainnetAnvil.WsUrl + configRaw.AggregatorDatabasePath = "" - logger, err := sdklogging.NewZapLogger(aggConfigRaw.Environment) - if err != nil { - t.Fatalf("Failed to create logger: %s", err.Error()) - } + return configRaw +} +func buildConfig(t *testing.T, sfflDeploymentRaw config.SFFLDeploymentRaw, aggConfigRaw config.ConfigRaw, logeer sdklogging.Logger) *config.Config { aggregatorEcdsaPrivateKeyString := "0x2a871d0798f97d79848a013d4936a73bf4cc922c825d33c1cf7073dff6d409c6" if aggregatorEcdsaPrivateKeyString[:2] == "0x" { aggregatorEcdsaPrivateKeyString = aggregatorEcdsaPrivateKeyString[2:] @@ -377,26 +383,16 @@ func buildConfig(t *testing.T, sfflDeploymentRaw config.SFFLDeploymentRaw, mainn t.Fatalf("Cannot get operator address: %s", err.Error()) } - privateKeySigner, _, err := signerv2.SignerFromConfig(signerv2.Config{PrivateKey: aggregatorEcdsaPrivateKey}, mainnetAnvil.ChainID) - if err != nil { - t.Fatalf("Cannot create signer: %s", err.Error()) - } - txMgr := txmgr.NewSimpleTxManager(mainnetAnvil.HttpClient, logger, privateKeySigner, aggregatorAddr) - return &config.Config{ EcdsaPrivateKey: aggregatorEcdsaPrivateKey, - Logger: logger, EthHttpRpcUrl: aggConfigRaw.EthRpcUrl, - EthHttpClient: mainnetAnvil.HttpClient, EthWsRpcUrl: aggConfigRaw.EthWsUrl, - EthWsClient: mainnetAnvil.WsClient, OperatorStateRetrieverAddr: common.HexToAddress(sfflDeploymentRaw.Addresses.OperatorStateRetrieverAddr), SFFLRegistryCoordinatorAddr: common.HexToAddress(sfflDeploymentRaw.Addresses.RegistryCoordinatorAddr), AggregatorServerIpPortAddr: aggConfigRaw.AggregatorServerIpPortAddr, AggregatorRestServerIpPortAddr: aggConfigRaw.AggregatorRestServerIpPortAddr, AggregatorDatabasePath: aggConfigRaw.AggregatorDatabasePath, RegisterOperatorOnStartup: aggConfigRaw.RegisterOperatorOnStartup, - TxMgr: txMgr, AggregatorAddress: aggregatorAddr, } }