add global variables to keep all GroupID

Signed-off-by: Leo Chen <leo@harmony.one>
pull/529/head
Leo Chen 6 years ago
parent 742eb6d08e
commit 7b4e20408d
  1. 30
      api/service/config.go
  2. 11
      api/service/manager_test.go
  3. 13
      p2p/group.go

@ -1,6 +1,9 @@
package service
import (
"strconv"
"github.com/harmony-one/harmony/internal/configs/node"
"github.com/harmony-one/harmony/p2p"
)
@ -9,9 +12,36 @@ import (
// This is to pass node configuration to services and prvent
// cyclic imports
type NodeConfig struct {
// The three groupID design, please refer to https://github.com/harmony-one/harmony/blob/master/node/node.md#libp2p-integration
Beacon p2p.GroupID // the beacon group ID
Group p2p.GroupID // the group ID of the shard
Client p2p.GroupID // the client group ID of the shard
IsClient bool // whether this node is a client node, such as wallet/txgen
IsBeacon bool // whether this node is a beacon node or not
IsLeader bool // whether this node is a leader or not
ShardID uint32 // shardID of this node
Actions map[p2p.GroupID]p2p.ActionType // actions on the groups
}
// GroupIDShards is a map of Group ID
// key is the shard ID
// value is the corresponding group ID
var (
GroupIDShards map[p2p.ShardIDType]p2p.GroupID
GroupIDShardClients map[p2p.ShardIDType]p2p.GroupID
)
func init() {
GroupIDShards = make(map[p2p.ShardIDType]p2p.GroupID)
GroupIDShardClients = make(map[p2p.ShardIDType]p2p.GroupID)
// init beacon chain group IDs
GroupIDShards["0"] = p2p.GroupIDBeacon
GroupIDShardClients["0"] = p2p.GroupIDBeaconClient
for i := 1; i < nodeconfig.MaxShards; i++ {
sid := p2p.ShardIDType(strconv.Itoa(i))
GroupIDShards[sid] = p2p.NewGroupIDShard(sid)
GroupIDShardClients[sid] = p2p.NewGroupIDShardClient(sid)
}
}

@ -6,6 +6,8 @@ import (
"time"
msg_pb "github.com/harmony-one/harmony/api/proto/message"
"github.com/harmony-one/harmony/internal/configs/node"
"github.com/harmony-one/harmony/p2p"
)
type SupportSyncingTest struct {
@ -95,3 +97,12 @@ func TestStopServices(t *testing.T) {
t.Error("Service did not stop")
}
}
func TestInit(t *testing.T) {
if GroupIDShards[p2p.ShardIDType("0")] != p2p.GroupIDBeacon {
t.Errorf("GroupIDShards[0]: %v != GroupIDBeacon: %v", GroupIDShards[p2p.ShardIDType("0")], p2p.GroupIDBeacon)
}
if len(GroupIDShards) != nodeconfig.MaxShards {
t.Errorf("len(GroupIDShards): %v != TotalShards: %v", len(GroupIDShards), nodeconfig.MaxShards)
}
}

@ -30,6 +30,19 @@ const (
GroupIDUnknown GroupID = "B1acKh0lE"
)
// ShardIDType defines the data type of a shard ID
type ShardIDType string
// NewGroupIDShard returns a new groupID for a shard
func NewGroupIDShard(sid ShardIDType) GroupID {
return GroupID(fmt.Sprintf("harmony/0.0.1/shard/%s", sid))
}
// NewGroupIDShardClient returns a new groupID for a shard's client
func NewGroupIDShardClient(sid ShardIDType) GroupID {
return GroupID(fmt.Sprintf("harmony/0.0.1/shard/%s/client", sid))
}
// ActionType lists action on group
type ActionType uint

Loading…
Cancel
Save