2022-11-25 16:54:11 -04:00
package store
import (
"context"
2023-10-30 12:55:36 -04:00
"encoding/hex"
2022-11-25 16:54:11 -04:00
"errors"
"math"
"sync"
"time"
2023-04-16 20:04:12 -04:00
"github.com/libp2p/go-libp2p/core/host"
2022-11-25 16:54:11 -04:00
"github.com/libp2p/go-libp2p/core/network"
"github.com/libp2p/go-libp2p/core/peer"
2023-02-06 18:16:20 -04:00
"github.com/libp2p/go-msgio/pbio"
2022-11-25 16:54:11 -04:00
"go.uber.org/zap"
"github.com/waku-org/go-waku/logging"
"github.com/waku-org/go-waku/waku/persistence"
"github.com/waku-org/go-waku/waku/v2/protocol"
2023-02-06 18:16:20 -04:00
wpb "github.com/waku-org/go-waku/waku/v2/protocol/pb"
2023-05-05 15:19:15 +05:30
"github.com/waku-org/go-waku/waku/v2/protocol/relay"
2023-02-06 18:16:20 -04:00
"github.com/waku-org/go-waku/waku/v2/protocol/store/pb"
2022-12-08 23:08:04 -04:00
"github.com/waku-org/go-waku/waku/v2/timesource"
2022-11-25 16:54:11 -04:00
)
2023-02-06 18:16:20 -04:00
func findMessages ( query * pb . HistoryQuery , msgProvider MessageProvider ) ( [ ] * wpb . WakuMessage , * pb . PagingInfo , error ) {
2022-11-25 16:54:11 -04:00
if query . PagingInfo == nil {
query . PagingInfo = & pb . PagingInfo {
Direction : pb . PagingInfo_FORWARD ,
}
}
if query . PagingInfo . PageSize == 0 || query . PagingInfo . PageSize > uint64 ( MaxPageSize ) {
query . PagingInfo . PageSize = MaxPageSize
}
cursor , queryResult , err := msgProvider . Query ( query )
if err != nil {
return nil , nil , err
}
if len ( queryResult ) == 0 { // no pagination is needed for an empty list
return nil , & pb . PagingInfo { Cursor : nil } , nil
}
2023-02-06 18:16:20 -04:00
resultMessages := make ( [ ] * wpb . WakuMessage , len ( queryResult ) )
2022-11-25 16:54:11 -04:00
for i := range queryResult {
resultMessages [ i ] = queryResult [ i ] . Message
}
return resultMessages , & pb . PagingInfo { Cursor : cursor } , nil
}
func ( store * WakuStore ) FindMessages ( query * pb . HistoryQuery ) * pb . HistoryResponse {
result := new ( pb . HistoryResponse )
messages , newPagingInfo , err := findMessages ( query , store . msgProvider )
if err != nil {
if err == persistence . ErrInvalidCursor {
result . Error = pb . HistoryResponse_INVALID_CURSOR
} else {
// TODO: return error in pb.HistoryResponse
store . log . Error ( "obtaining messages from db" , zap . Error ( err ) )
}
}
result . Messages = messages
result . PagingInfo = newPagingInfo
return result
}
type MessageProvider interface {
GetAll ( ) ( [ ] persistence . StoredMessage , error )
Query ( query * pb . HistoryQuery ) ( * pb . Index , [ ] persistence . StoredMessage , error )
2023-04-19 16:54:33 -04:00
Validate ( env * protocol . Envelope ) error
2022-11-25 16:54:11 -04:00
Put ( env * protocol . Envelope ) error
MostRecentTimestamp ( ) ( int64 , error )
2023-03-09 11:48:25 -04:00
Start ( ctx context . Context , timesource timesource . Timesource ) error
2022-11-25 16:54:11 -04:00
Stop ( )
Count ( ) ( int , error )
}
type Store interface {
2023-04-16 20:04:12 -04:00
SetHost ( h host . Host )
2023-10-21 01:26:18 +05:30
Start ( context . Context , * relay . Subscription ) error
2022-11-25 16:54:11 -04:00
Query ( ctx context . Context , query Query , opts ... HistoryRequestOption ) ( * Result , error )
2023-02-06 18:16:20 -04:00
Find ( ctx context . Context , query Query , cb criteriaFN , opts ... HistoryRequestOption ) ( * wpb . WakuMessage , error )
2022-11-25 16:54:11 -04:00
Next ( ctx context . Context , r * Result ) ( * Result , error )
Resume ( ctx context . Context , pubsubTopic string , peerList [ ] peer . ID ) ( int , error )
Stop ( )
}
// SetMessageProvider allows switching the message provider used with a WakuStore
func ( store * WakuStore ) SetMessageProvider ( p MessageProvider ) {
store . msgProvider = p
}
2023-04-16 20:04:12 -04:00
// Sets the host to be able to mount or consume a protocol
func ( store * WakuStore ) SetHost ( h host . Host ) {
store . h = h
}
2022-11-25 16:54:11 -04:00
// Start initializes the WakuStore by enabling the protocol and fetching records from a message provider
2023-10-21 01:26:18 +05:30
func ( store * WakuStore ) Start ( ctx context . Context , sub * relay . Subscription ) error {
2022-11-25 16:54:11 -04:00
if store . started {
2022-12-08 23:08:04 -04:00
return nil
2022-11-25 16:54:11 -04:00
}
if store . msgProvider == nil {
store . log . Info ( "Store protocol started (no message provider)" )
2022-12-08 23:08:04 -04:00
return nil
}
2023-03-09 11:48:25 -04:00
err := store . msgProvider . Start ( ctx , store . timesource ) // TODO: store protocol should not start a message provider
2022-12-08 23:08:04 -04:00
if err != nil {
store . log . Error ( "Error starting message provider" , zap . Error ( err ) )
2023-04-19 12:09:03 -04:00
return err
2022-11-25 16:54:11 -04:00
}
store . started = true
2023-03-09 11:48:25 -04:00
store . ctx , store . cancel = context . WithCancel ( ctx )
2023-05-05 15:19:15 +05:30
store . MsgC = sub
2022-11-25 16:54:11 -04:00
store . h . SetStreamHandlerMatch ( StoreID_v20beta4 , protocol . PrefixTextMatch ( string ( StoreID_v20beta4 ) ) , store . onRequest )
2023-04-19 16:54:33 -04:00
store . wg . Add ( 1 )
2023-03-09 11:48:25 -04:00
go store . storeIncomingMessages ( store . ctx )
2022-11-25 16:54:11 -04:00
store . log . Info ( "Store protocol started" )
2022-12-08 23:08:04 -04:00
return nil
2022-11-25 16:54:11 -04:00
}
func ( store * WakuStore ) storeMessage ( env * protocol . Envelope ) error {
if env . Message ( ) . Ephemeral {
return nil
}
2023-04-19 16:54:33 -04:00
err := store . msgProvider . Validate ( env )
if err != nil {
return err
}
err = store . msgProvider . Put ( env )
2022-11-25 16:54:11 -04:00
if err != nil {
store . log . Error ( "storing message" , zap . Error ( err ) )
2023-08-15 21:40:00 -04:00
store . metrics . RecordError ( storeFailure )
2022-11-25 16:54:11 -04:00
return err
}
return nil
}
func ( store * WakuStore ) storeIncomingMessages ( ctx context . Context ) {
defer store . wg . Done ( )
2023-05-05 15:19:15 +05:30
for envelope := range store . MsgC . Ch {
2022-11-25 16:54:11 -04:00
go func ( env * protocol . Envelope ) {
_ = store . storeMessage ( env )
} ( envelope )
}
}
2023-10-21 21:34:52 -04:00
func ( store * WakuStore ) onRequest ( stream network . Stream ) {
logger := store . log . With ( logging . HostID ( "peer" , stream . Conn ( ) . RemotePeer ( ) ) )
2022-11-25 16:54:11 -04:00
historyRPCRequest := & pb . HistoryRPC { }
2023-10-21 21:34:52 -04:00
writer := pbio . NewDelimitedWriter ( stream )
reader := pbio . NewDelimitedReader ( stream , math . MaxInt32 )
2022-11-25 16:54:11 -04:00
err := reader . ReadMsg ( historyRPCRequest )
if err != nil {
logger . Error ( "reading request" , zap . Error ( err ) )
2023-08-15 21:40:00 -04:00
store . metrics . RecordError ( decodeRPCFailure )
2023-10-21 21:34:52 -04:00
if err := stream . Reset ( ) ; err != nil {
store . log . Error ( "resetting connection" , zap . Error ( err ) )
}
2022-11-25 16:54:11 -04:00
return
}
2023-10-30 12:55:36 -04:00
if err := historyRPCRequest . ValidateQuery ( ) ; err != nil {
logger . Error ( "invalid request received" , zap . Error ( err ) )
store . metrics . RecordError ( decodeRPCFailure )
if err := stream . Reset ( ) ; err != nil {
store . log . Error ( "resetting connection" , zap . Error ( err ) )
}
// TODO: If store protocol is updated to include error messages
// `err.Error()` can be returned as a response
return
}
2022-11-25 16:54:11 -04:00
logger = logger . With ( zap . String ( "id" , historyRPCRequest . RequestId ) )
if query := historyRPCRequest . Query ; query != nil {
logger = logger . With ( logging . Filters ( query . GetContentFilters ( ) ) )
} else {
logger . Error ( "reading request" , zap . Error ( err ) )
2023-08-15 21:40:00 -04:00
store . metrics . RecordError ( emptyRPCQueryFailure )
2023-10-21 21:34:52 -04:00
if err := stream . Reset ( ) ; err != nil {
store . log . Error ( "resetting connection" , zap . Error ( err ) )
}
2022-11-25 16:54:11 -04:00
return
}
logger . Info ( "received history query" )
2023-08-15 21:40:00 -04:00
store . metrics . RecordQuery ( )
2022-11-25 16:54:11 -04:00
historyResponseRPC := & pb . HistoryRPC { }
historyResponseRPC . RequestId = historyRPCRequest . RequestId
historyResponseRPC . Response = store . FindMessages ( historyRPCRequest . Query )
logger = logger . With ( zap . Int ( "messages" , len ( historyResponseRPC . Response . Messages ) ) )
err = writer . WriteMsg ( historyResponseRPC )
if err != nil {
logger . Error ( "writing response" , zap . Error ( err ) , logging . PagingInfo ( historyResponseRPC . Response . PagingInfo ) )
2023-08-15 21:40:00 -04:00
store . metrics . RecordError ( writeResponseFailure )
2023-10-21 21:34:52 -04:00
if err := stream . Reset ( ) ; err != nil {
store . log . Error ( "resetting connection" , zap . Error ( err ) )
}
return
2022-11-25 16:54:11 -04:00
}
2023-10-21 21:34:52 -04:00
logger . Info ( "response sent" )
stream . Close ( )
}
2022-11-25 16:54:11 -04:00
// Stop closes the store message channel and removes the protocol stream handler
func ( store * WakuStore ) Stop ( ) {
2023-03-09 11:48:25 -04:00
if store . cancel == nil {
return
}
store . cancel ( )
2022-11-25 16:54:11 -04:00
store . started = false
2023-05-05 15:19:15 +05:30
store . MsgC . Unsubscribe ( )
2022-11-25 16:54:11 -04:00
if store . msgProvider != nil {
2023-03-09 11:48:25 -04:00
store . msgProvider . Stop ( ) // TODO: StoreProtocol should not stop a message provider
2022-11-25 16:54:11 -04:00
}
if store . h != nil {
store . h . RemoveStreamHandler ( StoreID_v20beta4 )
}
store . wg . Wait ( )
}
2023-10-30 12:55:36 -04:00
type queryLoopCandidateResponse struct {
peerID peer . ID
response * pb . HistoryResponse
err error
}
func ( store * WakuStore ) queryLoop ( ctx context . Context , query * pb . HistoryQuery , candidateList [ ] peer . ID ) ( [ ] * queryLoopCandidateResponse , error ) {
err := query . Validate ( )
if err != nil {
return nil , err
}
2022-11-25 16:54:11 -04:00
queryWg := sync . WaitGroup { }
queryWg . Add ( len ( candidateList ) )
2023-10-30 12:55:36 -04:00
resultChan := make ( chan * queryLoopCandidateResponse , len ( candidateList ) )
2022-11-25 16:54:11 -04:00
2023-10-30 12:55:36 -04:00
// loops through the candidateList in order and sends the query to each until one of the query gets resolved successfully
// returns the number of retrieved messages, or error if all the requests fail
2022-11-25 16:54:11 -04:00
for _ , peer := range candidateList {
func ( ) {
defer queryWg . Done ( )
2023-10-30 12:55:36 -04:00
historyRequest := & pb . HistoryRPC {
RequestId : hex . EncodeToString ( protocol . GenerateRequestID ( ) ) ,
Query : query ,
}
result := & queryLoopCandidateResponse {
peerID : peer ,
}
response , err := store . queryFrom ( ctx , historyRequest , peer )
if err != nil {
store . log . Error ( "resuming history" , logging . HostID ( "peer" , peer ) , zap . Error ( err ) )
result . err = err
} else {
result . response = response
2022-11-25 16:54:11 -04:00
}
2023-10-30 12:55:36 -04:00
resultChan <- result
2022-11-25 16:54:11 -04:00
} ( )
}
queryWg . Wait ( )
close ( resultChan )
2023-10-30 12:55:36 -04:00
var queryLoopResults [ ] * queryLoopCandidateResponse
2022-11-25 16:54:11 -04:00
for result := range resultChan {
2023-10-30 12:55:36 -04:00
queryLoopResults = append ( queryLoopResults , result )
2022-11-25 16:54:11 -04:00
}
2023-10-30 12:55:36 -04:00
return queryLoopResults , nil
2022-11-25 16:54:11 -04:00
}
func ( store * WakuStore ) findLastSeen ( ) ( int64 , error ) {
return store . msgProvider . MostRecentTimestamp ( )
}
func max ( x , y int64 ) int64 {
if x > y {
return x
}
return y
}
// Resume retrieves the history of waku messages published on the default waku pubsub topic since the last time the waku store node has been online
// messages are stored in the store node's messages field and in the message db
// the offline time window is measured as the difference between the current time and the timestamp of the most recent persisted waku message
// an offset of 20 second is added to the time window to count for nodes asynchrony
// the history is fetched from one of the peers persisted in the waku store node's peer manager unit
// peerList indicates the list of peers to query from. The history is fetched from the first available peer in this list. Such candidates should be found through a discovery method (to be developed).
// if no peerList is passed, one of the peers in the underlying peer manager unit of the store protocol is picked randomly to fetch the history from. The history gets fetched successfully if the dialed peer has been online during the queried time window.
// the resume proc returns the number of retrieved messages if no error occurs, otherwise returns the error string
func ( store * WakuStore ) Resume ( ctx context . Context , pubsubTopic string , peerList [ ] peer . ID ) ( int , error ) {
if ! store . started {
return 0 , errors . New ( "can't resume: store has not started" )
}
lastSeenTime , err := store . findLastSeen ( )
if err != nil {
return 0 , err
}
2023-09-11 10:24:05 -04:00
offset := int64 ( 20 * time . Nanosecond )
2022-12-08 23:08:04 -04:00
currentTime := store . timesource . Now ( ) . UnixNano ( ) + offset
2022-11-25 16:54:11 -04:00
lastSeenTime = max ( lastSeenTime - offset , 0 )
rpc := & pb . HistoryQuery {
PubsubTopic : pubsubTopic ,
StartTime : lastSeenTime ,
EndTime : currentTime ,
PagingInfo : & pb . PagingInfo {
PageSize : 0 ,
Direction : pb . PagingInfo_BACKWARD ,
} ,
}
if len ( peerList ) == 0 {
return - 1 , ErrNoPeersAvailable
}
2023-10-30 12:55:36 -04:00
queryLoopResults , err := store . queryLoop ( ctx , rpc , peerList )
2022-11-25 16:54:11 -04:00
if err != nil {
store . log . Error ( "resuming history" , zap . Error ( err ) )
return - 1 , ErrFailedToResumeHistory
}
msgCount := 0
2023-10-30 12:55:36 -04:00
for _ , r := range queryLoopResults {
if err == nil && r . response . GetError ( ) != pb . HistoryResponse_NONE {
r . err = errors . New ( "invalid cursor" )
}
if r . err != nil {
store . log . Warn ( "could not resume message history" , zap . Error ( r . err ) , logging . HostID ( "peer" , r . peerID ) )
continue
}
for _ , msg := range r . response . Messages {
if err = store . storeMessage ( protocol . NewEnvelope ( msg , store . timesource . Now ( ) . UnixNano ( ) , pubsubTopic ) ) ; err == nil {
msgCount ++
}
2022-11-25 16:54:11 -04:00
}
}
2023-10-30 12:55:36 -04:00
store . log . Info ( "retrieved messages since the last online time" , zap . Int ( "messages" , msgCount ) )
2022-11-25 16:54:11 -04:00
return msgCount , nil
}