Skip to content

Commit

Permalink
fix(rest/store): allow local queries (#1088)
Browse files Browse the repository at this point in the history
  • Loading branch information
richard-ramos authored Apr 17, 2024
1 parent 67bbbaf commit ea3f9d8
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 10 deletions.
6 changes: 6 additions & 0 deletions cmd/waku/server/rest/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,13 @@ func getStoreParams(r *http.Request) (*store.Query, []store.HistoryRequestOption
return nil, nil, err
}
options = append(options, store.WithPeerAddr(m))
} else {
// The user didn't specify a peer address and self-node is configured as a store node.
// In this case we assume that the user is willing to retrieve the messages stored by
// the local/self store node.
options = append(options, store.WithLocalQuery())
}

query.PubsubTopic = r.URL.Query().Get("pubsubTopic")

contentTopics := r.URL.Query().Get("contentTopics")
Expand Down
8 changes: 4 additions & 4 deletions waku/v2/protocol/lightpush/waku_lightpush.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,6 @@ func NewWakuLightPush(relay *relay.WakuRelay, pm *peermanager.PeerManager, reg p

wakuLP.limiter = params.limiter

if pm != nil {
wakuLP.pm.RegisterWakuProtocol(LightPushID_v20beta1, LightPushENRField)
}

return wakuLP
}

Expand All @@ -81,6 +77,10 @@ func (wakuLP *WakuLightPush) Start(ctx context.Context) error {
return errors.New("relay is required, without it, it is only a client and cannot be started")
}

if wakuLP.pm != nil {
wakuLP.pm.RegisterWakuProtocol(LightPushID_v20beta1, LightPushENRField)
}

ctx, cancel := context.WithCancel(ctx)

wakuLP.cancel = cancel
Expand Down
10 changes: 7 additions & 3 deletions waku/v2/protocol/store/waku_store_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,10 @@ func (store *WakuStore) localQuery(historyQuery *pb.HistoryRPC) (*pb.HistoryResp
return historyResponseRPC.Response, nil
}

func (store *WakuStore) isLocalQuery(p *HistoryRequestParameters) bool {
return p.localQuery && store.started
}

func (store *WakuStore) Query(ctx context.Context, query Query, opts ...HistoryRequestOption) (*Result, error) {
params := new(HistoryRequestParameters)
params.s = store
Expand All @@ -288,7 +292,7 @@ func (store *WakuStore) Query(ctx context.Context, query Query, opts ...HistoryR
}
}

if !params.localQuery {
if !store.isLocalQuery(params) {
pubsubTopics := []string{}
if query.PubsubTopic == "" {
for _, cTopic := range query.ContentTopics {
Expand Down Expand Up @@ -343,7 +347,7 @@ func (store *WakuStore) Query(ctx context.Context, query Query, opts ...HistoryR
historyRequest.Query.ContentFilters = append(historyRequest.Query.ContentFilters, &pb.ContentFilter{ContentTopic: cf})
}

if !params.localQuery && params.selectedPeer == "" {
if !store.isLocalQuery(params) && params.selectedPeer == "" {
store.metrics.RecordError(peerNotFoundFailure)
return nil, ErrNoPeersAvailable
}
Expand Down Expand Up @@ -373,7 +377,7 @@ func (store *WakuStore) Query(ctx context.Context, query Query, opts ...HistoryR

var response *pb.HistoryResponse

if params.localQuery {
if store.isLocalQuery(params) {
response, err = store.localQuery(historyRequest)
} else {
response, err = store.queryFrom(ctx, historyRequest, params.selectedPeer)
Expand Down
3 changes: 0 additions & 3 deletions waku/v2/protocol/store/waku_store_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,5 @@ func NewWakuStore(p MessageProvider, pm *peermanager.PeerManager, timesource tim
wakuStore.pm = pm
wakuStore.metrics = newMetrics(reg)

if pm != nil {
pm.RegisterWakuProtocol(StoreID_v20beta4, StoreENRField)
}
return wakuStore
}
4 changes: 4 additions & 0 deletions waku/v2/protocol/store/waku_store_protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@ func (store *WakuStore) Start(ctx context.Context, sub *relay.Subscription) erro
return err
}

if store.pm != nil {
store.pm.RegisterWakuProtocol(StoreID_v20beta4, StoreENRField)
}

store.started = true
store.ctx, store.cancel = context.WithCancel(ctx)
store.MsgC = sub
Expand Down

0 comments on commit ea3f9d8

Please sign in to comment.