From 352e3651417d229fe35e196f929004aa964ed264 Mon Sep 17 00:00:00 2001 From: David McFadzean Date: Tue, 17 Dec 2024 10:55:14 -0500 Subject: [PATCH] Test and fix for delayed confirm on hyperswarm --- .../hyperswarm/src/hyperswarm-mediator.js | 2 +- tests/hypr-confirm.js | 58 +++++++++++++++++++ 2 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 tests/hypr-confirm.js diff --git a/services/mediators/hyperswarm/src/hyperswarm-mediator.js b/services/mediators/hyperswarm/src/hyperswarm-mediator.js index ca2d3371..5c4125c7 100644 --- a/services/mediators/hyperswarm/src/hyperswarm-mediator.js +++ b/services/mediators/hyperswarm/src/hyperswarm-mediator.js @@ -363,7 +363,7 @@ async function flushQueue() { await gatekeeper.clearQueue(REGISTRY, batch); await relayMsg(msg); - await importBatch(batch); + await mergeBatch(batch); } } diff --git a/tests/hypr-confirm.js b/tests/hypr-confirm.js new file mode 100644 index 00000000..15006311 --- /dev/null +++ b/tests/hypr-confirm.js @@ -0,0 +1,58 @@ +import * as keymaster from '@mdip/keymaster/lib'; +import * as wallet from '@mdip/keymaster/db/json'; +import * as gatekeeper from '@mdip/gatekeeper/sdk'; +import * as cipher from '@mdip/cipher/node'; + +async function runTest() { + const expires = new Date(); + expires.setMinutes(expires.getMinutes() + 1); + const testOptions = { registry: 'hyperswarm', validUntil: expires.toISOString() }; + + const alice = await keymaster.createId('Alice', testOptions); + console.log(`alice: ${alice}`); + + const asset = await keymaster.createAsset({ version: 1 }, testOptions); + const doc1 = await keymaster.resolveDID(asset); + console.log(JSON.stringify(doc1, null, 4)); + + const ok = await keymaster.updateAsset(asset, { version: 2 }); + let doc2 = await keymaster.resolveDID(asset); + console.log(JSON.stringify(doc2, null, 4)); + + while (doc2.didDocumentMetadata.confirmed == false) { + // wait for 1 second before checking again + await new Promise(resolve => setTimeout(resolve, 1000)); + + doc2 = await keymaster.resolveDID(asset); + console.log(JSON.stringify(doc2, null, 4)); + } +} + +async function main() { + await gatekeeper.start({ + url: 'http://localhost:4224', + waitUntilReady: true, + }); + + await keymaster.start({ + gatekeeper, + wallet, + cipher, + }); + + const backup = await keymaster.loadWallet(); + await keymaster.newWallet(null, true); + + try { + await runTest(); + } + catch (error) { + console.log(error); + } + + await keymaster.saveWallet(backup); + await keymaster.stop(); + process.exit(); +} + +main();