Sponsor follow + one network holding tank + Sponsor check (Marty, 2026-10-22, approved 1-3)
Same design as InstantAdPay (shared sponsorfollow.js). Deliberate sponsor changes (tank adoption, rescue, release, adoption window ending, admin edit) are recorded and travel on the iapsync rows; InstantAdPay's deliberate changes are applied here only when the member is not bound or binding on the LinkSpin contract (memberId, wallet on chain, or an engine activation in flight), newer than our own, and both sides agreed before (or no sponsor here). Bound / diverged / race cases change nothing and are logged. Ordinary rows keep the fill-empty rule, except a sponsor emptied on purpose here is not refilled by an ordinary row. Carry: an InstantAdPay batch holds its members (carry.hold) so no auto-activation starts mid-batch; ensureOnChain waits, then re-reads the account so the contract binds the settled sponsor; wallets from a batch are linked after every sponsor decision; the wallet hook skips activation while a follow is pending and activates once it is settled. Registry keeps the follow record (loop guard) and a deliberate clear. Network tank, Admin > Members > Sponsor check and the partner endpoints as on InstantAdPay. Rows without the new facts hash as before (no resend on deploy). Tests: qa/sponsorfollow.test.cjs 27/27, qa/carry-hold.test.cjs ok, two-server e2e (in the InstantAdPay repo) 20/20, QA member walk 0 bugs. No sponsor was changed on production. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -21,6 +21,18 @@ const ABI = ['function activateFor(address account, uint32 sponsorId) returns (u
|
||||
const norm = e => String(e || '').trim().toLowerCase();
|
||||
|
||||
function init(opts) { X = opts; }
|
||||
|
||||
// ---- sponsor-follow guards (2026-10-22, sponsorfollow.js) ----
|
||||
// hold(emails): an InstantAdPay registry batch is being applied for these people; an activation that starts meanwhile
|
||||
// (a wallet arriving in the same batch fires onWalletLinked) waits until the batch is done and then re-reads the account,
|
||||
// so it binds the sponsor the batch settled on, never the one from before it.
|
||||
// isBinding(email): an engine activation for this person is in flight; the follow rule treats them as binding (bound).
|
||||
const holds = new Map(); const inflight = new Set();
|
||||
function hold(emails) { for (const e of emails || []) { const k = norm(e); if (k) holds.set(k, (holds.get(k) || 0) + 1); } }
|
||||
function unhold(emails) { for (const e of emails || []) { const k = norm(e); const c = (holds.get(k) || 0) - 1; if (c > 0) holds.set(k, c); else holds.delete(k); } }
|
||||
const isHeld = email => holds.has(norm(email));
|
||||
const isBinding = email => inflight.has(norm(email));
|
||||
async function waitUnheld(email, maxMs) { const t0 = Date.now(); while (isHeld(email) && Date.now() - t0 < (maxMs || 20000)) await sleep(250); }
|
||||
function claimDays() { const n = Number((X.siteConfig && X.siteConfig().carryClaimDays) || 3); return n > 0 ? n : 3; }
|
||||
const STATE = () => path.join(X.dataDir, 'carry-state.json');
|
||||
function state() { try { return JSON.parse(fs.readFileSync(STATE(), 'utf8')); } catch (e) { return { claims: {}, log: [] }; } }
|
||||
@@ -129,8 +141,13 @@ async function nearestWithWallet(email) {
|
||||
// activate `acct` on this contract via the engine, after its own sponsor is there (recursive, bounded)
|
||||
async function ensureOnChain(acct, depth, why) {
|
||||
if (!acct || !acct.address) return 0;
|
||||
// a registry batch is still settling this person's sponsor: wait for it, then use the account as it is NOW
|
||||
if (acct.email && isHeld(acct.email)) await waitUnheld(acct.email);
|
||||
if (acct.email) { try { const fresh = await X.accounts.byEmail(acct.email); if (fresh && fresh.address) acct = fresh; } catch (e) {} }
|
||||
try { const id = await X.chain.memberIdByAccount(acct.address); if (id) { if (Number(acct.memberId) !== Number(id)) await X.accounts.setMemberId(acct.email, id).catch(() => {}); return id; } } catch (e) {}
|
||||
if (!engineReady() || depth > 25) return 0;
|
||||
const me = norm(acct.email); inflight.add(me); let id = 0; // binding from here: the sponsor read below is the one the contract gets
|
||||
try {
|
||||
let sponsorId = 0;
|
||||
if (acct.sponsorRef) {
|
||||
let sp = await X.accounts.byCode(acct.sponsorRef); if (!sp) sp = await X.accounts.byUsername(acct.sponsorRef);
|
||||
@@ -140,7 +157,7 @@ async function ensureOnChain(acct, depth, why) {
|
||||
try { if (sponsorId > await X.chain.memberCount()) sponsorId = 0; } catch (e) {} // first member on a fresh contract: no catch position exists yet
|
||||
// send with provider rotation; after ANY error re-read the chain first (2026-10-15 seed: six activations
|
||||
// mined while a 403 on the follow-up read logged them as failures and skipped their notices)
|
||||
let id = 0, txHash = null, lastErr = null;
|
||||
let txHash = null, lastErr = null;
|
||||
for (let attempt = 0; attempt < 3 && !id; attempt++) {
|
||||
try {
|
||||
const w = signer(attempt); const ctr = new ethers.Contract(X.chain.getConfig().contract, ABI, w);
|
||||
@@ -149,11 +166,13 @@ async function ensureOnChain(acct, depth, why) {
|
||||
} catch (e) { lastErr = e; }
|
||||
for (let k = 0; k < 4 && !id; k++) { try { id = Number(await X.chain.memberIdByAccount(acct.address)) || 0; } catch (e) {} if (!id) await sleep(3000); }
|
||||
}
|
||||
} finally { if (!id) inflight.delete(me); } // on success the flag stays until the member number is saved below
|
||||
if (!id) { logEvent({ type: 'activateFailed', email: acct.email, error: String((lastErr && (lastErr.shortMessage || lastErr.message)) || 'not on-chain after send').slice(0, 160) }); return 0; }
|
||||
logEvent({ type: 'activated', email: acct.email, id, sponsorId, tx: txHash });
|
||||
// keep the member number on the account: the ledger and every name lookup go by it (2026-10-18: 340 engine
|
||||
// activations had none, so the live ledger showed "member #352" instead of the username)
|
||||
await X.accounts.setMemberId(acct.email, id).catch(() => {});
|
||||
inflight.delete(me);
|
||||
await noticeActivated(acct, why);
|
||||
return id;
|
||||
}
|
||||
@@ -205,4 +224,4 @@ async function seedAll(limit) {
|
||||
}
|
||||
function status() { const s = state(); return { engine: engineReady(), claims: Object.values(s.claims || {}), log: (s.log || []).slice(-50).reverse(), claimDays: claimDays() }; }
|
||||
|
||||
module.exports = { init, onJoin, resolveForChain, ensureOnChain, seedAll, status, engineReady, backfillMemberIds };
|
||||
module.exports = { init, onJoin, resolveForChain, ensureOnChain, seedAll, status, engineReady, backfillMemberIds, hold, unhold, isHeld, isBinding };
|
||||
|
||||
Reference in New Issue
Block a user