2021-10-28 12:41:17 +00:00
|
|
|
package lightpush
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/rand"
|
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/libp2p/go-libp2p-core/host"
|
|
|
|
"github.com/libp2p/go-libp2p-core/peerstore"
|
|
|
|
"github.com/status-im/go-waku/tests"
|
2021-11-01 14:42:55 +00:00
|
|
|
v2 "github.com/status-im/go-waku/waku/v2"
|
2021-11-01 12:38:03 +00:00
|
|
|
"github.com/status-im/go-waku/waku/v2/protocol"
|
2021-10-28 12:41:17 +00:00
|
|
|
"github.com/status-im/go-waku/waku/v2/protocol/pb"
|
|
|
|
"github.com/status-im/go-waku/waku/v2/protocol/relay"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2021-11-19 16:19:48 +00:00
|
|
|
func makeWakuRelay(t *testing.T, topic 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)
|
|
|
|
|
2021-11-01 14:42:55 +00:00
|
|
|
relay, err := relay.NewWakuRelay(context.Background(), host, v2.NewBroadcaster(10))
|
2021-10-28 12:41:17 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-11-19 20:01:52 +00:00
|
|
|
sub, err := relay.SubscribeWithTopic(context.Background(), topic)
|
2021-10-28 12:41:17 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
return relay, sub, host
|
|
|
|
}
|
|
|
|
|
|
|
|
// Node1: Relay
|
|
|
|
// Node2: Relay+Lightpush
|
|
|
|
// Client that will lightpush a message
|
|
|
|
//
|
|
|
|
// Node1 and Node 2 are peers
|
|
|
|
// Client and Node 2 are peers
|
|
|
|
// Node 3 will use lightpush request, sending the message to Node2
|
|
|
|
//
|
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()
|
|
|
|
lightPushNode2 := NewWakuLightPush(ctx, host2, node2)
|
2021-11-01 12:38:03 +00:00
|
|
|
err := lightPushNode2.Start()
|
|
|
|
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)
|
|
|
|
client := NewWakuLightPush(ctx, clientHost, nil)
|
|
|
|
|
|
|
|
host2.Peerstore().AddAddr(host1.ID(), tests.GetHostAddress(host1), peerstore.PermanentAddrTTL)
|
|
|
|
err = host2.Peerstore().AddProtocols(host1.ID(), string(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(), string(LightPushID_v20beta1))
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-11-01 12:38:03 +00:00
|
|
|
msg1 := tests.CreateWakuMessage("test1", float64(0))
|
|
|
|
msg2 := tests.CreateWakuMessage("test2", float64(1))
|
|
|
|
|
2021-10-28 12:41:17 +00:00
|
|
|
req := new(pb.PushRequest)
|
2021-11-01 12:38:03 +00:00
|
|
|
req.Message = msg1
|
2021-10-28 12:41:17 +00:00
|
|
|
req.PubsubTopic = string(testTopic)
|
|
|
|
|
|
|
|
// 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()
|
2021-11-01 14:42:55 +00:00
|
|
|
<-sub1.C
|
|
|
|
<-sub1.C
|
2021-10-28 12:41:17 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
2021-11-01 14:42:55 +00:00
|
|
|
<-sub2.C
|
|
|
|
<-sub2.C
|
2021-10-28 12:41:17 +00:00
|
|
|
}()
|
|
|
|
|
2021-11-06 13:22:23 +00:00
|
|
|
// Verifying successful request
|
2021-11-01 12:38:03 +00:00
|
|
|
resp, err := client.request(ctx, req)
|
2021-10-28 12:41:17 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.True(t, resp.IsSuccess)
|
|
|
|
|
2021-11-01 12:38:03 +00:00
|
|
|
// Checking that msg hash is correct
|
2021-11-19 20:01:52 +00:00
|
|
|
hash, err := client.PublishWithTopic(ctx, msg2, testTopic)
|
2021-11-01 12:38:03 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, protocol.NewEnvelope(msg2, 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)
|
|
|
|
client := NewWakuLightPush(ctx, clientHost, nil)
|
|
|
|
err = client.Start()
|
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)
|
|
|
|
client := NewWakuLightPush(ctx, clientHost, nil)
|
|
|
|
|
2021-11-19 20:01:52 +00:00
|
|
|
_, err = client.PublishWithTopic(ctx, tests.CreateWakuMessage("test", float64(0)), testTopic)
|
2021-11-01 12:38:03 +00:00
|
|
|
require.Errorf(t, err, "no suitable remote peers")
|
|
|
|
}
|