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-06-11 18:00:31 +00:00
|
|
|
"fmt"
|
2018-06-20 03:59:02 +00:00
|
|
|
"math/big"
|
2018-06-11 18:00:31 +00:00
|
|
|
|
2018-06-08 21:45:26 +00:00
|
|
|
"github.com/ethereum/go-ethereum/ethdb"
|
2018-05-22 16:42:49 +00:00
|
|
|
"github.com/ethereum/go-ethereum/log"
|
2018-06-20 03:59:02 +00:00
|
|
|
"github.com/ethereum/go-ethereum/sharding"
|
2018-06-13 12:44:33 +00:00
|
|
|
"github.com/ethereum/go-ethereum/sharding/p2p"
|
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-20 03:59:02 +00:00
|
|
|
p2p *p2p.Server
|
|
|
|
shard *sharding.Shard
|
|
|
|
ctx context.Context
|
|
|
|
cancel context.CancelFunc
|
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,
|
|
|
|
// it will have access to a p2p server and a shardChainDb.
|
2018-06-21 03:03:02 +00:00
|
|
|
func NewObserver(p2p *p2p.Server, shardChainDB ethdb.Database, shardID int) (*Observer, error) {
|
2018-06-20 03:59:02 +00:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
2018-06-21 03:03:02 +00:00
|
|
|
shard := sharding.NewShard(big.NewInt(int64(shardID)), shardChainDB)
|
2018-06-20 03:59:02 +00:00
|
|
|
return &Observer{p2p, shard, ctx, cancel}, 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-06-20 03:59:02 +00:00
|
|
|
log.Info(fmt.Sprintf("Starting observer service"))
|
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()
|
|
|
|
log.Info(fmt.Sprintf("Stopping observer service"))
|
2018-05-22 16:42:49 +00:00
|
|
|
return nil
|
|
|
|
}
|