2023-04-20 18:51:13 +00:00
|
|
|
package protocol
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/binary"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2023-07-06 18:21:23 +00:00
|
|
|
"strings"
|
2023-08-25 04:25:38 +00:00
|
|
|
|
|
|
|
"github.com/waku-org/go-waku/waku/v2/hash"
|
2023-04-20 18:51:13 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const MaxShardIndex = uint16(1023)
|
|
|
|
|
2023-08-25 04:25:38 +00:00
|
|
|
// ClusterIndex is the clusterID used in sharding space.
|
2023-10-31 10:50:13 +00:00
|
|
|
// For shardIDs allocation and other magic numbers refer to RFC 51
|
2023-08-25 04:25:38 +00:00
|
|
|
const ClusterIndex = 1
|
|
|
|
|
|
|
|
// GenerationZeroShardsCount is number of shards supported in generation-0
|
|
|
|
const GenerationZeroShardsCount = 8
|
|
|
|
|
2023-10-31 10:50:13 +00:00
|
|
|
var (
|
|
|
|
ErrInvalidShard = errors.New("invalid shard")
|
|
|
|
ErrInvalidShardCount = errors.New("invalid shard count")
|
|
|
|
ErrExpected130Bytes = errors.New("invalid data: expected 130 bytes")
|
|
|
|
)
|
|
|
|
|
2023-04-20 18:51:13 +00:00
|
|
|
type RelayShards struct {
|
2023-10-31 10:50:13 +00:00
|
|
|
ClusterID uint16 `json:"clusterID"`
|
|
|
|
ShardIDs []uint16 `json:"shardIDs"`
|
2023-04-20 18:51:13 +00:00
|
|
|
}
|
|
|
|
|
2023-10-31 10:50:13 +00:00
|
|
|
func NewRelayShards(clusterID uint16, shardIDs ...uint16) (RelayShards, error) {
|
|
|
|
shardIDSet := make(map[uint16]struct{})
|
|
|
|
for _, index := range shardIDs {
|
2023-04-20 18:51:13 +00:00
|
|
|
if index > MaxShardIndex {
|
2023-10-31 10:50:13 +00:00
|
|
|
return RelayShards{}, ErrInvalidShard
|
2023-04-20 18:51:13 +00:00
|
|
|
}
|
2023-10-31 10:50:13 +00:00
|
|
|
shardIDSet[index] = struct{}{} // dedup
|
2023-04-20 18:51:13 +00:00
|
|
|
}
|
|
|
|
|
2023-10-31 10:50:13 +00:00
|
|
|
if len(shardIDSet) == 0 {
|
|
|
|
return RelayShards{}, ErrInvalidShardCount
|
2023-04-20 18:51:13 +00:00
|
|
|
}
|
|
|
|
|
2023-10-31 10:50:13 +00:00
|
|
|
shardIDs = []uint16{}
|
|
|
|
for index := range shardIDSet {
|
|
|
|
shardIDs = append(shardIDs, index)
|
2023-04-20 18:51:13 +00:00
|
|
|
}
|
|
|
|
|
2023-10-31 10:50:13 +00:00
|
|
|
return RelayShards{ClusterID: clusterID, ShardIDs: shardIDs}, nil
|
2023-04-20 18:51:13 +00:00
|
|
|
}
|
|
|
|
|
2023-10-30 14:56:26 +00:00
|
|
|
func (rs RelayShards) Topics() []WakuPubSubTopic {
|
|
|
|
var result []WakuPubSubTopic
|
2023-10-31 10:50:13 +00:00
|
|
|
for _, i := range rs.ShardIDs {
|
|
|
|
result = append(result, NewStaticShardingPubsubTopic(rs.ClusterID, i))
|
2023-04-20 18:51:13 +00:00
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2024-03-14 14:21:47 +00:00
|
|
|
func (rs RelayShards) ContainsAnyShard(cluster uint16, indexes []uint16) bool {
|
2023-10-31 10:50:13 +00:00
|
|
|
if rs.ClusterID != cluster {
|
2023-04-20 18:51:13 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
found := false
|
2024-03-14 14:21:47 +00:00
|
|
|
for _, rsIdx := range rs.ShardIDs {
|
|
|
|
for _, idx := range indexes {
|
|
|
|
if rsIdx == idx {
|
|
|
|
return true
|
|
|
|
}
|
2023-04-20 18:51:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return found
|
|
|
|
}
|
|
|
|
|
2024-03-14 14:21:47 +00:00
|
|
|
func (rs RelayShards) Contains(cluster uint16, index uint16) bool {
|
|
|
|
return rs.ContainsAnyShard(cluster, []uint16{index})
|
|
|
|
}
|
|
|
|
|
2023-10-30 14:56:26 +00:00
|
|
|
func (rs RelayShards) ContainsShardPubsubTopic(topic WakuPubSubTopic) bool {
|
|
|
|
if shardedTopic, err := ToShardPubsubTopic(topic); err != nil {
|
2023-04-20 18:51:13 +00:00
|
|
|
return false
|
2023-10-30 14:56:26 +00:00
|
|
|
} else {
|
|
|
|
return rs.Contains(shardedTopic.Cluster(), shardedTopic.Shard())
|
2023-04-20 18:51:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-01 20:01:15 +00:00
|
|
|
func TopicsToRelayShards(topic ...string) ([]RelayShards, error) {
|
|
|
|
result := make([]RelayShards, 0)
|
|
|
|
dict := make(map[uint16]map[uint16]struct{})
|
|
|
|
for _, t := range topic {
|
2023-07-06 18:21:23 +00:00
|
|
|
if !strings.HasPrefix(t, StaticShardingPubsubTopicPrefix) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-06-01 20:01:15 +00:00
|
|
|
var ps StaticShardingPubsubTopic
|
|
|
|
err := ps.Parse(t)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-10-31 10:50:13 +00:00
|
|
|
shardIDs, ok := dict[ps.clusterID]
|
2023-06-01 20:01:15 +00:00
|
|
|
if !ok {
|
2023-10-31 10:50:13 +00:00
|
|
|
shardIDs = make(map[uint16]struct{})
|
2023-06-01 20:01:15 +00:00
|
|
|
}
|
|
|
|
|
2023-10-31 10:50:13 +00:00
|
|
|
shardIDs[ps.shardID] = struct{}{}
|
|
|
|
dict[ps.clusterID] = shardIDs
|
2023-06-01 20:01:15 +00:00
|
|
|
}
|
|
|
|
|
2023-10-31 10:50:13 +00:00
|
|
|
for clusterID, shardIDs := range dict {
|
|
|
|
idx := make([]uint16, 0, len(shardIDs))
|
|
|
|
for shardID := range shardIDs {
|
|
|
|
idx = append(idx, shardID)
|
2023-06-01 20:01:15 +00:00
|
|
|
}
|
|
|
|
|
2023-10-31 10:50:13 +00:00
|
|
|
rs, err := NewRelayShards(clusterID, idx...)
|
2023-06-01 20:01:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
result = append(result, rs)
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2023-04-20 18:51:13 +00:00
|
|
|
func (rs RelayShards) ContainsTopic(topic string) bool {
|
2023-10-30 14:56:26 +00:00
|
|
|
wTopic, err := ToWakuPubsubTopic(topic)
|
2023-04-20 18:51:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
2023-10-30 14:56:26 +00:00
|
|
|
return rs.ContainsShardPubsubTopic(wTopic)
|
2023-04-20 18:51:13 +00:00
|
|
|
}
|
|
|
|
|
2023-10-31 10:50:13 +00:00
|
|
|
func (rs RelayShards) ShardList() ([]byte, error) {
|
2023-04-20 18:51:13 +00:00
|
|
|
var result []byte
|
|
|
|
|
2023-10-31 10:50:13 +00:00
|
|
|
result = binary.BigEndian.AppendUint16(result, rs.ClusterID)
|
|
|
|
result = append(result, uint8(len(rs.ShardIDs)))
|
|
|
|
for _, index := range rs.ShardIDs {
|
2023-04-20 18:51:13 +00:00
|
|
|
result = binary.BigEndian.AppendUint16(result, index)
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2023-10-31 10:50:13 +00:00
|
|
|
func FromShardList(buf []byte) (RelayShards, error) {
|
2023-04-20 18:51:13 +00:00
|
|
|
if len(buf) < 3 {
|
|
|
|
return RelayShards{}, fmt.Errorf("insufficient data: expected at least 3 bytes, got %d bytes", len(buf))
|
|
|
|
}
|
|
|
|
|
|
|
|
cluster := binary.BigEndian.Uint16(buf[0:2])
|
|
|
|
length := int(buf[2])
|
|
|
|
|
|
|
|
if len(buf) != 3+2*length {
|
|
|
|
return RelayShards{}, fmt.Errorf("invalid data: `length` field is %d but %d bytes were provided", length, len(buf))
|
|
|
|
}
|
|
|
|
|
2023-10-31 10:50:13 +00:00
|
|
|
shardIDs := make([]uint16, length)
|
2023-04-20 18:51:13 +00:00
|
|
|
for i := 0; i < length; i++ {
|
2023-10-31 10:50:13 +00:00
|
|
|
shardIDs[i] = binary.BigEndian.Uint16(buf[3+2*i : 5+2*i])
|
2023-04-20 18:51:13 +00:00
|
|
|
}
|
|
|
|
|
2023-10-31 10:50:13 +00:00
|
|
|
return NewRelayShards(cluster, shardIDs...)
|
2023-04-20 18:51:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func setBit(n byte, pos uint) byte {
|
|
|
|
n |= (1 << pos)
|
|
|
|
return n
|
|
|
|
}
|
|
|
|
|
|
|
|
func hasBit(n byte, pos uint) bool {
|
|
|
|
val := n & (1 << pos)
|
|
|
|
return (val > 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rs RelayShards) BitVector() []byte {
|
|
|
|
// The value is comprised of a two-byte shard cluster index in network byte
|
|
|
|
// order concatenated with a 128-byte wide bit vector. The bit vector
|
|
|
|
// indicates which shards of the respective shard cluster the node is part
|
|
|
|
// of. The right-most bit in the bit vector represents shard 0, the left-most
|
|
|
|
// bit represents shard 1023.
|
|
|
|
var result []byte
|
2023-10-31 10:50:13 +00:00
|
|
|
result = binary.BigEndian.AppendUint16(result, rs.ClusterID)
|
2023-04-20 18:51:13 +00:00
|
|
|
|
|
|
|
vec := make([]byte, 128)
|
2023-10-31 10:50:13 +00:00
|
|
|
for _, index := range rs.ShardIDs {
|
2023-04-20 18:51:13 +00:00
|
|
|
n := vec[index/8]
|
|
|
|
vec[index/8] = byte(setBit(n, uint(index%8)))
|
|
|
|
}
|
|
|
|
|
|
|
|
return append(result, vec...)
|
|
|
|
}
|
|
|
|
|
2023-09-11 14:24:05 +00:00
|
|
|
// Generate a RelayShards from a byte slice
|
2023-04-20 18:51:13 +00:00
|
|
|
func FromBitVector(buf []byte) (RelayShards, error) {
|
|
|
|
if len(buf) != 130 {
|
2023-10-31 10:50:13 +00:00
|
|
|
return RelayShards{}, ErrExpected130Bytes
|
2023-04-20 18:51:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cluster := binary.BigEndian.Uint16(buf[0:2])
|
2023-10-31 10:50:13 +00:00
|
|
|
var shardIDs []uint16
|
2023-04-20 18:51:13 +00:00
|
|
|
|
|
|
|
for i := uint16(0); i < 128; i++ {
|
|
|
|
for j := uint(0); j < 8; j++ {
|
|
|
|
if !hasBit(buf[2+i], j) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-10-31 10:50:13 +00:00
|
|
|
shardIDs = append(shardIDs, uint16(j)+8*i)
|
2023-04-20 18:51:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-31 10:50:13 +00:00
|
|
|
return RelayShards{ClusterID: cluster, ShardIDs: shardIDs}, nil
|
2023-04-20 18:51:13 +00:00
|
|
|
}
|
2023-08-25 04:25:38 +00:00
|
|
|
|
|
|
|
// GetShardFromContentTopic runs Autosharding logic and returns a pubSubTopic
|
|
|
|
// This is based on Autosharding algorithm defined in RFC 51
|
2023-09-06 04:37:21 +00:00
|
|
|
func GetShardFromContentTopic(topic ContentTopic, shardCount int) StaticShardingPubsubTopic {
|
2023-08-25 04:25:38 +00:00
|
|
|
bytes := []byte(topic.ApplicationName)
|
2023-11-08 12:54:24 +00:00
|
|
|
bytes = append(bytes, []byte(topic.ApplicationVersion)...)
|
2023-08-25 04:25:38 +00:00
|
|
|
|
|
|
|
hash := hash.SHA256(bytes)
|
|
|
|
//We only use the last 64 bits of the hash as having more shards is unlikely.
|
|
|
|
hashValue := binary.BigEndian.Uint64(hash[24:])
|
|
|
|
|
|
|
|
shard := hashValue % uint64(shardCount)
|
|
|
|
|
|
|
|
return NewStaticShardingPubsubTopic(ClusterIndex, uint16(shard))
|
|
|
|
}
|
2023-09-29 05:13:25 +00:00
|
|
|
|
|
|
|
func GetPubSubTopicFromContentTopic(cTopicString string) (string, error) {
|
|
|
|
cTopic, err := StringToContentTopic(cTopicString)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("%s : %s", err.Error(), cTopicString)
|
|
|
|
}
|
|
|
|
pTopic := GetShardFromContentTopic(cTopic, GenerationZeroShardsCount)
|
|
|
|
|
|
|
|
return pTopic.String(), nil
|
|
|
|
}
|
2023-11-13 22:52:46 +00:00
|
|
|
|
|
|
|
func GeneratePubsubToContentTopicMap(pubsubTopic string, contentTopics []string) (map[string][]string, error) {
|
|
|
|
|
|
|
|
pubSubTopicMap := make(map[string][]string, 0)
|
|
|
|
|
|
|
|
if pubsubTopic == "" {
|
|
|
|
//Should we derive pubsub topic from contentTopic so that peer selection and discovery can be done accordingly?
|
|
|
|
for _, cTopic := range contentTopics {
|
|
|
|
pTopic, err := GetPubSubTopicFromContentTopic(cTopic)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
_, ok := pubSubTopicMap[pTopic]
|
|
|
|
if !ok {
|
|
|
|
pubSubTopicMap[pTopic] = []string{}
|
|
|
|
}
|
|
|
|
pubSubTopicMap[pTopic] = append(pubSubTopicMap[pTopic], cTopic)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
pubSubTopicMap[pubsubTopic] = append(pubSubTopicMap[pubsubTopic], contentTopics...)
|
|
|
|
}
|
|
|
|
return pubSubTopicMap, nil
|
|
|
|
}
|
2024-02-02 13:17:09 +00:00
|
|
|
|
|
|
|
func ShardsToTopics(clusterId int, shards []int) []string {
|
|
|
|
pubsubTopics := make([]string, len(shards))
|
|
|
|
for i, shard := range shards {
|
|
|
|
pubsubTopics[i] = NewStaticShardingPubsubTopic(uint16(clusterId), uint16(shard)).String()
|
|
|
|
}
|
|
|
|
return pubsubTopics
|
|
|
|
}
|