2018-05-22 16:42:49 +00:00
|
|
|
// Package observer launches a service attached to the sharding node
|
|
|
|
// that simply observes activity across the sharded Ethereum network.
|
|
|
|
package observer
|
|
|
|
|
|
|
|
import (
|
2018-06-20 03:59:02 +00:00
|
|
|
"context"
|
2018-07-07 17:23:19 +00:00
|
|
|
"math/big"
|
2018-06-11 18:00:31 +00:00
|
|
|
|
2018-07-20 21:31:26 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/client/database"
|
|
|
|
"github.com/prysmaticlabs/prysm/client/mainchain"
|
|
|
|
"github.com/prysmaticlabs/prysm/client/p2p"
|
|
|
|
"github.com/prysmaticlabs/prysm/client/syncer"
|
|
|
|
"github.com/prysmaticlabs/prysm/client/types"
|
2018-07-21 17:51:18 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2018-05-22 16:42:49 +00:00
|
|
|
)
|
|
|
|
|
2018-07-21 17:51:18 +00:00
|
|
|
var log = logrus.WithField("prefix", "observer")
|
|
|
|
|
2018-05-22 16:42:49 +00:00
|
|
|
// Observer holds functionality required to run an observer service
|
|
|
|
// in a sharded system. Must satisfy the Service interface defined in
|
|
|
|
// sharding/service.go.
|
2018-06-04 21:10:59 +00:00
|
|
|
type Observer struct {
|
2018-06-29 00:56:51 +00:00
|
|
|
p2p *p2p.Server
|
|
|
|
dbService *database.ShardDB
|
|
|
|
shardID int
|
2018-07-09 02:40:34 +00:00
|
|
|
shard *types.Shard
|
2018-06-29 00:56:51 +00:00
|
|
|
ctx context.Context
|
|
|
|
cancel context.CancelFunc
|
2018-07-07 17:23:19 +00:00
|
|
|
sync *syncer.Syncer
|
|
|
|
client *mainchain.SMCClient
|
2018-06-04 21:10:59 +00:00
|
|
|
}
|
2018-05-22 16:42:49 +00:00
|
|
|
|
2018-06-20 03:59:02 +00:00
|
|
|
// NewObserver creates a struct instance of a observer service,
|
2018-06-29 00:56:51 +00:00
|
|
|
// it will have access to a p2p server and a shardChainDB.
|
2018-07-07 17:23:19 +00:00
|
|
|
func NewObserver(p2p *p2p.Server, dbService *database.ShardDB, shardID int, sync *syncer.Syncer, client *mainchain.SMCClient) (*Observer, error) {
|
2018-06-20 03:59:02 +00:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
2018-07-07 17:23:19 +00:00
|
|
|
return &Observer{p2p, dbService, shardID, nil, ctx, cancel, sync, client}, nil
|
2018-05-22 16:42:49 +00:00
|
|
|
}
|
|
|
|
|
2018-06-20 03:59:02 +00:00
|
|
|
// Start the main loop for observer service.
|
2018-06-12 04:46:53 +00:00
|
|
|
func (o *Observer) Start() {
|
2018-07-10 02:27:23 +00:00
|
|
|
log.Info("Starting observer service")
|
2018-07-09 02:40:34 +00:00
|
|
|
o.shard = types.NewShard(big.NewInt(int64(o.shardID)), o.dbService.DB())
|
2018-07-10 02:27:23 +00:00
|
|
|
go o.sync.HandleCollationBodyRequests(o.shard, o.ctx.Done())
|
2018-05-22 16:42:49 +00:00
|
|
|
}
|
|
|
|
|
2018-06-20 03:59:02 +00:00
|
|
|
// Stop the main loop for observer service.
|
2018-05-22 16:42:49 +00:00
|
|
|
func (o *Observer) Stop() error {
|
2018-06-20 03:59:02 +00:00
|
|
|
// Triggers a cancel call in the service's context which shuts down every goroutine
|
|
|
|
// in this service.
|
|
|
|
defer o.cancel()
|
2018-07-10 02:27:23 +00:00
|
|
|
log.Info("Stopping observer service")
|
2018-05-22 16:42:49 +00:00
|
|
|
return nil
|
|
|
|
}
|