mirror of https://github.com/status-im/go-waku.git
feat: more utils functions to setup a relay shards field on the enr
This commit is contained in:
parent
1bffa35dfa
commit
b0c094b0b6
|
@ -1,6 +1,8 @@
|
||||||
package enr
|
package enr
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
|
|
||||||
"github.com/ethereum/go-ethereum/p2p/enode"
|
"github.com/ethereum/go-ethereum/p2p/enode"
|
||||||
"github.com/ethereum/go-ethereum/p2p/enr"
|
"github.com/ethereum/go-ethereum/p2p/enr"
|
||||||
"github.com/waku-org/go-waku/waku/v2/protocol"
|
"github.com/waku-org/go-waku/waku/v2/protocol"
|
||||||
|
@ -24,7 +26,7 @@ func WithWakuRelayShardingBitVector(rs protocol.RelayShards) ENROption {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func WithtWakuRelaySharding(rs protocol.RelayShards) ENROption {
|
func WithWakuRelaySharding(rs protocol.RelayShards) ENROption {
|
||||||
return func(localnode *enode.LocalNode) error {
|
return func(localnode *enode.LocalNode) error {
|
||||||
if len(rs.Indices) >= 64 {
|
if len(rs.Indices) >= 64 {
|
||||||
return WithWakuRelayShardingBitVector(rs)(localnode)
|
return WithWakuRelayShardingBitVector(rs)(localnode)
|
||||||
|
@ -34,6 +36,21 @@ func WithtWakuRelaySharding(rs protocol.RelayShards) ENROption {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func WithWakuRelayShardingTopics(topics ...string) ENROption {
|
||||||
|
return func(localnode *enode.LocalNode) error {
|
||||||
|
rs, err := protocol.TopicsToRelayShards(topics...)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(rs) != 1 {
|
||||||
|
return errors.New("expected a single RelayShards")
|
||||||
|
}
|
||||||
|
|
||||||
|
return WithWakuRelaySharding(rs[0])(localnode)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// ENR record accessors
|
// ENR record accessors
|
||||||
|
|
||||||
func RelayShardingIndicesList(localnode *enode.LocalNode) (*protocol.RelayShards, error) {
|
func RelayShardingIndicesList(localnode *enode.LocalNode) (*protocol.RelayShards, error) {
|
||||||
|
|
|
@ -72,6 +72,42 @@ func (rs RelayShards) ContainsNamespacedTopic(topic NamespacedPubsubTopic) bool
|
||||||
return rs.Contains(shardedTopic.Cluster(), shardedTopic.Shard())
|
return rs.Contains(shardedTopic.Cluster(), shardedTopic.Shard())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TopicsToRelayShards(topic ...string) ([]RelayShards, error) {
|
||||||
|
result := make([]RelayShards, 0)
|
||||||
|
dict := make(map[uint16]map[uint16]struct{})
|
||||||
|
for _, t := range topic {
|
||||||
|
var ps StaticShardingPubsubTopic
|
||||||
|
err := ps.Parse(t)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
indices, ok := dict[ps.cluster]
|
||||||
|
if !ok {
|
||||||
|
indices = make(map[uint16]struct{})
|
||||||
|
}
|
||||||
|
|
||||||
|
indices[ps.shard] = struct{}{}
|
||||||
|
dict[ps.cluster] = indices
|
||||||
|
}
|
||||||
|
|
||||||
|
for cluster, indices := range dict {
|
||||||
|
idx := make([]uint16, 0, len(indices))
|
||||||
|
for index := range indices {
|
||||||
|
idx = append(idx, index)
|
||||||
|
}
|
||||||
|
|
||||||
|
rs, err := NewRelayShards(cluster, idx...)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
result = append(result, rs)
|
||||||
|
}
|
||||||
|
|
||||||
|
return result, nil
|
||||||
|
}
|
||||||
|
|
||||||
func (rs RelayShards) ContainsTopic(topic string) bool {
|
func (rs RelayShards) ContainsTopic(topic string) bool {
|
||||||
nsTopic, err := ToShardedPubsubTopic(topic)
|
nsTopic, err := ToShardedPubsubTopic(topic)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
Loading…
Reference in New Issue