2019-01-28 15:40:40 +00:00
|
|
|
package rpc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2019-01-30 12:28:53 +00:00
|
|
|
"time"
|
2019-01-28 15:40:40 +00:00
|
|
|
|
|
|
|
ptypes "github.com/gogo/protobuf/types"
|
|
|
|
"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"
|
|
|
|
)
|
|
|
|
|
|
|
|
// BeaconServer defines a server implementation of the gRPC Beacon service,
|
|
|
|
// providing RPC endpoints for obtaining the canonical beacon chain head,
|
|
|
|
// fetching latest observed attestations, and more.
|
|
|
|
type BeaconServer struct {
|
|
|
|
beaconDB *db.BeaconDB
|
|
|
|
ctx context.Context
|
2019-01-30 12:28:53 +00:00
|
|
|
powChainService powChainService
|
2019-01-31 18:54:24 +00:00
|
|
|
operationService operationService
|
2019-01-28 15:40:40 +00:00
|
|
|
incomingAttestation chan *pbp2p.Attestation
|
|
|
|
canonicalStateChan chan *pbp2p.BeaconState
|
2019-01-30 12:28:53 +00:00
|
|
|
chainStartChan chan time.Time
|
|
|
|
}
|
|
|
|
|
|
|
|
// WaitForChainStart queries the logs of the Deposit Contract in order to verify the beacon chain
|
|
|
|
// has started its runtime and validators begin their responsibilities. If it has not, it then
|
|
|
|
// subscribes to an event stream triggered by the powchain service whenever the ChainStart log does
|
|
|
|
// occur in the Deposit Contract on ETH 1.0.
|
|
|
|
func (bs *BeaconServer) WaitForChainStart(req *ptypes.Empty, stream pb.BeaconService_WaitForChainStartServer) error {
|
|
|
|
ok, genesisTime, err := bs.powChainService.HasChainStartLogOccurred()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not verify ChainStart log occurred: %v", err)
|
|
|
|
}
|
|
|
|
if ok {
|
|
|
|
res := &pb.ChainStartResponse{
|
|
|
|
Started: true,
|
|
|
|
GenesisTime: uint64(genesisTime.Unix()),
|
|
|
|
}
|
|
|
|
return stream.Send(res)
|
|
|
|
}
|
|
|
|
|
|
|
|
sub := bs.powChainService.ChainStartFeed().Subscribe(bs.chainStartChan)
|
|
|
|
defer sub.Unsubscribe()
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case chainStartTime := <-bs.chainStartChan:
|
|
|
|
log.Info("Sending ChainStart log and genesis time to connected validator clients")
|
|
|
|
res := &pb.ChainStartResponse{
|
|
|
|
Started: true,
|
|
|
|
GenesisTime: uint64(chainStartTime.Unix()),
|
|
|
|
}
|
|
|
|
return stream.Send(res)
|
|
|
|
case <-sub.Err():
|
|
|
|
log.Debug("Subscriber closed, exiting goroutine")
|
|
|
|
return nil
|
|
|
|
case <-bs.ctx.Done():
|
|
|
|
log.Debug("RPC context closed, exiting goroutine")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2019-01-28 15:40:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// CanonicalHead of the current beacon chain. This method is requested on-demand
|
|
|
|
// by a validator when it is their time to propose or attest.
|
|
|
|
func (bs *BeaconServer) CanonicalHead(ctx context.Context, req *ptypes.Empty) (*pbp2p.BeaconBlock, error) {
|
|
|
|
block, err := bs.beaconDB.ChainHead()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("could not get canonical head block: %v", err)
|
|
|
|
}
|
|
|
|
return block, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// LatestAttestation streams the latest processed attestations to the rpc clients.
|
|
|
|
func (bs *BeaconServer) LatestAttestation(req *ptypes.Empty, stream pb.BeaconService_LatestAttestationServer) error {
|
2019-01-31 18:54:24 +00:00
|
|
|
sub := bs.operationService.IncomingAttFeed().Subscribe(bs.incomingAttestation)
|
2019-01-28 15:40:40 +00:00
|
|
|
defer sub.Unsubscribe()
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case attestation := <-bs.incomingAttestation:
|
|
|
|
log.Info("Sending attestation to RPC clients")
|
|
|
|
if err := stream.Send(attestation); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
case <-sub.Err():
|
|
|
|
log.Debug("Subscriber closed, exiting goroutine")
|
|
|
|
return nil
|
|
|
|
case <-bs.ctx.Done():
|
|
|
|
log.Debug("RPC context closed, exiting goroutine")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|