Skip to content

Commit

Permalink
Loop Once Over state.LatestAttestations in Epoch Processing (#2342)
Browse files Browse the repository at this point in the history
* single loop for extracting attestations

* complete
  • Loading branch information
rauljordan authored Apr 22, 2019
1 parent 19c24ec commit 629626d
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 441 deletions.
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

0 comments on commit 629626d

Please sign in to comment.