@antseed/node 0.2.181 → 0.2.183
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -0
- package/dist/buyer-identity-context.d.ts.map +1 -1
- package/dist/buyer-identity-context.js +2 -2
- package/dist/buyer-identity-context.js.map +1 -1
- package/dist/discovery/announcer.d.ts.map +1 -1
- package/dist/discovery/dht-health.d.ts.map +1 -1
- package/dist/discovery/dht-health.js.map +1 -1
- package/dist/discovery/dht-node.d.ts.map +1 -1
- package/dist/discovery/dht-node.js.map +1 -1
- package/dist/discovery/http-metadata-resolver.d.ts.map +1 -1
- package/dist/discovery/metadata/define-version.d.ts +11 -2
- package/dist/discovery/metadata/define-version.d.ts.map +1 -1
- package/dist/discovery/metadata/define-version.js +7 -3
- package/dist/discovery/metadata/define-version.js.map +1 -1
- package/dist/discovery/metadata/field.d.ts +17 -1
- package/dist/discovery/metadata/field.d.ts.map +1 -1
- package/dist/discovery/metadata/field.js +12 -1
- package/dist/discovery/metadata/field.js.map +1 -1
- package/dist/discovery/metadata/fields/provider-basics.d.ts +1 -1
- package/dist/discovery/metadata/fields/provider-basics.d.ts.map +1 -1
- package/dist/discovery/metadata/fields/provider-basics.js +5 -2
- package/dist/discovery/metadata/fields/provider-basics.js.map +1 -1
- package/dist/discovery/metadata/fields/service-api-protocols.d.ts +1 -1
- package/dist/discovery/metadata/fields/service-api-protocols.d.ts.map +1 -1
- package/dist/discovery/metadata/fields/service-api-protocols.js +2 -1
- package/dist/discovery/metadata/fields/service-api-protocols.js.map +1 -1
- package/dist/discovery/metadata/fields/service-categories.d.ts +1 -1
- package/dist/discovery/metadata/fields/service-categories.d.ts.map +1 -1
- package/dist/discovery/metadata/fields/service-categories.js +2 -1
- package/dist/discovery/metadata/fields/service-categories.js.map +1 -1
- package/dist/discovery/metadata/fields/service-unit-billing.d.ts +1 -1
- package/dist/discovery/metadata/fields/service-unit-billing.d.ts.map +1 -1
- package/dist/discovery/metadata/fields/service-unit-billing.js +4 -2
- package/dist/discovery/metadata/fields/service-unit-billing.js.map +1 -1
- package/dist/discovery/metadata/index.d.ts +3 -2
- package/dist/discovery/metadata/index.d.ts.map +1 -1
- package/dist/discovery/metadata/index.js +2 -2
- package/dist/discovery/metadata/index.js.map +1 -1
- package/dist/discovery/metadata/wire.d.ts.map +1 -1
- package/dist/discovery/metadata/wire.js.map +1 -1
- package/dist/discovery/metadata-server.d.ts.map +1 -1
- package/dist/discovery/peer-lookup.d.ts.map +1 -1
- package/dist/discovery/peer-lookup.js +9 -2
- package/dist/discovery/peer-lookup.js.map +1 -1
- package/dist/discovery/service-catalog.js.map +1 -1
- package/dist/health/gas-auto-refuel.d.ts.map +1 -1
- package/dist/health/gas-auto-refuel.js.map +1 -1
- package/dist/health/gas-health-monitor.d.ts.map +1 -1
- package/dist/health/model-health-checker.d.ts.map +1 -1
- package/dist/health/model-health-checker.js.map +1 -1
- package/dist/health/self-health-error.d.ts +6 -0
- package/dist/health/self-health-error.d.ts.map +1 -1
- package/dist/health/self-health-error.js +33 -0
- package/dist/health/self-health-error.js.map +1 -1
- package/dist/health/service-performance.d.ts.map +1 -1
- package/dist/health/service-performance.js.map +1 -1
- package/dist/health/stream-outcome.d.ts.map +1 -1
- package/dist/index.d.ts +5 -4
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -1
- package/dist/index.js.map +1 -1
- package/dist/metering/receipt-generator.d.ts.map +1 -1
- package/dist/metering/receipt-verifier.d.ts.map +1 -1
- package/dist/metering/seller-session-tracker.d.ts.map +1 -1
- package/dist/metering/session-tracker.d.ts.map +1 -1
- package/dist/metering/storage.d.ts +6 -0
- package/dist/metering/storage.d.ts.map +1 -1
- package/dist/metering/storage.js +8 -0
- package/dist/metering/storage.js.map +1 -1
- package/dist/metering/usage-aggregator.d.ts.map +1 -1
- package/dist/metering/usage-aggregator.js.map +1 -1
- package/dist/node.d.ts +21 -2
- package/dist/node.d.ts.map +1 -1
- package/dist/node.js +34 -16
- package/dist/node.js.map +1 -1
- package/dist/p2p/connection-auth.d.ts.map +1 -1
- package/dist/p2p/connection-auth.js.map +1 -1
- package/dist/p2p/connection-manager.d.ts +26 -0
- package/dist/p2p/connection-manager.d.ts.map +1 -1
- package/dist/p2p/connection-manager.js +88 -6
- package/dist/p2p/connection-manager.js.map +1 -1
- package/dist/p2p/identity-envelope.js.map +1 -1
- package/dist/p2p/identity.d.ts.map +1 -1
- package/dist/p2p/identity.js.map +1 -1
- package/dist/p2p/inbound-idle-tracker.d.ts +13 -5
- package/dist/p2p/inbound-idle-tracker.d.ts.map +1 -1
- package/dist/p2p/inbound-idle-tracker.js +11 -4
- package/dist/p2p/inbound-idle-tracker.js.map +1 -1
- package/dist/p2p/keepalive.d.ts.map +1 -1
- package/dist/p2p/nat-traversal.d.ts.map +1 -1
- package/dist/p2p/secure-channel.d.ts.map +1 -1
- package/dist/p2p/secure-channel.js.map +1 -1
- package/dist/p2p/sweep-mux.d.ts.map +1 -1
- package/dist/payments/balance-manager.d.ts.map +1 -1
- package/dist/payments/channel-store.d.ts +2 -0
- package/dist/payments/channel-store.d.ts.map +1 -1
- package/dist/payments/channel-store.js +9 -0
- package/dist/payments/channel-store.js.map +1 -1
- package/dist/payments/contract-stack.d.ts.map +1 -1
- package/dist/payments/contract-stack.js.map +1 -1
- package/dist/payments/deposit-relayer.d.ts.map +1 -1
- package/dist/payments/evm/ants-token-client.d.ts.map +1 -1
- package/dist/payments/evm/block-range.js.map +1 -1
- package/dist/payments/evm/deposit-relay-client.d.ts.map +1 -1
- package/dist/payments/evm/emissions-client.d.ts.map +1 -1
- package/dist/payments/evm/emissions-gate-client.d.ts +10 -10
- package/dist/payments/evm/emissions-gate-client.d.ts.map +1 -1
- package/dist/payments/evm/free-usage-client.d.ts.map +1 -1
- package/dist/payments/evm/identity-client.d.ts.map +1 -1
- package/dist/payments/evm/multicall.js.map +1 -1
- package/dist/payments/evm/points-policy-registry-client.d.ts.map +1 -1
- package/dist/payments/evm/position-init-client.d.ts.map +1 -1
- package/dist/payments/evm/registry-client.d.ts.map +1 -1
- package/dist/payments/evm/seller-pools-client.d.ts.map +1 -1
- package/dist/payments/evm/seller-pools-rewards-client.d.ts.map +1 -1
- package/dist/payments/evm/seller-registry-client.d.ts.map +1 -1
- package/dist/payments/evm/seller-registry-client.js.map +1 -1
- package/dist/payments/evm/seller-rewards-pool-client.d.ts.map +1 -1
- package/dist/payments/evm/staking-client.d.ts.map +1 -1
- package/dist/payments/evm/stats-client.d.ts.map +1 -1
- package/dist/payments/evm/trust-signals-client.d.ts.map +1 -1
- package/dist/payments/evm/trust-signals-client.js.map +1 -1
- package/dist/payments/evm/usage-accounting-client.d.ts.map +1 -1
- package/dist/payments/evm/usage-rewards-client.d.ts.map +1 -1
- package/dist/payments/evm/usdc-eth-swap-client.d.ts.map +1 -1
- package/dist/payments/evm/verification-signals-client.d.ts.map +1 -1
- package/dist/payments/evm/verification-signals-client.js.map +1 -1
- package/dist/payments/evm/verifier-client.d.ts +2 -2
- package/dist/payments/evm/verifier-client.d.ts.map +1 -1
- package/dist/payments/evm/verifier-client.js.map +1 -1
- package/dist/payments/evm/wash-trading-registry-client.d.ts.map +1 -1
- package/dist/payments/evm/wash-trading-registry-client.js.map +1 -1
- package/dist/payments/free-tier-conversations.d.ts +59 -0
- package/dist/payments/free-tier-conversations.d.ts.map +1 -0
- package/dist/payments/free-tier-conversations.js +127 -0
- package/dist/payments/free-tier-conversations.js.map +1 -0
- package/dist/payments/free-tier-program.js.map +1 -1
- package/dist/payments/free-tier-stake-resolver.d.ts.map +1 -1
- package/dist/payments/free-tier-stake-resolver.js.map +1 -1
- package/dist/payments/generated-contract-addresses.d.ts +33 -33
- package/dist/payments/generated-contract-addresses.d.ts.map +1 -1
- package/dist/payments/index.d.ts +4 -4
- package/dist/payments/index.d.ts.map +1 -1
- package/dist/payments/index.js +2 -2
- package/dist/payments/index.js.map +1 -1
- package/dist/payments/reward-claims.js.map +1 -1
- package/dist/payments/rpc-health.d.ts.map +1 -1
- package/dist/payments/seller-buyer-limiter.d.ts.map +1 -1
- package/dist/payments/seller-buyer-limiter.js.map +1 -1
- package/dist/payments/seller-free-tier-limiter.d.ts +54 -0
- package/dist/payments/seller-free-tier-limiter.d.ts.map +1 -1
- package/dist/payments/seller-free-tier-limiter.js +89 -2
- package/dist/payments/seller-free-tier-limiter.js.map +1 -1
- package/dist/payments/seller-free-usage-manager.d.ts.map +1 -1
- package/dist/payments/seller-free-usage-manager.js.map +1 -1
- package/dist/payments/seller-payment-manager.d.ts +37 -4
- package/dist/payments/seller-payment-manager.d.ts.map +1 -1
- package/dist/payments/seller-payment-manager.js +155 -46
- package/dist/payments/seller-payment-manager.js.map +1 -1
- package/dist/reputation/identity-history.d.ts.map +1 -1
- package/dist/reputation/identity-history.js.map +1 -1
- package/dist/reputation/model-reputation.js.map +1 -1
- package/dist/reputation/public-json.js.map +1 -1
- package/dist/reputation/rating-manager.d.ts.map +1 -1
- package/dist/reputation/report-manager.d.ts.map +1 -1
- package/dist/reputation/sybil-risk.js.map +1 -1
- package/dist/reputation/trust-score.d.ts.map +1 -1
- package/dist/reputation/uptime-tracker.d.ts.map +1 -1
- package/dist/requested-service.d.ts +4 -0
- package/dist/requested-service.d.ts.map +1 -1
- package/dist/requested-service.js +19 -1
- package/dist/requested-service.js.map +1 -1
- package/dist/resources/resource-ownership-store.d.ts.map +1 -1
- package/dist/resources/resource-ownership-store.js.map +1 -1
- package/dist/routing/default-router.d.ts.map +1 -1
- package/dist/routing/model-route-ranking.d.ts +12 -2
- package/dist/routing/model-route-ranking.d.ts.map +1 -1
- package/dist/routing/model-route-ranking.js +74 -9
- package/dist/routing/model-route-ranking.js.map +1 -1
- package/dist/routing/seller-health.d.ts.map +1 -1
- package/dist/routing/seller-health.js.map +1 -1
- package/dist/seller-request-handler.d.ts +24 -2
- package/dist/seller-request-handler.d.ts.map +1 -1
- package/dist/seller-request-handler.js +278 -79
- package/dist/seller-request-handler.js.map +1 -1
- package/dist/utils/file-lock.js.map +1 -1
- package/dist/utils/response-body.d.ts +33 -0
- package/dist/utils/response-body.d.ts.map +1 -0
- package/dist/utils/response-body.js +191 -0
- package/dist/utils/response-body.js.map +1 -0
- package/dist/utils/response-headers.d.ts +4 -0
- package/dist/utils/response-headers.d.ts.map +1 -0
- package/dist/utils/response-headers.js +19 -0
- package/dist/utils/response-headers.js.map +1 -0
- package/dist/verification/samples.d.ts.map +1 -1
- package/dist/verification/samples.js.map +1 -1
- package/dist/verification/storage.d.ts.map +1 -1
- package/dist/verification/tee-status.d.ts +1 -1
- package/dist/verification/tee-status.d.ts.map +1 -1
- package/package.json +5 -5
|
@@ -149,6 +149,8 @@ export class SellerPaymentManager {
|
|
|
149
149
|
_topUpRestoreDeferred = new Set();
|
|
150
150
|
/** Channels with a background topUp() running, and those asked for another meanwhile. */
|
|
151
151
|
_topUpsRunning = new Map();
|
|
152
|
+
/** SpendingAuths parked above the ceiling until a mining top-up lands, per channel. */
|
|
153
|
+
_overCeilingAuths = new Map();
|
|
152
154
|
/** Set once a stopping node closed its connections: nothing finished after it reaches a buyer. */
|
|
153
155
|
_connectionsClosed = false;
|
|
154
156
|
_topUpsWanted = new Set();
|
|
@@ -247,6 +249,10 @@ export class SellerPaymentManager {
|
|
|
247
249
|
metadata: channel.latestMetadata ?? '',
|
|
248
250
|
});
|
|
249
251
|
}
|
|
252
|
+
else {
|
|
253
|
+
// Only an amount the buyer signed a SpendingAuth for counts as accepted.
|
|
254
|
+
this._acceptedCumulative.set(channel.sessionId, 0n);
|
|
255
|
+
}
|
|
250
256
|
}
|
|
251
257
|
for (const channel of this._channelStore.getActiveChannels(CHANNEL_ROLE.SELLER, CHANNEL_KIND.ONE_OFF)) {
|
|
252
258
|
this._oneOffChannelIds.add(channel.sessionId);
|
|
@@ -264,6 +270,9 @@ export class SellerPaymentManager {
|
|
|
264
270
|
metadata: channel.latestMetadata ?? '',
|
|
265
271
|
});
|
|
266
272
|
}
|
|
273
|
+
else {
|
|
274
|
+
this._acceptedCumulative.set(channel.sessionId, 0n);
|
|
275
|
+
}
|
|
267
276
|
}
|
|
268
277
|
}
|
|
269
278
|
/**
|
|
@@ -369,10 +378,18 @@ export class SellerPaymentManager {
|
|
|
369
378
|
* submitted, not the one they asked for — persist that, not `amount`.
|
|
370
379
|
*/
|
|
371
380
|
_submitClose(channelId, amount, submit) {
|
|
381
|
+
return this._registerClose(channelId, () => submit().then((txHash) => ({ closed: true, amount, txHash }), (error) => ({ closed: false, error })));
|
|
382
|
+
}
|
|
383
|
+
/**
|
|
384
|
+
* Run `start` as the channel's one close operation, or join the one in
|
|
385
|
+
* flight. While it is registered, the channel counts as closing: requests
|
|
386
|
+
* wait for it and a retained channel is not reactivated.
|
|
387
|
+
*/
|
|
388
|
+
_registerClose(channelId, start) {
|
|
372
389
|
const existing = this._closingChannels.get(channelId);
|
|
373
390
|
if (existing)
|
|
374
391
|
return existing;
|
|
375
|
-
const operation =
|
|
392
|
+
const operation = start();
|
|
376
393
|
this._closingChannels.set(channelId, operation);
|
|
377
394
|
void operation.finally(() => {
|
|
378
395
|
if (this._closingChannels.get(channelId) === operation) {
|
|
@@ -514,18 +531,79 @@ export class SellerPaymentManager {
|
|
|
514
531
|
}
|
|
515
532
|
return outcome;
|
|
516
533
|
};
|
|
517
|
-
|
|
534
|
+
const first = await runLocked(true);
|
|
535
|
+
if (first !== 'awaiting-top-up')
|
|
536
|
+
return first;
|
|
518
537
|
// Only a top-up that is mining covers this auth: wait for it with the
|
|
519
538
|
// lock released, so this buyer's other auths and requests keep moving,
|
|
520
539
|
// then judge the auth again against the new ceiling. The one that lands
|
|
521
540
|
// can be a lower one, with the top-up this auth needs queued behind it:
|
|
522
541
|
// keep waiting, within the same budget.
|
|
523
542
|
const deadline = Date.now() + OVER_CEILING_AUTH_WAIT_MS;
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
result
|
|
543
|
+
const parked = (async () => {
|
|
544
|
+
let result = first;
|
|
545
|
+
while (result === 'awaiting-top-up') {
|
|
546
|
+
await this.waitForTopUp(payload.channelId, Math.max(0, deadline - Date.now()), BigInt(payload.cumulativeAmount));
|
|
547
|
+
result = await runLocked(Date.now() < deadline);
|
|
548
|
+
}
|
|
549
|
+
return result;
|
|
550
|
+
})();
|
|
551
|
+
// A close must not settle below an auth the buyer already signed: it
|
|
552
|
+
// waits for the parked ones (see _waitForOverCeilingAuths).
|
|
553
|
+
const channelKey = payload.channelId;
|
|
554
|
+
let set = this._overCeilingAuths.get(channelKey);
|
|
555
|
+
if (!set)
|
|
556
|
+
this._overCeilingAuths.set(channelKey, set = new Set());
|
|
557
|
+
set.add(parked);
|
|
558
|
+
try {
|
|
559
|
+
return await parked;
|
|
560
|
+
}
|
|
561
|
+
finally {
|
|
562
|
+
set.delete(parked);
|
|
563
|
+
if (set.size === 0 && this._overCeilingAuths.get(channelKey) === set)
|
|
564
|
+
this._overCeilingAuths.delete(channelKey);
|
|
565
|
+
}
|
|
566
|
+
}
|
|
567
|
+
/**
|
|
568
|
+
* Close a channel whose buyer has SpendingAuths parked behind a mining
|
|
569
|
+
* top-up: wait for them to be judged, then close at the latest accepted
|
|
570
|
+
* auth. Runs as the registered close operation, so no new request is
|
|
571
|
+
* admitted on the channel while it waits.
|
|
572
|
+
*/
|
|
573
|
+
async _closeAfterParkedAuths(channelId, retries) {
|
|
574
|
+
await this._waitForOverCeilingAuths(channelId, OVER_CEILING_AUTH_WAIT_MS);
|
|
575
|
+
const { amount, metadata, sig } = this._getSettleParams(channelId);
|
|
576
|
+
debugLog(`[SellerPayment] Closing channel ${channelId.slice(0, 18)}... cumulative=${amount} after parked auths (attempt ${retries + 1}/${SellerPaymentManager.MAX_CLOSE_RETRIES})`);
|
|
577
|
+
try {
|
|
578
|
+
const txHash = await this._channelsClient.close(this._signer, channelId, amount, metadata, sig);
|
|
579
|
+
return { closed: true, amount, txHash };
|
|
580
|
+
}
|
|
581
|
+
catch (error) {
|
|
582
|
+
return { closed: false, error };
|
|
583
|
+
}
|
|
584
|
+
}
|
|
585
|
+
/**
|
|
586
|
+
* Wait (up to `timeoutMs`) for the SpendingAuths parked above the ceiling
|
|
587
|
+
* of a channel until a mining top-up lands. A close on buyer disconnect
|
|
588
|
+
* runs while a top-up is mining; settling before those auths are accepted
|
|
589
|
+
* would close at the old ceiling and leave delivered work unpaid.
|
|
590
|
+
*/
|
|
591
|
+
async _waitForOverCeilingAuths(channelId, timeoutMs) {
|
|
592
|
+
const deadline = Date.now() + timeoutMs;
|
|
593
|
+
for (;;) {
|
|
594
|
+
const parked = this._overCeilingAuths.get(channelId);
|
|
595
|
+
const remaining = deadline - Date.now();
|
|
596
|
+
if (!parked || parked.size === 0 || remaining <= 0)
|
|
597
|
+
return;
|
|
598
|
+
let timer;
|
|
599
|
+
const timedOut = new Promise((resolve) => { timer = setTimeout(resolve, remaining); });
|
|
600
|
+
try {
|
|
601
|
+
await Promise.race([Promise.allSettled([...parked]), timedOut]);
|
|
602
|
+
}
|
|
603
|
+
finally {
|
|
604
|
+
clearTimeout(timer);
|
|
605
|
+
}
|
|
527
606
|
}
|
|
528
|
-
return result;
|
|
529
607
|
}
|
|
530
608
|
/**
|
|
531
609
|
* Wait for any in-flight SpendingAuth processing for this buyer to complete.
|
|
@@ -562,10 +640,17 @@ export class SellerPaymentManager {
|
|
|
562
640
|
const { channels: channelsAddr } = await this._resolvedAddresses;
|
|
563
641
|
const channelsDomain = makeChannelsDomain(this._config.chainId, channelsAddr);
|
|
564
642
|
if (existingCumulative === undefined) {
|
|
643
|
+
// Hex in another case names the same on-chain channel: it must not read
|
|
644
|
+
// as an unknown channel, which would start over with nothing spent.
|
|
645
|
+
const sameChannel = this._channelStore.getChannelIgnoringCase(channelId);
|
|
646
|
+
if (sameChannel && sameChannel.sessionId !== channelId) {
|
|
647
|
+
debugWarn(`[SellerPayment] Rejecting SpendingAuth naming channel ${sameChannel.sessionId.slice(0, 18)}... in another case`);
|
|
648
|
+
return 'rejected';
|
|
649
|
+
}
|
|
565
650
|
// A channel this seller already closed stays closed: a lagging RPC
|
|
566
651
|
// node can still report it active, and recovering it would block the
|
|
567
652
|
// buyer's replacement channel. No chain read is needed to say so.
|
|
568
|
-
if (
|
|
653
|
+
if (sameChannel?.status === CHANNEL_STATUS.SETTLED) {
|
|
569
654
|
debugLog(`[SellerPayment] SpendingAuth for closed channel ${channelId.slice(0, 18)}... — rejected`);
|
|
570
655
|
return 'rejected';
|
|
571
656
|
}
|
|
@@ -603,7 +688,7 @@ export class SellerPaymentManager {
|
|
|
603
688
|
}
|
|
604
689
|
}
|
|
605
690
|
const reserveSalt = payload.reserveSalt ?? channelId;
|
|
606
|
-
await this._reserveWithBackpressureRetry(channelId, () => this._channelsClient.reserve(this._signer, buyerEvmAddr, reserveSalt, reserveMaxAmount, BigInt(reserveDeadline), payload.spendingAuthSig));
|
|
691
|
+
await this._reserveWithBackpressureRetry(buyerPeerId, channelId, () => this._channelsClient.reserve(this._signer, buyerEvmAddr, reserveSalt, reserveMaxAmount, BigInt(reserveDeadline), payload.spendingAuthSig));
|
|
607
692
|
// Store new session (sessionId field stores channelId for backward compat)
|
|
608
693
|
const now = Date.now();
|
|
609
694
|
const { seller: sellerEvmAddr } = await this._resolvedAddresses;
|
|
@@ -614,7 +699,7 @@ export class SellerPaymentManager {
|
|
|
614
699
|
sellerEvmAddr,
|
|
615
700
|
buyerEvmAddr,
|
|
616
701
|
nonce: 0,
|
|
617
|
-
authMax:
|
|
702
|
+
authMax: '0',
|
|
618
703
|
previousConsumption: reserveMaxAmount.toString(), // repurposed: stores reserveMax
|
|
619
704
|
deadline: reserveDeadline,
|
|
620
705
|
previousSessionId: '',
|
|
@@ -1022,7 +1107,7 @@ export class SellerPaymentManager {
|
|
|
1022
1107
|
this._pendingOneOffPlans.delete(planKey);
|
|
1023
1108
|
// Counted before the reserve goes out, so a parallel create sees it.
|
|
1024
1109
|
this._unacceptedOneOffs.set(channelId, buyerPeerId);
|
|
1025
|
-
await this._reserveWithBackpressureRetry(channelId, () => this._channelsClient.reserve(this._signer, buyerEvmAddr, payload.reserveSalt, openingReserve, BigInt(reserveDeadline), payload.spendingAuthSig));
|
|
1110
|
+
await this._reserveWithBackpressureRetry(buyerPeerId, channelId, () => this._channelsClient.reserve(this._signer, buyerEvmAddr, payload.reserveSalt, openingReserve, BigInt(reserveDeadline), payload.spendingAuthSig));
|
|
1026
1111
|
const now = Date.now();
|
|
1027
1112
|
const { seller: sellerEvmAddr } = await this._resolvedAddresses;
|
|
1028
1113
|
if (batch) {
|
|
@@ -1228,7 +1313,7 @@ export class SellerPaymentManager {
|
|
|
1228
1313
|
* retries let a rejected submission land as soon as the provider's account
|
|
1229
1314
|
* queue has room, without holding unrelated buyers until a receipt is mined.
|
|
1230
1315
|
*/
|
|
1231
|
-
async _reserveWithBackpressureRetry(channelId, submit) {
|
|
1316
|
+
async _reserveWithBackpressureRetry(buyerPeerId, channelId, submit) {
|
|
1232
1317
|
for (let attempt = 0;; attempt += 1) {
|
|
1233
1318
|
try {
|
|
1234
1319
|
debugLog(`[SellerPayment] Reserving channel ${channelId.slice(0, 18)}... on-chain` +
|
|
@@ -1238,6 +1323,7 @@ export class SellerPaymentManager {
|
|
|
1238
1323
|
catch (err) {
|
|
1239
1324
|
const delayMs = RESERVE_BACKPRESSURE_RETRY_DELAYS_MS[attempt];
|
|
1240
1325
|
if (!this._isRetryableTxSubmissionFailure(err) || delayMs === undefined) {
|
|
1326
|
+
this._config.onReserveFailed?.({ buyerPeerId, channelId, error: this._formatError(err) });
|
|
1241
1327
|
throw err;
|
|
1242
1328
|
}
|
|
1243
1329
|
debugWarn(`[SellerPayment] Reserve hit transaction backpressure for ${channelId.slice(0, 18)}... ` +
|
|
@@ -1823,8 +1909,12 @@ export class SellerPaymentManager {
|
|
|
1823
1909
|
* - settleOnly=false (default): calls close() — charges buyer, credits seller, ends channel.
|
|
1824
1910
|
* - settleOnly=true: calls settle() — charges buyer, credits seller, keeps channel open
|
|
1825
1911
|
* for future requests. No cleanup is performed so the session can resume.
|
|
1912
|
+
* - onlyIfIdle: does nothing while a request is being served, which means the
|
|
1913
|
+
* channel is not idle. Finishing that request re-arms the idle timers.
|
|
1826
1914
|
*/
|
|
1827
|
-
async settleSession(buyerPeerId, { cleanupOnFailure = false, settleOnly = false } = {}) {
|
|
1915
|
+
async settleSession(buyerPeerId, { cleanupOnFailure = false, settleOnly = false, onlyIfIdle = false } = {}) {
|
|
1916
|
+
if (onlyIfIdle && this.hasInFlightRequests(buyerPeerId))
|
|
1917
|
+
return;
|
|
1828
1918
|
const session = this._channelStore.getActiveChannelByPeer(buyerPeerId, CHANNEL_ROLE.SELLER);
|
|
1829
1919
|
if (!session) {
|
|
1830
1920
|
debugWarn(`[SellerPayment] settleSession: no active session for buyer ${buyerPeerId.slice(0, 12)}...`);
|
|
@@ -1836,49 +1926,58 @@ export class SellerPaymentManager {
|
|
|
1836
1926
|
if (accepted === 0n) {
|
|
1837
1927
|
if (settleOnly)
|
|
1838
1928
|
return;
|
|
1929
|
+
// A channel with nothing signed stays open for the timeout checker: it keeps
|
|
1930
|
+
// its spend, so a buyer that comes back still owes what it was served.
|
|
1839
1931
|
debugLog(`[SellerPayment] Zero-cumulative channel ${channelId.slice(0, 18)}... — deferring to timeout checker`);
|
|
1932
|
+
this._deactivateBuyerForChannel(buyerPeerId, channelId);
|
|
1933
|
+
return;
|
|
1840
1934
|
}
|
|
1841
|
-
|
|
1935
|
+
if (settleOnly) {
|
|
1842
1936
|
await this._settleLatestAuth(channelId, 'idle settle', { respectMinSettleDelta: true });
|
|
1843
1937
|
return;
|
|
1844
1938
|
}
|
|
1939
|
+
const retries = this._closeRetryCount.get(channelId) ?? 0;
|
|
1940
|
+
if (retries >= SellerPaymentManager.MAX_CLOSE_RETRIES) {
|
|
1941
|
+
debugWarn(`[SellerPayment] close() failed ${retries} times for ${channelId.slice(0, 18)}... — falling back to timeout path`);
|
|
1942
|
+
this._channelStore.updateChannelStatus(channelId, CHANNEL_STATUS.TIMEOUT);
|
|
1943
|
+
}
|
|
1845
1944
|
else {
|
|
1846
|
-
|
|
1847
|
-
|
|
1848
|
-
|
|
1849
|
-
|
|
1945
|
+
// Auths parked behind a mining top-up are part of what the buyer
|
|
1946
|
+
// signed: close only once they are judged (_closeAfterParkedAuths).
|
|
1947
|
+
const hasParkedAuths = this._overCeilingAuths.has(channelId);
|
|
1948
|
+
if (!hasParkedAuths) {
|
|
1949
|
+
debugLog(`[SellerPayment] Closing channel ${channelId.slice(0, 18)}... cumulative=${amount} (attempt ${retries + 1}/${SellerPaymentManager.MAX_CLOSE_RETRIES})`);
|
|
1950
|
+
}
|
|
1951
|
+
const closeResult = await (hasParkedAuths
|
|
1952
|
+
? this._registerClose(channelId, () => this._closeAfterParkedAuths(channelId, retries))
|
|
1953
|
+
: this._submitClose(channelId, amount, () => this._channelsClient.close(this._signer, channelId, amount, metadata, sig)));
|
|
1954
|
+
if (closeResult.closed) {
|
|
1955
|
+
this._channelStore.updateChannelStatus(channelId, CHANNEL_STATUS.SETTLED, closeResult.amount.toString());
|
|
1956
|
+
this._closeRetryCount.delete(channelId);
|
|
1850
1957
|
}
|
|
1851
1958
|
else {
|
|
1852
|
-
|
|
1853
|
-
|
|
1854
|
-
|
|
1855
|
-
|
|
1856
|
-
|
|
1857
|
-
}
|
|
1858
|
-
else {
|
|
1859
|
-
if (this._isRetryableTxSubmissionFailure(closeResult.error)) {
|
|
1860
|
-
debugWarn(`[SellerPayment] Close hit transaction backpressure for ${channelId.slice(0, 18)}... ` +
|
|
1861
|
-
`— keeping channel state for retry: ${this._formatError(closeResult.error)}`);
|
|
1862
|
-
if (cleanupOnFailure) {
|
|
1863
|
-
this._deactivateBuyerForChannel(buyerPeerId, channelId);
|
|
1864
|
-
}
|
|
1865
|
-
return;
|
|
1959
|
+
if (this._isRetryableTxSubmissionFailure(closeResult.error)) {
|
|
1960
|
+
debugWarn(`[SellerPayment] Close hit transaction backpressure for ${channelId.slice(0, 18)}... ` +
|
|
1961
|
+
`— keeping channel state for retry: ${this._formatError(closeResult.error)}`);
|
|
1962
|
+
if (cleanupOnFailure) {
|
|
1963
|
+
this._deactivateBuyerForChannel(buyerPeerId, channelId);
|
|
1866
1964
|
}
|
|
1867
|
-
|
|
1868
|
-
|
|
1869
|
-
|
|
1870
|
-
|
|
1871
|
-
|
|
1872
|
-
|
|
1873
|
-
|
|
1874
|
-
|
|
1965
|
+
return;
|
|
1966
|
+
}
|
|
1967
|
+
const failedAttempts = retries + 1;
|
|
1968
|
+
debugWarn(`[SellerPayment] Failed to close channel (attempt ${failedAttempts}): ${this._formatError(closeResult.error)}`);
|
|
1969
|
+
this._closeRetryCount.set(channelId, failedAttempts);
|
|
1970
|
+
if (failedAttempts < SellerPaymentManager.MAX_CLOSE_RETRIES) {
|
|
1971
|
+
if (cleanupOnFailure) {
|
|
1972
|
+
this._deactivateBuyerForChannel(buyerPeerId, channelId);
|
|
1875
1973
|
}
|
|
1876
|
-
|
|
1877
|
-
this._channelStore.updateChannelStatus(channelId, CHANNEL_STATUS.TIMEOUT);
|
|
1974
|
+
return;
|
|
1878
1975
|
}
|
|
1976
|
+
debugWarn(`[SellerPayment] close() failed ${failedAttempts} times for ${channelId.slice(0, 18)}... — falling back to timeout path`);
|
|
1977
|
+
this._channelStore.updateChannelStatus(channelId, CHANNEL_STATUS.TIMEOUT);
|
|
1879
1978
|
}
|
|
1880
1979
|
}
|
|
1881
|
-
// Clean up maps after successful close
|
|
1980
|
+
// Clean up maps after successful close or exhausted retries
|
|
1882
1981
|
this._forgetChannel(channelId, buyerPeerId);
|
|
1883
1982
|
}
|
|
1884
1983
|
/**
|
|
@@ -1960,7 +2059,9 @@ export class SellerPaymentManager {
|
|
|
1960
2059
|
return;
|
|
1961
2060
|
this._buyerDisconnectMarkers.set(buyerPeerId, {});
|
|
1962
2061
|
const settleOnDisconnect = this._config.settleOnDisconnect ?? true;
|
|
1963
|
-
|
|
2062
|
+
// A request still being served has yet to record its spend on this channel:
|
|
2063
|
+
// keep it, and let the timeout checker close it once that is done.
|
|
2064
|
+
if (settleOnDisconnect && !this.hasInFlightRequests(buyerPeerId)) {
|
|
1964
2065
|
const accepted = this._acceptedCumulative.get(session.sessionId) ?? 0n;
|
|
1965
2066
|
if (accepted > 0n) {
|
|
1966
2067
|
debugLog(`[SellerPayment] Buyer ${buyerPeerId.slice(0, 12)}... disconnected — closing channel immediately`);
|
|
@@ -2012,6 +2113,9 @@ export class SellerPaymentManager {
|
|
|
2012
2113
|
const hydratedZeroAuthExpired = accepted === 0n
|
|
2013
2114
|
&& this._hydratedChannelIds.has(channel.sessionId)
|
|
2014
2115
|
&& nowSecs > channel.deadline;
|
|
2116
|
+
// Requests still being served bill this channel: a later pass closes it.
|
|
2117
|
+
if (this.hasInFlightRequests(channel.peerId))
|
|
2118
|
+
continue;
|
|
2015
2119
|
// If we have auths and the buyer is disconnected, try to close
|
|
2016
2120
|
if (accepted > 0n && buyerDisconnected) {
|
|
2017
2121
|
debugLog(`[SellerPayment] Channel ${channel.sessionId.slice(0, 18)}... buyer disconnected — attempting close`);
|
|
@@ -2123,6 +2227,11 @@ export class SellerPaymentManager {
|
|
|
2123
2227
|
// replacement once they are done.
|
|
2124
2228
|
if (this.hasInFlightRequests(channel.peerId))
|
|
2125
2229
|
return false;
|
|
2230
|
+
// Auths parked behind a mining top-up need the buyer lock this reserve
|
|
2231
|
+
// holds, and the close must wait for them: closing here would deadlock
|
|
2232
|
+
// or settle below what the buyer signed. The buyer retries once they land.
|
|
2233
|
+
if (this._overCeilingAuths.has(channelId))
|
|
2234
|
+
return false;
|
|
2126
2235
|
const accepted = this._acceptedCumulative.get(channelId)
|
|
2127
2236
|
?? this._restorePersistedSpendingAuth(channel)
|
|
2128
2237
|
?? 0n;
|
|
@@ -2555,8 +2664,8 @@ export class SellerPaymentManager {
|
|
|
2555
2664
|
}
|
|
2556
2665
|
/**
|
|
2557
2666
|
* Take the buyer-supplied auth as our latest when it authorizes strictly more
|
|
2558
|
-
* than
|
|
2559
|
-
* doesn't lose the higher claim.
|
|
2667
|
+
* than the latest auth in memory. Persisted, its amount with its signature and
|
|
2668
|
+
* metadata, so a crash between here and close() doesn't lose the higher claim.
|
|
2560
2669
|
*/
|
|
2561
2670
|
_adoptBuyerCloseAuth(channelId, buyerAuth) {
|
|
2562
2671
|
const current = this._latestAuth.get(channelId);
|
|
@@ -2571,7 +2680,7 @@ export class SellerPaymentManager {
|
|
|
2571
2680
|
}
|
|
2572
2681
|
const stored = this._channelStore.getChannel(channelId);
|
|
2573
2682
|
if (stored) {
|
|
2574
|
-
stored.authMax =
|
|
2683
|
+
stored.authMax = buyerAuth.cumulativeAmount.toString();
|
|
2575
2684
|
stored.latestBuyerSig = buyerAuth.spendingAuthSig;
|
|
2576
2685
|
stored.latestSpendingAuthSig = buyerAuth.spendingAuthSig;
|
|
2577
2686
|
stored.latestMetadata = buyerAuth.metadata;
|