2018-06-05 23:03:15 +00:00
|
|
|
// Package proposer defines all relevant functionality for a Proposer actor
|
2018-08-12 17:58:02 +00:00
|
|
|
// within Ethereum 2.0.
|
2018-05-22 11:34:12 +00:00
|
|
|
package proposer
|
|
|
|
|
|
|
|
import (
|
2018-06-07 22:16:34 +00:00
|
|
|
"context"
|
|
|
|
|
2018-09-05 03:35:32 +00:00
|
|
|
"github.com/ethereum/go-ethereum/event"
|
|
|
|
"github.com/gogo/protobuf/proto"
|
|
|
|
"github.com/golang/protobuf/ptypes"
|
|
|
|
pbp2p "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
|
|
|
|
pb "github.com/prysmaticlabs/prysm/proto/beacon/rpc/v1"
|
2018-07-21 17:51:18 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2018-09-11 17:08:31 +00:00
|
|
|
blake2b "golang.org/x/crypto/blake2b"
|
2018-05-22 11:34:12 +00:00
|
|
|
)
|
|
|
|
|
2018-07-21 17:51:18 +00:00
|
|
|
var log = logrus.WithField("prefix", "proposer")
|
|
|
|
|
2018-09-05 03:35:32 +00:00
|
|
|
type rpcClientService interface {
|
|
|
|
ProposerServiceClient() pb.ProposerServiceClient
|
|
|
|
}
|
|
|
|
|
|
|
|
type assignmentAnnouncer interface {
|
|
|
|
ProposerAssignmentFeed() *event.Feed
|
|
|
|
}
|
|
|
|
|
2018-08-12 17:58:02 +00:00
|
|
|
// Proposer holds functionality required to run a block proposer
|
|
|
|
// in Ethereum 2.0. Must satisfy the Service interface defined in
|
2018-05-22 11:34:12 +00:00
|
|
|
// sharding/service.go.
|
2018-06-04 21:10:59 +00:00
|
|
|
type Proposer struct {
|
2018-09-05 03:35:32 +00:00
|
|
|
ctx context.Context
|
|
|
|
cancel context.CancelFunc
|
|
|
|
assigner assignmentAnnouncer
|
|
|
|
rpcClientService rpcClientService
|
|
|
|
assignmentChan chan *pbp2p.BeaconBlock
|
|
|
|
}
|
|
|
|
|
|
|
|
// Config options for proposer service.
|
|
|
|
type Config struct {
|
|
|
|
AssignmentBuf int
|
|
|
|
Assigner assignmentAnnouncer
|
|
|
|
Client rpcClientService
|
2018-06-04 21:10:59 +00:00
|
|
|
}
|
2018-05-22 11:34:12 +00:00
|
|
|
|
2018-08-12 17:58:02 +00:00
|
|
|
// NewProposer creates a new attester instance.
|
2018-09-05 03:35:32 +00:00
|
|
|
func NewProposer(ctx context.Context, cfg *Config) *Proposer {
|
2018-08-12 17:58:02 +00:00
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
2018-06-20 03:59:02 +00:00
|
|
|
return &Proposer{
|
2018-09-05 03:35:32 +00:00
|
|
|
ctx: ctx,
|
|
|
|
cancel: cancel,
|
|
|
|
assigner: cfg.Assigner,
|
|
|
|
rpcClientService: cfg.Client,
|
|
|
|
assignmentChan: make(chan *pbp2p.BeaconBlock, cfg.AssignmentBuf),
|
2018-08-12 17:58:02 +00:00
|
|
|
}
|
2018-05-22 11:34:12 +00:00
|
|
|
}
|
|
|
|
|
2018-08-12 17:58:02 +00:00
|
|
|
// Start the main routine for a proposer.
|
2018-06-11 22:21:24 +00:00
|
|
|
func (p *Proposer) Start() {
|
2018-08-12 17:58:02 +00:00
|
|
|
log.Info("Starting service")
|
2018-09-05 03:35:32 +00:00
|
|
|
client := p.rpcClientService.ProposerServiceClient()
|
|
|
|
go p.run(p.ctx.Done(), client)
|
2018-05-22 12:47:35 +00:00
|
|
|
}
|
|
|
|
|
2018-08-12 17:58:02 +00:00
|
|
|
// Stop the main loop.
|
2018-05-22 12:47:35 +00:00
|
|
|
func (p *Proposer) Stop() error {
|
2018-06-20 03:59:02 +00:00
|
|
|
defer p.cancel()
|
2018-08-12 17:58:02 +00:00
|
|
|
log.Info("Stopping service")
|
2018-05-22 11:34:12 +00:00
|
|
|
return nil
|
|
|
|
}
|
2018-06-11 22:21:24 +00:00
|
|
|
|
2018-08-12 17:58:02 +00:00
|
|
|
// run the main event loop that listens for a proposer assignment.
|
2018-09-05 03:35:32 +00:00
|
|
|
func (p *Proposer) run(done <-chan struct{}, client pb.ProposerServiceClient) {
|
|
|
|
sub := p.assigner.ProposerAssignmentFeed().Subscribe(p.assignmentChan)
|
|
|
|
defer sub.Unsubscribe()
|
2018-06-20 03:59:02 +00:00
|
|
|
for {
|
|
|
|
select {
|
2018-08-07 17:56:28 +00:00
|
|
|
case <-done:
|
2018-06-29 00:56:51 +00:00
|
|
|
log.Debug("Proposer context closed, exiting goroutine")
|
2018-06-20 03:59:02 +00:00
|
|
|
return
|
2018-09-05 03:35:32 +00:00
|
|
|
|
|
|
|
// TODO: On the beacon node side, calculate active and crystallized and update the
|
|
|
|
// active/crystallize state hash values in the proposed block.
|
|
|
|
|
|
|
|
// When we receive an assignment on a slot, we leverage the fields
|
|
|
|
// from the latest canonical beacon block to perform a proposal responsibility.
|
|
|
|
case latestBeaconBlock := <-p.assignmentChan:
|
2018-08-12 17:58:02 +00:00
|
|
|
log.Info("Performing proposer responsibility")
|
2018-09-05 03:35:32 +00:00
|
|
|
|
|
|
|
// Extract the hash of the latest beacon block to use as parent hash in
|
|
|
|
// the proposal.
|
|
|
|
data, err := proto.Marshal(latestBeaconBlock)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Could not marshal latest beacon block: %v", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
latestBlockHash := blake2b.Sum512(data)
|
|
|
|
|
|
|
|
// TODO: Implement real proposals with randao reveals and attestation fields.
|
|
|
|
req := &pb.ProposeRequest{
|
|
|
|
ParentHash: latestBlockHash[:],
|
|
|
|
SlotNumber: latestBeaconBlock.GetSlotNumber() + 1,
|
|
|
|
RandaoReveal: []byte{},
|
|
|
|
AttestationBitmask: []byte{},
|
|
|
|
AttestationAggregateSig: []uint32{},
|
|
|
|
Timestamp: ptypes.TimestampNow(),
|
|
|
|
}
|
|
|
|
|
|
|
|
res, err := client.ProposeBlock(p.ctx, req)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Could not propose block: %v", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
log.Infof("Block proposed successfully with hash 0x%x", res.BlockHash)
|
2018-06-20 03:59:02 +00:00
|
|
|
}
|
2018-06-07 22:16:34 +00:00
|
|
|
}
|
2018-06-20 03:59:02 +00:00
|
|
|
}
|