For N domains, admin/domain.consensus went from roughly 2N consensus reads + up to N full ledger scans down to 1 base.update() + N cached view lookups (plus one entries scan for the domain list itself).

This commit is contained in:
Raven Scott
2026-05-31 00:53:26 -04:00
parent ebee5e5bb7
commit 3f49c2c500
8 changed files with 263 additions and 239 deletions
+4 -58
View File
@@ -2,7 +2,8 @@ const fs = require('fs');
const fsp = require('fs').promises;
const path = require('path');
const state = require('../../../infrastructure/state');
const { getAllEntries, getHashForDomain, doAutoVotes, getConsensusState, invalidateEntriesCache, removeOwnClaimAndVotes } = require('../../../core/core');
const { doAutoVotes, getConsensusState, invalidateEntriesCache, removeOwnClaimAndVotes, getAllEntries } = require('../../../core/core');
const { buildResolvedDomainsList } = require('../../../core/resolved-domains-list');
const { addDomain } = require('../../../core/domains');
const { validateDomainAddition, validateDomainRemoval } = require('../../../infrastructure/validation');
const { atomicDomainCleanup } = require('../../../core/domain_cleanup');
@@ -23,64 +24,9 @@ async function handleDomainsRoutes(req, res) {
if (method === 'GET' && urlPath === '/api/resolved-domains') {
try {
// Use fresh data (bypass cache) to ensure latest consensus state
const allEntries = await getAllEntries(state.dnsPass, false);
const domainClaimants = new Map();
for (const entry of allEntries) {
if (entry.key.startsWith('claim:')) {
const parts = entry.key.split(':');
if (parts.length === 3) {
const domain = parts[1];
const claimant = parts[2];
if (!domainClaimants.has(domain)) domainClaimants.set(domain, new Set());
domainClaimants.get(domain).add(claimant);
}
}
}
const localWriter = getPersistentPublicKey();
const domains = new Set(domainClaimants.keys());
const resolved = [];
for (const domain of domains) {
const hash = await getHashForDomain(domain) || 'none';
const isLocal = localWriter ? domainClaimants.get(domain)?.has(localWriter) || false : false;
let isOwner = false;
let consensusState = null;
try {
consensusState = await getConsensusState(domain);
isOwner = localWriter ? consensusState.resolvedClaimant === localWriter : false;
} catch (err) {
logDebug('Admin', `Error checking ownership/consensus for ${domain}: ${err.message}`);
}
// Determine consensus status, including conflict detection
let consensusStatus = null;
if (consensusState) {
if (isLocal && !isOwner && consensusState.status === 'resolved') {
// Conflict: user has local claim but another claimant won
consensusStatus = 'conflict';
} else {
consensusStatus = consensusState.status;
}
}
resolved.push({ domain, hash, isLocal, isOwner, consensusState, consensusStatus });
}
let internalDomains = ['p2ns.admin'];
try {
const { getInternalDomains } = require('../../../plugins/plugin-handler');
internalDomains = await getInternalDomains();
} catch (err) {
// Fallback if plugin system not available
}
// Only add internal domains that aren't already in the resolved list
for (const d of internalDomains) {
if (!resolved.some(r => r.domain === d)) {
resolved.push({ domain: d, hash: 'internal', isLocal: true, isOwner: true, consensusStatus: 'internal' });
}
}
const resolved = await buildResolvedDomainsList({ useFreshEntries: true });
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify(resolved.sort((a, b) => a.domain.localeCompare(b.domain))));
res.end(JSON.stringify(resolved));
} catch (err) {
logError('Admin', `Failed to fetch resolved domains: ${err.message}`);
res.writeHead(500);