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.
92 lines
2.2 KiB
92 lines
2.2 KiB
6 years ago
|
package resharding
|
||
6 years ago
|
|
||
|
import (
|
||
6 years ago
|
"time"
|
||
|
|
||
6 years ago
|
msg_pb "github.com/harmony-one/harmony/api/proto/message"
|
||
6 years ago
|
"github.com/harmony-one/harmony/core"
|
||
6 years ago
|
"github.com/harmony-one/harmony/internal/utils"
|
||
|
)
|
||
|
|
||
6 years ago
|
// Constants for resharding service.
|
||
|
const (
|
||
|
ReshardingCheckTime = time.Second
|
||
|
)
|
||
|
|
||
6 years ago
|
// Service is the role conversion service.
|
||
|
type Service struct {
|
||
|
stopChan chan struct{}
|
||
|
stoppedChan chan struct{}
|
||
6 years ago
|
messageChan chan *msg_pb.Message
|
||
6 years ago
|
beaconChain *core.BlockChain
|
||
6 years ago
|
}
|
||
|
|
||
6 years ago
|
// New returns role conversion service.
|
||
6 years ago
|
func New(beaconChain *core.BlockChain) *Service {
|
||
|
return &Service{beaconChain: beaconChain}
|
||
6 years ago
|
}
|
||
|
|
||
|
// StartService starts role conversion service.
|
||
|
func (s *Service) StartService() {
|
||
|
s.stopChan = make(chan struct{})
|
||
|
s.stoppedChan = make(chan struct{})
|
||
|
|
||
|
s.Init()
|
||
|
s.Run(s.stopChan, s.stoppedChan)
|
||
|
}
|
||
|
|
||
|
// Init initializes role conversion service.
|
||
|
func (s *Service) Init() {
|
||
|
}
|
||
|
|
||
|
// Run runs role conversion.
|
||
|
func (s *Service) Run(stopChan chan struct{}, stoppedChan chan struct{}) {
|
||
|
go func() {
|
||
|
defer close(stoppedChan)
|
||
|
for {
|
||
|
select {
|
||
|
default:
|
||
|
utils.GetLogInstance().Info("Running role conversion")
|
||
|
// TODO: Write some logic here.
|
||
|
s.DoService()
|
||
|
case <-stopChan:
|
||
|
return
|
||
|
}
|
||
|
}
|
||
|
}()
|
||
|
}
|
||
|
|
||
|
// DoService does role conversion.
|
||
|
func (s *Service) DoService() {
|
||
6 years ago
|
tick := time.NewTicker(ReshardingCheckTime)
|
||
|
// Get current shard state hash.
|
||
|
currentShardStateHash := s.beaconChain.CurrentBlock().Header().ShardStateHash
|
||
|
for {
|
||
|
select {
|
||
|
case <-tick.C:
|
||
|
LatestShardStateHash := s.beaconChain.CurrentBlock().Header().ShardStateHash
|
||
|
if currentShardStateHash != LatestShardStateHash {
|
||
|
// TODO(minhdoan): Add resharding logic later after modifying the resharding func as it current doesn't calculate the role (leader/validator)
|
||
|
}
|
||
|
}
|
||
|
}
|
||
6 years ago
|
}
|
||
|
|
||
|
// StopService stops role conversion service.
|
||
|
func (s *Service) StopService() {
|
||
|
utils.GetLogInstance().Info("Stopping role conversion service.")
|
||
|
s.stopChan <- struct{}{}
|
||
|
<-s.stoppedChan
|
||
|
utils.GetLogInstance().Info("Role conversion stopped.")
|
||
|
}
|
||
6 years ago
|
|
||
|
// NotifyService notify service
|
||
|
func (s *Service) NotifyService(params map[string]interface{}) {
|
||
|
return
|
||
|
}
|
||
6 years ago
|
|
||
|
// SetMessageChan sets up message channel to service.
|
||
|
func (s *Service) SetMessageChan(messageChan chan *msg_pb.Message) {
|
||
|
s.messageChan = messageChan
|
||
|
}
|