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-08-22 19:15:21 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/validator/types"
|
2018-07-21 17:51:18 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2018-05-22 11:34:12 +00:00
|
|
|
)
|
|
|
|
|
2018-07-21 17:51:18 +00:00
|
|
|
var log = logrus.WithField("prefix", "proposer")
|
|
|
|
|
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-08-12 17:58:02 +00:00
|
|
|
ctx context.Context
|
|
|
|
cancel context.CancelFunc
|
2018-08-22 19:15:21 +00:00
|
|
|
beaconService types.BeaconValidator
|
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-08-22 19:15:21 +00:00
|
|
|
func NewProposer(ctx context.Context, beaconService types.BeaconValidator) *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-08-12 17:58:02 +00:00
|
|
|
ctx: ctx,
|
|
|
|
cancel: cancel,
|
|
|
|
beaconService: beaconService,
|
|
|
|
}
|
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-08-07 17:56:28 +00:00
|
|
|
go p.run(p.ctx.Done())
|
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-08-07 17:56:28 +00:00
|
|
|
func (p *Proposer) run(done <-chan struct{}) {
|
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-08-12 17:58:02 +00:00
|
|
|
case <-p.beaconService.ProposerAssignment():
|
|
|
|
log.Info("Performing proposer responsibility")
|
|
|
|
continue
|
2018-06-20 03:59:02 +00:00
|
|
|
}
|
2018-06-07 22:16:34 +00:00
|
|
|
}
|
2018-06-20 03:59:02 +00:00
|
|
|
}
|