2022-12-09 16:16:21 +00:00
|
|
|
package wakuv2
|
|
|
|
|
|
|
|
import (
|
2023-02-22 21:58:17 +00:00
|
|
|
"context"
|
|
|
|
"crypto/rand"
|
2022-12-09 16:16:21 +00:00
|
|
|
"errors"
|
2023-02-22 21:58:17 +00:00
|
|
|
"math/big"
|
|
|
|
"os"
|
2022-12-09 16:16:21 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/cenkalti/backoff/v3"
|
|
|
|
"github.com/stretchr/testify/require"
|
2023-02-22 21:58:17 +00:00
|
|
|
"github.com/waku-org/go-waku/waku/v2/dnsdisc"
|
|
|
|
"github.com/waku-org/go-waku/waku/v2/protocol/pb"
|
2023-05-22 21:38:02 +00:00
|
|
|
"github.com/waku-org/go-waku/waku/v2/protocol/relay"
|
2023-02-22 21:58:17 +00:00
|
|
|
"github.com/waku-org/go-waku/waku/v2/protocol/store"
|
2023-09-13 10:50:23 +00:00
|
|
|
"github.com/waku-org/go-waku/waku/v2/protocol/subscription"
|
|
|
|
"golang.org/x/exp/maps"
|
2022-12-09 16:16:21 +00:00
|
|
|
|
|
|
|
"github.com/status-im/status-go/protocol/tt"
|
2023-02-22 21:58:17 +00:00
|
|
|
"github.com/status-im/status-go/wakuv2/common"
|
2022-12-09 16:16:21 +00:00
|
|
|
)
|
|
|
|
|
2023-09-27 17:26:10 +00:00
|
|
|
var testENRBootstrap = "enrtree://AL65EKLJAUXKKPG43HVTML5EFFWEZ7L4LOKTLZCLJASG4DSESQZEC@prod.status.nodes.status.im"
|
2022-12-09 16:16:21 +00:00
|
|
|
|
|
|
|
func TestDiscoveryV5(t *testing.T) {
|
|
|
|
config := &Config{}
|
|
|
|
config.EnableDiscV5 = true
|
|
|
|
config.DiscV5BootstrapNodes = []string{testENRBootstrap}
|
|
|
|
config.DiscoveryLimit = 20
|
|
|
|
config.UDPPort = 9001
|
2023-07-05 15:56:34 +00:00
|
|
|
w, err := New("", "", config, nil, nil, nil, nil, nil)
|
2022-12-09 16:16:21 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.NoError(t, w.Start())
|
|
|
|
|
|
|
|
err = tt.RetryWithBackOff(func() error {
|
|
|
|
if len(w.Peers()) == 0 {
|
|
|
|
return errors.New("no peers discovered")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.NotEqual(t, 0, len(w.Peers()))
|
|
|
|
require.NoError(t, w.Stop())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRestartDiscoveryV5(t *testing.T) {
|
|
|
|
config := &Config{}
|
|
|
|
config.EnableDiscV5 = true
|
|
|
|
// Use wrong discv5 bootstrap address, to simulate being offline
|
|
|
|
config.DiscV5BootstrapNodes = []string{"enrtree://AOGECG2SPND25EEFMAJ5WF3KSGJNSGV356DSTL2YVLLZWIV6SAYBM@1.1.1.2"}
|
|
|
|
config.DiscoveryLimit = 20
|
|
|
|
config.UDPPort = 9002
|
2023-07-05 15:56:34 +00:00
|
|
|
w, err := New("", "", config, nil, nil, nil, nil, nil)
|
2022-12-09 16:16:21 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.NoError(t, w.Start())
|
|
|
|
|
|
|
|
require.False(t, w.seededBootnodesForDiscV5)
|
|
|
|
|
|
|
|
options := func(b *backoff.ExponentialBackOff) {
|
|
|
|
b.MaxElapsedTime = 2 * time.Second
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sanity check, not great, but it's probably helpful
|
|
|
|
err = tt.RetryWithBackOff(func() error {
|
|
|
|
if len(w.Peers()) == 0 {
|
|
|
|
return errors.New("no peers discovered")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}, options)
|
|
|
|
|
|
|
|
require.Error(t, err)
|
|
|
|
|
|
|
|
w.discV5BootstrapNodes = []string{testENRBootstrap}
|
|
|
|
|
|
|
|
options = func(b *backoff.ExponentialBackOff) {
|
|
|
|
b.MaxElapsedTime = 30 * time.Second
|
|
|
|
}
|
|
|
|
|
|
|
|
err = tt.RetryWithBackOff(func() error {
|
|
|
|
if len(w.Peers()) == 0 {
|
|
|
|
return errors.New("no peers discovered")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}, options)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.True(t, w.seededBootnodesForDiscV5)
|
|
|
|
require.NotEqual(t, 0, len(w.Peers()))
|
|
|
|
require.NoError(t, w.Stop())
|
|
|
|
}
|
2023-02-22 21:58:17 +00:00
|
|
|
|
|
|
|
func TestBasicWakuV2(t *testing.T) {
|
|
|
|
enrTreeAddress := testENRBootstrap
|
|
|
|
envEnrTreeAddress := os.Getenv("ENRTREE_ADDRESS")
|
|
|
|
if envEnrTreeAddress != "" {
|
|
|
|
enrTreeAddress = envEnrTreeAddress
|
|
|
|
}
|
|
|
|
|
|
|
|
config := &Config{}
|
|
|
|
config.Port = 0
|
|
|
|
config.EnableDiscV5 = true
|
|
|
|
config.DiscV5BootstrapNodes = []string{enrTreeAddress}
|
|
|
|
config.DiscoveryLimit = 20
|
|
|
|
config.UDPPort = 9001
|
|
|
|
config.WakuNodes = []string{enrTreeAddress}
|
2023-07-05 15:56:34 +00:00
|
|
|
w, err := New("", "", config, nil, nil, nil, nil, nil)
|
2023-02-22 21:58:17 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NoError(t, w.Start())
|
|
|
|
|
|
|
|
// DNSDiscovery
|
|
|
|
ctx, cancel := context.WithTimeout(context.TODO(), 30*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
discoveredNodes, err := dnsdisc.RetrieveNodes(ctx, enrTreeAddress)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Peer used for retrieving history
|
|
|
|
r, err := rand.Int(rand.Reader, big.NewInt(int64(len(discoveredNodes))))
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
storeNode := discoveredNodes[int(r.Int64())]
|
|
|
|
|
|
|
|
// Wait for some peers to be discovered
|
|
|
|
time.Sleep(3 * time.Second)
|
|
|
|
|
|
|
|
// At least 3 peers should have been discovered
|
|
|
|
require.Greater(t, w.PeerCount(), 3)
|
|
|
|
|
|
|
|
filter := &common.Filter{
|
2023-09-13 10:50:23 +00:00
|
|
|
Messages: common.NewMemoryMessageStore(),
|
|
|
|
ContentTopics: common.NewTopicSetFromBytes([][]byte{[]byte{1, 2, 3, 4}}),
|
2023-02-22 21:58:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_, err = w.Subscribe(filter)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
msgTimestamp := w.timestamp()
|
2023-09-13 10:50:23 +00:00
|
|
|
contentTopic := maps.Keys(filter.ContentTopics)[0]
|
2023-02-22 21:58:17 +00:00
|
|
|
|
2023-05-22 21:38:02 +00:00
|
|
|
_, err = w.Send(relay.DefaultWakuTopic, &pb.WakuMessage{
|
2023-02-22 21:58:17 +00:00
|
|
|
Payload: []byte{1, 2, 3, 4, 5},
|
|
|
|
ContentTopic: contentTopic.ContentTopic(),
|
|
|
|
Version: 0,
|
|
|
|
Timestamp: msgTimestamp,
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
|
|
|
|
messages := filter.Retrieve()
|
|
|
|
require.Len(t, messages, 1)
|
|
|
|
|
|
|
|
timestampInSeconds := msgTimestamp / int64(time.Second)
|
2023-05-22 21:38:02 +00:00
|
|
|
storeResult, err := w.query(context.Background(), storeNode.PeerID, relay.DefaultWakuTopic, []common.TopicType{contentTopic}, uint64(timestampInSeconds-20), uint64(timestampInSeconds+20), []store.HistoryRequestOption{})
|
2023-02-22 21:58:17 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotZero(t, len(storeResult.Messages))
|
2023-06-07 09:02:19 +00:00
|
|
|
|
|
|
|
require.NoError(t, w.Stop())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWakuV2Filter(t *testing.T) {
|
|
|
|
enrTreeAddress := testENRBootstrap
|
|
|
|
envEnrTreeAddress := os.Getenv("ENRTREE_ADDRESS")
|
|
|
|
if envEnrTreeAddress != "" {
|
|
|
|
enrTreeAddress = envEnrTreeAddress
|
|
|
|
}
|
|
|
|
|
|
|
|
config := &Config{}
|
|
|
|
config.Port = 0
|
|
|
|
config.LightClient = true
|
|
|
|
config.KeepAliveInterval = 1
|
|
|
|
config.MinPeersForFilter = 2
|
|
|
|
config.EnableDiscV5 = true
|
|
|
|
config.DiscV5BootstrapNodes = []string{enrTreeAddress}
|
|
|
|
config.DiscoveryLimit = 20
|
|
|
|
config.UDPPort = 9001
|
|
|
|
config.WakuNodes = []string{enrTreeAddress}
|
|
|
|
fleet := "status.test" // Need a name fleet so that LightClient is not set to false
|
2023-07-05 15:56:34 +00:00
|
|
|
w, err := New("", fleet, config, nil, nil, nil, nil, nil)
|
2023-06-07 09:02:19 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NoError(t, w.Start())
|
|
|
|
|
|
|
|
// DNSDiscovery
|
|
|
|
// Wait for some peers to be discovered
|
|
|
|
time.Sleep(10 * time.Second)
|
|
|
|
|
|
|
|
// At least 3 peers should have been discovered
|
|
|
|
require.Greater(t, w.PeerCount(), 3)
|
|
|
|
|
|
|
|
filter := &common.Filter{
|
2023-09-13 10:50:23 +00:00
|
|
|
Messages: common.NewMemoryMessageStore(),
|
|
|
|
ContentTopics: common.NewTopicSetFromBytes([][]byte{[]byte{1, 2, 3, 4}}),
|
2023-06-07 09:02:19 +00:00
|
|
|
}
|
|
|
|
|
2023-09-13 10:50:23 +00:00
|
|
|
filterID, err := w.Subscribe(filter)
|
2023-06-07 09:02:19 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
msgTimestamp := w.timestamp()
|
2023-09-13 10:50:23 +00:00
|
|
|
contentTopic := maps.Keys(filter.ContentTopics)[0]
|
2023-06-07 09:02:19 +00:00
|
|
|
|
2023-05-22 21:38:02 +00:00
|
|
|
_, err = w.Send(relay.DefaultWakuTopic, &pb.WakuMessage{
|
2023-06-07 09:02:19 +00:00
|
|
|
Payload: []byte{1, 2, 3, 4, 5},
|
|
|
|
ContentTopic: contentTopic.ContentTopic(),
|
|
|
|
Version: 0,
|
|
|
|
Timestamp: msgTimestamp,
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-06-15 14:42:54 +00:00
|
|
|
time.Sleep(5 * time.Second)
|
2023-06-07 09:02:19 +00:00
|
|
|
|
2023-09-13 10:50:23 +00:00
|
|
|
// Ensure there is at least 1 active filter subscription
|
|
|
|
subscriptions := w.node.FilterLightnode().Subscriptions()
|
|
|
|
require.Greater(t, len(subscriptions), 0)
|
|
|
|
|
2023-06-07 09:02:19 +00:00
|
|
|
// Ensure there are some active peers for this filter subscription
|
2023-09-13 10:50:23 +00:00
|
|
|
stats := w.getFilterStats()
|
|
|
|
require.Greater(t, len(stats[filterID]), 0)
|
2023-06-07 09:02:19 +00:00
|
|
|
|
|
|
|
messages := filter.Retrieve()
|
|
|
|
require.Len(t, messages, 1)
|
|
|
|
|
|
|
|
// Mock peers going down
|
2023-09-13 10:50:23 +00:00
|
|
|
isFilterSubAliveBak := w.filterManager.isFilterSubAlive
|
|
|
|
w.filterManager.settings.MinPeersForFilter = 0
|
|
|
|
w.filterManager.isFilterSubAlive = func(sub *subscription.SubscriptionDetails) error {
|
2023-06-07 09:02:19 +00:00
|
|
|
return errors.New("peer down")
|
|
|
|
}
|
|
|
|
|
2023-09-13 10:50:23 +00:00
|
|
|
time.Sleep(5 * time.Second)
|
2023-06-07 09:02:19 +00:00
|
|
|
|
|
|
|
// Ensure there are 0 active peers now
|
2023-09-13 10:50:23 +00:00
|
|
|
|
|
|
|
stats = w.getFilterStats()
|
|
|
|
require.Len(t, stats[filterID], 0)
|
2023-06-07 09:02:19 +00:00
|
|
|
|
|
|
|
// Reconnect
|
2023-09-13 10:50:23 +00:00
|
|
|
w.filterManager.settings.MinPeersForFilter = 2
|
|
|
|
w.filterManager.isFilterSubAlive = isFilterSubAliveBak
|
2023-06-07 09:02:19 +00:00
|
|
|
time.Sleep(10 * time.Second)
|
|
|
|
|
|
|
|
// Ensure there are some active peers now
|
2023-09-13 10:50:23 +00:00
|
|
|
stats = w.getFilterStats()
|
|
|
|
require.Greater(t, len(stats[filterID]), 0)
|
2023-06-07 09:02:19 +00:00
|
|
|
|
|
|
|
require.NoError(t, w.Stop())
|
2023-02-22 21:58:17 +00:00
|
|
|
}
|