Skip to content

Commit

Permalink
review
Browse files Browse the repository at this point in the history
  • Loading branch information
rkapka committed Jul 4, 2024
1 parent d53a296 commit c0e3963
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ package validator

import (
"bytes"
"context"
"sort"
"testing"

"github.com/prysmaticlabs/go-bitfield"
"github.com/prysmaticlabs/prysm/v5/beacon-chain/operations/attestations"
"github.com/prysmaticlabs/prysm/v5/config/params"
"github.com/prysmaticlabs/prysm/v5/consensus-types/primitives"
ethpb "github.com/prysmaticlabs/prysm/v5/proto/prysm/v1alpha1"
"github.com/prysmaticlabs/prysm/v5/testing/assert"
Expand Down Expand Up @@ -423,3 +426,106 @@ func TestProposer_ProposerAtts_dedup(t *testing.T) {
})
}
}

func Test_packAttestations(t *testing.T) {
ctx := context.Background()
phase0Att := &ethpb.Attestation{
AggregationBits: bitfield.Bitlist{0b11111},
Data: &ethpb.AttestationData{
BeaconBlockRoot: make([]byte, 32),
Source: &ethpb.Checkpoint{
Epoch: 0,
Root: make([]byte, 32),
},
Target: &ethpb.Checkpoint{
Epoch: 0,
Root: make([]byte, 32),
},
},
Signature: make([]byte, 96),
}
cb := primitives.NewAttestationCommitteeBits()
cb.SetBitAt(0, true)
electraAtt := &ethpb.AttestationElectra{
AggregationBits: bitfield.Bitlist{0b11111},
CommitteeBits: cb,
Data: &ethpb.AttestationData{
BeaconBlockRoot: make([]byte, 32),
Source: &ethpb.Checkpoint{
Epoch: 0,
Root: make([]byte, 32),
},
Target: &ethpb.Checkpoint{
Epoch: 0,
Root: make([]byte, 32),
},
},
Signature: make([]byte, 96),
}
pool := attestations.NewPool()
require.NoError(t, pool.SaveAggregatedAttestations([]ethpb.Att{phase0Att, electraAtt}))
s := &Server{AttPool: pool}

t.Run("Phase 0", func(t *testing.T) {
st, _ := util.DeterministicGenesisState(t, 64)
require.NoError(t, st.SetSlot(1))

atts, err := s.packAttestations(ctx, st, 0)
require.NoError(t, err)
require.Equal(t, 1, len(atts))
assert.DeepEqual(t, phase0Att, atts[0])
})
t.Run("Electra", func(t *testing.T) {
params.SetupTestConfigCleanup(t)
cfg := params.BeaconConfig().Copy()
cfg.ElectraForkEpoch = 1
params.OverrideBeaconConfig(cfg)

st, _ := util.DeterministicGenesisStateElectra(t, 64)
require.NoError(t, st.SetSlot(params.BeaconConfig().SlotsPerEpoch+1))

atts, err := s.packAttestations(ctx, st, params.BeaconConfig().SlotsPerEpoch)
require.NoError(t, err)
require.Equal(t, 1, len(atts))
assert.DeepEqual(t, electraAtt, atts[0])
})
}

