Compare commits

...

7 Commits

Author SHA1 Message Date
nisdas
8a3c0e4dfb Fix Tests 2024-06-19 16:17:59 +08:00
nisdas
df2b47213a Fix Tests 2024-06-19 13:49:07 +08:00
nisdas
e9250e1d0d Nogo 2024-06-19 13:30:03 +08:00
nisdas
3efbc9d001 Gazelle 2024-06-19 13:24:03 +08:00
nisdas
46acb4210f Merge branch 'develop' of https://github.com/prysmaticlabs/geth-sharding into processSlotsNew 2024-06-19 13:16:25 +08:00
nisdas
a16ab9b439 Fix Mocks 2024-06-19 13:15:00 +08:00
nisdas
83fe9bc2f2 Pick out changes into develop 2024-06-17 17:00:44 +08:00
8 changed files with 54 additions and 14 deletions

View File

@@ -6,6 +6,7 @@ import (
"time" "time"
"github.com/pkg/errors" "github.com/pkg/errors"
coreState "github.com/prysmaticlabs/prysm/v5/beacon-chain/core/transition"
"go.opencensus.io/trace" "go.opencensus.io/trace"
"github.com/prysmaticlabs/prysm/v5/beacon-chain/core/helpers" "github.com/prysmaticlabs/prysm/v5/beacon-chain/core/helpers"
@@ -71,6 +72,7 @@ type HeadFetcher interface {
HeadState(ctx context.Context) (state.BeaconState, error) HeadState(ctx context.Context) (state.BeaconState, error)
HeadStateReadOnly(ctx context.Context) (state.ReadOnlyBeaconState, error) HeadStateReadOnly(ctx context.Context) (state.ReadOnlyBeaconState, error)
HeadValidatorsIndices(ctx context.Context, epoch primitives.Epoch) ([]primitives.ValidatorIndex, error) HeadValidatorsIndices(ctx context.Context, epoch primitives.Epoch) ([]primitives.ValidatorIndex, error)
HeadValidatorsIndicesFromAdvancedSlots(ctx context.Context, slot primitives.Slot) ([]primitives.ValidatorIndex, error)
HeadGenesisValidatorsRoot() [32]byte HeadGenesisValidatorsRoot() [32]byte
HeadETH1Data() *ethpb.Eth1Data HeadETH1Data() *ethpb.Eth1Data
HeadPublicKeyToValidatorIndex(pubKey [fieldparams.BLSPubkeyLength]byte) (primitives.ValidatorIndex, bool) HeadPublicKeyToValidatorIndex(pubKey [fieldparams.BLSPubkeyLength]byte) (primitives.ValidatorIndex, bool)
@@ -244,6 +246,22 @@ func (s *Service) HeadValidatorsIndices(ctx context.Context, epoch primitives.Ep
return helpers.ActiveValidatorIndices(ctx, s.headState(ctx), epoch) return helpers.ActiveValidatorIndices(ctx, s.headState(ctx), epoch)
} }
// HeadValidatorsIndicesFromAdvancedSlots returns a list of active validator indices from the head view of a given epoch.
func (s *Service) HeadValidatorsIndicesFromAdvancedSlots(ctx context.Context, slot primitives.Slot) ([]primitives.ValidatorIndex, error) {
s.headLock.RLock()
defer s.headLock.RUnlock()
if !s.hasHeadState() {
return []primitives.ValidatorIndex{}, nil
}
rt := s.headRoot()
st, err := coreState.ProcessSlotsUsingNextSlotCache(ctx, s.headState(ctx), rt[:], slot)
if err != nil {
return nil, err
}
return helpers.ActiveValidatorIndices(ctx, st, slots.ToEpoch(slot))
}
// HeadGenesisValidatorsRoot returns genesis validators root of the head state. // HeadGenesisValidatorsRoot returns genesis validators root of the head state.
func (s *Service) HeadGenesisValidatorsRoot() [32]byte { func (s *Service) HeadGenesisValidatorsRoot() [32]byte {
s.headLock.RLock() s.headLock.RLock()

View File

@@ -31,6 +31,7 @@ go_library(
"//encoding/bytesutil:go_default_library", "//encoding/bytesutil:go_default_library",
"//proto/engine/v1:go_default_library", "//proto/engine/v1:go_default_library",
"//proto/prysm/v1alpha1:go_default_library", "//proto/prysm/v1alpha1:go_default_library",
"//time/slots:go_default_library",
"@com_github_pkg_errors//:go_default_library", "@com_github_pkg_errors//:go_default_library",
"@com_github_sirupsen_logrus//:go_default_library", "@com_github_sirupsen_logrus//:go_default_library",
], ],

View File

@@ -30,6 +30,7 @@ import (
"github.com/prysmaticlabs/prysm/v5/encoding/bytesutil" "github.com/prysmaticlabs/prysm/v5/encoding/bytesutil"
enginev1 "github.com/prysmaticlabs/prysm/v5/proto/engine/v1" enginev1 "github.com/prysmaticlabs/prysm/v5/proto/engine/v1"
ethpb "github.com/prysmaticlabs/prysm/v5/proto/prysm/v1alpha1" ethpb "github.com/prysmaticlabs/prysm/v5/proto/prysm/v1alpha1"
"github.com/prysmaticlabs/prysm/v5/time/slots"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
) )
@@ -346,6 +347,13 @@ func (s *ChainService) HeadValidatorsIndices(ctx context.Context, epoch primitiv
return helpers.ActiveValidatorIndices(ctx, s.State, epoch) return helpers.ActiveValidatorIndices(ctx, s.State, epoch)
} }
func (s *ChainService) HeadValidatorsIndicesFromAdvancedSlots(ctx context.Context, slot primitives.Slot) ([]primitives.ValidatorIndex, error) {
if s.State == nil {
return []primitives.ValidatorIndex{}, nil
}
return helpers.ActiveValidatorIndices(ctx, s.State, slots.ToEpoch(slot))
}
// HeadETH1Data provides the current ETH1Data of the head state. // HeadETH1Data provides the current ETH1Data of the head state.
func (s *ChainService) HeadETH1Data() *ethpb.Eth1Data { func (s *ChainService) HeadETH1Data() *ethpb.Eth1Data {
return s.ETH1Data return s.ETH1Data

View File

@@ -147,8 +147,7 @@ func (s *Server) SubmitAttestations(w http.ResponseWriter, r *http.Request) {
failedBroadcasts := make([]string, 0) failedBroadcasts := make([]string, 0)
for i, att := range validAttestations { for i, att := range validAttestations {
// Determine subnet to broadcast attestation to // Determine subnet to broadcast attestation to
wantedEpoch := slots.ToEpoch(att.Data.Slot) vals, err := s.HeadFetcher.HeadValidatorsIndicesFromAdvancedSlots(ctx, att.Data.Slot)
vals, err := s.HeadFetcher.HeadValidatorsIndices(ctx, wantedEpoch)
if err != nil { if err != nil {
httputil.HandleError(w, "Could not get head validator indices: "+err.Error(), http.StatusInternalServerError) httputil.HandleError(w, "Could not get head validator indices: "+err.Error(), http.StatusInternalServerError)
return return

View File

@@ -360,8 +360,7 @@ func (s *Server) SubmitBeaconCommitteeSubscription(w http.ResponseWriter, r *htt
} }
fetchValsLen := func(slot primitives.Slot) (uint64, error) { fetchValsLen := func(slot primitives.Slot) (uint64, error) {
wantedEpoch := slots.ToEpoch(slot) vals, err := s.HeadFetcher.HeadValidatorsIndicesFromAdvancedSlots(ctx, slot)
vals, err := s.HeadFetcher.HeadValidatorsIndices(ctx, wantedEpoch)
if err != nil { if err != nil {
return 0, err return 0, err
} }

View File

@@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"github.com/prysmaticlabs/prysm/v5/beacon-chain/core/helpers" "github.com/prysmaticlabs/prysm/v5/beacon-chain/core/helpers"
"github.com/prysmaticlabs/prysm/v5/beacon-chain/core/transition"
"github.com/prysmaticlabs/prysm/v5/beacon-chain/rpc/core" "github.com/prysmaticlabs/prysm/v5/beacon-chain/rpc/core"
"github.com/prysmaticlabs/prysm/v5/config/params" "github.com/prysmaticlabs/prysm/v5/config/params"
"github.com/prysmaticlabs/prysm/v5/encoding/bytesutil" "github.com/prysmaticlabs/prysm/v5/encoding/bytesutil"
@@ -32,10 +33,18 @@ func (vs *Server) SubmitAggregateSelectionProof(ctx context.Context, req *ethpb.
return nil, err return nil, err
} }
st, err := vs.HeadFetcher.HeadStateReadOnly(ctx) st, err := vs.HeadFetcher.HeadState(ctx)
if err != nil { if err != nil {
return nil, status.Errorf(codes.Internal, "Could not determine head state: %v", err) return nil, status.Errorf(codes.Internal, "Could not determine head state: %v", err)
} }
rt, err := vs.HeadFetcher.HeadRoot(ctx)
if err != nil {
return nil, status.Errorf(codes.Internal, "Could not determine head root: %v", err)
}
st, err = transition.ProcessSlotsUsingNextSlotCache(ctx, st, rt, req.Slot)
if err != nil {
return nil, err
}
validatorIndex, exists := st.ValidatorIndexByPubkey(bytesutil.ToBytes48(req.PublicKey)) validatorIndex, exists := st.ValidatorIndexByPubkey(bytesutil.ToBytes48(req.PublicKey))
if !exists { if !exists {

View File

@@ -109,6 +109,7 @@ func TestSubmitAggregateAndProof_UnaggregateOk(t *testing.T) {
beaconState, privKeys := util.DeterministicGenesisState(t, 32) beaconState, privKeys := util.DeterministicGenesisState(t, 32)
att0, err := generateUnaggregatedAtt(beaconState, 0, privKeys) att0, err := generateUnaggregatedAtt(beaconState, 0, privKeys)
att0.Data.Slot += params.BeaconConfig().MinAttestationInclusionDelay
require.NoError(t, err) require.NoError(t, err)
err = beaconState.SetSlot(beaconState.Slot() + params.BeaconConfig().MinAttestationInclusionDelay) err = beaconState.SetSlot(beaconState.Slot() + params.BeaconConfig().MinAttestationInclusionDelay)
require.NoError(t, err) require.NoError(t, err)
@@ -127,7 +128,7 @@ func TestSubmitAggregateAndProof_UnaggregateOk(t *testing.T) {
v, err := beaconState.ValidatorAtIndex(1) v, err := beaconState.ValidatorAtIndex(1)
require.NoError(t, err) require.NoError(t, err)
pubKey := v.PublicKey pubKey := v.PublicKey
req := &ethpb.AggregateSelectionRequest{CommitteeIndex: 1, SlotSignature: sig.Marshal(), PublicKey: pubKey} req := &ethpb.AggregateSelectionRequest{Slot: beaconState.Slot(), CommitteeIndex: 1, SlotSignature: sig.Marshal(), PublicKey: pubKey}
require.NoError(t, aggregatorServer.AttPool.SaveUnaggregatedAttestation(att0)) require.NoError(t, aggregatorServer.AttPool.SaveUnaggregatedAttestation(att0))
_, err = aggregatorServer.SubmitAggregateSelectionProof(ctx, req) _, err = aggregatorServer.SubmitAggregateSelectionProof(ctx, req)
@@ -144,8 +145,10 @@ func TestSubmitAggregateAndProof_AggregateOk(t *testing.T) {
beaconState, privKeys := util.DeterministicGenesisState(t, 32) beaconState, privKeys := util.DeterministicGenesisState(t, 32)
att0, err := generateAtt(beaconState, 0, privKeys) att0, err := generateAtt(beaconState, 0, privKeys)
att0.Data.Slot += params.BeaconConfig().MinAttestationInclusionDelay
require.NoError(t, err) require.NoError(t, err)
att1, err := generateAtt(beaconState, 2, privKeys) att1, err := generateAtt(beaconState, 2, privKeys)
att1.Data.Slot += params.BeaconConfig().MinAttestationInclusionDelay
require.NoError(t, err) require.NoError(t, err)
err = beaconState.SetSlot(beaconState.Slot() + params.BeaconConfig().MinAttestationInclusionDelay) err = beaconState.SetSlot(beaconState.Slot() + params.BeaconConfig().MinAttestationInclusionDelay)
@@ -165,7 +168,7 @@ func TestSubmitAggregateAndProof_AggregateOk(t *testing.T) {
v, err := beaconState.ValidatorAtIndex(1) v, err := beaconState.ValidatorAtIndex(1)
require.NoError(t, err) require.NoError(t, err)
pubKey := v.PublicKey pubKey := v.PublicKey
req := &ethpb.AggregateSelectionRequest{CommitteeIndex: 1, SlotSignature: sig.Marshal(), PublicKey: pubKey} req := &ethpb.AggregateSelectionRequest{Slot: beaconState.Slot(), CommitteeIndex: 1, SlotSignature: sig.Marshal(), PublicKey: pubKey}
require.NoError(t, aggregatorServer.AttPool.SaveAggregatedAttestation(att0)) require.NoError(t, aggregatorServer.AttPool.SaveAggregatedAttestation(att0))
require.NoError(t, aggregatorServer.AttPool.SaveAggregatedAttestation(att1)) require.NoError(t, aggregatorServer.AttPool.SaveAggregatedAttestation(att1))
@@ -205,7 +208,7 @@ func TestSubmitAggregateAndProof_AggregateNotOk(t *testing.T) {
v, err := beaconState.ValidatorAtIndex(1) v, err := beaconState.ValidatorAtIndex(1)
require.NoError(t, err) require.NoError(t, err)
pubKey := v.PublicKey pubKey := v.PublicKey
req := &ethpb.AggregateSelectionRequest{CommitteeIndex: 1, SlotSignature: sig.Marshal(), PublicKey: pubKey} req := &ethpb.AggregateSelectionRequest{Slot: beaconState.Slot(), CommitteeIndex: 1, SlotSignature: sig.Marshal(), PublicKey: pubKey}
_, err = aggregatorServer.SubmitAggregateSelectionProof(ctx, req) _, err = aggregatorServer.SubmitAggregateSelectionProof(ctx, req)
assert.ErrorContains(t, "Could not find attestation for slot and committee in pool", err) assert.ErrorContains(t, "Could not find attestation for slot and committee in pool", err)
@@ -308,14 +311,17 @@ func TestSubmitAggregateAndProof_PreferOwnAttestation(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
att0.Data.BeaconBlockRoot = bytesutil.PadTo([]byte("foo"), fieldparams.RootLength) att0.Data.BeaconBlockRoot = bytesutil.PadTo([]byte("foo"), fieldparams.RootLength)
att0.AggregationBits = bitfield.Bitlist{0b11100} att0.AggregationBits = bitfield.Bitlist{0b11100}
att0.Data.Slot += params.BeaconConfig().MinAttestationInclusionDelay
att1, err := generateAtt(beaconState, 0, privKeys) att1, err := generateAtt(beaconState, 0, privKeys)
require.NoError(t, err) require.NoError(t, err)
att1.Data.BeaconBlockRoot = bytesutil.PadTo([]byte("bar"), fieldparams.RootLength) att1.Data.BeaconBlockRoot = bytesutil.PadTo([]byte("bar"), fieldparams.RootLength)
att1.AggregationBits = bitfield.Bitlist{0b11001} att1.AggregationBits = bitfield.Bitlist{0b11001}
att1.Data.Slot += params.BeaconConfig().MinAttestationInclusionDelay
att2, err := generateAtt(beaconState, 2, privKeys) att2, err := generateAtt(beaconState, 2, privKeys)
require.NoError(t, err) require.NoError(t, err)
att2.Data.BeaconBlockRoot = bytesutil.PadTo([]byte("foo"), fieldparams.RootLength) att2.Data.BeaconBlockRoot = bytesutil.PadTo([]byte("foo"), fieldparams.RootLength)
att2.AggregationBits = bitfield.Bitlist{0b11110} att2.AggregationBits = bitfield.Bitlist{0b11110}
att2.Data.Slot += params.BeaconConfig().MinAttestationInclusionDelay
err = beaconState.SetSlot(beaconState.Slot() + params.BeaconConfig().MinAttestationInclusionDelay) err = beaconState.SetSlot(beaconState.Slot() + params.BeaconConfig().MinAttestationInclusionDelay)
require.NoError(t, err) require.NoError(t, err)
@@ -334,7 +340,7 @@ func TestSubmitAggregateAndProof_PreferOwnAttestation(t *testing.T) {
v, err := beaconState.ValidatorAtIndex(1) v, err := beaconState.ValidatorAtIndex(1)
require.NoError(t, err) require.NoError(t, err)
pubKey := v.PublicKey pubKey := v.PublicKey
req := &ethpb.AggregateSelectionRequest{CommitteeIndex: 1, SlotSignature: sig.Marshal(), PublicKey: pubKey} req := &ethpb.AggregateSelectionRequest{Slot: beaconState.Slot(), CommitteeIndex: 1, SlotSignature: sig.Marshal(), PublicKey: pubKey}
err = aggregatorServer.AttPool.SaveAggregatedAttestations([]ethpb.Att{ err = aggregatorServer.AttPool.SaveAggregatedAttestations([]ethpb.Att{
att0, att0,
@@ -363,10 +369,12 @@ func TestSubmitAggregateAndProof_SelectsMostBitsWhenOwnAttestationNotPresent(t *
require.NoError(t, err) require.NoError(t, err)
att0.Data.BeaconBlockRoot = bytesutil.PadTo([]byte("foo"), fieldparams.RootLength) att0.Data.BeaconBlockRoot = bytesutil.PadTo([]byte("foo"), fieldparams.RootLength)
att0.AggregationBits = bitfield.Bitlist{0b11100} att0.AggregationBits = bitfield.Bitlist{0b11100}
att0.Data.Slot += params.BeaconConfig().MinAttestationInclusionDelay
att1, err := generateAtt(beaconState, 2, privKeys) att1, err := generateAtt(beaconState, 2, privKeys)
require.NoError(t, err) require.NoError(t, err)
att1.Data.BeaconBlockRoot = bytesutil.PadTo([]byte("bar"), fieldparams.RootLength) att1.Data.BeaconBlockRoot = bytesutil.PadTo([]byte("bar"), fieldparams.RootLength)
att1.AggregationBits = bitfield.Bitlist{0b11110} att1.AggregationBits = bitfield.Bitlist{0b11110}
att1.Data.Slot += params.BeaconConfig().MinAttestationInclusionDelay
err = beaconState.SetSlot(beaconState.Slot() + params.BeaconConfig().MinAttestationInclusionDelay) err = beaconState.SetSlot(beaconState.Slot() + params.BeaconConfig().MinAttestationInclusionDelay)
require.NoError(t, err) require.NoError(t, err)
@@ -385,7 +393,7 @@ func TestSubmitAggregateAndProof_SelectsMostBitsWhenOwnAttestationNotPresent(t *
v, err := beaconState.ValidatorAtIndex(1) v, err := beaconState.ValidatorAtIndex(1)
require.NoError(t, err) require.NoError(t, err)
pubKey := v.PublicKey pubKey := v.PublicKey
req := &ethpb.AggregateSelectionRequest{CommitteeIndex: 1, SlotSignature: sig.Marshal(), PublicKey: pubKey} req := &ethpb.AggregateSelectionRequest{Slot: beaconState.Slot(), CommitteeIndex: 1, SlotSignature: sig.Marshal(), PublicKey: pubKey}
err = aggregatorServer.AttPool.SaveAggregatedAttestations([]ethpb.Att{ err = aggregatorServer.AttPool.SaveAggregatedAttestations([]ethpb.Att{
att0, att0,

View File

@@ -63,8 +63,7 @@ func (vs *Server) ProposeAttestation(ctx context.Context, att *ethpb.Attestation
}) })
// Determine subnet to broadcast attestation to // Determine subnet to broadcast attestation to
wantedEpoch := slots.ToEpoch(att.Data.Slot) vals, err := vs.HeadFetcher.HeadValidatorsIndicesFromAdvancedSlots(ctx, att.Data.Slot)
vals, err := vs.HeadFetcher.HeadValidatorsIndices(ctx, wantedEpoch)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -102,8 +101,7 @@ func (vs *Server) SubscribeCommitteeSubnets(ctx context.Context, req *ethpb.Comm
} }
fetchValsLen := func(slot primitives.Slot) (uint64, error) { fetchValsLen := func(slot primitives.Slot) (uint64, error) {
wantedEpoch := slots.ToEpoch(slot) vals, err := vs.HeadFetcher.HeadValidatorsIndicesFromAdvancedSlots(ctx, slot)
vals, err := vs.HeadFetcher.HeadValidatorsIndices(ctx, wantedEpoch)
if err != nil { if err != nil {
return 0, err return 0, err
} }