2021-10-28 12:41:17 +00:00
|
|
|
package lightpush
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/rand"
|
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2024-02-05 12:53:15 +00:00
|
|
|
"github.com/waku-org/go-waku/waku/v2/peermanager"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
|
2022-10-19 19:39:32 +00:00
|
|
|
"github.com/libp2p/go-libp2p/core/host"
|
|
|
|
"github.com/libp2p/go-libp2p/core/peerstore"
|
2023-08-16 01:40:00 +00:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2021-10-28 12:41:17 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2022-11-09 19:53:01 +00:00
|
|
|
"github.com/waku-org/go-waku/tests"
|
|
|
|
"github.com/waku-org/go-waku/waku/v2/protocol"
|
|
|
|
"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-09 19:53:01 +00:00
|
|
|
"github.com/waku-org/go-waku/waku/v2/utils"
|
2021-10-28 12:41:17 +00:00
|
|
|
)
|
|
|
|
|
2023-09-29 05:13:25 +00:00
|
|
|
func makeWakuRelay(t *testing.T, pusubTopic string) (*relay.WakuRelay, *relay.Subscription, host.Host) {
|
2021-10-28 12:41:17 +00:00
|
|
|
port, err := tests.FindFreePort(t, "", 5)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
host, err := tests.MakeHost(context.Background(), port, rand.Reader)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-05-05 09:49:15 +00:00
|
|
|
b := relay.NewBroadcaster(10)
|
2023-04-14 21:50:44 +00:00
|
|
|
require.NoError(t, b.Start(context.Background()))
|
2023-08-16 01:40:00 +00:00
|
|
|
relay := relay.NewWakuRelay(b, 0, timesource.NewDefaultClock(), prometheus.DefaultRegisterer, utils.Logger())
|
2023-04-17 00:04:12 +00:00
|
|
|
relay.SetHost(host)
|
2023-01-06 22:37:57 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
err = relay.Start(context.Background())
|
2021-10-28 12:41:17 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-10-20 19:56:18 +00:00
|
|
|
sub, err := relay.Subscribe(context.Background(), protocol.NewContentFilter(pusubTopic))
|
2021-10-28 12:41:17 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-10-20 19:56:18 +00:00
|
|
|
return relay, sub[0], host
|
2021-10-28 12:41:17 +00:00
|
|
|
}
|
|
|
|
|
2023-12-15 14:09:56 +00:00
|
|
|
func waitForMsg(t *testing.T, wg *sync.WaitGroup, ch chan *protocol.Envelope) {
|
|
|
|
wg.Add(1)
|
|
|
|
log := utils.Logger()
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
select {
|
|
|
|
case env := <-ch:
|
|
|
|
msg := env.Message()
|
|
|
|
log.Info("Received ", zap.String("msg", msg.String()))
|
|
|
|
case <-time.After(2 * time.Second):
|
|
|
|
require.Fail(t, "Message timeout")
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
wg.Wait()
|
|
|
|
}
|
|
|
|
|
2021-10-28 12:41:17 +00:00
|
|
|
// Node1: Relay
|
|
|
|
// Node2: Relay+Lightpush
|
|
|
|
// Client that will lightpush a message
|
|
|
|
//
|
|
|
|
// Node1 and Node 2 are peers
|
|
|
|
// Client and Node 2 are peers
|
2023-08-02 05:54:50 +00:00
|
|
|
// Client will use lightpush request, sending the message to Node2
|
2021-10-28 12:41:17 +00:00
|
|
|
//
|
2021-11-06 13:22:23 +00:00
|
|
|
// Client send a successful message using lightpush
|
2021-10-28 12:41:17 +00:00
|
|
|
// Node2 receive the message and broadcast it
|
|
|
|
// Node1 receive the message
|
|
|
|
func TestWakuLightPush(t *testing.T) {
|
2021-11-19 16:19:48 +00:00
|
|
|
testTopic := "/waku/2/go/lightpush/test"
|
2021-10-28 12:41:17 +00:00
|
|
|
node1, sub1, host1 := makeWakuRelay(t, testTopic)
|
|
|
|
defer node1.Stop()
|
2021-11-01 14:42:55 +00:00
|
|
|
defer sub1.Unsubscribe()
|
2021-10-28 12:41:17 +00:00
|
|
|
|
|
|
|
node2, sub2, host2 := makeWakuRelay(t, testTopic)
|
|
|
|
defer node2.Stop()
|
2021-11-01 14:42:55 +00:00
|
|
|
defer sub2.Unsubscribe()
|
2021-10-28 12:41:17 +00:00
|
|
|
|
|
|
|
ctx := context.Background()
|
2023-08-16 01:40:00 +00:00
|
|
|
lightPushNode2 := NewWakuLightPush(node2, nil, prometheus.DefaultRegisterer, utils.Logger())
|
2023-04-17 00:04:12 +00:00
|
|
|
lightPushNode2.SetHost(host2)
|
2023-01-06 22:37:57 +00:00
|
|
|
err := lightPushNode2.Start(ctx)
|
2021-11-01 12:38:03 +00:00
|
|
|
require.NoError(t, err)
|
2021-10-28 12:41:17 +00:00
|
|
|
defer lightPushNode2.Stop()
|
|
|
|
|
|
|
|
port, err := tests.FindFreePort(t, "", 5)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
clientHost, err := tests.MakeHost(context.Background(), port, rand.Reader)
|
|
|
|
require.NoError(t, err)
|
2023-08-16 01:40:00 +00:00
|
|
|
client := NewWakuLightPush(nil, nil, prometheus.DefaultRegisterer, utils.Logger())
|
2023-04-17 00:04:12 +00:00
|
|
|
client.SetHost(clientHost)
|
2021-10-28 12:41:17 +00:00
|
|
|
|
|
|
|
host2.Peerstore().AddAddr(host1.ID(), tests.GetHostAddress(host1), peerstore.PermanentAddrTTL)
|
2023-02-16 16:17:52 +00:00
|
|
|
err = host2.Peerstore().AddProtocols(host1.ID(), relay.WakuRelayID_v200)
|
2021-10-28 12:41:17 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = host2.Connect(ctx, host2.Peerstore().PeerInfo(host1.ID()))
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
clientHost.Peerstore().AddAddr(host2.ID(), tests.GetHostAddress(host2), peerstore.PermanentAddrTTL)
|
2023-02-16 16:17:52 +00:00
|
|
|
err = clientHost.Peerstore().AddProtocols(host2.ID(), LightPushID_v20beta1)
|
2021-10-28 12:41:17 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-04-19 20:54:33 +00:00
|
|
|
msg2 := tests.CreateWakuMessage("test2", utils.GetUnixEpoch())
|
2021-11-01 12:38:03 +00:00
|
|
|
|
2021-10-28 12:41:17 +00:00
|
|
|
// Wait for the mesh connection to happen between node1 and node2
|
2021-10-28 13:03:23 +00:00
|
|
|
time.Sleep(2 * time.Second)
|
2021-10-28 12:41:17 +00:00
|
|
|
var wg sync.WaitGroup
|
|
|
|
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
2023-05-05 09:49:15 +00:00
|
|
|
<-sub1.Ch
|
2021-10-28 12:41:17 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
2023-05-05 09:49:15 +00:00
|
|
|
<-sub2.Ch
|
2021-10-28 12:41:17 +00:00
|
|
|
}()
|
|
|
|
|
2024-02-05 12:53:15 +00:00
|
|
|
var lpOptions []RequestOption
|
2023-09-29 05:13:25 +00:00
|
|
|
lpOptions = append(lpOptions, WithPubSubTopic(testTopic))
|
2023-10-16 16:42:01 +00:00
|
|
|
lpOptions = append(lpOptions, WithPeer(host2.ID()))
|
2021-10-28 12:41:17 +00:00
|
|
|
|
2021-11-01 12:38:03 +00:00
|
|
|
// Checking that msg hash is correct
|
2023-10-30 16:30:25 +00:00
|
|
|
hash, err := client.Publish(ctx, msg2, lpOptions...)
|
2021-11-01 12:38:03 +00:00
|
|
|
require.NoError(t, err)
|
2023-11-07 19:48:43 +00:00
|
|
|
require.Equal(t, protocol.NewEnvelope(msg2, *utils.GetUnixEpoch(), string(testTopic)).Hash(), hash)
|
2021-10-28 12:41:17 +00:00
|
|
|
wg.Wait()
|
|
|
|
}
|
2021-11-01 12:38:03 +00:00
|
|
|
|
|
|
|
func TestWakuLightPushStartWithoutRelay(t *testing.T) {
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
clientHost, err := tests.MakeHost(context.Background(), 0, rand.Reader)
|
|
|
|
require.NoError(t, err)
|
2023-08-16 01:40:00 +00:00
|
|
|
client := NewWakuLightPush(nil, nil, prometheus.DefaultRegisterer, utils.Logger())
|
2023-04-17 00:04:12 +00:00
|
|
|
client.SetHost(clientHost)
|
2023-01-06 22:37:57 +00:00
|
|
|
err = client.Start(ctx)
|
2021-11-01 14:42:55 +00:00
|
|
|
|
2021-11-01 12:38:03 +00:00
|
|
|
require.Errorf(t, err, "relay is required")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWakuLightPushNoPeers(t *testing.T) {
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
2021-11-19 16:19:48 +00:00
|
|
|
testTopic := "abc"
|
2021-11-01 12:38:03 +00:00
|
|
|
|
|
|
|
clientHost, err := tests.MakeHost(context.Background(), 0, rand.Reader)
|
|
|
|
require.NoError(t, err)
|
2023-08-16 01:40:00 +00:00
|
|
|
client := NewWakuLightPush(nil, nil, prometheus.DefaultRegisterer, utils.Logger())
|
2023-04-17 00:04:12 +00:00
|
|
|
client.SetHost(clientHost)
|
2024-02-05 12:53:15 +00:00
|
|
|
var lpOptions []RequestOption
|
2023-09-29 05:13:25 +00:00
|
|
|
lpOptions = append(lpOptions, WithPubSubTopic(testTopic))
|
|
|
|
|
2023-10-30 16:30:25 +00:00
|
|
|
_, err = client.Publish(ctx, tests.CreateWakuMessage("test", utils.GetUnixEpoch()), lpOptions...)
|
2021-11-01 12:38:03 +00:00
|
|
|
require.Errorf(t, err, "no suitable remote peers")
|
|
|
|
}
|
2023-09-29 05:13:25 +00:00
|
|
|
|
|
|
|
// Node1: Relay
|
|
|
|
// Node2: Relay+Lightpush
|
|
|
|
// Client that will lightpush a message
|
|
|
|
//
|
|
|
|
// Node1 and Node 2 are peers
|
|
|
|
// Client and Node 2 are peers
|
|
|
|
// Client will use lightpush request, sending the message to Node2
|
|
|
|
//
|
|
|
|
// Client send a successful message using lightpush
|
|
|
|
// Node2 receive the message and broadcast it
|
|
|
|
// Node1 receive the message
|
|
|
|
|
|
|
|
func TestWakuLightPushAutoSharding(t *testing.T) {
|
|
|
|
contentTopic := "0/test/1/testTopic/proto"
|
|
|
|
cTopic1, err := protocol.StringToContentTopic(contentTopic)
|
|
|
|
require.NoError(t, err)
|
|
|
|
//Computing pubSubTopic only for filterFullNode.
|
|
|
|
pubSubTopicInst := protocol.GetShardFromContentTopic(cTopic1, protocol.GenerationZeroShardsCount)
|
|
|
|
pubSubTopic := pubSubTopicInst.String()
|
|
|
|
node1, sub1, host1 := makeWakuRelay(t, pubSubTopic)
|
|
|
|
defer node1.Stop()
|
|
|
|
defer sub1.Unsubscribe()
|
|
|
|
|
|
|
|
node2, sub2, host2 := makeWakuRelay(t, pubSubTopic)
|
|
|
|
defer node2.Stop()
|
|
|
|
defer sub2.Unsubscribe()
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
lightPushNode2 := NewWakuLightPush(node2, nil, prometheus.DefaultRegisterer, utils.Logger())
|
|
|
|
lightPushNode2.SetHost(host2)
|
|
|
|
err = lightPushNode2.Start(ctx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer lightPushNode2.Stop()
|
|
|
|
|
|
|
|
port, err := tests.FindFreePort(t, "", 5)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
clientHost, err := tests.MakeHost(context.Background(), port, rand.Reader)
|
|
|
|
require.NoError(t, err)
|
|
|
|
client := NewWakuLightPush(nil, nil, prometheus.DefaultRegisterer, utils.Logger())
|
|
|
|
client.SetHost(clientHost)
|
|
|
|
|
|
|
|
host2.Peerstore().AddAddr(host1.ID(), tests.GetHostAddress(host1), peerstore.PermanentAddrTTL)
|
|
|
|
err = host2.Peerstore().AddProtocols(host1.ID(), relay.WakuRelayID_v200)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = host2.Connect(ctx, host2.Peerstore().PeerInfo(host1.ID()))
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
clientHost.Peerstore().AddAddr(host2.ID(), tests.GetHostAddress(host2), peerstore.PermanentAddrTTL)
|
|
|
|
err = clientHost.Peerstore().AddProtocols(host2.ID(), LightPushID_v20beta1)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
msg1 := tests.CreateWakuMessage(contentTopic, utils.GetUnixEpoch())
|
|
|
|
|
|
|
|
// Wait for the mesh connection to happen between node1 and node2
|
|
|
|
time.Sleep(2 * time.Second)
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
<-sub1.Ch
|
|
|
|
}()
|
|
|
|
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
<-sub2.Ch
|
|
|
|
|
|
|
|
}()
|
2024-02-05 12:53:15 +00:00
|
|
|
var lpOptions []RequestOption
|
2023-10-16 16:42:01 +00:00
|
|
|
lpOptions = append(lpOptions, WithPeer(host2.ID()))
|
2023-09-29 05:13:25 +00:00
|
|
|
// Verifying successful request
|
2023-10-16 16:42:01 +00:00
|
|
|
hash1, err := client.Publish(ctx, msg1, lpOptions...)
|
2023-09-29 05:13:25 +00:00
|
|
|
require.NoError(t, err)
|
2023-11-07 19:48:43 +00:00
|
|
|
require.Equal(t, protocol.NewEnvelope(msg1, *utils.GetUnixEpoch(), string(pubSubTopic)).Hash(), hash1)
|
2023-09-29 05:13:25 +00:00
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
|
|
|
|
}
|
2023-12-15 14:09:56 +00:00
|
|
|
|
|
|
|
func TestWakuLightPushCornerCases(t *testing.T) {
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
testTopic := "/waku/2/go/lightpush/test"
|
|
|
|
testContentTopic := "/test/10/my-lp-app/proto"
|
|
|
|
|
|
|
|
// Prepare peer manager instance to include in test
|
2024-03-14 14:21:47 +00:00
|
|
|
pm := peermanager.NewPeerManager(10, 10, nil, utils.Logger())
|
2023-12-15 14:09:56 +00:00
|
|
|
|
|
|
|
node1, sub1, host1 := makeWakuRelay(t, testTopic)
|
|
|
|
defer node1.Stop()
|
|
|
|
defer sub1.Unsubscribe()
|
|
|
|
|
|
|
|
node2, sub2, host2 := makeWakuRelay(t, testTopic)
|
|
|
|
defer node2.Stop()
|
|
|
|
defer sub2.Unsubscribe()
|
|
|
|
|
|
|
|
lightPushNode2 := NewWakuLightPush(node2, pm, prometheus.DefaultRegisterer, utils.Logger())
|
|
|
|
lightPushNode2.SetHost(host2)
|
|
|
|
err := lightPushNode2.Start(ctx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer lightPushNode2.Stop()
|
|
|
|
|
|
|
|
port, err := tests.FindFreePort(t, "", 5)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
clientHost, err := tests.MakeHost(context.Background(), port, rand.Reader)
|
|
|
|
require.NoError(t, err)
|
|
|
|
client := NewWakuLightPush(nil, nil, prometheus.DefaultRegisterer, utils.Logger())
|
|
|
|
client.SetHost(clientHost)
|
|
|
|
|
|
|
|
host2.Peerstore().AddAddr(host1.ID(), tests.GetHostAddress(host1), peerstore.PermanentAddrTTL)
|
|
|
|
err = host2.Peerstore().AddProtocols(host1.ID(), relay.WakuRelayID_v200)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = host2.Connect(ctx, host2.Peerstore().PeerInfo(host1.ID()))
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
clientHost.Peerstore().AddAddr(host2.ID(), tests.GetHostAddress(host2), peerstore.PermanentAddrTTL)
|
|
|
|
err = clientHost.Peerstore().AddProtocols(host2.ID(), LightPushID_v20beta1)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
msg2 := tests.CreateWakuMessage(testContentTopic, utils.GetUnixEpoch())
|
|
|
|
|
|
|
|
// Wait for the mesh connection to happen between node1 and node2
|
|
|
|
time.Sleep(2 * time.Second)
|
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
|
2024-02-05 12:53:15 +00:00
|
|
|
var lpOptions []RequestOption
|
2023-12-15 14:09:56 +00:00
|
|
|
lpOptions = append(lpOptions, WithPubSubTopic(testTopic))
|
|
|
|
lpOptions = append(lpOptions, WithPeer(host2.ID()))
|
|
|
|
|
|
|
|
// Check that msg publish has passed for nominal case
|
|
|
|
_, err = client.Publish(ctx, msg2, lpOptions...)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Wait for the nominal case message at node1
|
|
|
|
waitForMsg(t, &wg, sub1.Ch)
|
|
|
|
|
|
|
|
// Test error case with nil message
|
|
|
|
_, err = client.Publish(ctx, nil, lpOptions...)
|
|
|
|
require.Error(t, err)
|
|
|
|
|
|
|
|
// Create new "dummy" host - not related to any node
|
|
|
|
host3, err := tests.MakeHost(context.Background(), 12345, rand.Reader)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2024-02-05 12:53:15 +00:00
|
|
|
var lpOptions2 []RequestOption
|
2023-12-15 14:09:56 +00:00
|
|
|
|
|
|
|
// Test error case with empty options
|
|
|
|
_, err = client.Publish(ctx, msg2, lpOptions2...)
|
|
|
|
require.Error(t, err)
|
|
|
|
|
|
|
|
// Test error case with unrelated host
|
|
|
|
_, err = client.Publish(ctx, msg2, WithPubSubTopic(testTopic), WithPeer(host3.ID()))
|
|
|
|
require.Error(t, err)
|
|
|
|
|
|
|
|
// Test corner case with default pubSub topic
|
|
|
|
_, err = client.Publish(ctx, msg2, WithDefaultPubsubTopic(), WithPeer(host2.ID()))
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Test situation when cancel func is nil
|
|
|
|
lightPushNode2.cancel = nil
|
|
|
|
}
|