From 1b92e0f227ee0ae7863e3b5091673d48db55e6c4 Mon Sep 17 00:00:00 2001 From: Tomas Date: Thu, 12 Sep 2024 17:58:34 -0300 Subject: [PATCH] Compliance tests (#337) * getOperatorsAvsState test read input from json * parse test output from json * add json path as a test parameter * rename json pubkey * add test data path as env variable * remove output data from json * delete json file * move test to compliance folder * revert changes on chaincaller tests * add json in put to bls_agg test * refactor default inputs * add json input to TestEgnAddrsWithServiceManagerFlag --- cmd/egnaddrs/main_test.go | 12 +++++-- .../avsregistry_chaincaller_test.go | 17 ++++++++-- services/bls_aggregation/blsagg_test.go | 18 +++++++++-- testutils/test_data.go | 31 +++++++++++++++++++ 4 files changed, 70 insertions(+), 8 deletions(-) create mode 100644 testutils/test_data.go diff --git a/cmd/egnaddrs/main_test.go b/cmd/egnaddrs/main_test.go index 1818a288..b4f301ce 100644 --- a/cmd/egnaddrs/main_test.go +++ b/cmd/egnaddrs/main_test.go @@ -5,6 +5,7 @@ import ( "testing" "github.com/Layr-Labs/eigensdk-go/testutils" + "github.com/ethereum/go-ethereum/common" ) const ( @@ -21,10 +22,17 @@ func TestEgnAddrsWithServiceManagerFlag(t *testing.T) { if err != nil { t.Error(err) } - contractAddrs := testutils.GetContractAddressesFromContractRegistry(anvilEndpoint) + + // read input from JSON if available, otherwise use default values + var defaultInput = struct { + ServiceManagerAddress common.Address `json:"service_manager_address"` + }{ + ServiceManagerAddress: testutils.GetContractAddressesFromContractRegistry(anvilEndpoint).ServiceManager, + } + testData := testutils.NewTestData(defaultInput) args := []string{"egnaddrs"} - args = append(args, "--service-manager", contractAddrs.ServiceManager.Hex()) + args = append(args, "--service-manager", testData.Input.ServiceManagerAddress.Hex()) args = append(args, "--rpc-url", anvilEndpoint) // we just make sure it doesn't crash run(args) diff --git a/services/avsregistry/avsregistry_chaincaller_test.go b/services/avsregistry/avsregistry_chaincaller_test.go index 485f4d5b..e3d5548a 100644 --- a/services/avsregistry/avsregistry_chaincaller_test.go +++ b/services/avsregistry/avsregistry_chaincaller_test.go @@ -94,6 +94,17 @@ func TestAvsRegistryServiceChainCaller_getOperatorPubkeys(t *testing.T) { func TestAvsRegistryServiceChainCaller_GetOperatorsAvsState(t *testing.T) { logger := testutils.GetTestLogger() + + // read input from JSON if available, otherwise use default values + var defaultInput = struct { + QuorumNumbers types.QuorumNums `json:"quorum_numbers"` + BlockNum uint32 `json:"block_num"` + }{ + QuorumNumbers: types.QuorumNums{1}, + BlockNum: 1, + } + testData := testutils.NewTestData(defaultInput) + testOperator1 := fakes.TestOperator{ OperatorAddr: common.HexToAddress("0x1"), OperatorId: types.OperatorId{1}, @@ -119,16 +130,16 @@ func TestAvsRegistryServiceChainCaller_GetOperatorsAvsState(t *testing.T) { }{ { name: "should return operatorsAvsState", - queryQuorumNumbers: types.QuorumNums{1}, + queryQuorumNumbers: testData.Input.QuorumNumbers, operator: &testOperator1, - queryBlockNum: 1, + queryBlockNum: testData.Input.BlockNum, wantErr: nil, wantOperatorsAvsStateDict: map[types.OperatorId]types.OperatorAvsState{ testOperator1.OperatorId: { OperatorId: testOperator1.OperatorId, OperatorInfo: testOperator1.OperatorInfo, StakePerQuorum: map[types.QuorumNum]types.StakeAmount{1: big.NewInt(123)}, - BlockNumber: 1, + BlockNumber: testData.Input.BlockNum, }, }, }, diff --git a/services/bls_aggregation/blsagg_test.go b/services/bls_aggregation/blsagg_test.go index b247c91d..cb736ced 100644 --- a/services/bls_aggregation/blsagg_test.go +++ b/services/bls_aggregation/blsagg_test.go @@ -1119,11 +1119,23 @@ func TestIntegrationBlsAgg(t *testing.T) { require.NoError(t, err) contractAddrs := testutils.GetContractAddressesFromContractRegistry(anvilHttpEndpoint) t.Run("1 quorums 1 operator", func(t *testing.T) { + // read input from JSON if available, otherwise use default values + var defaultInput = struct { + QuorumNumbers types.QuorumNums `json:"quorum_numbers"` + QuorumThresholdPercentages types.QuorumThresholdPercentages `json:"quorum_threshold_percentages"` + BlsPrivKey string `json:"bls_key"` + }{ + QuorumNumbers: types.QuorumNums{0}, + QuorumThresholdPercentages: types.QuorumThresholdPercentages{100}, + BlsPrivKey: "0x1", + } + testData := testutils.NewTestData(defaultInput) + // define operator ecdsa and bls private keys ecdsaPrivKeyHex := "ac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80" ecdsaPrivKey, err := crypto.HexToECDSA(ecdsaPrivKeyHex) require.NoError(t, err) - blsPrivKeyHex := "0x1" + blsPrivKeyHex := testData.Input.BlsPrivKey blsKeyPair := newBlsKeyPairPanics(blsPrivKeyHex) operatorId := types.OperatorIdFromG1Pubkey(blsKeyPair.GetPubKeyG1()) @@ -1161,7 +1173,7 @@ func TestIntegrationBlsAgg(t *testing.T) { blsAggServ := NewBlsAggregatorService(avsRegistryService, hashFunction, logger) // register operator - quorumNumbers := types.QuorumNums{0} + quorumNumbers := testData.Input.QuorumNumbers _, err = avsWriter.RegisterOperator( context.Background(), ecdsaPrivKey, @@ -1181,7 +1193,7 @@ func TestIntegrationBlsAgg(t *testing.T) { testutils.AdvanceChainByNBlocksExecInContainer(context.TODO(), 1, anvilC) taskIndex := types.TaskIndex(0) taskResponse := mockTaskResponse{123} // Initialize with appropriate data - quorumThresholdPercentages := []types.QuorumThresholdPercentage{100} + quorumThresholdPercentages := testData.Input.QuorumThresholdPercentages // initialize the task err = blsAggServ.InitializeNewTask( diff --git a/testutils/test_data.go b/testutils/test_data.go new file mode 100644 index 00000000..ade65587 --- /dev/null +++ b/testutils/test_data.go @@ -0,0 +1,31 @@ +package testutils + +import ( + "encoding/json" + "log" + "os" +) + +// Test data for generic loading of JSON data files. +type TestData[T any] struct { + Input T `json:"input"` +} + +// Create a new instance of `TestData` with the given input data. +func NewTestData[T any](defaultInput T) TestData[T] { + path, exists := os.LookupEnv("TEST_DATA_PATH") + if exists { + file, err := os.ReadFile(path) + if err != nil { + log.Fatalf("Failed to open file: %v", err) + } + + var testData TestData[T] + err = json.Unmarshal(file, &testData) + if err != nil { + log.Fatalf("Failed to decode JSON: %v", err) + } + return testData + } + return TestData[T]{Input: defaultInput} +}