prysm-pulse/sharding/observer/service.go
Preston Van Loon 68eba02cc2 Remove most of the remaining geth code and set up bazel (#235)
* Remove most of the remaining geth code and set up bazel for this

* chmod +x

* Add flake check

* better flake detection


Former-commit-id: 5c332ecbf2923943f646f1fe40befa95be883329 [formerly 99590fc354514584700e5ce8d7d30a8a7d541f29]
Former-commit-id: e5f919b553fe698e98090965d34eb721990b5693
2018-07-07 13:23:19 -04:00

54 lines
1.8 KiB
Go

// Package observer launches a service attached to the sharding node
// that simply observes activity across the sharded Ethereum network.
package observer
import (
"context"
"fmt"
"math/big"
"github.com/ethereum/go-ethereum/log"
"github.com/prysmaticlabs/geth-sharding/sharding"
"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/syncer"
)
// Observer holds functionality required to run an observer service
// in a sharded system. Must satisfy the Service interface defined in
// sharding/service.go.
type Observer struct {
p2p *p2p.Server
dbService *database.ShardDB
shardID int
shard *sharding.Shard
ctx context.Context
cancel context.CancelFunc
sync *syncer.Syncer
client *mainchain.SMCClient
}
// NewObserver creates a struct instance of a observer service,
// it will have access to a p2p server and a shardChainDB.
func NewObserver(p2p *p2p.Server, dbService *database.ShardDB, shardID int, sync *syncer.Syncer, client *mainchain.SMCClient) (*Observer, error) {
ctx, cancel := context.WithCancel(context.Background())
return &Observer{p2p, dbService, shardID, nil, ctx, cancel, sync, client}, nil
}
// Start the main loop for observer service.
func (o *Observer) Start() {
log.Info(fmt.Sprintf("Starting observer service"))
o.shard = sharding.NewShard(big.NewInt(int64(o.shardID)), o.dbService.DB())
go o.sync.HandleCollationBodyRequests(o.shard)
}
// Stop the main loop for observer service.
func (o *Observer) Stop() error {
// Triggers a cancel call in the service's context which shuts down every goroutine
// in this service.
defer o.cancel()
log.Info(fmt.Sprintf("Stopping observer service"))
return nil
}