Skip to content

Commit

Permalink
Compliance tests (#337)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
TomasArrachea authored Sep 12, 2024
1 parent cd86e21 commit 1b92e0f
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 8 deletions.
12 changes: 10 additions & 2 deletions cmd/egnaddrs/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"testing"

"github.com/Layr-Labs/eigensdk-go/testutils"
"github.com/ethereum/go-ethereum/common"
)

const (
Expand All @@ -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)
Expand Down
17 changes: 14 additions & 3 deletions services/avsregistry/avsregistry_chaincaller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand All @@ -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,
},
},
},
Expand Down
18 changes: 15 additions & 3 deletions services/bls_aggregation/blsagg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())

Expand Down Expand Up @@ -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,
Expand All @@ -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(
Expand Down
31 changes: 31 additions & 0 deletions testutils/test_data.go
Original file line number Diff line number Diff line change
@@ -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}
}

0 comments on commit 1b92e0f

Please sign in to comment.