func Test_limitToMaxAttestations(t *testing.T) {
t.Run("Phase 0", func(t *testing.T) {
atts := make([]ethpb.Att, params.BeaconConfig().MaxAttestations+1)
for i := range atts {
atts[i] = &ethpb.Attestation{}
}

// 1 less than limit
pAtts := proposerAtts(atts[:len(atts)-3])
assert.Equal(t, len(pAtts), len(pAtts.limitToMaxAttestations()))

// equal to limit
pAtts = atts[:len(atts)-2]
assert.Equal(t, len(pAtts), len(pAtts.limitToMaxAttestations()))

// 1 more than limit
pAtts = atts
assert.Equal(t, len(pAtts)-1, len(pAtts.limitToMaxAttestations()))
})
t.Run("Electra", func(t *testing.T) {
atts := make([]ethpb.Att, params.BeaconConfig().MaxAttestationsElectra+1)
for i := range atts {
atts[i] = &ethpb.AttestationElectra{}
}

// 1 less than limit
pAtts := proposerAtts(atts[:len(atts)-3])
assert.Equal(t, len(pAtts), len(pAtts.limitToMaxAttestations()))

// equal to limit
pAtts = atts[:len(atts)-2]
assert.Equal(t, len(pAtts), len(pAtts.limitToMaxAttestations()))

// 1 more than limit
pAtts = atts
assert.Equal(t, len(pAtts)-1, len(pAtts.limitToMaxAttestations()))
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,5 @@ import (
)

func TestMainnet_Electra_Forkchoice(t *testing.T) {
t.Skip("TODO: Electra")
forkchoice.Run(t, "mainnet", version.Electra)
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,5 @@ import (
)

func TestMinimal_Electra_Forkchoice(t *testing.T) {
t.Skip("TODO: Electra")
forkchoice.Run(t, "minimal", version.Electra)
}
28 changes: 10 additions & 18 deletions validator/client/attest.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,26 +193,18 @@ func (v *validator) SubmitAttestation(ctx context.Context, slot primitives.Slot,
return
}

span.AddAttributes(
trace.Int64Attribute("slot", int64(slot)), // lint:ignore uintcast -- This conversion is OK for tracing.
trace.StringAttribute("attestationHash", fmt.Sprintf("%#x", attResp.AttestationDataRoot)),
trace.StringAttribute("blockRoot", fmt.Sprintf("%#x", data.BeaconBlockRoot)),
trace.Int64Attribute("justifiedEpoch", int64(data.Source.Epoch)),
trace.Int64Attribute("targetEpoch", int64(data.Target.Epoch)),
trace.StringAttribute("aggregationBitfield", fmt.Sprintf("%#x", aggregationBitfield)),
)
if postElectra {
span.AddAttributes(
trace.Int64Attribute("slot", int64(slot)), // lint:ignore uintcast -- This conversion is OK for tracing.
trace.StringAttribute("attestationHash", fmt.Sprintf("%#x", attResp.AttestationDataRoot)),
trace.StringAttribute("committeeBitfield", fmt.Sprintf("%#x", committeeBits)),
trace.StringAttribute("blockRoot", fmt.Sprintf("%#x", data.BeaconBlockRoot)),
trace.Int64Attribute("justifiedEpoch", int64(data.Source.Epoch)),
trace.Int64Attribute("targetEpoch", int64(data.Target.Epoch)),
trace.StringAttribute("aggregationBitfield", fmt.Sprintf("%#x", aggregationBitfield)),
)
span.AddAttributes(trace.StringAttribute("committeeBitfield", fmt.Sprintf("%#x", committeeBits)))
} else {
span.AddAttributes(
trace.Int64Attribute("slot", int64(slot)), // lint:ignore uintcast -- This conversion is OK for tracing.
trace.StringAttribute("attestationHash", fmt.Sprintf("%#x", attResp.AttestationDataRoot)),
trace.Int64Attribute("committeeIndex", int64(data.CommitteeIndex)),
trace.StringAttribute("blockRoot", fmt.Sprintf("%#x", data.BeaconBlockRoot)),
trace.Int64Attribute("justifiedEpoch", int64(data.Source.Epoch)),
trace.Int64Attribute("targetEpoch", int64(data.Target.Epoch)),
trace.StringAttribute("aggregationBitfield", fmt.Sprintf("%#x", aggregationBitfield)),
)
span.AddAttributes(trace.Int64Attribute("committeeIndex", int64(data.CommitteeIndex)))
}

if v.emitAccountMetrics {
Expand Down

0 comments on commit c0e3963

Please sign in to comment.