This commit is contained in:
Raven Scott
2025-12-17 22:32:07 -05:00
parent ad4784d05d
commit eb5675adb2
6 changed files with 106 additions and 61 deletions
@@ -94,6 +94,11 @@ async function handleConsensusRoutes(req, res) {
const allEntries = await getAllEntries();
await autoVoteForDomain(domain, allEntries);
// Notify other peers to recalculate consensus for this domain
if (state.sendConsensusRequest) {
state.sendConsensusRequest(domain);
}
// Broadcast update
broadcast({ type: 'update-database' });
broadcast({ type: 'update-stats' });
+70 -58
View File
@@ -331,9 +331,12 @@ async function getConsensusState(domain) {
if (Object.keys(claims).length === 0) {
status = 'no_claims';
} else if (!quorumMet && totalVotes === 0) {
// No votes but have claims - check if single local claim
if (Object.keys(claims).length === 1 && claims.hasOwnProperty(localWriter)) {
} else if (!quorumMet) {
// IMPROVED: Allow resolution for a single local claim even if we've already auto-voted
const isSingleLocalClaim = Object.keys(claims).length === 1 && claims.hasOwnProperty(localWriter);
const onlyWeVoted = totalVotes === 0 || (totalVotes === 1 && voters.has(localWriter));
if (isSingleLocalClaim && (totalVotes === 0 || onlyWeVoted)) {
status = 'resolved';
resolvedClaimant = localWriter;
hash = claims[localWriter];
@@ -350,16 +353,9 @@ async function getConsensusState(domain) {
countedQuorumFailures.add(domain);
consensusMetrics.quorumFailures++;
}
}
} else if (!quorumMet) {
status = 'insufficient_quorum';
// Count quorum failure once per domain
if (!countedQuorumFailures.has(domain)) {
countedQuorumFailures.add(domain);
consensusMetrics.quorumFailures++;
}
if (trackConsensusEvent) {
trackConsensusEvent('quorum_failure', { domain, totalVotes, minVotes, activePeers });
if (trackConsensusEvent && totalVotes > 0) {
trackConsensusEvent('quorum_failure', { domain, totalVotes, minVotes, activePeers });
}
}
} else {
const maxVotes = Math.max(...Object.values(voteCounts));
@@ -498,29 +494,29 @@ async function doAutoVotes() {
}
}
async function autoVoteForDomain(domain, allEntries) {
async function voteForDomain(domain, claimant, allEntries = null) {
const pass = state.dnsPass;
if (!pass) return;
if (!pass) return false;
logDebug('Core', `Auto-voting for domain: ${domain}`);
const config = getConsensusConfig();
const claims = {};
const claimTimestamps = {};
const myVotes = new Map(); // claimant -> voteKey
const localWriter = getPersistentPublicKey();
if (!localWriter) {
logWarn('Core', 'Cannot auto-vote: persistent public key not available');
return;
logWarn('Core', 'Cannot vote: persistent public key not available');
return false;
}
if (!allEntries) {
allEntries = await getAllEntries(pass, true);
}
const claims = {};
const myVotes = new Map(); // claimant -> voteKey
// Collect claims and timestamps
// Collect claims and our current votes
for (const entry of allEntries) {
if (entry.key.startsWith(`claim:${domain}:`)) {
const claimant = entry.key.slice(`claim:${domain}:`.length);
const entryClaimant = entry.key.slice(`claim:${domain}:`.length);
const parsed = parseClaimValue(entry.value);
claims[claimant] = parsed.hash;
claimTimestamps[claimant] = parsed.timestamp;
logDebug('Core', `Processing claim for ${domain}: claimant=${claimant}, hash=${parsed.hash}, timestamp=${parsed.timestamp}`);
claims[entryClaimant] = parsed.hash;
}
if (entry.key.startsWith(`vote:${domain}:`)) {
const parts = entry.key.split(':');
@@ -529,53 +525,68 @@ async function autoVoteForDomain(domain, allEntries) {
}
}
}
if (!claims.hasOwnProperty(claimant)) {
logWarn('Core', `Cannot vote for claimant ${claimant}: no claim found for domain ${domain}`);
return false;
}
// Validate vote
if (!validateVote(claimant, claims)) {
return false;
}
// Remove existing votes for other claimants
for (const [votedClaimant, voteKey] of myVotes) {
if (votedClaimant !== claimant) {
await pass.remove(voteKey);
logInfo('Core', `Removed vote for ${domain} claimant ${votedClaimant}`);
}
}
// Add new vote if not already voting for this claimant
if (!myVotes.has(claimant)) {
const voteKey = `vote:${domain}:${claimant}:${localWriter}`;
await pass.add(voteKey, claims[claimant]);
logInfo('Core', `Voted for ${domain} claimant ${claimant}`);
invalidateEntriesCache();
}
return true;
}
async function autoVoteForDomain(domain, allEntries) {
const localWriter = getPersistentPublicKey();
if (!localWriter) return;
const claims = {};
const claimTimestamps = {};
for (const entry of allEntries) {
if (entry.key.startsWith(`claim:${domain}:`)) {
const claimant = entry.key.slice(`claim:${domain}:`.length);
const parsed = parseClaimValue(entry.value);
claims[claimant] = parsed.hash;
claimTimestamps[claimant] = parsed.timestamp;
}
}
const claimsLength = Object.keys(claims).length;
logDebug('Core', `Found ${claimsLength} claims for ${domain}`);
if (claimsLength === 0) return;
// Get consensus state to inform voting decision
const consensusState = await getConsensusState(domain);
// Determine intended claimant
const localHasClaim = claims.hasOwnProperty(localWriter);
let intendedClaimant;
if (localHasClaim) {
intendedClaimant = localWriter;
logDebug('Core', `Voting for own claim: ${intendedClaimant}`);
} else if (claimsLength === 1) {
intendedClaimant = Object.keys(claims)[0];
logDebug('Core', `Voting for single claim: ${intendedClaimant}`);
} else {
// Multiple claims - use tie-breaker strategy
const candidates = Object.keys(claims);
intendedClaimant = applyTieBreaker(candidates, claims, claimTimestamps, localWriter);
logDebug('Core', `Voting for claimant selected by tie-breaker: ${intendedClaimant}`);
}
// Validate intended claimant exists
if (!validateVote(intendedClaimant, claims)) {
logWarn('Core', `Cannot vote for invalid claimant: ${intendedClaimant}`);
return;
}
// Remove votes for other claimants
for (const [votedClaimant, voteKey] of myVotes) {
if (votedClaimant !== intendedClaimant) {
await pass.remove(voteKey);
logInfo('Core', `Removed vote for ${domain} claimant ${votedClaimant}`);
}
}
// Add vote if not already voting for intended claimant
if (!myVotes.has(intendedClaimant)) {
const voteKey = `vote:${domain}:${intendedClaimant}:${localWriter}`;
await pass.add(voteKey, claims[intendedClaimant]);
logInfo('Core', `Auto-voted for ${domain} claimant ${intendedClaimant} (reason: ${localHasClaim ? 'own_claim' : claimsLength === 1 ? 'single_claim' : 'tie_breaker'})`);
} else {
logDebug('Core', `Already voted for ${domain} claimant ${intendedClaimant}`);
}
await voteForDomain(domain, intendedClaimant, allEntries);
}
async function removeDomain(domain) {
@@ -699,6 +710,7 @@ module.exports = {
doAutoVotes,
getAllEntries,
autoVoteForDomain,
voteForDomain,
removeDomain,
removeAllRecords,
invalidateEntriesCache,
+5
View File
@@ -45,6 +45,11 @@ async function addDomain(domain, hash, ssl = false) {
if (config.CONSENSUS_IMMEDIATE_UPDATE !== false) {
const allEntries = await getAllEntries();
await autoVoteForDomain(domain, allEntries);
// Notify other peers to recalculate consensus for this domain
if (state.sendConsensusRequest) {
state.sendConsensusRequest(domain);
}
}
}
function addInternalDomain(domain, ip = '127.0.0.1') {
+1
View File
@@ -70,6 +70,7 @@ module.exports = {
localDnsRecords: [],
isMaster: process.argv.includes('--master'),
sendRemovalRequest: null,
sendConsensusRequest: null,
domainsWithBoth: new Set(),
publicIpForDomain: {},
caForDomain: new Map(),
+9 -3
View File
@@ -21,6 +21,7 @@ const {
removeDomain,
doAutoVotes,
autoVoteForDomain,
voteForDomain,
invalidateEntriesCache
} = require('../core/core');
const { atomicDomainCleanup } = require('../core/domain_cleanup');
@@ -685,9 +686,14 @@ const sdk = {
if (!state.dnsPass) {
throw new Error('DNS service not initialized');
}
const allEntries = await getAllEntries();
await autoVoteForDomain(domain, allEntries);
// Check if vote was successful
const success = await voteForDomain(domain, claimant);
if (!success) return false;
// Invalidate cache to ensure subsequent state check is fresh
invalidateEntriesCache();
// Check if vote resulted in expected resolution
const consensusState = await getConsensusState(domain);
return consensusState.resolvedClaimant === claimant;
},
+16
View File
@@ -804,6 +804,14 @@ async function main() {
const domain = message.slice(14);
logDebug('Swarm', `Processing removal request for domain: ${domain}`);
await removeDomain(domain);
} else if (message.startsWith('recalculate_consensus:')) {
const domain = message.slice(22);
logDebug('Swarm', `Processing consensus recalculation request for domain: ${domain}`);
// Invalidate cache first to ensure we get the latest replicated data
const { invalidateEntriesCache, getAllEntries, autoVoteForDomain } = require('./includes/core/core');
invalidateEntriesCache();
const allEntries = await getAllEntries();
await autoVoteForDomain(domain, allEntries);
} else {
logWarn('Swarm', `Unknown message from ${peerId}: ${message}`);
}
@@ -2030,6 +2038,14 @@ async function main() {
logDebug('Main', `Sent removal request for ${domain} to ${count} peers`);
}
state.sendRemovalRequest = sendRemovalRequest;
// Function to send consensus recalculation request to all peers
function sendConsensusRequest(domain) {
// Use channel-manager broadcast to send to all peers
const count = channelManager.broadcastToPeers(CORE_DOMAIN, 'request', `recalculate_consensus:${domain}`);
logDebug('Main', `Sent consensus recalculation request for ${domain} to ${count} peers`);
}
state.sendConsensusRequest = sendConsensusRequest;
// Function to auto-subscribe to services on bootup
async function autoSubscribeToServices() {