2024-08-01 18:36:25 +00:00
|
|
|
package wakuv2
|
|
|
|
|
|
|
|
import (
|
2024-08-14 05:45:44 +00:00
|
|
|
"errors"
|
|
|
|
|
2024-08-01 18:36:25 +00:00
|
|
|
"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"
|
2024-08-07 12:34:04 +00:00
|
|
|
|
|
|
|
gethcommon "github.com/ethereum/go-ethereum/common"
|
2024-09-26 22:37:32 +00:00
|
|
|
gocommon "github.com/status-im/status-go/common"
|
2024-08-07 12:34:04 +00:00
|
|
|
"github.com/status-im/status-go/wakuv2/common"
|
2024-08-01 18:36:25 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// 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 {
|
2024-08-07 00:17:56 +00:00
|
|
|
err := w.sendQueue.Push(w.ctx, envelope, *priority)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-08-01 18:36:25 +00:00
|
|
|
} else {
|
2024-08-07 00:17:56 +00:00
|
|
|
err := w.sendQueue.Push(w.ctx, envelope)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-08-01 18:36:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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() {
|
2024-09-26 22:37:32 +00:00
|
|
|
defer gocommon.LogOnPanic()
|
2024-09-12 14:04:37 +00:00
|
|
|
defer w.wg.Done()
|
2024-08-01 18:36:25 +00:00
|
|
|
for {
|
|
|
|
var envelope *protocol.Envelope
|
|
|
|
|
|
|
|
select {
|
2024-08-07 00:17:56 +00:00
|
|
|
case envelope = <-w.sendQueue.Pop(w.ctx):
|
2024-08-01 18:36:25 +00:00
|
|
|
|
|
|
|
case <-w.ctx.Done():
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-08-08 07:15:48 +00:00
|
|
|
w.wg.Add(1)
|
|
|
|
go w.publishEnvelope(envelope)
|
|
|
|
}
|
|
|
|
}
|
2024-08-01 18:36:25 +00:00
|
|
|
|
2024-08-08 07:15:48 +00:00
|
|
|
func (w *Waku) publishEnvelope(envelope *protocol.Envelope) {
|
2024-09-26 22:37:32 +00:00
|
|
|
defer gocommon.LogOnPanic()
|
2024-08-08 07:15:48 +00:00
|
|
|
defer w.wg.Done()
|
2024-08-01 18:36:25 +00:00
|
|
|
|
2024-08-08 07:15:48 +00:00
|
|
|
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()))
|
2024-08-01 18:36:25 +00:00
|
|
|
|
2024-08-14 05:45:44 +00:00
|
|
|
var err error
|
2024-08-08 07:15:48 +00:00
|
|
|
// only used in testing to simulate going offline
|
|
|
|
if w.cfg.SkipPublishToTopic {
|
|
|
|
logger.Info("skipping publish to topic")
|
2024-08-14 05:45:44 +00:00
|
|
|
err = errors.New("test send failure")
|
|
|
|
} else {
|
|
|
|
err = w.messageSender.Send(publish.NewRequest(w.ctx, envelope))
|
2024-08-08 07:15:48 +00:00
|
|
|
}
|
2024-08-01 18:36:25 +00:00
|
|
|
|
2024-10-23 20:22:51 +00:00
|
|
|
/* TODO-nwaku
|
2024-08-08 07:15:48 +00:00
|
|
|
if w.statusTelemetryClient != nil {
|
|
|
|
if err == nil {
|
2024-08-14 05:45:44 +00:00
|
|
|
w.statusTelemetryClient.PushSentEnvelope(w.ctx, SentEnvelope{Envelope: envelope, PublishMethod: w.messageSender.PublishMethod()})
|
2024-08-08 07:15:48 +00:00
|
|
|
} else {
|
2024-08-14 05:45:44 +00:00
|
|
|
w.statusTelemetryClient.PushErrorSendingEnvelope(w.ctx, ErrorSendingEnvelope{Error: err, SentEnvelope: SentEnvelope{Envelope: envelope, PublishMethod: w.messageSender.PublishMethod()}})
|
2024-08-08 07:15:48 +00:00
|
|
|
}
|
2024-08-01 18:36:25 +00:00
|
|
|
}
|
2024-10-23 20:22:51 +00:00
|
|
|
*/
|
2024-08-01 18:36:25 +00:00
|
|
|
|
2024-08-08 07:15:48 +00:00
|
|
|
if err != nil {
|
2024-08-01 18:36:25 +00:00
|
|
|
logger.Error("could not send message", zap.Error(err))
|
|
|
|
w.SendEnvelopeEvent(common.EnvelopeEvent{
|
|
|
|
Hash: gethcommon.BytesToHash(envelope.Hash().Bytes()),
|
|
|
|
Event: common.EventEnvelopeExpired,
|
|
|
|
})
|
|
|
|
return
|
2024-08-08 07:15:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if !w.cfg.EnableStoreConfirmationForMessagesSent {
|
|
|
|
w.SendEnvelopeEvent(common.EnvelopeEvent{
|
|
|
|
Hash: gethcommon.BytesToHash(envelope.Hash().Bytes()),
|
|
|
|
Event: common.EventEnvelopeSent,
|
|
|
|
})
|
2024-08-01 18:36:25 +00:00
|
|
|
}
|
|
|
|
}
|