From 490570c0ece4d7d73564e4b9e335900c790626d8 Mon Sep 17 00:00:00 2001 From: Richard Ramos Date: Tue, 10 Jan 2023 10:50:49 -0400 Subject: [PATCH] fix: convert TopicType to stack variable and use recvMessage.Topic instead of extracting content topic from WakuMessage --- wakuv2/common/filter.go | 11 ++--------- wakuv2/common/message.go | 6 +++--- wakuv2/common/topic.go | 8 ++++---- 3 files changed, 9 insertions(+), 16 deletions(-) diff --git a/wakuv2/common/filter.go b/wakuv2/common/filter.go index f88762770..bfd3c3986 100644 --- a/wakuv2/common/filter.go +++ b/wakuv2/common/filter.go @@ -165,18 +165,11 @@ func (fs *Filters) NotifyWatchers(recvMessage *ReceivedMessage) bool { fs.mutex.RLock() defer fs.mutex.RUnlock() - topic, err := ExtractTopicFromContentTopic(recvMessage.Envelope.Message().ContentTopic) - if err != nil { - log.Trace(err.Error(), "topic", recvMessage.Envelope.Message().ContentTopic) - return false - } - var matched bool - - candidates := fs.GetWatchersByTopic(*topic) + candidates := fs.GetWatchersByTopic(recvMessage.Topic) if len(candidates) == 0 { - log.Debug("no filters available for this topic", "message", recvMessage.Hash().Hex(), "topic", (*topic).String()) + log.Debug("no filters available for this topic", "message", recvMessage.Hash().Hex(), "topic", recvMessage.Topic.String()) } for _, watcher := range candidates { diff --git a/wakuv2/common/message.go b/wakuv2/common/message.go index 41331c309..27b92b70e 100644 --- a/wakuv2/common/message.go +++ b/wakuv2/common/message.go @@ -154,14 +154,14 @@ type MemoryMessageStore struct { func NewReceivedMessage(env *protocol.Envelope, msgType MessageType) *ReceivedMessage { ct, err := ExtractTopicFromContentTopic(env.Message().ContentTopic) if err != nil { - ct = new(TopicType) + log.Error("failed to extract content topic from message", "topic", env.Message().ContentTopic, "err", err) } return &ReceivedMessage{ Envelope: env, MsgType: msgType, Sent: uint32(env.Message().Timestamp / int64(time.Second)), - Topic: *ct, + Topic: ct, } } @@ -241,7 +241,7 @@ func (msg *ReceivedMessage) Open(watcher *Filter) (result *ReceivedMessage) { return nil } - result.Topic = *ct + result.Topic = ct return result } diff --git a/wakuv2/common/topic.go b/wakuv2/common/topic.go index 7c2f79b02..5dc703e35 100644 --- a/wakuv2/common/topic.go +++ b/wakuv2/common/topic.go @@ -69,18 +69,18 @@ func (t TopicType) ContentTopic() string { return "/waku/1/" + enc + "/rfc26" } -func ExtractTopicFromContentTopic(s string) (*TopicType, error) { +func ExtractTopicFromContentTopic(s string) (TopicType, error) { p := strings.Split(s, "/") if len(p) != 5 || p[1] != "waku" || p[2] != "1" || p[4] != "rfc26" { - return nil, errors.New("invalid content topic format") + return TopicType{}, errors.New("invalid content topic format") } str, err := hexutil.Decode(p[3]) if err != nil { - return nil, err + return TopicType{}, err } result := BytesToTopic(str) - return &result, nil + return result, nil }