Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added mergeBatch #13

Merged
merged 1 commit into from
Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading