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

Loop Once Over state.LatestAttestations in Epoch Processing #2342

Merged
merged 2 commits into from
Apr 22, 2019
Merged
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
119 changes: 0 additions & 119 deletions beacon-chain/core/epoch/epoch_operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,134 +5,15 @@
package epoch

import (
"bytes"
"fmt"
"math"

block "github.com/prysmaticlabs/prysm/beacon-chain/core/blocks"
"github.com/prysmaticlabs/prysm/beacon-chain/core/helpers"
"github.com/prysmaticlabs/prysm/beacon-chain/core/validators"
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
b "github.com/prysmaticlabs/prysm/shared/bytesutil"
)

// CurrentAttestations returns the pending attestations from current epoch.
//
// Spec pseudocode definition:
// return [a for a in state.latest_attestations if current_epoch ==
// slot_to_epoch(a.data.slot)]
// (Note: this is the set of attestations of slots in the epoch
// current_epoch, not attestations that got included in the chain
// during the epoch current_epoch.)
func CurrentAttestations(state *pb.BeaconState) []*pb.PendingAttestation {
var currentEpochAttestations []*pb.PendingAttestation
currentEpoch := helpers.CurrentEpoch(state)

for _, attestation := range state.LatestAttestations {
if currentEpoch == helpers.SlotToEpoch(attestation.Data.Slot) {
currentEpochAttestations = append(currentEpochAttestations, attestation)
}
}
return currentEpochAttestations
}

// CurrentEpochBoundaryAttestations returns the pending attestations from
// the epoch's boundary block.
//
// Spec pseudocode definition:
// return [a for a in current_epoch_attestations if a.data.epoch_boundary_root ==
// get_block_root(state, get_epoch_start_slot(current_epoch))
func CurrentEpochBoundaryAttestations(
state *pb.BeaconState,
currentEpochAttestations []*pb.PendingAttestation,
) ([]*pb.PendingAttestation, error) {
var boundaryAttestations []*pb.PendingAttestation
for _, attestation := range currentEpochAttestations {
boundaryBlockRoot, err := block.BlockRoot(state, helpers.StartSlot(helpers.CurrentEpoch(state)))
if err != nil {
return nil, err
}

attestationData := attestation.Data
sameRoot := bytes.Equal(attestationData.EpochBoundaryRootHash32, boundaryBlockRoot)
if sameRoot {
boundaryAttestations = append(boundaryAttestations, attestation)
}
}
return boundaryAttestations, nil
}

// PrevAttestations returns the attestations of the previous epoch
// (state.slot - 2 * SLOTS_PER_EPOCH...state.slot - EPOCH_LENGTH).
//
// Spec pseudocode definition:
// return [a for a in state.latest_attestations if
// previous_epoch == slot_to_epoch(a.data.slot)].
func PrevAttestations(state *pb.BeaconState) []*pb.PendingAttestation {
var prevEpochAttestations []*pb.PendingAttestation
prevEpoch := helpers.PrevEpoch(state)

for _, attestation := range state.LatestAttestations {
if prevEpoch == helpers.SlotToEpoch(attestation.Data.Slot) {
prevEpochAttestations = append(prevEpochAttestations, attestation)
}
}

return prevEpochAttestations
}

// PrevEpochBoundaryAttestations returns the boundary attestations
// at the start of the previous epoch.
//
// Spec pseudocode definition:
// return [a for a in previous_epoch_attestations
// if a.epoch_boundary_root == get_block_root(state, get_epoch_start_slot(previous_epoch)]
func PrevEpochBoundaryAttestations(
state *pb.BeaconState,
prevEpochAttestations []*pb.PendingAttestation,
) ([]*pb.PendingAttestation, error) {
var prevEpochBoundaryAttestations []*pb.PendingAttestation

prevBoundaryBlockRoot, err := block.BlockRoot(state,
helpers.StartSlot(helpers.PrevEpoch(state)))
if err != nil {
return nil, err
}

for _, attestation := range prevEpochAttestations {
if bytes.Equal(attestation.Data.EpochBoundaryRootHash32, prevBoundaryBlockRoot) {
prevEpochBoundaryAttestations = append(prevEpochBoundaryAttestations, attestation)
}
}
return prevEpochBoundaryAttestations, nil
}

// PrevHeadAttestations returns the pending attestations from
// the canonical beacon chain.
//
// Spec pseudocode definition:
// return [a for a in previous_epoch_attestations
// if a.data.beacon_block_root == get_block_root(state, a.data.slot)]
func PrevHeadAttestations(
state *pb.BeaconState,
prevEpochAttestations []*pb.PendingAttestation,
) ([]*pb.PendingAttestation, error) {

var headAttestations []*pb.PendingAttestation
for _, attestation := range prevEpochAttestations {
canonicalBlockRoot, err := block.BlockRoot(state, attestation.Data.Slot)
if err != nil {
return nil, err
}

attestationData := attestation.Data
if bytes.Equal(attestationData.BeaconBlockRootHash32, canonicalBlockRoot) {
headAttestations = append(headAttestations, attestation)
}
}
return headAttestations, nil
}

// TotalBalance returns the total balance at stake of the validators
// from the shard committee regardless of validators attested or not.
//
Expand Down
Loading