@ -35,7 +35,6 @@ import (
"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"
downloader_pb "github.com/harmony-one/harmony/api/service/syncing/downloader/proto"
@ -197,6 +196,10 @@ type Node struct {
// Group Message Receiver
groupReceiver p2p . GroupReceiver
// Client Message Receiver to handle light client messages
// Beacon leader needs to use this receiver to talk to new node
clientReceiver p2p . GroupReceiver
// Duplicated Ping Message Received
duplicatedPing map [ string ] bool
@ -205,6 +208,9 @@ type Node struct {
// My GroupID
MyShardGroupID p2p . GroupID
// My ShardClient GroupID
MyClientGroupID p2p . GroupID
}
// Blockchain returns the blockchain from node
@ -229,7 +235,7 @@ func (node *Node) getTransactionsForNewBlock(maxNumTxs int) types.Transactions {
utils . GetLogInstance ( ) . Debug ( "Invalid transactions discarded" , "number" , len ( invalid ) )
node . pendingTransactions = unselected
utils . GetLogInstance ( ) . Debug ( "Remaining pending transactions" , "number" , len ( node . pendingTransactions ) )
utils . GetLogInstance ( ) . Debug ( "Remaining pending transactions" , "number" , len ( node . pendingTransactions ) , "selected" , len ( selected ) )
node . pendingTxMutex . Unlock ( )
return selected
}
@ -313,6 +319,7 @@ func New(host p2p.Host, consensus *bft.Consensus, db ethdb.Database) *Node {
if consensus != nil && consensus . IsLeader {
node . State = NodeLeader
go node . ReceiveClientGroupMessage ( )
} else {
node . State = NodeInit
}
@ -329,7 +336,11 @@ func New(host p2p.Host, consensus *bft.Consensus, db ethdb.Database) *Node {
node . duplicatedPing = make ( map [ string ] bool )
if utils . UseLibP2P {
node . startConsensus = make ( chan struct { } )
} else {
node . startConsensus = nil
}
return & node
}
@ -716,25 +727,45 @@ func decodeFuncSign(data []byte) string {
return funcSign
}
func ( node * Node ) setupForShardLeader ( ) {
func ( node * Node ) initNodeConfiguration ( ) ( service . NodeConfig , chan p2p . Peer ) {
chanPeer := make ( chan p2p . Peer )
nodeConfig := service . NodeConfig {
IsBeacon : false ,
IsClient : fals e,
IsClient : tru e,
Beacon : p2p . GroupIDBeacon ,
Group : p2p . GroupIDUnknown ,
Actions : make ( map [ p2p . GroupID ] p2p . ActionType ) ,
}
nodeConfig . Actions [ p2p . GroupIDBeacon ] = p2p . ActionStart
nodeConfig . Actions [ p2p . GroupIDBeaconClient ] = p2p . ActionStart
var err error
node . groupReceiver , err = node . host . GroupReceiver ( p2p . GroupIDBeacon )
node . groupReceiver , err = node . host . GroupReceiver ( p2p . GroupIDBeaconClient )
if err != nil {
utils . GetLogInstance ( ) . Error ( "create group receiver error" , "msg" , err )
return
}
return nodeConfig , chanPeer
}
func ( node * Node ) initBeaconNodeConfiguration ( ) ( service . NodeConfig , chan p2p . Peer ) {
nodeConfig , chanPeer := node . initNodeConfiguration ( )
nodeConfig . IsBeacon = true
var err error
// All beacon chain node will subscribe to BeaconClient topic
node . clientReceiver , err = node . host . GroupReceiver ( p2p . GroupIDBeaconClient )
if err != nil {
utils . GetLogInstance ( ) . Error ( "create client receiver error" , "msg" , err )
}
node . MyClientGroupID = p2p . GroupIDBeaconClient
return nodeConfig , chanPeer
}
func ( node * Node ) setupForShardLeader ( ) {
nodeConfig , chanPeer := node . initNodeConfiguration ( )
// Register peer discovery service. No need to do staking for beacon chain node.
node . serviceManager . RegisterService ( service_manager . PeerDiscovery , discovery . New ( node . host , nodeConfig , chanPeer ) )
// Register networkinfo service. "0" is the beacon shard ID
@ -753,47 +784,16 @@ func (node *Node) setupForShardLeader() {
}
func ( node * Node ) setupForShardValidator ( ) {
chanPeer := make ( chan p2p . Peer )
nodeConfig := service . NodeConfig {
IsBeacon : false ,
IsClient : false ,
Beacon : p2p . GroupIDBeacon ,
Group : p2p . GroupIDUnknown ,
Actions : make ( map [ p2p . GroupID ] p2p . ActionType ) ,
}
nodeConfig . Actions [ p2p . GroupIDBeacon ] = p2p . ActionStart
var err error
node . groupReceiver , err = node . host . GroupReceiver ( p2p . GroupIDBeacon )
if err != nil {
utils . GetLogInstance ( ) . Error ( "create group receiver error" , "msg" , err )
return
}
nodeConfig , chanPeer := node . initNodeConfiguration ( )
// Register peer discovery service. "0" is the beacon shard ID. No need to do staking for beacon chain node.
node . serviceManager . RegisterService ( service_manager . PeerDiscovery , discovery . New ( node . host , nodeConfig , chanPeer ) )
// Register networkinfo service. "0" is the beacon shard ID
node . serviceManager . RegisterService ( service_manager . NetworkInfo , networkinfo . New ( node . host , p2p . GroupIDBeacon , chanPeer ) )
}
func ( node * Node ) setupForBeaconLeader ( ) {
chanPeer := make ( chan p2p . Peer )
nodeConfig := service . NodeConfig {
IsBeacon : true ,
IsClient : false ,
Beacon : p2p . GroupIDBeacon ,
Group : p2p . GroupIDUnknown ,
Actions : make ( map [ p2p . GroupID ] p2p . ActionType ) ,
}
nodeConfig . Actions [ p2p . GroupIDBeacon ] = p2p . ActionStart
var err error
node . groupReceiver , err = node . host . GroupReceiver ( p2p . GroupIDBeacon )
if err != nil {
utils . GetLogInstance ( ) . Error ( "create group receiver error" , "msg" , err )
return
}
nodeConfig , chanPeer := node . initBeaconNodeConfiguration ( )
// Register peer discovery service. No need to do staking for beacon chain node.
node . serviceManager . RegisterService ( service_manager . PeerDiscovery , discovery . New ( node . host , nodeConfig , chanPeer ) )
@ -808,26 +808,10 @@ func (node *Node) setupForBeaconLeader() {
node . serviceManager . RegisterService ( service_manager . ClientSupport , clientsupport . New ( node . blockchain . State , node . CallFaucetContract , node . getDeployedStakingContract , node . SelfPeer . IP , node . SelfPeer . Port ) )
// Register randomness service
node . serviceManager . RegisterService ( service_manager . Randomness , randomness_service . New ( node . DRand ) )
}
func ( node * Node ) setupForBeaconValidator ( ) {
chanPeer := make ( chan p2p . Peer )
nodeConfig := service . NodeConfig {
IsBeacon : true ,
IsClient : false ,
Beacon : p2p . GroupIDBeacon ,
Group : p2p . GroupIDUnknown ,
Actions : make ( map [ p2p . GroupID ] p2p . ActionType ) ,
}
nodeConfig . Actions [ p2p . GroupIDBeacon ] = p2p . ActionStart
var err error
node . groupReceiver , err = node . host . GroupReceiver ( p2p . GroupIDBeacon )
if err != nil {
utils . GetLogInstance ( ) . Error ( "create group receiver error" , "msg" , err )
return
}
nodeConfig , chanPeer := node . initBeaconNodeConfiguration ( )
// Register peer discovery service. No need to do staking for beacon chain node.
node . serviceManager . RegisterService ( service_manager . PeerDiscovery , discovery . New ( node . host , nodeConfig , chanPeer ) )
@ -838,28 +822,12 @@ func (node *Node) setupForBeaconValidator() {
}
func ( node * Node ) setupForNewNode ( ) {
chanPeer := make ( chan p2p . Peer )
stakingPeer := make ( chan p2p . Peer )
nodeConfig := service . NodeConfig {
IsBeacon : false ,
IsClient : false ,
Beacon : p2p . GroupIDBeacon ,
Group : p2p . GroupIDUnknown ,
Actions : make ( map [ p2p . GroupID ] p2p . ActionType ) ,
}
nodeConfig . Actions [ p2p . GroupIDBeacon ] = p2p . ActionStart
var err error
node . groupReceiver , err = node . host . GroupReceiver ( p2p . GroupIDBeacon )
if err != nil {
utils . GetLogInstance ( ) . Error ( "create group receiver error" , "msg" , err )
return
}
nodeConfig , chanPeer := node . initNodeConfiguration ( )
// Register staking service.
node . serviceManager . RegisterService ( service_manager . Staking , staking . New ( node . AccountKey , 0 , stakingPeer ) )
// Register peer discovery service. "0" is the beacon shard ID
// node.serviceManager.RegisterService(service_manager.Staking, staking.New(node.AccountKey, 0, stakingPeer))
// TODO: (leo) no need to start discovery service for new node until we received the sharding info
// Register peer discovery service.
node . serviceManager . RegisterService ( service_manager . PeerDiscovery , discovery . New ( node . host , nodeConfig , chanPeer ) )
// Register networkinfo service. "0" is the beacon shard ID
node . serviceManager . RegisterService ( service_manager . NetworkInfo , networkinfo . New ( node . host , p2p . GroupIDBeacon , chanPeer ) )