Skip to content

Commit 73bb337

Browse files
authored
feat(kad): add provider tests (#8)
1 parent 0a4e36c commit 73bb337

File tree

4 files changed

+309
-63
lines changed

4 files changed

+309
-63
lines changed

src/libp2p_module_interface.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,20 @@ class Libp2pModuleInterface : public PluginInterface
1212
{
1313
public:
1414
virtual ~Libp2pModuleInterface() {}
15+
Q_INVOKABLE virtual bool toCid(const QByteArray &key) = 0;
1516
Q_INVOKABLE virtual bool foo(const QString &bar) = 0;
17+
1618
Q_INVOKABLE virtual bool libp2pStart() = 0;
1719
Q_INVOKABLE virtual bool libp2pStop() = 0;
18-
Q_INVOKABLE virtual bool setEventCallback() = 0;
20+
1921
Q_INVOKABLE virtual bool findNode(const QString &peerId) = 0;
2022
Q_INVOKABLE virtual bool putValue(const QByteArray &key, const QByteArray &value) = 0;
2123
Q_INVOKABLE virtual bool getValue(const QByteArray &key, int quorum = -1) = 0;
2224
Q_INVOKABLE virtual bool addProvider(const QString &cid) = 0;
2325
Q_INVOKABLE virtual bool startProviding(const QString &cid) = 0;
2426
Q_INVOKABLE virtual bool stopProviding(const QString &cid) = 0;
2527
Q_INVOKABLE virtual bool getProviders(const QString &cid) = 0;
26-
28+
Q_INVOKABLE virtual bool setEventCallback() = 0;
2729

2830
signals:
2931
// Generic async callback bridge

src/libp2p_module_plugin.cpp

Lines changed: 103 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@ struct CallbackContext {
1111
Libp2pModulePlugin *instance;
1212
};
1313

14+
struct PeerInfo {
15+
QByteArray peerId;
16+
QList<QByteArray> addrs;
17+
};
18+
19+
20+
/* -------------------- Callbacks -------------------- */
21+
1422
void Libp2pModulePlugin::onLibp2pEventDefault(
1523
int result,
1624
const QString &reqId,
@@ -131,22 +139,49 @@ void Libp2pModulePlugin::libp2pBufferCallback(
131139
if (data && dataLen > 0)
132140
buffer = QByteArray(reinterpret_cast<const char *>(data), int(dataLen));
133141

142+
QString message;
143+
if (msg && len > 0)
144+
message = QString::fromUtf8(msg, int(len));
145+
134146
QPointer<Libp2pModulePlugin> safeSelf(self);
135-
if (caller == "getValue") {
136-
QMetaObject::invokeMethod(safeSelf, [safeSelf, callerRet, buffer, reqId]() {
137-
if (!safeSelf) return;
138-
emit safeSelf->getValueFinished(callerRet, reqId, buffer);
139-
}, Qt::QueuedConnection);
140-
} else {
141-
QMetaObject::invokeMethod(safeSelf, [safeSelf, callerRet, buffer, caller, reqId]() {
142-
if (!safeSelf) return;
143-
emit safeSelf->libp2pEvent(callerRet, reqId, caller, QString(), QVariant(buffer));
144-
}, Qt::QueuedConnection);
145-
}
147+
QMetaObject::invokeMethod(safeSelf, [safeSelf, callerRet, message, buffer, caller, reqId]() {
148+
if (!safeSelf) return;
149+
emit safeSelf->libp2pEvent(callerRet, reqId, caller, message, QVariant(buffer));
150+
}, Qt::QueuedConnection);
146151

147152
delete callbackCtx;
148153
}
149154

155+
QList<PeerInfo> copyProviders(
156+
const Libp2pPeerInfo *providers,
157+
size_t providersLen
158+
)
159+
{
160+
QList<PeerInfo> providersCopy;
161+
162+
if (!providers || providersLen == 0) {
163+
return providersCopy;
164+
}
165+
166+
providersCopy.reserve(providersLen);
167+
168+
for (size_t i = 0; i < providersLen; ++i) {
169+
PeerInfo copy;
170+
171+
if (providers[i].peerId)
172+
copy.peerId = providers[i].peerId;
173+
174+
for (size_t j = 0; j < providers[i].addrsLen; ++j) {
175+
const char* addr = providers[i].addrs[j];
176+
if (addr)
177+
copy.addrs.append(addr);
178+
}
179+
180+
providersCopy.append(std::move(copy));
181+
}
182+
183+
return providersCopy;
184+
}
150185

151186
void Libp2pModulePlugin::getProvidersCallback(
152187
int callerRet,
@@ -166,21 +201,24 @@ void Libp2pModulePlugin::getProvidersCallback(
166201
QString caller = callbackCtx->caller;
167202
QString reqId = callbackCtx->reqId;
168203

204+
QList<PeerInfo> providersCopy = copyProviders(providers, providersLen);
205+
169206
QString message;
170207
if (msg && len > 0)
171208
message = QString::fromUtf8(msg, int(len));
172209

173210
QPointer<Libp2pModulePlugin> safeSelf(self);
174211
QMetaObject::invokeMethod(
175212
safeSelf,
176-
[safeSelf, callerRet, message, caller, reqId]() {
213+
[safeSelf, callerRet, reqId, caller, message,
214+
providersCopy = std::move(providersCopy)]() { // avoid copying providers again
177215
if (!safeSelf) return;
178216
emit safeSelf->libp2pEvent(
179217
callerRet,
180218
reqId,
181219
caller,
182220
message,
183-
QVariant()
221+
QVariant::fromValue(providersCopy)
184222
);
185223
},
186224
Qt::QueuedConnection
@@ -189,9 +227,14 @@ void Libp2pModulePlugin::getProvidersCallback(
189227
delete callbackCtx;
190228
}
191229

230+
/* -------------------- End Callbacks -------------------- */
231+
192232
Libp2pModulePlugin::Libp2pModulePlugin()
193233
: ctx(nullptr)
194234
{
235+
qRegisterMetaType<Libp2pPeerInfo>("Libp2pPeerInfo");
236+
qRegisterMetaType<QList<PeerInfo>>("QList<PeerInfo>");
237+
195238
std::memset(&config, 0, sizeof(config));
196239

197240
config.flags |= LIBP2P_CFG_GOSSIPSUB;
@@ -234,6 +277,32 @@ void Libp2pModulePlugin::initLogos(LogosAPI* logosAPIInstance) {
234277
logosAPI = logosAPIInstance;
235278
}
236279

280+
/* ---------------- Helper Functions ----------------- */
281+
282+
bool Libp2pModulePlugin::toCid(const QByteArray &key)
283+
{
284+
if (key.isEmpty())
285+
return {};
286+
287+
auto *callbackCtx = new CallbackContext{ "toCid", QUuid::createUuid().toString(), this };
288+
289+
int ret = libp2p_create_cid(
290+
1, // CIDv1
291+
"dag-pb",
292+
"sha2-256",
293+
reinterpret_cast<const uint8_t *>(key.constData()),
294+
size_t(key.size()),
295+
&Libp2pModulePlugin::libp2pCallback,
296+
callbackCtx
297+
);
298+
299+
if (ret != RET_OK) {
300+
delete callbackCtx;
301+
}
302+
303+
return ret == RET_OK;
304+
}
305+
237306
bool Libp2pModulePlugin::foo(const QString &bar)
238307
{
239308
qDebug() << "Libp2pModulePlugin::foo called with:" << bar;
@@ -253,6 +322,8 @@ bool Libp2pModulePlugin::foo(const QString &bar)
253322
return true;
254323
}
255324

325+
/* --------------- Start/stop --------------- */
326+
256327
bool Libp2pModulePlugin::libp2pStart()
257328
{
258329
qDebug() << "Libp2pModulePlugin::libp2pStart called";
@@ -291,6 +362,8 @@ bool Libp2pModulePlugin::libp2pStop()
291362
return ret == RET_OK;
292363
}
293364

365+
/* --------------- Kademlia --------------- */
366+
294367
bool Libp2pModulePlugin::findNode(const QString &peerId)
295368
{
296369
qDebug() << "Libp2pModulePlugin::findNode called:" << peerId;
@@ -392,20 +465,20 @@ bool Libp2pModulePlugin::addProvider(const QString &cid)
392465
return ret == RET_OK;
393466
}
394467

395-
bool Libp2pModulePlugin::startProviding(const QString &cid)
468+
bool Libp2pModulePlugin::getProviders(const QString &cid)
396469
{
397-
qDebug() << "Libp2pModulePlugin::startProviding called:" << cid;
470+
qDebug() << "Libp2pModulePlugin::getProviders called:" << cid;
398471
if (!ctx) {
399-
qDebug() << "startProviding called without a context";
472+
qDebug() << "getProviders called without a context";
400473
return false;
401474
}
402475

403-
auto *callbackCtx = new CallbackContext{ "startProviding", QUuid::createUuid().toString(), this };
476+
auto *callbackCtx = new CallbackContext{ "getProviders", QUuid::createUuid().toString(), this };
404477

405-
int ret = libp2p_start_providing(
478+
int ret = libp2p_get_providers(
406479
ctx,
407480
cid.toUtf8().constData(),
408-
&Libp2pModulePlugin::libp2pCallback,
481+
&Libp2pModulePlugin::getProvidersCallback,
409482
callbackCtx
410483
);
411484

@@ -416,17 +489,17 @@ bool Libp2pModulePlugin::startProviding(const QString &cid)
416489
return ret == RET_OK;
417490
}
418491

419-
bool Libp2pModulePlugin::stopProviding(const QString &cid)
492+
bool Libp2pModulePlugin::startProviding(const QString &cid)
420493
{
421-
qDebug() << "Libp2pModulePlugin::stopProviding called:" << cid;
494+
qDebug() << "Libp2pModulePlugin::startProviding called:" << cid;
422495
if (!ctx) {
423-
qDebug() << "stopProviding called without a context";
496+
qDebug() << "startProviding called without a context";
424497
return false;
425498
}
426499

427-
auto *callbackCtx = new CallbackContext{ "stopProviding", QUuid::createUuid().toString(), this };
500+
auto *callbackCtx = new CallbackContext{ "startProviding", QUuid::createUuid().toString(), this };
428501

429-
int ret = libp2p_stop_providing(
502+
int ret = libp2p_start_providing(
430503
ctx,
431504
cid.toUtf8().constData(),
432505
&Libp2pModulePlugin::libp2pCallback,
@@ -440,22 +513,20 @@ bool Libp2pModulePlugin::stopProviding(const QString &cid)
440513
return ret == RET_OK;
441514
}
442515

443-
bool Libp2pModulePlugin::getProviders(const QString &cid)
516+
bool Libp2pModulePlugin::stopProviding(const QString &cid)
444517
{
445-
qDebug() << "Libp2pModulePlugin::getProviders called:" << cid;
518+
qDebug() << "Libp2pModulePlugin::stopProviding called:" << cid;
446519
if (!ctx) {
447-
qDebug() << "getProviders called without a context";
520+
qDebug() << "stopProviding called without a context";
448521
return false;
449522
}
450523

451-
// TODO: return providers
452-
453-
auto *callbackCtx = new CallbackContext{ "getProviders", QUuid::createUuid().toString(), this };
524+
auto *callbackCtx = new CallbackContext{ "stopProviding", QUuid::createUuid().toString(), this };
454525

455-
int ret = libp2p_get_providers(
526+
int ret = libp2p_stop_providing(
456527
ctx,
457528
cid.toUtf8().constData(),
458-
&Libp2pModulePlugin::getProvidersCallback,
529+
&Libp2pModulePlugin::libp2pCallback,
459530
callbackCtx
460531
);
461532

src/libp2p_module_plugin.h

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ class Libp2pModulePlugin : public QObject, public Libp2pModuleInterface
2525
QString name() const override { return "libp2p_module"; }
2626
QString version() const override { return "1.0.0"; }
2727

28+
Q_INVOKABLE bool toCid(const QByteArray &key) override;
2829
Q_INVOKABLE bool foo(const QString &bar) override;
30+
2931
Q_INVOKABLE bool libp2pStart() override;
3032
Q_INVOKABLE bool libp2pStop() override;
3133

@@ -51,11 +53,6 @@ class Libp2pModulePlugin : public QObject, public Libp2pModuleInterface
5153
QString message,
5254
QVariant data
5355
);
54-
void getValueFinished(
55-
int result,
56-
QString reqId,
57-
QByteArray value
58-
);
5956
void eventResponse(const QString& eventName, const QVariantList& data);
6057

6158
private slots:

0 commit comments

Comments
 (0)