Skip to content

Commit

Permalink
eth2exp: refactor tests (#1107)
Browse files Browse the repository at this point in the history
Refactor eth2exp tests.

category: test
ticket: none
  • Loading branch information
xenowits committed Sep 10, 2022
1 parent c2af392 commit 50e6b5c
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 32 deletions.
27 changes: 27 additions & 0 deletions eth2util/eth2exp/attagg_internal_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright © 2022 Obol Labs Inc.
//
// This program is free software: you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the Free
// Software Foundation, either version 3 of the License, or (at your option)
// any later version.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
// more details.
//
// You should have received a copy of the GNU General Public License along with
// this program. If not, see <http://www.gnu.org/licenses/>.

package eth2exp

import (
"context"

eth2p0 "github.com/attestantio/go-eth2-client/spec/phase0"
)

// IsAggregator exports the unexported function isAggregator to package eth2exp_test.
func IsAggregator(ctx context.Context, eth2Cl eth2Provider, commLen uint64, slotSig eth2p0.BLSSignature) (bool, error) {
return isAggregator(ctx, eth2Cl, commLen, slotSig)
}
66 changes: 34 additions & 32 deletions eth2util/eth2exp/attagg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,7 @@ func TestCalculateCommitteeSubscriptionResponse(t *testing.T) {
}

t.Run("aggregator", func(t *testing.T) {
// https://github.com/prysmaticlabs/prysm/blob/8627fe72e80009ae162430140bcfff6f209d7a32/beacon-chain/core/helpers/attestation_test.go#L28
sig, err := hex.DecodeString("8776a37d6802c4797d113169c5fcfda50e68a32058eb6356a6f00d06d7da64c841a00c7c38b9b94a204751eca53707bd03523ce4797827d9bacff116a6e776a20bbccff4b683bf5201b610797ed0502557a58a65c8395f8a1649b976c3112d15")
require.NoError(t, err)
blsSig, err := tblsconv.SigFromBytes(sig)
require.NoError(t, err)
subscription.SlotSignature = tblsconv.SigToETH2(blsSig)

// https://github.com/prysmaticlabs/prysm/blob/8627fe72e80009ae162430140bcfff6f209d7a32/beacon-chain/core/helpers/attestation_test.go#L26
commLen := 3
commLen := 43
bmock.BeaconCommitteesAtEpochFunc = func(_ context.Context, _ string, _ eth2p0.Epoch) ([]*eth2v1.BeaconCommittee, error) {
return []*eth2v1.BeaconCommittee{beaconCommittee(commLen)}, nil
}
Expand All @@ -85,54 +77,64 @@ func TestCalculateCommitteeSubscriptionResponse(t *testing.T) {
require.NoError(t, err)
require.Equal(t, resp.ValidatorIndex, subscription.ValidatorIndex)
require.True(t, resp.IsAggregator)
})

// https://github.com/prysmaticlabs/prysm/blob/fc509cc220a82efd555704d41aa362903a06ab9e/beacon-chain/core/helpers/attestation_test.go#L39
commLen = 64
t.Run("not an aggregator", func(t *testing.T) {
commLen := 61
bmock.BeaconCommitteesAtEpochFunc = func(_ context.Context, _ string, _ eth2p0.Epoch) ([]*eth2v1.BeaconCommittee, error) {
return []*eth2v1.BeaconCommittee{beaconCommittee(commLen)}, nil
}

resp, err = eth2exp.CalculateCommitteeSubscriptionResponse(ctx, bmock, &subscription)
resp, err := eth2exp.CalculateCommitteeSubscriptionResponse(ctx, bmock, &subscription)
require.NoError(t, err)
require.Equal(t, resp.ValidatorIndex, subscription.ValidatorIndex)
require.False(t, resp.IsAggregator)
})
}

t.Run("is aggregator", func(t *testing.T) {
commLen := 43
bmock.BeaconCommitteesAtEpochFunc = func(_ context.Context, _ string, _ eth2p0.Epoch) ([]*eth2v1.BeaconCommittee, error) {
return []*eth2v1.BeaconCommittee{beaconCommittee(commLen)}, nil
}
func TestIsAggregator(t *testing.T) {
ctx := context.Background()

resp, err := eth2exp.CalculateCommitteeSubscriptionResponse(ctx, bmock, &subscription)
bmock, err := beaconmock.New()
require.NoError(t, err)

// https://github.com/prysmaticlabs/prysm/blob/8627fe72e80009ae162430140bcfff6f209d7a32/beacon-chain/core/helpers/attestation_test.go#L28
sig, err := hex.DecodeString("8776a37d6802c4797d113169c5fcfda50e68a32058eb6356a6f00d06d7da64c841a00c7c38b9b94a204751eca53707bd03523ce4797827d9bacff116a6e776a20bbccff4b683bf5201b610797ed0502557a58a65c8395f8a1649b976c3112d15")
require.NoError(t, err)
blsSig, err := tblsconv.SigFromBytes(sig)
require.NoError(t, err)

t.Run("aggregator", func(t *testing.T) {
// https://github.com/prysmaticlabs/prysm/blob/8627fe72e80009ae162430140bcfff6f209d7a32/beacon-chain/core/helpers/attestation_test.go#L26
commLen := uint64(3)
isAgg, err := eth2exp.IsAggregator(ctx, bmock, commLen, tblsconv.SigToETH2(blsSig))
require.NoError(t, err)
require.Equal(t, resp.ValidatorIndex, subscription.ValidatorIndex)
require.True(t, resp.IsAggregator)
require.True(t, isAgg)
})

t.Run("is not aggregator", func(t *testing.T) {
commLen := 61
bmock.BeaconCommitteesAtEpochFunc = func(_ context.Context, _ string, _ eth2p0.Epoch) ([]*eth2v1.BeaconCommittee, error) {
return []*eth2v1.BeaconCommittee{beaconCommittee(commLen)}, nil
}

resp, err := eth2exp.CalculateCommitteeSubscriptionResponse(ctx, bmock, &subscription)
t.Run("not an aggregator", func(t *testing.T) {
// https://github.com/prysmaticlabs/prysm/blob/fc509cc220a82efd555704d41aa362903a06ab9e/beacon-chain/core/helpers/attestation_test.go#L39
commLen := uint64(64)
isAgg, err := eth2exp.IsAggregator(ctx, bmock, commLen, tblsconv.SigToETH2(blsSig))
require.NoError(t, err)
require.Equal(t, resp.ValidatorIndex, subscription.ValidatorIndex)
require.False(t, resp.IsAggregator)
require.False(t, isAgg)
})
}

// beaconCommittees returns a BeaconCommittee with the list of commLen validator indexes.
func beaconCommittee(commLen int) *eth2v1.BeaconCommittee {
var vals []eth2p0.ValidatorIndex
var (
slot = eth2p0.Slot(1)
commIdx = eth2p0.CommitteeIndex(1)
vals []eth2p0.ValidatorIndex
)
for idx := 1; idx <= commLen; idx++ {
vals = append(vals, eth2p0.ValidatorIndex(idx))
}

return &eth2v1.BeaconCommittee{
Slot: eth2p0.Slot(1),
Index: eth2p0.CommitteeIndex(1),
Slot: slot,
Index: commIdx,
Validators: vals,
}
}

0 comments on commit 50e6b5c

Please sign in to comment.