2019-01-28 15:40:40 +00:00
|
|
|
package rpc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
2019-02-11 16:15:25 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/core/blocks"
|
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/core/helpers"
|
2019-03-02 00:33:55 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/core/state"
|
2019-02-11 16:15:25 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/db"
|
2019-02-06 16:20:38 +00:00
|
|
|
pbp2p "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
|
2019-01-28 15:40:40 +00:00
|
|
|
pb "github.com/prysmaticlabs/prysm/proto/beacon/rpc/v1"
|
|
|
|
"github.com/prysmaticlabs/prysm/shared/hashutil"
|
2019-02-15 23:19:36 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/params"
|
2019-01-28 15:40:40 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// 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 {
|
2019-02-11 16:15:25 +00:00
|
|
|
beaconDB *db.BeaconDB
|
2019-01-31 18:54:24 +00:00
|
|
|
operationService operationService
|
2019-01-28 15:40:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
2019-02-06 16:20:38 +00:00
|
|
|
func (as *AttesterServer) AttestHead(ctx context.Context, att *pbp2p.Attestation) (*pb.AttestResponse, error) {
|
|
|
|
h, err := hashutil.HashProto(att)
|
2019-01-28 15:40:40 +00:00
|
|
|
if err != nil {
|
2019-01-28 19:41:04 +00:00
|
|
|
return nil, fmt.Errorf("could not hash attestation: %v", err)
|
2019-01-28 15:40:40 +00:00
|
|
|
}
|
2019-03-17 02:56:05 +00:00
|
|
|
|
|
|
|
if err := as.operationService.HandleAttestations(ctx, att); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-01-28 15:40:40 +00:00
|
|
|
return &pb.AttestResponse{AttestationHash: h[:]}, nil
|
|
|
|
}
|
2019-02-06 16:20:38 +00:00
|
|
|
|
2019-02-27 20:21:15 +00:00
|
|
|
// AttestationDataAtSlot fetches the necessary information from the current canonical head
|
2019-02-11 16:15:25 +00:00
|
|
|
// 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.
|
2019-02-27 20:21:15 +00:00
|
|
|
func (as *AttesterServer) AttestationDataAtSlot(ctx context.Context, req *pb.AttestationDataRequest) (*pb.AttestationDataResponse, error) {
|
2019-02-11 16:15:25 +00:00
|
|
|
// 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.
|
2019-02-19 05:49:56 +00:00
|
|
|
head, err := as.beaconDB.ChainHead()
|
2019-02-11 16:15:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to retrieve chain head: %v", err)
|
|
|
|
}
|
2019-02-26 03:42:31 +00:00
|
|
|
blockRoot, err := hashutil.HashBeaconBlock(head)
|
2019-02-11 16:15:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("could not tree hash beacon block: %v", err)
|
|
|
|
}
|
2019-02-28 03:55:47 +00:00
|
|
|
beaconState, err := as.beaconDB.State(ctx)
|
2019-02-11 16:15:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("could not fetch beacon state: %v", err)
|
|
|
|
}
|
2019-03-02 00:33:55 +00:00
|
|
|
for beaconState.Slot < req.Slot {
|
|
|
|
beaconState, err = state.ExecuteStateTransition(
|
2019-03-18 06:19:44 +00:00
|
|
|
ctx, beaconState, nil /* block */, blockRoot, state.DefaultConfig(),
|
2019-03-02 00:33:55 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("could not execute head transition: %v", err)
|
|
|
|
}
|
|
|
|
}
|
2019-02-11 16:15:25 +00:00
|
|
|
// 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).
|
2019-02-25 02:09:45 +00:00
|
|
|
// 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 {
|
2019-02-26 03:42:31 +00:00
|
|
|
hash, err := hashutil.HashBeaconBlock(head)
|
2019-02-25 02:09:45 +00:00
|
|
|
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)
|
|
|
|
}
|
2019-02-11 16:15:25 +00:00
|
|
|
}
|
2019-02-25 02:09:45 +00:00
|
|
|
// epoch_start_slot = get_epoch_start_slot(slot_to_epoch(head.slot))
|
2019-02-11 16:15:25 +00:00
|
|
|
// 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).
|
2019-02-25 02:09:45 +00:00
|
|
|
// 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 {
|
2019-04-04 20:56:20 +00:00
|
|
|
var justifiedBlock *pbp2p.BeaconBlock
|
|
|
|
for i := uint64(0); justifiedBlock == nil && i < params.BeaconConfig().SlotsPerEpoch; i++ {
|
|
|
|
justifiedBlock, err = as.beaconDB.BlockBySlot(lastJustifiedSlot - i)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("could not get justified block: %v", err)
|
|
|
|
}
|
2019-02-25 02:09:45 +00:00
|
|
|
}
|
2019-04-04 20:56:20 +00:00
|
|
|
|
2019-04-03 22:50:35 +00:00
|
|
|
justifiedBlockRoot32, err := hashutil.HashBeaconBlock(justifiedBlock)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("could not get justified block: %v", err)
|
|
|
|
}
|
|
|
|
justifiedBlockRoot = justifiedBlockRoot32[:]
|
2019-02-25 02:09:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if beaconState.Slot == params.BeaconConfig().GenesisSlot {
|
|
|
|
epochBoundaryRoot = blockRoot[:]
|
|
|
|
justifiedBlockRoot = blockRoot[:]
|
2019-02-11 16:15:25 +00:00
|
|
|
}
|
2019-02-27 20:21:15 +00:00
|
|
|
return &pb.AttestationDataResponse{
|
2019-02-11 16:15:25 +00:00
|
|
|
BeaconBlockRootHash32: blockRoot[:],
|
2019-02-25 02:09:45 +00:00
|
|
|
EpochBoundaryRootHash32: epochBoundaryRoot,
|
2019-02-11 16:15:25 +00:00
|
|
|
JustifiedEpoch: beaconState.JustifiedEpoch,
|
2019-02-25 02:09:45 +00:00
|
|
|
JustifiedBlockRootHash32: justifiedBlockRoot,
|
2019-02-11 16:15:25 +00:00
|
|
|
LatestCrosslink: beaconState.LatestCrosslinks[req.Shard],
|
|
|
|
}, nil
|
2019-02-06 16:20:38 +00:00
|
|
|
}
|