Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Remove crosslink and shards across beacon node #3760

Closed
wants to merge 18 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions beacon-chain/archiver/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ go_library(
"//beacon-chain/db:go_default_library",
"//proto/beacon/p2p/v1:go_default_library",
"//proto/eth/v1alpha1:go_default_library",
"//shared/params:go_default_library",
"@com_github_pkg_errors//:go_default_library",
"@com_github_sirupsen_logrus//:go_default_library",
],
Expand Down
14 changes: 3 additions & 11 deletions beacon-chain/archiver/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/prysmaticlabs/prysm/beacon-chain/db"
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
ethpb "github.com/prysmaticlabs/prysm/proto/eth/v1alpha1"
"github.com/prysmaticlabs/prysm/shared/params"
"github.com/sirupsen/logrus"
)

Expand Down Expand Up @@ -68,26 +69,17 @@ func (s *Service) Status() error {
// We archive committee information pertaining to the head state's epoch.
func (s *Service) archiveCommitteeInfo(ctx context.Context, headState *pb.BeaconState) error {
currentEpoch := helpers.SlotToEpoch(headState.Slot)
committeeCount, err := helpers.CommitteeCount(headState, currentEpoch)
if err != nil {
return errors.Wrap(err, "could not get committee count")
}
seed, err := helpers.Seed(headState, currentEpoch)
seed, err := helpers.Seed(headState, currentEpoch, params.BeaconConfig().DomainBeaconAttester)
if err != nil {
return errors.Wrap(err, "could not generate seed")
}
startShard, err := helpers.StartShard(headState, currentEpoch)
if err != nil {
return errors.Wrap(err, "could not get start shard")
}
proposerIndex, err := helpers.BeaconProposerIndex(headState)
if err != nil {
return errors.Wrap(err, "could not get beacon proposer index")
}
info := &ethpb.ArchivedCommitteeInfo{
Seed: seed[:],
StartShard: startShard,
CommitteeCount: committeeCount,
CommitteeCount: params.BeaconConfig().MaxCommitteesPerSlot * params.BeaconConfig().SlotsPerEpoch,
ProposerIndex: proposerIndex,
}
if err := s.beaconDB.SaveArchivedCommitteeInfo(ctx, currentEpoch, info); err != nil {
Expand Down
13 changes: 2 additions & 11 deletions beacon-chain/blockchain/forkchoice/process_attestation.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,12 +185,7 @@ func (s *Store) waitForAttInclDelay(ctx context.Context, a *ethpb.Attestation, t
ctx, span := trace.StartSpan(ctx, "beacon-chain.forkchoice.waitForAttInclDelay")
defer span.End()

slot, err := helpers.AttestationDataSlot(targetState, a.Data)
if err != nil {
return errors.Wrap(err, "could not get attestation slot")
}

nextSlot := slot + 1
nextSlot := a.Data.Slot + 1
duration := time.Duration(nextSlot*params.BeaconConfig().SecondsPerSlot) * time.Second
timeToInclude := time.Unix(int64(targetState.GenesisTime), 0).Add(duration)

Expand Down Expand Up @@ -225,11 +220,7 @@ func (s *Store) aggregateAttestation(ctx context.Context, att *ethpb.Attestation

// verifyAttSlotTime validates input attestation is not from the future.
func (s *Store) verifyAttSlotTime(ctx context.Context, baseState *pb.BeaconState, d *ethpb.AttestationData) error {
aSlot, err := helpers.AttestationDataSlot(baseState, d)
if err != nil {
return errors.Wrap(err, "could not get attestation slot")
}
return helpers.VerifySlotTime(baseState.GenesisTime, aSlot+1)
return helpers.VerifySlotTime(baseState.GenesisTime, d.Slot+1)
}

// verifyAttestation validates input attestation is valid.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ func TestStore_AggregateAttestation(t *testing.T) {
CurrentVersion: params.BeaconConfig().GenesisForkVersion,
Epoch: 0,
}
domain := helpers.Domain(f, 0, params.BeaconConfig().DomainAttestation)
domain := helpers.Domain(f, 0, params.BeaconConfig().DomainBeaconAttester)
sig := privKeys[0].Sign([]byte{}, domain)

store := &Store{attsQueue: make(map[[32]byte]*ethpb.Attestation)}
Expand Down
2 changes: 1 addition & 1 deletion beacon-chain/cache/attestation_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ func wrapperToKey(i interface{}) (string, error) {
}

func reqToKey(req *pb.AttestationRequest) (string, error) {
return fmt.Sprintf("%d-%d", req.Shard, req.Slot), nil
return fmt.Sprintf("%d-%d", req.Index, req.Slot), nil
}

type attestationReqResWrapper struct {
Expand Down
14 changes: 5 additions & 9 deletions beacon-chain/cache/committee.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (

"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prysmaticlabs/prysm/shared/params"
"github.com/prysmaticlabs/prysm/shared/sliceutil"
"k8s.io/client-go/tools/cache"
)
Expand Down Expand Up @@ -66,7 +65,7 @@ func NewCommitteeCache() *CommitteeCache {

// ShuffledIndices fetches the shuffled indices by epoch and shard. Every list of indices
// represent one committee. Returns true if the list exists with epoch and shard. Otherwise returns false, nil.
func (c *CommitteeCache) ShuffledIndices(epoch uint64, shard uint64) ([]uint64, error) {
func (c *CommitteeCache) ShuffledIndices(epoch uint64, index uint64) ([]uint64, error) {
c.lock.RLock()
defer c.lock.RUnlock()
obj, exists, err := c.CommitteeCache.GetByKey(strconv.Itoa(int(epoch)))
Expand All @@ -86,8 +85,7 @@ func (c *CommitteeCache) ShuffledIndices(epoch uint64, shard uint64) ([]uint64,
return nil, ErrNotCommittee
}

start, end := startEndIndices(item, shard)

start, end := startEndIndices(item, index)
return item.Committee[start:end], nil
}

Expand Down Expand Up @@ -208,12 +206,10 @@ func (c *CommitteeCache) ActiveIndices(epoch uint64) ([]uint64, error) {
return item.Committee, nil
}

func startEndIndices(c *Committee, wantedShard uint64) (uint64, uint64) {
shardCount := params.BeaconConfig().ShardCount
currentShard := (wantedShard + shardCount - c.StartShard) % shardCount
func startEndIndices(c *Committee, index uint64) (uint64, uint64) {
validatorCount := uint64(len(c.Committee))
start := sliceutil.SplitOffset(validatorCount, c.CommitteeCount, currentShard)
end := sliceutil.SplitOffset(validatorCount, c.CommitteeCount, currentShard+1)
start := sliceutil.SplitOffset(validatorCount, c.CommitteeCount, index)
end := sliceutil.SplitOffset(validatorCount, c.CommitteeCount, index+1)

return start, end
}
63 changes: 5 additions & 58 deletions beacon-chain/core/blocks/block_operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -512,13 +512,11 @@ func ProcessAttestationsNoVerify(ctx context.Context, beaconState *pb.BeaconStat
// Process ``Attestation`` operation.
// """
// data = attestation.data
// assert data.crosslink.shard < SHARD_COUNT
// assert data.index < get_committee_count_at_slot(state, data.slot)
// assert data.target.epoch in (get_previous_epoch(state), get_current_epoch(state))
// assert data.slot + MIN_ATTESTATION_INCLUSION_DELAY <= state.slot <= data.slot + SLOTS_PER_EPOCH
//
// attestation_slot = get_attestation_data_slot(state, data)
// assert attestation_slot + MIN_ATTESTATION_INCLUSION_DELAY <= state.slot <= attestation_slot + SLOTS_PER_EPOCH
//
// committee = get_crosslink_committee(state, data.target.epoch, data.crosslink.shard)
// committee = get_beacon_committee(state, data.slot, data.index)
// assert len(attestation.aggregation_bits) == len(attestation.custody_bits) == len(committee)
//
// pending_attestation = PendingAttestation(
Expand Down Expand Up @@ -561,14 +559,6 @@ func ProcessAttestationNoVerify(ctx context.Context, beaconState *pb.BeaconState

data := att.Data

if data.Crosslink.Shard > params.BeaconConfig().ShardCount {
return nil, fmt.Errorf(
"expected crosslink shard %d to be less than SHARD_COUNT %d",
data.Crosslink.Shard,
params.BeaconConfig().ShardCount,
)
}

if data.Target.Epoch != helpers.PrevEpoch(beaconState) && data.Target.Epoch != helpers.CurrentEpoch(beaconState) {
return nil, fmt.Errorf(
"expected target epoch (%d) to be the previous epoch (%d) or the current epoch (%d)",
Expand All @@ -577,11 +567,7 @@ func ProcessAttestationNoVerify(ctx context.Context, beaconState *pb.BeaconState
helpers.CurrentEpoch(beaconState),
)
}

attestationSlot, err := helpers.AttestationDataSlot(beaconState, data)
if err != nil {
return nil, errors.Wrap(err, "could not get attestation slot")
}
attestationSlot := att.Data.Slot
minInclusionCheck := attestationSlot+params.BeaconConfig().MinAttestationInclusionDelay <= beaconState.Slot
epochInclusionCheck := beaconState.Slot <= attestationSlot+params.BeaconConfig().SlotsPerEpoch
if !minInclusionCheck {
Expand Down Expand Up @@ -619,27 +605,15 @@ func ProcessAttestationNoVerify(ctx context.Context, beaconState *pb.BeaconState
var ffgSourceEpoch uint64
var ffgSourceRoot []byte
var ffgTargetEpoch uint64
var parentCrosslink *ethpb.Crosslink
if data.Target.Epoch == helpers.CurrentEpoch(beaconState) {
ffgSourceEpoch = beaconState.CurrentJustifiedCheckpoint.Epoch
ffgSourceRoot = beaconState.CurrentJustifiedCheckpoint.Root
ffgTargetEpoch = helpers.CurrentEpoch(beaconState)
crosslinkShard := data.Crosslink.Shard
if int(crosslinkShard) >= len(beaconState.CurrentCrosslinks) {
return nil, fmt.Errorf("invalid shard given in attestation: %d", crosslinkShard)
}

parentCrosslink = beaconState.CurrentCrosslinks[crosslinkShard]
beaconState.CurrentEpochAttestations = append(beaconState.CurrentEpochAttestations, pendingAtt)
} else {
ffgSourceEpoch = beaconState.PreviousJustifiedCheckpoint.Epoch
ffgSourceRoot = beaconState.PreviousJustifiedCheckpoint.Root
ffgTargetEpoch = helpers.PrevEpoch(beaconState)
crosslinkShard := data.Crosslink.Shard
if int(crosslinkShard) >= len(beaconState.PreviousCrosslinks) {
return nil, fmt.Errorf("invalid shard given in attestation: %d", crosslinkShard)
}
parentCrosslink = beaconState.PreviousCrosslinks[crosslinkShard]
beaconState.PreviousEpochAttestations = append(beaconState.PreviousEpochAttestations, pendingAtt)
}
if data.Source.Epoch != ffgSourceEpoch {
Expand All @@ -651,34 +625,7 @@ func ProcessAttestationNoVerify(ctx context.Context, beaconState *pb.BeaconState
if data.Target.Epoch != ffgTargetEpoch {
return nil, fmt.Errorf("expected target epoch %d, received %d", ffgTargetEpoch, data.Target.Epoch)
}
endEpoch := parentCrosslink.EndEpoch + params.BeaconConfig().MaxEpochsPerCrosslink
if data.Target.Epoch < endEpoch {
endEpoch = data.Target.Epoch
}
if data.Crosslink.StartEpoch != parentCrosslink.EndEpoch {
return nil, fmt.Errorf("expected crosslink start epoch %d, received %d",
parentCrosslink.EndEpoch, data.Crosslink.StartEpoch)
}
if data.Crosslink.EndEpoch != endEpoch {
return nil, fmt.Errorf("expected crosslink end epoch %d, received %d",
endEpoch, data.Crosslink.EndEpoch)
}
crosslinkParentRoot, err := ssz.HashTreeRoot(parentCrosslink)
if err != nil {
return nil, errors.Wrap(err, "could not tree hash parent crosslink")
}
if !bytes.Equal(data.Crosslink.ParentRoot, crosslinkParentRoot[:]) {
return nil, fmt.Errorf(
"mismatched parent crosslink root, expected %#x, received %#x",
crosslinkParentRoot,
data.Crosslink.ParentRoot,
)
}

// To be removed in Phase 1
if !bytes.Equal(data.Crosslink.DataRoot, params.BeaconConfig().ZeroHash[:]) {
return nil, fmt.Errorf("expected data root %#x == ZERO_HASH", data.Crosslink.DataRoot)
}
return beaconState, nil
}

Expand Down Expand Up @@ -816,7 +763,7 @@ func VerifyIndexedAttestation(ctx context.Context, beaconState *pb.BeaconState,
return fmt.Errorf("custody Bit1 indices are not sorted, got %v", custodyBit1Indices)
}

domain := helpers.Domain(beaconState.Fork, indexedAtt.Data.Target.Epoch, params.BeaconConfig().DomainAttestation)
domain := helpers.Domain(beaconState.Fork, indexedAtt.Data.Target.Epoch, params.BeaconConfig().DomainBeaconAttester)
var pubkeys []*bls.PublicKey
if len(custodyBit0Indices) > 0 {
pubkey, err := bls.PublicKeyFromBytes(beaconState.Validators[custodyBit0Indices[0]].PublicKey)
Expand Down
Loading