2018-05-22 11:53:15 +00:00
|
|
|
package notary
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/ethereum/go-ethereum/log"
|
2018-05-22 18:49:59 +00:00
|
|
|
"github.com/ethereum/go-ethereum/sharding/database"
|
2018-05-22 12:47:35 +00:00
|
|
|
"github.com/ethereum/go-ethereum/sharding/node"
|
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-05-22 18:49:59 +00:00
|
|
|
node node.Node
|
2018-05-24 23:36:20 +00:00
|
|
|
shardDB database.ShardBackend
|
2018-05-22 11:53:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewNotary creates a new notary instance.
|
2018-05-22 22:12:02 +00:00
|
|
|
func NewNotary(node node.Node) (*Notary, error) {
|
2018-05-22 18:49:59 +00:00
|
|
|
// Initializes a shardDB that writes to disk at /path/to/datadir/shardchaindata.
|
|
|
|
// This DB can be used by the Notary service to create Shard struct
|
|
|
|
// instances.
|
2018-05-24 23:36:20 +00:00
|
|
|
shardDB, err := database.NewShardDB(node.DataDirFlag(), "shardchaindata")
|
2018-05-22 18:49:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-05-25 15:52:18 +00:00
|
|
|
return &Notary{node, shardDB}, nil
|
2018-05-22 11:53:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Start the main routine for a notary.
|
|
|
|
func (n *Notary) Start() error {
|
2018-05-22 12:47:35 +00:00
|
|
|
log.Info("Starting notary service")
|
2018-05-22 11:53:15 +00:00
|
|
|
|
2018-05-22 12:56:56 +00:00
|
|
|
// TODO: handle this better through goroutines. Right now, these methods
|
|
|
|
// have their own nested channels and goroutines within them. We need
|
|
|
|
// to make this as flat as possible at the Notary layer.
|
|
|
|
if n.node.DepositFlagSet() {
|
|
|
|
if err := joinNotaryPool(n.node); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2018-05-22 11:53:15 +00:00
|
|
|
|
2018-05-22 12:56:56 +00:00
|
|
|
return subscribeBlockHeaders(n.node)
|
2018-05-22 12:47:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Stop the main loop for notarizing collations.
|
|
|
|
func (n *Notary) Stop() error {
|
|
|
|
log.Info("Stopping notary service")
|
2018-05-22 11:53:15 +00:00
|
|
|
return nil
|
|
|
|
}
|