From 693a53950346468a24c1317902429c11a8f41d23 Mon Sep 17 00:00:00 2001 From: martbost Date: Tue, 20 Oct 2026 05:21:48 -0500 Subject: [PATCH] Speed (same fixes as InstantAdPay): catalog reads in parallel + stale-while-revalidate /api/catalog, positions read side by side, member reads cached 15 s (cleared on every new chain event) Co-Authored-By: Claude Opus 5.5 --- chain.js | 27 ++++++++++++++++++--------- server.js | 20 +++++++++++--------- 2 files changed, 29 insertions(+), 18 deletions(-) diff --git a/chain.js b/chain.js index 3280a05..25b2a50 100644 --- a/chain.js +++ b/chain.js @@ -138,7 +138,16 @@ async function memberIdByAccount(addr) { } async function memberCount() { return toNum(word(await call(SEL.memberCount), 0)); } async function productCount() { return toNum(word(await call(SEL.productCount), 0)); } +// member reads are cached 15 s (2026-10-20): dashboards ask for the same members many times a minute and every read is +// an RPC round trip; sponsor never changes and a buyer count up to 15 s old is fine for display +const memberCache = new Map(); async function member(id) { + const hit = memberCache.get(Number(id)); if (hit && Date.now() - hit.at < 15000) return hit.v; + const v = await memberRead(id); memberCache.set(Number(id), { at: Date.now(), v }); + if (memberCache.size > 5000) memberCache.clear(); + return v; +} +async function memberRead(id) { const r = await call(SEL.members, [id]); return { account: toAddr(word(r, 0)), sponsorId: toNum(word(r, 1)), buyerCount: toNum(word(r, 2)), activated: toNum(word(r, 3)) === 1, countedAsBuyer: toNum(word(r, 4)) === 1 }; @@ -151,15 +160,15 @@ async function product(id) { async function quoteWei(id) { try { return toBig(word(await call(SEL.quoteWei, [id]), 0)); } catch (e) { return null; } } async function creditBalance(id, type) { return Number(toBig(word(await call(SEL.creditBalance, [id, type || 0]), 0))); } async function catalog() { + // products and quotes are read side by side, not one after another (2026-10-20, same fix as InstantAdPay) const n = await productCount(); - const out = []; - for (let i = 1; i <= n; i++) { - const p = await product(i); - if (!p.exists || !p.active) continue; - const q = await quoteWei(i); - out.push(Object.assign(p, { costWei: q === null ? null : q.toString() })); - } - return out; + const ids = Array.from({ length: n }, (_, k) => k + 1); + const rows = await Promise.all(ids.map(async i => { + const [p, q] = await Promise.all([product(i), quoteWei(i)]); + if (!p.exists || !p.active) return null; + return Object.assign(p, { costWei: q === null ? null : q.toString() }); + })); + return rows.filter(Boolean); } // ---- event decode ---- @@ -231,7 +240,7 @@ async function tail() { const ev = decodeLog(lg); if (!ev) continue; ev.ts = Date.now(); // indexed-at time: powers honest time-series charts - state.events.push(ev); + state.events.push(ev); memberCache.clear(); // a new chain event can change any member: drop the 15 s read cache tally(ev); if (onEvent) { try { onEvent(ev); } catch (e) { console.error('chain onEvent', e.message); } } } diff --git a/server.js b/server.js index 5e0bbf5..51edf51 100644 --- a/server.js +++ b/server.js @@ -1282,10 +1282,10 @@ const server = http.createServer(async (req, res) => { if (p === '/api/catalog' && req.method === 'GET') { // five live quotes per call: serve a 20-second cache so pages that load the // ladder (join, home, buy) are not waiting on the RPC every time - if (!catalogCache.at || Date.now() - catalogCache.at > 20000) { - try { catalogCache.products = await chain.catalog(); catalogCache.at = Date.now(); } - catch (e) { if (!catalogCache.products) throw e; } - } + // stale-while-revalidate (2026-10-20, same as InstantAdPay): serve the cached ladder at once, refresh in the background + const age = catalogCache.at ? Date.now() - catalogCache.at : Infinity; + if (age > 20000 && !catalogCache.busy) catalogCache.busy = chain.catalog().then(pr => { catalogCache.products = pr; catalogCache.at = Date.now(); }).catch(e => console.error('catalog refresh', e.message)).finally(() => { catalogCache.busy = null; }); + if (!catalogCache.products || age > 120000) { try { await catalogCache.busy; } catch (e) {} if (!catalogCache.products) throw new Error('catalog unavailable'); } return json(res, 200, { products: catalogCache.products }); } if (p === '/api/feed' && req.method === 'GET') { @@ -1792,17 +1792,19 @@ const server = http.createServer(async (req, res) => { const out = []; const posIds = [mainId, ...list.map(p => p.memberId)].filter(Boolean); let balPer = {}, credited = 0; try { const bb = await ads.balances(posIds, s.email); credited = bb.credited; for (const p of bb.per) balPer[p.memberId] = p.avail; } catch (e) {} - for (const pos of list) { + // every position's chain reads run side by side (2026-10-20: one after another took 3-8 s, same fix as InstantAdPay) + out.push(...await Promise.all(list.map(async pos => { let id = pos.memberId; if (!id) { try { id = await chain.memberIdByAccount(pos.address); if (id) await accounts.setPositionMember(pos.address, id); } catch (e) {} } const row = { address: pos.address, memberId: id || 0, buyerCount: 0, counted: false, credits: 0, created: pos.created }; if (id) { - try { const mm = await chain.member(id); row.buyerCount = mm.buyerCount; row.counted = mm.countedAsBuyer; row.sponsorId = mm.sponsorId; } catch (e) {} + const [mm, blocked] = await Promise.all([chain.member(id).catch(() => null), payoutChainBlocked(id, 3).catch(() => false)]); + if (mm) { row.buyerCount = mm.buyerCount; row.counted = mm.countedAsBuyer; row.sponsorId = mm.sponsorId; } row.credits = balPer[id] != null ? balPer[id] : 0; - row.noPayout = await payoutChainBlocked(id, 3); // linkage only: a buy from here would pay a listed wallet + row.noPayout = blocked; // linkage only: a buy from here would pay a listed wallet } - out.push(row); - } + return row; + }))); const main = { address: (acct && acct.address) || null, memberId: mainId, credits: 0, buyerCount: 0 }; if (mainId) { main.credits = balPer[mainId] != null ? balPer[mainId] : 0;