prysm-pulse/beacon-chain/rpc/attester_server.go
Preston Van Loon 612bb38077 Cross p2p spans, more spans, synchronous attestations, minor fixes (#2009)
* Fix assignments bug where validators don't retry for assignments on failure

* synch only please

* trying to fix state issues

* trying random stuff

* do not explode

* use ctx

* working build, failing tests

* broadcast local addrs as well as relay addrs

* fixed p2p tests, more tests to fix still

* another test fixed, log warning instead of throw error

* Fix last tests

* godoc

* add test for broadcast in apply fork choiec

* remove unneeded code

* remove tracer adapter, not needed

* remove extra stuff

* remove any

* revert addr_factory

* revert addr_factory

* Revert "revert addr_factory"

This reverts commit e93fb706494a1070158b8db31e67146d6b0648ad.

* Revert "revert addr_factory"

This reverts commit dedaa405559cc818698870c4e4570953367f1e3c.

* revert removal of this code

* unused param
2019-03-17 10:56:05 +08:00

111 lines
4.5 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/core/state"
"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"
)
// 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)
}
if err := as.operationService.HandleAttestations(ctx, att); err != nil {
return nil, err
}
return &pb.AttestResponse{AttestationHash: h[:]}, nil
}
// AttestationDataAtSlot 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) AttestationDataAtSlot(ctx context.Context, req *pb.AttestationDataRequest) (*pb.AttestationDataResponse, 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 := hashutil.HashBeaconBlock(head)
if err != nil {
return nil, fmt.Errorf("could not tree hash beacon block: %v", err)
}
beaconState, err := as.beaconDB.State(ctx)
if err != nil {
return nil, fmt.Errorf("could not fetch beacon state: %v", err)
}
for beaconState.Slot < req.Slot {
beaconState, err = state.ExecuteStateTransition(
ctx, beaconState, nil /* block */, blockRoot, false, /* verify signatures */
)
if err != nil {
return nil, fmt.Errorf("could not execute head transition: %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).
// 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 := hashutil.HashBeaconBlock(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[:]
}
return &pb.AttestationDataResponse{
BeaconBlockRootHash32: blockRoot[:],
EpochBoundaryRootHash32: epochBoundaryRoot,
JustifiedEpoch: beaconState.JustifiedEpoch,
JustifiedBlockRootHash32: justifiedBlockRoot,
LatestCrosslink: beaconState.LatestCrosslinks[req.Shard],
}, nil
}