Hub switch-on (Marty 2026-11-14). Rule 2: a hub arrival gets person.locks.linkspin's sponsor when the LinkSpin contract holds them, never the plain network sponsor; sponsor.changed stays ignored. Quiet: a wallet filled from a hub event never switches payouts on (isQuiet: no gas, no lock, no 'payouts are switched on' mail). Rule 3: the holding tank (waiting, release, expiry, rescue, readyTick, autoAssign) leaves out anyone the hub reports locked on any contract or sponsored, and hub waiting copies that never signed in here; /v1/locked detail cached 10 min, last answer kept 1 h, after that held (unknown). Alignment to the hub sponsor only with HUB_TANK_ALIGN=on. Needs network-hub b44dba6. hubsync 15 checks, sponsorfollow 30/30, tank-cap 15/15, networksso ALL PASSED, tank-hold, carry-hold ok
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
+84
-1
@@ -13,6 +13,11 @@ const HUB_DIR = process.env.HUB_DIR || [path.join(__dirname, '..', '..', 'networ
|
||||
const LS_DIR = fs.mkdtempSync(path.join(os.tmpdir(), 'ls-hub-')); const HUB_DATA = fs.mkdtempSync(path.join(os.tmpdir(), 'hub-data-'));
|
||||
const LS_SECRET = 'l'.repeat(40), CTB_SECRET = 'c'.repeat(40);
|
||||
delete process.env.DATABASE_URL; delete process.env.HUB_ENABLED;
|
||||
// mail audit (2026-11-14): every outbound request this process makes is recorded; only the local hub and site may be called.
|
||||
// mailer.js sends over https, Telegram and Sendy too, so both doors are watched.
|
||||
const outside = [];
|
||||
{ const realFetch = global.fetch; global.fetch = (u, o) => { const s = String(u && u.url ? u.url : u); if (!/^http:[/][/]127[.]0[.]0[.]1:/.test(s)) outside.push(s); return realFetch(u, o); };
|
||||
const https = require('https'); const realReq = https.request; https.request = function (...a) { outside.push('https ' + JSON.stringify((a[0] && a[0].hostname) || a[0])); return realReq.apply(this, a); }; }
|
||||
const accounts = require(path.join(__dirname, '..', 'accounts.js'));
|
||||
const registry = require(path.join(__dirname, '..', 'registry.js'));
|
||||
const carry = require(path.join(__dirname, '..', 'carry.js'));
|
||||
@@ -20,6 +25,9 @@ const hubsync = require(path.join(__dirname, '..', 'hubsync.js'));
|
||||
accounts.init({ dataDir: LS_DIR }); registry.init({ dataDir: LS_DIR });
|
||||
hubsync.init({ dataDir: LS_DIR, accounts, registry, carry, follow: null });
|
||||
accounts.onChange(e => hubsync.touch(e));
|
||||
// the wallet hook as server.js wires it: the engine would switch payouts on (gas, a lock, the "payouts are switched on" mail)
|
||||
const activations = [];
|
||||
accounts.onWalletLinked(async email => { if (hubsync.isQuiet(email)) return; activations.push(email); });
|
||||
const E = n => n + '@example.test';
|
||||
const W = n => '0x' + String(n).padStart(40, '0');
|
||||
|
||||
@@ -143,7 +151,82 @@ const W = n => '0x' + String(n).padStart(40, '0');
|
||||
assert.strictEqual(tur.bindings.linkspin.spentCents, 1000, 'one purchase counted once');
|
||||
assert.strictEqual(tur.sponsorId, per(E('ptw')).id, 'the earlier spend (InstantAdPay) still decides');
|
||||
assert.strictEqual(hubsync.adminView().stats.spends, 1);
|
||||
console.log('hubsync: ok (11 checks)');
|
||||
// 12. rule 2 on arrival (2026-11-14): someone the LinkSpin contract holds arrives under THAT lock's sponsor, not the network
|
||||
// sponsor (another contract where money was spent); a later sponsor.changed never moves them
|
||||
const HP = require(path.join(HUB_DIR, 'lib', 'people.js'));
|
||||
await ctb('/v1/join', { email: E('lx'), username: 'lockx' }); await ctb('/v1/join', { email: E('ly'), username: 'locky' });
|
||||
await ctb('/v1/join', { email: E('lk'), username: 'lockkid', ref: 'locky' });
|
||||
HP.bindContract({ site: 'linkspin', contract: 'linkspin', email: E('lk'), wallet: W(81), sponsorRef: 'lockx', at: 1000 });
|
||||
HP.bindContract({ site: 'iap', contract: 'iap', email: E('lk'), wallet: W(81), sponsorRef: 'locky', at: 2000, spentCents: 2500 });
|
||||
await hstore.flush();
|
||||
assert.strictEqual(per(E('lk')).sponsorId, per(E('ly')).id, 'network sponsor = InstantAdPay (money spent there)');
|
||||
await hooks.tick();
|
||||
assert.strictEqual((await accounts.sponsorOf(E('lk'))).email, E('lx'), 'the LinkSpin lock sponsor is written here');
|
||||
const lkSeq = lastSeq() + 1000;
|
||||
const evRaw = JSON.stringify({ events: [{ seq: lkSeq, type: 'sponsor.changed', personId: per(E('lk')).id, data: { by: 'contract', contract: 'iap' } }] }); const evTs = String(Date.now());
|
||||
assert.strictEqual((await hubsync.receive({ 'x-hub-ts': evTs, 'x-hub-sig': crypto.createHmac('sha256', LS_SECRET).update(evTs + '\n' + evRaw).digest('hex') }, evRaw)).status, 200);
|
||||
assert.strictEqual((await accounts.sponsorOf(E('lk'))).email, E('lx'), 'sponsor.changed is ignored');
|
||||
hstore.setCursor('linkspin', lastSeq()); await hstore.flush(); // the test event above moved lastSeq past the hub's numbering
|
||||
const st0 = JSON.parse(fs.readFileSync(path.join(LS_DIR, 'hubsync.json'), 'utf8')); st0.lastSeq = lastSeq(); fs.writeFileSync(path.join(LS_DIR, 'hubsync.json'), JSON.stringify(st0));
|
||||
hubsync.init({ dataDir: LS_DIR, accounts, registry, carry, follow: null });
|
||||
|
||||
// 13. rule 3: the tank never offers anyone locked on any network contract, sponsored in the hub, or a hub waiting copy that
|
||||
// never signed in here; release refuses a locked member. Tank as server.js wires it, mailer and messages only record.
|
||||
const tank = require(path.join(__dirname, '..', 'tank.js'));
|
||||
const sent = [];
|
||||
const mailer = { hasKey: () => true, send: async (...a) => { sent.push(['mail', a[0], a[1]]); } };
|
||||
const messages = { sendChat: async (...a) => { sent.push(['chat', a[2]]); }, deliver: async (...a) => { sent.push(['notice', a[2]]); }, lastFrom: async () => Date.now() };
|
||||
tank.init({ dataDir: LS_DIR, accounts, chain: { recentEvents: () => [], memberIdByAccount: async () => 0 }, messages, mailer,
|
||||
hooks: { network: async e => hubsync.hubSponsor(e), networkHeld: a => hubsync.heldAmong(a), networkLocked: e => hubsync.lockedAnywhere(e) } });
|
||||
await ctb('/v1/join', { email: E('hs') }); await hooks.tick(); // a CTB join with no sponsor anywhere: a waiting copy here
|
||||
assert.ok(await accounts.byEmail(E('hs')), 'waiting copy made');
|
||||
await ctb('/v1/join', { email: E('tk1'), username: 'tk1ctb', ref: 'ann' }); // the hub knows a sponsor
|
||||
for (const n of ['tk1', 'tk2', 'tk3']) assert.ok((await accounts.ensure(E(n), '', '', null)).created); // signed up here, no sponsor
|
||||
await hubsync.flush();
|
||||
HP.bindContract({ site: 'iap', contract: 'iap', email: E('tk2'), wallet: W(82), sponsorRef: '' }); await hstore.flush(); // locked, top of that tree
|
||||
hubsync._heldReset();
|
||||
const held = await hubsync.heldAmong(await Promise.all(['tk1', 'tk2', 'tk3', 'hs'].map(n => accounts.byEmail(E(n)))));
|
||||
assert.deepStrictEqual([...held.entries()].sort(), [[E('hs'), 'hub-shell'], [E('tk1'), 'sponsored'], [E('tk2'), 'locked']]);
|
||||
let w = (await tank.waiting()).map(x => x.email);
|
||||
assert.ok(w.includes(E('tk3')), 'a free member is offered');
|
||||
for (const n of ['tk1', 'tk2', 'hs']) assert.ok(!w.includes(E(n)), n + ' is not offered: ' + w.join(','));
|
||||
await accounts.setSponsorRef(E('tk2'), 'zed');
|
||||
const rel = await tank.release(E('zed'), E('tk2'));
|
||||
assert.ok(rel.error && /locked/.test(rel.error), JSON.stringify(rel)); assert.strictEqual((await accounts.sponsorOf(E('tk2'))).email, E('zed'));
|
||||
await tank.readyTick(); assert.strictEqual((await accounts.byEmail(E('tk1'))).sponsorRef || '', '', 'alignment from the hub is off by default');
|
||||
process.env.HUB_TANK_ALIGN = 'on'; await tank.readyTick(); delete process.env.HUB_TANK_ALIGN; // aligns tk1 to the sponsor the hub knows, and leaves the waiting copy alone
|
||||
assert.strictEqual((await accounts.sponsorOf(E('tk1'))).email, E('ann'), 'aligned to the hub sponsor');
|
||||
assert.strictEqual((await accounts.byEmail(E('hs'))).sponsorRef || '', '', 'the waiting copy is not touched');
|
||||
|
||||
// 14. fail-safe: hub unreachable past the 1-hour cache = held; within the hour the last answer stands; back = offered again
|
||||
process.env.HUB_URL = 'http://127.0.0.1:1'; hubsync._heldAge(30 * 60000);
|
||||
assert.ok((await tank.waiting()).map(x => x.email).includes(E('tk3')), 'within the hour');
|
||||
hubsync._heldReset();
|
||||
assert.ok(!(await tank.waiting()).map(x => x.email).includes(E('tk3')), 'unknown = held');
|
||||
assert.strictEqual(await hubsync.lockedAnywhere(E('tk3')), 'unknown');
|
||||
process.env.HUB_URL = `http://127.0.0.1:${hubPort}`; hubsync._heldReset();
|
||||
assert.ok((await tank.waiting()).map(x => x.email).includes(E('tk3')));
|
||||
|
||||
// 15. mail audit: hub arrivals with wallets (new, sponsor climbed first, an empty wallet filled later) switch nothing on and
|
||||
// send no mail, chat, notice or outside call; neither do the tank ticks afterwards
|
||||
sent.length = 0; outside.length = 0; activations.length = 0;
|
||||
await ctb('/v1/join', { email: E('ma'), username: 'mailtop' }); await ctb('/v1/wallet', { email: E('ma'), wallet: W(83) });
|
||||
await ctb('/v1/join', { email: E('mb'), username: 'mailkid', ref: 'mailtop' }); await ctb('/v1/wallet', { email: E('mb'), wallet: W(84) });
|
||||
await ctb('/v1/join', { email: E('mc'), username: 'mailfill' });
|
||||
const mbEv = hstore.eventsAfter(0, 1e6).find(e => e.type === 'member.joined' && e.personId === per(E('mb')).id);
|
||||
hstore.setCursor('linkspin', mbEv.seq - 1); await hstore.flush(); // the kid's join first: the sponsor is placed from the hub
|
||||
await hooks.tick();
|
||||
await ctb('/v1/wallet', { email: E('mc'), wallet: W(85) }); await hooks.tick(); // wallet.connected on a waiting copy
|
||||
assert.strictEqual((await accounts.byEmail(E('mb'))).address, W(84)); assert.strictEqual((await accounts.byEmail(E('mc'))).address, W(85));
|
||||
assert.strictEqual((await accounts.sponsorOf(E('mb'))).email, E('ma'));
|
||||
await hubsync.flush(); await tank.readyTick(); await tank.sweep();
|
||||
assert.deepStrictEqual(activations, [], 'no payouts switch-on for wallets filled from the hub');
|
||||
assert.deepStrictEqual(sent, [], 'nothing sent');
|
||||
assert.deepStrictEqual(outside, [], 'no outside call');
|
||||
const loaded = Object.keys(require.cache).map(f => path.basename(f));
|
||||
for (const m of ['mailer.js', 'sendy.js', 'messages.js', 'drip.js']) assert.ok(!loaded.includes(m), m + ' is loaded');
|
||||
await accounts.linkWallet(E('tk3'), W(86)); assert.deepStrictEqual(activations, [E('tk3')], 'a wallet linked here still switches payouts on');
|
||||
console.log('hubsync: ok (15 checks)');
|
||||
} finally { hub.close(); ls.close(); }
|
||||
process.exit(0);
|
||||
})().catch(e => { console.error('hubsync: FAIL', e.stack || e.message); process.exit(1); });
|
||||
|
||||
Reference in New Issue
Block a user