The core protocol of WoopChain
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
woop/api/service/staking/service.go

84 lines
2.0 KiB

6 years ago
package staking
import (
"github.com/ethereum/go-ethereum/log"
"github.com/harmony-one/harmony/api/proto/message"
6 years ago
"github.com/harmony-one/harmony/internal/utils"
6 years ago
"github.com/harmony-one/harmony/p2p"
)
6 years ago
// Service is the staking service.
6 years ago
type Service struct {
stopChan chan struct{}
stoppedChan chan struct{}
peerChan <-chan p2p.Peer
6 years ago
}
// New returns staking service.
func New(peerChan <-chan p2p.Peer) *Service {
return &Service{
stopChan: make(chan struct{}),
stoppedChan: make(chan struct{}),
peerChan: peerChan,
}
}
6 years ago
// StartService starts staking service.
6 years ago
func (s *Service) StartService() {
log.Info("Start Staking Service")
s.Init()
s.Run()
}
6 years ago
// Init initializes staking service.
func (s *Service) Init() {
}
6 years ago
// Run runs staking.
func (s *Service) Run() {
// Wait until peer info of beacon chain is ready.
go func() {
defer close(s.stoppedChan)
for {
select {
case peer := <-s.peerChan:
utils.GetLogInstance().Info("Running role conversion")
// TODO: Write some logic here.
s.DoService(&peer)
case <-s.stopChan:
return
}
}
}()
6 years ago
}
6 years ago
// DoService does staking.
func (s *Service) DoService(peer *p2p.Peer) {
utils.GetLogInstance().Info("Staking with Peer")
// TODO(minhdoan): How to use the p2p or pubsub to send Staking Message to beacon chain.
// See below of how to create a staking message.
}
func (s *Service) createStakingMessage() *message.Message {
// TODO(minhdoan): Add signature and public key.
// To add public we need to assign a fake account to new node initially.
return &message.Message{
Type: message.MessageType_NEWNODE_BEACON_STAKING,
Request: &message.Message_Staking{
Staking: &message.StakingRequest{
Transaction: []byte{},
NodeId: "",
},
},
}
6 years ago
}
6 years ago
// StopService stops staking service.
6 years ago
func (s *Service) StopService() {
6 years ago
utils.GetLogInstance().Info("Stopping staking service.")
s.stopChan <- struct{}{}
<-s.stoppedChan
utils.GetLogInstance().Info("Role conversion stopped.")
6 years ago
}