Skip to content

Commit

Permalink
Add test for SubscribeToOperatorSocketUpdates
Browse files Browse the repository at this point in the history
  • Loading branch information
damiramirez committed Jan 21, 2025
1 parent 2828d04 commit ad12dc3
Showing 1 changed file with 54 additions and 32 deletions.
86 changes: 54 additions & 32 deletions chainio/clients/avsregistry/subscriber_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,36 +19,58 @@ func TestSubscriberAvsRegistry(t *testing.T) {
chainSubscriber := client.AvsRegistryChainSubscriber
chainWriter := client.AvsRegistryChainWriter

pubKeyRegistrations, event, err := chainSubscriber.SubscribeToNewPubkeyRegistrations()
defer event.Unsubscribe()
require.NoError(t, err)

// Emit a NewPubkeyRegistration event creating a new operator
keypair, err := bls.NewKeyPairFromString("0x01")
require.NoError(t, err, "Failed to create BLS key pair")

ecdsaPrivateKey, err := crypto.HexToECDSA(testutils.ANVIL_FIRST_PRIVATE_KEY)
require.NoError(t, err, "Failed to parse ECDSA private key")

quorumNumbers := types.QuorumNums{0}

receipt, err := chainWriter.RegisterOperator(
context.Background(),
ecdsaPrivateKey,
keypair,
quorumNumbers,
"",
true,
)
require.NoError(t, err)
require.NotNil(t, receipt)

select {
case newPubkeyRegistration := <-pubKeyRegistrations:
expectedOperator := crypto.PubkeyToAddress(ecdsaPrivateKey.PublicKey)
assert.Equal(t, expectedOperator, newPubkeyRegistration.Operator)
case <-time.After(10 * time.Second):
// Throw an error if the event is not received within 10 seconds, making the test fail
t.Fatal("Timed out waiting for NewPubkeyRegistration event")
}
t.Run("subscribe to new pubkey registrations", func(t *testing.T) {
pubKeyRegistrationsC, event, err := chainSubscriber.SubscribeToNewPubkeyRegistrations()
defer event.Unsubscribe()
require.NoError(t, err)

// Emit a NewPubkeyRegistration event creating a new operator
keypair, err := bls.NewKeyPairFromString("0x01")
require.NoError(t, err, "Failed to create BLS key pair")

ecdsaPrivateKey, err := crypto.HexToECDSA(testutils.ANVIL_FIRST_PRIVATE_KEY)
require.NoError(t, err, "Failed to parse ECDSA private key")

quorumNumbers := types.QuorumNums{0}

receipt, err := chainWriter.RegisterOperator(
context.Background(),
ecdsaPrivateKey,
keypair,
quorumNumbers,
"",
true,
)
require.NoError(t, err)
require.NotNil(t, receipt)

select {
case newPubkeyRegistration := <-pubKeyRegistrationsC:
expectedOperator := crypto.PubkeyToAddress(ecdsaPrivateKey.PublicKey)
assert.Equal(t, expectedOperator, newPubkeyRegistration.Operator)
case <-time.After(10 * time.Second):
// Throw an error if the event is not received within 10 seconds, making the test fail
t.Fatal("Timed out waiting for NewPubkeyRegistration event")
}
})

t.Run("subscribe to operator socket updates", func(t *testing.T) {
socketC, event, err := chainSubscriber.SubscribeToOperatorSocketUpdates()
defer event.Unsubscribe()
require.NoError(t, err)

// Emit a SocketUpdate event
socketUpdate := "socket-update"
receipt, err := chainWriter.UpdateSocket(context.Background(), types.Socket(socketUpdate), true)
require.NoError(t, err)
require.NotNil(t, receipt)

select {
case operatorSocketUpdate := <-socketC:
assert.Equal(t, socketUpdate, operatorSocketUpdate.Socket)
case <-time.After(10 * time.Second):
// Throw an error if the event is not received within 10 seconds, making the test fail
t.Fatal("Timed out waiting for NewPubkeyRegistration event")
}
})
}

0 comments on commit ad12dc3

Please sign in to comment.