2022-11-25 20:54:11 +00:00
|
|
|
package store
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/libp2p/go-libp2p/core/host"
|
|
|
|
libp2pProtocol "github.com/libp2p/go-libp2p/core/protocol"
|
2023-08-16 01:40:00 +00:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2023-08-10 12:58:22 +00:00
|
|
|
"github.com/waku-org/go-waku/waku/v2/peermanager"
|
2023-05-05 09:49:15 +00:00
|
|
|
"github.com/waku-org/go-waku/waku/v2/protocol/relay"
|
2022-12-09 03:08:04 +00:00
|
|
|
"github.com/waku-org/go-waku/waku/v2/timesource"
|
2022-11-25 20:54:11 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
|
|
|
// StoreID_v20beta4 is the current Waku Store protocol identifier
|
|
|
|
const StoreID_v20beta4 = libp2pProtocol.ID("/vac/waku/store/2.0.0-beta4")
|
|
|
|
|
|
|
|
// MaxPageSize is the maximum number of waku messages to return per page
|
2023-10-12 12:42:53 +00:00
|
|
|
const MaxPageSize = 20
|
2022-11-25 20:54:11 +00:00
|
|
|
|
|
|
|
// MaxContentFilters is the maximum number of allowed content filters in a query
|
|
|
|
const MaxContentFilters = 10
|
|
|
|
|
|
|
|
var (
|
|
|
|
// ErrMaxContentFilters is returned when the number of content topics in the query
|
|
|
|
// exceeds the limit
|
|
|
|
ErrMaxContentFilters = errors.New("exceeds the maximum number of content filters allowed")
|
|
|
|
|
|
|
|
// ErrNoPeersAvailable is returned when there are no store peers in the peer store
|
|
|
|
// that could be used to retrieve message history
|
|
|
|
ErrNoPeersAvailable = errors.New("no suitable remote peers")
|
|
|
|
|
2023-09-11 14:24:05 +00:00
|
|
|
// ErrInvalidID is returned when no RequestID is given
|
|
|
|
ErrInvalidID = errors.New("invalid request id")
|
2022-11-25 20:54:11 +00:00
|
|
|
|
|
|
|
// ErrFailedToResumeHistory is returned when the node attempted to retrieve historic
|
|
|
|
// messages to fill its own message history but for some reason it failed
|
|
|
|
ErrFailedToResumeHistory = errors.New("failed to resume the history")
|
|
|
|
|
|
|
|
// ErrFailedQuery is emitted when the query fails to return results
|
|
|
|
ErrFailedQuery = errors.New("failed to resolve the query")
|
|
|
|
)
|
|
|
|
|
2023-01-06 22:37:57 +00:00
|
|
|
type WakuSwap interface {
|
|
|
|
// TODO: add functions
|
|
|
|
}
|
|
|
|
|
2022-11-25 20:54:11 +00:00
|
|
|
type WakuStore struct {
|
2022-12-09 03:08:04 +00:00
|
|
|
ctx context.Context
|
2023-03-09 15:48:25 +00:00
|
|
|
cancel context.CancelFunc
|
2022-12-09 03:08:04 +00:00
|
|
|
timesource timesource.Timesource
|
2023-08-16 01:40:00 +00:00
|
|
|
metrics Metrics
|
2023-05-05 09:49:15 +00:00
|
|
|
MsgC relay.Subscription
|
2022-12-09 03:08:04 +00:00
|
|
|
wg *sync.WaitGroup
|
2022-11-25 20:54:11 +00:00
|
|
|
|
|
|
|
log *zap.Logger
|
|
|
|
|
|
|
|
started bool
|
|
|
|
|
|
|
|
msgProvider MessageProvider
|
|
|
|
h host.Host
|
2023-08-10 12:58:22 +00:00
|
|
|
pm *peermanager.PeerManager
|
2022-11-25 20:54:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewWakuStore creates a WakuStore using an specific MessageProvider for storing the messages
|
2023-08-10 12:58:22 +00:00
|
|
|
// Takes an optional peermanager if WakuStore is being created along with WakuNode.
|
|
|
|
// If using libp2p host, then pass peermanager as nil
|
2023-08-16 01:40:00 +00:00
|
|
|
func NewWakuStore(p MessageProvider, pm *peermanager.PeerManager, timesource timesource.Timesource, reg prometheus.Registerer, log *zap.Logger) *WakuStore {
|
2022-11-25 20:54:11 +00:00
|
|
|
wakuStore := new(WakuStore)
|
|
|
|
wakuStore.msgProvider = p
|
|
|
|
wakuStore.wg = &sync.WaitGroup{}
|
|
|
|
wakuStore.log = log.Named("store")
|
2022-12-09 03:08:04 +00:00
|
|
|
wakuStore.timesource = timesource
|
2023-08-10 12:58:22 +00:00
|
|
|
wakuStore.pm = pm
|
2023-08-16 01:40:00 +00:00
|
|
|
wakuStore.metrics = newMetrics(reg)
|
2022-11-25 20:54:11 +00:00
|
|
|
|
|
|
|
return wakuStore
|
|
|
|
}
|