@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
|
@@ -0,0 +1,779 @@
|
|
|
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 { ReaderError } from "@integraledger/lcp/evm";
|
|
33
|
+
import { Refusal, rpcCall } from "./node.js";
|
|
34
|
+
const HASH = /^0x[0-9a-fA-F]{64}$/;
|
|
35
|
+
const QUANTITY = /^0x[0-9a-fA-F]{1,64}$/;
|
|
36
|
+
class Unreadable extends Error {
|
|
37
|
+
}
|
|
38
|
+
/** A proof that ends where it is, what it found kept: its deadline is spent, or the node answers that it is asked too fast. */
|
|
39
|
+
class Stopped extends Error {
|
|
40
|
+
}
|
|
41
|
+
/** The most blocks one proof reads whole for the key's transaction at the nonce, and the most above the block given. */
|
|
42
|
+
const SCAN_BLOCKS = 8;
|
|
43
|
+
const SCAN_MOST = 256;
|
|
44
|
+
/** The most calls this module has under way at one endpoint at once, whatever number of proofs run. */
|
|
45
|
+
export const CALLS_AT_ONCE = 4;
|
|
46
|
+
/** The pause between this module's calls to an endpoint that answered it is asked too fast: the first, and the longest, in ms. */
|
|
47
|
+
const PAUSE_FIRST = 100;
|
|
48
|
+
const PAUSE_MOST = 1_000;
|
|
49
|
+
/** How long after its last such answer an endpoint's pause is halved, in ms. */
|
|
50
|
+
const PAUSE_EASES = 10_000;
|
|
51
|
+
/** How often one call is asked again after such an answer, where the proof's deadline leaves the time. */
|
|
52
|
+
const ASKED_AGAIN = 3;
|
|
53
|
+
/**
|
|
54
|
+
* A node's answer that it is asked too fast, in words: HTTP 429, or a JSON-RPC error that says so. The node call's own
|
|
55
|
+
* reading of a rate limit (`Refusal` `why` "rate"), by HTTP status 429, JSON-RPC code 429 or such words, comes first.
|
|
56
|
+
*/
|
|
57
|
+
const TOO_FAST = /status 429|rate.?limit|request limit|too many requests/i;
|
|
58
|
+
/** Each endpoint's pace, by its URL. */
|
|
59
|
+
const PACES = new Map();
|
|
60
|
+
function paceOf(rpc) {
|
|
61
|
+
let p = PACES.get(rpc);
|
|
62
|
+
if (p === undefined) {
|
|
63
|
+
p = { flying: 0, waiting: [], pause: 0, next: 0, slowed: 0 };
|
|
64
|
+
PACES.set(rpc, p);
|
|
65
|
+
}
|
|
66
|
+
return p;
|
|
67
|
+
}
|
|
68
|
+
/** Runs one node call at the endpoint's pace: at most `CALLS_AT_ONCE` under way, each started after the pause. */
|
|
69
|
+
async function paced(rpc, deadline, f) {
|
|
70
|
+
const p = paceOf(rpc);
|
|
71
|
+
for (;;) {
|
|
72
|
+
const now = performance.now();
|
|
73
|
+
if (now >= deadline)
|
|
74
|
+
throw new Stopped("the proof's time is spent");
|
|
75
|
+
if (p.pause > 0 && now - p.slowed > PAUSE_EASES) {
|
|
76
|
+
p.pause = p.pause > PAUSE_FIRST ? p.pause / 2 : 0;
|
|
77
|
+
p.slowed = now;
|
|
78
|
+
}
|
|
79
|
+
if (p.flying < CALLS_AT_ONCE && now >= p.next)
|
|
80
|
+
break;
|
|
81
|
+
// Until a call under way ends, the pause is over, or the proof's time is spent, whichever is first.
|
|
82
|
+
await new Promise((resolve) => {
|
|
83
|
+
const until = p.flying >= CALLS_AT_ONCE ? deadline : Math.min(deadline, p.next);
|
|
84
|
+
const timer = setTimeout(done, Math.max(0, until - now));
|
|
85
|
+
function done() {
|
|
86
|
+
clearTimeout(timer);
|
|
87
|
+
resolve();
|
|
88
|
+
}
|
|
89
|
+
if (p.flying >= CALLS_AT_ONCE)
|
|
90
|
+
p.waiting.push(done);
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
p.flying += 1;
|
|
94
|
+
p.next = performance.now() + p.pause;
|
|
95
|
+
try {
|
|
96
|
+
return await f();
|
|
97
|
+
}
|
|
98
|
+
finally {
|
|
99
|
+
p.flying -= 1;
|
|
100
|
+
for (const w of p.waiting.splice(0))
|
|
101
|
+
w();
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
/** What an error says, for a reason an operator reads. */
|
|
105
|
+
function textOf(e) {
|
|
106
|
+
return (e instanceof Error ? e.message : String(e)).slice(0, 160);
|
|
107
|
+
}
|
|
108
|
+
/** The reads a proof's node did not serve, as one line: each once, the first few. */
|
|
109
|
+
function detailOf(unserved) {
|
|
110
|
+
return `the node did not serve: ${[...new Set(unserved)].slice(0, 4).join("; ")}`;
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* This module's calls to the endpoint `rpc`, each ending by `deadline`: paced (`paced`), and asked again after the
|
|
114
|
+
* endpoint's pause where it answers that it is asked too fast, which slows every later call to it. A call whose time
|
|
115
|
+
* is spent, or that is still refused as too fast, ends the proof (`Stopped`); any other failure rejects as the node
|
|
116
|
+
* call does.
|
|
117
|
+
*/
|
|
118
|
+
function callOf(rpc, deadline) {
|
|
119
|
+
return async (method, params) => {
|
|
120
|
+
for (let again = 0;; again++) {
|
|
121
|
+
try {
|
|
122
|
+
return await paced(rpc, deadline, () => rpcCall(rpc, method, params, deadline));
|
|
123
|
+
}
|
|
124
|
+
catch (e) {
|
|
125
|
+
if (e instanceof Stopped)
|
|
126
|
+
throw e;
|
|
127
|
+
const text = e instanceof Error ? e.message : String(e);
|
|
128
|
+
if ((e instanceof Refusal && e.why === "rate") || TOO_FAST.test(text)) {
|
|
129
|
+
const p = paceOf(rpc);
|
|
130
|
+
const now = performance.now();
|
|
131
|
+
// Longer only where the endpoint refuses a call that waited the pause: calls refused at one moment count once.
|
|
132
|
+
if (p.pause === 0 || now - p.slowed >= p.pause)
|
|
133
|
+
p.pause = Math.min(Math.max(p.pause * 2, PAUSE_FIRST), PAUSE_MOST);
|
|
134
|
+
p.slowed = now;
|
|
135
|
+
p.next = Math.max(p.next, now + p.pause);
|
|
136
|
+
if (again < ASKED_AGAIN)
|
|
137
|
+
continue;
|
|
138
|
+
throw new Stopped(`the node answers that it is asked too fast: ${textOf(e)}`, { cause: e });
|
|
139
|
+
}
|
|
140
|
+
if ((e instanceof ReaderError && e.kind === "timeout") || /\bspent\b/.test(text))
|
|
141
|
+
throw new Stopped(`the proof's time is spent: ${textOf(e)}`, { cause: e });
|
|
142
|
+
throw e;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* EIP-2935's history contract: `get(n)` answers the hash of block n, for the 8191 blocks below the block it is read at.
|
|
149
|
+
* 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
|
|
150
|
+
* 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
|
|
151
|
+
* runs a call at a block over the state the next block starts from answers that hash, the block's own, for the block
|
|
152
|
+
* 8191 below it. So this module asks for a block at most 8190 below the block it reads at (`HISTORY_REACH`), whose slot
|
|
153
|
+
* no block up to and including the next has written since.
|
|
154
|
+
*/
|
|
155
|
+
export const HISTORY_CONTRACT = "0x0000F90827F1C53a10cb7A02335B175320002935";
|
|
156
|
+
const HISTORY_REACH = 8190n;
|
|
157
|
+
/** A walk by headers of more than this many blocks is first asked of the history contract read at the node's latest block. */
|
|
158
|
+
const WALK_SHORT = 32n;
|
|
159
|
+
/** The most hops one proof makes through the history contract; below them the proof walks on by headers. */
|
|
160
|
+
const HISTORY_HOPS = 1024;
|
|
161
|
+
/**
|
|
162
|
+
* The most block headers one proof reads one by one, by each one's parent's hash, where the history contract does not
|
|
163
|
+
* answer. A walk that stops short keeps where it stopped as an anchor (`Anchors`), and the next proof walks on from it.
|
|
164
|
+
*/
|
|
165
|
+
export const ANCESTRY_WALK = 1024;
|
|
166
|
+
/** The anchors of each node endpoint, by its URL, as the module that reads it registered them. */
|
|
167
|
+
const ANCHORS = new Map();
|
|
168
|
+
/** Registers where the proven anchors of the endpoint `rpc` are kept; undefined, none. */
|
|
169
|
+
export function registerAnchors(rpc, anchors) {
|
|
170
|
+
if (anchors === undefined)
|
|
171
|
+
ANCHORS.delete(rpc);
|
|
172
|
+
else
|
|
173
|
+
ANCHORS.set(rpc, anchors);
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* The most anchors kept per network. A proof keeps one only where its walk stops short of its target, or where the
|
|
177
|
+
* history contract showed a block it walks on from, and one per `ANCESTRY_WALK` blocks of the chain (the one of the
|
|
178
|
+
* same span replaces it), so a deep walk under way keeps its own progress whatever other proofs run. Past this many, the highest are dropped: a block that high is reached again
|
|
179
|
+
* from the finalized block.
|
|
180
|
+
*/
|
|
181
|
+
const KEPT_ANCHORS = 16_384;
|
|
182
|
+
/** The most times a change to the kept anchors is read and swapped in before it is given up (`anchorsIn`). */
|
|
183
|
+
const ANCHOR_WRITES = 16;
|
|
184
|
+
/**
|
|
185
|
+
* Anchors kept as a list in a module's durable state under `key`, and each search under `key`, the key's address and
|
|
186
|
+
* the nonce, as JSON (numbers as decimal strings).
|
|
187
|
+
*/
|
|
188
|
+
export function anchorsIn(state, key) {
|
|
189
|
+
const searchAt = (k, nonce) => `${key}:search:${k.toLowerCase()}:${nonce.toString(10)}`;
|
|
190
|
+
const number = (v) => (typeof v === "string" && /^[0-9]+$/.test(v) ? BigInt(v) : undefined);
|
|
191
|
+
const read = async () => listOf(await state.get(key));
|
|
192
|
+
const listOf = (v) => {
|
|
193
|
+
if (!Array.isArray(v))
|
|
194
|
+
return [];
|
|
195
|
+
const out = [];
|
|
196
|
+
for (const e of v) {
|
|
197
|
+
if (isObject(e) && typeof e["number"] === "string" && /^[0-9]+$/.test(e["number"]) && typeof e["hash"] === "string" && HASH.test(e["hash"])) {
|
|
198
|
+
out.push({ number: BigInt(e["number"]), hash: e["hash"].toLowerCase() });
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
return out;
|
|
202
|
+
};
|
|
203
|
+
/**
|
|
204
|
+
* Writes the list `f` makes of the list stored, swapped from the value read, so that two proofs writing at once never
|
|
205
|
+
* lose one's change, nor bring back an anchor the other dropped: where another wrote between the read and the write,
|
|
206
|
+
* it reads again. After `ANCHOR_WRITES` tries it gives up: an anchor is progress only, never a proof by itself
|
|
207
|
+
* (`ancestor` reads the node's own block at its number before it uses one).
|
|
208
|
+
*/
|
|
209
|
+
const change = async (f) => {
|
|
210
|
+
for (let i = 0; i < ANCHOR_WRITES; i++) {
|
|
211
|
+
const now = await state.get(key);
|
|
212
|
+
const next = f(listOf(now)).map((x) => ({ number: x.number.toString(10), hash: x.hash }));
|
|
213
|
+
if (await state.swap(key, now, next))
|
|
214
|
+
return;
|
|
215
|
+
}
|
|
216
|
+
};
|
|
217
|
+
return {
|
|
218
|
+
get: read,
|
|
219
|
+
async add(a) {
|
|
220
|
+
const span = a.number / BigInt(ANCESTRY_WALK);
|
|
221
|
+
await change((all) => {
|
|
222
|
+
const next = all.filter((x) => x.number / BigInt(ANCESTRY_WALK) !== span);
|
|
223
|
+
next.push(a);
|
|
224
|
+
next.sort((x, y) => (x.number < y.number ? -1 : x.number > y.number ? 1 : 0));
|
|
225
|
+
return next.slice(0, KEPT_ANCHORS);
|
|
226
|
+
});
|
|
227
|
+
},
|
|
228
|
+
async remove(a) {
|
|
229
|
+
await change((all) => all.filter((x) => !(x.number === a.number && x.hash === a.hash)));
|
|
230
|
+
},
|
|
231
|
+
async clear() {
|
|
232
|
+
await change(() => []);
|
|
233
|
+
},
|
|
234
|
+
async search(k, nonce) {
|
|
235
|
+
const v = await state.get(searchAt(k, nonce));
|
|
236
|
+
if (!isObject(v))
|
|
237
|
+
return undefined;
|
|
238
|
+
const below = number(v["below"]);
|
|
239
|
+
const above = number(v["above"]);
|
|
240
|
+
const p = v["passedIn"];
|
|
241
|
+
const at = isObject(p) ? number(p["number"]) : undefined;
|
|
242
|
+
const passedIn = isObject(p) && at !== undefined && typeof p["hash"] === "string" && HASH.test(p["hash"]) ? { number: at, hash: p["hash"].toLowerCase() } : undefined;
|
|
243
|
+
const unserved = number(v["unserved"]);
|
|
244
|
+
const sc = v["scanned"];
|
|
245
|
+
const scannedFrom = isObject(sc) ? number(sc["from"]) : undefined;
|
|
246
|
+
const scannedTo = isObject(sc) ? number(sc["to"]) : undefined;
|
|
247
|
+
return {
|
|
248
|
+
...(below === undefined ? {} : { below }),
|
|
249
|
+
...(above === undefined ? {} : { above }),
|
|
250
|
+
...(passedIn === undefined ? {} : { passedIn }),
|
|
251
|
+
...(passedIn !== undefined && v["counted"] === true ? { counted: true } : {}),
|
|
252
|
+
...(unserved === undefined ? {} : { unserved }),
|
|
253
|
+
...(scannedFrom === undefined || scannedTo === undefined ? {} : { scanned: { from: scannedFrom, to: scannedTo } }),
|
|
254
|
+
};
|
|
255
|
+
},
|
|
256
|
+
async keepSearch(k, nonce, s, dropping) {
|
|
257
|
+
const v = s === undefined
|
|
258
|
+
? null
|
|
259
|
+
: {
|
|
260
|
+
...(s.below === undefined ? {} : { below: s.below.toString(10) }),
|
|
261
|
+
...(s.above === undefined ? {} : { above: s.above.toString(10) }),
|
|
262
|
+
...(s.passedIn === undefined ? {} : { passedIn: { number: s.passedIn.number.toString(10), hash: s.passedIn.hash } }),
|
|
263
|
+
...(s.counted === true ? { counted: true } : {}),
|
|
264
|
+
...(s.unserved === undefined ? {} : { unserved: s.unserved.toString(10) }),
|
|
265
|
+
...(s.scanned === undefined ? {} : { scanned: { from: s.scanned.from.toString(10), to: s.scanned.to.toString(10) } }),
|
|
266
|
+
};
|
|
267
|
+
// Swapped from the value read, so that a range written by one proof never lands over a block another kept.
|
|
268
|
+
for (;;) {
|
|
269
|
+
const now = await state.get(searchAt(k, nonce));
|
|
270
|
+
const there = isObject(now) && isObject(now["passedIn"]) ? now["passedIn"] : undefined;
|
|
271
|
+
if (there !== undefined && (s === undefined ? !(dropping !== undefined && there["number"] === dropping.number.toString(10) && there["hash"] === dropping.hash) : s.passedIn === undefined))
|
|
272
|
+
return;
|
|
273
|
+
if (await state.swap(searchAt(k, nonce), now, v))
|
|
274
|
+
return;
|
|
275
|
+
}
|
|
276
|
+
},
|
|
277
|
+
};
|
|
278
|
+
}
|
|
279
|
+
function isObject(v) {
|
|
280
|
+
return typeof v === "object" && v !== null && !Array.isArray(v);
|
|
281
|
+
}
|
|
282
|
+
function quantity(v) {
|
|
283
|
+
if (typeof v !== "string" || !QUANTITY.test(v))
|
|
284
|
+
throw new Unreadable("not a quantity");
|
|
285
|
+
return BigInt(v);
|
|
286
|
+
}
|
|
287
|
+
function hash(v) {
|
|
288
|
+
if (typeof v !== "string" || !HASH.test(v))
|
|
289
|
+
throw new Unreadable("not a hash");
|
|
290
|
+
return v.toLowerCase();
|
|
291
|
+
}
|
|
292
|
+
function hex(v) {
|
|
293
|
+
return `0x${v.toString(16)}`;
|
|
294
|
+
}
|
|
295
|
+
/** A block as `eth_getBlockBy*` answers it: its hash, number and parent's hash, and its transactions where asked whole. */
|
|
296
|
+
function headerOf(b) {
|
|
297
|
+
if (!isObject(b))
|
|
298
|
+
throw new Unreadable("no block");
|
|
299
|
+
const transactions = Array.isArray(b["transactions"]) ? b["transactions"] : [];
|
|
300
|
+
return { hash: hash(b["hash"]), number: quantity(b["number"]), parentHash: hash(b["parentHash"]), transactions };
|
|
301
|
+
}
|
|
302
|
+
/** The transaction of `key` (in lower case) at `nonce` among a block's transactions, as the block read whole states them. */
|
|
303
|
+
function takerOf(b, key, nonce) {
|
|
304
|
+
for (const t of b.transactions) {
|
|
305
|
+
if (!isObject(t) || typeof t["from"] !== "string" || t["from"].toLowerCase() !== key)
|
|
306
|
+
continue;
|
|
307
|
+
if (quantity(t["nonce"]) !== nonce)
|
|
308
|
+
continue;
|
|
309
|
+
return hash(t["hash"]);
|
|
310
|
+
}
|
|
311
|
+
return undefined;
|
|
312
|
+
}
|
|
313
|
+
/**
|
|
314
|
+
* What the finalized chain shows of `key`'s nonce `nonce`, `ours` being the hashes of every transaction signed at that
|
|
315
|
+
* nonce. Never rejects: a read that fails is `no-answer`.
|
|
316
|
+
*/
|
|
317
|
+
export async function nonceProof(rpc, key, nonce, ours, at) {
|
|
318
|
+
const unserved = [];
|
|
319
|
+
try {
|
|
320
|
+
return await prove(rpc, key.toLowerCase(), nonce, new Set(ours.map((h) => h.toLowerCase())), at, unserved);
|
|
321
|
+
}
|
|
322
|
+
catch (e) {
|
|
323
|
+
unserved.push(textOf(e));
|
|
324
|
+
return { answer: "no-answer", reason: "unreadable", detail: detailOf(unserved) };
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
async function prove(rpc, key, nonce, ours, at, unserved) {
|
|
328
|
+
const call = callOf(rpc, at.deadline);
|
|
329
|
+
const countByHash = async (block) => quantity(await call("eth_getTransactionCount", [key, { blockHash: block, requireCanonical: true }]));
|
|
330
|
+
const countAt = async (block) => quantity(await call("eth_getTransactionCount", [key, hex(block)]));
|
|
331
|
+
const anchors = ANCHORS.get(rpc);
|
|
332
|
+
const keep = async (s, dropping) => {
|
|
333
|
+
await anchors?.keepSearch(key, nonce, s, dropping).catch(() => { });
|
|
334
|
+
};
|
|
335
|
+
// A read the node does not serve (state it dropped, a method or a block it refuses) is no answer, and no evidence:
|
|
336
|
+
// it is noted for the reason given, and the proof goes on to what the node does serve. A deadline spent or a rate
|
|
337
|
+
// limit ends the proof (`Stopped`).
|
|
338
|
+
const served = async (what, f) => {
|
|
339
|
+
try {
|
|
340
|
+
return await f();
|
|
341
|
+
}
|
|
342
|
+
catch (e) {
|
|
343
|
+
if (e instanceof Stopped)
|
|
344
|
+
throw e;
|
|
345
|
+
unserved.push(`${what}: ${textOf(e)}`);
|
|
346
|
+
return undefined;
|
|
347
|
+
}
|
|
348
|
+
};
|
|
349
|
+
const taker = (b) => takerOf(b, key, nonce);
|
|
350
|
+
const none = (reason) => ({ answer: "no-answer", reason, ...(unserved.length === 0 ? {} : { detail: detailOf(unserved) }) });
|
|
351
|
+
const finalized = headerOf(await call("eth_getBlockByNumber", ["finalized", false]));
|
|
352
|
+
// Whether the key's count at the finalized block has passed the nonce; undefined where the node does not serve that
|
|
353
|
+
// block's state (a node that keeps less state than the finalized block lies below its head).
|
|
354
|
+
const final = await served("the key's count at the finalized block", () => countByHash(finalized.hash));
|
|
355
|
+
const passed = final === undefined ? undefined : final > nonce;
|
|
356
|
+
// What an earlier proof's search found. A kept block that is not the node's own at its number (a fork the node no
|
|
357
|
+
// longer follows) is searched for again; one the node does not answer by number stands, and the reads below decide.
|
|
358
|
+
let kept = anchors === undefined ? undefined : await anchors.search(key, nonce).catch(() => undefined);
|
|
359
|
+
if (kept?.passedIn !== undefined) {
|
|
360
|
+
const there = await call("eth_getBlockByNumber", [hex(kept.passedIn.number), false]);
|
|
361
|
+
if (there !== null && headerOf(there).hash !== kept.passedIn.hash) {
|
|
362
|
+
const refused = kept.passedIn;
|
|
363
|
+
kept = undefined;
|
|
364
|
+
await keep(undefined, refused);
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
let passedIn = kept?.passedIn;
|
|
368
|
+
let read;
|
|
369
|
+
/** Whether the counts by the block's hash and its parent's were served, and showed the count passing the nonce there. */
|
|
370
|
+
let counted = kept?.passedIn !== undefined && kept.counted === true;
|
|
371
|
+
/** The search as this proof leaves it kept, where it found no block. */
|
|
372
|
+
let narrowed = { ...kept };
|
|
373
|
+
// The block at which the search by counts ends: the finalized one where its count has passed the nonce; else the
|
|
374
|
+
// node's latest, whose state a node that keeps recent state only holds, so that the block is found, and kept, while
|
|
375
|
+
// its state is held, before it is final. A count at the latest block at or below the nonce shows the same of every
|
|
376
|
+
// ancestor of it: the finalized block's count has not passed the nonce where that block is shown one of them.
|
|
377
|
+
let top = passed === true ? finalized : undefined;
|
|
378
|
+
if (passedIn === undefined && top === undefined) {
|
|
379
|
+
const latest = await served("the latest block", async () => headerOf(await call("eth_getBlockByNumber", ["latest", false])));
|
|
380
|
+
if (latest !== undefined && latest.number > finalized.number) {
|
|
381
|
+
const now = await served("the key's count at the latest block", () => countByHash(latest.hash));
|
|
382
|
+
if (now !== undefined && now > nonce)
|
|
383
|
+
top = latest;
|
|
384
|
+
else if (now !== undefined && (passed === false || (await ancestor(call, latest, finalized))))
|
|
385
|
+
return none("not-finalized");
|
|
386
|
+
}
|
|
387
|
+
if (top === undefined && passed === false)
|
|
388
|
+
return none("not-finalized");
|
|
389
|
+
}
|
|
390
|
+
if (passedIn === undefined && top !== undefined) {
|
|
391
|
+
// The first block at or below `top` at which the count has passed the nonce: passed at `b`, not at `a`, or `a` lies
|
|
392
|
+
// below the network's first block. Going on from the range an earlier proof narrowed, and keeping the range this
|
|
393
|
+
// one narrows, whatever stops it. `floor` is the highest block of the range at which the node served no count (a
|
|
394
|
+
// node that dropped that state serves none below it either): the search goes on above it, and ends unfound where
|
|
395
|
+
// the count has passed the nonce at the lowest block whose count the node serves.
|
|
396
|
+
let a = passed === false ? finalized.number : -1n;
|
|
397
|
+
let b = top.number;
|
|
398
|
+
if (kept?.above !== undefined && kept.above < b && kept.above > a)
|
|
399
|
+
b = kept.above;
|
|
400
|
+
if (kept?.below !== undefined && kept.below < b && kept.below > a)
|
|
401
|
+
a = kept.below;
|
|
402
|
+
let floor = kept?.unserved !== undefined && kept.unserved > a && kept.unserved < b ? kept.unserved : undefined;
|
|
403
|
+
const countOr = (block) => served(`the key's count at block ${block.toString(10)}`, () => countAt(block));
|
|
404
|
+
try {
|
|
405
|
+
if (at.from !== undefined && at.from > a && at.from < b) {
|
|
406
|
+
const c = await countOr(at.from);
|
|
407
|
+
if (c === undefined)
|
|
408
|
+
floor = floor === undefined || at.from > floor ? at.from : floor;
|
|
409
|
+
else if (c > nonce)
|
|
410
|
+
b = at.from;
|
|
411
|
+
else
|
|
412
|
+
a = at.from;
|
|
413
|
+
if (floor !== undefined && (floor >= b || floor <= a))
|
|
414
|
+
floor = undefined;
|
|
415
|
+
}
|
|
416
|
+
// A block kept as unserved is asked once more where it is all that is left: a node that serves it now (a
|
|
417
|
+
// better node) is searched on. No block is asked twice by one proof.
|
|
418
|
+
const asked = new Set();
|
|
419
|
+
for (;;) {
|
|
420
|
+
const low = floor !== undefined && floor > a ? floor : a;
|
|
421
|
+
let m;
|
|
422
|
+
if (b - low > 1n)
|
|
423
|
+
m = low + (b - low) / 2n;
|
|
424
|
+
else if (low > a && !asked.has(low))
|
|
425
|
+
m = low;
|
|
426
|
+
else
|
|
427
|
+
break;
|
|
428
|
+
asked.add(m);
|
|
429
|
+
const c = await countOr(m);
|
|
430
|
+
if (c === undefined) {
|
|
431
|
+
floor = m;
|
|
432
|
+
}
|
|
433
|
+
else if (c > nonce) {
|
|
434
|
+
b = m;
|
|
435
|
+
if (floor !== undefined && floor >= b)
|
|
436
|
+
floor = undefined;
|
|
437
|
+
}
|
|
438
|
+
else {
|
|
439
|
+
a = m;
|
|
440
|
+
floor = undefined;
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
finally {
|
|
445
|
+
narrowed = { ...(a < 0n ? {} : { below: a }), above: b, ...(floor === undefined ? {} : { unserved: floor }), ...(kept?.scanned === undefined ? {} : { scanned: kept.scanned }) };
|
|
446
|
+
if (a !== (kept?.below ?? -1n) || b !== kept?.above || floor !== kept?.unserved)
|
|
447
|
+
await keep(narrowed);
|
|
448
|
+
}
|
|
449
|
+
if (b === 0n)
|
|
450
|
+
return none("unshown");
|
|
451
|
+
if (b - a === 1n) {
|
|
452
|
+
// The facts that decide, each read by the block's hash. Where they do not hold, the range read by number led
|
|
453
|
+
// nowhere (a node that answered it from another block), and the next proof searches from the start.
|
|
454
|
+
const named = headerOf(await call("eth_getBlockByNumber", [hex(b), false]));
|
|
455
|
+
read = headerOf(await call("eth_getBlockByHash", [named.hash, true]));
|
|
456
|
+
if (read.hash !== named.hash || read.number !== b)
|
|
457
|
+
return none("unreadable");
|
|
458
|
+
// The counts at the block and at its parent, by their hashes, where the node serves them: they never contradict
|
|
459
|
+
// the block. Where it serves one of them no more, the block's own transactions, read by its hash, show the key's
|
|
460
|
+
// transaction at the nonce, or the block is not kept.
|
|
461
|
+
const here = await served("the key's count at the block the count by number names", () => countByHash(read.hash));
|
|
462
|
+
const before = await served("the key's count at that block's parent", () => countByHash(read.parentHash));
|
|
463
|
+
if ((here !== undefined && here <= nonce) || (before !== undefined && before > nonce)) {
|
|
464
|
+
await keep(undefined);
|
|
465
|
+
return none("unreadable");
|
|
466
|
+
}
|
|
467
|
+
if ((here === undefined || before === undefined) && taker(read) === undefined) {
|
|
468
|
+
// Neither the counts by hash nor the block's transactions show the nonce taken there: the block is not kept.
|
|
469
|
+
read = undefined;
|
|
470
|
+
}
|
|
471
|
+
else {
|
|
472
|
+
// Kept at once, before the ancestry's walk, which may take several proofs: those need no count here again.
|
|
473
|
+
passedIn = { number: b, hash: read.hash };
|
|
474
|
+
counted = here !== undefined && before !== undefined;
|
|
475
|
+
await keep({ passedIn, ...(counted ? { counted } : {}) });
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
}
|
|
479
|
+
if (passedIn === undefined) {
|
|
480
|
+
// The node serves no count that finds the block. A transaction of ours that the node shows mined names its block
|
|
481
|
+
// by hash: that block, read by its hash, holds it at the key's nonce, or shows nothing.
|
|
482
|
+
for (const h of ours) {
|
|
483
|
+
const block = await served("a transaction signed at the nonce", async () => {
|
|
484
|
+
const t = await call("eth_getTransactionByHash", [h]);
|
|
485
|
+
if (!isObject(t) || typeof t["blockHash"] !== "string")
|
|
486
|
+
return undefined;
|
|
487
|
+
const b = headerOf(await call("eth_getBlockByHash", [hash(t["blockHash"]), true]));
|
|
488
|
+
return b.hash === hash(t["blockHash"]) && taker(b) === h ? b : undefined;
|
|
489
|
+
});
|
|
490
|
+
if (block === undefined)
|
|
491
|
+
continue;
|
|
492
|
+
read = block;
|
|
493
|
+
passedIn = { number: block.number, hash: block.hash };
|
|
494
|
+
await keep({ passedIn });
|
|
495
|
+
break;
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
if (passedIn === undefined && at.from !== undefined) {
|
|
499
|
+
// Nor is the key's own transaction shown. The blocks above the one given, in which the nonce was not yet taken,
|
|
500
|
+
// are read whole, a few on each proof, for the key's transaction at the nonce: the block that holds it is read
|
|
501
|
+
// again by its hash. Blocks read by number only choose which block that is, and none above the finalized block is
|
|
502
|
+
// read, so that a block noted as read is never replaced.
|
|
503
|
+
const done = kept?.scanned !== undefined && kept.scanned.from === at.from ? kept.scanned.to : at.from;
|
|
504
|
+
const end = [done + BigInt(SCAN_BLOCKS), at.from + BigInt(SCAN_MOST), finalized.number].reduce((x, y) => (x < y ? x : y));
|
|
505
|
+
let to = done;
|
|
506
|
+
try {
|
|
507
|
+
for (let n = done + 1n; n <= end; n++) {
|
|
508
|
+
const block = await served(`block ${n.toString(10)}`, async () => {
|
|
509
|
+
const got = await call("eth_getBlockByNumber", [hex(n), true]);
|
|
510
|
+
return got === null ? undefined : headerOf(got);
|
|
511
|
+
});
|
|
512
|
+
if (block === undefined || block.number !== n)
|
|
513
|
+
break;
|
|
514
|
+
if (taker(block) === undefined) {
|
|
515
|
+
to = n;
|
|
516
|
+
continue;
|
|
517
|
+
}
|
|
518
|
+
const again = headerOf(await call("eth_getBlockByHash", [block.hash, true]));
|
|
519
|
+
if (again.hash !== block.hash || again.number !== n || taker(again) === undefined)
|
|
520
|
+
break;
|
|
521
|
+
read = again;
|
|
522
|
+
passedIn = { number: n, hash: again.hash };
|
|
523
|
+
break;
|
|
524
|
+
}
|
|
525
|
+
}
|
|
526
|
+
finally {
|
|
527
|
+
if (passedIn !== undefined)
|
|
528
|
+
await keep({ passedIn });
|
|
529
|
+
else if (to !== done)
|
|
530
|
+
await keep({ ...narrowed, scanned: { from: at.from, to } });
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
if (passedIn === undefined)
|
|
534
|
+
return none(passed === false ? "not-finalized" : "unreadable");
|
|
535
|
+
if (passedIn.number > finalized.number) {
|
|
536
|
+
// The block is above the finalized one: nothing final is shown yet. Where the finalized block's count has passed
|
|
537
|
+
// the nonce, the block is of another chain than the finalized one, and is searched for again; where the node does
|
|
538
|
+
// not serve that count, the finalized block is shown an ancestor of the block, or the proof says nothing.
|
|
539
|
+
if (passed === true) {
|
|
540
|
+
await keep(undefined, passedIn);
|
|
541
|
+
return none("unreadable");
|
|
542
|
+
}
|
|
543
|
+
if (passed === false || (await ancestor(call, passedIn, finalized)))
|
|
544
|
+
return none("not-finalized");
|
|
545
|
+
return none("unreadable");
|
|
546
|
+
}
|
|
547
|
+
const block = read ?? headerOf(await call("eth_getBlockByHash", [passedIn.hash, true]));
|
|
548
|
+
if (block.hash !== passedIn.hash || block.number !== passedIn.number)
|
|
549
|
+
return none("unreadable");
|
|
550
|
+
// A count the node serves never contradicts the block, however it was found: the finalized block's count has passed
|
|
551
|
+
// the nonce, and the count passes it in the block. Where one does, the block is dropped and searched for again.
|
|
552
|
+
let against = passed === false;
|
|
553
|
+
if (!against && !counted) {
|
|
554
|
+
const here = await served("the key's count at the block that holds its transaction at the nonce", () => countByHash(block.hash));
|
|
555
|
+
const before = await served("the key's count at that block's parent", () => countByHash(block.parentHash));
|
|
556
|
+
against = (here !== undefined && here <= nonce) || (before !== undefined && before > nonce);
|
|
557
|
+
if (!against && here !== undefined && before !== undefined)
|
|
558
|
+
await keep({ passedIn, counted: true });
|
|
559
|
+
}
|
|
560
|
+
if (against) {
|
|
561
|
+
await keep(undefined, passedIn);
|
|
562
|
+
return none("unreadable");
|
|
563
|
+
}
|
|
564
|
+
if (!(await ancestor(call, finalized, block, anchors)))
|
|
565
|
+
return none("unreadable");
|
|
566
|
+
const transaction = taker(block);
|
|
567
|
+
if (transaction === undefined)
|
|
568
|
+
return none("unshown");
|
|
569
|
+
return { answer: ours.has(transaction) ? "ours" : "taken-by-another", block: block.hash, transaction };
|
|
570
|
+
}
|
|
571
|
+
/**
|
|
572
|
+
* Whether the key's count at the block the node names `finalized`, read by that block's hash, has passed `nonce`: then
|
|
573
|
+
* 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
|
|
574
|
+
* not serve that block's state, the same is shown by other facts or not at all: passed, where the block a proof kept
|
|
575
|
+
* for that nonce (`Search`), read by its hash, holds the key's transaction at it and is an ancestor of the finalized
|
|
576
|
+
* block; not passed, where the count at the node's latest block, by its hash, is at or below the nonce and the
|
|
577
|
+
* finalized block is an ancestor of that block. A read that fails, and a count neither shows, rejects. `at`, where
|
|
578
|
+
* given, is the finalized block the caller read, by its number and hash, which the answer is then about.
|
|
579
|
+
*/
|
|
580
|
+
export async function passedAtFinalized(rpc, key, nonce, deadline, at) {
|
|
581
|
+
const call = callOf(rpc, deadline);
|
|
582
|
+
const count = async (block) => quantity(await call("eth_getTransactionCount", [key, { blockHash: block, requireCanonical: true }]));
|
|
583
|
+
try {
|
|
584
|
+
const finalized = at === undefined ? headerOf(await call("eth_getBlockByNumber", ["finalized", false])) : { number: at.number, hash: at.hash.toLowerCase() };
|
|
585
|
+
let unserved;
|
|
586
|
+
try {
|
|
587
|
+
return (await count(finalized.hash)) > nonce;
|
|
588
|
+
}
|
|
589
|
+
catch (e) {
|
|
590
|
+
if (e instanceof Stopped)
|
|
591
|
+
throw e;
|
|
592
|
+
unserved = e;
|
|
593
|
+
}
|
|
594
|
+
try {
|
|
595
|
+
const anchors = ANCHORS.get(rpc);
|
|
596
|
+
const kept = (await anchors?.search(key.toLowerCase(), nonce).catch(() => undefined))?.passedIn;
|
|
597
|
+
if (kept !== undefined && kept.number <= finalized.number) {
|
|
598
|
+
const block = headerOf(await call("eth_getBlockByHash", [kept.hash, true]));
|
|
599
|
+
const holds = block.hash === kept.hash && block.number === kept.number && takerOf(block, key.toLowerCase(), nonce) !== undefined;
|
|
600
|
+
const before = holds ? await count(block.parentHash).catch((e) => (e instanceof Stopped ? Promise.reject(e) : undefined)) : undefined;
|
|
601
|
+
if (holds && (before === undefined || before <= nonce) && (await ancestor(call, finalized, block, anchors)))
|
|
602
|
+
return true;
|
|
603
|
+
}
|
|
604
|
+
const latest = headerOf(await call("eth_getBlockByNumber", ["latest", false]));
|
|
605
|
+
if (latest.number > finalized.number && (await count(latest.hash)) <= nonce && (await ancestor(call, latest, finalized)))
|
|
606
|
+
return false;
|
|
607
|
+
}
|
|
608
|
+
catch (e) {
|
|
609
|
+
if (e instanceof Stopped)
|
|
610
|
+
throw e;
|
|
611
|
+
}
|
|
612
|
+
throw unserved;
|
|
613
|
+
}
|
|
614
|
+
catch (e) {
|
|
615
|
+
// The node call's own failure, as before this module paced its calls.
|
|
616
|
+
throw e instanceof Stopped && e.cause !== undefined ? e.cause : e;
|
|
617
|
+
}
|
|
618
|
+
}
|
|
619
|
+
/**
|
|
620
|
+
* The receipt of `transaction` where the finalized chain shows it: its block, named by the receipt's `blockHash`, at or
|
|
621
|
+
* below the block the node names `finalized` and shown an ancestor of it from the finalized block's own hash
|
|
622
|
+
* (`ancestor`), or that block itself. Undefined where the node shows no receipt, its block is above the finalized one,
|
|
623
|
+
* or is not shown on the finalized chain (a node behind, or on another fork, answers so), or a read fails: nothing
|
|
624
|
+
* 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
|
|
625
|
+
* is its block there (see the module's note on one view of the chain).
|
|
626
|
+
*/
|
|
627
|
+
export async function finalizedReceipt(rpc, transaction, deadline) {
|
|
628
|
+
const call = callOf(rpc, deadline);
|
|
629
|
+
try {
|
|
630
|
+
const r = await call("eth_getTransactionReceipt", [transaction]);
|
|
631
|
+
if (r === null || !isObject(r))
|
|
632
|
+
return undefined;
|
|
633
|
+
const status = quantity(r["status"]);
|
|
634
|
+
if (status !== 0n && status !== 1n)
|
|
635
|
+
return undefined;
|
|
636
|
+
const at = { hash: hash(r["blockHash"]), number: quantity(r["blockNumber"]) };
|
|
637
|
+
const finalized = headerOf(await call("eth_getBlockByNumber", ["finalized", false]));
|
|
638
|
+
if (at.number > finalized.number)
|
|
639
|
+
return undefined;
|
|
640
|
+
if (!(await ancestor(call, finalized, at, ANCHORS.get(rpc))))
|
|
641
|
+
return undefined;
|
|
642
|
+
return { status: status === 1n ? 1 : 0, blockNumber: at.number, blockHash: at.hash };
|
|
643
|
+
}
|
|
644
|
+
catch {
|
|
645
|
+
return undefined;
|
|
646
|
+
}
|
|
647
|
+
}
|
|
648
|
+
/**
|
|
649
|
+
* Whether `b` is an ancestor of the finalized block `f`, each named by its hash, shown from `f`'s hash down: through
|
|
650
|
+
* EIP-2935's history contract read at a block by its hash, which answers the hash of a block up to 8191 below it, in
|
|
651
|
+
* hops of 8190 blocks (`HISTORY_REACH`), at most `HISTORY_HOPS` of them, from the lowest kept anchor at or above `b` where it lies at
|
|
652
|
+
* least 8190 below `f`, else from `f`; then, from the lowest block the contract showed or the lowest kept anchor at or
|
|
653
|
+
* above `b`, by reading each header by its parent's hash, at most `ANCESTRY_WALK` of them. A hop the contract does not
|
|
654
|
+
* answer with a hash (a read that fails, a revert, an empty or all-zero answer) shows nothing and ends the hops. False
|
|
655
|
+
* where neither reaches `b` and finds its hash; a header read that fails rejects. A kept anchor is used only once the
|
|
656
|
+
* node's own block at its number, read by number, is shown to be it; else it is dropped, and every kept anchor with it
|
|
657
|
+
* where the node holds another block there.
|
|
658
|
+
*/
|
|
659
|
+
async function ancestor(call, f, b, anchors) {
|
|
660
|
+
if (b.number === f.number)
|
|
661
|
+
return b.hash === f.hash;
|
|
662
|
+
// The lowest kept anchor at or above `b`: a block once shown on the finalized chain stays on it.
|
|
663
|
+
let low;
|
|
664
|
+
for (const a of anchors === undefined ? [] : await anchors.get()) {
|
|
665
|
+
if (a.number >= b.number && a.number < f.number && (low === undefined || a.number < low.number))
|
|
666
|
+
low = a;
|
|
667
|
+
}
|
|
668
|
+
// A kept anchor is a memory of a proof, not a proof: it is used only where the node's own block at its number, which
|
|
669
|
+
// lies below the finalized block, is that block. Where the node holds none there, that anchor goes; where it holds
|
|
670
|
+
// another, the anchors were shown on a chain that is not the node's (a network whose history was replaced), and every
|
|
671
|
+
// one goes. Either way this proof goes on from `f`. A read that fails rejects and drops nothing.
|
|
672
|
+
if (low !== undefined) {
|
|
673
|
+
const there = await call("eth_getBlockByNumber", [hex(low.number), false]);
|
|
674
|
+
const own = there === null ? undefined : headerOf(there);
|
|
675
|
+
if (own === undefined || own.number !== low.number) {
|
|
676
|
+
await anchors?.remove(low).catch(() => { });
|
|
677
|
+
low = undefined;
|
|
678
|
+
}
|
|
679
|
+
else if (own.hash !== low.hash) {
|
|
680
|
+
await anchors?.clear().catch(() => { });
|
|
681
|
+
low = undefined;
|
|
682
|
+
}
|
|
683
|
+
}
|
|
684
|
+
// The hops go on from where earlier proofs' hops or walks reached, where that is below where `f`'s first hop reaches.
|
|
685
|
+
const from = low !== undefined && low.number > b.number && low.number <= f.number - HISTORY_REACH ? low : { hash: f.hash, number: f.number };
|
|
686
|
+
let at = from;
|
|
687
|
+
for (let hop = 0; hop < HISTORY_HOPS; hop++) {
|
|
688
|
+
const target = at.number - b.number <= HISTORY_REACH ? b.number : at.number - HISTORY_REACH;
|
|
689
|
+
const word = await history(call, at.hash, target);
|
|
690
|
+
if (word === undefined)
|
|
691
|
+
break;
|
|
692
|
+
if (target === b.number)
|
|
693
|
+
return word === b.hash;
|
|
694
|
+
at = { hash: word, number: target };
|
|
695
|
+
}
|
|
696
|
+
// The lowest block the contract showed is on the finalized chain for ever: it is kept as an anchor, and the walk
|
|
697
|
+
// starts from it, or from a lower kept anchor at or above `b`.
|
|
698
|
+
const shown = at.number < from.number;
|
|
699
|
+
if (shown)
|
|
700
|
+
await anchors?.add(at).catch(() => { });
|
|
701
|
+
let start = at;
|
|
702
|
+
if (low !== undefined && low.number < start.number)
|
|
703
|
+
start = low;
|
|
704
|
+
const anchored = start.number < f.number;
|
|
705
|
+
if (start.number === b.number) {
|
|
706
|
+
// The anchor is the node's block at that number, read just now: `b` is on the finalized chain where it is that block,
|
|
707
|
+
// and is not (a fork block) where it is another. The anchor stays.
|
|
708
|
+
return start.hash === b.hash;
|
|
709
|
+
}
|
|
710
|
+
if (start.number - b.number > WALK_SHORT) {
|
|
711
|
+
const through = await throughLatest(call, start, b);
|
|
712
|
+
if (through !== undefined)
|
|
713
|
+
return through;
|
|
714
|
+
}
|
|
715
|
+
let hash = start.hash;
|
|
716
|
+
for (let n = start.number; n > b.number; n--) {
|
|
717
|
+
// `hash` is block n's, committed to by the parent hash of the header above it, read by its own hash.
|
|
718
|
+
if (start.number - n >= BigInt(ANCESTRY_WALK)) {
|
|
719
|
+
await anchors?.add({ number: n, hash });
|
|
720
|
+
return false;
|
|
721
|
+
}
|
|
722
|
+
let header;
|
|
723
|
+
try {
|
|
724
|
+
const got = await call("eth_getBlockByHash", [hash, false]);
|
|
725
|
+
header = got === null ? undefined : headerOf(got);
|
|
726
|
+
}
|
|
727
|
+
catch (e) {
|
|
728
|
+
// The walk so far stands: where it stopped is kept, so the next proof walks on from it. A read that fails (a
|
|
729
|
+
// deadline spent, a node that errs) is no answer that the node does not hold the block: no anchor goes.
|
|
730
|
+
if (n < start.number)
|
|
731
|
+
await anchors?.add({ number: n, hash }).catch(() => { });
|
|
732
|
+
throw e;
|
|
733
|
+
}
|
|
734
|
+
if (header === undefined || header.hash !== hash || header.number !== n) {
|
|
735
|
+
// An anchor the node does not hold as such starts no walk again.
|
|
736
|
+
if (n === start.number && anchored)
|
|
737
|
+
await anchors?.remove(start).catch(() => { });
|
|
738
|
+
return false;
|
|
739
|
+
}
|
|
740
|
+
hash = header.parentHash;
|
|
741
|
+
}
|
|
742
|
+
return hash === b.hash;
|
|
743
|
+
}
|
|
744
|
+
/** The hash of block `n` as EIP-2935's history contract answers it at the block `at`; undefined where it answers none. */
|
|
745
|
+
async function history(call, at, n) {
|
|
746
|
+
let word;
|
|
747
|
+
try {
|
|
748
|
+
word = await call("eth_call", [{ to: HISTORY_CONTRACT, data: `0x${n.toString(16).padStart(64, "0")}` }, { blockHash: at, requireCanonical: true }]);
|
|
749
|
+
}
|
|
750
|
+
catch {
|
|
751
|
+
return undefined;
|
|
752
|
+
}
|
|
753
|
+
return typeof word === "string" && HASH.test(word) && !/^0x0{64}$/.test(word) ? word.toLowerCase() : undefined;
|
|
754
|
+
}
|
|
755
|
+
/**
|
|
756
|
+
* Whether `b` is an ancestor of `s`, a block above it, from what the history contract states at the node's latest
|
|
757
|
+
* block, read by that block's hash: a block that states `s`'s hash at `s`'s number descends from `s`, and the hash it
|
|
758
|
+
* states at `b`'s number is then the hash of `s`'s ancestor there, which is `b`'s or another block's. A node that
|
|
759
|
+
* keeps recent state only holds the latest block's state where it holds neither `s`'s nor any below it. Undefined
|
|
760
|
+
* where the latest block is not above `s`, lies more than the contract's reach above `b`, or the contract answers
|
|
761
|
+
* neither: nothing is shown, and the walk by headers goes on.
|
|
762
|
+
*/
|
|
763
|
+
async function throughLatest(call, s, b) {
|
|
764
|
+
let latest;
|
|
765
|
+
try {
|
|
766
|
+
latest = headerOf(await call("eth_getBlockByNumber", ["latest", false]));
|
|
767
|
+
}
|
|
768
|
+
catch (e) {
|
|
769
|
+
if (e instanceof Stopped)
|
|
770
|
+
throw e;
|
|
771
|
+
return undefined;
|
|
772
|
+
}
|
|
773
|
+
if (latest.number <= s.number || latest.number - b.number > HISTORY_REACH)
|
|
774
|
+
return undefined;
|
|
775
|
+
if ((await history(call, latest.hash, s.number)) !== s.hash)
|
|
776
|
+
return undefined;
|
|
777
|
+
const word = await history(call, latest.hash, b.number);
|
|
778
|
+
return word === undefined ? undefined : word === b.hash;
|
|
779
|
+
}
|