mirror of
https://github.com/status-im/status-go.git
synced 2025-02-08 04:43:52 +00:00
a3bff47800
- some minor progress to add nwaku in status-go - nwaku.go: GetNumConnectedPeers controls when passed pubsub is empty - waku_test.go: adapt TestWakuV2Store - add missing shard.go - feat_: build nwaku with nix and use build tags to choose between go-waku and nwaku (#5896) - chore_: update nwaku - nwaku bump (#5911) - bump: nwaku - chore: add USE_NWAKU env flag - fix: build libwaku only if needed - feat: testing discovery and dialing with nwaku integration (#5940) - message publisher and sent verifier (#5966) - storenode requestor for missing message retrieval and result iterator impl (#5971) - uncomment code that would allow status-go/go-waku to compile and libwaku test to run (#5986) - supporting peer exchange with nwaku (#5983) - store queries - ping - ping storenodes using AddrInfo (#6004) - dial, drop and retrieve connected peers (#6013) - integrate on-demand DNS discovery and implement discoverAndConnectPeers (#6017) - extract libwaku calls into WakuNode struct (#6027) - async nwaku - remove nwaku process loop - receive messages via relay (#6185) - extract timeout from context - use correct port field, get free ports and uncomment some functions (#6200) - enable filter/lightpush/px and setup rate limits - add protected topics
121 lines
3.2 KiB
Go
121 lines
3.2 KiB
Go
package wakuv2
|
|
|
|
import (
|
|
"errors"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
"github.com/waku-org/go-waku/waku/v2/api/publish"
|
|
"github.com/waku-org/go-waku/waku/v2/protocol"
|
|
"github.com/waku-org/go-waku/waku/v2/protocol/pb"
|
|
"github.com/waku-org/go-waku/waku/v2/protocol/relay"
|
|
|
|
gethcommon "github.com/ethereum/go-ethereum/common"
|
|
gocommon "github.com/status-im/status-go/common"
|
|
"github.com/status-im/status-go/wakuv2/common"
|
|
)
|
|
|
|
// Send injects a message into the waku send queue, to be distributed in the
|
|
// network in the coming cycles.
|
|
func (w *Waku) Send(pubsubTopic string, msg *pb.WakuMessage, priority *int) ([]byte, error) {
|
|
pubsubTopic = w.GetPubsubTopic(pubsubTopic)
|
|
if w.protectedTopicStore != nil {
|
|
privKey, err := w.protectedTopicStore.FetchPrivateKey(pubsubTopic)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if privKey != nil {
|
|
err = relay.SignMessage(privKey, msg, pubsubTopic)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
}
|
|
|
|
envelope := protocol.NewEnvelope(msg, msg.GetTimestamp(), pubsubTopic)
|
|
|
|
if priority != nil {
|
|
err := w.sendQueue.Push(w.ctx, envelope, *priority)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
} else {
|
|
err := w.sendQueue.Push(w.ctx, envelope)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
|
|
w.poolMu.Lock()
|
|
alreadyCached := w.envelopeCache.Has(gethcommon.BytesToHash(envelope.Hash().Bytes()))
|
|
w.poolMu.Unlock()
|
|
if !alreadyCached {
|
|
recvMessage := common.NewReceivedMessage(envelope, common.SendMessageType)
|
|
w.postEvent(recvMessage) // notify the local node about the new message
|
|
w.addEnvelope(recvMessage)
|
|
}
|
|
|
|
return envelope.Hash().Bytes(), nil
|
|
}
|
|
|
|
func (w *Waku) broadcast() {
|
|
defer gocommon.LogOnPanic()
|
|
defer w.wg.Done()
|
|
for {
|
|
var envelope *protocol.Envelope
|
|
|
|
select {
|
|
case envelope = <-w.sendQueue.Pop(w.ctx):
|
|
|
|
case <-w.ctx.Done():
|
|
return
|
|
}
|
|
|
|
w.wg.Add(1)
|
|
go w.publishEnvelope(envelope)
|
|
}
|
|
}
|
|
|
|
func (w *Waku) publishEnvelope(envelope *protocol.Envelope) {
|
|
defer gocommon.LogOnPanic()
|
|
defer w.wg.Done()
|
|
|
|
logger := w.logger.With(zap.Stringer("envelopeHash", envelope.Hash()), zap.String("pubsubTopic", envelope.PubsubTopic()), zap.String("contentTopic", envelope.Message().ContentTopic), zap.Int64("timestamp", envelope.Message().GetTimestamp()))
|
|
|
|
var err error
|
|
// only used in testing to simulate going offline
|
|
if w.cfg.SkipPublishToTopic {
|
|
logger.Info("skipping publish to topic")
|
|
err = errors.New("test send failure")
|
|
} else {
|
|
err = w.messageSender.Send(publish.NewRequest(w.ctx, envelope))
|
|
}
|
|
|
|
/* TODO-nwaku
|
|
if w.statusTelemetryClient != nil {
|
|
if err == nil {
|
|
w.statusTelemetryClient.PushSentEnvelope(w.ctx, SentEnvelope{Envelope: envelope, PublishMethod: w.messageSender.PublishMethod()})
|
|
} else {
|
|
w.statusTelemetryClient.PushErrorSendingEnvelope(w.ctx, ErrorSendingEnvelope{Error: err, SentEnvelope: SentEnvelope{Envelope: envelope, PublishMethod: w.messageSender.PublishMethod()}})
|
|
}
|
|
}
|
|
*/
|
|
|
|
if err != nil {
|
|
logger.Error("could not send message", zap.Error(err))
|
|
w.SendEnvelopeEvent(common.EnvelopeEvent{
|
|
Hash: gethcommon.BytesToHash(envelope.Hash().Bytes()),
|
|
Event: common.EventEnvelopeExpired,
|
|
})
|
|
return
|
|
}
|
|
|
|
if !w.cfg.EnableStoreConfirmationForMessagesSent {
|
|
w.SendEnvelopeEvent(common.EnvelopeEvent{
|
|
Hash: gethcommon.BytesToHash(envelope.Hash().Bytes()),
|
|
Event: common.EventEnvelopeSent,
|
|
})
|
|
}
|
|
}
|