Skip to content

Commit

Permalink
Modify ListBeaconCommittees to use new state service (#5411)
Browse files Browse the repository at this point in the history
  • Loading branch information
terencechain committed Apr 15, 2020
1 parent 899670e commit 497fa6e
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 257 deletions.
1 change: 0 additions & 1 deletion beacon-chain/rpc/beacon/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,5 @@ go_test(
"@com_github_prysmaticlabs_ethereumapis//eth/v1alpha1:go_default_library",
"@com_github_prysmaticlabs_go_bitfield//:go_default_library",
"@com_github_prysmaticlabs_go_ssz//:go_default_library",
"@in_gopkg_d4l3k_messagediff_v1//:go_default_library",
],
)
17 changes: 13 additions & 4 deletions beacon-chain/rpc/beacon/attestations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -889,6 +889,7 @@ func TestServer_StreamIndexedAttestations_ContextCanceled(t *testing.T) {
}

func TestServer_StreamIndexedAttestations_OK(t *testing.T) {
params.UseMainnetConfig()
db := dbTest.SetupDB(t)
defer dbTest.TeardownDB(t, db)
exitRoutine := make(chan bool)
Expand All @@ -898,11 +899,18 @@ func TestServer_StreamIndexedAttestations_OK(t *testing.T) {

numValidators := 64
headState, privKeys := testutil.DeterministicGenesisState(t, uint64(numValidators))
randaoMixes := make([][]byte, params.BeaconConfig().EpochsPerHistoricalVector)
for i := 0; i < len(randaoMixes); i++ {
randaoMixes[i] = make([]byte, 32)
b := &ethpb.SignedBeaconBlock{Block: &ethpb.BeaconBlock{}}
if err := db.SaveBlock(ctx, b); err != nil {
t.Fatal(err)
}
gRoot, err := ssz.HashTreeRoot(b.Block)
if err != nil {
t.Fatal(err)
}
if err := db.SaveGenesisBlockRoot(ctx, gRoot); err != nil {
t.Fatal(err)
}
if err := headState.SetRandaoMixes(randaoMixes); err != nil {
if err := db.SaveState(ctx, headState, gRoot); err != nil {
t.Fatal(err)
}

Expand Down Expand Up @@ -995,6 +1003,7 @@ func TestServer_StreamIndexedAttestations_OK(t *testing.T) {
},
AttestationNotifier: chainService.OperationNotifier(),
CollectedAttestationsBuffer: make(chan []*ethpb.Attestation, 1),
StateGen: stategen.New(db, cache.NewStateSummaryCache()),
}

mockStream := mockRPC.NewMockBeaconChain_StreamIndexedAttestationsServer(ctrl)
Expand Down
106 changes: 32 additions & 74 deletions beacon-chain/rpc/beacon/committees.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (

ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
"github.com/prysmaticlabs/prysm/beacon-chain/core/helpers"
"github.com/prysmaticlabs/prysm/shared/bytesutil"
"github.com/prysmaticlabs/prysm/shared/params"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
Expand All @@ -20,31 +19,39 @@ func (bs *Server) ListBeaconCommittees(
req *ethpb.ListCommitteesRequest,
) (*ethpb.BeaconCommittees, error) {

var requestingGenesis bool
var startSlot uint64
headSlot := bs.GenesisTimeFetcher.CurrentSlot()
currentSlot := bs.GenesisTimeFetcher.CurrentSlot()
var requestedSlot uint64
switch q := req.QueryFilter.(type) {
case *ethpb.ListCommitteesRequest_Epoch:
startSlot = helpers.StartSlot(q.Epoch)
requestedSlot = helpers.StartSlot(q.Epoch)
case *ethpb.ListCommitteesRequest_Genesis:
requestingGenesis = q.Genesis
if !requestingGenesis {
startSlot = headSlot
}
requestedSlot = 0
default:
startSlot = headSlot
requestedSlot = currentSlot
}

requestedEpoch := helpers.SlotToEpoch(requestedSlot)
currentEpoch := helpers.SlotToEpoch(currentSlot)
if requestedEpoch > currentEpoch {
return nil, status.Errorf(
codes.InvalidArgument,
"Cannot retrieve information for an future epoch, current epoch %d, requesting %d",
currentEpoch,
requestedEpoch,
)
}
committees, activeIndices, err := bs.retrieveCommitteesForEpoch(ctx, helpers.SlotToEpoch(startSlot))

committees, activeIndices, err := bs.retrieveCommitteesForEpoch(ctx, requestedEpoch)
if err != nil {
return nil, status.Errorf(
codes.Internal,
"Could not retrieve committees for epoch %d: %v",
helpers.SlotToEpoch(startSlot),
requestedEpoch,
err,
)
}
return &ethpb.BeaconCommittees{
Epoch: helpers.SlotToEpoch(startSlot),
Epoch: requestedEpoch,
Committees: committees,
ActiveValidatorCount: uint64(len(activeIndices)),
}, nil
Expand All @@ -54,70 +61,21 @@ func (bs *Server) retrieveCommitteesForEpoch(
ctx context.Context,
epoch uint64,
) (map[uint64]*ethpb.BeaconCommittees_CommitteesList, []uint64, error) {
var attesterSeed [32]byte
var activeIndices []uint64
var err error
startSlot := helpers.StartSlot(epoch)
currentEpoch := helpers.SlotToEpoch(bs.GenesisTimeFetcher.CurrentSlot())
if helpers.SlotToEpoch(startSlot)+1 < currentEpoch {
activeIndices, err = bs.HeadFetcher.HeadValidatorsIndices(helpers.SlotToEpoch(startSlot))
if err != nil {
return nil, nil, status.Errorf(
codes.Internal,
"Could not retrieve active indices for epoch %d: %v",
helpers.SlotToEpoch(startSlot),
err,
)
}
archivedCommitteeInfo, err := bs.BeaconDB.ArchivedCommitteeInfo(ctx, helpers.SlotToEpoch(startSlot))
if err != nil {
return nil, nil, status.Errorf(
codes.Internal,
"Could not request archival data for epoch %d: %v",
helpers.SlotToEpoch(startSlot),
err,
)
}
if archivedCommitteeInfo == nil {
return nil, nil, status.Errorf(
codes.NotFound,
"Could not retrieve data for epoch %d, perhaps --archive in the running beacon node is disabled",
helpers.SlotToEpoch(startSlot),
)
}
attesterSeed = bytesutil.ToBytes32(archivedCommitteeInfo.AttesterSeed)
} else if helpers.SlotToEpoch(startSlot)+1 == currentEpoch || helpers.SlotToEpoch(startSlot) == currentEpoch {
// Otherwise, we use current beacon state to calculate the committees.
requestedEpoch := helpers.SlotToEpoch(startSlot)
activeIndices, err = bs.HeadFetcher.HeadValidatorsIndices(requestedEpoch)
if err != nil {
return nil, nil, status.Errorf(
codes.Internal,
"Could not retrieve active indices for requested epoch %d: %v",
requestedEpoch,
err,
)
}
attesterSeed, err = bs.HeadFetcher.HeadSeed(requestedEpoch)
if err != nil {
return nil, nil, status.Errorf(
codes.Internal,
"Could not retrieve attester seed for requested epoch %d: %v",
requestedEpoch,
err,
)
}
} else {
// Otherwise, we are requesting data from the future and we return an error.
return nil, nil, status.Errorf(
codes.InvalidArgument,
"Cannot retrieve information about an epoch in the future, current epoch %d, requesting %d",
currentEpoch,
helpers.SlotToEpoch(startSlot),
)
requestedState, err := bs.StateGen.StateBySlot(ctx, startSlot)
if err != nil {
return nil, nil, status.Error(codes.Internal, "Could not get state")
}
seed, err := helpers.Seed(requestedState, epoch, params.BeaconConfig().DomainBeaconAttester)
if err != nil {
return nil, nil, status.Error(codes.Internal, "Could not get seed")
}
activeIndices, err := helpers.ActiveValidatorIndices(requestedState, epoch)
if err != nil {
return nil, nil, status.Error(codes.Internal, "Could not get active indices")
}

committeesListsBySlot, err := computeCommittees(startSlot, activeIndices, attesterSeed)
committeesListsBySlot, err := computeCommittees(startSlot, activeIndices, seed)
if err != nil {
return nil, nil, status.Errorf(
codes.InvalidArgument,
Expand Down
Loading

0 comments on commit 497fa6e

Please sign in to comment.