prysm-pulse/beacon-chain/rpc/attester_server.go
terence tsao 8c04ced1a6 Name Changes to Align with V0.3 (#1621)
* fixed epoch_processing

* penalize->slash

* exit -> voluntary_exit

*  SEED_LOOKAHEAD -> MIN_SEED_LOOKAHED

* ENTRY_EXIT_DELAY -> ACTIVATION_EXIT_DELAY

* `INCLUDER_REWARD_QUOTIENT` -> `ATTESTATION_INCLUSION_REWARD_QUOTIEN`

* LatestIndexRoots -> LatestActiveIndexRoots

* `MIN_VALIDATOR_WITHDRAWAL_EPOCHS` -> `MIN_VALIDATOR_WITHDRAWAL_DELAY`

* MAX_WITHDRAWALS_PER_EPOCH -> MAX_EXIT_DEQUEUES_PER_EPOCH

* ETH1_DATA_VOTING_PERIOD -> EPOCHS_PER_ETH1_VOTING_PERIOD

* SLOT_DURATION -> SECONDS_PER_SLOT

* EPOCH_LENGTH -> SLOTS_PER_EPOCH

* SLOT_DURATION -> SECONDS_PER_SLOT take 2

* rest of the misc fixes for config name changes

* remove tools/bootnode/.!74296!bootnode.go

* `current_epoch_start_shard` -> `current_shuffling_start_shard`, `current_shuffling_epoch`, `current_shuffling_see`

* go fmt

* fixed comment

* updated pseudocode comments

* merged master
2019-02-18 10:52:16 -06:00

81 lines
3.6 KiB
Go

package rpc
import (
"context"
"fmt"
"github.com/prysmaticlabs/prysm/beacon-chain/core/blocks"
"github.com/prysmaticlabs/prysm/beacon-chain/core/helpers"
"github.com/prysmaticlabs/prysm/beacon-chain/db"
pbp2p "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
pb "github.com/prysmaticlabs/prysm/proto/beacon/rpc/v1"
"github.com/prysmaticlabs/prysm/shared/hashutil"
"github.com/prysmaticlabs/prysm/shared/params"
"github.com/prysmaticlabs/prysm/shared/ssz"
)
// AttesterServer defines a server implementation of the gRPC Attester service,
// providing RPC methods for validators acting as attesters to broadcast votes on beacon blocks.
type AttesterServer struct {
beaconDB *db.BeaconDB
operationService operationService
}
// AttestHead is a function called by an attester in a sharding validator to vote
// on a block via an attestation object as defined in the Ethereum Serenity specification.
func (as *AttesterServer) AttestHead(ctx context.Context, att *pbp2p.Attestation) (*pb.AttestResponse, error) {
h, err := hashutil.HashProto(att)
if err != nil {
return nil, fmt.Errorf("could not hash attestation: %v", err)
}
// Relays the attestation to chain service.
as.operationService.IncomingAttFeed().Send(att)
return &pb.AttestResponse{AttestationHash: h[:]}, nil
}
// AttestationInfoAtSlot fetches the necessary information from the current canonical head
// and beacon state for an assigned attester to perform necessary responsibilities. This includes
// fetching the epoch boundary roots, the latest justified block root, among others.
func (as *AttesterServer) AttestationInfoAtSlot(ctx context.Context, req *pb.AttestationInfoRequest) (*pb.AttestationInfoResponse, error) {
// Set the attestation data's beacon block root = hash_tree_root(head) where head
// is the validator's view of the head block of the beacon chain during the slot.
head, err := as.beaconDB.BlockBySlot(req.Slot)
if err != nil {
return nil, fmt.Errorf("failed to retrieve chain head: %v", err)
}
if head == nil {
return nil, fmt.Errorf("no block found at slot %d", req.Slot)
}
blockRoot, err := ssz.TreeHash(head)
if err != nil {
return nil, fmt.Errorf("could not tree hash beacon block: %v", err)
}
beaconState, err := as.beaconDB.State()
if err != nil {
return nil, fmt.Errorf("could not fetch beacon state: %v", err)
}
// Fetch the epoch boundary root = hash_tree_root(epoch_boundary)
// where epoch_boundary is the block at the most recent epoch boundary in the
// chain defined by head -- i.e. the BeaconBlock where block.slot == get_epoch_start_slot(head.slot).
// On the server side, this is fetched by calling get_block_root(state, get_epoch_start_slot(head.slot)).
epochBoundary := head.Slot / params.BeaconConfig().SlotsPerEpoch
epochBoundaryRoot, err := blocks.BlockRoot(beaconState, helpers.StartSlot(epochBoundary))
if err != nil {
return nil, fmt.Errorf("could not get epoch boundary block: %v", err)
}
// Fetch the justified block root = hash_tree_root(justified_block) where
// justified_block is the block at state.justified_epoch in the chain defined by head.
// On the server side, this is fetched by calling get_block_root(state, justified_epoch).
justifiedBlockRoot, err := blocks.BlockRoot(beaconState, helpers.StartSlot(beaconState.JustifiedEpoch))
if err != nil {
return nil, fmt.Errorf("could not get justified block: %v", err)
}
return &pb.AttestationInfoResponse{
BeaconBlockRootHash32: blockRoot[:],
EpochBoundaryRootHash32: epochBoundaryRoot[:],
JustifiedEpoch: beaconState.JustifiedEpoch,
JustifiedBlockRootHash32: justifiedBlockRoot[:],
LatestCrosslink: beaconState.LatestCrosslinks[req.Shard],
}, nil
}