-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: decouple API from go-waku (#1239)
- Loading branch information
1 parent
76275f6
commit 37f936d
Showing
8 changed files
with
212 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package common | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/libp2p/go-libp2p/core/peer" | ||
"github.com/waku-org/go-waku/waku/v2/protocol/store" | ||
"github.com/waku-org/go-waku/waku/v2/protocol/store/pb" | ||
) | ||
|
||
type StoreRequestResult interface { | ||
Cursor() []byte | ||
IsComplete() bool | ||
PeerID() peer.ID | ||
Next(ctx context.Context, opts ...store.RequestOption) error // TODO: see how to decouple store.RequestOption | ||
Messages() []*pb.WakuMessageKeyValue | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package missing | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/libp2p/go-libp2p/core/peer" | ||
"github.com/waku-org/go-waku/waku/v2/api/common" | ||
"github.com/waku-org/go-waku/waku/v2/protocol" | ||
"github.com/waku-org/go-waku/waku/v2/protocol/pb" | ||
"github.com/waku-org/go-waku/waku/v2/protocol/store" | ||
) | ||
|
||
func NewDefaultStorenodeRequestor(store *store.WakuStore) StorenodeRequestor { | ||
return &defaultStorenodeRequestor{ | ||
store: store, | ||
} | ||
} | ||
|
||
type defaultStorenodeRequestor struct { | ||
store *store.WakuStore | ||
} | ||
|
||
func (d *defaultStorenodeRequestor) GetMessagesByHash(ctx context.Context, peerID peer.ID, pageSize uint64, messageHashes []pb.MessageHash) (common.StoreRequestResult, error) { | ||
return d.store.QueryByHash(ctx, messageHashes, store.WithPeer(peerID), store.WithPaging(false, pageSize)) | ||
} | ||
|
||
func (d *defaultStorenodeRequestor) QueryWithCriteria(ctx context.Context, peerID peer.ID, pageSize uint64, pubsubTopic string, contentTopics []string, from *int64, to *int64) (common.StoreRequestResult, error) { | ||
return d.store.Query(ctx, store.FilterCriteria{ | ||
ContentFilter: protocol.NewContentFilter(pubsubTopic, contentTopics...), | ||
TimeStart: from, | ||
TimeEnd: to, | ||
}, store.WithPeer(peerID), store.WithPaging(false, pageSize), store.IncludeData(false)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package publish | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
|
||
"github.com/libp2p/go-libp2p/core/peer" | ||
"github.com/waku-org/go-waku/waku/v2/protocol/lightpush" | ||
"github.com/waku-org/go-waku/waku/v2/protocol/pb" | ||
"github.com/waku-org/go-waku/waku/v2/protocol/relay" | ||
) | ||
|
||
var ErrRelayNotAvailable = errors.New("relay is not available") | ||
var ErrLightpushNotAvailable = errors.New("lightpush is not available") | ||
|
||
func NewDefaultPublisher(lightpush *lightpush.WakuLightPush, relay *relay.WakuRelay) Publisher { | ||
return &defaultPublisher{ | ||
lightpush: lightpush, | ||
relay: relay, | ||
} | ||
} | ||
|
||
type defaultPublisher struct { | ||
lightpush *lightpush.WakuLightPush | ||
relay *relay.WakuRelay | ||
} | ||
|
||
func (d *defaultPublisher) RelayListPeers(pubsubTopic string) ([]peer.ID, error) { | ||
if d.relay == nil { | ||
return nil, ErrRelayNotAvailable | ||
} | ||
|
||
return d.relay.PubSub().ListPeers(pubsubTopic), nil | ||
} | ||
|
||
func (d *defaultPublisher) RelayPublish(ctx context.Context, message *pb.WakuMessage, pubsubTopic string) (pb.MessageHash, error) { | ||
if d.relay == nil { | ||
return pb.MessageHash{}, ErrRelayNotAvailable | ||
} | ||
|
||
return d.relay.Publish(ctx, message, relay.WithPubSubTopic(pubsubTopic)) | ||
} | ||
|
||
func (d *defaultPublisher) LightpushPublish(ctx context.Context, message *pb.WakuMessage, pubsubTopic string, maxPeers int) (pb.MessageHash, error) { | ||
if d.lightpush == nil { | ||
return pb.MessageHash{}, ErrLightpushNotAvailable | ||
} | ||
|
||
return d.lightpush.Publish(ctx, message, lightpush.WithPubSubTopic(pubsubTopic), lightpush.WithMaxPeers(maxPeers)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package publish | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/libp2p/go-libp2p/core/peer" | ||
"github.com/waku-org/go-waku/waku/v2/protocol/pb" | ||
"github.com/waku-org/go-waku/waku/v2/protocol/store" | ||
) | ||
|
||
func NewDefaultStorenodeMessageVerifier(store *store.WakuStore) StorenodeMessageVerifier { | ||
return &defaultStorenodeMessageVerifier{ | ||
store: store, | ||
} | ||
} | ||
|
||
type defaultStorenodeMessageVerifier struct { | ||
store *store.WakuStore | ||
} | ||
|
||
func (d *defaultStorenodeMessageVerifier) MessageHashesExist(ctx context.Context, requestID []byte, peerID peer.ID, pageSize uint64, messageHashes []pb.MessageHash) ([]pb.MessageHash, error) { | ||
var opts []store.RequestOption | ||
opts = append(opts, store.WithRequestID(requestID)) | ||
opts = append(opts, store.WithPeer(peerID)) | ||
opts = append(opts, store.WithPaging(false, pageSize)) | ||
opts = append(opts, store.IncludeData(false)) | ||
|
||
response, err := d.store.QueryByHash(ctx, messageHashes, opts...) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
result := make([]pb.MessageHash, len(response.Messages())) | ||
for i, msg := range response.Messages() { | ||
result[i] = msg.WakuMessageHash() | ||
} | ||
|
||
return result, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.