Skip to content

Commit

Permalink
Clean config (#33)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
taco-paco authored Mar 7, 2024
1 parent b77b08b commit 32edc02
Show file tree
Hide file tree
Showing 13 changed files with 124 additions and 137 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
64 changes: 43 additions & 21 deletions aggregator/aggregator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
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(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,
Expand Down Expand Up @@ -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) {
Expand Down
30 changes: 21 additions & 9 deletions aggregator/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -21,7 +22,6 @@ var (
)

func main() {

app := cli.NewApp()
app.Flags = config.Flags
app.Version = fmt.Sprintf("%s-%s-%s", Version, GitCommit, GitDate)
Expand All @@ -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
}
Expand All @@ -62,5 +75,4 @@ func aggregatorMain(ctx *cli.Context) error {
}

return nil

}
1 change: 0 additions & 1 deletion aggregator/rpc_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 4 additions & 2 deletions core/chainio/avs_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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,
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
6 changes: 3 additions & 3 deletions core/chainio/avs_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 {
Expand Down Expand Up @@ -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) {
Expand Down
Loading

0 comments on commit 32edc02

Please sign in to comment.