Remove invite cache

This commit is contained in:
Raven Scott
2026-05-27 20:46:41 -04:00
parent bcdc912b77
commit 7cd98231f7
+19 -61
View File
@@ -338,7 +338,6 @@ async function main() {
let processingInvite = false;
let currentInvitePromise = null;
let currentPairOperation = null; // Track active pairing operation for cleanup
let cachedMasterInviteWire = null;
// Helper function to safely get dnsPass and ensure state consistency
function getDnsPass() {
@@ -384,26 +383,6 @@ async function main() {
}
}
async function prepareMasterInvite(reason = 'startup') {
if (!isMaster) return null;
const pass = getDnsPass();
if (!pass) return null;
if (cachedMasterInviteWire) return cachedMasterInviteWire;
const { createInvite, inviteToWire, invitePreview } = require('./includes/core/dns-pass-queue');
logInfo('Swarm', `Preparing cached Autopass invite (${reason})...`);
const inv = await createInvite(pass);
cachedMasterInviteWire = inviteToWire(inv);
logInfo('Swarm', `Cached Autopass invite ready (${invitePreview(inv)})`);
return cachedMasterInviteWire;
}
function clearCachedMasterInvite(reason = 'unknown') {
if (!cachedMasterInviteWire) return;
cachedMasterInviteWire = null;
logDebug('Swarm', `Cleared cached Autopass invite (${reason})`);
}
// Helper functions for invite processing mutex
function isProcessingInvite() {
return processingInvite;
@@ -475,10 +454,10 @@ async function main() {
logInfo('Swarm', `Processing pending invite request from ${peerId} (queued for ${Math.round(age/1000)}s)`);
try {
const invWire = await prepareMasterInvite(`pending request from ${peerId.substring(0, 16)}...`);
if (!invWire) {
throw new Error('cached master invite unavailable');
}
const { createInvite, inviteToWire, invitePreview } = require('./includes/core/dns-pass-queue');
const inv = await createInvite(state.dnsPass);
const invWire = inviteToWire(inv);
logInfo('Swarm', `Created invite for pending request from ${peerId}: ${invitePreview(inv)}`);
const sent = channelManager.sendToPeer(CORE_DOMAIN, 'invite', peerId, invWire);
if (sent) {
logInfo('Swarm', `Successfully sent pending invite to ${peerId}`);
@@ -802,14 +781,6 @@ async function main() {
// Handle invite acknowledgment from joiner
if (message === 'invite_ack') {
logInfo('Swarm', `Received invite_ack from peer ${peerId}`);
clearCachedMasterInvite(`ack from ${peerId.substring(0, 16)}...`);
if (isMaster) {
setTimeout(() => {
prepareMasterInvite('post-ack refresh').catch((err) => {
logWarn('Swarm', `Could not refresh cached invite after ack: ${err.message}`);
});
}, 3000);
}
// Clear pending ack timeout for this peer
const pending = pendingInviteAcks.get(peerId);
if (pending) {
@@ -901,7 +872,7 @@ async function main() {
channelManager.sendToPeer(CORE_DOMAIN, 'request', peerId, 'invite_unavailable:creation_failed');
return;
}
logInfo('Swarm', `Sending cached Autopass invite to peer ${peerId}...`);
logInfo('Swarm', `Creating Autopass invite for peer ${peerId}...`);
const inviteChannelInfo = channelManager.getChannelInfo(CORE_DOMAIN, 'invite');
const invitePeerChannel = inviteChannelInfo?.peerChannels?.get(peerId);
if (invitePeerChannel) {
@@ -910,11 +881,10 @@ async function main() {
logWarn('Swarm', `Invite channel not fully open for ${peerId}, sending invite anyway (protomux may queue)`);
}
}
const invWire = await prepareMasterInvite(`request from ${peerId.substring(0, 16)}...`);
if (!invWire) {
throw new Error('cached master invite unavailable');
}
logInfo('Swarm', `Cached invite ready for requesting peer ${peerId}`);
const { createInvite, inviteToWire, invitePreview } = require('./includes/core/dns-pass-queue');
const inv = await createInvite(pass);
const invWire = inviteToWire(inv);
logInfo('Swarm', `Created invite for requesting peer ${peerId}: ${invitePreview(inv)}`);
try {
const sent = channelManager.sendToPeer(CORE_DOMAIN, 'invite', peerId, invWire);
if (!sent) {
@@ -952,15 +922,14 @@ async function main() {
setTimeout(async () => {
const retryPass = getDnsPass();
const { isDnsPassUsable } = require('./includes/core/core');
const { whenDnsPassIdle, ensureDnsPassOpen } = require('./includes/core/dns-pass-queue');
const { createInvite, inviteToWire, whenDnsPassIdle, ensureDnsPassOpen } = require('./includes/core/dns-pass-queue');
if (!connectedPeers.has(peerId) || !retryPass) return;
await whenDnsPassIdle();
await ensureDnsPassOpen(retryPass);
if (!isDnsPassUsable(retryPass)) return;
try {
const invWire = await prepareMasterInvite(`retry for ${peerId.substring(0, 16)}...`);
if (!invWire) return;
channelManager.sendToPeer(CORE_DOMAIN, 'invite', peerId, invWire);
const inv = await createInvite(retryPass);
channelManager.sendToPeer(CORE_DOMAIN, 'invite', peerId, inviteToWire(inv));
logInfo('Swarm', `Sent invite to ${peerId} after dnsPass busy retry`);
} catch (retryErr) {
logWarn('Swarm', `Invite retry failed for ${peerId}: ${retryErr.message}`);
@@ -996,14 +965,10 @@ async function main() {
if (pass && isDnsPassUsable(pass) && (isMaster || process.env.ALLOW_ANY_WRITER_INVITES === 'true')) {
// We can create an invite - send it back through the relay chain
try {
const { whenDnsPassIdle } = require('./includes/core/dns-pass-queue');
const { createInvite, inviteToWire, whenDnsPassIdle } = require('./includes/core/dns-pass-queue');
await whenDnsPassIdle();
const invWire = isMaster
? await prepareMasterInvite(`relay for ${originPeerId.substring(0, 16)}...`)
: null;
if (!invWire) {
throw new Error('cached relay invite unavailable');
}
const inv = await createInvite(pass);
const invWire = inviteToWire(inv);
logInfo('Swarm', `Created relay invite for origin ${originPeerId.substring(0, 16)}...`);
channelManager.sendToPeer(CORE_DOMAIN, 'request', peerId, `relay_invite_response:${originPeerId}:${invWire}`);
} catch (err) {
@@ -1346,11 +1311,10 @@ async function main() {
pendingInviteAcks.delete(peerId);
return;
}
const invWire = await prepareMasterInvite(`proactive send to ${peerId.substring(0, 16)}...`);
if (!invWire) {
throw new Error('cached master invite unavailable');
}
logInfo('Swarm', `Using cached proactive invite for peer ${peerId} (attempt ${retryCount}/${maxRetries})`);
const { createInvite, inviteToWire, invitePreview } = require('./includes/core/dns-pass-queue');
const inv = await createInvite(pass);
const invWire = inviteToWire(inv);
logInfo('Swarm', `Created proactive invite for peer ${peerId} (attempt ${retryCount}/${maxRetries}): ${invitePreview(inv)}`);
const success = channelManager.sendToPeer(CORE_DOMAIN, 'invite', peerId, invWire);
if (success) {
@@ -1793,12 +1757,6 @@ async function main() {
}
logInfo('Main', `Core retrieved. Writable: ${core.writable}, Key: ${core.key.toString('hex')}`);
try {
await prepareMasterInvite('master startup before p2ns swarm join');
} catch (err) {
logError('Main', `Failed to prepare initial Autopass invite before swarm join: ${err.message}`);
logError('Main', 'Restart with --clean --master if this persists; invite requests will fail until an invite can be cached.');
}
// Setup domains watcher for master
setupDomainsWatcher();
setupListeners();