-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathintegration.cpp
More file actions
293 lines (221 loc) · 9.6 KB
/
integration.cpp
File metadata and controls
293 lines (221 loc) · 9.6 KB
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
#include <QtTest>
#include <vector>
#include <random>
#include <plugin.h>
class TestIntegration : public QObject
{
Q_OBJECT
private slots:
void connectAndDisconnect()
{
Libp2pModulePlugin nodeA;
Libp2pModulePlugin nodeB;
QVERIFY(nodeA.syncLibp2pStart().ok);
QVERIFY(nodeB.syncLibp2pStart().ok);
PeerInfo nodeAPeerInfo = nodeA.syncPeerInfo().data.value<PeerInfo>();
// cannot disconnect, invalid peerId
QVERIFY(!nodeB.syncDisconnectPeer("fakePeerId").ok);
QVERIFY(nodeB.syncConnectPeer(nodeAPeerInfo.peerId, nodeAPeerInfo.addrs, 500).ok);
QCOMPARE(nodeB.syncConnectedPeers(Direction_Out).data.value<QList<QString>>().size(), 1);
QCOMPARE(nodeA.syncConnectedPeers(Direction_In).data.value<QList<QString>>().size(), 1);
QVERIFY(nodeB.syncDisconnectPeer(nodeAPeerInfo.peerId).ok);
QVERIFY(nodeA.syncLibp2pStop().ok);
QVERIFY(nodeB.syncLibp2pStop().ok);
}
void kadPutGet()
{
// setup node A
Libp2pModulePlugin nodeA;
QVERIFY(nodeA.syncLibp2pStart().ok);
PeerInfo nodeAPeerInfo = nodeA.syncPeerInfo().data.value<PeerInfo>();
// setup node B
Libp2pModulePlugin nodeB({}, { nodeAPeerInfo });
QVERIFY(nodeB.syncLibp2pStart().ok);
QByteArray key = "integration-key";
QByteArray value = "hello";
// put value until it shows on other peer
int quorum = 1;
QByteArray result;
for (int i = 0; i < 10; ++i) {
QVERIFY(nodeA.syncKadPutValue(key, value).ok);
QThread::msleep(200);
result = nodeB.syncKadGetValue(key, quorum).data.value<QByteArray>();
if (!result.isEmpty())
break;
QThread::msleep(200);
}
QCOMPARE(result, value);
QVERIFY(nodeB.syncDisconnectPeer(nodeAPeerInfo.peerId).ok);
QVERIFY(nodeA.syncLibp2pStop().ok);
QVERIFY(nodeB.syncLibp2pStop().ok);
}
void mixDialAndReply()
{
const int NUM_NODES = 5;
std::vector<std::unique_ptr<Libp2pModulePlugin>> nodes;
QVector<PeerInfo> infos(NUM_NODES);
QVector<QByteArray> mixPrivKeys(NUM_NODES);
QVector<QByteArray> mixPubKeys(NUM_NODES);
QVector<QByteArray> libp2pPubKeys(NUM_NODES);
// --- Start nodes ---
for (int i = 0; i < NUM_NODES; ++i) {
nodes.push_back(std::make_unique<Libp2pModulePlugin>());
QVERIFY(nodes[i]->syncLibp2pStart().ok);
infos[i] = nodes[i]->syncPeerInfo().data.value<PeerInfo>();
QVERIFY(!infos[i].peerId.isEmpty());
QVERIFY(!infos[i].addrs.isEmpty());
mixPrivKeys[i] = nodes[i]->mixGeneratePrivKey();
mixPubKeys[i] = nodes[i]->mixPublicKey(mixPrivKeys[i]);
libp2pPubKeys[i] =
nodes[i]->syncLibp2pPublicKey().data.value<QByteArray>();
QVERIFY(nodes[i]->syncMixSetNodeInfo(
infos[i].addrs[0],
mixPrivKeys[i]).ok);
QVERIFY(nodes[i]->syncMixRegisterDestReadBehavior(
"/ipfs/ping/1.0.0",
LIBP2P_MIX_READ_EXACTLY,
32).ok);
}
// --- Populate mix pools ---
for (int i = 0; i < NUM_NODES; ++i) {
for (int j = 0; j < NUM_NODES; ++j) {
if (i == j)
continue;
QVERIFY(nodes[i]->syncMixNodepoolAdd(
infos[j].peerId,
infos[j].addrs[0],
mixPubKeys[j],
libp2pPubKeys[j]).ok);
}
}
// --- Dial through mix ---
Libp2pResult dialResult = nodes[0]->syncMixDialWithReply(
infos[4].peerId,
infos[4].addrs[0],
"/ipfs/ping/1.0.0",
1,
0
);
QVERIFY(dialResult.ok);
uint64_t streamId = dialResult.data.value<uint64_t>();
// generate a random payload
QByteArray payload(32, 0);
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<int> dist(0, 255);
for (int i = 0; i < payload.size(); ++i)
payload[i] = static_cast<char>(dist(gen));
QVERIFY(nodes[0]->syncStreamWrite(streamId, payload).ok);
Libp2pResult readRes =
nodes[0]->syncStreamReadExactly(streamId, payload.size());
QVERIFY(readRes.ok);
QByteArray received = readRes.data.value<QByteArray>();
QCOMPARE(received, payload);
QVERIFY(nodes[0]->syncStreamClose(streamId).ok);
QVERIFY(nodes[0]->syncStreamRelease(streamId).ok);
// --- Shutdown ---
for (int i = 0; i < NUM_NODES; ++i) {
QVERIFY(nodes[i]->syncLibp2pStop().ok);
}
}
void mixNodepoolRouting()
{
// Smaller test focused on pool correctness
Libp2pModulePlugin nodeA;
Libp2pModulePlugin nodeB;
Libp2pModulePlugin nodeC;
QVERIFY(nodeA.syncLibp2pStart().ok);
QVERIFY(nodeB.syncLibp2pStart().ok);
QVERIFY(nodeC.syncLibp2pStart().ok);
PeerInfo infoA = nodeA.syncPeerInfo().data.value<PeerInfo>();
PeerInfo infoB = nodeB.syncPeerInfo().data.value<PeerInfo>();
PeerInfo infoC = nodeC.syncPeerInfo().data.value<PeerInfo>();
QByteArray privA = nodeA.mixGeneratePrivKey();
QByteArray privB = nodeB.mixGeneratePrivKey();
QByteArray privC = nodeC.mixGeneratePrivKey();
QByteArray pubA = nodeA.mixPublicKey(privA);
QByteArray pubB = nodeB.mixPublicKey(privB);
QByteArray pubC = nodeC.mixPublicKey(privC);
QByteArray libp2pPubA =
nodeA.syncLibp2pPublicKey().data.value<QByteArray>();
QByteArray libp2pPubB =
nodeB.syncLibp2pPublicKey().data.value<QByteArray>();
QByteArray libp2pPubC =
nodeC.syncLibp2pPublicKey().data.value<QByteArray>();
QVERIFY(nodeA.syncMixSetNodeInfo(infoA.addrs[0], privA).ok);
QVERIFY(nodeB.syncMixSetNodeInfo(infoB.addrs[0], privB).ok);
QVERIFY(nodeC.syncMixSetNodeInfo(infoC.addrs[0], privC).ok);
QVERIFY(nodeA.syncMixNodepoolAdd(infoB.peerId, infoB.addrs[0], pubB, libp2pPubB).ok);
QVERIFY(nodeA.syncMixNodepoolAdd(infoC.peerId, infoC.addrs[0], pubC, libp2pPubC).ok);
QVERIFY(nodeB.syncMixNodepoolAdd(infoA.peerId, infoA.addrs[0], pubA, libp2pPubA).ok);
QVERIFY(nodeC.syncMixNodepoolAdd(infoA.peerId, infoA.addrs[0], pubA, libp2pPubA).ok);
QVERIFY(nodeA.syncLibp2pStop().ok);
QVERIFY(nodeB.syncLibp2pStop().ok);
QVERIFY(nodeC.syncLibp2pStop().ok);
}
void subscribeAndPublish()
{
Libp2pModulePlugin nodeA;
Libp2pModulePlugin nodeB;
QVERIFY(nodeA.syncLibp2pStart().ok);
QVERIFY(nodeB.syncLibp2pStart().ok);
// Connect nodeB to nodeA
PeerInfo infoA = nodeA.syncPeerInfo().data.value<PeerInfo>();
PeerInfo infoB = nodeB.syncPeerInfo().data.value<PeerInfo>();
QVERIFY(nodeB.syncConnectPeer(infoA.peerId, infoA.addrs, 500).ok);
// gossipsub requires both peers to be subscribed to the topic
// and subscriptions to be broadcasted (takes 2 heartbeats)
QString topic = "integration-topic";
QVERIFY(nodeB.syncGossipsubSubscribe(topic).ok);
QVERIFY(nodeA.syncGossipsubSubscribe(topic).ok);
QThread::msleep(2000);
QByteArray payload = "Hello from Node A";
QVERIFY(nodeA.syncGossipsubPublish(topic, payload).ok);
QByteArray received = nodeB.syncGossipsubNextMessage(topic).data.value<QByteArray>();
QCOMPARE(received, payload);
QVERIFY(nodeA.syncLibp2pStop().ok);
QVERIFY(nodeB.syncLibp2pStop().ok);
}
void multipleSubscribers()
{
Libp2pModulePlugin nodeA;
QVERIFY(nodeA.syncLibp2pStart().ok);
QString topic = "multi-topic";
QVERIFY(nodeA.syncGossipsubSubscribe(topic).ok);
const int NUM_SUBS = 3;
std::vector<std::unique_ptr<Libp2pModulePlugin>> subscribers;
// Start subscribers
for (int i = 0; i < NUM_SUBS; ++i) {
subscribers.emplace_back(std::make_unique<Libp2pModulePlugin>());
QVERIFY(subscribers.back()->syncLibp2pStart().ok);
// Connect each subscriber to nodeA
PeerInfo infoA = nodeA.syncPeerInfo().data.value<PeerInfo>();
QVERIFY(subscribers.back()->syncConnectPeer(infoA.peerId, infoA.addrs, 500).ok);
QVERIFY(subscribers.back()->syncGossipsubSubscribe(topic).ok);
}
QThread::msleep(2000);
QByteArray payload = "Broadcast message";
QVERIFY(nodeA.syncGossipsubPublish(topic, payload).ok);
for (auto &sub : subscribers) {
QByteArray received = sub->syncGossipsubNextMessage(topic).data.value<QByteArray>();
QCOMPARE(received, payload);
QVERIFY(sub->syncLibp2pStop().ok);
}
QVERIFY(nodeA.syncLibp2pStop().ok);
}
void subscribeUnsubscribe()
{
Libp2pModulePlugin node;
QVERIFY(node.syncLibp2pStart().ok);
QString topic = "temp-topic";
QVERIFY(node.syncGossipsubSubscribe(topic).ok);
QVERIFY(node.syncGossipsubUnsubscribe(topic).ok);
QByteArray payload = "Test after unsubscribe";
QVERIFY(node.syncGossipsubPublish(topic, payload).ok);
// Should not receive messages after unsubscribe
QVERIFY(!node.syncGossipsubNextMessage(topic).ok);
QVERIFY(node.syncLibp2pStop().ok);
}
};
QTEST_MAIN(TestIntegration)
#include "integration.moc"