@integraledger/profile-facilitator 0.5.0 → 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 +556 -0
- package/README.md +754 -111
- 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 +111 -24
- package/dist/module.js +39 -0
- package/dist/modules/algorand/index.js +1 -1
- package/dist/modules/aptos/index.js +1 -1
- package/dist/modules/cardano/index.js +1 -1
- package/dist/modules/casper/index.js +15 -3
- package/dist/modules/concordium/index.js +1 -1
- package/dist/modules/erc7710/index.d.ts +8 -5
- package/dist/modules/erc7710/index.js +1111 -34
- package/dist/modules/evm/escrow.d.ts +125 -0
- package/dist/modules/evm/escrow.js +619 -0
- package/dist/modules/evm/forms.d.ts +39 -11
- package/dist/modules/evm/forms.js +191 -25
- package/dist/modules/evm/index.d.ts +41 -6
- package/dist/modules/evm/index.js +1135 -20
- 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/hedera/executor.js +1 -1
- package/dist/modules/hedera/index.js +1 -1
- package/dist/modules/lnbtc/index.js +3 -2
- package/dist/modules/near/index.js +1 -1
- package/dist/modules/polkadot/index.js +1 -1
- package/dist/modules/solana/index.d.ts +3 -0
- package/dist/modules/solana/index.js +119 -10
- 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/starknet/index.js +9 -2
- package/dist/modules/stellar/index.d.ts +5 -0
- package/dist/modules/stellar/index.js +18 -8
- package/dist/modules/stellar/node.d.ts +7 -1
- package/dist/modules/stellar/node.js +51 -7
- package/dist/modules/sui/index.js +66 -20
- package/dist/modules/sui/node.d.ts +13 -0
- package/dist/modules/sui/node.js +60 -0
- package/dist/modules/ton/index.js +6 -2
- package/dist/modules/tron/index.js +1 -1
- package/dist/modules/xrpl/index.js +61 -12
- package/dist/modules/xrpl/node.d.ts +31 -2
- package/dist/modules/xrpl/node.js +88 -17
- package/dist/modules/xrpl/upto.js +106 -20
- package/dist/record.d.ts +52 -8
- package/dist/record.js +39 -5
- package/dist/wrapper.d.ts +7 -5
- package/dist/wrapper.js +7 -5
- package/package.json +4 -3
|
@@ -2,11 +2,26 @@
|
|
|
2
2
|
* The seller's EVM node, read over JSON-RPC: lcp's `EvmReader` for the pairings' reads, and the fee of a transaction
|
|
3
3
|
* the module's key sent. Every call is bounded by the node call's own bounds and by the deadline the caller gives.
|
|
4
4
|
*/
|
|
5
|
-
import { type EvmReader, type Hex } from "@integraledger/lcp/evm";
|
|
5
|
+
import { ReaderError, type EvmReader, type Hex } from "@integraledger/lcp/evm";
|
|
6
6
|
import type { Eip155 } from "@integraledger/lcp/evm";
|
|
7
7
|
import type { Charged, FeeNeeds } from "../../module.js";
|
|
8
|
+
/**
|
|
9
|
+
* A refusal the node answered, as lcp's `ReaderError`, with what the reader's own log search acts on: `why`, and for a
|
|
10
|
+
* log range the node bounds, the most blocks it says it takes (`span`), or for a rate limit, how long it asks the
|
|
11
|
+
* caller to wait (`waitMs`).
|
|
12
|
+
*/
|
|
13
|
+
export declare class Refusal extends ReaderError {
|
|
14
|
+
readonly why: "rate" | "range" | "ahead" | "other";
|
|
15
|
+
readonly span?: bigint | undefined;
|
|
16
|
+
readonly waitMs?: number | undefined;
|
|
17
|
+
constructor(kind: ReaderError["kind"], message: string, why: "rate" | "range" | "ahead" | "other", span?: bigint | undefined, waitMs?: number | undefined);
|
|
18
|
+
}
|
|
8
19
|
/** One JSON-RPC call to `rpc` that ends by `deadline`, a `performance.now()` time. */
|
|
9
20
|
export declare function rpcCall(rpc: string, method: string, params: readonly unknown[], deadline: number): Promise<unknown>;
|
|
21
|
+
/** The most `eth_getLogs` calls one `logs` call of the reader makes for a range the endpoint bounds. */
|
|
22
|
+
export declare const LOG_CALLS_MAX = 20n;
|
|
23
|
+
/** Forgets the bound each endpoint stated on a log query's range. */
|
|
24
|
+
export declare function forgetEndpoints(): void;
|
|
10
25
|
/** lcp's reader of `network` over the JSON-RPC endpoint `rpc`, each call ending by `deadline`. */
|
|
11
26
|
export declare function evmReader(network: Eip155, rpc: string, deadline: number): EvmReader;
|
|
12
27
|
/**
|
package/dist/modules/evm/node.js
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
*/
|
|
5
5
|
import { ReaderError } from "@integraledger/lcp/evm";
|
|
6
6
|
import { encodeFunctionData, serializeTransaction } from "viem";
|
|
7
|
-
import { NodeError, postJson } from "../../http.js";
|
|
7
|
+
import { NodeError, postJson, sleep } from "../../http.js";
|
|
8
8
|
const HEX_QUANTITY = /^0x[0-9a-fA-F]{1,64}$/;
|
|
9
9
|
const HEX_DATA = /^0x([0-9a-fA-F]{2})*$/;
|
|
10
10
|
const HASH = /^0x[0-9a-fA-F]{64}$/;
|
|
@@ -28,16 +28,99 @@ function data(v, what) {
|
|
|
28
28
|
function hex(v) {
|
|
29
29
|
return `0x${v.toString(16)}`;
|
|
30
30
|
}
|
|
31
|
-
/**
|
|
32
|
-
|
|
31
|
+
/**
|
|
32
|
+
* A refusal the node answered, as lcp's `ReaderError`, with what the reader's own log search acts on: `why`, and for a
|
|
33
|
+
* log range the node bounds, the most blocks it says it takes (`span`), or for a rate limit, how long it asks the
|
|
34
|
+
* caller to wait (`waitMs`).
|
|
35
|
+
*/
|
|
36
|
+
export class Refusal extends ReaderError {
|
|
37
|
+
why;
|
|
38
|
+
span;
|
|
39
|
+
waitMs;
|
|
40
|
+
constructor(kind, message, why, span, waitMs) {
|
|
41
|
+
super(kind, message);
|
|
42
|
+
this.why = why;
|
|
43
|
+
this.span = span;
|
|
44
|
+
this.waitMs = waitMs;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
/** A rate limit, in the words endpoints answer one with. */
|
|
48
|
+
const RATE = /rate.?limit|request rate|too many requests|request limit|\/second|over rate|cu limit|too fast|traffic is too high|throttl/i;
|
|
49
|
+
/** A log range that ends past the head of the node that answered: no bound on the range, and no smaller range helps. */
|
|
50
|
+
const AHEAD = /beyond current head|BlockOutOfRange/i;
|
|
51
|
+
/** A log query the node will not serve whole: its range in blocks, or the size of its answer. */
|
|
52
|
+
const RANGE = /range|too many|limit|exceed|too big|too large|more than \d+ results/i;
|
|
53
|
+
/** A refusal that speaks of a range of blocks: a bound on the query, whatever other words it uses. */
|
|
54
|
+
const BLOCKS = /block range|\bblocks\b|\d range\b/i;
|
|
55
|
+
/** The contract's own revert of a call, which is no refusal of the endpoint's, whatever its reason says. */
|
|
56
|
+
const REVERT = /^execution reverted|^VM Exception|\brevert/i;
|
|
57
|
+
/**
|
|
58
|
+
* The words after which an endpoint states the most blocks one log query may span, the number as digits with an
|
|
59
|
+
* optional `k` for thousands; a number of results is no span.
|
|
60
|
+
*/
|
|
61
|
+
const SPAN = [
|
|
62
|
+
/"maxAllowedRange"\s*:\s*([\d,]+)()/i,
|
|
63
|
+
/(?:maximum|max(?:imum)? block range:?|up to a|limited to a|ranges over|range greater than|max allowed range:?)\s*([\d,]+)(k?)(?![\d,.]*\s*(?:results|logs|bytes))/i,
|
|
64
|
+
];
|
|
65
|
+
/** The most blocks one log query may span, where the endpoint's refusal states it. */
|
|
66
|
+
function spanIn(text) {
|
|
67
|
+
for (const r of SPAN) {
|
|
68
|
+
const m = r.exec(text);
|
|
69
|
+
if (m === null)
|
|
70
|
+
continue;
|
|
71
|
+
const n = BigInt(m[1].replace(/,/g, "")) * (m[2] === "" ? 1n : 1000n);
|
|
72
|
+
if (n > 0n)
|
|
73
|
+
return n;
|
|
74
|
+
}
|
|
75
|
+
return undefined;
|
|
76
|
+
}
|
|
77
|
+
/** A `Retry-After` header as milliseconds: its seconds, or the time until its date. */
|
|
78
|
+
function waitOf(retryAfter) {
|
|
79
|
+
if (retryAfter === undefined)
|
|
80
|
+
return undefined;
|
|
81
|
+
const seconds = Number(retryAfter);
|
|
82
|
+
if (Number.isFinite(seconds) && seconds >= 0)
|
|
83
|
+
return seconds * 1000;
|
|
84
|
+
const at = Date.parse(retryAfter);
|
|
85
|
+
return Number.isNaN(at) ? undefined : Math.max(0, at - Date.now());
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* A JSON-RPC error the node answered, with the HTTP status and `Retry-After` it came with where it had one: a rate
|
|
89
|
+
* limit (`transport`: asking for less changes nothing), the range of a log query it will not serve (`too-large`),
|
|
90
|
+
* a range past its head (`transport`), history it does not hold, or other. The error's `data` is read with its
|
|
91
|
+
* message, since some endpoints state the bound there.
|
|
92
|
+
*/
|
|
93
|
+
function nodeRefused(e, status, retryAfter) {
|
|
33
94
|
const code = isObject(e) ? e["code"] : undefined;
|
|
34
|
-
const
|
|
35
|
-
|
|
36
|
-
|
|
95
|
+
const own = typeof e === "string" ? e : isObject(e) && typeof e["message"] === "string" ? e["message"] : "";
|
|
96
|
+
const data = isObject(e) && e["data"] !== undefined ? (typeof e["data"] === "string" ? e["data"] : JSON.stringify(e["data"])) : "";
|
|
97
|
+
const message = data === "" ? own : `${own}: ${data}`;
|
|
98
|
+
// A contract's revert is the call's own answer, never the endpoint's limit, whatever words its reason holds.
|
|
99
|
+
if (code === 3 || REVERT.test(own))
|
|
100
|
+
return new ReaderError("transport", message);
|
|
101
|
+
if (AHEAD.test(message))
|
|
102
|
+
return new Refusal("transport", message, "ahead");
|
|
103
|
+
// A bound on a range of blocks first: one stated as a "request limit" is still a range to split, not a rate.
|
|
104
|
+
if (status !== 429 && code !== 429 && BLOCKS.test(message) && RANGE.test(message))
|
|
105
|
+
return new Refusal("too-large", message, "range", spanIn(message));
|
|
106
|
+
if (status === 429 || code === 429 || RATE.test(message))
|
|
107
|
+
return new Refusal("transport", message || "rate limit", "rate", undefined, waitOf(retryAfter));
|
|
108
|
+
if (code === -32005 || status === 413 || RANGE.test(message))
|
|
109
|
+
return new Refusal("too-large", message, "range", spanIn(message));
|
|
37
110
|
if (/missing trie node|pruned|history|not available|header not found/i.test(message))
|
|
38
111
|
return new ReaderError("history", message);
|
|
39
112
|
return new ReaderError("transport", message || `JSON-RPC error ${String(code)}`);
|
|
40
113
|
}
|
|
114
|
+
/** The JSON-RPC error in the body a node answered with an HTTP status that is not a success, where it holds one. */
|
|
115
|
+
function errorIn(body) {
|
|
116
|
+
try {
|
|
117
|
+
const parsed = JSON.parse(new TextDecoder("utf-8", { fatal: true }).decode(body));
|
|
118
|
+
return isObject(parsed) ? parsed["error"] : undefined;
|
|
119
|
+
}
|
|
120
|
+
catch {
|
|
121
|
+
return undefined;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
41
124
|
/** One JSON-RPC call to `rpc` that ends by `deadline`, a `performance.now()` time. */
|
|
42
125
|
export async function rpcCall(rpc, method, params, deadline) {
|
|
43
126
|
let answer;
|
|
@@ -48,6 +131,12 @@ export async function rpcCall(rpc, method, params, deadline) {
|
|
|
48
131
|
if (e instanceof NodeError && (e.code === "malformed" || e.code === "too-large")) {
|
|
49
132
|
throw new ReaderError(e.code === "too-large" ? "too-large" : "malformed", e.message);
|
|
50
133
|
}
|
|
134
|
+
// A refusal answered with an HTTP status: a rate limit (429), or a JSON-RPC error in the body, read as one is.
|
|
135
|
+
if (e instanceof NodeError && e.answered !== undefined) {
|
|
136
|
+
const error = errorIn(e.answered.body);
|
|
137
|
+
if (error !== undefined || e.answered.status === 429)
|
|
138
|
+
throw nodeRefused(error, e.answered.status, e.answered.retryAfter);
|
|
139
|
+
}
|
|
51
140
|
throw new ReaderError(e instanceof NodeError && e.code === "timeout" ? "timeout" : "transport", String(e));
|
|
52
141
|
}
|
|
53
142
|
if (!isObject(answer))
|
|
@@ -58,6 +147,18 @@ export async function rpcCall(rpc, method, params, deadline) {
|
|
|
58
147
|
malformed(method);
|
|
59
148
|
return answer["result"];
|
|
60
149
|
}
|
|
150
|
+
/** How often a read the endpoint refused for its rate, or for a range past the answering node's head, is asked again. */
|
|
151
|
+
const READ_RETRIES = 4;
|
|
152
|
+
/** The longest one pause before a read is asked again. */
|
|
153
|
+
const PAUSE_MAX_MS = 2_000;
|
|
154
|
+
/** The most `eth_getLogs` calls one `logs` call of the reader makes for a range the endpoint bounds. */
|
|
155
|
+
export const LOG_CALLS_MAX = 20n;
|
|
156
|
+
/** The most blocks one `eth_getLogs` call may span, as each endpoint stated it in a refusal, for the process's life. */
|
|
157
|
+
const spans = new Map();
|
|
158
|
+
/** Forgets the bound each endpoint stated on a log query's range. */
|
|
159
|
+
export function forgetEndpoints() {
|
|
160
|
+
spans.clear();
|
|
161
|
+
}
|
|
61
162
|
function logOf(l, what) {
|
|
62
163
|
if (!isObject(l) || typeof l["address"] !== "string" || !ADDRESS.test(l["address"]) || !Array.isArray(l["topics"])) {
|
|
63
164
|
malformed(what);
|
|
@@ -75,7 +176,25 @@ function tag(at) {
|
|
|
75
176
|
}
|
|
76
177
|
/** lcp's reader of `network` over the JSON-RPC endpoint `rpc`, each call ending by `deadline`. */
|
|
77
178
|
export function evmReader(network, rpc, deadline) {
|
|
78
|
-
|
|
179
|
+
// A read the endpoint refuses for its rate is asked again after the pause it names, or a short one that doubles; one
|
|
180
|
+
// it refuses because the node that answered is behind the range, at once and then after short pauses, since another
|
|
181
|
+
// of the endpoint's nodes may hold it. Neither is asked again past the deadline: the refusal then stands.
|
|
182
|
+
const call = async (method, params) => {
|
|
183
|
+
for (let tries = 0;; tries++) {
|
|
184
|
+
try {
|
|
185
|
+
return await rpcCall(rpc, method, params, deadline);
|
|
186
|
+
}
|
|
187
|
+
catch (e) {
|
|
188
|
+
if (!(e instanceof Refusal) || (e.why !== "rate" && e.why !== "ahead") || tries === READ_RETRIES)
|
|
189
|
+
throw e;
|
|
190
|
+
const pause = e.why === "ahead" ? 50 * tries : (e.waitMs ?? 250 * 2 ** tries);
|
|
191
|
+
if (pause > PAUSE_MAX_MS || performance.now() + pause >= deadline)
|
|
192
|
+
throw e;
|
|
193
|
+
if (pause > 0)
|
|
194
|
+
await sleep(pause);
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
};
|
|
79
198
|
const block = async (at) => {
|
|
80
199
|
const b = await call("eth_getBlockByNumber", [tag(at), false]);
|
|
81
200
|
if (b === null)
|
|
@@ -122,27 +241,63 @@ export function evmReader(network, rpc, deadline) {
|
|
|
122
241
|
async call(to, input, at) {
|
|
123
242
|
return data(await call("eth_call", [{ to, data: input }, hex(at)]), "call result");
|
|
124
243
|
},
|
|
244
|
+
/**
|
|
245
|
+
* `eth_getLogs` over the range asked, in as many calls as the endpoint's bound on a call's range needs: the bound
|
|
246
|
+
* the endpoint stated before, or states now in refusing a call (the blocks it names, else half the range refused).
|
|
247
|
+
* A range that would take more than `LOG_CALLS_MAX` calls rejects `too-large`, as one the endpoint refuses at one
|
|
248
|
+
* block does, so that a caller that pages asks for less. Nothing of a range is answered unless every call of it
|
|
249
|
+
* was: a call that fails rejects the whole.
|
|
250
|
+
*/
|
|
125
251
|
async logs(filter) {
|
|
126
|
-
const
|
|
127
|
-
topics: filter.topics,
|
|
128
|
-
|
|
129
|
-
|
|
252
|
+
const one = async (fromBlock, toBlock) => {
|
|
253
|
+
const params = { topics: filter.topics, fromBlock: hex(fromBlock), toBlock: hex(toBlock) };
|
|
254
|
+
if (filter.address !== undefined)
|
|
255
|
+
params["address"] = filter.address;
|
|
256
|
+
const logs = await call("eth_getLogs", [params]);
|
|
257
|
+
if (!Array.isArray(logs))
|
|
258
|
+
malformed("logs");
|
|
259
|
+
return logs.map((l) => {
|
|
260
|
+
if (!isObject(l) || typeof l["transactionHash"] !== "string" || !HASH.test(l["transactionHash"]))
|
|
261
|
+
malformed("log");
|
|
262
|
+
return {
|
|
263
|
+
...logOf(l, "log"),
|
|
264
|
+
transactionHash: l["transactionHash"].toLowerCase(),
|
|
265
|
+
blockNumber: quantity(l["blockNumber"], "log block"),
|
|
266
|
+
logIndex: Number(quantity(l["logIndex"], "log index")),
|
|
267
|
+
};
|
|
268
|
+
});
|
|
130
269
|
};
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
270
|
+
const out = [];
|
|
271
|
+
// The bound for this call alone, where the endpoint refuses a range without stating one.
|
|
272
|
+
let halved;
|
|
273
|
+
let calls = 0n;
|
|
274
|
+
let lo = filter.fromBlock;
|
|
275
|
+
while (lo <= filter.toBlock) {
|
|
276
|
+
const stated = spans.get(rpc);
|
|
277
|
+
const span = halved !== undefined && (stated === undefined || halved < stated) ? halved : stated;
|
|
278
|
+
const hi = span === undefined || lo + span - 1n > filter.toBlock ? filter.toBlock : lo + span - 1n;
|
|
279
|
+
if (span !== undefined && calls + (filter.toBlock - lo) / span + 1n > LOG_CALLS_MAX) {
|
|
280
|
+
throw new Refusal("too-large", `the range takes more than ${LOG_CALLS_MAX} calls of ${span} blocks`, "range", span);
|
|
281
|
+
}
|
|
282
|
+
let page;
|
|
283
|
+
try {
|
|
284
|
+
page = await one(lo, hi);
|
|
285
|
+
}
|
|
286
|
+
catch (e) {
|
|
287
|
+
if (!(e instanceof Refusal) || e.why !== "range" || hi === lo)
|
|
288
|
+
throw e;
|
|
289
|
+
const width = hi - lo + 1n;
|
|
290
|
+
if (e.span !== undefined && e.span < width)
|
|
291
|
+
spans.set(rpc, e.span);
|
|
292
|
+
else
|
|
293
|
+
halved = width / 2n;
|
|
294
|
+
continue;
|
|
295
|
+
}
|
|
296
|
+
calls++;
|
|
297
|
+
out.push(...page);
|
|
298
|
+
lo = hi + 1n;
|
|
299
|
+
}
|
|
300
|
+
return out;
|
|
146
301
|
},
|
|
147
302
|
block,
|
|
148
303
|
};
|
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Whether a different transaction took a nonce of a key, from what one finalized block, named by its hash, states.
|
|
3
|
+
*
|
|
4
|
+
* The answer rests on positive facts only. A node that finds nothing by a hash, or answers a count from one block and
|
|
5
|
+
* nothing for the block, is no evidence of anything, so it gives no answer. Every read that decides names its block by
|
|
6
|
+
* hash (EIP-1898, with `requireCanonical`), which has one answer on every node that holds it, or none: the key's count
|
|
7
|
+
* at the finalized block has passed the nonce; the key's count passes the nonce in block B, above it at B and at or
|
|
8
|
+
* below it at B's parent; B is an ancestor of the finalized block, shown from the finalized block's own hash
|
|
9
|
+
* (`ancestor`); and B, read by its hash, holds the key's transaction at that nonce, whose hash is the answer. Reads of
|
|
10
|
+
* the count by block number only choose which block B to read. The counts at B and its parent need the node to hold
|
|
11
|
+
* the state of those blocks, which a node that keeps recent state only drops: B, once those counts show it, is kept
|
|
12
|
+
* (`Search`), and so is the range a search narrows, so that the proof is had where any one proof found B while the node
|
|
13
|
+
* held that state.
|
|
14
|
+
*
|
|
15
|
+
* A node may serve no count where the proof asks one: it keeps less state than its finalized block lies below its head,
|
|
16
|
+
* or its endpoint refuses state by block number, or at all. A count it does not serve is no evidence, and the proof
|
|
17
|
+
* then rests on what it does serve, which shows the same: a block that is an ancestor of the finalized block
|
|
18
|
+
* (`ancestor`) and, read by its hash, holds the key's transaction at the nonce, took the nonce there, whatever counts
|
|
19
|
+
* the node serves. Such a block is found by the counts the node does serve (above the blocks it serves none at; from
|
|
20
|
+
* its latest block where it serves none at the finalized one, so that B is kept while its state is held, before it is
|
|
21
|
+
* final); from a transaction of ours that the node shows mined, which names its block; or among the blocks above the
|
|
22
|
+
* one given (`NonceProofAt.from`), read whole, a few on each proof. The counts by hash at B and its parent are asked
|
|
23
|
+
* however B was found, until the node has served both, and one it serves never contradicts B. Where nothing finds B the proof gives no answer
|
|
24
|
+
* and names the reads the node did not serve (`detail`); what it kept is no obstacle to a later proof at a node that
|
|
25
|
+
* serves them.
|
|
26
|
+
*
|
|
27
|
+
* Kept blocks (anchors, `Anchors`) are trusted on the node's own answer, by number, that each is its block at that number:
|
|
28
|
+
* a proof that goes on from one rests on that answer and not on the finalized block's hash alone. So the proof assumes the
|
|
29
|
+
* node gives one view of the chain: an endpoint that answers from backends on different chains (one still on a history
|
|
30
|
+
* since replaced, as after a reset) can show a block of that other history as final.
|
|
31
|
+
*/
|
|
32
|
+
import { type Hex } from "@integraledger/lcp/evm";
|
|
33
|
+
/**
|
|
34
|
+
* What the finalized chain shows of a key's nonce: `taken-by-another`, a transaction whose hash is none of `ours` took
|
|
35
|
+
* it, in the block named; `ours`, one of `ours` took it, in the block named; `no-answer`, where the chain as read shows
|
|
36
|
+
* neither. `no-answer` says nothing about the chain: the key's count at the finalized block has not passed the nonce
|
|
37
|
+
* (`not-finalized`), a read failed or the node does not hold the block or its state (`unreadable`), or the block in
|
|
38
|
+
* which the count passed the nonce, read by its hash, holds no transaction of the key at the nonce (`unshown`).
|
|
39
|
+
* `detail` names the reads the node did not serve, for the operator: a node that serves them (one that keeps the state
|
|
40
|
+
* of older blocks) gives the proof on a later repeat.
|
|
41
|
+
*/
|
|
42
|
+
export type NonceProof = {
|
|
43
|
+
readonly answer: "taken-by-another";
|
|
44
|
+
readonly block: Hex;
|
|
45
|
+
readonly transaction: Hex;
|
|
46
|
+
} | {
|
|
47
|
+
readonly answer: "ours";
|
|
48
|
+
readonly block: Hex;
|
|
49
|
+
readonly transaction: Hex;
|
|
50
|
+
} | {
|
|
51
|
+
readonly answer: "no-answer";
|
|
52
|
+
readonly reason: "not-finalized" | "unreadable" | "unshown";
|
|
53
|
+
readonly detail?: string;
|
|
54
|
+
};
|
|
55
|
+
/** Where the search for the block starts, and when each node call ends. */
|
|
56
|
+
export interface NonceProofAt {
|
|
57
|
+
/** A `performance.now()` time by which every node call ends. */
|
|
58
|
+
readonly deadline: number;
|
|
59
|
+
/**
|
|
60
|
+
* A block at or below which the nonce was not yet taken, as the block the node's head was when the first transaction
|
|
61
|
+
* at the nonce was signed. It only chooses where the search reads first, and which blocks are read whole where the
|
|
62
|
+
* node serves no count: a block at which the nonce was already taken (a number read from a node on another fork)
|
|
63
|
+
* costs reads, and changes no answer.
|
|
64
|
+
*/
|
|
65
|
+
readonly from?: bigint;
|
|
66
|
+
}
|
|
67
|
+
/** The most calls this module has under way at one endpoint at once, whatever number of proofs run. */
|
|
68
|
+
export declare const CALLS_AT_ONCE = 4;
|
|
69
|
+
/**
|
|
70
|
+
* EIP-2935's history contract: `get(n)` answers the hash of block n, for the 8191 blocks below the block it is read at.
|
|
71
|
+
* Each block's first act writes its parent's hash into a ring of 8191 slots, at the parent's number modulo 8191, so the
|
|
72
|
+
* slot of the block 8191 below a block is the one the next block writes over with that block's own hash. A node that
|
|
73
|
+
* runs a call at a block over the state the next block starts from answers that hash, the block's own, for the block
|
|
74
|
+
* 8191 below it. So this module asks for a block at most 8190 below the block it reads at (`HISTORY_REACH`), whose slot
|
|
75
|
+
* no block up to and including the next has written since.
|
|
76
|
+
*/
|
|
77
|
+
export declare const HISTORY_CONTRACT = "0x0000F90827F1C53a10cb7A02335B175320002935";
|
|
78
|
+
/**
|
|
79
|
+
* The most block headers one proof reads one by one, by each one's parent's hash, where the history contract does not
|
|
80
|
+
* answer. A walk that stops short keeps where it stopped as an anchor (`Anchors`), and the next proof walks on from it.
|
|
81
|
+
*/
|
|
82
|
+
export declare const ANCESTRY_WALK = 1024;
|
|
83
|
+
/**
|
|
84
|
+
* A block shown on the finalized chain: reached by parent hashes from a finalized block, or answered by the history
|
|
85
|
+
* contract read at a block so shown, so finalized for ever.
|
|
86
|
+
*/
|
|
87
|
+
export interface Anchor {
|
|
88
|
+
readonly number: bigint;
|
|
89
|
+
readonly hash: Hex;
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Where a network's proven anchors are kept between proofs (the module's durable state): where a walk from a finalized
|
|
93
|
+
* block by parent hashes stopped, and the lowest block the history contract showed on a proof that walks on. The walk
|
|
94
|
+
* starts from the lowest one at or above its target, and the hops from it where it lies 8190 or more below the finalized
|
|
95
|
+
* block, so that a block any depth below the finalized one is shown on a later proof, `ANCESTRY_WALK` headers further
|
|
96
|
+
* each time. Each key's nonce's search (`Search`) is kept there too, so that a later proof needs no state the node has
|
|
97
|
+
* since dropped.
|
|
98
|
+
*/
|
|
99
|
+
export interface Anchors {
|
|
100
|
+
get(): Promise<readonly Anchor[]>;
|
|
101
|
+
add(a: Anchor): Promise<void>;
|
|
102
|
+
/** Drops an anchor the node no longer holds (a node that never held it): it starts no walk. */
|
|
103
|
+
remove(a: Anchor): Promise<void>;
|
|
104
|
+
/**
|
|
105
|
+
* Drops every anchor kept: where the node's own block at a kept anchor's number is another block, the chain those
|
|
106
|
+
* anchors were shown on is not the node's (a network whose history was replaced, as a reset), and none is trusted.
|
|
107
|
+
*/
|
|
108
|
+
clear(): Promise<void>;
|
|
109
|
+
/** The search kept for `key`'s nonce `nonce`; undefined where none is. */
|
|
110
|
+
search(key: Hex, nonce: bigint): Promise<Search | undefined>;
|
|
111
|
+
/**
|
|
112
|
+
* Keeps the search for `key`'s nonce `nonce`; undefined drops it. A range never replaces a block kept (`passedIn`),
|
|
113
|
+
* which another proof of the same nonce may have kept since this one read the search; nor does a drop, unless the
|
|
114
|
+
* block kept is `dropping`, the one the proof that drops it read and refused.
|
|
115
|
+
*/
|
|
116
|
+
keepSearch(key: Hex, nonce: bigint, s: Search | undefined, dropping?: Anchor): Promise<void>;
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* What a proof's search for the block in which a key's count passed a nonce found, kept so that a later proof goes on
|
|
120
|
+
* from it: a node that keeps recent state only answers no count at an older block, so a search repeated from the start
|
|
121
|
+
* on each proof finds nothing once the node has dropped that state. `below` and `above` are the range it narrowed, by
|
|
122
|
+
* block number, the count read by number at or below the nonce at `below` and above it at `above`: they only choose
|
|
123
|
+
* which block the next proof reads. `passedIn` is the block itself, where the reads by its hash showed the count above
|
|
124
|
+
* the nonce there and at or below it at its parent (a block named by its hash has those counts for ever), or, where
|
|
125
|
+
* the node served one of them no more, the block's own transactions showed the key's at the nonce. It may be kept
|
|
126
|
+
* before it is final. Every other fact a proof rests on (that the block is at or below the finalized one; its ancestry
|
|
127
|
+
* from it; its transactions) is read on every proof.
|
|
128
|
+
*/
|
|
129
|
+
export interface Search {
|
|
130
|
+
readonly below?: bigint;
|
|
131
|
+
readonly above?: bigint;
|
|
132
|
+
readonly passedIn?: Anchor;
|
|
133
|
+
/** Whether the counts by `passedIn`'s hash and its parent's were served and agreed: they are read no more. */
|
|
134
|
+
readonly counted?: boolean;
|
|
135
|
+
/** The highest block above `below` at which the node served no count: the search goes on above it. */
|
|
136
|
+
readonly unserved?: bigint;
|
|
137
|
+
/** The blocks read whole for the key's transaction at the nonce, above the block `from` a proof was given. */
|
|
138
|
+
readonly scanned?: {
|
|
139
|
+
readonly from: bigint;
|
|
140
|
+
readonly to: bigint;
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
/** Registers where the proven anchors of the endpoint `rpc` are kept; undefined, none. */
|
|
144
|
+
export declare function registerAnchors(rpc: string, anchors: Anchors | undefined): void;
|
|
145
|
+
/**
|
|
146
|
+
* Anchors kept as a list in a module's durable state under `key`, and each search under `key`, the key's address and
|
|
147
|
+
* the nonce, as JSON (numbers as decimal strings).
|
|
148
|
+
*/
|
|
149
|
+
export declare function anchorsIn(state: {
|
|
150
|
+
get(key: string): Promise<unknown>;
|
|
151
|
+
set(key: string, value: never): Promise<void>;
|
|
152
|
+
swap(key: string, expected: never, value: never): Promise<boolean>;
|
|
153
|
+
}, key: string): Anchors;
|
|
154
|
+
/**
|
|
155
|
+
* What the finalized chain shows of `key`'s nonce `nonce`, `ours` being the hashes of every transaction signed at that
|
|
156
|
+
* nonce. Never rejects: a read that fails is `no-answer`.
|
|
157
|
+
*/
|
|
158
|
+
export declare function nonceProof(rpc: string, key: Hex, nonce: bigint, ours: readonly string[], at: NonceProofAt): Promise<NonceProof>;
|
|
159
|
+
/**
|
|
160
|
+
* Whether the key's count at the block the node names `finalized`, read by that block's hash, has passed `nonce`: then
|
|
161
|
+
* a transaction of the key at that nonce is in a finalized block, and no other at it can ever land. Where the node does
|
|
162
|
+
* not serve that block's state, the same is shown by other facts or not at all: passed, where the block a proof kept
|
|
163
|
+
* for that nonce (`Search`), read by its hash, holds the key's transaction at it and is an ancestor of the finalized
|
|
164
|
+
* block; not passed, where the count at the node's latest block, by its hash, is at or below the nonce and the
|
|
165
|
+
* finalized block is an ancestor of that block. A read that fails, and a count neither shows, rejects. `at`, where
|
|
166
|
+
* given, is the finalized block the caller read, by its number and hash, which the answer is then about.
|
|
167
|
+
*/
|
|
168
|
+
export declare function passedAtFinalized(rpc: string, key: Hex, nonce: bigint, deadline: number, at?: {
|
|
169
|
+
readonly number: bigint;
|
|
170
|
+
readonly hash: Hex;
|
|
171
|
+
}): Promise<boolean>;
|
|
172
|
+
/** A receipt the finalized chain shows: its status, and the block that holds it, by number and hash. */
|
|
173
|
+
export interface FinalizedReceipt {
|
|
174
|
+
readonly status: 0 | 1;
|
|
175
|
+
readonly blockNumber: bigint;
|
|
176
|
+
readonly blockHash: Hex;
|
|
177
|
+
}
|
|
178
|
+
/**
|
|
179
|
+
* The receipt of `transaction` where the finalized chain shows it: its block, named by the receipt's `blockHash`, at or
|
|
180
|
+
* below the block the node names `finalized` and shown an ancestor of it from the finalized block's own hash
|
|
181
|
+
* (`ancestor`), or that block itself. Undefined where the node shows no receipt, its block is above the finalized one,
|
|
182
|
+
* or is not shown on the finalized chain (a node behind, or on another fork, answers so), or a read fails: nothing
|
|
183
|
+
* final rests on it. Where the proof goes on from a kept anchor, it rests on the node's answer by number that the anchor
|
|
184
|
+
* is its block there (see the module's note on one view of the chain).
|
|
185
|
+
*/
|
|
186
|
+
export declare function finalizedReceipt(rpc: string, transaction: Hex, deadline: number): Promise<FinalizedReceipt | undefined>;
|