prysm-pulse/beacon-chain/rpc/attester_server.go
Raul Jordan e19920aec1
Miscellaneous Runtime Fixes & Improvements - Raul (#1674)
* fatal if impossible to receive chainstart

* fix tests

* fix

* custom delay

* completed custom delay

* errors

* better logs, nothing at genesis

* use demo in val

* add gazelle

* log

* starting to log stuff

* pass in ops

* avoid printing the large #s for debug, still working on tests..

* all around better logging

* fixed build error in epoch process

* fixed state transiton tests

* fixed block tests

* lint

* verify sigs in randao

* ready for inclusion falg

* only print waiting when slot is not valid

* fix build

* mod config

* fixed last justified slot issue

* fix inclusion

* fixed attestation issue

* using zero hash from params instead

* fix tests

* update balance

* removed swp

* more `- genesis_slot` for logs

* rem unused log

* fix broken tests

* account for skip slots in state root computation

* fixes done

* validator guide bug fixes - 671

* epoch boundary at the last slot of the epoch

* fix epoch issue

* more balance cal logs for debugging

* greater balance

* attestaton fixes

* fixes

* addressed testrun

* fixed ejection balance

* fix tests with far future epoch

* revert sync change

* revert initial sync change

* fix changes

* off by one att fix

* revert the att fix

* address comments

* format

* fix build

* rem file
2019-02-24 20:09:45 -06:00

103 lines
4.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.ChainHead()
if err != nil {
return nil, fmt.Errorf("failed to retrieve chain head: %v", err)
}
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)).
// If the epoch boundary slot is the same as state current slot,
// we set epoch boundary root to an empty root.
epochBoundaryRoot := make([]byte, 32)
epochStartSlot := helpers.StartSlot(helpers.SlotToEpoch(head.Slot))
if epochStartSlot == head.Slot {
hash, err := ssz.TreeHash(head)
if err != nil {
return nil, fmt.Errorf("could not tree hash head block: %v", err)
}
epochBoundaryRoot = hash[:]
} else {
epochBoundaryRoot, err = blocks.BlockRoot(beaconState, epochStartSlot)
if err != nil {
return nil, fmt.Errorf("could not get epoch boundary block: %v", err)
}
}
// epoch_start_slot = get_epoch_start_slot(slot_to_epoch(head.slot))
// 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).
// If the last justified boundary slot is the same as state current slot (ex: slot 0),
// we set justified block root to an empty root.
lastJustifiedSlot := helpers.StartSlot(beaconState.JustifiedEpoch)
justifiedBlockRoot := make([]byte, 32)
if lastJustifiedSlot != beaconState.Slot {
justifiedBlockRoot, err = blocks.BlockRoot(beaconState, lastJustifiedSlot)
if err != nil {
return nil, fmt.Errorf("could not get justified block: %v", err)
}
}
if beaconState.Slot == params.BeaconConfig().GenesisSlot {
epochBoundaryRoot = blockRoot[:]
justifiedBlockRoot = blockRoot[:]
}
log.Infof("Fetching epoch boundary root: %#x, state slot: %d", epochBoundaryRoot, beaconState.Slot-params.BeaconConfig().GenesisSlot)
log.Infof("Fetching justified block root: %#x, state slot: %d", justifiedBlockRoot, beaconState.Slot-params.BeaconConfig().GenesisSlot)
return &pb.AttestationInfoResponse{
BeaconBlockRootHash32: blockRoot[:],
EpochBoundaryRootHash32: epochBoundaryRoot,
JustifiedEpoch: beaconState.JustifiedEpoch,
JustifiedBlockRootHash32: justifiedBlockRoot,
LatestCrosslink: beaconState.LatestCrosslinks[req.Shard],
}, nil
}