2023-08-10 13:30:38 +00:00
|
|
|
package library
|
2022-04-12 12:12:14 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"C"
|
|
|
|
"encoding/json"
|
|
|
|
|
2024-05-03 16:07:03 +00:00
|
|
|
"github.com/waku-org/go-waku/waku/v2/protocol/legacy_store/pb"
|
2023-02-06 22:16:20 +00:00
|
|
|
wpb "github.com/waku-org/go-waku/waku/v2/protocol/pb"
|
2022-04-12 12:12:14 +00:00
|
|
|
)
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"time"
|
|
|
|
|
2022-10-19 19:39:32 +00:00
|
|
|
"github.com/libp2p/go-libp2p/core/peer"
|
2024-05-03 16:07:03 +00:00
|
|
|
"github.com/waku-org/go-waku/waku/v2/protocol/legacy_store"
|
2022-04-12 12:12:14 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type storePagingOptions struct {
|
|
|
|
PageSize uint64 `json:"pageSize,omitempty"`
|
|
|
|
Cursor *pb.Index `json:"cursor,omitempty"`
|
|
|
|
Forward bool `json:"forward,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type storeMessagesArgs struct {
|
2023-11-07 19:48:43 +00:00
|
|
|
Topic string `json:"pubsubTopic,omitempty"`
|
|
|
|
ContentTopics []string `json:"contentTopics,omitempty"`
|
|
|
|
StartTime *int64 `json:"startTime,omitempty"`
|
|
|
|
EndTime *int64 `json:"endTime,omitempty"`
|
|
|
|
PagingOptions *storePagingOptions `json:"pagingOptions,omitempty"`
|
2022-04-12 12:12:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type storeMessagesReply struct {
|
2023-02-06 22:16:20 +00:00
|
|
|
Messages []*wpb.WakuMessage `json:"messages,omitempty"`
|
2022-04-12 12:12:14 +00:00
|
|
|
PagingInfo storePagingOptions `json:"pagingInfo,omitempty"`
|
|
|
|
Error string `json:"error,omitempty"`
|
|
|
|
}
|
|
|
|
|
2024-05-03 16:07:03 +00:00
|
|
|
func queryResponse(ctx context.Context, instance *WakuInstance, args storeMessagesArgs, options []legacy_store.HistoryRequestOption) (string, error) {
|
|
|
|
res, err := instance.node.LegacyStore().Query(
|
2023-02-10 20:17:23 +00:00
|
|
|
ctx,
|
2024-05-03 16:07:03 +00:00
|
|
|
legacy_store.Query{
|
2023-11-13 22:52:46 +00:00
|
|
|
PubsubTopic: args.Topic,
|
2023-10-28 23:37:53 +00:00
|
|
|
ContentTopics: args.ContentTopics,
|
2023-02-10 20:17:23 +00:00
|
|
|
StartTime: args.StartTime,
|
|
|
|
EndTime: args.EndTime,
|
|
|
|
},
|
|
|
|
options...,
|
|
|
|
)
|
|
|
|
|
|
|
|
reply := storeMessagesReply{}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
reply.Error = err.Error()
|
2023-08-10 13:30:38 +00:00
|
|
|
return marshalJSON(reply)
|
2023-02-10 20:17:23 +00:00
|
|
|
}
|
|
|
|
reply.Messages = res.Messages
|
|
|
|
reply.PagingInfo = storePagingOptions{
|
|
|
|
PageSize: args.PagingOptions.PageSize,
|
|
|
|
Cursor: res.Cursor(),
|
|
|
|
Forward: args.PagingOptions.Forward,
|
|
|
|
}
|
|
|
|
|
2023-08-10 13:30:38 +00:00
|
|
|
return marshalJSON(reply)
|
2023-02-10 20:17:23 +00:00
|
|
|
}
|
|
|
|
|
2023-08-10 13:30:38 +00:00
|
|
|
// StoreQuery is used to retrieve historic messages using waku store protocol.
|
2023-12-15 14:46:21 +00:00
|
|
|
func StoreQuery(instance *WakuInstance, queryJSON string, peerID string, ms int) (string, error) {
|
|
|
|
if err := validateInstance(instance, MustBeStarted); err != nil {
|
|
|
|
return "", err
|
2022-04-12 12:12:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var args storeMessagesArgs
|
|
|
|
err := json.Unmarshal([]byte(queryJSON), &args)
|
|
|
|
if err != nil {
|
2023-08-10 13:30:38 +00:00
|
|
|
return "", err
|
2022-04-12 12:12:14 +00:00
|
|
|
}
|
|
|
|
|
2024-05-03 16:07:03 +00:00
|
|
|
options := []legacy_store.HistoryRequestOption{
|
|
|
|
legacy_store.WithAutomaticRequestID(),
|
|
|
|
legacy_store.WithPaging(args.PagingOptions.Forward, args.PagingOptions.PageSize),
|
|
|
|
legacy_store.WithCursor(args.PagingOptions.Cursor),
|
2022-04-12 12:12:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if peerID != "" {
|
|
|
|
p, err := peer.Decode(peerID)
|
|
|
|
if err != nil {
|
2023-08-10 13:30:38 +00:00
|
|
|
return "", err
|
2022-04-12 12:12:14 +00:00
|
|
|
}
|
2024-05-03 16:07:03 +00:00
|
|
|
options = append(options, legacy_store.WithPeer(p))
|
2022-04-12 12:12:14 +00:00
|
|
|
} else {
|
2024-05-03 16:07:03 +00:00
|
|
|
options = append(options, legacy_store.WithAutomaticPeerSelection())
|
2022-04-12 12:12:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var ctx context.Context
|
|
|
|
var cancel context.CancelFunc
|
|
|
|
|
|
|
|
if ms > 0 {
|
2023-12-15 14:46:21 +00:00
|
|
|
ctx, cancel = context.WithTimeout(instance.ctx, time.Duration(int(ms))*time.Millisecond)
|
2022-04-12 12:12:14 +00:00
|
|
|
defer cancel()
|
|
|
|
} else {
|
2023-12-15 14:46:21 +00:00
|
|
|
ctx = instance.ctx
|
2022-04-12 12:12:14 +00:00
|
|
|
}
|
|
|
|
|
2023-12-15 14:46:21 +00:00
|
|
|
return queryResponse(ctx, instance, args, options)
|
2023-02-10 20:17:23 +00:00
|
|
|
}
|
2022-08-09 18:26:57 +00:00
|
|
|
|
2023-08-10 13:30:38 +00:00
|
|
|
// StoreLocalQuery is used to retrieve historic messages stored in the localDB using waku store protocol.
|
2023-12-15 14:46:21 +00:00
|
|
|
func StoreLocalQuery(instance *WakuInstance, queryJSON string) (string, error) {
|
|
|
|
if err := validateInstance(instance, MustBeStarted); err != nil {
|
|
|
|
return "", err
|
2023-02-10 20:17:23 +00:00
|
|
|
}
|
2022-04-12 12:12:14 +00:00
|
|
|
|
2023-02-10 20:17:23 +00:00
|
|
|
var args storeMessagesArgs
|
|
|
|
err := json.Unmarshal([]byte(queryJSON), &args)
|
2022-04-12 12:12:14 +00:00
|
|
|
if err != nil {
|
2023-08-10 13:30:38 +00:00
|
|
|
return "", err
|
2022-04-12 12:12:14 +00:00
|
|
|
}
|
2023-02-10 20:17:23 +00:00
|
|
|
|
2024-05-03 16:07:03 +00:00
|
|
|
options := []legacy_store.HistoryRequestOption{
|
|
|
|
legacy_store.WithAutomaticRequestID(),
|
|
|
|
legacy_store.WithPaging(args.PagingOptions.Forward, args.PagingOptions.PageSize),
|
|
|
|
legacy_store.WithCursor(args.PagingOptions.Cursor),
|
|
|
|
legacy_store.WithLocalQuery(),
|
2022-04-12 12:12:14 +00:00
|
|
|
}
|
|
|
|
|
2023-12-15 14:46:21 +00:00
|
|
|
return queryResponse(instance.ctx, instance, args, options)
|
2022-04-12 12:12:14 +00:00
|
|
|
}
|