Hub rule 2 (Marty 2026-11-14): every paid Purchase on the LinkSpin contract goes to the hub's /v1/contract-spend from its own retried queue (spends, sent after the binds, tx hash so a resend counts once, POL-in only; activation alone is not a spend), so when two contracts lock different sponsors the one with money spent decides the network sponsor. Still one way, only with HUB_ENABLED; needs network-hub 296aa5d. qa/hubsync.test.cjs 11 checks ok

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
martbost
2026-11-14 07:55:25 -06:00
parent 9c4a6458f6
commit 5fbaf4f17d
2 changed files with 49 additions and 2 deletions
+30 -1
View File
@@ -14,6 +14,7 @@
// (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).
// 2026-11-14: every paid Purchase goes to /v1/contract-spend (queue spends, after the binds) for the hub's rule 2.
// The InstantAdPay<->LinkSpin sync (iapsync.js, /api/partner/registry) is not touched by any of this.
'use strict';
const crypto = require('crypto');
@@ -185,6 +186,7 @@ async function report(e) {
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 (ev && ev.type === 'Purchase') return onPurchase(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) {}
@@ -192,6 +194,33 @@ function onChain(ev) {
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));
}
// chain.onEvent: money paid into the LinkSpin contract (a Purchase with POL in; activation alone is not a spend) -> the hub's
// /v1/contract-spend (Marty 2026-11-14, rule 2: when two contracts hold different sponsors, the one with money spent decides
// the network sponsor). Sent after the binds in each flush; the tx hash makes a resend count once; a 404 (bind not there yet)
// waits and retries.
function onPurchase(ev) {
if (!R || !on() || !ev.buyerId || !(Number(ev.priceCents) > 0)) return;
try { if (BigInt(ev.paidWei || '0') <= 0n) return; } catch (e) { return; }
(async () => {
const m = await R.chain.member(ev.buyerId); const w = m && m.account ? norm(m.account) : null; if (!w) return;
const st = load(); st.spends = st.spends || [];
if (st.spends.length < 20000 && !st.spends.some(x => x.tx && x.tx === ev.tx)) { st.spends.push({ wallet: w, cents: Number(ev.priceCents), tx: ev.tx || null, paidAt: Number(ev.ts) || Date.now(), tries: 0, next: 0 }); save(); }
})().catch(err => console.error('hubsync onPurchase', err.message));
}
async function flushSpends() {
const st = load(); st.spends = st.spends || []; const now = Date.now();
const waitingBind = new Set((st.binds || []).map(b => b.wallet));
for (const x of st.spends.slice()) {
if (x.next > now || waitingBind.has(x.wallet)) continue; // its bind goes first
let owner = null; try { owner = await R.accounts.byAddress(x.wallet); } catch (e) {}
let r; try { r = await call('/v1/contract-spend', { contract: 'linkspin', email: owner && owner.email ? owner.email : undefined, wallet: x.wallet, cents: x.cents, tx: x.tx || undefined, at: x.paidAt }); }
catch (e) { r = { status: 0, body: { error: e.message } }; }
if (r.status === 200) { st.spends.splice(st.spends.indexOf(x), 1); st.stats.spends = (st.stats.spends || 0) + 1; }
else if (r.status === 400 || r.status === 409) { st.spends.splice(st.spends.indexOf(x), 1); st.stats.dropped++; console.log('hubsync spend refused', r.status, r.body.error || '', x.wallet); }
else { x.tries++; if (x.tries >= 20) { st.spends.splice(st.spends.indexOf(x), 1); st.stats.dropped++; } else x.next = now + Math.min(3600000, 30000 * 2 ** Math.min(x.tries, 7)); }
save();
}
}
async function flushBinds() {
const st = load(); st.binds = st.binds || []; const now = Date.now();
for (const b of st.binds.slice()) {
@@ -210,7 +239,7 @@ async function flush() {
flushing = true; let done = 0, failed = 0;
try {
const st = load(); const now = Date.now();
await flushBinds();
await flushBinds(); await flushSpends();
for (const [e, o] of Object.entries(st.outbox)) {
if (o.next > now) continue;
try { await report(e); delete st.outbox[e]; done++; }