2018-07-26 01:57:19 +00:00
|
|
|
// Package attester defines all relevant functionality for a Attester actor
|
2018-06-06 02:03:58 +00:00
|
|
|
// within a sharded Ethereum blockchain.
|
2018-07-26 01:57:19 +00:00
|
|
|
package attester
|
2018-05-22 11:53:15 +00:00
|
|
|
|
|
|
|
import (
|
2018-07-20 21:31:26 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/client/mainchain"
|
|
|
|
"github.com/prysmaticlabs/prysm/client/params"
|
2018-07-30 06:14:50 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/database"
|
2018-07-24 15:21:58 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/p2p"
|
2018-07-21 17:51:18 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2018-05-22 11:53:15 +00:00
|
|
|
)
|
|
|
|
|
2018-07-26 01:57:19 +00:00
|
|
|
var log = logrus.WithField("prefix", "attester")
|
2018-07-21 17:51:18 +00:00
|
|
|
|
2018-07-26 01:57:19 +00:00
|
|
|
// Attester holds functionality required to run a collation attester
|
2018-05-22 11:53:15 +00:00
|
|
|
// in a sharded system. Must satisfy the Service interface defined in
|
|
|
|
// sharding/service.go.
|
2018-07-26 01:57:19 +00:00
|
|
|
type Attester struct {
|
2018-06-29 00:56:51 +00:00
|
|
|
config *params.Config
|
|
|
|
smcClient *mainchain.SMCClient
|
|
|
|
p2p *p2p.Server
|
2018-07-30 06:14:50 +00:00
|
|
|
dbService *database.DB
|
2018-05-22 11:53:15 +00:00
|
|
|
}
|
|
|
|
|
2018-07-26 01:57:19 +00:00
|
|
|
// NewAttester creates a new attester instance.
|
2018-07-30 06:14:50 +00:00
|
|
|
func NewAttester(config *params.Config, smcClient *mainchain.SMCClient, p2p *p2p.Server, dbService *database.DB) (*Attester, error) {
|
2018-08-03 21:31:52 +00:00
|
|
|
return &Attester{
|
|
|
|
config: config,
|
|
|
|
smcClient: smcClient,
|
|
|
|
p2p: p2p,
|
|
|
|
dbService: dbService,
|
|
|
|
}, nil
|
2018-05-22 11:53:15 +00:00
|
|
|
}
|
|
|
|
|
2018-07-26 01:57:19 +00:00
|
|
|
// Start the main routine for a attester.
|
|
|
|
func (n *Attester) Start() {
|
|
|
|
log.Info("Starting attester service")
|
2018-06-11 22:21:24 +00:00
|
|
|
go n.notarizeCollations()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop the main loop for notarizing collations.
|
2018-07-26 01:57:19 +00:00
|
|
|
func (n *Attester) Stop() error {
|
|
|
|
log.Info("Stopping attester service")
|
2018-06-11 22:21:24 +00:00
|
|
|
return nil
|
|
|
|
}
|
2018-05-22 11:53:15 +00:00
|
|
|
|
2018-06-17 17:39:42 +00:00
|
|
|
// notarizeCollations checks incoming block headers and determines if
|
2018-07-26 01:57:19 +00:00
|
|
|
// we are an eligible attester for collations.
|
|
|
|
func (n *Attester) notarizeCollations() {
|
2018-06-17 17:39:42 +00:00
|
|
|
|
2018-05-22 12:56:56 +00:00
|
|
|
// TODO: handle this better through goroutines. Right now, these methods
|
2018-06-06 15:04:20 +00:00
|
|
|
// are blocking.
|
2018-06-04 20:34:48 +00:00
|
|
|
if n.smcClient.DepositFlag() {
|
2018-07-26 01:57:19 +00:00
|
|
|
if err := joinAttesterPool(n.smcClient, n.smcClient); err != nil {
|
2018-07-10 02:27:23 +00:00
|
|
|
log.Errorf("Could not fetch current block number: %v", err)
|
2018-06-11 22:21:24 +00:00
|
|
|
return
|
2018-05-22 12:56:56 +00:00
|
|
|
}
|
|
|
|
}
|
2018-05-22 11:53:15 +00:00
|
|
|
|
2018-06-17 17:39:42 +00:00
|
|
|
if err := subscribeBlockHeaders(n.smcClient.ChainReader(), n.smcClient, n.smcClient.Account()); err != nil {
|
2018-07-10 02:27:23 +00:00
|
|
|
log.Errorf("Could not fetch current block number: %v", err)
|
2018-06-11 22:21:24 +00:00
|
|
|
return
|
|
|
|
}
|
2018-05-22 11:53:15 +00:00
|
|
|
}
|