Skip to content

Commit

Permalink
fix: convert topic into UTF-8 compatible
Browse files Browse the repository at this point in the history
Signed-off-by: D4ryl00 <[email protected]>
  • Loading branch information
D4ryl00 committed Oct 4, 2024
1 parent 0334304 commit c32d6f0
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
5 changes: 3 additions & 2 deletions pkg/rendezvous/rotation.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package rendezvous

import (
"encoding/base64"
"fmt"
"sync"
"time"
Expand Down Expand Up @@ -49,7 +50,7 @@ func (r *RotationInterval) NextTimePeriod(at time.Time) time.Time {
}

func (r *RotationInterval) PointForRawRotation(rotation []byte) (*Point, error) {
return r.PointForRotation(string(rotation))
return r.PointForRotation(base64.StdEncoding.EncodeToString(rotation))
}

func (r *RotationInterval) PointForRotation(rotation string) (*Point, error) {
Expand Down Expand Up @@ -158,7 +159,7 @@ func (p *Point) RawTopic() []byte {
}

func (p *Point) RotationTopic() string {
return string(p.rotation)
return base64.StdEncoding.EncodeToString(p.rotation)
}

func (p *Point) RawRotationTopic() []byte {
Expand Down
15 changes: 8 additions & 7 deletions tinder_swiper.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package weshnet

import (
"context"
"encoding/base64"
"encoding/hex"
"fmt"
mrand "math/rand"
Expand Down Expand Up @@ -67,7 +68,7 @@ func (s *Swiper) RefreshContactRequest(ctx context.Context, topic []byte) (addrs

// canceling find peers
s.muRequest.Lock()
req, ok := s.inprogressLookup[string(topic)]
req, ok := s.inprogressLookup[base64.StdEncoding.EncodeToString(topic)]
if !ok {
err = fmt.Errorf("unknown topic")
s.muRequest.Unlock()
Expand Down Expand Up @@ -109,7 +110,7 @@ func (s *Swiper) WatchTopic(ctx context.Context, topic, seed []byte) <-chan peer
defer s.muRequest.Unlock()

s.logger.Debug("start watch for peer with",
logutil.PrivateString("topic", string(topic)),
logutil.PrivateString("topic", base64.StdEncoding.EncodeToString(topic)),
zap.String("seed", string(seed)))

var point *rendezvous.Point
Expand All @@ -124,15 +125,15 @@ func (s *Swiper) WatchTopic(ctx context.Context, topic, seed []byte) <-chan peer

for ctx.Err() == nil {
if point == nil || time.Now().After(point.Deadline()) {
point = s.rp.NewRendezvousPointForPeriod(time.Now(), string(topic), seed)
point = s.rp.NewRendezvousPointForPeriod(time.Now(), base64.StdEncoding.EncodeToString(topic), seed)
}

bstrat := s.backoffFactory()

// store watch peers informations to be later use by the refresh method to force a lookup
s.muRequest.Lock()
wctx, cancel := context.WithCancel(ctx)
s.inprogressLookup[string(topic)] = &swiperRequest{
s.inprogressLookup[base64.StdEncoding.EncodeToString(topic)] = &swiperRequest{
bstrat: bstrat,
wgRefresh: &wgRefresh,
ctx: wctx,
Expand All @@ -156,7 +157,7 @@ func (s *Swiper) WatchTopic(ctx context.Context, topic, seed []byte) <-chan peer
}

s.muRequest.Lock()
delete(s.inprogressLookup, string(topic))
delete(s.inprogressLookup, base64.StdEncoding.EncodeToString(topic))
s.muRequest.Unlock()

// wait all refresh job are done before closing the channel
Expand Down Expand Up @@ -212,13 +213,13 @@ func (s *Swiper) Announce(ctx context.Context, topic, seed []byte) {
var point *rendezvous.Point

s.logger.Debug("start announce for peer with",
logutil.PrivateString("topic", string(topic)),
logutil.PrivateString("topic", base64.StdEncoding.EncodeToString(topic)),
logutil.PrivateString("seed", string(seed)))

go func() {
for ctx.Err() == nil {
if point == nil || time.Now().After(point.Deadline()) {
point = s.rp.NewRendezvousPointForPeriod(time.Now(), string(topic), seed)
point = s.rp.NewRendezvousPointForPeriod(time.Now(), base64.StdEncoding.EncodeToString(topic), seed)
}

s.logger.Debug("self announce topic for time", logutil.PrivateString("topic", point.RotationTopic()))
Expand Down

0 comments on commit c32d6f0

Please sign in to comment.