Hub rule 2 (Marty 2026-11-14): every paid Purchase on the InstantAdPay contract goes to the hub's /v1/contract-spend (keyed by the position wallet so its bind is sent first, tx hash so a resend counts once, POL-in only), 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 15/15
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
+21
-1
@@ -26,6 +26,8 @@
|
||||
// - a DELIBERATE sponsor change (sponsorfollow: adopt, rescue, release, expiry, admin) goes to /v1/sponsor-change;
|
||||
// - every MemberActivated on the InstantAdPay contract goes to /v1/contract-bind (contract 'iap', the sponsor by wallet),
|
||||
// by email when the wallet belongs to an account here, else by wallet alone.
|
||||
// - every paid Purchase goes to /v1/contract-spend (2026-11-14, rule 2: the contract with money spent decides the network
|
||||
// sponsor when two contracts hold different ones).
|
||||
// Where the hub and InstantAdPay name different sponsors, nothing is forced on either side: the case goes to `disagree`
|
||||
// (Admin > /api/admin/hub) for Marty, since an InstantAdPay member bound on our contract cannot move anyway.
|
||||
'use strict';
|
||||
@@ -187,6 +189,7 @@ function touch(email) {
|
||||
}
|
||||
// chain.onEvent (once across copies): a payout switch-on on the InstantAdPay contract goes to /v1/contract-bind
|
||||
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) {}
|
||||
@@ -194,6 +197,19 @@ function onChain(ev) {
|
||||
s.outbox.push({ kind: 'bind', email: norm(ev.account), wallet: norm(ev.account), sponsorWallet: spW, at: Date.now(), tries: 0, next: 0 }); save(); kick();
|
||||
})().catch(err => console.error('hubsync onChain', err.message));
|
||||
}
|
||||
// chain.onEvent: money paid into the InstantAdPay contract (Purchase with POL in) goes to /v1/contract-spend (Marty 2026-11-14,
|
||||
// rule 2: when two contracts hold different sponsors, the one with money spent decides the network sponsor). Keyed by the
|
||||
// buyer's position wallet, the same key as its bind, so the bind is always sent first; the tx hash makes a resend count once.
|
||||
// A 404 (bind not there yet, or a linked extra position the hub does not know) is retried like any other miss.
|
||||
function onPurchase(ev) {
|
||||
if (!R || !on() || !ev.buyerId || !(Number(ev.priceCents) > 0)) return;
|
||||
try { if (BigInt(ev.paidWei || '0') <= 0n) return; } catch (e) { return; } // nothing paid in: not a spend
|
||||
(async () => {
|
||||
const m = await R.chain.member(ev.buyerId); const w = m && m.account ? norm(m.account) : null; if (!w) return;
|
||||
const s = load(); if (s.outbox.length >= 20000) { s.stats.sendErrors++; return; }
|
||||
s.outbox.push({ kind: 'spend', email: w, wallet: w, cents: Number(ev.priceCents), tx: ev.tx || null, paidAt: Number(ev.ts) || Date.now(), at: Date.now(), tries: 0, next: 0 }); save(); kick();
|
||||
})().catch(err => console.error('hubsync onPurchase', err.message));
|
||||
}
|
||||
// accounts.onWalletLinked: a main wallet linked here goes to /v1/wallet
|
||||
function wallet(email) {
|
||||
if (!R || !on() || !email) return;
|
||||
@@ -202,8 +218,12 @@ function wallet(email) {
|
||||
if (a && a.address && !R.accounts.isUnclaimedShell(a)) enqueue('wallet', norm(email)); // a waiting account's wallet came from a sync
|
||||
})().catch(err => console.error('hubsync wallet', err.message));
|
||||
}
|
||||
const ROUTE = { join: '/v1/join', wallet: '/v1/wallet', sponsor: '/v1/sponsor-change', bind: '/v1/contract-bind' };
|
||||
const ROUTE = { join: '/v1/join', wallet: '/v1/wallet', sponsor: '/v1/sponsor-change', bind: '/v1/contract-bind', spend: '/v1/contract-spend' };
|
||||
async function bodyFor(item) {
|
||||
if (item.kind === 'spend') {
|
||||
let owner = null; try { owner = (await R.accounts.byAddress(item.wallet)) || (R.accounts.positionOwner ? await R.accounts.positionOwner(item.wallet) : null); } catch (e) {}
|
||||
return { contract: 'iap', email: owner && owner.email ? owner.email : undefined, wallet: item.wallet, cents: item.cents, tx: item.tx || undefined, at: item.paidAt };
|
||||
}
|
||||
if (item.kind === 'bind') {
|
||||
let owner = null; try { owner = await R.accounts.byAddress(item.wallet); } catch (e) {}
|
||||
return { contract: 'iap', email: owner && owner.email ? owner.email : undefined, wallet: item.wallet, sponsorRef: item.sponsorWallet || '' };
|
||||
|
||||
@@ -164,6 +164,29 @@ const IAP_DATA = fs.mkdtempSync(path.join(os.tmpdir(), 'hubsync-iap-'));
|
||||
const v = hubsync.adminView(); assert.strictEqual(v.outbox, 0); assert.ok(v.recent.find(l => l.type === 'out-refused' && l.code === 'wallet_taken'));
|
||||
});
|
||||
|
||||
await t('rule 2: a paid Purchase on the InstantAdPay contract reaches the hub and moves the network sponsor to this contract (2026-11-14)', async () => {
|
||||
// tureia2003's shape: locked on LinkSpin under silverstussy (activated only), then bound AND paid here under paidtowatch
|
||||
await ctb('/v1/join', { email: 'ptw@example.test', username: 'ptw' }); await ctb('/v1/wallet', { email: 'ptw@example.test', wallet: W('d') });
|
||||
await ctb('/v1/join', { email: 'sil@example.test', username: 'sil' }); await ctb('/v1/wallet', { email: 'sil@example.test', wallet: W('f') });
|
||||
await ctb('/v1/join', { email: 'tur@example.test', ref: 'sil', username: 'tur' });
|
||||
await ctb('/v1/contract-bind', { contract: 'linkspin', email: 'tur@example.test', wallet: W('7'), sponsorRef: W('f') });
|
||||
assert.strictEqual(hubPerson('tur@example.test').sponsorId, hubPerson('sil@example.test').id);
|
||||
const members = { 5: { account: W('d') }, 12: { account: W('7') } };
|
||||
hubsync.init({ dataDir: IAP_DATA, accounts, lsinbound, chain: { member: async id => members[id] || null } });
|
||||
hubsync.onChain({ type: 'MemberActivated', id: 12, account: W('7'), sponsorId: 5 });
|
||||
hubsync.onChain({ type: 'Purchase', buyerId: 12, priceCents: 0, paidWei: '0', tx: '0xfree' }); // nothing paid in: not a spend
|
||||
hubsync.onChain({ type: 'Purchase', buyerId: 12, priceCents: 3500, paidWei: '1000', tx: '0xpaid', ts: 1700000000000 });
|
||||
hubsync.onChain({ type: 'Purchase', buyerId: 12, priceCents: 3500, paidWei: '1000', tx: '0xpaid', ts: 1700000000000 }); // seen twice
|
||||
await settle(); await hubsync.flush(true);
|
||||
const p = hubPerson('tur@example.test');
|
||||
assert.strictEqual(p.bindings.iap.sponsorId, hubPerson('ptw@example.test').id);
|
||||
assert.strictEqual(p.bindings.iap.spentCents, 3500, 'one purchase counted once');
|
||||
assert.strictEqual(p.bindings.linkspin.sponsorId, hubPerson('sil@example.test').id, 'the LinkSpin lock keeps its sponsor');
|
||||
assert.strictEqual(p.sponsorId, hubPerson('ptw@example.test').id, 'network sponsor = the contract with money spent');
|
||||
assert.strictEqual(hubsync.adminView().outbox, 0);
|
||||
hubsync.init({ dataDir: IAP_DATA, accounts, lsinbound });
|
||||
});
|
||||
|
||||
await t('sponsor.changed and unknown event types are acknowledged and ignored', async () => {
|
||||
const s = signed([{ seq: 99001, type: 'sponsor.changed', personId: hubPerson('bob@example.test').id, data: { by: 'admin' } }, { seq: 99002, type: 'member.blocked', personId: 'h_0000000000000000', data: {} }]);
|
||||
assert.strictEqual((await hubsync.receive(s.headers, s.raw)).status, 200);
|
||||
|
||||
Reference in New Issue
Block a user