Skip to content

Commit

Permalink
chore_: more logs
Browse files Browse the repository at this point in the history
  • Loading branch information
kaichaosun committed May 29, 2024
1 parent 34e04eb commit d305d7f
Showing 1 changed file with 36 additions and 27 deletions.
63 changes: 36 additions & 27 deletions wakuv2/waku.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ type Waku struct {
sendMsgIDs map[string]map[gethcommon.Hash]uint32
sendMsgIDsMu sync.RWMutex

storePeerID peer.ID

topicHealthStatusChan chan peermanager.TopicHealthStatus
connStatusSubscriptions map[string]*types.ConnStatusSubscription
connStatusMu sync.Mutex
Expand Down Expand Up @@ -993,6 +995,9 @@ func (w *Waku) checkIfMessagesStored() {
for pubsubTopic, subMsgs := range w.sendMsgIDs {
var queryMsgIds []gethcommon.Hash
for msgID, sendTime := range subMsgs {
if len(queryMsgIds) >= 20 {
break
}
// message is sent 5 seconds ago, check if it's stored
if uint32(w.timesource.Now().Unix()) > sendTime+5 {
queryMsgIds = append(queryMsgIds, msgID)
Expand Down Expand Up @@ -1061,35 +1066,43 @@ func (w *Waku) Send(pubsubTopic string, msg *pb.WakuMessage) ([]byte, error) {

// ctx, peer, r.PubsubTopic, contentTopics, uint64(r.From), uint64(r.To), options, processEnvelopes
func (w *Waku) messageHashBasedQuery(ctx context.Context, hashes []gethcommon.Hash, pubsubTopic string) {
selectedPeers, err := w.node.PeerManager().SelectPeers(
peermanager.PeerSelectionCriteria{
SelectionType: peermanager.Automatic,
Proto: store.StoreQueryID_v300,
PubsubTopics: []string{pubsubTopic},
Ctx: ctx,
},
)
if err != nil {
w.logger.Warn("could not select peers", zap.Error(err))
return
selectedPeer := w.storePeerID
if selectedPeer == "" {
selectedPeers, err := w.node.PeerManager().SelectPeers(
peermanager.PeerSelectionCriteria{
SelectionType: peermanager.Automatic,
Proto: store.StoreQueryID_v300,
PubsubTopics: []string{pubsubTopic},
Ctx: ctx,
},
)
if err != nil {
w.logger.Error("could not select peers", zap.Error(err))
return
}
selectedPeer = selectedPeers[0]
}

var opts []store.RequestOption
requestID := protocol.GenerateRequestID()
opts = append(opts, store.WithRequestID(requestID))
opts = append(opts, store.WithPeer(selectedPeers[0]))
opts = append(opts, store.WithPeer(selectedPeer))

messageHashes := make([]pb.MessageHash, len(hashes))
for i, hash := range hashes {
messageHashes[i] = pb.ToMessageHash(hash.Bytes())
}

w.logger.Debug("store.queryByHash request", zap.String("requestID", hexutil.Encode(requestID)), zap.String("peerID", selectedPeer.String()), zap.Any("messageHashes", messageHashes))

result, err := w.node.Store().QueryByHash(ctx, messageHashes, opts...)
if err != nil {
w.logger.Warn("store.queryByHash failed", zap.String("requestID", hexutil.Encode(requestID)), zap.Error(err))
w.logger.Error("store.queryByHash failed", zap.String("requestID", hexutil.Encode(requestID)), zap.String("peerID", selectedPeer.String()), zap.Error(err))
return
}

w.logger.Debug("store.queryByHash result", zap.String("requestID", hexutil.Encode(requestID)), zap.Int("messages", len(result.Messages())))

var ackHashes []gethcommon.Hash
var missedHashes []gethcommon.Hash
for _, hash := range hashes {
Expand All @@ -1100,10 +1113,19 @@ func (w *Waku) messageHashBasedQuery(ctx context.Context, hashes []gethcommon.Ha
break
}
}

if found {
ackHashes = append(ackHashes, hash)
w.SendEnvelopeEvent(common.EnvelopeEvent{
Hash: hash,
Event: common.EventEnvelopeSent,
})
} else {
missedHashes = append(missedHashes, hash)
w.SendEnvelopeEvent(common.EnvelopeEvent{
Hash: hash,
Event: common.EventEnvelopeExpired,
})
}

subMsgs := w.sendMsgIDs[pubsubTopic]
Expand All @@ -1117,20 +1139,6 @@ func (w *Waku) messageHashBasedQuery(ctx context.Context, hashes []gethcommon.Ha

w.logger.Debug("Ack message hashes", zap.Any("ackHashes", ackHashes))
w.logger.Debug("Missed message hashes", zap.Any("missedHashes", missedHashes))

for _, hash := range ackHashes {
w.SendEnvelopeEvent(common.EnvelopeEvent{
Hash: hash,
Event: common.EventEnvelopeSent,
})
}

for _, hash := range missedHashes {
w.SendEnvelopeEvent(common.EnvelopeEvent{
Hash: hash,
Event: common.EventEnvelopeExpired,
})
}
}

func (w *Waku) query(ctx context.Context, peerID peer.ID, pubsubTopic string, topics []common.TopicType, from uint64, to uint64, requestID []byte, opts []legacy_store.HistoryRequestOption) (*legacy_store.Result, error) {
Expand Down Expand Up @@ -1165,6 +1173,7 @@ func (w *Waku) query(ctx context.Context, peerID peer.ID, pubsubTopic string, to
}

func (w *Waku) Query(ctx context.Context, peerID peer.ID, pubsubTopic string, topics []common.TopicType, from uint64, to uint64, opts []legacy_store.HistoryRequestOption, processEnvelopes bool) (cursor *storepb.Index, envelopesCount int, err error) {
w.storePeerID = peerID
requestID := protocol.GenerateRequestID()
pubsubTopic = w.getPubsubTopic(pubsubTopic)

Expand Down

0 comments on commit d305d7f

Please sign in to comment.