diff --git a/packages/gatekeeper/src/gatekeeper-lib.js b/packages/gatekeeper/src/gatekeeper-lib.js index 2a19c6a0..f2471f34 100644 --- a/packages/gatekeeper/src/gatekeeper-lib.js +++ b/packages/gatekeeper/src/gatekeeper-lib.js @@ -125,9 +125,12 @@ export async function checkDIDs(options = {}) { const total = dids.length; let n = 0; + let confirmed = 0; + let unconfirmed = 0; let ephemeral = 0; let invalid = 0; const byRegistry = {}; + const byVersion = {}; for (const did of dids) { n += 1; @@ -137,16 +140,22 @@ export async function checkDIDs(options = {}) { console.log(`resolved ${n}/${total} ${did} OK`); } + if (doc.didDocumentMetadata.confirmed) { + confirmed += 1; + } + else { + unconfirmed += 1; + } + if (doc.mdip.validUntil) { ephemeral += 1; } - if (byRegistry[doc.mdip.registry]) { - byRegistry[doc.mdip.registry] += 1; - } - else { - byRegistry[doc.mdip.registry] = 1; - } + const registry = doc.mdip.registry; + byRegistry[registry] = (byRegistry[registry] || 0) + 1; + + const version = doc.didDocumentMetadata.version; + byVersion[version] = (byVersion[version] || 0) + 1; } catch (error) { invalid += 1; @@ -156,7 +165,7 @@ export async function checkDIDs(options = {}) { } } - return { total, ephemeral, invalid, byRegistry }; + return { total, confirmed, unconfirmed, ephemeral, invalid, byRegistry, byVersion }; } export async function initRegistries(csvRegistries) { diff --git a/services/gatekeeper/server/src/gatekeeper-api.js b/services/gatekeeper/server/src/gatekeeper-api.js index e5e3a359..f57da561 100644 --- a/services/gatekeeper/server/src/gatekeeper-api.js +++ b/services/gatekeeper/server/src/gatekeeper-api.js @@ -262,6 +262,7 @@ async function gcLoop() { try { const response = await gatekeeper.verifyDb(); console.log(`DID garbage collection: ${JSON.stringify(response)} waiting ${config.gcInterval} minutes...`); + await checkDids(); } catch (error) { console.error(`Error in DID garbage collection: ${error}`); @@ -291,16 +292,30 @@ async function reportStatus() { console.log('Status -----------------------------'); - console.log('DID Database:'); + console.log(`DID Database (${config.db}):`); console.log(` Total: ${status.dids.total}`); + console.log(` By registry:`); - for (let registry in status.dids.byRegistry) { + const registries = Object.keys(status.dids.byRegistry).sort(); + for (let registry of registries) { console.log(` ${registry}: ${status.dids.byRegistry[registry]}`); } + + console.log(` By version:`); + let count = 0; + for (let version of [1, 2, 3, 4, 5]) { + const num = status.dids.byVersion[version]; + console.log(` ${version}: ${num}`); + count += num; + } + console.log(` 6+: ${status.dids.total - count}`); + + console.log(` Confirmed: ${status.dids.confirmed}`); + console.log(` Unconfirmed: ${status.dids.unconfirmed}`); console.log(` Ephemeral: ${status.dids.ephemeral}`); console.log(` Invalid: ${status.dids.invalid}`); - console.log('Memory Usage Report:'); + console.log(`Memory Usage Report:`); console.log(` RSS: ${formatBytes(status.memoryUsage.rss)} (Resident Set Size - total memory allocated for the process)`); console.log(` Heap Total: ${formatBytes(status.memoryUsage.heapTotal)} (Total heap allocated)`); console.log(` Heap Used: ${formatBytes(status.memoryUsage.heapUsed)} (Heap actually used)`); diff --git a/tests/gatekeeper.test.js b/tests/gatekeeper.test.js index bcf6e434..75abf531 100644 --- a/tests/gatekeeper.test.js +++ b/tests/gatekeeper.test.js @@ -2613,7 +2613,7 @@ describe('checkDIDs', () => { mockFs.restore(); }); - it('should check all DIDs in db', async () => { + it('should check all DIDs', async () => { mockFs({}); const keypair = cipher.generateRandomJwk(); @@ -2625,8 +2625,37 @@ describe('checkDIDs', () => { const check = await gatekeeper.checkDIDs({ chatty: true }); expect(check.total).toBe(2); + expect(check.confirmed).toBe(2); expect(check.ephemeral).toBe(1); expect(check.invalid).toBe(0); + expect(check.byRegistry['local']).toBe(2); + expect(check.byVersion[1]).toBe(2); + }); + + it('should report unconfirmed DIDs', async () => { + mockFs({}); + + const keypair = cipher.generateRandomJwk(); + const agentOp = await createAgentOp(keypair, 1, 'hyperswarm'); + const agentDID = await gatekeeper.createDID(agentOp); + const assetOp = await createAssetOp(agentDID, keypair, 'hyperswarm'); + const assetDID = await gatekeeper.createDID(assetOp); + const doc = await gatekeeper.resolveDID(assetDID); + doc.didDocumentData = { mock: 1 }; + const updateOp = await createUpdateOp(keypair, assetDID, doc); + const ok = await gatekeeper.updateDID(updateOp); + + const check = await gatekeeper.checkDIDs({ chatty: true }); + + expect(ok).toBe(true); + expect(check.total).toBe(2); + expect(check.confirmed).toBe(1); + expect(check.unconfirmed).toBe(1); + expect(check.ephemeral).toBe(0); + expect(check.invalid).toBe(0); + expect(check.byRegistry['hyperswarm']).toBe(2); + expect(check.byVersion[1]).toBe(1); + expect(check.byVersion[2]).toBe(1); }); it('should report invalid DIDs', async () => {