further adjustments

This commit is contained in:
Raven Scott
2025-12-17 22:39:20 -05:00
parent eb5675adb2
commit 5f022eae91
2 changed files with 83 additions and 19 deletions
+2 -1
View File
@@ -91,5 +91,6 @@ module.exports = {
reconnectionAttempts: new Map(), // Map<peerId, attempt count>
lastReconnectionAttempt: new Map(), // Map<peerId, timestamp>
// Invite diagnostics tracking
consecutiveInviteFailures: 0
consecutiveInviteFailures: 0,
networkConsensusTimer: null
};
+81 -18
View File
@@ -452,6 +452,22 @@ async function main() {
const channelManager = require('./includes/plugins/channel-manager');
const CORE_DOMAIN = 'p2ns.core';
// Function to send removal request to all peers
function sendRemovalRequest(domain) {
// Use channel-manager broadcast to send to all peers
const count = channelManager.broadcastToPeers(CORE_DOMAIN, 'request', `remove_domain:${domain}`);
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;
// Register invite channel for sending/receiving invites
channelManager.registerPluginChannel(CORE_DOMAIN, 'invite', {
encoding: 'string',
@@ -487,6 +503,15 @@ async function main() {
await listDomains();
// Perform initial auto-votes
doAutoVotes();
// Notify other peers to recalculate consensus once our autopass is ready
// This ensures our presence and any local claims/votes are recognized by the network
if (state.sendConsensusRequest) {
setTimeout(() => {
state.sendConsensusRequest('all');
}, 2000);
}
// Set up update listeners
setupListeners();
// Reset failure counter on successful invite processing
@@ -730,6 +755,14 @@ async function main() {
await listDomains();
// Perform initial auto-votes
doAutoVotes();
// Notify other peers to recalculate consensus once our autopass is ready
if (state.sendConsensusRequest) {
setTimeout(() => {
state.sendConsensusRequest('all');
}, 2000);
}
// Set up update listeners
setupListeners();
// Reset failure counter on successful invite processing
@@ -808,10 +841,15 @@ async function main() {
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');
const { invalidateEntriesCache, getAllEntries, autoVoteForDomain, doAutoVotes } = require('./includes/core/core');
invalidateEntriesCache();
const allEntries = await getAllEntries();
await autoVoteForDomain(domain, allEntries);
if (domain === 'all') {
await doAutoVotes();
} else {
const allEntries = await getAllEntries();
await autoVoteForDomain(domain, allEntries);
}
} else {
logWarn('Swarm', `Unknown message from ${peerId}: ${message}`);
}
@@ -1292,6 +1330,31 @@ async function main() {
}
// Store timer reference for cleanup
state.swarmKeepAliveTimer = swarmKeepAliveTimer;
// Set up periodic network-wide consensus recalculation
// This ensures that consensus remains consistent across the entire network
const networkConsensusInterval = parseInt(process.env.NETWORK_CONSENSUS_INTERVAL || '300000', 10); // Default 5 minutes
if (networkConsensusInterval > 0) {
const networkConsensusTimer = setInterval(() => {
if (isShuttingDown || !state.dnsPass) return;
try {
logInfo('Consensus', 'Triggering periodic network-wide consensus recalculation');
if (state.sendConsensusRequest) {
state.sendConsensusRequest('all');
}
// Also trigger locally
const { doAutoVotes } = require('./includes/core/core');
doAutoVotes().catch(err => {
logError('Consensus', `Error in periodic auto-votes: ${err.message}`);
});
} catch (err) {
logWarn('Consensus', `Periodic network consensus trigger failed: ${err.message}`);
}
}, networkConsensusInterval);
state.networkConsensusTimer = networkConsensusTimer;
logInfo('Main', `Network consensus interval enabled: ${networkConsensusInterval}ms`);
}
// Set up connection handler with timeout handling for better local network peer support
swarm.on('connection', async (conn, info) => {
@@ -1441,6 +1504,15 @@ async function main() {
doAutoVotes().catch(err => {
logError('Swarm', `Error in immediate auto-votes after peer connect: ${err.message}`);
});
// Notify other peers to recalculate consensus when a new node connects
// This ensures the new node's claims/votes are considered by the network
if (state.sendConsensusRequest) {
// Use a small delay to allow replication to begin before requesting consensus
setTimeout(() => {
state.sendConsensusRequest('all');
}, 2000);
}
}
// Start replication
@@ -2031,21 +2103,6 @@ async function main() {
swarm.on('error', (err) => {
logError('Swarm', `Swarm error: ${err.message}`);
});
// Function to send removal request to all peers
function sendRemovalRequest(domain) {
// Use channel-manager broadcast to send to all peers
const count = channelManager.broadcastToPeers(CORE_DOMAIN, 'request', `remove_domain:${domain}`);
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() {
@@ -2381,6 +2438,12 @@ async function main() {
clearTimeout(listenerRefs.autoVoteDebounce);
listenerRefs.autoVoteDebounce = null;
}
// Clear network consensus timer
if (state.networkConsensusTimer) {
clearInterval(state.networkConsensusTimer);
state.networkConsensusTimer = null;
logDebug('Main', 'Cleared network consensus timer');
}
// Clear swarm keep-alive timer
if (state.swarmKeepAliveTimer) {
clearInterval(state.swarmKeepAliveTimer);