2021-10-28 13:03:23 +00:00
|
|
|
package store
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/libp2p/go-libp2p"
|
2022-10-19 19:39:32 +00:00
|
|
|
"github.com/libp2p/go-libp2p/core/peer"
|
|
|
|
"github.com/libp2p/go-libp2p/core/peerstore"
|
2023-08-16 01:40:00 +00:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2021-10-28 13:03:23 +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/pb"
|
2023-05-05 09:49:15 +00:00
|
|
|
"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"
|
2023-11-07 19:48:43 +00:00
|
|
|
"google.golang.org/protobuf/proto"
|
2021-10-28 13:03:23 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestFindLastSeenMessage(t *testing.T) {
|
2023-11-07 19:48:43 +00:00
|
|
|
now := *utils.GetUnixEpoch()
|
|
|
|
msg1 := protocol.NewEnvelope(tests.CreateWakuMessage("1", proto.Int64(now+1)), *utils.GetUnixEpoch(), "test")
|
|
|
|
msg2 := protocol.NewEnvelope(tests.CreateWakuMessage("2", proto.Int64(now+2)), *utils.GetUnixEpoch(), "test")
|
|
|
|
msg3 := protocol.NewEnvelope(tests.CreateWakuMessage("3", proto.Int64(now+3)), *utils.GetUnixEpoch(), "test")
|
|
|
|
msg4 := protocol.NewEnvelope(tests.CreateWakuMessage("4", proto.Int64(now+4)), *utils.GetUnixEpoch(), "test")
|
|
|
|
msg5 := protocol.NewEnvelope(tests.CreateWakuMessage("5", proto.Int64(now+5)), *utils.GetUnixEpoch(), "test")
|
2021-10-28 13:03:23 +00:00
|
|
|
|
2023-08-16 01:40:00 +00:00
|
|
|
s := NewWakuStore(MemoryDB(t), nil, timesource.NewDefaultClock(), prometheus.DefaultRegisterer, utils.Logger())
|
2022-02-23 15:01:53 +00:00
|
|
|
_ = s.storeMessage(msg1)
|
|
|
|
_ = s.storeMessage(msg3)
|
|
|
|
_ = s.storeMessage(msg5)
|
|
|
|
_ = s.storeMessage(msg2)
|
|
|
|
_ = s.storeMessage(msg4)
|
2021-10-28 13:03:23 +00:00
|
|
|
|
2022-05-30 18:48:22 +00:00
|
|
|
lastSeen, err := s.findLastSeen()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-11-07 19:48:43 +00:00
|
|
|
require.Equal(t, msg5.Message().GetTimestamp(), lastSeen)
|
2021-10-28 13:03:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestResume(t *testing.T) {
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
2022-03-22 13:12:58 +00:00
|
|
|
host1, err := libp2p.New(libp2p.DefaultTransports, libp2p.ListenAddrStrings("/ip4/0.0.0.0/tcp/0"))
|
2021-10-28 13:03:23 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-08-16 01:40:00 +00:00
|
|
|
s1 := NewWakuStore(MemoryDB(t), nil, timesource.NewDefaultClock(), prometheus.DefaultRegisterer, utils.Logger())
|
2023-04-17 00:04:12 +00:00
|
|
|
s1.SetHost(host1)
|
2023-10-20 19:56:18 +00:00
|
|
|
sub := relay.NewSubscription(protocol.NewContentFilter(relay.DefaultWakuTopic))
|
|
|
|
err = s1.Start(ctx, sub)
|
2022-12-09 03:08:04 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-10-28 13:03:23 +00:00
|
|
|
defer s1.Stop()
|
|
|
|
|
2023-11-07 19:48:43 +00:00
|
|
|
now := *utils.GetUnixEpoch()
|
|
|
|
for i := int64(0); i < 10; i++ {
|
2021-10-28 13:03:23 +00:00
|
|
|
var contentTopic = "1"
|
|
|
|
if i%2 == 0 {
|
|
|
|
contentTopic = "2"
|
|
|
|
}
|
|
|
|
|
2023-11-07 19:48:43 +00:00
|
|
|
wakuMessage := tests.CreateWakuMessage(contentTopic, proto.Int64(now+i+1))
|
|
|
|
msg := protocol.NewEnvelope(wakuMessage, *utils.GetUnixEpoch(), "test")
|
2022-02-23 15:01:53 +00:00
|
|
|
_ = s1.storeMessage(msg)
|
2021-10-28 13:03:23 +00:00
|
|
|
}
|
|
|
|
|
2022-03-22 13:12:58 +00:00
|
|
|
host2, err := libp2p.New(libp2p.DefaultTransports, libp2p.ListenAddrStrings("/ip4/0.0.0.0/tcp/0"))
|
2021-10-28 13:03:23 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-08-16 01:40:00 +00:00
|
|
|
s2 := NewWakuStore(MemoryDB(t), nil, timesource.NewDefaultClock(), prometheus.DefaultRegisterer, utils.Logger())
|
2023-04-17 00:04:12 +00:00
|
|
|
s2.SetHost(host2)
|
2023-10-20 19:56:18 +00:00
|
|
|
sub1 := relay.NewSubscription(protocol.NewContentFilter(relay.DefaultWakuTopic))
|
|
|
|
|
|
|
|
err = s2.Start(ctx, sub1)
|
2022-12-09 03:08:04 +00:00
|
|
|
require.NoError(t, err)
|
2021-10-28 13:03:23 +00:00
|
|
|
defer s2.Stop()
|
|
|
|
|
|
|
|
host2.Peerstore().AddAddr(host1.ID(), tests.GetHostAddress(host1), peerstore.PermanentAddrTTL)
|
2023-02-16 16:17:52 +00:00
|
|
|
err = host2.Peerstore().AddProtocols(host1.ID(), StoreID_v20beta4)
|
2021-10-28 13:03:23 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
msgCount, err := s2.Resume(ctx, "test", []peer.ID{host1.ID()})
|
|
|
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 10, msgCount)
|
2022-05-30 18:48:22 +00:00
|
|
|
|
|
|
|
allMsgs, err := s2.msgProvider.GetAll()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.Len(t, allMsgs, 10)
|
2021-10-28 13:03:23 +00:00
|
|
|
|
|
|
|
// Test duplication
|
|
|
|
msgCount, err = s2.Resume(ctx, "test", []peer.ID{host1.ID()})
|
|
|
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 0, msgCount)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestResumeWithListOfPeers(t *testing.T) {
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
// Host that does not support store protocol
|
2022-03-22 13:12:58 +00:00
|
|
|
invalidHost, err := libp2p.New(libp2p.DefaultTransports, libp2p.ListenAddrStrings("/ip4/0.0.0.0/tcp/0"))
|
2021-10-28 13:03:23 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-03-22 13:12:58 +00:00
|
|
|
host1, err := libp2p.New(libp2p.DefaultTransports, libp2p.ListenAddrStrings("/ip4/0.0.0.0/tcp/0"))
|
2021-10-28 13:03:23 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-08-16 01:40:00 +00:00
|
|
|
s1 := NewWakuStore(MemoryDB(t), nil, timesource.NewDefaultClock(), prometheus.DefaultRegisterer, utils.Logger())
|
2023-04-17 00:04:12 +00:00
|
|
|
s1.SetHost(host1)
|
2023-10-20 19:56:18 +00:00
|
|
|
sub := relay.NewSubscription(protocol.NewContentFilter(relay.DefaultWakuTopic))
|
|
|
|
|
|
|
|
err = s1.Start(ctx, sub)
|
2022-12-09 03:08:04 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-10-28 13:03:23 +00:00
|
|
|
defer s1.Stop()
|
|
|
|
|
2023-11-07 19:48:43 +00:00
|
|
|
msg0 := &pb.WakuMessage{Payload: []byte{1, 2, 3}, ContentTopic: "2", Timestamp: utils.GetUnixEpoch()}
|
2021-10-28 13:03:23 +00:00
|
|
|
|
2023-11-07 19:48:43 +00:00
|
|
|
_ = s1.storeMessage(protocol.NewEnvelope(msg0, *utils.GetUnixEpoch(), "test"))
|
2021-10-28 13:03:23 +00:00
|
|
|
|
2022-03-22 13:12:58 +00:00
|
|
|
host2, err := libp2p.New(libp2p.DefaultTransports, libp2p.ListenAddrStrings("/ip4/0.0.0.0/tcp/0"))
|
2021-10-28 13:03:23 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-08-16 01:40:00 +00:00
|
|
|
s2 := NewWakuStore(MemoryDB(t), nil, timesource.NewDefaultClock(), prometheus.DefaultRegisterer, utils.Logger())
|
2023-04-17 00:04:12 +00:00
|
|
|
s2.SetHost(host2)
|
2023-10-20 19:56:18 +00:00
|
|
|
sub1 := relay.NewSubscription(protocol.NewContentFilter(relay.DefaultWakuTopic))
|
|
|
|
|
|
|
|
err = s2.Start(ctx, sub1)
|
2022-12-09 03:08:04 +00:00
|
|
|
require.NoError(t, err)
|
2021-10-28 13:03:23 +00:00
|
|
|
defer s2.Stop()
|
|
|
|
|
|
|
|
host2.Peerstore().AddAddr(host1.ID(), tests.GetHostAddress(host1), peerstore.PermanentAddrTTL)
|
2023-02-16 16:17:52 +00:00
|
|
|
err = host2.Peerstore().AddProtocols(host1.ID(), StoreID_v20beta4)
|
2021-10-28 13:03:23 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
msgCount, err := s2.Resume(ctx, "test", []peer.ID{invalidHost.ID(), host1.ID()})
|
|
|
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 1, msgCount)
|
2022-05-30 18:48:22 +00:00
|
|
|
|
|
|
|
allMsgs, err := s2.msgProvider.GetAll()
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, allMsgs, 1)
|
2021-10-28 13:03:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestResumeWithoutSpecifyingPeer(t *testing.T) {
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
2022-03-22 13:12:58 +00:00
|
|
|
host1, err := libp2p.New(libp2p.DefaultTransports, libp2p.ListenAddrStrings("/ip4/0.0.0.0/tcp/0"))
|
2021-10-28 13:03:23 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-08-16 01:40:00 +00:00
|
|
|
s1 := NewWakuStore(MemoryDB(t), nil, timesource.NewDefaultClock(), prometheus.DefaultRegisterer, utils.Logger())
|
2023-04-17 00:04:12 +00:00
|
|
|
s1.SetHost(host1)
|
2023-10-20 19:56:18 +00:00
|
|
|
sub := relay.NewSubscription(protocol.NewContentFilter(relay.DefaultWakuTopic))
|
|
|
|
|
|
|
|
err = s1.Start(ctx, sub)
|
2022-12-09 03:08:04 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-10-28 13:03:23 +00:00
|
|
|
defer s1.Stop()
|
|
|
|
|
2023-11-07 19:48:43 +00:00
|
|
|
msg0 := &pb.WakuMessage{Payload: []byte{1, 2, 3}, ContentTopic: "2"}
|
2021-10-28 13:03:23 +00:00
|
|
|
|
2023-11-07 19:48:43 +00:00
|
|
|
_ = s1.storeMessage(protocol.NewEnvelope(msg0, *utils.GetUnixEpoch(), "test"))
|
2021-10-28 13:03:23 +00:00
|
|
|
|
2022-03-22 13:12:58 +00:00
|
|
|
host2, err := libp2p.New(libp2p.DefaultTransports, libp2p.ListenAddrStrings("/ip4/0.0.0.0/tcp/0"))
|
2021-10-28 13:03:23 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-08-16 01:40:00 +00:00
|
|
|
s2 := NewWakuStore(MemoryDB(t), nil, timesource.NewDefaultClock(), prometheus.DefaultRegisterer, utils.Logger())
|
2023-04-17 00:04:12 +00:00
|
|
|
s2.SetHost(host2)
|
2023-10-20 19:56:18 +00:00
|
|
|
sub1 := relay.NewSubscription(protocol.NewContentFilter(relay.DefaultWakuTopic))
|
|
|
|
|
|
|
|
err = s2.Start(ctx, sub1)
|
2022-12-09 03:08:04 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-10-28 13:03:23 +00:00
|
|
|
defer s2.Stop()
|
|
|
|
|
|
|
|
host2.Peerstore().AddAddr(host1.ID(), tests.GetHostAddress(host1), peerstore.PermanentAddrTTL)
|
2023-02-16 16:17:52 +00:00
|
|
|
err = host2.Peerstore().AddProtocols(host1.ID(), StoreID_v20beta4)
|
2021-10-28 13:03:23 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-11-25 20:54:11 +00:00
|
|
|
_, err = s2.Resume(ctx, "test", []peer.ID{})
|
|
|
|
require.Error(t, err)
|
2021-10-28 13:03:23 +00:00
|
|
|
}
|