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.
105 lines
2.6 KiB
105 lines
2.6 KiB
5 years ago
|
package nodeconfig
|
||
6 years ago
|
|
||
|
import (
|
||
|
"fmt"
|
||
6 years ago
|
"strconv"
|
||
6 years ago
|
)
|
||
|
|
||
|
// GroupID is a multicast group ID.
|
||
|
//
|
||
|
// It is a binary string,
|
||
|
// conducive to layering and scoped generation using cryptographic hash.
|
||
|
//
|
||
|
// Applications define their own group ID, without central allocation.
|
||
|
// A cryptographically secure random string of enough length – 32 bytes for
|
||
|
// example – may be used.
|
||
|
type GroupID string
|
||
|
|
||
|
func (id GroupID) String() string {
|
||
6 years ago
|
return fmt.Sprintf("%s", string(id))
|
||
6 years ago
|
}
|
||
|
|
||
6 years ago
|
// Const of group ID
|
||
|
const (
|
||
5 years ago
|
GroupIDBeacon GroupID = "%s/0.0.1/node/beacon"
|
||
|
GroupIDBeaconClient GroupID = "%s/0.0.1/client/beacon"
|
||
|
GroupIDShardPrefix GroupID = "%s/0.0.1/node/shard/%s"
|
||
|
GroupIDShardClientPrefix GroupID = "%s/0.0.1/client/shard/%s"
|
||
|
GroupIDGlobal GroupID = "%s/0.0.1/node/global"
|
||
|
GroupIDGlobalClient GroupID = "%s/0.0.1/node/global"
|
||
|
GroupIDUnknown GroupID = "%s/B1acKh0lE"
|
||
6 years ago
|
)
|
||
|
|
||
6 years ago
|
// ShardID defines the ID of a shard
|
||
|
type ShardID uint32
|
||
6 years ago
|
|
||
5 years ago
|
func getNetworkPrefix(shardID ShardID) (netPre string) {
|
||
|
switch GetShardConfig(uint32(shardID)).GetNetworkType() {
|
||
|
case Mainnet:
|
||
|
netPre = "harmony"
|
||
|
case Testnet:
|
||
|
netPre = "testnet"
|
||
|
case Pangaea:
|
||
|
netPre = "pangaea"
|
||
|
case Devnet:
|
||
|
netPre = "devnet"
|
||
|
case Localnet:
|
||
|
netPre = "local"
|
||
|
default:
|
||
|
netPre = "misc"
|
||
|
}
|
||
|
return
|
||
|
}
|
||
|
|
||
6 years ago
|
// NewGroupIDByShardID returns a new groupID for a shard
|
||
|
func NewGroupIDByShardID(shardID ShardID) GroupID {
|
||
|
if shardID == 0 {
|
||
5 years ago
|
return GroupID(fmt.Sprintf(GroupIDBeacon.String(), getNetworkPrefix(shardID)))
|
||
6 years ago
|
}
|
||
5 years ago
|
return GroupID(fmt.Sprintf(GroupIDShardPrefix.String(), getNetworkPrefix(shardID), strconv.Itoa(int(shardID))))
|
||
6 years ago
|
}
|
||
|
|
||
6 years ago
|
// NewClientGroupIDByShardID returns a new groupID for a shard's client
|
||
|
func NewClientGroupIDByShardID(shardID ShardID) GroupID {
|
||
|
if shardID == 0 {
|
||
5 years ago
|
return GroupID(fmt.Sprintf(GroupIDBeaconClient.String(), getNetworkPrefix(shardID)))
|
||
6 years ago
|
}
|
||
5 years ago
|
return GroupID(fmt.Sprintf(GroupIDShardClientPrefix.String(), getNetworkPrefix(shardID), strconv.Itoa(int(shardID))))
|
||
6 years ago
|
}
|
||
|
|
||
6 years ago
|
// ActionType lists action on group
|
||
|
type ActionType uint
|
||
|
|
||
|
// Const of different Action type
|
||
|
const (
|
||
|
ActionStart ActionType = iota
|
||
|
ActionPause
|
||
|
ActionResume
|
||
|
ActionStop
|
||
|
ActionUnknown
|
||
|
)
|
||
|
|
||
6 years ago
|
func (a ActionType) String() string {
|
||
|
switch a {
|
||
|
case ActionStart:
|
||
|
return "ActionStart"
|
||
|
case ActionPause:
|
||
|
return "ActionPause"
|
||
|
case ActionResume:
|
||
|
return "ActionResume"
|
||
|
case ActionStop:
|
||
|
return "ActionStop"
|
||
|
}
|
||
|
return "ActionUnknown"
|
||
|
}
|
||
|
|
||
6 years ago
|
// GroupAction specify action on corresponding group
|
||
|
type GroupAction struct {
|
||
|
Name GroupID
|
||
|
Action ActionType
|
||
|
}
|
||
|
|
||
6 years ago
|
func (g GroupAction) String() string {
|
||
|
return fmt.Sprintf("%s/%s", g.Name, g.Action)
|
||
|
}
|