2018-06-05 21:28:57 +00:00
|
|
|
// Package notary defines all relevant functionality for a Notary actor
|
2018-06-06 02:03:58 +00:00
|
|
|
// within a sharded Ethereum blockchain.
|
2018-05-22 11:53:15 +00:00
|
|
|
package notary
|
|
|
|
|
|
|
|
import (
|
2018-06-11 22:21:24 +00:00
|
|
|
"fmt"
|
|
|
|
|
2018-05-22 11:53:15 +00:00
|
|
|
"github.com/ethereum/go-ethereum/log"
|
2018-07-07 17:23:19 +00:00
|
|
|
"github.com/prysmaticlabs/geth-sharding/sharding/database"
|
|
|
|
"github.com/prysmaticlabs/geth-sharding/sharding/mainchain"
|
|
|
|
"github.com/prysmaticlabs/geth-sharding/sharding/p2p"
|
|
|
|
"github.com/prysmaticlabs/geth-sharding/sharding/params"
|
2018-05-22 11:53:15 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Notary holds functionality required to run a collation notary
|
|
|
|
// in a sharded system. Must satisfy the Service interface defined in
|
|
|
|
// sharding/service.go.
|
|
|
|
type Notary struct {
|
2018-06-29 00:56:51 +00:00
|
|
|
config *params.Config
|
|
|
|
smcClient *mainchain.SMCClient
|
|
|
|
p2p *p2p.Server
|
|
|
|
dbService *database.ShardDB
|
2018-05-22 11:53:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewNotary creates a new notary instance.
|
2018-06-29 00:56:51 +00:00
|
|
|
func NewNotary(config *params.Config, smcClient *mainchain.SMCClient, p2p *p2p.Server, dbService *database.ShardDB) (*Notary, error) {
|
|
|
|
return &Notary{config, smcClient, p2p, dbService}, nil
|
2018-05-22 11:53:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Start the main routine for a notary.
|
2018-06-11 22:21:24 +00:00
|
|
|
func (n *Notary) Start() {
|
2018-05-22 12:47:35 +00:00
|
|
|
log.Info("Starting notary service")
|
2018-06-11 22:21:24 +00:00
|
|
|
go n.notarizeCollations()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop the main loop for notarizing collations.
|
|
|
|
func (n *Notary) Stop() error {
|
|
|
|
log.Info("Stopping notary service")
|
|
|
|
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
|
|
|
|
// we are an eligible notary for collations.
|
2018-06-11 22:21:24 +00:00
|
|
|
func (n *Notary) 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-06-20 19:15:05 +00:00
|
|
|
if err := joinNotaryPool(n.smcClient, n.smcClient, n.config); err != nil {
|
2018-06-11 22:21:24 +00:00
|
|
|
log.Error(fmt.Sprintf("Could not fetch current block number: %v", err))
|
|
|
|
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-06-11 22:21:24 +00:00
|
|
|
log.Error(fmt.Sprintf("Could not fetch current block number: %v", err))
|
|
|
|
return
|
|
|
|
}
|
2018-05-22 11:53:15 +00:00
|
|
|
}
|