Skip to content

Commit

Permalink
Added importDID profiling
Browse files Browse the repository at this point in the history
  • Loading branch information
macterra committed Mar 4, 2024
1 parent 2be706c commit 15bedea
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 4 deletions.
103 changes: 101 additions & 2 deletions gatekeeper.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { json } from '@helia/json';
import { CID } from 'multiformats/cid';
import { base58btc } from 'multiformats/bases/base58';
import fs from 'fs';
import canonicalize from 'canonicalize';
import { createHelia } from 'helia';
import { FsBlockstore } from 'blockstore-fs';
import { performance } from 'perf_hooks';

import * as cipher from './cipher.js';

const dataFolder = 'data';
Expand Down Expand Up @@ -46,7 +47,7 @@ export async function stop() {
helia.stop();
}

function submitTxn(did, registry, txn, time, ordinal=0) {
function submitTxn(did, registry, txn, time, ordinal = 0) {
const db = loadDb();

const update = {
Expand All @@ -70,19 +71,52 @@ function submitTxn(did, registry, txn, time, ordinal=0) {
writeDb(db);
}

export async function anchorSeedold(seed) {
const cid = await ipfs.add(JSON.parse(canonicalize(seed)));
const did = `did:mdip:${cid.toString(base58btc)}`;
const db = loadDb();

if (!db.anchors) {
db.anchors = {};
}

const anchor = await ipfs.get(cid);
db.anchors[did] = anchor;

writeDb(db);
return did;
}

export async function anchorSeed(seed) {
let t0, t1;

t0 = performance.now();
const cid = await ipfs.add(JSON.parse(canonicalize(seed)));
t1 = performance.now();
console.log("Adding seed to IPFS took " + (t1 - t0) + " milliseconds.");

const did = `did:mdip:${cid.toString(base58btc)}`;

t0 = performance.now();
const db = loadDb();
t1 = performance.now();
console.log("Loading DB took " + (t1 - t0) + " milliseconds.");

if (!db.anchors) {
db.anchors = {};
}

t0 = performance.now();
const anchor = await ipfs.get(cid);
db.anchors[did] = anchor;
t1 = performance.now();
console.log("Getting anchor from IPFS and updating DB took " + (t1 - t0) + " milliseconds.");

t0 = performance.now();
writeDb(db);
t1 = performance.now();
console.log("Writing DB took " + (t1 - t0) + " milliseconds.");

return did;
}

Expand Down Expand Up @@ -406,16 +440,72 @@ export async function exportDID(did) {
return fetchUpdates(registry, did);
}

export async function importDIDold(txns) {

if (!txns || !Array.isArray(txns) || txns.length < 1) {
throw "Invalid import";
}

const create = txns[0];
const did = create.did;
const current = await exportDID(did);

if (current.length === 0) {
const check = await createDID(create.txn);

if (did !== check) {
throw "Invalid import";
}
}
else {
if (create.txn.signature.value !== current[0].txn.signature.value) {
throw "Invalid import";
}
}

for (let i = 1; i < txns.length; i++) {
if (i < current.length) {
// Verify previous update txns
if (txns[i].txn.signature.value !== current[i].txn.signature.value) {
throw "Invalid import";
}
}
else {
// Add new updates
const ok = await updateDID(txns[i].txn);

if (!ok) {
throw "Invalid import";
}
}
}

const after = await exportDID(did);
const diff = after.length - current.length;

return diff;
}


export async function importDID(txns) {
let t0, t1;

t0 = performance.now();
if (!txns || !Array.isArray(txns) || txns.length < 1) {
throw "Invalid import";
}
t1 = performance.now();
console.log("Checking transaction validity took " + (t1 - t0) + " milliseconds.");

const create = txns[0];
const did = create.did;

t0 = performance.now();
const current = await exportDID(did);
t1 = performance.now();
console.log("Exporting DID took " + (t1 - t0) + " milliseconds.");

t0 = performance.now();
if (current.length === 0) {
const check = await createDID(create.txn);

Expand All @@ -428,8 +518,11 @@ export async function importDID(txns) {
throw "Invalid import";
}
}
t1 = performance.now();
console.log("Creating or checking DID took " + (t1 - t0) + " milliseconds.");

for (let i = 1; i < txns.length; i++) {
t0 = performance.now();
if (i < current.length) {
// Verify previous update txns
if (txns[i].txn.signature.value !== current[i].txn.signature.value) {
Expand All @@ -444,9 +537,15 @@ export async function importDID(txns) {
throw "Invalid import";
}
}
t1 = performance.now();
console.log("Verifying or updating DID took " + (t1 - t0) + " milliseconds.");
}

t0 = performance.now();
const after = await exportDID(did);
t1 = performance.now();
console.log("Exporting DID after updates took " + (t1 - t0) + " milliseconds.");

const diff = after.length - current.length;

return diff;
Expand Down
8 changes: 7 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
"hyperswarm": "^4.7.14",
"json-schema-faker": "^0.5.5",
"morgan": "^1.10.0",
"multiformats": "^13.0.0"
"multiformats": "^13.0.0",
"perf_hooks": "^0.0.1"
},
"devDependencies": {
"jest": "^29.7.0",
Expand Down

0 comments on commit 15bedea

Please sign in to comment.