Add option to enable lightpush protocol

This commit is contained in:
Richard Ramos 2021-04-28 16:23:03 -04:00
parent 3d8aae5b81
commit 9138d58152
No known key found for this signature in database
GPG Key ID: 80D4B01265FDFE8F
3 changed files with 22 additions and 5 deletions

View File

@ -95,6 +95,10 @@ func New(ctx context.Context, opts ...WakuNodeOption) (*WakuNode, error) {
}
}
if params.enableLightPush {
w.mountLightPush()
}
for _, addr := range w.ListenAddresses() {
log.Info("Listening on ", addr)
}
@ -147,6 +151,10 @@ func (w *WakuNode) mountRelay(opts ...wakurelay.Option) error {
return err
}
func (w *WakuNode) mountLightPush() {
w.lightPush = lightpush.NewWakuLightPush(w.ctx, w.host, w.relay)
}
func (w *WakuNode) startStore() error {
_, err := w.Subscribe(nil)
if err != nil {

View File

@ -24,6 +24,8 @@ type WakuNodeParameters struct {
enableStore bool
storeMsgs bool
store *store.WakuStore
enableLightPush bool
}
type WakuNodeOption func(*WakuNodeParameters) error
@ -107,6 +109,14 @@ func WithMessageProvider(s store.MessageProvider) WakuNodeOption {
}
}
// WithLightPush is a WakuNodeOption that enables the lightpush protocol
func WithLightPush() WakuNodeOption {
return func(params *WakuNodeParameters) error {
params.enableLightPush = true
return nil
}
}
// Default options used in the libp2p node
var DefaultLibP2POptions = []libp2p.Option{
libp2p.DefaultTransports,

View File

@ -36,17 +36,16 @@ type WakuLightPush struct {
ctx context.Context
}
func NewWakuLightPush(ctx context.Context, relay *relay.WakuRelay) *WakuLightPush {
func NewWakuLightPush(ctx context.Context, h host.Host, relay *relay.WakuRelay) *WakuLightPush {
wakuLP := new(WakuLightPush)
wakuLP.relay = relay
wakuLP.ctx = ctx
return wakuLP
}
func (wakuLP *WakuLightPush) Start(h host.Host) {
wakuLP.h = h
wakuLP.h.SetStreamHandler(WakuLightPushProtocolId, wakuLP.onRequest)
log.Info("Light Push protocol started")
return wakuLP
}
func (wakuLP *WakuLightPush) onRequest(s network.Stream) {