add staking service and hook up with networkinfo service

pull/385/head
Minh Doan 6 years ago committed by Minh Doan
parent c287d9db3c
commit 5d4418c548
  1. 4
      api/service/manager.go
  2. 54
      api/service/staking/service.go
  3. 4
      node/node.go

@ -47,10 +47,10 @@ func (t Type) String() string {
return "BlockProposal"
case NetworkInfo:
return "NetworkInfo"
case PeerDiscovery:
return "PeerDiscovery"
case Staking:
return "Staking"
case PeerDiscovery:
return "PeerDiscovery"
case Test:
return "Test"
case Done:

@ -5,21 +5,59 @@ import (
"github.com/harmony-one/harmony/p2p"
)
// Service is the struct for staking service.
// Service is the role conversion service.
type Service struct {
Host p2p.Host
stopChan chan struct{}
stoppedChan chan struct{}
peerChan chan *p2p.Peer
}
//StartService starts the staking service.
// NewService returns role conversion service.
func NewService(peerChan chan *p2p.Peer) *Service {
return &Service{
stopChan: make(chan struct{}),
stoppedChan: make(chan struct{}),
peerChan: peerChan,
}
}
// StartService starts role conversion service.
func (s *Service) StartService() {
utils.GetLogInstance().Info("Starting staking service.")
s.Init()
s.Run()
}
// Init initializes role conversion service.
func (s *Service) Init() {
}
// Run runs role conversion.
func (s *Service) Run() {
// Wait until peer info of beacon chain is ready.
peer := <-s.peerChan
go func() {
defer close(s.stoppedChan)
for {
select {
default:
utils.GetLogInstance().Info("Running role conversion")
// TODO: Write some logic here.
s.DoService(peer)
case <-s.stopChan:
return
}
}
}()
}
func (s *Service) createStakingTransaction() {
//creates staking transaction.
// DoService does role conversion.
func (s *Service) DoService(peer *p2p.Peer) {
}
// StopService shutdowns staking service.
// StopService stops role conversion service.
func (s *Service) StopService() {
utils.GetLogInstance().Info("Shutting down staking service.")
utils.GetLogInstance().Info("Stopping role conversion service.")
s.stopChan <- struct{}{}
<-s.stoppedChan
utils.GetLogInstance().Info("Role conversion stopped.")
}

@ -30,6 +30,7 @@ import (
consensus_service "github.com/harmony-one/harmony/api/service/consensus"
"github.com/harmony-one/harmony/api/service/explorer"
"github.com/harmony-one/harmony/api/service/networkinfo"
"github.com/harmony-one/harmony/api/service/staking"
"github.com/harmony-one/harmony/api/service/syncing"
"github.com/harmony-one/harmony/api/service/syncing/downloader"
downloader_pb "github.com/harmony-one/harmony/api/service/syncing/downloader/proto"
@ -692,7 +693,10 @@ func (node *Node) setupForBeaconValidator() {
func (node *Node) setupForNewNode() {
chanPeer := make(chan *p2p.Peer)
// Add network info serivce.
node.serviceManager.RegisterService(service_manager.NetworkInfo, networkinfo.NewService(chanPeer))
// Add staking service.
node.serviceManager.RegisterService(service_manager.Staking, staking.NewService(chanPeer))
}
// ServiceManagerSetup setups service store.

Loading…
Cancel
Save