From 60268bb67bb408cf2b96c07924b9e6c6ae627135 Mon Sep 17 00:00:00 2001 From: 0xpanicError Date: Mon, 13 May 2024 18:37:18 +0530 Subject: [PATCH 1/2] refactor: aggregator uses flags instead of config-file --- Makefile | 10 ++++++++- core/config/config.go | 49 ++++++++++++++++++++++++++++++++++++++----- 2 files changed, 53 insertions(+), 6 deletions(-) diff --git a/Makefile b/Makefile index 97a79f53..1ad5e7f4 100644 --- a/Makefile +++ b/Makefile @@ -7,6 +7,11 @@ help: AGGREGATOR_ECDSA_PRIV_KEY=0x2a871d0798f97d79848a013d4936a73bf4cc922c825d33c1cf7073dff6d409c6 CHALLENGER_ECDSA_PRIV_KEY=0x5de4111afa1a4b94908f83103eb1f1706367c2e68ca870fc3fb9a804cdab365a +ETH_RPC_URL=http://localhost:8545 +ETH_WS_URL=ws://localhost:8545 + +AGGREGATOR_SERVER_IP_PORT_ADDRESS=localhost:8090 + CHAINID=31337 # Make sure to update this if the strategy address changes # check in contracts/script/output/${CHAINID}/credible_squaring_avs_deployment_output.json @@ -68,7 +73,10 @@ send-fund: ## sends fund to the operator saved in tests/keys/test.ecdsa.key.json # TODO: piping to zap-pretty only works when zapper environment is set to production, unsure why ____OFFCHAIN_SOFTWARE___: ## start-aggregator: ## - go run aggregator/cmd/main.go --config config-files/aggregator.yaml \ + go run aggregator/cmd/main.go --environment production \ + --eth-rpc-url ${ETH_RPC_URL} \ + --eth-ws-url ${ETH_WS_URL} \ + --aggregator-server-ip-port-address ${AGGREGATOR_SERVER_IP_PORT_ADDRESS} \ --credible-squaring-deployment ${DEPLOYMENT_FILES_DIR}/credible_squaring_avs_deployment_output.json \ --ecdsa-private-key ${AGGREGATOR_ECDSA_PRIV_KEY} \ 2>&1 | zap-pretty diff --git a/core/config/config.go b/core/config/config.go index 70f54548..51fbcf42 100644 --- a/core/config/config.go +++ b/core/config/config.go @@ -61,15 +61,31 @@ type IncredibleSquaringContractsRaw struct { OperatorStateRetrieverAddr string `json:"operatorStateRetriever"` } -// NewConfig parses config file to read from from flags or environment variables +// NewConfig parses config file to read 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 - configFilePath := ctx.GlobalString(ConfigFileFlag.Name) - if configFilePath != "" { - sdkutils.ReadYamlConfig(configFilePath, &configRaw) + + environment := ctx.GlobalString(EnvironmentFlag.Name) + if environment != "" { + configRaw.Environment = sdklogging.LogLevel(environment) + } + + ethRpcUrl := ctx.GlobalString(EthRpcUrlFlag.Name) + if ethRpcUrl != "" { + configRaw.EthRpcUrl = ethRpcUrl + } + + ethWsUrl := ctx.GlobalString(EthWsUrlFlag.Name) + if ethWsUrl != "" { + configRaw.EthWsUrl = ethWsUrl + } + + aggregatorServerIpPortAddr := ctx.GlobalString(AggregatorServerIpPortAddressFlag.Name) + if aggregatorServerIpPortAddr != "" { + configRaw.AggregatorServerIpPortAddr = aggregatorServerIpPortAddr } var credibleSquaringDeploymentRaw IncredibleSquaringDeploymentRaw @@ -164,6 +180,26 @@ var ( Required: true, Usage: "Load configuration from `FILE`", } + EnvironmentFlag = cli.StringFlag{ + Name: "environment", + Required: true, + Usage: "Set the environment (production or development)", + } + EthRpcUrlFlag = cli.StringFlag{ + Name: "eth-rpc-url", + Required: true, + Usage: "Ethereum RPC URL", + } + EthWsUrlFlag = cli.StringFlag{ + Name: "eth-ws-url", + Required: true, + Usage: "Ethereum WS URL", + } + AggregatorServerIpPortAddressFlag = cli.StringFlag{ + Name: "aggregator-server-ip-port-address", + Required: true, + Usage: "Aggregator server IP PORT address", + } CredibleSquaringDeploymentFileFlag = cli.StringFlag{ Name: "credible-squaring-deployment", Required: true, @@ -179,7 +215,10 @@ var ( ) var requiredFlags = []cli.Flag{ - ConfigFileFlag, + EnvironmentFlag, + EthRpcUrlFlag, + EthWsUrlFlag, + AggregatorServerIpPortAddressFlag, CredibleSquaringDeploymentFileFlag, EcdsaPrivateKeyFlag, } From ec54a77ae7040f7ee25ee8127aff989872fc4699 Mon Sep 17 00:00:00 2001 From: 0xpanicError Date: Wed, 15 May 2024 12:06:26 +0530 Subject: [PATCH 2/2] refactor: added EnvVars, removed ConfigRaw --- .env.example | 8 ++ .gitignore | 5 +- Makefile | 4 +- core/config/config.go | 101 ++++++++++++-------------- go.mod | 1 + go.sum | 2 + tests/integration/integration_test.go | 27 ++++--- 7 files changed, 77 insertions(+), 71 deletions(-) create mode 100644 .env.example diff --git a/.env.example b/.env.example new file mode 100644 index 00000000..9432d5d8 --- /dev/null +++ b/.env.example @@ -0,0 +1,8 @@ +# Example env file to set aggregator configuration + +# ETH Client Config +ETH_RPC_URL=http://localhost:8545 +ETH_WS_URL=ws://localhost:8545 + +# Aggregator Server Config +AGGREGATOR_SERVER_IP_PORT_ADDRESS=localhost:8090 \ No newline at end of file diff --git a/.gitignore b/.gitignore index 75f95977..abb267f8 100644 --- a/.gitignore +++ b/.gitignore @@ -25,4 +25,7 @@ coverage.html logs.txt # just for example -id_rsa \ No newline at end of file +id_rsa + +# env file +.env \ No newline at end of file diff --git a/Makefile b/Makefile index 1ad5e7f4..683be29c 100644 --- a/Makefile +++ b/Makefile @@ -73,10 +73,8 @@ send-fund: ## sends fund to the operator saved in tests/keys/test.ecdsa.key.json # TODO: piping to zap-pretty only works when zapper environment is set to production, unsure why ____OFFCHAIN_SOFTWARE___: ## start-aggregator: ## + set -a && source .env && set +a && \ go run aggregator/cmd/main.go --environment production \ - --eth-rpc-url ${ETH_RPC_URL} \ - --eth-ws-url ${ETH_WS_URL} \ - --aggregator-server-ip-port-address ${AGGREGATOR_SERVER_IP_PORT_ADDRESS} \ --credible-squaring-deployment ${DEPLOYMENT_FILES_DIR}/credible_squaring_avs_deployment_output.json \ --ecdsa-private-key ${AGGREGATOR_ECDSA_PRIV_KEY} \ 2>&1 | zap-pretty diff --git a/core/config/config.go b/core/config/config.go index 51fbcf42..ad3a0f0e 100644 --- a/core/config/config.go +++ b/core/config/config.go @@ -43,15 +43,6 @@ type Config struct { AggregatorAddress common.Address } -// These are read from ConfigFileFlag -type ConfigRaw struct { - Environment sdklogging.LogLevel `yaml:"environment"` - EthRpcUrl string `yaml:"eth_rpc_url"` - EthWsUrl string `yaml:"eth_ws_url"` - AggregatorServerIpPortAddr string `yaml:"aggregator_server_ip_port_address"` - RegisterOperatorOnStartup bool `yaml:"register_operator_on_startup"` -} - // These are read from CredibleSquaringDeploymentFileFlag type IncredibleSquaringDeploymentRaw struct { Addresses IncredibleSquaringContractsRaw `json:"addresses"` @@ -65,29 +56,29 @@ type IncredibleSquaringContractsRaw struct { // 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 - - environment := ctx.GlobalString(EnvironmentFlag.Name) - if environment != "" { - configRaw.Environment = sdklogging.LogLevel(environment) + Environment := sdklogging.LogLevel(ctx.GlobalString(EnvironmentFlag.Name)) + logger, err := sdklogging.NewZapLogger(Environment) + if err != nil { + return nil, err } - ethRpcUrl := ctx.GlobalString(EthRpcUrlFlag.Name) - if ethRpcUrl != "" { - configRaw.EthRpcUrl = ethRpcUrl + EthRpcUrl := ctx.GlobalString(EthRpcUrlFlag.Name) + if EthRpcUrl == "" { + logger.Errorf("EthRpcUrl is required") } - ethWsUrl := ctx.GlobalString(EthWsUrlFlag.Name) - if ethWsUrl != "" { - configRaw.EthWsUrl = ethWsUrl + EthWsUrl := ctx.GlobalString(EthWsUrlFlag.Name) + if EthWsUrl == "" { + logger.Errorf("EthWsUrl is required") } - aggregatorServerIpPortAddr := ctx.GlobalString(AggregatorServerIpPortAddressFlag.Name) - if aggregatorServerIpPortAddr != "" { - configRaw.AggregatorServerIpPortAddr = aggregatorServerIpPortAddr + AggregatorServerIpPortAddr := ctx.GlobalString(AggregatorServerIpPortAddressFlag.Name) + if AggregatorServerIpPortAddr == "" { + logger.Errorf("AggregatorServerIpPortAddress is required") } + RegisterOperatorOnStartup := true // default value + var credibleSquaringDeploymentRaw IncredibleSquaringDeploymentRaw credibleSquaringDeploymentFilePath := ctx.GlobalString(CredibleSquaringDeploymentFileFlag.Name) if _, err := os.Stat(credibleSquaringDeploymentFilePath); errors.Is(err, os.ErrNotExist) { @@ -95,18 +86,13 @@ func NewConfig(ctx *cli.Context) (*Config, error) { } sdkutils.ReadJsonConfig(credibleSquaringDeploymentFilePath, &credibleSquaringDeploymentRaw) - logger, err := sdklogging.NewZapLogger(configRaw.Environment) - if err != nil { - return nil, err - } - - ethRpcClient, err := eth.NewClient(configRaw.EthRpcUrl) + ethRpcClient, err := eth.NewClient(EthRpcUrl) if err != nil { logger.Errorf("Cannot create http ethclient", "err", err) return nil, err } - ethWsClient, err := eth.NewClient(configRaw.EthWsUrl) + ethWsClient, err := eth.NewClient(EthWsUrl) if err != nil { logger.Errorf("Cannot create ws ethclient", "err", err) return nil, err @@ -147,14 +133,14 @@ func NewConfig(ctx *cli.Context) (*Config, error) { config := &Config{ EcdsaPrivateKey: ecdsaPrivateKey, Logger: logger, - EthWsRpcUrl: configRaw.EthWsUrl, - EthHttpRpcUrl: configRaw.EthRpcUrl, + EthWsRpcUrl: EthWsUrl, + EthHttpRpcUrl: EthRpcUrl, EthHttpClient: ethRpcClient, EthWsClient: ethWsClient, OperatorStateRetrieverAddr: common.HexToAddress(credibleSquaringDeploymentRaw.Addresses.OperatorStateRetrieverAddr), IncredibleSquaringRegistryCoordinatorAddr: common.HexToAddress(credibleSquaringDeploymentRaw.Addresses.RegistryCoordinatorAddr), - AggregatorServerIpPortAddr: configRaw.AggregatorServerIpPortAddr, - RegisterOperatorOnStartup: configRaw.RegisterOperatorOnStartup, + AggregatorServerIpPortAddr: AggregatorServerIpPortAddr, + RegisterOperatorOnStartup: RegisterOperatorOnStartup, SignerFn: signerV2, TxMgr: txMgr, AggregatorAddress: aggregatorAddr, @@ -180,51 +166,56 @@ var ( Required: true, Usage: "Load configuration from `FILE`", } + CredibleSquaringDeploymentFileFlag = cli.StringFlag{ + Name: "credible-squaring-deployment", + Required: true, + Usage: "Load credible squaring contract addresses from `FILE`", + } + EcdsaPrivateKeyFlag = cli.StringFlag{ + Name: "ecdsa-private-key", + Usage: "Ethereum private key", + Required: true, + EnvVar: "ECDSA_PRIVATE_KEY", + } + /* Optional Flags */ EnvironmentFlag = cli.StringFlag{ Name: "environment", - Required: true, + Required: false, Usage: "Set the environment (production or development)", + Value: "development", // default value } EthRpcUrlFlag = cli.StringFlag{ Name: "eth-rpc-url", - Required: true, + Required: false, Usage: "Ethereum RPC URL", + EnvVar: "ETH_RPC_URL", } EthWsUrlFlag = cli.StringFlag{ Name: "eth-ws-url", - Required: true, + Required: false, Usage: "Ethereum WS URL", + EnvVar: "ETH_WS_URL", } AggregatorServerIpPortAddressFlag = cli.StringFlag{ Name: "aggregator-server-ip-port-address", - Required: true, + Required: false, Usage: "Aggregator server IP PORT address", + EnvVar: "AGGREGATOR_SERVER_IP_PORT_ADDRESS", } - CredibleSquaringDeploymentFileFlag = cli.StringFlag{ - Name: "credible-squaring-deployment", - Required: true, - Usage: "Load credible squaring contract addresses from `FILE`", - } - EcdsaPrivateKeyFlag = cli.StringFlag{ - Name: "ecdsa-private-key", - Usage: "Ethereum private key", - Required: true, - EnvVar: "ECDSA_PRIVATE_KEY", - } - /* Optional Flags */ ) var requiredFlags = []cli.Flag{ + CredibleSquaringDeploymentFileFlag, + EcdsaPrivateKeyFlag, +} + +var optionalFlags = []cli.Flag{ EnvironmentFlag, EthRpcUrlFlag, EthWsUrlFlag, AggregatorServerIpPortAddressFlag, - CredibleSquaringDeploymentFileFlag, - EcdsaPrivateKeyFlag, } -var optionalFlags = []cli.Flag{} - func init() { Flags = append(requiredFlags, optionalFlags...) } diff --git a/go.mod b/go.mod index 476190da..4a5529c1 100644 --- a/go.mod +++ b/go.mod @@ -72,6 +72,7 @@ require ( github.com/holiman/uint256 v1.2.4 // indirect github.com/huin/goupnp v1.3.0 // indirect github.com/jackpal/go-nat-pmp v1.0.2 // indirect + github.com/joho/godotenv v1.5.1 // indirect github.com/klauspost/compress v1.16.0 // indirect github.com/kr/pretty v0.3.1 // indirect github.com/kr/text v0.2.0 // indirect diff --git a/go.sum b/go.sum index a6a98f47..50bc3498 100644 --- a/go.sum +++ b/go.sum @@ -231,6 +231,8 @@ github.com/iris-contrib/pongo2 v0.0.1/go.mod h1:Ssh+00+3GAZqSQb30AvBRNxBx7rf0Gqw github.com/iris-contrib/schema v0.0.1/go.mod h1:urYA3uvUNG1TIIjOSCzHr9/LmbQo8LrOcOqfqxa4hXw= github.com/jackpal/go-nat-pmp v1.0.2 h1:KzKSgb7qkJvOUTqYl9/Hg/me3pWgBmERKrTGD7BdWus= github.com/jackpal/go-nat-pmp v1.0.2/go.mod h1:QPH045xvCAeXUZOxsnwmrtiCoxIr9eob+4orBN1SBKc= +github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= +github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU= github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU= diff --git a/tests/integration/integration_test.go b/tests/integration/integration_test.go index 963cc173..54d72e1d 100644 --- a/tests/integration/integration_test.go +++ b/tests/integration/integration_test.go @@ -46,25 +46,26 @@ func TestIntegration(t *testing.T) { } /* Prepare the config file for aggregator */ - var aggConfigRaw config.ConfigRaw - aggConfigFilePath := "../../config-files/aggregator.yaml" - sdkutils.ReadYamlConfig(aggConfigFilePath, &aggConfigRaw) - aggConfigRaw.EthRpcUrl = "http://" + anvilEndpoint - aggConfigRaw.EthWsUrl = "ws://" + anvilEndpoint + // aggConfigFilePath := "../../config-files/aggregator.yaml" + // sdkutils.ReadYamlConfig(aggConfigFilePath, &aggConfigRaw) + Environment := sdklogging.LogLevel("production") + EthRpcUrl := "http://" + anvilEndpoint + EthWsUrl := "ws://" + anvilEndpoint + AggregatorServerIpPortAddr := "localhost:8090" var credibleSquaringDeploymentRaw config.IncredibleSquaringDeploymentRaw credibleSquaringDeploymentFilePath := "../../contracts/script/output/31337/credible_squaring_avs_deployment_output.json" sdkutils.ReadJsonConfig(credibleSquaringDeploymentFilePath, &credibleSquaringDeploymentRaw) - logger, err := sdklogging.NewZapLogger(aggConfigRaw.Environment) + logger, err := sdklogging.NewZapLogger(Environment) if err != nil { t.Fatalf("Failed to create logger: %s", err.Error()) } - ethRpcClient, err := eth.NewClient(aggConfigRaw.EthRpcUrl) + ethRpcClient, err := eth.NewClient(EthRpcUrl) if err != nil { t.Fatalf("Failed to create eth client: %s", err.Error()) } - ethWsClient, err := eth.NewClient(aggConfigRaw.EthWsUrl) + ethWsClient, err := eth.NewClient(EthWsUrl) if err != nil { t.Fatalf("Failed to create eth client: %s", err.Error()) } @@ -97,17 +98,19 @@ func TestIntegration(t *testing.T) { } txMgr := txmgr.NewSimpleTxManager(skWallet, ethRpcClient, logger, aggregatorAddr) + RegisterOperatorOnStartup := true // default value + config := &config.Config{ EcdsaPrivateKey: aggregatorEcdsaPrivateKey, Logger: logger, - EthHttpRpcUrl: aggConfigRaw.EthRpcUrl, + EthHttpRpcUrl: EthRpcUrl, EthHttpClient: ethRpcClient, - EthWsRpcUrl: aggConfigRaw.EthWsUrl, + EthWsRpcUrl: EthWsUrl, EthWsClient: ethWsClient, OperatorStateRetrieverAddr: common.HexToAddress(credibleSquaringDeploymentRaw.Addresses.OperatorStateRetrieverAddr), IncredibleSquaringRegistryCoordinatorAddr: common.HexToAddress(credibleSquaringDeploymentRaw.Addresses.RegistryCoordinatorAddr), - AggregatorServerIpPortAddr: aggConfigRaw.AggregatorServerIpPortAddr, - RegisterOperatorOnStartup: aggConfigRaw.RegisterOperatorOnStartup, + AggregatorServerIpPortAddr: AggregatorServerIpPortAddr, + RegisterOperatorOnStartup: RegisterOperatorOnStartup, TxMgr: txMgr, AggregatorAddress: aggregatorAddr, }