From 89f5d5c495607b73520acf7619ded6987b6b74da Mon Sep 17 00:00:00 2001 From: David McFadzean Date: Wed, 6 Mar 2024 15:28:04 -0500 Subject: [PATCH] Added mergeBatch --- gatekeeper-sdk.js | 10 ++++++++++ gatekeeper.js | 14 ++++++++++++++ hyperswarm-monitor.js | 38 +++++++++++++++++++++++++++++--------- server.js | 11 +++++++++++ 4 files changed, 64 insertions(+), 9 deletions(-) diff --git a/gatekeeper-sdk.js b/gatekeeper-sdk.js index 1cd8ee55..4a46725e 100644 --- a/gatekeeper-sdk.js +++ b/gatekeeper-sdk.js @@ -80,3 +80,13 @@ export async function importDID(txns) { throwError(error); } } + +export async function mergeBatch(batch) { + try { + const response = await axios.post(`${config.gatekeeperURL}/merge/`, batch); + return response.data; + } + catch (error) { + throwError(error); + } +} diff --git a/gatekeeper.js b/gatekeeper.js index e07dc8b5..dbafbd0e 100644 --- a/gatekeeper.js +++ b/gatekeeper.js @@ -460,3 +460,17 @@ export async function importDID(txns) { return diff; } + +export async function mergeBatch(batch) { + let merged = 0; + + for (const txns of batch) { + const diff = await importDID(txns); + + if (diff > 0) { + merged += 1; + } + } + + return merged; +} diff --git a/hyperswarm-monitor.js b/hyperswarm-monitor.js index d4ac4c44..10c0c4bc 100644 --- a/hyperswarm-monitor.js +++ b/hyperswarm-monitor.js @@ -96,21 +96,41 @@ async function mergeDb(db) { let dids = Object.keys(db.hyperswarm); dids.sort((a, b) => db.hyperswarm[a][0].time - db.hyperswarm[b][0].time); + let batch = []; for (const did of dids) { - console.log(`${did} ${db.hyperswarm[did][0].time}`); + console.log(`Adding to batch: ${did} ${db.hyperswarm[did][0].time}`); + batch.push(db.hyperswarm[did]); + + if (batch.length >= 25) { + try { + const imported = await gatekeeper.mergeBatch(batch); + if (imported > 0) { + console.log(`* imported ${imported} DIDs *`); + } + else { + console.log(`* DID synchronization confirmed *`); + } + } + catch (error) { + console.error(`error importing DID: ${did}: ${error}`); + } + + batch = []; + } } - for (const did of dids) { - try { - const imported = await gatekeeper.importDID(db.hyperswarm[did]); - if (imported > 0) { - console.log(`* imported DID ${did} *`); - } + try { + const imported = await gatekeeper.mergeBatch(batch); + if (imported > 0) { + console.log(`* imported ${imported} DIDs *`); } - catch (error) { - console.error(`error importing DID: ${did}: ${error}`); + else { + console.log(`* DID synchronization confirmed *`); } } + catch (error) { + console.error(`error importing DID: ${did}: ${error}`); + } } merging = false; } diff --git a/server.js b/server.js index dead060c..8856ae60 100644 --- a/server.js +++ b/server.js @@ -84,6 +84,17 @@ app.post('/import', async (req, res) => { } }); +app.post('/merge', async (req, res) => { + try { + const batch = req.body; + const did = await gatekeeper.mergeBatch(batch); + res.json(did); + } catch (error) { + console.error(error); + res.status(500).send(error.toString()); + } +}); + const port = 3000; app.listen(port, () => {