Skip to content

Commit

Permalink
Added mergeBatch
Browse files Browse the repository at this point in the history
  • Loading branch information
macterra committed Mar 6, 2024
1 parent d546ecd commit 89f5d5c
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 9 deletions.
10 changes: 10 additions & 0 deletions gatekeeper-sdk.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
14 changes: 14 additions & 0 deletions gatekeeper.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
38 changes: 29 additions & 9 deletions hyperswarm-monitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
11 changes: 11 additions & 0 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, () => {
Expand Down

0 comments on commit 89f5d5c

Please sign in to comment.