2024-02-06 21:20:19 +01:00
|
|
|
package protocol
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"go.uber.org/zap"
|
|
|
|
|
|
|
|
gethbridge "github.com/status-im/status-go/eth-node/bridge/geth"
|
|
|
|
"github.com/status-im/status-go/eth-node/types"
|
2025-01-10 19:56:00 +01:00
|
|
|
"github.com/status-im/status-go/wakuv1"
|
2024-02-06 21:20:19 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type testWakuWrapper struct {
|
|
|
|
*gethbridge.GethWakuWrapper
|
|
|
|
|
|
|
|
publicWakuAPIWrapper *testPublicWakuAPIWrapper
|
|
|
|
}
|
|
|
|
|
2025-01-10 19:56:00 +01:00
|
|
|
func newTestWaku(w *wakuv1.Waku) types.Waku {
|
2024-02-06 21:20:19 +01:00
|
|
|
wrapper := gethbridge.NewGethWakuWrapper(w)
|
|
|
|
return &testWakuWrapper{
|
|
|
|
GethWakuWrapper: wrapper.(*gethbridge.GethWakuWrapper),
|
2025-01-10 19:56:00 +01:00
|
|
|
publicWakuAPIWrapper: newTestPublicWakuAPI(wakuv1.NewPublicWakuAPI(w)).(*testPublicWakuAPIWrapper),
|
2024-02-06 21:20:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tw *testWakuWrapper) PublicWakuAPI() types.PublicWakuAPI {
|
|
|
|
return tw.publicWakuAPIWrapper
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tw *testWakuWrapper) SubscribePostEvents() chan *PostMessageSubscription {
|
|
|
|
subscription := make(chan *PostMessageSubscription, 100)
|
|
|
|
tw.publicWakuAPIWrapper.postSubscriptions = append(tw.publicWakuAPIWrapper.postSubscriptions, subscription)
|
|
|
|
return subscription
|
|
|
|
}
|
|
|
|
|
|
|
|
type PostMessageSubscription struct {
|
|
|
|
id []byte
|
|
|
|
msg *types.NewMessage
|
|
|
|
}
|
|
|
|
|
|
|
|
type testPublicWakuAPIWrapper struct {
|
|
|
|
*gethbridge.GethPublicWakuAPIWrapper
|
|
|
|
|
|
|
|
postSubscriptions []chan *PostMessageSubscription
|
|
|
|
}
|
|
|
|
|
2025-01-10 19:56:00 +01:00
|
|
|
func newTestPublicWakuAPI(api *wakuv1.PublicWakuAPI) types.PublicWakuAPI {
|
2024-02-06 21:20:19 +01:00
|
|
|
wrapper := gethbridge.NewGethPublicWakuAPIWrapper(api)
|
|
|
|
return &testPublicWakuAPIWrapper{
|
|
|
|
GethPublicWakuAPIWrapper: wrapper.(*gethbridge.GethPublicWakuAPIWrapper),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tp *testPublicWakuAPIWrapper) Post(ctx context.Context, req types.NewMessage) ([]byte, error) {
|
|
|
|
id, err := tp.GethPublicWakuAPIWrapper.Post(ctx, req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for _, s := range tp.postSubscriptions {
|
|
|
|
select {
|
|
|
|
case s <- &PostMessageSubscription{id: id, msg: &req}:
|
|
|
|
default:
|
|
|
|
// subscription channel full
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return id, err
|
|
|
|
}
|
|
|
|
|
2025-01-10 19:56:00 +01:00
|
|
|
func newTestWakuWrapper(config *wakuv1.Config, logger *zap.Logger) (*testWakuWrapper, error) {
|
2024-02-06 21:20:19 +01:00
|
|
|
if config == nil {
|
2025-01-10 19:56:00 +01:00
|
|
|
config = &wakuv1.DefaultConfig
|
2024-02-06 21:20:19 +01:00
|
|
|
}
|
2025-01-10 19:56:00 +01:00
|
|
|
w := wakuv1.New(config, logger)
|
2024-02-06 21:20:19 +01:00
|
|
|
return newTestWaku(w).(*testWakuWrapper), w.Start()
|
|
|
|
}
|