-
Notifications
You must be signed in to change notification settings - Fork 246
/
messenger_communities_sharding_test.go
226 lines (189 loc) · 6.8 KB
/
messenger_communities_sharding_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
package protocol
import (
"context"
"crypto/ecdsa"
"testing"
"github.com/golang/protobuf/proto"
"github.com/stretchr/testify/suite"
"go.uber.org/zap"
gethbridge "github.com/status-im/status-go/eth-node/bridge/geth"
"github.com/status-im/status-go/eth-node/types"
"github.com/status-im/status-go/protocol/common"
"github.com/status-im/status-go/protocol/common/shard"
"github.com/status-im/status-go/protocol/communities"
"github.com/status-im/status-go/protocol/protobuf"
"github.com/status-im/status-go/protocol/requests"
"github.com/status-im/status-go/protocol/tt"
)
func TestMessengerCommunitiesShardingSuite(t *testing.T) {
suite.Run(t, new(MessengerCommunitiesShardingSuite))
}
type MessengerCommunitiesShardingSuite struct {
suite.Suite
owner *Messenger
ownerWaku types.Waku
alice *Messenger
aliceWaku types.Waku
aliceUnhandledMessagesTracker *unhandledMessagesTracker
logger *zap.Logger
}
func (s *MessengerCommunitiesShardingSuite) SetupTest() {
s.logger = tt.MustCreateTestLogger()
wakuNodes := CreateWakuV2Network(&s.Suite, s.logger, true, []string{"owner", "alice"})
nodeConfig := defaultTestCommunitiesMessengerNodeConfig()
nodeConfig.WakuV2Config.UseShardAsDefaultTopic = true
s.ownerWaku = wakuNodes[0]
s.owner = newTestCommunitiesMessenger(&s.Suite, s.ownerWaku, testCommunitiesMessengerConfig{
testMessengerConfig: testMessengerConfig{
name: "owner",
logger: s.logger,
},
nodeConfig: nodeConfig,
})
s.aliceUnhandledMessagesTracker = &unhandledMessagesTracker{
messages: map[protobuf.ApplicationMetadataMessage_Type][]*unhandedMessage{},
}
s.aliceWaku = wakuNodes[1]
s.alice = newTestCommunitiesMessenger(&s.Suite, s.aliceWaku, testCommunitiesMessengerConfig{
testMessengerConfig: testMessengerConfig{
name: "alice",
logger: s.logger,
unhandledMessagesTracker: s.aliceUnhandledMessagesTracker,
},
nodeConfig: nodeConfig,
})
_, err := s.owner.Start()
s.Require().NoError(err)
_, err = s.alice.Start()
s.Require().NoError(err)
}
func (s *MessengerCommunitiesShardingSuite) TearDownTest() {
if s.owner != nil {
TearDownMessenger(&s.Suite, s.owner)
}
if s.ownerWaku != nil {
s.Require().NoError(gethbridge.GetGethWakuV2From(s.ownerWaku).Stop())
}
if s.alice != nil {
TearDownMessenger(&s.Suite, s.alice)
}
if s.aliceWaku != nil {
s.Require().NoError(gethbridge.GetGethWakuV2From(s.aliceWaku).Stop())
}
_ = s.logger.Sync()
}
func (s *MessengerCommunitiesShardingSuite) testPostToCommunityChat(shard *shard.Shard, community *communities.Community, chat *Chat) {
_, err := s.owner.SetCommunityShard(&requests.SetCommunityShard{
CommunityID: community.ID(),
Shard: shard,
})
s.Require().NoError(err)
_, err = WaitOnMessengerResponse(s.alice, func(mr *MessengerResponse) bool {
if len(mr.communities) == 0 {
return false
}
if shard == nil {
return mr.Communities()[0].Shard() == nil
}
return mr.Communities()[0].Shard() != nil && mr.Communities()[0].Shard().Index == shard.Index
}, "shard info not updated")
s.Require().NoError(err)
message := buildTestMessage(*chat)
_, err = s.owner.SendChatMessage(context.Background(), message)
s.Require().NoError(err)
_, err = WaitOnMessengerResponse(s.alice, func(mr *MessengerResponse) bool {
return len(mr.messages) > 0 && mr.Messages()[0].ID == message.ID
}, "message not received")
s.Require().NoError(err)
}
func (s *MessengerCommunitiesShardingSuite) TestPostToCommunityChat() {
community, chat := createCommunity(&s.Suite, s.owner)
advertiseCommunityToUserOldWay(&s.Suite, community, s.owner, s.alice)
joinCommunity(&s.Suite, community, s.owner, s.alice, &requests.RequestToJoinCommunity{CommunityID: community.ID()}, "")
// Members should be able to receive messages in a community with sharding enabled.
{
shard := &shard.Shard{
Cluster: shard.MainStatusShardCluster,
Index: 128,
}
s.testPostToCommunityChat(shard, community, chat)
}
// Members should be able to receive messages in a community where the sharding configuration has been edited.
{
shard := &shard.Shard{
Cluster: shard.MainStatusShardCluster,
Index: 256,
}
s.testPostToCommunityChat(shard, community, chat)
}
// Members should continue to receive messages in a community if it is moved back to default shard.
{
shard := &shard.Shard{
Cluster: shard.MainStatusShardCluster,
Index: 32,
}
s.testPostToCommunityChat(shard, community, chat)
}
}
func (s *MessengerCommunitiesShardingSuite) TestIgnoreOutdatedShardKey() {
community, _ := createCommunity(&s.Suite, s.owner)
advertiseCommunityToUserOldWay(&s.Suite, community, s.owner, s.alice)
joinCommunity(&s.Suite, community, s.owner, s.alice, &requests.RequestToJoinCommunity{CommunityID: community.ID()}, "")
shard := &shard.Shard{
Cluster: shard.MainStatusShardCluster,
Index: 128,
}
// Members should receive shard update.
{
response, err := s.owner.SetCommunityShard(&requests.SetCommunityShard{
CommunityID: community.ID(),
Shard: shard,
})
s.Require().NoError(err)
s.Require().Len(response.Communities(), 1)
community = response.Communities()[0]
_, err = WaitOnMessengerResponse(s.alice, func(mr *MessengerResponse) bool {
return len(mr.communities) > 0 && mr.Communities()[0].Shard() != nil && mr.Communities()[0].Shard().Index == shard.Index
}, "shard info not updated")
s.Require().NoError(err)
}
// Members should ignore outdated shard update.
{
// Simulate outdated CommunityShardKey message.
shard.Index = 256
communityShardKey := &protobuf.CommunityShardKey{
Clock: community.Clock() - 1, // simulate outdated clock
CommunityId: community.ID(),
Shard: shard.Protobuffer(),
}
encodedMessage, err := proto.Marshal(communityShardKey)
s.Require().NoError(err)
rawMessage := common.RawMessage{
Recipients: []*ecdsa.PublicKey{&s.alice.identity.PublicKey},
ResendType: common.ResendTypeDataSync,
MessageType: protobuf.ApplicationMetadataMessage_COMMUNITY_SHARD_KEY,
Payload: encodedMessage,
}
_, err = s.owner.sender.SendPubsubTopicKey(context.Background(), &rawMessage)
s.Require().NoError(err)
_, err = WaitOnMessengerResponse(s.alice, func(mr *MessengerResponse) bool {
msgType := protobuf.ApplicationMetadataMessage_COMMUNITY_SHARD_KEY
msgs, exists := s.aliceUnhandledMessagesTracker.messages[msgType]
if !exists {
return false
}
for _, msg := range msgs {
p := &protobuf.CommunityShardKey{}
err := proto.Unmarshal(msg.ApplicationLayer.Payload, p)
if err != nil {
panic(err)
}
if msg.err == communities.ErrOldShardInfo && p.Shard != nil && p.Shard.Index == int32(shard.Index) {
return true
}
}
return false
}, "shard info with outdated clock either not received or not ignored")
s.Require().NoError(err)
}
}