Skip to content

Commit

Permalink
Add tests and fix linter
Browse files Browse the repository at this point in the history
  • Loading branch information
KaloyanTanev committed Feb 5, 2025
1 parent 8743c27 commit a4c750f
Show file tree
Hide file tree
Showing 12 changed files with 181 additions and 90 deletions.
8 changes: 4 additions & 4 deletions app/eth2wrap/httpwrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
"github.com/obolnetwork/charon/app/z"
"github.com/obolnetwork/charon/eth2util"
"github.com/obolnetwork/charon/eth2util/eth2exp"
"github.com/obolnetwork/charon/eth2util/statecommittees"
"github.com/obolnetwork/charon/eth2util/statecomm"
)

// BlockAttestationsProvider is the interface for providing attestations included in blocks.
Expand All @@ -43,7 +43,7 @@ type BlockAttestationsProvider interface {
// It is a standard beacon API endpoint not implemented by eth2client.
// See https://ethereum.github.io/beacon-APIs/#/Beacon/getEpochCommittees.
type BeaconStateCommitteesProvider interface {
BeaconStateCommittees(ctx context.Context, slot uint64) ([]*statecommittees.StateCommittee, error)
BeaconStateCommittees(ctx context.Context, slot uint64) ([]*statecomm.StateCommittee, error)
}

// NodePeerCountProvider is the interface for providing node peer count.
Expand Down Expand Up @@ -298,7 +298,7 @@ func (h *httpAdapter) BlockAttestationsV2(ctx context.Context, stateID string) (

// BeaconStateCommittees returns the attestations included in the requested block.
// See https://ethereum.github.io/beacon-APIs/#/Beacon/getStateValidators.
func (h *httpAdapter) BeaconStateCommittees(ctx context.Context, slot uint64) ([]*statecommittees.StateCommittee, error) {
func (h *httpAdapter) BeaconStateCommittees(ctx context.Context, slot uint64) ([]*statecomm.StateCommittee, error) {
r := strconv.FormatUint(slot, 10)
path := fmt.Sprintf("/eth/v1/beacon/states/%v/committees", r)
queryParams := map[string]string{
Expand All @@ -313,7 +313,7 @@ func (h *httpAdapter) BeaconStateCommittees(ctx context.Context, slot uint64) ([
return nil, errors.New("request state committees for slot failed", z.Int("status", statusCode), z.U64("slot", slot))
}

Check warning on line 314 in app/eth2wrap/httpwrap.go

View check run for this annotation

Codecov / codecov/patch

app/eth2wrap/httpwrap.go#L312-L314

Added lines #L312 - L314 were not covered by tests

var res statecommittees.StateCommitteesResponse
var res statecomm.StateCommitteesResponse
err = json.Unmarshal(respBody, &res)
if err != nil {
return nil, errors.Wrap(err, "unmarshal state committees", z.Int("status", statusCode), z.U64("slot", slot))
Expand Down
4 changes: 2 additions & 2 deletions app/eth2wrap/lazy.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
eth2p0 "github.com/attestantio/go-eth2-client/spec/phase0"

"github.com/obolnetwork/charon/eth2util/eth2exp"
"github.com/obolnetwork/charon/eth2util/statecommittees"
"github.com/obolnetwork/charon/eth2util/statecomm"
)

//go:generate mockery --name=Client --output=mocks --outpkg=mocks --case=underscore
Expand Down Expand Up @@ -213,7 +213,7 @@ func (l *lazy) BlockAttestationsV2(ctx context.Context, stateID string) ([]*spec
return cl.BlockAttestationsV2(ctx, stateID)
}

func (l *lazy) BeaconStateCommittees(ctx context.Context, slot uint64) ([]*statecommittees.StateCommittee, error) {
func (l *lazy) BeaconStateCommittees(ctx context.Context, slot uint64) ([]*statecomm.StateCommittee, error) {
cl, err := l.getOrCreateClient(ctx)
if err != nil {
return nil, err
Expand Down
12 changes: 6 additions & 6 deletions app/eth2wrap/mocks/client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions app/eth2wrap/multi.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
eth2p0 "github.com/attestantio/go-eth2-client/spec/phase0"

"github.com/obolnetwork/charon/eth2util/eth2exp"
"github.com/obolnetwork/charon/eth2util/statecommittees"
"github.com/obolnetwork/charon/eth2util/statecomm"
)

// NewMultiForT creates a new mutil client for testing.
Expand Down Expand Up @@ -214,12 +214,12 @@ func (m multi) BlockAttestationsV2(ctx context.Context, stateID string) ([]*spec
return res, err
}

func (m multi) BeaconStateCommittees(ctx context.Context, slot uint64) ([]*statecommittees.StateCommittee, error) {
func (m multi) BeaconStateCommittees(ctx context.Context, slot uint64) ([]*statecomm.StateCommittee, error) {
const label = "beacon_state_committees"
defer latency(ctx, label, false)()

res, err := provide(ctx, m.clients, m.fallbacks,
func(ctx context.Context, args provideArgs) ([]*statecommittees.StateCommittee, error) {
func(ctx context.Context, args provideArgs) ([]*statecomm.StateCommittee, error) {
return args.client.BeaconStateCommittees(ctx, slot)
},

Check warning on line 224 in app/eth2wrap/multi.go

View check run for this annotation

Codecov / codecov/patch

app/eth2wrap/multi.go#L217-L224

Added lines #L217 - L224 were not covered by tests
nil, m.selector,
Expand Down
14 changes: 7 additions & 7 deletions core/signeddata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ func TestSignedDataSetSignature(t *testing.T) {
Version: eth2spec.DataVersionPhase0,
Phase0: &eth2p0.Attestation{
AggregationBits: testutil.RandomBitList(1),
Data: testutil.RandomAttestationData(),
Data: testutil.RandomAttestationDataPhase0(),
Signature: testutil.RandomEth2Signature(),
},
},
Expand All @@ -285,7 +285,7 @@ func TestSignedDataSetSignature(t *testing.T) {
Version: eth2spec.DataVersionAltair,
Altair: &eth2p0.Attestation{
AggregationBits: testutil.RandomBitList(1),
Data: testutil.RandomAttestationData(),
Data: testutil.RandomAttestationDataPhase0(),
Signature: testutil.RandomEth2Signature(),
},
},
Expand All @@ -298,7 +298,7 @@ func TestSignedDataSetSignature(t *testing.T) {
Version: eth2spec.DataVersionBellatrix,
Bellatrix: &eth2p0.Attestation{
AggregationBits: testutil.RandomBitList(1),
Data: testutil.RandomAttestationData(),
Data: testutil.RandomAttestationDataPhase0(),
Signature: testutil.RandomEth2Signature(),
},
},
Expand All @@ -311,7 +311,7 @@ func TestSignedDataSetSignature(t *testing.T) {
Version: eth2spec.DataVersionCapella,
Capella: &eth2p0.Attestation{
AggregationBits: testutil.RandomBitList(1),
Data: testutil.RandomAttestationData(),
Data: testutil.RandomAttestationDataPhase0(),
Signature: testutil.RandomEth2Signature(),
},
},
Expand All @@ -324,7 +324,7 @@ func TestSignedDataSetSignature(t *testing.T) {
Version: eth2spec.DataVersionDeneb,
Deneb: &eth2p0.Attestation{
AggregationBits: testutil.RandomBitList(1),
Data: testutil.RandomAttestationData(),
Data: testutil.RandomAttestationDataPhase0(),
Signature: testutil.RandomEth2Signature(),
},
},
Expand All @@ -337,7 +337,7 @@ func TestSignedDataSetSignature(t *testing.T) {
Version: eth2spec.DataVersionElectra,
Electra: &electra.Attestation{
AggregationBits: testutil.RandomBitList(1),
Data: testutil.RandomAttestationData(),
Data: testutil.RandomAttestationDataPhase0(),
Signature: testutil.RandomEth2Signature(),
CommitteeBits: testutil.RandomBitVec64(),
},
Expand Down Expand Up @@ -747,7 +747,7 @@ func TestVersionedSignedProposal(t *testing.T) {
}

func TestVersionedSignedAggregateAndProofUtilFunctions(t *testing.T) {
data := testutil.RandomAttestationData()
data := testutil.RandomAttestationDataPhase0()
aggregationBits := testutil.RandomBitList(64)
type testCase struct {
name string
Expand Down
24 changes: 12 additions & 12 deletions core/tracker/inclusion.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"github.com/obolnetwork/charon/app/log"
"github.com/obolnetwork/charon/app/z"
"github.com/obolnetwork/charon/core"
"github.com/obolnetwork/charon/eth2util/statecommittees"
"github.com/obolnetwork/charon/eth2util/statecomm"
)

const (
Expand Down Expand Up @@ -510,7 +510,7 @@ func (a *InclusionChecker) checkBlock(ctx context.Context, slot uint64) error {
attCommittee := &attCommittee{
Attestation: att,
}
err = a.conjugateAggregationBits(attCommittee, attsCommitteesMap, root, committeesForState)
err = conjugateAggregationBits(attCommittee, attsCommitteesMap, root, committeesForState)
if err != nil {
return err

Check warning on line 515 in core/tracker/inclusion.go

View check run for this annotation

Codecov / codecov/patch

core/tracker/inclusion.go#L515

Added line #L515 was not covered by tests
}
Expand Down Expand Up @@ -636,50 +636,50 @@ func setAttestationAggregationBits(att eth2spec.VersionedAttestation, bits bitfi
}
}

func (a *InclusionChecker) conjugateAggregationBits(att *attCommittee, attsMap map[eth2p0.Root]*attCommittee, root eth2p0.Root, committeesForState []*statecommittees.StateCommittee) error {
func conjugateAggregationBits(att *attCommittee, attsMap map[eth2p0.Root]*attCommittee, root eth2p0.Root, committeesForState []*statecomm.StateCommittee) error {
switch att.Attestation.Version {
case eth2spec.DataVersionPhase0:
if att.Attestation.Phase0 == nil {
return errors.New("no Phase0 attestation")
}

Check warning on line 644 in core/tracker/inclusion.go

View check run for this annotation

Codecov / codecov/patch

core/tracker/inclusion.go#L643-L644

Added lines #L643 - L644 were not covered by tests

return a.conjugateAggregationBitsPhase0(att, attsMap, root)
return conjugateAggregationBitsPhase0(att, attsMap, root)
case eth2spec.DataVersionAltair:
if att.Attestation.Altair == nil {
return errors.New("no Altair attestation")
}

Check warning on line 650 in core/tracker/inclusion.go

View check run for this annotation

Codecov / codecov/patch

core/tracker/inclusion.go#L649-L650

Added lines #L649 - L650 were not covered by tests

return a.conjugateAggregationBitsPhase0(att, attsMap, root)
return conjugateAggregationBitsPhase0(att, attsMap, root)
case eth2spec.DataVersionBellatrix:
if att.Attestation.Bellatrix == nil {
return errors.New("no Bellatrix attestation")
}

Check warning on line 656 in core/tracker/inclusion.go

View check run for this annotation

Codecov / codecov/patch

core/tracker/inclusion.go#L655-L656

Added lines #L655 - L656 were not covered by tests

return a.conjugateAggregationBitsPhase0(att, attsMap, root)
return conjugateAggregationBitsPhase0(att, attsMap, root)
case eth2spec.DataVersionCapella:
if att.Attestation.Capella == nil {
return errors.New("no Capella attestation")
}

Check warning on line 662 in core/tracker/inclusion.go

View check run for this annotation

Codecov / codecov/patch

core/tracker/inclusion.go#L661-L662

Added lines #L661 - L662 were not covered by tests

return a.conjugateAggregationBitsPhase0(att, attsMap, root)
return conjugateAggregationBitsPhase0(att, attsMap, root)
case eth2spec.DataVersionDeneb:
if att.Attestation.Deneb == nil {
return errors.New("no Deneb attestation")
}

Check warning on line 668 in core/tracker/inclusion.go

View check run for this annotation

Codecov / codecov/patch

core/tracker/inclusion.go#L667-L668

Added lines #L667 - L668 were not covered by tests

return a.conjugateAggregationBitsPhase0(att, attsMap, root)
return conjugateAggregationBitsPhase0(att, attsMap, root)
case eth2spec.DataVersionElectra:
if att.Attestation.Electra == nil {
return errors.New("no Electra attestation")
}

Check warning on line 674 in core/tracker/inclusion.go

View check run for this annotation

Codecov / codecov/patch

core/tracker/inclusion.go#L673-L674

Added lines #L673 - L674 were not covered by tests

return a.conjugateAggregationBitsElectra(att, attsMap, root, committeesForState)
return conjugateAggregationBitsElectra(att, attsMap, root, committeesForState)
default:
return errors.New("unknown attestation version", z.Str("version", att.Attestation.Version.String()))

Check warning on line 678 in core/tracker/inclusion.go

View check run for this annotation

Codecov / codecov/patch

core/tracker/inclusion.go#L677-L678

Added lines #L677 - L678 were not covered by tests
}
}

func (a *InclusionChecker) conjugateAggregationBitsPhase0(att *attCommittee, attsMap map[eth2p0.Root]*attCommittee, root eth2p0.Root) error {
func conjugateAggregationBitsPhase0(att *attCommittee, attsMap map[eth2p0.Root]*attCommittee, root eth2p0.Root) error {
attAggregationBits, err := att.Attestation.AggregationBits()
if err != nil {
return errors.Wrap(err, "get attestation aggregation bits")
Expand All @@ -704,7 +704,7 @@ func (a *InclusionChecker) conjugateAggregationBitsPhase0(att *attCommittee, att
return nil
}

func (a *InclusionChecker) conjugateAggregationBitsElectra(att *attCommittee, attsMap map[eth2p0.Root]*attCommittee, root eth2p0.Root, committeesForState []*statecommittees.StateCommittee) error {
func conjugateAggregationBitsElectra(att *attCommittee, attsMap map[eth2p0.Root]*attCommittee, root eth2p0.Root, committeesForState []*statecomm.StateCommittee) error {
fullAttestationAggregationBits, err := att.Attestation.AggregationBits()
if err != nil {
return err
Expand All @@ -717,7 +717,7 @@ func (a *InclusionChecker) conjugateAggregationBitsElectra(att *attCommittee, at
if exist, ok := attsMap[root]; ok {
updateAggregationBits(committeeBits, exist.CommitteeAggregations, fullAttestationAggregationBits)
} else {
// Create new empty map of committee indeces and aggregations per committee.
// Create new empty map of committee indices and aggregations per committee.
attsAggBits := make(map[eth2p0.CommitteeIndex]bitfield.Bitlist)
// Create a 0'ed bitlist of aggregations of size the amount of validators for all committees.
for _, comm := range committeesForState {
Expand Down
Loading

0 comments on commit a4c750f

Please sign in to comment.