Add randomness service to beacon leader

pull/433/head
Rongjian Lan 6 years ago
parent fad7f96158
commit f8aa5a218e
  1. 3
      api/service/manager.go
  2. 22
      api/service/randomness/service.go
  3. 6
      cmd/harmony.go
  4. 6
      node/node.go

@ -25,6 +25,7 @@ const (
ClientSupport
SupportExplorer
Consensus
Randomness
BlockProposal
NetworkInfo
PeerDiscovery
@ -43,6 +44,8 @@ func (t Type) String() string {
return "SupportExplorer"
case Consensus:
return "Consensus"
case Randomness:
return "Randomness"
case BlockProposal:
return "BlockProposal"
case NetworkInfo:

@ -1,21 +1,23 @@
package randgen
package randomness
import (
"github.com/harmony-one/harmony/drand"
"github.com/harmony-one/harmony/internal/utils"
)
// Service is the random generation service.
// Service is the randomness generation service.
type Service struct {
stopChan chan struct{}
stoppedChan chan struct{}
DRand *drand.DRand
}
// New returns random generation service.
func New() *Service {
return &Service{}
// New returns randomness generation service.
func New(dRand *drand.DRand) *Service {
return &Service{DRand: dRand}
}
// StartService starts random generation service.
// StartService starts randomness generation service.
func (s *Service) StartService() {
s.stopChan = make(chan struct{})
s.stoppedChan = make(chan struct{})
@ -24,11 +26,11 @@ func (s *Service) StartService() {
s.Run(s.stopChan, s.stoppedChan)
}
// Init initializes random generation.
// Init initializes randomness generation.
func (s *Service) Init() {
}
// Run runs random generation.
// Run runs randomness generation.
func (s *Service) Run(stopChan chan struct{}, stoppedChan chan struct{}) {
go func() {
defer close(stoppedChan)
@ -45,12 +47,12 @@ func (s *Service) Run(stopChan chan struct{}, stoppedChan chan struct{}) {
}()
}
// DoRandomGeneration does random generation.
// DoRandomGeneration does rarandomnessndom generation.
func (s *Service) DoRandomGeneration() {
}
// StopService stops random generation service.
// StopService stops randomness generation service.
func (s *Service) StopService() {
utils.GetLogInstance().Info("Stopping random generation service.")
s.stopChan <- struct{}{}

@ -9,6 +9,8 @@ import (
"runtime"
"time"
"github.com/harmony-one/harmony/drand"
"github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/log"
"github.com/harmony-one/harmony/consensus"
@ -244,6 +246,10 @@ func main() {
} else {
currentNode.Role = node.BeaconValidator
}
// Add randomness protocol
dRand := drand.New(host, shardID, peers, leader)
currentNode.DRand = dRand
} else {
if role == "leader" {
currentNode.Role = node.ShardLeader

@ -12,6 +12,8 @@ import (
"sync"
"time"
"github.com/harmony-one/harmony/drand"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethdb"
@ -28,6 +30,7 @@ import (
"github.com/harmony-one/harmony/api/service/discovery"
"github.com/harmony-one/harmony/api/service/explorer"
"github.com/harmony-one/harmony/api/service/networkinfo"
randomness_service "github.com/harmony-one/harmony/api/service/randomness"
"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"
@ -120,6 +123,7 @@ type Node struct {
pendingTransactions types.Transactions // All the transactions received but not yet processed for Consensus
transactionInConsensus []*types.Transaction // The transactions selected into the new block and under Consensus process
pendingTxMutex sync.Mutex
DRand *drand.DRand // The instance for distributed randomness protocol
blockchain *core.BlockChain // The blockchain for the shard where this node belongs
db *ethdb.LDBDatabase // LevelDB to store blockchain.
@ -620,6 +624,8 @@ func (node *Node) setupForBeaconLeader() {
node.serviceManager.RegisterService(service_manager.BlockProposal, blockproposal.New(node.Consensus.ReadySignal, node.WaitForConsensusReady))
// Register client support service.
node.serviceManager.RegisterService(service_manager.ClientSupport, clientsupport.New(node.blockchain.State, node.CallFaucetContract, node.SelfPeer.IP, node.SelfPeer.Port))
// Register randomness service
node.serviceManager.RegisterService(service_manager.Randomness, randomness_service.New(node.DRand))
}
func (node *Node) setupForBeaconValidator() {

Loading…
Cancel
Save