Hub gaps (Marty 2026-11-13): deliberate sponsor changes -> /v1/sponsor-change, BTC4Hits accounts reported (no other door does), MemberActivated -> /v1/contract-bind (contract linkspin) from a retried queue; still one way, only with HUB_ENABLED

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
martbost
2026-11-13 08:21:59 -06:00
parent 33285ad590
commit a7f9b80c18
2 changed files with 40 additions and 4 deletions
+38 -2
View File
@@ -10,6 +10,10 @@
// wallet.connected: fills an empty wallet here, same as the registry sync. Every other event is acknowledged and ignored.
// OUT accounts.onChange -> touch(email): a persisted outbox (DATA_DIR/hubsync.json) reports LinkSpin's own joins
// (/v1/join, sponsor named by email) and wallet links (/v1/wallet). Never blocks sign-up; failures retry with backoff.
// Gaps closed 2026-11-13 (Marty: "let's get the gaps report built"), still one way: a DELIBERATE sponsor change
// (sponsorfollow: adopt, rescue, release, expiry, admin) also goes to /v1/sponsor-change; BTC4Hits accounts (carried
// here with joinedRef 'btc4hits', no other door reports them) are reported like LinkSpin's own; every MemberActivated on
// the LinkSpin contract goes to /v1/contract-bind (contract 'linkspin', sponsor by wallet) from its own queue (binds).
// The InstantAdPay<->LinkSpin sync (iapsync.js, /api/partner/registry) is not touched by any of this.
'use strict';
const crypto = require('crypto');
@@ -146,7 +150,7 @@ function touch(email) {
async function report(e) {
const a = await R.accounts.byEmail(e); if (!a) return;
const st = load(); const s = st.sent[e] || {};
if (a.joinedVia !== 'carried') { // a waiting copy of someone from another door: that door reports them
if (a.joinedVia !== 'carried' || a.joinedRef === 'btc4hits') { // a waiting copy of someone from another door: that door reports them (BTC4Hits has no door of its own)
const sp = a.sponsorRef ? await R.accounts.sponsorOf(e) : null;
const ref = sp ? norm(sp.email) : (a.sponsorRef || null);
const key = (a.username || '') + '|' + (ref || '');
@@ -168,13 +172,45 @@ async function report(e) {
st.stats.dropped++; console.log('hubsync wallet refused', r.status, r.body.error || '', mask(e)); }
s.w = w;
}
const rec = R.follow && R.follow.get ? R.follow.get(e) : null;
const spNow = a.sponsorRef ? await R.accounts.sponsorOf(e) : null;
if (rec && DELIBERATE.has(rec.by) && Number(rec.at) > 0 && norm(rec.sponsorEmail) === (spNow ? norm(spNow.email) : '') && s.sc !== Number(rec.at)) {
const r = await call('/v1/sponsor-change', { email: e, sponsorEmail: spNow ? spNow.email : null, by: rec.by });
if (r.status === 200) st.stats.sponsorChanges = (st.stats.sponsorChanges || 0) + 1;
else { st.stats.dropped++; noteDisagree(e, '(hub refused: ' + (r.body.error || r.status) + ')', spNow ? spNow.email : '', 'sponsor-change'); console.log('hubsync sponsor-change refused', r.status, r.body.error || '', mask(e)); }
s.sc = Number(rec.at);
}
st.sent[e] = s;
}
const DELIBERATE = new Set(['adopt', 'rescue', 'release', 'expiry', 'admin']); // sponsorfollow.js kinds the hub accepts
// chain.onEvent: a payout switch-on on the LinkSpin contract -> /v1/contract-bind (queued, retried with backoff)
function onChain(ev) {
if (!R || !on() || !ev || ev.type !== 'MemberActivated' || !ev.account) return;
(async () => {
let spW = null; try { const m = ev.sponsorId ? await R.chain.member(ev.sponsorId) : null; spW = m && m.account ? norm(m.account) : null; } catch (e) {}
const st = load(); st.binds = st.binds || [];
if (st.binds.length < 20000 && !st.binds.some(b => b.wallet === norm(ev.account))) { st.binds.push({ wallet: norm(ev.account), sponsorWallet: spW, tries: 0, next: 0 }); save(); }
})().catch(err => console.error('hubsync onChain', err.message));
}
async function flushBinds() {
const st = load(); st.binds = st.binds || []; const now = Date.now();
for (const b of st.binds.slice()) {
if (b.next > now) continue;
let owner = null; try { owner = await R.accounts.byAddress(b.wallet); } catch (e) {}
let r; try { r = await call('/v1/contract-bind', { contract: 'linkspin', email: owner && owner.email ? owner.email : undefined, wallet: b.wallet, sponsorRef: b.sponsorWallet || '' }); }
catch (e) { r = { status: 0, body: { error: e.message } }; }
if (r.status === 200) { st.binds.splice(st.binds.indexOf(b), 1); st.stats.binds = (st.stats.binds || 0) + 1; }
else if (r.status === 400 || r.status === 409) { st.binds.splice(st.binds.indexOf(b), 1); st.stats.dropped++; console.log('hubsync bind refused', r.status, r.body.error || '', b.wallet); }
else { b.tries++; if (b.tries >= 20) { st.binds.splice(st.binds.indexOf(b), 1); st.stats.dropped++; } else b.next = now + Math.min(3600000, 30000 * 2 ** Math.min(b.tries, 7)); }
save();
}
}
async function flush() {
if (!on() || flushing) return { skipped: true };
flushing = true; let done = 0, failed = 0;
try {
const st = load(); const now = Date.now();
await flushBinds();
for (const [e, o] of Object.entries(st.outbox)) {
if (o.next > now) continue;
try { await report(e); delete st.outbox[e]; done++; }
@@ -190,4 +226,4 @@ function adminView() {
return { on: on(), url: URL_(), site: SITE(), lastSeq: st.lastSeq, outbox: Object.keys(st.outbox).length, stats: st.stats, lastError: st.lastError || null,
disagree: Object.entries(st.disagree).slice(-50).map(([e, d]) => ({ member: mask(e), hub: mask(d.hub), here: mask(d.here), how: d.how, at: d.at })), disagreeTotal: Object.keys(st.disagree).length };
}
module.exports = { init, on, receive, touch, flush, tick, adminView };
module.exports = { init, on, receive, touch, onChain, flush, tick, adminView };
+2 -2
View File
@@ -630,7 +630,7 @@ async function boot() {
marks.init({ db, dataDir: DATA_DIR });
marks.ensureTable().catch(e => console.error('marks table', e.message));
setInterval(() => marks.prune().catch(() => {}), 6 * 3600 * 1000).unref();
chain.init({ onEvent: ev => { attachNames([ev]).then(a => pushFeed(a[0])).catch(() => pushFeed(ev)); emailOnEvent(ev).catch(() => {}); telegramOnEvent(ev).catch(() => {});
chain.init({ onEvent: ev => { attachNames([ev]).then(a => pushFeed(a[0])).catch(() => pushFeed(ev)); hubsync.onChain(ev); emailOnEvent(ev).catch(() => {}); telegramOnEvent(ev).catch(() => {});
if (ev && ev.type === 'Purchase' && ev.buyerId) accounts.byMemberId(Number(ev.buyerId)).then(a => { if (a) return postbacks.convertFor(a.email, 'purchase', { payout: (Number(ev.priceCents) || 0) / 100, currency: 'USD', txid: ev.tx || '' }); }).catch(() => {});
if (ev && ev.type === 'Purchase') refuel.onPurchase(ev).catch(e => console.error('refuel report', e.message)); } }); // no-op while refuelStreak is off
auth.init({ dataDir: DATA_DIR, chain, isProd: IS_PROD, site: SITE_HOST });
@@ -732,7 +732,7 @@ async function boot() {
setTimeout(() => carry.backfillMemberIds().then(r => console.log('memberId backfill', JSON.stringify(r))).catch(e => console.error('memberId backfill', e.message)), 60000);
// reverse registry sync (2026-10-15): account changes reach InstantAdPay within seconds; a 2-minute reconcile catches the rest
iapsync.init({ dataDir: DATA_DIR, accounts, registry, host: SITE_HOST, follow: followStore, tank: () => tank }); accounts.onChange(email => { iapsync.touch(email); hubsync.touch(email); });
hubsync.init({ dataDir: DATA_DIR, accounts, registry, carry, follow: followStore }); setInterval(() => hubsync.tick().catch(e => console.error('hubsync', e.message)), 30 * 1000); // touch/tick do nothing while HUB_ENABLED is off
hubsync.init({ dataDir: DATA_DIR, accounts, registry, carry, follow: followStore, chain }); setInterval(() => hubsync.tick().catch(e => console.error('hubsync', e.message)), 30 * 1000); // touch/tick do nothing while HUB_ENABLED is off
setTimeout(() => iapsync.tick().catch(e => console.error('iapsync', e.message)), 2 * 60 * 1000);
setInterval(() => iapsync.tick().catch(e => console.error('iapsync', e.message)), 2 * 60 * 1000);
const ownHosts = () => [SITE_HOST].concat(String(siteConfig().linkDomains || '').split(',').map(x => x.trim().toLowerCase()).filter(Boolean), pagesHost() ? [pagesHost()] : []);