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 ClientSupport
SupportExplorer SupportExplorer
Consensus Consensus
Randomness
BlockProposal BlockProposal
NetworkInfo NetworkInfo
PeerDiscovery PeerDiscovery
@ -43,6 +44,8 @@ func (t Type) String() string {
return "SupportExplorer" return "SupportExplorer"
case Consensus: case Consensus:
return "Consensus" return "Consensus"
case Randomness:
return "Randomness"
case BlockProposal: case BlockProposal:
return "BlockProposal" return "BlockProposal"
case NetworkInfo: case NetworkInfo:

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

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

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

Loading…
Cancel
Save