@integraledger/profile-facilitator 0.5.1 → 0.6.0
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/CHANGELOG.md +449 -0
- package/README.md +551 -47
- package/dist/core.d.ts +1 -1
- package/dist/core.js +683 -66
- package/dist/http.d.ts +19 -4
- package/dist/http.js +16 -6
- package/dist/module.d.ts +108 -21
- package/dist/module.js +39 -0
- package/dist/modules/casper/index.js +14 -2
- package/dist/modules/erc7710/index.d.ts +8 -5
- package/dist/modules/erc7710/index.js +1110 -33
- package/dist/modules/evm/escrow.d.ts +125 -0
- package/dist/modules/evm/escrow.js +619 -0
- package/dist/modules/evm/forms.d.ts +37 -9
- package/dist/modules/evm/forms.js +165 -21
- package/dist/modules/evm/index.d.ts +41 -6
- package/dist/modules/evm/index.js +1127 -18
- package/dist/modules/evm/node.d.ts +16 -1
- package/dist/modules/evm/node.js +181 -26
- package/dist/modules/evm/nonce-proof.d.ts +186 -0
- package/dist/modules/evm/nonce-proof.js +779 -0
- package/dist/modules/solana/index.d.ts +3 -0
- package/dist/modules/solana/index.js +94 -7
- package/dist/modules/solana/reader.d.ts +2 -1
- package/dist/modules/solana/reader.js +42 -4
- package/dist/modules/solana/upto-claim.d.ts +58 -0
- package/dist/modules/solana/upto-claim.js +269 -0
- package/dist/modules/stellar/index.d.ts +5 -0
- package/dist/modules/stellar/index.js +17 -7
- package/dist/modules/stellar/node.d.ts +7 -1
- package/dist/modules/stellar/node.js +51 -7
- package/dist/modules/sui/index.js +63 -18
- package/dist/modules/sui/node.d.ts +13 -0
- package/dist/modules/sui/node.js +60 -0
- package/dist/modules/ton/index.js +5 -1
- package/dist/modules/xrpl/index.js +60 -11
- package/dist/modules/xrpl/node.d.ts +31 -2
- package/dist/modules/xrpl/node.js +88 -17
- package/dist/modules/xrpl/upto.js +105 -19
- package/dist/record.d.ts +52 -8
- package/dist/record.js +39 -5
- package/dist/wrapper.d.ts +3 -1
- package/dist/wrapper.js +4 -2
- package/package.json +3 -3
package/dist/core.js
CHANGED
|
@@ -5,12 +5,14 @@
|
|
|
5
5
|
* holds no chain's rule and no key: each module's facilitator verifies and settles, and the core decides only what its
|
|
6
6
|
* record, its budget and lcp's read of the chain tell it.
|
|
7
7
|
*/
|
|
8
|
+
import { AsyncLocalStorage } from "node:async_hooks";
|
|
8
9
|
import { createHash, timingSafeEqual } from "node:crypto";
|
|
9
10
|
import { createServer } from "node:http";
|
|
10
11
|
import { x402Facilitator } from "@x402/core/facilitator";
|
|
11
12
|
import { failed, guarded, invalid, isObject, operatorLine, pending, readShape, } from "./answers.js";
|
|
12
13
|
import { NODE_TIMEOUT_MS, readRequestJson, sendJson, sleep, X402_REQUEST_MAX_BYTES } from "./http.js";
|
|
13
|
-
import {
|
|
14
|
+
import { namingSends, } from "./module.js";
|
|
15
|
+
import { answerReserve, feesOn, inTime, keepUntil, keyOf, moduleState, openPool, openRecord, sweeping, toStored, } from "./record.js";
|
|
14
16
|
import { settleReason, unexpectedSettle, unexpectedVerify } from "./unexpected.js";
|
|
15
17
|
import { declared, kindOf, modulesOf, route, unsound, Wrapper } from "./wrapper.js";
|
|
16
18
|
const DEFAULT_SETTLE_WAIT_MS = 30_000;
|
|
@@ -18,6 +20,14 @@ const DEFAULT_SETTLE_WAIT_MS = 30_000;
|
|
|
18
20
|
const WATCH_MS = 1_000;
|
|
19
21
|
/** RFC 6750's `b64token`, the characters a bearer credential may hold. */
|
|
20
22
|
const B64TOKEN = /^[A-Za-z0-9\-._~+/]+=*$/;
|
|
23
|
+
/**
|
|
24
|
+
* A `/settle` refused by the checks of `/verify`: their reason as a settle answer's, and their message, where they gave
|
|
25
|
+
* one, as x402's `errorMessage`, so the caller of `/settle` reads what the caller of `/verify` reads.
|
|
26
|
+
*/
|
|
27
|
+
function refusedAtSettle(v, network) {
|
|
28
|
+
const answer = failed(settleReason(v.invalidReason ?? "invalid_payload"), "", network);
|
|
29
|
+
return v.invalidMessage === undefined ? answer : { ...answer, errorMessage: v.invalidMessage };
|
|
30
|
+
}
|
|
21
31
|
/** The body's `paymentRequirements.network` when it is a string, else "". */
|
|
22
32
|
function networkOf(body) {
|
|
23
33
|
const requirements = isObject(body) ? body["paymentRequirements"] : undefined;
|
|
@@ -70,14 +80,65 @@ function canonical(v) {
|
|
|
70
80
|
const o = v;
|
|
71
81
|
return Object.fromEntries(Object.keys(o).sort().map((k) => [k, canonical(o[k])]));
|
|
72
82
|
}
|
|
73
|
-
/**
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
83
|
+
/**
|
|
84
|
+
* The digest of a payment's body: what its module states makes it one body (`Instrumented.body`), else its payload and
|
|
85
|
+
* its requirements as presented, whatever order their members came in.
|
|
86
|
+
*/
|
|
87
|
+
function bodyOf(p, i) {
|
|
88
|
+
const body = i.body === undefined ? { payload: p.payload, requirements: p.requirements } : { declared: toStored(i.body) };
|
|
89
|
+
return createHash("sha256").update(JSON.stringify(canonical(body))).digest("hex");
|
|
78
90
|
}
|
|
91
|
+
/**
|
|
92
|
+
* The longest the read made at once after a success on a receipt at the head may delay that success: the row is made
|
|
93
|
+
* final then where the finalized chain already shows it, and otherwise by a repeat's read.
|
|
94
|
+
*/
|
|
95
|
+
const AT_HEAD_READ_MS = 1_000;
|
|
96
|
+
/** The reason of the settler's own failure (`unexpectedSettle`), which a repeat asks again: never a refusal. */
|
|
97
|
+
const OWN_FAILURE = unexpectedSettle(() => { }, { event: "", network: "" }).errorReason;
|
|
79
98
|
/** A read the core could not make: nothing is known of the instrument. */
|
|
80
99
|
const UNREAD = { outcome: "live", answer: { state: "unreadable", reason: "transport", foreign: [] } };
|
|
100
|
+
/**
|
|
101
|
+
* The store calls made inside the class's settle of a submission (the module's state, the fee meter, the naming write):
|
|
102
|
+
* the first that failed, where one did. The class turns such a failure into a refusal carrying the database's text, made
|
|
103
|
+
* before anything was sent, which is no answer about the payment.
|
|
104
|
+
*/
|
|
105
|
+
const storeFaults = new AsyncLocalStorage();
|
|
106
|
+
/** `p`, a store call, its failure kept for the submission it is made in. */
|
|
107
|
+
function faulting(p) {
|
|
108
|
+
return p.catch((e) => {
|
|
109
|
+
const f = storeFaults.getStore();
|
|
110
|
+
if (f !== undefined && f.cause === undefined)
|
|
111
|
+
f.cause = e;
|
|
112
|
+
throw e;
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
/** A module's state whose failures inside a submission are kept for it (`storeFaults`). */
|
|
116
|
+
function watchedState(state) {
|
|
117
|
+
return {
|
|
118
|
+
get: (key) => faulting(state.get(key)),
|
|
119
|
+
set: (key, value) => faulting(state.set(key, value)),
|
|
120
|
+
swap: (key, expected, value) => faulting(state.swap(key, expected, value)),
|
|
121
|
+
delete: (key) => faulting(state.delete(key)),
|
|
122
|
+
pendingSettlements() {
|
|
123
|
+
const p = state.pendingSettlements();
|
|
124
|
+
return {
|
|
125
|
+
get: (key) => faulting(p.get(key)),
|
|
126
|
+
set: (key, txHash) => faulting(p.set(key, txHash)),
|
|
127
|
+
delete: (key) => faulting(p.delete(key)),
|
|
128
|
+
};
|
|
129
|
+
},
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
/** A fee meter whose `sending`, a store call, fails into `storeFaults` inside a submission. */
|
|
133
|
+
function watchedMeter(meter) {
|
|
134
|
+
return { ...meter, sending: (s) => faulting(meter.sending(s)), included: (i) => meter.included(i), sent: (t, shown) => meter.sent(t, shown) };
|
|
135
|
+
}
|
|
136
|
+
/** A naming write the claim's fencing number refused: another settle took the claim, or the row is final. */
|
|
137
|
+
class FencedOut extends Error {
|
|
138
|
+
constructor() {
|
|
139
|
+
super("the claim was taken by another settle, or is final: nothing is sent");
|
|
140
|
+
}
|
|
141
|
+
}
|
|
81
142
|
/**
|
|
82
143
|
* Starts the settler with the modules of `list` that `c` names. The public start function passes the settler's own
|
|
83
144
|
* list; a test passes one of its own.
|
|
@@ -106,6 +167,12 @@ export async function serveSettler(c, list) {
|
|
|
106
167
|
const offered = [];
|
|
107
168
|
// The modules started with the fee meter of their network: their keys' fees are counted as they send.
|
|
108
169
|
const metered = new Set();
|
|
170
|
+
// The submission whose class's settle is running, for a module that names its transaction before it sends it.
|
|
171
|
+
const inSubmission = new AsyncLocalStorage();
|
|
172
|
+
const naming = (transaction) => {
|
|
173
|
+
const keep = inSubmission.getStore();
|
|
174
|
+
return keep === undefined ? Promise.reject(new Error("naming is called outside a submission's settle")) : keep(transaction);
|
|
175
|
+
};
|
|
109
176
|
for (const listed of list) {
|
|
110
177
|
const own = configured[listed.name];
|
|
111
178
|
if (own === undefined)
|
|
@@ -115,11 +182,12 @@ export async function serveSettler(c, list) {
|
|
|
115
182
|
const core = {
|
|
116
183
|
log,
|
|
117
184
|
settleWaitMs,
|
|
118
|
-
state: moduleState(pool, listed.name),
|
|
185
|
+
state: watchedState(moduleState(pool, listed.name)),
|
|
119
186
|
meter: (network) => {
|
|
120
187
|
meters.add(network);
|
|
121
|
-
return fees.meter(network, () => Date.now(), log);
|
|
188
|
+
return watchedMeter(fees.meter(network, () => Date.now(), log));
|
|
122
189
|
},
|
|
190
|
+
naming,
|
|
123
191
|
};
|
|
124
192
|
const started = await starter.start(own, core);
|
|
125
193
|
if (started.close !== undefined)
|
|
@@ -234,7 +302,8 @@ export async function serveSettler(c, list) {
|
|
|
234
302
|
}
|
|
235
303
|
}
|
|
236
304
|
/**
|
|
237
|
-
* Counts the fee of a transaction the attempt's published class sent,
|
|
305
|
+
* Counts the fee of a transaction the attempt's published class sent, or a module named on a repeat before it sent it
|
|
306
|
+
* (`resent`), where its network has a budget. It is asked
|
|
238
307
|
* once the answer is stored, on its own bounds, since the class's settle runs on its own and can outlast the
|
|
239
308
|
* request.
|
|
240
309
|
*/
|
|
@@ -263,10 +332,18 @@ export async function serveSettler(c, list) {
|
|
|
263
332
|
log({ ...about(a, { transaction }), event: "store-call-failed", call: "sent" }, e);
|
|
264
333
|
}
|
|
265
334
|
}
|
|
266
|
-
/**
|
|
267
|
-
|
|
335
|
+
/**
|
|
336
|
+
* Ends the attempt's hold of its claim, keeping what it learnt, where the attempt holds it under `fence`, the fencing
|
|
337
|
+
* number it took the claim with: the write holds only while the row's number is still `fence`, so a settle whose hold
|
|
338
|
+
* another settle took never ends that settle's hold. An attempt that holds no claim (`fence` undefined: it watched
|
|
339
|
+
* another settle's, or the record held none) ends nothing. A failure goes to the log, and the hold lapses.
|
|
340
|
+
*/
|
|
341
|
+
async function release(a, fence, learnt) {
|
|
342
|
+
if (fence === undefined)
|
|
343
|
+
return;
|
|
268
344
|
try {
|
|
269
|
-
await inTime(record.release(a.network, a.key, learnt), Infinity)
|
|
345
|
+
if (!(await inTime(record.release(a.network, a.key, fence, learnt), Infinity)))
|
|
346
|
+
log({ ...about(a), event: "claim-taken", call: "release" });
|
|
270
347
|
}
|
|
271
348
|
catch (e) {
|
|
272
349
|
log({ ...about(a), event: "store-call-failed", call: "release" }, e);
|
|
@@ -302,43 +379,93 @@ export async function serveSettler(c, list) {
|
|
|
302
379
|
}
|
|
303
380
|
/**
|
|
304
381
|
* The answer, once lcp's read shows the instrument final, and the row made final. A success is this settler's only
|
|
305
|
-
* where it sent the payment
|
|
306
|
-
*
|
|
307
|
-
*
|
|
308
|
-
*
|
|
309
|
-
*
|
|
310
|
-
*
|
|
311
|
-
*
|
|
382
|
+
* where it sent the payment and the read shows settled one of the transactions its submissions for the claim named
|
|
383
|
+
* (`sent`), or the payment presented itself, which lcp's read can name otherwise than the submission did (a Polkadot
|
|
384
|
+
* extrinsic by its timepoint, not its hash); and only to the call that made the row final. It sent the payment where
|
|
385
|
+
* `sent` names a transaction, or where a settle holding the claim gave it to the class's settle (`submitted`), whose
|
|
386
|
+
* answer naming the transaction can be lost: the node's answer to the send dropped, a class that answers `""` on
|
|
387
|
+
* any failure after its broadcast, a class's settle that outlasts the hold, or the settler stopped before the
|
|
388
|
+
* answer. A class's failure naming nothing reads the same whether or not the node took the send, so a claim given to
|
|
389
|
+
* the class is this settler's even where the payment then landed by another route. Both facts are of the body that
|
|
390
|
+
* claimed the row (`claimBody`): a call that presents another body under the instrument, which the settler never
|
|
391
|
+
* gave to the class, is answered `invalid_transaction_state` naming what landed, and the row stays the claim's. A
|
|
392
|
+
* landed act that is another body than the one presented is never that call's success, and a read that states
|
|
393
|
+
* `presented` `unshown` shows no body: where it names none of `sent`, it is no success either, and is answered
|
|
394
|
+
* `invalid_transaction_state` naming the transaction, the row made final. One that landed by another
|
|
395
|
+
* route where no settle gave the claim to the class (no claim, or a claim refused before the class's settle) is
|
|
396
|
+
* `invalid_transaction_state` naming it. A failed instrument is `invalid_transaction_state` naming the transaction
|
|
397
|
+
* the read found, or the last one the settler sent, or the class's own failure where it named the transaction it
|
|
398
|
+
* sent; where the call presents another body than the claim's, the read is of that body, so the answer is
|
|
399
|
+
* `invalid_transaction_state` and the row stays the claim's. `fence` is the fencing number the attempt holds the claim under, undefined where it holds none: only a holder
|
|
400
|
+
* ends the hold. `atHead` is the transaction a success was already given for on a receipt the chain had not shown
|
|
401
|
+
* final (`RecordNeeds.successAtHead`): where the read shows it settled, the row is made final with that success and
|
|
402
|
+
* the call is answered as a repeat of a final row is, `invalid_transaction_state` naming it; where the read shows
|
|
403
|
+
* another transaction of the settler's settled the payment, that is answered a success naming it, stating in
|
|
404
|
+
* `extra.supersedes` the transaction of the success given before, which the chain does not hold; where the read shows
|
|
405
|
+
* the instrument failed, its answer stands. Either change goes to the operator log.
|
|
312
406
|
*/
|
|
313
|
-
async function byFinalRead(a, read, sent, payer, claimed, classAnswer) {
|
|
407
|
+
async function byFinalRead(a, read, sent, submitted, claimBody, fence, payer, claimed, classAnswer, atHead) {
|
|
314
408
|
const answer = read.answer;
|
|
315
409
|
const kept = { ...(payer === undefined ? {} : { payer }), ...(claimed === undefined ? {} : { claimed }) };
|
|
410
|
+
const withdrawn = (now) => {
|
|
411
|
+
if (atHead !== undefined && (now === undefined || !sameTransaction(atHead, now))) {
|
|
412
|
+
log({ ...about(a, { transaction: atHead }), event: "success-withdrawn", shown: answer.state, ...(now === undefined ? {} : { now }) });
|
|
413
|
+
}
|
|
414
|
+
};
|
|
316
415
|
if (answer.state === "settled") {
|
|
317
416
|
const mine = sent.find((t) => sameTransaction(answer.transaction, t));
|
|
318
417
|
if (mine !== undefined && answer.presented === "no") {
|
|
319
418
|
// The settler's transaction, of another body under this instrument than this call presents: not this call's
|
|
320
419
|
// success, and the row stays the claim's.
|
|
321
|
-
await release(a, {});
|
|
420
|
+
await release(a, fence, {});
|
|
421
|
+
return failed("invalid_transaction_state", answer.transaction, a.network);
|
|
422
|
+
}
|
|
423
|
+
const given = sent.length > 0 || submitted;
|
|
424
|
+
if (given && !claimBody) {
|
|
425
|
+
// Another body than the one that claimed the row, which no settle gave to the class: never this call's
|
|
426
|
+
// success, whatever the read states of it, and the row stays the claim's, whose own body may be what landed.
|
|
427
|
+
await release(a, fence, {});
|
|
322
428
|
return failed("invalid_transaction_state", answer.transaction, a.network);
|
|
323
429
|
}
|
|
324
|
-
if (mine !== undefined || (
|
|
430
|
+
if (mine !== undefined || (given && answer.presented === "yes")) {
|
|
325
431
|
const transaction = mine ?? answer.transaction;
|
|
326
|
-
const
|
|
432
|
+
const confirmed = atHead !== undefined && sameTransaction(atHead, transaction);
|
|
433
|
+
const success = {
|
|
434
|
+
success: true,
|
|
435
|
+
transaction,
|
|
436
|
+
network: a.network,
|
|
437
|
+
payer: payer ?? "",
|
|
438
|
+
...(atHead === undefined || confirmed ? {} : { extra: { supersedes: atHead } }),
|
|
439
|
+
};
|
|
440
|
+
if (atHead !== undefined && !confirmed)
|
|
441
|
+
log({ ...about(a, { transaction }), event: "success-superseded", supersedes: atHead });
|
|
327
442
|
const wrote = await finish(a, { outcome: "settled", answer: success, transaction, ...kept });
|
|
328
443
|
void countFee(a, transaction);
|
|
329
444
|
if (wrote === undefined)
|
|
330
445
|
return pending(transaction, a.network);
|
|
446
|
+
// The success given at the head, now final: a repeat's answer, as from the final row.
|
|
447
|
+
if (confirmed)
|
|
448
|
+
return failed("invalid_transaction_state", transaction, a.network);
|
|
331
449
|
return wrote ? success : failed("invalid_transaction_state", transaction, a.network);
|
|
332
450
|
}
|
|
451
|
+
withdrawn(answer.transaction);
|
|
333
452
|
const refused = failed("invalid_transaction_state", answer.transaction, a.network);
|
|
334
453
|
await finish(a, { outcome: "settled", answer: refused, transaction: answer.transaction, ...kept });
|
|
335
454
|
return refused;
|
|
336
455
|
}
|
|
337
456
|
const last = sent.at(-1);
|
|
457
|
+
if (!claimBody) {
|
|
458
|
+
// What the read shows of another body than the one that claimed the row (its own validity passed, or the value
|
|
459
|
+
// consumed by an act the read names): never the claim's answer. The row stays the claim's, whose own body's next
|
|
460
|
+
// read decides it.
|
|
461
|
+
await release(a, fence, {});
|
|
462
|
+
return failed("invalid_transaction_state", answer.transaction ?? last ?? "", a.network);
|
|
463
|
+
}
|
|
338
464
|
if (last !== undefined)
|
|
339
465
|
void countFee(a, last);
|
|
340
466
|
const own = classAnswer !== undefined && classAnswer.transaction !== "" ? classAnswer : undefined;
|
|
341
467
|
const named = answer.transaction ?? own?.transaction ?? last;
|
|
468
|
+
withdrawn(undefined);
|
|
342
469
|
const refused = own ?? failed("invalid_transaction_state", named ?? "", a.network);
|
|
343
470
|
await finish(a, { outcome: answer.reason, answer: refused, ...(named === undefined ? {} : { transaction: named }), ...kept });
|
|
344
471
|
return refused;
|
|
@@ -346,31 +473,120 @@ export async function serveSettler(c, list) {
|
|
|
346
473
|
/**
|
|
347
474
|
* For a claim this attempt holds: the fee budget, just before anything is sent; the submission of what
|
|
348
475
|
* the payer signed, through x402's class and the kind's wrapper; then the answer: a success the class gives once
|
|
349
|
-
* it is stored final, else by lcp's read. `sent` is every transaction earlier submissions for the claim named
|
|
476
|
+
* it is stored final, else by lcp's read. `sent` is every transaction earlier submissions for the claim named; the
|
|
477
|
+
* read also takes each one the module named before it sent it (`Core.naming`, or `beforeSend` for a claim that
|
|
478
|
+
* `landsOnce` or is `resendable`), which the row holds already. `fence` is the claim's fencing number as this attempt
|
|
479
|
+
* took it: a transaction a module names, by either call, is kept in the row only while the row's fencing number is
|
|
480
|
+
* still `fence`, in the same write, and is otherwise not sent, so that a settle whose hold lapsed and was taken by
|
|
481
|
+
* another sends nothing after it. `repeat` is given by a repeat that took a claim an earlier settle gave to the
|
|
482
|
+
* class: `names`, the transaction the row names for it where it may still land. While the row names one, no exit
|
|
483
|
+
* answers a refusal that names nothing: each is `settlement_pending` naming it, the refusal going to the operator
|
|
484
|
+
* log. Where the row names none and its claim is one given to the class unnamed, the class's refusal naming nothing
|
|
485
|
+
* is never the answer: it may be of the earlier submission's own transaction, which no read that finds nothing
|
|
486
|
+
* rules out, so the answer is the settler's own failure (`claim-unread`) until a read names the transaction or
|
|
487
|
+
* shows the instrument final. `atHead`, where the row holds a success given before on a receipt the chain had not shown
|
|
488
|
+
* final, is that success's transaction: a success the class now answers naming another states it (`givenAtHead`).
|
|
350
489
|
*/
|
|
351
|
-
async function submit(a, payer, sent, claimed, since) {
|
|
490
|
+
async function submit(a, payer, sent, claimed, since, fence, repeat) {
|
|
491
|
+
const names = repeat?.names;
|
|
352
492
|
if (gated(a.module, a.payment)) {
|
|
353
493
|
let spent;
|
|
354
494
|
try {
|
|
355
495
|
spent = await inTime(fees.spent(a.network, Date.now()), a.deadline);
|
|
356
496
|
}
|
|
357
497
|
catch (e) {
|
|
358
|
-
await release(a, {});
|
|
359
|
-
|
|
498
|
+
await release(a, fence, {});
|
|
499
|
+
const entry = { ...about(a, { ...(payer === undefined ? {} : { payer }) }), event: "store-call-failed", call: "fee" };
|
|
500
|
+
return names === undefined ? unexpectedSettle(log, entry, e) : withheld(a, names, entry, e);
|
|
360
501
|
}
|
|
361
502
|
if (spent) {
|
|
362
|
-
await release(a, {});
|
|
363
|
-
return failed("fee_budget_spent", "", a.network);
|
|
503
|
+
await release(a, fence, {});
|
|
504
|
+
return names === undefined ? failed("fee_budget_spent", "", a.network) : withheld(a, names, { ...about(a), event: "refusal-withheld", reason: "fee_budget_spent" });
|
|
364
505
|
}
|
|
365
506
|
}
|
|
366
507
|
if (a.waitUntil - performance.now() <= 0) {
|
|
367
|
-
await release(a, {});
|
|
368
|
-
|
|
508
|
+
await release(a, fence, {});
|
|
509
|
+
const entry = { ...about(a), event: "settle-deadline-passed", before: "submission", settleWaitMs };
|
|
510
|
+
return names === undefined ? unexpectedSettle(log, entry) : withheld(a, names, entry);
|
|
511
|
+
}
|
|
512
|
+
try {
|
|
513
|
+
// Before anything can be sent: a lost answer, or a stop before it, is then still known as this settler's.
|
|
514
|
+
await inTime(record.submitting(a.network, a.key), a.deadline);
|
|
515
|
+
}
|
|
516
|
+
catch (e) {
|
|
517
|
+
await release(a, fence, {});
|
|
518
|
+
const entry = { ...about(a, { ...(payer === undefined ? {} : { payer }) }), event: "store-call-failed", call: "submitting" };
|
|
519
|
+
return names === undefined ? unexpectedSettle(log, entry, e) : withheld(a, names, entry, e);
|
|
369
520
|
}
|
|
370
521
|
let s;
|
|
371
522
|
let thrown;
|
|
523
|
+
// Every transaction a module names before it sends it, by `Core.naming` or `beforeSend`, is kept in the row by the
|
|
524
|
+
// one write that holds only while the row's fencing number is still this attempt's: a settle whose hold another
|
|
525
|
+
// took sends nothing.
|
|
526
|
+
const fenced = async (transaction, call) => {
|
|
527
|
+
let kept;
|
|
528
|
+
try {
|
|
529
|
+
kept = await faulting(inTime(record.naming(a.network, a.key, fence, transaction), a.deadline));
|
|
530
|
+
}
|
|
531
|
+
catch (e) {
|
|
532
|
+
log({ ...about(a, { transaction }), event: "store-call-failed", call }, e);
|
|
533
|
+
throw e;
|
|
534
|
+
}
|
|
535
|
+
if (!kept) {
|
|
536
|
+
log({ ...about(a, { transaction }), event: "claim-taken-before-send" });
|
|
537
|
+
throw new FencedOut();
|
|
538
|
+
}
|
|
539
|
+
};
|
|
540
|
+
// What the module named before it sent (`Core.naming`), kept in the row before the send.
|
|
541
|
+
const namedFirst = [];
|
|
542
|
+
// Takes back a transaction named and not sent, its own bound: a refused send, or a store call that failed. The write
|
|
543
|
+
// holds only while the row's fencing number is still this attempt's, so a settle that took the claim since, and may
|
|
544
|
+
// have named the same transaction (the same bytes, signed at the same nonce), keeps its name.
|
|
545
|
+
const withdraw = async (transaction) => {
|
|
546
|
+
const at = namedFirst.findIndex((t) => sameTransaction(t, transaction));
|
|
547
|
+
if (at >= 0)
|
|
548
|
+
namedFirst.splice(at, 1);
|
|
549
|
+
try {
|
|
550
|
+
if (!(await inTime(record.unsent(a.network, a.key, fence, transaction), Infinity))) {
|
|
551
|
+
log({ ...about(a, { transaction }), event: "claim-taken", call: "unsent" });
|
|
552
|
+
}
|
|
553
|
+
}
|
|
554
|
+
catch (e) {
|
|
555
|
+
log({ ...about(a, { transaction }), event: "store-call-failed", call: "unsent" }, e);
|
|
556
|
+
}
|
|
557
|
+
};
|
|
558
|
+
// Whether the module named a transaction in this submission before sending it: one that does shows, by a row
|
|
559
|
+
// that names nothing, that nothing of the claim is out.
|
|
560
|
+
let namesFirst = false;
|
|
561
|
+
const keep = async (transaction) => {
|
|
562
|
+
namesFirst = true;
|
|
563
|
+
try {
|
|
564
|
+
await fenced(transaction, "naming");
|
|
565
|
+
}
|
|
566
|
+
catch (e) {
|
|
567
|
+
// Nothing is sent, so nothing may name it: taken back where the store call failed and its write may have landed
|
|
568
|
+
// before this, while this settle still holds the claim. A write that lands after it, on another connection, is
|
|
569
|
+
// not taken back, and a repeat then names a transaction never sent. Where the fence refused the write, this
|
|
570
|
+
// settle wrote nothing, and the row may name the same transaction for the settle that took the claim: it is left.
|
|
571
|
+
if (!(e instanceof FencedOut))
|
|
572
|
+
await withdraw(transaction);
|
|
573
|
+
throw e;
|
|
574
|
+
}
|
|
575
|
+
namedFirst.push(transaction);
|
|
576
|
+
return () => withdraw(transaction);
|
|
577
|
+
};
|
|
578
|
+
// Each transaction a module names through `beforeSend` (a claim that `landsOnce` or is `resendable`).
|
|
579
|
+
const announced = [];
|
|
580
|
+
const name = async (transaction) => {
|
|
581
|
+
await fenced(transaction, "naming");
|
|
582
|
+
announced.push(transaction);
|
|
583
|
+
};
|
|
584
|
+
const throughBeforeSend = a.instrumented.landsOnce === true || a.instrumented.resendable === true;
|
|
585
|
+
// A store call that failed inside the class's settle, before anything was sent or named.
|
|
586
|
+
const faults = {};
|
|
372
587
|
try {
|
|
373
|
-
|
|
588
|
+
const call = () => settling.settle(a.payment.payload, a.payment.requirements);
|
|
589
|
+
s = await storeFaults.run(faults, () => inSubmission.run(keep, () => (throughBeforeSend ? namingSends({ name, named: sent }, call) : call())));
|
|
374
590
|
}
|
|
375
591
|
catch (e) {
|
|
376
592
|
thrown = { cause: e };
|
|
@@ -378,9 +594,13 @@ export async function serveSettler(c, list) {
|
|
|
378
594
|
const failure = { ...about(a, { ...(payer === undefined ? {} : { payer }) }), event: "settle-error" };
|
|
379
595
|
const named = s !== undefined && s.transaction !== "" ? s.transaction : undefined;
|
|
380
596
|
if (s?.success === true) {
|
|
597
|
+
const answerPayer = s.payer ?? payer;
|
|
598
|
+
// A success on a receipt at the head: given now, the row kept live naming it until a read shows it final.
|
|
599
|
+
if (named !== undefined && a.module.record.successAtHead === true) {
|
|
600
|
+
return givenAtHead(a, s, named, { fence, payer: answerPayer, atHead: repeat?.atHead, sent, since, claimed });
|
|
601
|
+
}
|
|
381
602
|
// A success is given only once the row holds it final, with the transaction it names where it names one (a
|
|
382
603
|
// published class's settle of a zero amount names none): a repeat is then answered from the record.
|
|
383
|
-
const answerPayer = s.payer ?? payer;
|
|
384
604
|
const wrote = await finish(a, {
|
|
385
605
|
outcome: "settled",
|
|
386
606
|
answer: s,
|
|
@@ -399,7 +619,10 @@ export async function serveSettler(c, list) {
|
|
|
399
619
|
return unexpectedSettle(log, { ...about(a), event: "success-not-stored" });
|
|
400
620
|
return pending(named, a.network);
|
|
401
621
|
}
|
|
402
|
-
const all =
|
|
622
|
+
const all = [...sent];
|
|
623
|
+
for (const t of [...namedFirst, ...announced, ...(named === undefined ? [] : [named])])
|
|
624
|
+
if (!all.some((h) => sameTransaction(h, t)))
|
|
625
|
+
all.push(t);
|
|
403
626
|
if (named !== undefined) {
|
|
404
627
|
await note(a, named);
|
|
405
628
|
void countFee(a, named);
|
|
@@ -408,19 +631,185 @@ export async function serveSettler(c, list) {
|
|
|
408
631
|
if (thrown !== undefined && (read.outcome === "final" || all.length > 0))
|
|
409
632
|
log(failure, thrown.cause);
|
|
410
633
|
if (read.outcome === "final")
|
|
411
|
-
return byFinalRead(a, read, all, s?.payer ?? payer, claimed, s);
|
|
634
|
+
return byFinalRead(a, read, all, true, true, fence, s?.payer ?? payer, claimed, s, repeat?.atHead);
|
|
412
635
|
const live = read.answer;
|
|
413
|
-
await release(a,
|
|
636
|
+
await release(a, fence, learnt(live, all.at(-1)));
|
|
414
637
|
if (s !== undefined && named !== undefined) {
|
|
415
638
|
// A failed result that consumed nothing is the class's own answer; anything else sent is not yet shown.
|
|
416
639
|
const consumedNothing = live.state === "pending" && live.reason === "instrument-live" && live.transaction !== undefined;
|
|
417
640
|
return consumedNothing ? s : pending(named, a.network);
|
|
418
641
|
}
|
|
419
|
-
|
|
642
|
+
const lastAnnounced = announced.at(-1);
|
|
643
|
+
// A `resendable` instrument's read shows whether the message landed, never that a transaction named for it cannot
|
|
644
|
+
// land (a node that does not show it states nothing of the chain): every one named is answered pending.
|
|
645
|
+
const cannotLand = (t) => a.instrumented.resendable !== true && unableToLand(live, t);
|
|
646
|
+
if (lastAnnounced !== undefined && !cannotLand(lastAnnounced)) {
|
|
647
|
+
// The module named a transaction it sent, which the class's answer does not name (x402's classes answer `""`
|
|
648
|
+
// where the send's answer was lost or its wait threw): it may land, so the answer is pending, never a refusal.
|
|
649
|
+
return pending(lastAnnounced, a.network);
|
|
650
|
+
}
|
|
651
|
+
// The read names a transaction of the instrument above the mark it reads at: nothing is final, whatever the class
|
|
652
|
+
// answered, and the answer is pending naming it, as a repeat's is.
|
|
653
|
+
if (live.state === "pending" && live.reason === "below-mark" && live.transaction !== undefined) {
|
|
654
|
+
if (s !== undefined)
|
|
655
|
+
log({ ...about(a, { transaction: live.transaction }), event: "refusal-withheld", reason: s.errorReason });
|
|
656
|
+
return pending(live.transaction, a.network);
|
|
657
|
+
}
|
|
658
|
+
// The row names a transaction an earlier submission sent, which may still land or has: never a refusal naming nothing.
|
|
659
|
+
if (names !== undefined && !cannotLand(names)) {
|
|
660
|
+
return withheld(a, names, { ...about(a), event: "refusal-withheld", reason: s?.errorReason ?? "settle-error" });
|
|
661
|
+
}
|
|
662
|
+
// A refusal that reads final: not pending, nor the settler's own failure, which a repeat asks again.
|
|
663
|
+
const refusing = s !== undefined && s.errorReason !== "settlement_pending" && s.errorReason !== OWN_FAILURE;
|
|
664
|
+
if (s !== undefined && refusing) {
|
|
665
|
+
// Before the class's refusal: what the row and the chain show now, where another settle may have taken the claim
|
|
666
|
+
// and settled the payment while this one waited.
|
|
667
|
+
const now = await lastLook(a, fence, all, s.payer ?? payer, claimed, since, repeat?.atHead);
|
|
668
|
+
if (now !== undefined)
|
|
669
|
+
return now;
|
|
670
|
+
}
|
|
671
|
+
if (s !== undefined && refusing && faults.cause !== undefined) {
|
|
672
|
+
// The class's refusal of a store call that failed in its settle (the database's own text), with nothing named:
|
|
673
|
+
// not an answer about the payment, and nothing reads final. The settler's own failure, which a repeat asks again.
|
|
674
|
+
return unexpectedSettle(log, { ...about(a), event: "store-call-failed", call: "submission", refused: s.errorReason }, faults.cause);
|
|
675
|
+
}
|
|
676
|
+
if (s !== undefined) {
|
|
677
|
+
// A claim given to the class before, unnamed, whose row names nothing: the class's refusal may be of that
|
|
678
|
+
// submission's own transaction (its authorization used by it, mined or held by the node). Not where the module
|
|
679
|
+
// named a transaction before sending it in this submission: it names what it sends, so nothing is out.
|
|
680
|
+
if (repeat?.unnamed === true && !namedBeforeSent(a) && !namesFirst) {
|
|
681
|
+
return unexpectedSettle(log, { ...about(a), event: "claim-unread", reason: live.reason, refused: s.errorReason });
|
|
682
|
+
}
|
|
420
683
|
return s;
|
|
684
|
+
}
|
|
421
685
|
const last = all.at(-1);
|
|
422
686
|
return last === undefined ? unexpectedSettle(log, failure, thrown?.cause) : pending(last, a.network);
|
|
423
687
|
}
|
|
688
|
+
/**
|
|
689
|
+
* A success the class answered on a receipt at the head (`RecordNeeds.successAtHead`), naming `transaction`: kept in
|
|
690
|
+
* the row, which stays live naming it and is released, and given. The row is made final only by a read that shows
|
|
691
|
+
* the instrument final: one made at once, within `AT_HEAD_READ_MS` and the request's wait, where the finalized chain already
|
|
692
|
+
* holds it, else a repeat's (`byFinalRead`); until then a repeat reads the chain and answers what it shows. Where an
|
|
693
|
+
* earlier success named another transaction (`atHead`), which the chain then did not hold, this one states it in
|
|
694
|
+
* `extra.supersedes`. Where the store does not take it, the answer is pending naming the transaction; where the row
|
|
695
|
+
* is final or another settle took the claim, the answer is the row's.
|
|
696
|
+
*/
|
|
697
|
+
async function givenAtHead(a, s, transaction, at) {
|
|
698
|
+
const { fence, payer, atHead } = at;
|
|
699
|
+
const superseded = atHead !== undefined && !sameTransaction(atHead, transaction);
|
|
700
|
+
const answer = superseded ? { ...s, extra: { ...(s.extra ?? {}), supersedes: atHead } } : s;
|
|
701
|
+
if (superseded)
|
|
702
|
+
log({ ...about(a, { transaction }), event: "success-superseded", supersedes: atHead });
|
|
703
|
+
void countFee(a, transaction);
|
|
704
|
+
let kept;
|
|
705
|
+
try {
|
|
706
|
+
const keep = keepUntil(a.instrumented.keep, a.instrumented.nonce, at.claimed);
|
|
707
|
+
kept = await inTime(record.answered(a.network, a.key, fence, transaction, toStored(answer), payer, keep), Infinity);
|
|
708
|
+
}
|
|
709
|
+
catch (e) {
|
|
710
|
+
log({ ...about(a, { transaction }), event: "answer-not-stored" }, e);
|
|
711
|
+
return pending(transaction, a.network);
|
|
712
|
+
}
|
|
713
|
+
if (kept) {
|
|
714
|
+
// Final at once only where the finalized chain already shows it, as a read of the row would.
|
|
715
|
+
const all = at.sent.some((t) => sameTransaction(t, transaction)) ? at.sent : [...at.sent, transaction];
|
|
716
|
+
const read = await readChain({ ...a, waitUntil: Math.min(a.waitUntil, performance.now() + AT_HEAD_READ_MS) }, { since: at.since, transaction });
|
|
717
|
+
if (read.outcome !== "final")
|
|
718
|
+
return answer;
|
|
719
|
+
if (read.answer.state === "settled" && sameTransaction(read.answer.transaction, transaction)) {
|
|
720
|
+
await finish(a, {
|
|
721
|
+
outcome: "settled",
|
|
722
|
+
answer: answer,
|
|
723
|
+
transaction,
|
|
724
|
+
...(payer === undefined ? {} : { payer }),
|
|
725
|
+
claimed: at.claimed,
|
|
726
|
+
});
|
|
727
|
+
return answer;
|
|
728
|
+
}
|
|
729
|
+
// The finalized chain shows another end than the receipt did: what it shows is the answer, the success not given,
|
|
730
|
+
// stating any success an earlier call was given.
|
|
731
|
+
return byFinalRead(a, read, all, true, true, fence, payer, at.claimed, undefined, atHead);
|
|
732
|
+
}
|
|
733
|
+
log({ ...about(a, { transaction }), event: "claim-taken", call: "answered" });
|
|
734
|
+
const row = await inTime(record.read(a.network, a.key), Infinity).catch(() => undefined);
|
|
735
|
+
if (row?.final === true)
|
|
736
|
+
return answerFinal(a, row);
|
|
737
|
+
return pending(row?.transaction ?? transaction, a.network);
|
|
738
|
+
}
|
|
739
|
+
/** The transaction of a success a row that is not final holds, given on a receipt the chain had not shown final. */
|
|
740
|
+
function givenOf(row) {
|
|
741
|
+
const t = row.final ? undefined : row.answer?.["success"] === true ? row.answer["transaction"] : undefined;
|
|
742
|
+
return typeof t === "string" && t !== "" ? t : undefined;
|
|
743
|
+
}
|
|
744
|
+
/**
|
|
745
|
+
* Before a refusal that reads final is given for a claim this settle took under `fence` (undefined: it took none): the
|
|
746
|
+
* row read again, then, where the request's wait left no time for the chain's read, the chain, each on its own bound,
|
|
747
|
+
* so that a settle that paused past its hold, or lost the race to the claim, answers what they show and not the
|
|
748
|
+
* refusal of a payment another settle executed. A final row is answered as a final row is; a claim another settle
|
|
749
|
+
* holds or took since is pending naming what its row names, or the settler's own failure (`claim-taken`); a read
|
|
750
|
+
* the chain shows final decides. Undefined where neither shows anything: the refusal stands. A store call that fails
|
|
751
|
+
* here is the settler's own failure.
|
|
752
|
+
*/
|
|
753
|
+
async function lastLook(a, fence, sent, payer, claimed, since, atHead) {
|
|
754
|
+
let row;
|
|
755
|
+
try {
|
|
756
|
+
row = await inTime(record.read(a.network, a.key), Infinity);
|
|
757
|
+
}
|
|
758
|
+
catch (e) {
|
|
759
|
+
return unexpectedSettle(log, { ...about(a), event: "store-call-failed", call: "read" }, e);
|
|
760
|
+
}
|
|
761
|
+
if (row?.final === true)
|
|
762
|
+
return answerFinal(a, row);
|
|
763
|
+
if (row !== undefined && (fence === undefined || row.fence !== fence)) {
|
|
764
|
+
if (row.transaction !== undefined)
|
|
765
|
+
return pending(row.transaction, a.network);
|
|
766
|
+
return unexpectedSettle(log, { ...about(a), event: "claim-taken", call: "refusal" });
|
|
767
|
+
}
|
|
768
|
+
if (a.waitUntil - performance.now() > 0)
|
|
769
|
+
return undefined;
|
|
770
|
+
const read = await readChain({ ...a, waitUntil: performance.now() + NODE_TIMEOUT_MS }, { since, transaction: sent.at(-1) });
|
|
771
|
+
// Past the request's wait its caller may be gone: a settled read is answered pending, naming the transaction, and
|
|
772
|
+
// the next repeat's read makes the row final with the success; a failed one is final as read.
|
|
773
|
+
if (read.outcome === "final" && read.answer.state === "settled")
|
|
774
|
+
return pending(read.answer.transaction, a.network);
|
|
775
|
+
if (read.outcome === "final")
|
|
776
|
+
return byFinalRead(a, read, sent, true, true, fence, payer, claimed, undefined, atHead);
|
|
777
|
+
const live = read.answer;
|
|
778
|
+
if (live.state === "pending" && live.reason === "below-mark" && live.transaction !== undefined)
|
|
779
|
+
return pending(live.transaction, a.network);
|
|
780
|
+
return undefined;
|
|
781
|
+
}
|
|
782
|
+
/**
|
|
783
|
+
* `settlement_pending` naming `transaction`, the one a repeat's row names, in place of an answer that names nothing:
|
|
784
|
+
* `entry`, what was withheld, goes to the operator log, with its transaction.
|
|
785
|
+
*/
|
|
786
|
+
function withheld(a, transaction, entry, cause) {
|
|
787
|
+
log({ ...entry, transaction }, cause);
|
|
788
|
+
return pending(transaction, a.network);
|
|
789
|
+
}
|
|
790
|
+
/**
|
|
791
|
+
* Whether the attempt's module names every transaction it sends for the claim in the row before it sends it
|
|
792
|
+
* (`beforeSend`, or `Core.naming` with `RecordNeeds.resend` for a claim named by its digest): a row of such a claim
|
|
793
|
+
* that names nothing shows nothing was sent for it. Any other claim (an escrow's hold, a published class's own send)
|
|
794
|
+
* is given to the class unnamed, and a row naming nothing says nothing of what was sent.
|
|
795
|
+
*/
|
|
796
|
+
function namedBeforeSent(a) {
|
|
797
|
+
const i = a.instrumented;
|
|
798
|
+
return i.landsOnce === true || i.resendable === true || (a.module.record.resend !== undefined && i.instrument.by === "digest");
|
|
799
|
+
}
|
|
800
|
+
/**
|
|
801
|
+
* The search position a read that is not final gives, as the row keeps it for the next read to resume from: none
|
|
802
|
+
* where the answer names a transaction the row does not name (`named`), one the read found above the mark it reads
|
|
803
|
+
* at. The position can be past that transaction, and the row keeps no name a read found: the next read then
|
|
804
|
+
* searches again from the position the row holds, and meets the transaction again.
|
|
805
|
+
*/
|
|
806
|
+
function learnt(live, named) {
|
|
807
|
+
if (live.state !== "pending" || live.search === undefined)
|
|
808
|
+
return {};
|
|
809
|
+
if (live.transaction !== undefined && (named === undefined || !sameTransaction(live.transaction, named)))
|
|
810
|
+
return {};
|
|
811
|
+
return { search: live.search };
|
|
812
|
+
}
|
|
424
813
|
/**
|
|
425
814
|
* A claim another settle holds and may be sending: the row is read until that settle answers or its hold ends,
|
|
426
815
|
* and the chain then, and nothing is sent.
|
|
@@ -432,8 +821,9 @@ export async function serveSettler(c, list) {
|
|
|
432
821
|
return answerFinal(a, current);
|
|
433
822
|
if (current.heldUntil <= Date.now()) {
|
|
434
823
|
const read = await readChain(a, current);
|
|
824
|
+
// This settle holds no claim: it ends no hold.
|
|
435
825
|
if (read.outcome === "final")
|
|
436
|
-
return byFinalRead(a, read, current.sent, current.payer, current.claimed);
|
|
826
|
+
return byFinalRead(a, read, current.sent, current.submitted, current.body === bodyOf(a.payment, a.instrumented), undefined, current.payer, current.claimed, undefined, givenOf(current));
|
|
437
827
|
if (read.answer.state === "pending" && read.answer.reason === "below-mark" && read.answer.transaction !== undefined) {
|
|
438
828
|
return pending(read.answer.transaction, a.network);
|
|
439
829
|
}
|
|
@@ -461,9 +851,14 @@ export async function serveSettler(c, list) {
|
|
|
461
851
|
}
|
|
462
852
|
/**
|
|
463
853
|
* A claim the record holds that is not final. Where another settle holds it, this one watches. Otherwise it takes
|
|
464
|
-
* the claim, reads the chain, and submits the identical signed payment again only where the
|
|
465
|
-
*
|
|
466
|
-
*
|
|
854
|
+
* the claim, reads the chain, and submits the identical signed payment again only where the body is the one that
|
|
855
|
+
* claimed the row, `/verify`'s checks pass for it again, and either the form's nonce is single-use and the read
|
|
856
|
+
* shows the value unconsumed and its validity open (`instrument-live`), or the instrument is named by its digest,
|
|
857
|
+
* its module names every transaction before sending it (`landsOnce`), the read shows it not landed, and either no
|
|
858
|
+
* transaction was sent for it or the read shows the last one unable to land (`unsentOnce`), or its module names
|
|
859
|
+
* every transaction before sending it and keeps what it signed (`RecordNeeds.resend`) and the row, as taken, named
|
|
860
|
+
* none (`unnamed`). Before the read, such a module sends each transaction the row names again or retires it
|
|
861
|
+
* (`resent`), except where the network's fee budget is spent, and nothing is then sent before the read.
|
|
467
862
|
*/
|
|
468
863
|
async function held(a, row) {
|
|
469
864
|
if (row.heldUntil > Date.now())
|
|
@@ -479,38 +874,248 @@ export async function serveSettler(c, list) {
|
|
|
479
874
|
const again = await inTime(record.read(a.network, a.key), a.deadline).catch(() => undefined);
|
|
480
875
|
return watch(a, again ?? { ...row, heldUntil: Date.now() + settleWaitMs });
|
|
481
876
|
}
|
|
482
|
-
|
|
877
|
+
// A repeat sends nothing before its read where the network's fee budget is spent: no transaction the row names is
|
|
878
|
+
// sent again or replaced, and the row stays as taken.
|
|
879
|
+
let spent = false;
|
|
880
|
+
if (gated(a.module, a.payment)) {
|
|
881
|
+
try {
|
|
882
|
+
spent = await inTime(fees.spent(a.network, Date.now()), a.deadline);
|
|
883
|
+
}
|
|
884
|
+
catch (e) {
|
|
885
|
+
log({ ...about(a), event: "store-call-failed", call: "fee" }, e);
|
|
886
|
+
spent = true;
|
|
887
|
+
}
|
|
888
|
+
}
|
|
889
|
+
const now = spent ? took : await resent(a, took);
|
|
890
|
+
const read = await readChain(a, now);
|
|
891
|
+
const atHead = givenOf(took);
|
|
483
892
|
if (read.outcome === "final")
|
|
484
|
-
return byFinalRead(a, read,
|
|
893
|
+
return byFinalRead(a, read, now.sent, now.submitted, now.body === bodyOf(a.payment, a.instrumented), now.fence, now.payer, now.claimed, undefined, atHead);
|
|
485
894
|
const live = read.answer;
|
|
486
|
-
const search = live
|
|
487
|
-
|
|
488
|
-
|
|
895
|
+
const search = learnt(live, now.transaction);
|
|
896
|
+
const again = (live.state === "pending" &&
|
|
897
|
+
((live.reason === "instrument-live" && resendable(a.instrumented)) || unsentOnce(a.instrumented, now, live))) ||
|
|
898
|
+
unnamed(a, took);
|
|
899
|
+
if (again) {
|
|
900
|
+
if (now.body !== bodyOf(a.payment, a.instrumented)) {
|
|
489
901
|
// Another body under the claimed instrument (a copy signed again): never sent in place of the one claimed.
|
|
490
|
-
await release(a, search);
|
|
491
|
-
return failed("invalid_transaction_state",
|
|
902
|
+
await release(a, now.fence, search);
|
|
903
|
+
return failed("invalid_transaction_state", now.transaction ?? "", a.network);
|
|
904
|
+
}
|
|
905
|
+
// What the row names for a claim an earlier settle gave to the class, unless the read shows it unable to land:
|
|
906
|
+
// while it names one, no exit below answers a refusal that names nothing.
|
|
907
|
+
// Not an instrument keyed by its transaction's id, which its row names from the claim on, sent or not: a
|
|
908
|
+
// refusal of one the class never sent stays its refusal.
|
|
909
|
+
const last = now.submitted && known(a.instrumented) === undefined ? (now.transaction ?? now.sent.at(-1)) : undefined;
|
|
910
|
+
// Whether the row names nothing, so that it says nothing of what an earlier submission sent.
|
|
911
|
+
const unnamed = now.transaction === undefined && now.sent.length === 0;
|
|
912
|
+
const names = last !== undefined && (a.instrumented.resendable === true || !unableToLand(live, last)) ? last : undefined;
|
|
913
|
+
let v;
|
|
914
|
+
try {
|
|
915
|
+
v = await settling.verify(a.payment.payload, a.payment.requirements);
|
|
916
|
+
}
|
|
917
|
+
catch (e) {
|
|
918
|
+
await release(a, now.fence, search);
|
|
919
|
+
const entry = { ...about(a), event: "settle-error" };
|
|
920
|
+
return names === undefined ? unexpectedSettle(log, entry, e) : withheld(a, names, entry, e);
|
|
492
921
|
}
|
|
493
|
-
const v = await settling.verify(a.payment.payload, a.payment.requirements);
|
|
494
922
|
if (!v.isValid) {
|
|
495
|
-
|
|
496
|
-
|
|
923
|
+
const refused = refusedAtSettle(v, a.network);
|
|
924
|
+
const reason = settleReason(v.invalidReason ?? "invalid_payload");
|
|
925
|
+
if (names !== undefined) {
|
|
926
|
+
await release(a, now.fence, search);
|
|
927
|
+
return withheld(a, names, { ...about(a), event: "refusal-withheld", reason });
|
|
928
|
+
}
|
|
929
|
+
if (!now.submitted || !unnamed || namedBeforeSent(a)) {
|
|
930
|
+
await release(a, now.fence, search);
|
|
931
|
+
return refused;
|
|
932
|
+
}
|
|
933
|
+
// The claim was given to the class before and its row names nothing: the check may refuse the submission's own
|
|
934
|
+
// transaction, landed since this repeat's read. The read is made again, and decides.
|
|
935
|
+
const after = await readChain(a, now);
|
|
936
|
+
if (after.outcome === "final")
|
|
937
|
+
return byFinalRead(a, after, now.sent, now.submitted, true, now.fence, now.payer, now.claimed, undefined, atHead);
|
|
938
|
+
const then = after.answer;
|
|
939
|
+
await release(a, now.fence, learnt(then, now.transaction));
|
|
940
|
+
if (then.state === "pending" && then.reason === "below-mark" && then.transaction !== undefined) {
|
|
941
|
+
return withheld(a, then.transaction, { ...about(a), event: "refusal-withheld", reason });
|
|
942
|
+
}
|
|
943
|
+
return unexpectedSettle(log, { ...about(a), event: "claim-unread", reason: then.reason, refused: reason });
|
|
497
944
|
}
|
|
498
|
-
return submit(a,
|
|
945
|
+
return submit(a, now.payer, now.sent, now.claimed, now.since, now.fence, now.submitted ? { names, unnamed, atHead } : undefined);
|
|
499
946
|
}
|
|
500
|
-
await release(a, search);
|
|
947
|
+
await release(a, now.fence, search);
|
|
501
948
|
if (live.state === "pending" && live.reason === "below-mark" && live.transaction !== undefined) {
|
|
502
949
|
return pending(live.transaction, a.network);
|
|
503
950
|
}
|
|
504
|
-
if (
|
|
505
|
-
return pending(
|
|
951
|
+
if (now.transaction !== undefined)
|
|
952
|
+
return pending(now.transaction, a.network);
|
|
506
953
|
return unexpectedSettle(log, { ...about(a), event: "claim-unread", reason: live.reason });
|
|
507
954
|
}
|
|
508
955
|
/**
|
|
509
|
-
*
|
|
510
|
-
*
|
|
956
|
+
* A later message the record does not hold (`Instrumented.unrecorded`): what the kind declares, the fee budget, then
|
|
957
|
+
* the class's check and its settle, each time it is sent, the settle only where the request's wait leaves time for
|
|
958
|
+
* it. Its answer is the class's; nothing is stored.
|
|
959
|
+
*/
|
|
960
|
+
async function unrecorded(a) {
|
|
961
|
+
const excluded = declared(a.kind, a.module, a.payment, a.instrumented, "settle");
|
|
962
|
+
if (excluded !== undefined)
|
|
963
|
+
return failed(excluded, "", a.network);
|
|
964
|
+
if (gated(a.module, a.payment)) {
|
|
965
|
+
let spent;
|
|
966
|
+
try {
|
|
967
|
+
spent = await inTime(fees.spent(a.network, Date.now()), a.deadline);
|
|
968
|
+
}
|
|
969
|
+
catch (e) {
|
|
970
|
+
return unexpectedSettle(log, { ...about(a), event: "store-call-failed", call: "fee" }, e);
|
|
971
|
+
}
|
|
972
|
+
if (spent)
|
|
973
|
+
return failed("fee_budget_spent", "", a.network);
|
|
974
|
+
}
|
|
975
|
+
const v = await settling.verify(a.payment.payload, a.payment.requirements);
|
|
976
|
+
if (!v.isValid)
|
|
977
|
+
return failed(settleReason(v.invalidReason ?? "invalid_payload"), "", a.network);
|
|
978
|
+
// As for a claim: nothing is given to the class's settle once the request's wait has no time left for it.
|
|
979
|
+
if (a.waitUntil - performance.now() <= 0) {
|
|
980
|
+
return unexpectedSettle(log, { ...about(a), event: "settle-deadline-passed", before: "submission", settleWaitMs });
|
|
981
|
+
}
|
|
982
|
+
let s;
|
|
983
|
+
try {
|
|
984
|
+
s = await settling.settle(a.payment.payload, a.payment.requirements);
|
|
985
|
+
}
|
|
986
|
+
catch (e) {
|
|
987
|
+
return unexpectedSettle(log, { ...about(a), event: "settle-error" }, e);
|
|
988
|
+
}
|
|
989
|
+
if (s.transaction !== "")
|
|
990
|
+
void countFee(a, s.transaction);
|
|
991
|
+
return s;
|
|
992
|
+
}
|
|
993
|
+
/**
|
|
994
|
+
* For a claim this attempt holds, where its module names its transactions before it sends them
|
|
995
|
+
* (`RecordNeeds.resend`): each transaction the row holds is given to the module, which sends what it signed again
|
|
996
|
+
* where the chain could still take it, or a replacement of it at the same nonce, which it names first
|
|
997
|
+
* (`Core.naming`) by the write that holds only while the row's fencing number is still this attempt's; one the chain
|
|
998
|
+
* shows can never land is taken out of the row. The row as it then stands, naming the transaction the record's row
|
|
999
|
+
* names (its last naming write, or, where the one it named was taken out, the last left), so that the read and the
|
|
1000
|
+
* pending answer that follow are of that one. A failure leaves the transaction in the row, and goes to the log.
|
|
1001
|
+
*/
|
|
1002
|
+
async function resent(a, row) {
|
|
1003
|
+
const needs = a.module.record;
|
|
1004
|
+
if (needs.resend === undefined || row.sent.length === 0)
|
|
1005
|
+
return row;
|
|
1006
|
+
let sent = [...row.sent];
|
|
1007
|
+
// What the record's row names, as each write below leaves it: `naming` names the transaction it keeps; `unsent`
|
|
1008
|
+
// takes one out and, where the row named it, names the last left.
|
|
1009
|
+
let named = row.transaction;
|
|
1010
|
+
const takenOut = (transaction) => {
|
|
1011
|
+
sent = sent.filter((h) => !sameTransaction(h, transaction));
|
|
1012
|
+
if (named !== undefined && sameTransaction(named, transaction))
|
|
1013
|
+
named = sent.at(-1);
|
|
1014
|
+
};
|
|
1015
|
+
// Each transaction the module names on this repeat, before it sends it: counted against the fee budget once it is
|
|
1016
|
+
// sent, as a class's transaction is, at its bound until the chain shows what it paid.
|
|
1017
|
+
const toCount = [];
|
|
1018
|
+
const keep = async (transaction) => {
|
|
1019
|
+
let kept;
|
|
1020
|
+
try {
|
|
1021
|
+
kept = await inTime(record.naming(a.network, a.key, row.fence, transaction), a.deadline);
|
|
1022
|
+
}
|
|
1023
|
+
catch (e) {
|
|
1024
|
+
log({ ...about(a, { transaction }), event: "store-call-failed", call: "naming" }, e);
|
|
1025
|
+
throw e;
|
|
1026
|
+
}
|
|
1027
|
+
if (!kept) {
|
|
1028
|
+
log({ ...about(a, { transaction }), event: "claim-taken-before-send" });
|
|
1029
|
+
throw new FencedOut();
|
|
1030
|
+
}
|
|
1031
|
+
if (!sent.some((h) => sameTransaction(h, transaction)))
|
|
1032
|
+
sent.push(transaction);
|
|
1033
|
+
named = transaction;
|
|
1034
|
+
toCount.push(transaction);
|
|
1035
|
+
return async () => {
|
|
1036
|
+
try {
|
|
1037
|
+
if (await inTime(record.unsent(a.network, a.key, row.fence, transaction), Infinity))
|
|
1038
|
+
takenOut(transaction);
|
|
1039
|
+
}
|
|
1040
|
+
catch (e) {
|
|
1041
|
+
log({ ...about(a, { transaction }), event: "store-call-failed", call: "unsent" }, e);
|
|
1042
|
+
}
|
|
1043
|
+
};
|
|
1044
|
+
};
|
|
1045
|
+
for (const t of row.sent) {
|
|
1046
|
+
let fate;
|
|
1047
|
+
try {
|
|
1048
|
+
fate = await inSubmission.run(keep, () => needs.resend(a.payment, t, { deadline: a.waitUntil }));
|
|
1049
|
+
}
|
|
1050
|
+
catch (e) {
|
|
1051
|
+
log({ ...about(a, { transaction: t }), event: "resend-failed" }, e);
|
|
1052
|
+
continue;
|
|
1053
|
+
}
|
|
1054
|
+
if (fate !== "gone")
|
|
1055
|
+
continue;
|
|
1056
|
+
try {
|
|
1057
|
+
if (!(await inTime(record.unsent(a.network, a.key, row.fence, t), a.deadline))) {
|
|
1058
|
+
// Another settle took the claim since this one did: the row is its own.
|
|
1059
|
+
log({ ...about(a, { transaction: t }), event: "claim-taken", call: "unsent" });
|
|
1060
|
+
for (const n of toCount)
|
|
1061
|
+
void countFee(a, n);
|
|
1062
|
+
return row;
|
|
1063
|
+
}
|
|
1064
|
+
}
|
|
1065
|
+
catch (e) {
|
|
1066
|
+
log({ ...about(a, { transaction: t }), event: "store-call-failed", call: "unsent" }, e);
|
|
1067
|
+
continue;
|
|
1068
|
+
}
|
|
1069
|
+
log({ ...about(a, { transaction: t }), event: "named-transaction-retired" });
|
|
1070
|
+
takenOut(t);
|
|
1071
|
+
}
|
|
1072
|
+
for (const t of toCount)
|
|
1073
|
+
void countFee(a, t);
|
|
1074
|
+
const same = (x, y) => (x === undefined ? y === undefined : y !== undefined && sameTransaction(x, y));
|
|
1075
|
+
if (sent.length === row.sent.length && sent.every((h, i) => sameTransaction(h, row.sent[i])) && same(named, row.transaction))
|
|
1076
|
+
return row;
|
|
1077
|
+
const { transaction: _named, ...rest } = row;
|
|
1078
|
+
return { ...rest, sent, ...(named === undefined ? {} : { transaction: named }) };
|
|
1079
|
+
}
|
|
1080
|
+
/**
|
|
1081
|
+
* Whether a held claim the read shows live may be sent again: where the form's nonce is single-use and the
|
|
1082
|
+
* instrument is not named by the digest of its signed payment, or where the module's read shows whether a message
|
|
1083
|
+
* keyed by its digest landed (`Instrumented.resendable`). Other instruments named by a digest, whose read cannot show
|
|
1084
|
+
* their value unconsumed, are decided by `unsentOnce`.
|
|
511
1085
|
*/
|
|
512
1086
|
function resendable(i) {
|
|
513
|
-
return i.nonce === "single-use" && i.instrument.by !== "digest";
|
|
1087
|
+
return i.resendable === true || (i.nonce === "single-use" && i.instrument.by !== "digest");
|
|
1088
|
+
}
|
|
1089
|
+
/**
|
|
1090
|
+
* Whether a held claim's module names every transaction before it sends it (`Core.naming`, with
|
|
1091
|
+
* `RecordNeeds.resend`) and the row, as this repeat took it, names none: then nothing of the claim was ever sent,
|
|
1092
|
+
* so it may be submitted again. The repeat that takes a name back on the node's evidence (`resent`) does not submit:
|
|
1093
|
+
* it judges the row as it took it. The next repeat finds the row naming none and submits it again, so a name is taken
|
|
1094
|
+
* back only where the module shows the transaction can never land. Only for a claim named by its digest, the claims
|
|
1095
|
+
* such a module names before sending: a payment keyed by its own nonce (an escrow's hold) is given to the class
|
|
1096
|
+
* unnamed, so a row naming none says nothing of what was sent for it.
|
|
1097
|
+
*/
|
|
1098
|
+
function unnamed(a, row) {
|
|
1099
|
+
return a.module.record.resend !== undefined && a.instrumented.instrument.by === "digest" && row.sent.length === 0 && row.transaction === undefined;
|
|
1100
|
+
}
|
|
1101
|
+
/**
|
|
1102
|
+
* Whether a held claim named by its digest, whose read shows it not landed, may be given to the class again: only
|
|
1103
|
+
* where its module names every transaction before it is sent (`landsOnce`), and either none was sent for the claim
|
|
1104
|
+
* (it was never given to the class, or the class refused or stopped before sending), or the read shows the
|
|
1105
|
+
* instrument open and the last transaction sent for it unable to land. A transaction that may still land is
|
|
1106
|
+
* answered pending, as for any other form.
|
|
1107
|
+
*/
|
|
1108
|
+
function unsentOnce(i, row, live) {
|
|
1109
|
+
if (i.instrument.by !== "digest" || i.landsOnce !== true || live.state !== "pending")
|
|
1110
|
+
return false;
|
|
1111
|
+
const last = row.sent.at(-1);
|
|
1112
|
+
if (last === undefined)
|
|
1113
|
+
return row.transaction === undefined && (live.reason === "not-found" || live.reason === "instrument-live");
|
|
1114
|
+
return unableToLand(live, last);
|
|
1115
|
+
}
|
|
1116
|
+
/** Whether the read shows the instrument open and `transaction`, sent for it, unable to land. */
|
|
1117
|
+
function unableToLand(live, transaction) {
|
|
1118
|
+
return live.state === "pending" && live.reason === "instrument-live" && live.transaction !== undefined && sameTransaction(live.transaction, transaction);
|
|
514
1119
|
}
|
|
515
1120
|
/** The transaction an instrument keyed by its transaction id names, which is known before it is sent. */
|
|
516
1121
|
function known(i) {
|
|
@@ -544,8 +1149,8 @@ export async function serveSettler(c, list) {
|
|
|
544
1149
|
const i = await m.record.instrument(p);
|
|
545
1150
|
if ("refused" in i)
|
|
546
1151
|
return { isValid: false, invalidReason: i.refused, ...(i.message === undefined ? {} : { invalidMessage: i.message }) };
|
|
547
|
-
|
|
548
|
-
if (
|
|
1152
|
+
// A message the record does not hold (`Instrumented.unrecorded`) is judged by the class alone.
|
|
1153
|
+
if (i.unrecorded !== true) {
|
|
549
1154
|
let row;
|
|
550
1155
|
try {
|
|
551
1156
|
row = await inTime(record.read(network, keyOf(i.instrument)), Infinity);
|
|
@@ -571,7 +1176,7 @@ export async function serveSettler(c, list) {
|
|
|
571
1176
|
return invalid("fee_budget_spent");
|
|
572
1177
|
}
|
|
573
1178
|
const v = await facilitator.verify(p.payload, p.requirements);
|
|
574
|
-
if (!v.isValid ||
|
|
1179
|
+
if (!v.isValid || i.unrecorded === true)
|
|
575
1180
|
return v;
|
|
576
1181
|
let read;
|
|
577
1182
|
try {
|
|
@@ -614,6 +1219,8 @@ export async function serveSettler(c, list) {
|
|
|
614
1219
|
deadline,
|
|
615
1220
|
waitUntil: deadline - answerReserve(settleWaitMs),
|
|
616
1221
|
};
|
|
1222
|
+
if (i.unrecorded === true)
|
|
1223
|
+
return await unrecorded(a);
|
|
617
1224
|
let row;
|
|
618
1225
|
try {
|
|
619
1226
|
row = await inTime(record.read(network, a.key), deadline);
|
|
@@ -637,8 +1244,18 @@ export async function serveSettler(c, list) {
|
|
|
637
1244
|
const read = await readChain(a, {});
|
|
638
1245
|
const lapsed = read.answer.state === "failed" && read.answer.reason === "instrument-unused";
|
|
639
1246
|
if (read.outcome === "final" && !lapsed)
|
|
640
|
-
return byFinalRead(a, read, [], v.payer, undefined);
|
|
641
|
-
|
|
1247
|
+
return byFinalRead(a, read, [], false, true, undefined, v.payer, undefined);
|
|
1248
|
+
// Another settle may have claimed the payment since the record was read, and sent it: its claim decides.
|
|
1249
|
+
let since;
|
|
1250
|
+
try {
|
|
1251
|
+
since = await inTime(record.read(network, a.key), Infinity);
|
|
1252
|
+
}
|
|
1253
|
+
catch (e) {
|
|
1254
|
+
return unread(a, e, "read");
|
|
1255
|
+
}
|
|
1256
|
+
if (since !== undefined)
|
|
1257
|
+
return since.final ? answerFinal(a, since) : held(a, since);
|
|
1258
|
+
return refusedAtSettle(v, network);
|
|
642
1259
|
}
|
|
643
1260
|
// The claim, with the chain's position the module reads for it, where its read searches from one.
|
|
644
1261
|
let since;
|
|
@@ -658,7 +1275,7 @@ export async function serveSettler(c, list) {
|
|
|
658
1275
|
pairing: i.pairing,
|
|
659
1276
|
...(v.payer === undefined ? {} : { payer: v.payer }),
|
|
660
1277
|
...(transaction === undefined ? {} : { transaction }),
|
|
661
|
-
body: bodyOf(payment),
|
|
1278
|
+
body: bodyOf(payment, i),
|
|
662
1279
|
...(since === undefined ? {} : { since }),
|
|
663
1280
|
now: claimed,
|
|
664
1281
|
holdMs: settleWaitMs,
|
|
@@ -669,10 +1286,10 @@ export async function serveSettler(c, list) {
|
|
|
669
1286
|
}
|
|
670
1287
|
if (!won) {
|
|
671
1288
|
const now = await inTime(record.read(network, a.key), deadline).catch(() => undefined);
|
|
672
|
-
return watch(a, now ?? { pairing: i.pairing, claimed, heldUntil: claimed + settleWaitMs, final: false, sent: [] });
|
|
1289
|
+
return watch(a, now ?? { pairing: i.pairing, claimed, heldUntil: claimed + settleWaitMs, final: false, sent: [], submitted: false, fence: 1 });
|
|
673
1290
|
}
|
|
674
1291
|
// The fee budget, the submission and the answer.
|
|
675
|
-
return await submit(a, v.payer, transaction === undefined ? [] : [transaction], claimed, since);
|
|
1292
|
+
return await submit(a, v.payer, transaction === undefined ? [] : [transaction], claimed, since, 1);
|
|
676
1293
|
}
|
|
677
1294
|
catch (e) {
|
|
678
1295
|
return unexpectedSettle(log, { event: "settle-error", network }, e);
|