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-06-20 03:59:02 +00:00
|
|
|
"github.com/ethereum/go-ethereum/sharding/database"
|
2018-06-05 23:03:15 +00:00
|
|
|
"github.com/ethereum/go-ethereum/sharding/mainchain"
|
2018-06-13 12:44:33 +00:00
|
|
|
"github.com/ethereum/go-ethereum/sharding/p2p"
|
2018-06-12 23:03:20 +00:00
|
|
|
"github.com/ethereum/go-ethereum/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-13 17:37:23 +00:00
|
|
|
config *params.Config
|
2018-06-08 20:15:18 +00:00
|
|
|
smcClient *mainchain.SMCClient
|
2018-06-13 12:44:33 +00:00
|
|
|
p2p *p2p.Server
|
2018-06-20 03:59:02 +00:00
|
|
|
shardChainDb *database.ShardDB
|
2018-05-22 11:53:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewNotary creates a new notary instance.
|
2018-06-20 03:59:02 +00:00
|
|
|
func NewNotary(config *params.Config, smcClient *mainchain.SMCClient, p2p *p2p.Server, shardChainDb *database.ShardDB) (*Notary, error) {
|
2018-06-13 17:37:23 +00:00
|
|
|
return &Notary{config, smcClient, p2p, shardChainDb}, 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-17 17:39:42 +00:00
|
|
|
if err := joinNotaryPool(n.smcClient, n.smcClient.Account(), 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
|
|
|
}
|