webevm 0.6.0 → 0.8.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/README.md +148 -63
- package/dist/derived-trie.d.ts +38 -0
- package/dist/derived-trie.d.ts.map +1 -0
- package/dist/derived-trie.js +187 -0
- package/dist/derived-trie.js.map +1 -0
- package/dist/engine.d.ts +1 -2
- package/dist/engine.d.ts.map +1 -1
- package/dist/engine.js +2 -3
- package/dist/engine.js.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js.map +1 -1
- package/dist/node.d.ts +45 -0
- package/dist/node.d.ts.map +1 -1
- package/dist/node.js +1125 -141
- package/dist/node.js.map +1 -1
- package/dist/revm-state-store.d.ts +5 -0
- package/dist/revm-state-store.d.ts.map +1 -1
- package/dist/revm-state-store.js +42 -13
- package/dist/revm-state-store.js.map +1 -1
- package/dist/revm.d.ts.map +1 -1
- package/dist/revm.js +5 -19
- package/dist/revm.js.map +1 -1
- package/dist/state-manager.d.ts +256 -18
- package/dist/state-manager.d.ts.map +1 -1
- package/dist/state-manager.js +429 -20
- package/dist/state-manager.js.map +1 -1
- package/dist/storage-keys.d.ts +8 -0
- package/dist/storage-keys.d.ts.map +1 -1
- package/dist/storage-keys.js +18 -2
- package/dist/storage-keys.js.map +1 -1
- package/dist/types.d.ts +180 -34
- package/dist/types.d.ts.map +1 -1
- package/dist/worker-client.d.ts.map +1 -1
- package/dist/worker-client.js +37 -11
- package/dist/worker-client.js.map +1 -1
- package/dist/worker-host.d.ts.map +1 -1
- package/dist/worker-host.js +21 -8
- package/dist/worker-host.js.map +1 -1
- package/dist/worker-rpc-error.d.ts +21 -0
- package/dist/worker-rpc-error.d.ts.map +1 -0
- package/dist/worker-rpc-error.js +121 -0
- package/dist/worker-rpc-error.js.map +1 -0
- package/package.json +4 -1
- package/src/derived-trie.ts +214 -0
- package/src/engine.ts +2 -3
- package/src/index.ts +3 -1
- package/src/node.ts +1335 -138
- package/src/revm-state-store.ts +47 -13
- package/src/revm.ts +11 -25
- package/src/state-manager.ts +515 -21
- package/src/storage-keys.ts +21 -2
- package/src/types.ts +183 -35
- package/src/worker-client.ts +42 -11
- package/src/worker-host.ts +21 -8
- package/src/worker-rpc-error.ts +139 -0
package/src/node.ts
CHANGED
|
@@ -4,7 +4,10 @@
|
|
|
4
4
|
* Design: sits BETWEEN bare `EVM.runCall` (no blocks/receipts/logs) and a full
|
|
5
5
|
* node (heavy). Runs on `@ethereumjs/vm` through the engine seam (see ./engine.ts)
|
|
6
6
|
* + a minimal mock blockchain with `SimpleStateManager` (plain Maps, NO trie, NO
|
|
7
|
-
* state-root) and NONE of the node / RPC / mempool / signing bloat.
|
|
7
|
+
* state-root) and NONE of the node / RPC / mempool / signing bloat. Every node
|
|
8
|
+
* runs on that one flat state; a node with `computeStateRoot: true` additionally DERIVES
|
|
9
|
+
* a Merkle-Patricia trie from it at the end of each block (./derived-trie.ts,
|
|
10
|
+
* `docs/adr/0014-the-trie-is-derived-from-the-flat-state-not-a-state-manager.md`).
|
|
8
11
|
*
|
|
9
12
|
* It is EXECUTION-ONLY:
|
|
10
13
|
* - NO account methods (eth_sendTransaction, eth_accounts, eth_sign,
|
|
@@ -34,8 +37,19 @@
|
|
|
34
37
|
* `docs/adr/0012-one-request-at-a-time-the-node-serialises-its-whole-public-surface.md`.
|
|
35
38
|
*/
|
|
36
39
|
import {createVM, type VM} from '@ethereumjs/vm';
|
|
37
|
-
import {
|
|
38
|
-
import {
|
|
40
|
+
import {DerivedStateTrie} from './derived-trie.js';
|
|
41
|
+
import {
|
|
42
|
+
OverlayStorageStateManager,
|
|
43
|
+
copyAccount,
|
|
44
|
+
mergeChangeSetOlderWins,
|
|
45
|
+
type ChangeSet,
|
|
46
|
+
} from './state-manager.js';
|
|
47
|
+
import {
|
|
48
|
+
packAddressKey,
|
|
49
|
+
packSlotKey,
|
|
50
|
+
unpackAddressKey,
|
|
51
|
+
unpackSlotKey,
|
|
52
|
+
} from './storage-keys.js';
|
|
39
53
|
import {Common, Mainnet, Hardfork} from '@ethereumjs/common';
|
|
40
54
|
import {createBlock, type Block} from '@ethereumjs/block';
|
|
41
55
|
import {createTxFromRLP, createTx, type TypedTransaction} from '@ethereumjs/tx';
|
|
@@ -50,6 +64,7 @@ import {
|
|
|
50
64
|
bigIntToHex,
|
|
51
65
|
setLengthLeft,
|
|
52
66
|
bigIntToBytes,
|
|
67
|
+
unpadBytes,
|
|
53
68
|
} from '@ethereumjs/util';
|
|
54
69
|
|
|
55
70
|
// `@ethereumjs/util`'s hexToBytes is typed to require a `0x${string}` literal.
|
|
@@ -64,6 +79,12 @@ import {connectEngine, createEthereumjsEngine} from './engine.js';
|
|
|
64
79
|
// Sender derivation stays the node's on every engine; what an engine may lend it
|
|
65
80
|
// is the CURVE step. See ./sender-recovery.ts.
|
|
66
81
|
import {recoverSender} from './sender-recovery.js';
|
|
82
|
+
// `serveOn(port)`: EIP-1193 `request` over a MessagePort, in the wire format the
|
|
83
|
+
// consumer's `providerOverPort` speaks. Imported HERE, the one place the node's
|
|
84
|
+
// public surface is built, so every node (main-thread or worker-hosted) serves a
|
|
85
|
+
// port the same way; see `serveOn` at the bottom of this file for why this is a
|
|
86
|
+
// dependency of the core rather than of `webevm/worker-host` alone.
|
|
87
|
+
import {serveProvider, type ServedProvider} from '@eip-1193/over-port';
|
|
67
88
|
// The engine reports EXECUTION gas for a read and the node adds intrinsic gas on top;
|
|
68
89
|
// an engine that charges intrinsic gas itself (revm) subtracts the SAME formula,
|
|
69
90
|
// so it has exactly one home. See ./intrinsic-gas.ts.
|
|
@@ -84,6 +105,7 @@ import {
|
|
|
84
105
|
type SerializedReceipt,
|
|
85
106
|
type SerializedLog,
|
|
86
107
|
type SerializedTx,
|
|
108
|
+
type SerializedHistoryBlock,
|
|
87
109
|
} from './types.js';
|
|
88
110
|
|
|
89
111
|
const ZERO_HASH = '0x' + '00'.repeat(32);
|
|
@@ -176,6 +198,96 @@ function hex(b: Uint8Array): string {
|
|
|
176
198
|
function numHex(n: number | bigint): string {
|
|
177
199
|
return bigIntToHex(BigInt(n));
|
|
178
200
|
}
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* A change set as the dump carries it ({@link SerializedHistoryBlock}): keys in
|
|
204
|
+
* `0x` hex, values hex, `null` for absent. Reads `changes` and does not keep a
|
|
205
|
+
* reference to anything in it.
|
|
206
|
+
*/
|
|
207
|
+
function serializeChangeSet(
|
|
208
|
+
number: number,
|
|
209
|
+
changes: ChangeSet,
|
|
210
|
+
): SerializedHistoryBlock {
|
|
211
|
+
const accounts: Record<string, string | null> = {};
|
|
212
|
+
for (const [key, account] of changes.accounts)
|
|
213
|
+
accounts[key] = account === undefined ? null : hex(account.serialize());
|
|
214
|
+
const code: Record<string, string | null> = {};
|
|
215
|
+
for (const [key, value] of changes.code)
|
|
216
|
+
code[key] = value === undefined ? null : hex(value);
|
|
217
|
+
const storage: Record<string, Record<string, string | null>> = {};
|
|
218
|
+
for (const [addressKey, slots] of changes.storage) {
|
|
219
|
+
const out: Record<string, string | null> = {};
|
|
220
|
+
for (const [slotKey, value] of slots)
|
|
221
|
+
out[unpackSlotKey(slotKey)] = value === undefined ? null : hex(value);
|
|
222
|
+
storage[unpackAddressKey(addressKey)] = out;
|
|
223
|
+
}
|
|
224
|
+
return {
|
|
225
|
+
number,
|
|
226
|
+
accounts,
|
|
227
|
+
code,
|
|
228
|
+
storage,
|
|
229
|
+
storageCleared: [...changes.storageCleared].map(unpackAddressKey),
|
|
230
|
+
};
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/**
|
|
234
|
+
* Merge serialized record `newer` INTO `older`, the OLDER entry winning for a
|
|
235
|
+
* key both name: the serialized twin of `mergeChangeSetOlderWins`, used by
|
|
236
|
+
* `dumpState` to fold the open record into the head's record (see there).
|
|
237
|
+
*/
|
|
238
|
+
function mergeSerializedOlderWins(
|
|
239
|
+
older: SerializedHistoryBlock,
|
|
240
|
+
newer: SerializedHistoryBlock,
|
|
241
|
+
): void {
|
|
242
|
+
for (const [key, value] of Object.entries(newer.accounts))
|
|
243
|
+
if (!(key in older.accounts)) older.accounts[key] = value;
|
|
244
|
+
for (const [key, value] of Object.entries(newer.code))
|
|
245
|
+
if (!(key in older.code)) older.code[key] = value;
|
|
246
|
+
for (const [address, slots] of Object.entries(newer.storage)) {
|
|
247
|
+
const target = (older.storage[address] ??= {});
|
|
248
|
+
for (const [slot, value] of Object.entries(slots))
|
|
249
|
+
if (!(slot in target)) target[slot] = value;
|
|
250
|
+
}
|
|
251
|
+
for (const address of newer.storageCleared)
|
|
252
|
+
if (!older.storageCleared.includes(address))
|
|
253
|
+
older.storageCleared.push(address);
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
/**
|
|
257
|
+
* The inverse of {@link serializeChangeSet}: keys back to the representation's
|
|
258
|
+
* own (`address.toString()` for accounts and code, PACKED for storage), `null`
|
|
259
|
+
* back to `undefined`. Addresses are lowercased, so a hand-edited checksummed
|
|
260
|
+
* key still names the account the maps hold.
|
|
261
|
+
*/
|
|
262
|
+
function deserializeChangeSet(record: SerializedHistoryBlock): ChangeSet {
|
|
263
|
+
const accounts = new Map<string, Account | undefined>();
|
|
264
|
+
for (const [key, value] of Object.entries(record.accounts))
|
|
265
|
+
accounts.set(
|
|
266
|
+
key.toLowerCase(),
|
|
267
|
+
value === null ? undefined : createAccountFromRLP(hexToBytes(value)),
|
|
268
|
+
);
|
|
269
|
+
const code = new Map<string, Uint8Array | undefined>();
|
|
270
|
+
for (const [key, value] of Object.entries(record.code))
|
|
271
|
+
code.set(key.toLowerCase(), value === null ? undefined : hexToBytes(value));
|
|
272
|
+
const storage: ChangeSet['storage'] = new Map();
|
|
273
|
+
for (const [address, slots] of Object.entries(record.storage)) {
|
|
274
|
+
const out = new Map<string, Uint8Array | undefined>();
|
|
275
|
+
for (const [slot, value] of Object.entries(slots))
|
|
276
|
+
out.set(
|
|
277
|
+
packSlotKey(hexToBytes(slot)),
|
|
278
|
+
value === null ? undefined : hexToBytes(value),
|
|
279
|
+
);
|
|
280
|
+
storage.set(packAddressKey(hexToBytes(address)), out);
|
|
281
|
+
}
|
|
282
|
+
return {
|
|
283
|
+
accounts,
|
|
284
|
+
code,
|
|
285
|
+
storage,
|
|
286
|
+
storageCleared: new Set(
|
|
287
|
+
record.storageCleared.map((a) => packAddressKey(hexToBytes(a))),
|
|
288
|
+
),
|
|
289
|
+
};
|
|
290
|
+
}
|
|
179
291
|
function txHashOf(tx: TypedTransaction): string {
|
|
180
292
|
return hex(tx.hash());
|
|
181
293
|
}
|
|
@@ -258,6 +370,32 @@ interface StoredBlock {
|
|
|
258
370
|
*/
|
|
259
371
|
function swallow(): void {}
|
|
260
372
|
|
|
373
|
+
/**
|
|
374
|
+
* ONE ACCOUNT'S WORTH OF STATE TO WRITE into the checkpoint a pure read opens
|
|
375
|
+
* (`withStateEntries` in {@link createNodeWithInternals}), already parsed so it
|
|
376
|
+
* cannot fail to apply. Two producers, one apply step (`applyStateEntry`):
|
|
377
|
+
* the geth-style state overrides of `eth_call` / `eth_estimateGas`
|
|
378
|
+
* (`parseStateOverrides`), and block K's state for a call pinned to K
|
|
379
|
+
* (`historicalEntries`, from the `stateHistory` undo log). INTERNAL: not RPC
|
|
380
|
+
* JSON, and not exported.
|
|
381
|
+
*/
|
|
382
|
+
interface StateEntry {
|
|
383
|
+
readonly address: Address;
|
|
384
|
+
/** The address's code; written FIRST (it rewrites, or creates, the account). */
|
|
385
|
+
code?: Uint8Array;
|
|
386
|
+
/** A WHOLE account (history), replacing whatever is there. */
|
|
387
|
+
account?: Account;
|
|
388
|
+
/** Override fields, a read-modify-write on the account. */
|
|
389
|
+
balance?: bigint;
|
|
390
|
+
nonce?: bigint;
|
|
391
|
+
/** Clear the account's storage before `slots` are written. */
|
|
392
|
+
clearStorage: boolean;
|
|
393
|
+
/** Slot writes, keys 32 bytes, values in shortest form. */
|
|
394
|
+
slots: {key: Uint8Array; value: Uint8Array}[];
|
|
395
|
+
/** The account is ABSENT: emptied code, then deleted, as the LAST step. */
|
|
396
|
+
absent: boolean;
|
|
397
|
+
}
|
|
398
|
+
|
|
261
399
|
/**
|
|
262
400
|
* ONE SUBMITTED TRANSACTION, as the node carries it from `eth_sendRawTransaction*`
|
|
263
401
|
* (or the `evm_*As` cheats) to the block it is mined in: the parsed transaction,
|
|
@@ -277,15 +415,143 @@ interface SubmittedTx {
|
|
|
277
415
|
readonly sender: Address;
|
|
278
416
|
}
|
|
279
417
|
|
|
418
|
+
/**
|
|
419
|
+
* Switches the node's INTERNAL machinery on that no {@link NodeOptions} field
|
|
420
|
+
* exposes yet. NOT exported from `src/index.ts`: only this package's own tests
|
|
421
|
+
* and the next tasks reach it.
|
|
422
|
+
*/
|
|
423
|
+
export interface NodeInternals {
|
|
424
|
+
/**
|
|
425
|
+
* Record the per-block CHANGE SET (the open record of prior values; see the
|
|
426
|
+
* header of ./state-manager.ts). Off by default, and a node with it off
|
|
427
|
+
* records nothing and pays nothing. `stateHistory` turns it on (and RETAINS
|
|
428
|
+
* the records); `computeStateRoot: true` does too (the change set is the list of
|
|
429
|
+
* keys the derived trie rehashes, ./derived-trie.ts). This flag turns on
|
|
430
|
+
* recording alone, for the tests, through {@link createNodeWithInternals}.
|
|
431
|
+
*/
|
|
432
|
+
recordChangeSets?: boolean;
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
/**
|
|
436
|
+
* What {@link changeSetsForTests} returns: the change set of the HEAD block
|
|
437
|
+
* (taken when it was mined) and the OPEN record (every write since), as live
|
|
438
|
+
* read-only references. `recording: false` and both `undefined` on a node that
|
|
439
|
+
* does not record.
|
|
440
|
+
*/
|
|
441
|
+
export interface ChangeSetProbe {
|
|
442
|
+
recording: boolean;
|
|
443
|
+
/**
|
|
444
|
+
* The block numbers whose change set the node RETAINS as a sealed undo record
|
|
445
|
+
* (`stateHistory`), oldest first. Empty on a node without history.
|
|
446
|
+
*/
|
|
447
|
+
sealedBlocks: readonly number[];
|
|
448
|
+
/** What the last MINED block changed, with each key's value at the end of the block before it. */
|
|
449
|
+
headBlock: ChangeSet | undefined;
|
|
450
|
+
/** Every write since the head was mined (cheats, a loaded baseline is cleared). */
|
|
451
|
+
open: ChangeSet | undefined;
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
const changeSetProbes = new WeakMap<SlimNode, () => ChangeSetProbe>();
|
|
455
|
+
|
|
456
|
+
/**
|
|
457
|
+
* TEST-ONLY ACCESSOR for a node's change sets, named as such: it hands out the
|
|
458
|
+
* node's own record objects (read-only by contract) so a test can hold them
|
|
459
|
+
* against the state it reads through the public surface. Not part of
|
|
460
|
+
* `SlimNode` (the worker proxy's shape test would otherwise demand it cross a
|
|
461
|
+
* comlink boundary) and not exported from `src/index.ts`.
|
|
462
|
+
*/
|
|
463
|
+
export function changeSetsForTests(node: SlimNode): ChangeSetProbe {
|
|
464
|
+
const probe = changeSetProbes.get(node);
|
|
465
|
+
if (probe === undefined)
|
|
466
|
+
throw new Error(
|
|
467
|
+
'webevm: changeSetsForTests() was handed an object createNode() did not return.',
|
|
468
|
+
);
|
|
469
|
+
return probe();
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
/**
|
|
473
|
+
* Validate {@link NodeOptions.stateHistory}: ABSENT (`undefined`) is off and
|
|
474
|
+
* returns `undefined`; `{blocks: N}` with N a positive safe integer returns N;
|
|
475
|
+
* anything else throws.
|
|
476
|
+
*
|
|
477
|
+
* It composes with `computeStateRoot: true`: every node runs on the same flat state,
|
|
478
|
+
* which is what the undo log records, and the derived trie only reads it. (It
|
|
479
|
+
* was refused there while trie mode, as a root-computing node was then
|
|
480
|
+
* called, ran on `MerkleStateManager`, which recorded
|
|
481
|
+
* nothing; `trie-derived-from-the-flat-state` lifted that.)
|
|
482
|
+
*/
|
|
483
|
+
function validateStateHistory(options: NodeOptions): number | undefined {
|
|
484
|
+
if (options.stateHistory === undefined) return undefined;
|
|
485
|
+
const value = options.stateHistory as unknown;
|
|
486
|
+
const blocks =
|
|
487
|
+
typeof value === 'object' && value !== null
|
|
488
|
+
? (value as {blocks?: unknown}).blocks
|
|
489
|
+
: undefined;
|
|
490
|
+
if (
|
|
491
|
+
typeof blocks !== 'number' ||
|
|
492
|
+
!Number.isSafeInteger(blocks) ||
|
|
493
|
+
blocks <= 0
|
|
494
|
+
)
|
|
495
|
+
throw new Error(
|
|
496
|
+
`webevm: stateHistory must be {blocks: N} with N a positive safe integer ` +
|
|
497
|
+
`(how many blocks below the head stay readable), or absent for no history; ` +
|
|
498
|
+
`got ${describeOption(value)}.`,
|
|
499
|
+
);
|
|
500
|
+
return blocks;
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
/**
|
|
504
|
+
* Validate {@link NodeOptions.genesisTimestamp}: ABSENT (`undefined`) returns
|
|
505
|
+
* `undefined` (block 0 takes the wall clock); a non-negative bigint returns
|
|
506
|
+
* itself; anything else throws. A NUMBER is refused rather than converted, even
|
|
507
|
+
* a whole one: every other timestamp the node takes (`blockEnv.timestamp`) is a
|
|
508
|
+
* bigint, and accepting both would make the option mean two types.
|
|
509
|
+
*/
|
|
510
|
+
function validateGenesisTimestamp(options: NodeOptions): bigint | undefined {
|
|
511
|
+
if (options.genesisTimestamp === undefined) return undefined;
|
|
512
|
+
const value = options.genesisTimestamp as unknown;
|
|
513
|
+
if (typeof value !== 'bigint' || value < 0n)
|
|
514
|
+
throw new Error(
|
|
515
|
+
`webevm: genesisTimestamp must be a non-negative bigint (seconds since the ` +
|
|
516
|
+
`epoch, e.g. 1700000000n), or absent for the wall clock; got ` +
|
|
517
|
+
`${typeof value === 'bigint' ? `${value}n` : `${describeOption(value)} (${typeof value})`}.`,
|
|
518
|
+
);
|
|
519
|
+
return value;
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
function describeOption(value: unknown): string {
|
|
523
|
+
try {
|
|
524
|
+
const s = JSON.stringify(value);
|
|
525
|
+
return s === undefined ? String(value) : s;
|
|
526
|
+
} catch {
|
|
527
|
+
return String(value);
|
|
528
|
+
}
|
|
529
|
+
}
|
|
530
|
+
|
|
280
531
|
export async function createNode(options: NodeOptions = {}): Promise<SlimNode> {
|
|
532
|
+
return createNodeWithInternals(options, {});
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
/** {@link createNode} with {@link NodeInternals}; internal, see there. */
|
|
536
|
+
export async function createNodeWithInternals(
|
|
537
|
+
options: NodeOptions,
|
|
538
|
+
internals: NodeInternals,
|
|
539
|
+
): Promise<SlimNode> {
|
|
281
540
|
const chainId = options.chainId ?? 31337;
|
|
282
|
-
const stateMode = options.stateMode ?? 'none';
|
|
283
541
|
const senderMode: SenderMode = options.senderMode ?? 'recover';
|
|
284
542
|
const miningConfig = options.miningConfig ?? {type: 'auto'};
|
|
285
543
|
const baseFeePerGas = options.baseFeePerGas ?? 1_000_000_000n;
|
|
286
544
|
const gasPrice = options.gasPrice ?? 1_000_000_000n;
|
|
287
545
|
const maxPriorityFeePerGas = options.maxPriorityFeePerGas ?? 1_000_000_000n;
|
|
288
546
|
const blockGasLimit = options.blockGasLimit ?? 30_000_000n;
|
|
547
|
+
// Validated before anything is built, so a bad option costs nothing.
|
|
548
|
+
const stateHistoryBlocks = validateStateHistory(options);
|
|
549
|
+
const genesisTimestamp = validateGenesisTimestamp(options);
|
|
550
|
+
/**
|
|
551
|
+
* `computeStateRoot: true`: derive a trie from the flat state, report its
|
|
552
|
+
* root. Only `true` turns it on (see {@link NodeOptions.computeStateRoot}).
|
|
553
|
+
*/
|
|
554
|
+
const computeStateRoot = options.computeStateRoot === true;
|
|
289
555
|
|
|
290
556
|
const common = new Common({
|
|
291
557
|
chain: {...Mainnet, chainId, name: 'webevm'},
|
|
@@ -293,17 +559,63 @@ export async function createNode(options: NodeOptions = {}): Promise<SlimNode> {
|
|
|
293
559
|
customCrypto: {keccak256: (m: Uint8Array) => keccak_256(m)},
|
|
294
560
|
});
|
|
295
561
|
|
|
296
|
-
// State backing
|
|
297
|
-
//
|
|
298
|
-
//
|
|
299
|
-
//
|
|
300
|
-
//
|
|
301
|
-
//
|
|
302
|
-
//
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
562
|
+
// State backing, ONE for every node: our `SimpleStateManager` SUBCLASS, plain
|
|
563
|
+
// Maps with no trie. Storage is per-account with per-checkpoint OVERLAYS
|
|
564
|
+
// (upstream copies the whole flat storage map on every message frame), and it
|
|
565
|
+
// implements the `clearStorage(address)` that upstream ships as an empty no-op,
|
|
566
|
+
// without which a contract created at an address that already holds storage
|
|
567
|
+
// inherits it. See ./state-manager.ts. `computeStateRoot` does NOT change it:
|
|
568
|
+
// it adds a trie DERIVED from this state (`derivedTrie` below), which no engine
|
|
569
|
+
// ever reads.
|
|
570
|
+
const sm = new OverlayStorageStateManager();
|
|
571
|
+
|
|
572
|
+
// THE CHANGE SET, switched on BEFORE the first write so that the construction
|
|
573
|
+
// baselines below really go through the recorder and really get cleared when
|
|
574
|
+
// genesis is stored (a baseline left in the record would reconstruct EMPTY
|
|
575
|
+
// state at genesis). `changeSets` is the manager when recording, else
|
|
576
|
+
// `undefined`, so every use below is one optional call.
|
|
577
|
+
let changeSets: OverlayStorageStateManager | undefined;
|
|
578
|
+
if (
|
|
579
|
+
internals.recordChangeSets ||
|
|
580
|
+
stateHistoryBlocks !== undefined ||
|
|
581
|
+
computeStateRoot
|
|
582
|
+
) {
|
|
583
|
+
sm.enableChangeSets();
|
|
584
|
+
changeSets = sm;
|
|
585
|
+
}
|
|
586
|
+
/**
|
|
587
|
+
* The change set of the HEAD block, taken from the open record when the block
|
|
588
|
+
* was mined. With `stateHistory` it is also the newest entry of {@link sealed};
|
|
589
|
+
* this slot is what the test probe reports as `headBlock`, with or without it.
|
|
590
|
+
*/
|
|
591
|
+
let headBlockChangeSet: ChangeSet | undefined;
|
|
592
|
+
/**
|
|
593
|
+
* THE UNDO LOG (`stateHistory`): the SEALED change set of each of the last N
|
|
594
|
+
* mined blocks, oldest first. `sealed[i].changes` holds, for every key block
|
|
595
|
+
* `sealed[i].number` changed, the value it had at the END of the block before.
|
|
596
|
+
* Appended in `executeAndMine`, evicted there (anything at or below `head - N`
|
|
597
|
+
* serves no block in the window), and cleared by `loadState`. Always empty on a
|
|
598
|
+
* node without the option. See {@link historicalValue} for how a read uses it,
|
|
599
|
+
* and `docs/adr/0013-bounded-state-history-is-an-undo-log-over-the-flat-state.md`
|
|
600
|
+
* for why it is an undo log.
|
|
601
|
+
*/
|
|
602
|
+
const sealed: {number: number; changes: ChangeSet}[] = [];
|
|
603
|
+
/**
|
|
604
|
+
* The oldest block whose state the undo log can reconstruct, WHATEVER the
|
|
605
|
+
* window: genesis at construction, the loaded head after `loadState` (a loaded
|
|
606
|
+
* dump is a baseline and carries no history). The window's oldest servable
|
|
607
|
+
* block is the later of this and `head - N`.
|
|
608
|
+
*/
|
|
609
|
+
let historyFloor = 0;
|
|
610
|
+
/**
|
|
611
|
+
* Run a PURE READ (`eth_call`, `eth_estimateGas`, `eth_fillTransaction`'s
|
|
612
|
+
* estimate, with or without state overrides) with change-set recording
|
|
613
|
+
* suspended: every level it opens is reverted, so recording there is wasted
|
|
614
|
+
* work. A cost rule; see `withChangeSetsSuspended` in ./state-manager.ts.
|
|
615
|
+
*/
|
|
616
|
+
function pureRead<T>(read: () => Promise<T>): Promise<T> {
|
|
617
|
+
return changeSets ? changeSets.withChangeSetsSuspended(read) : read();
|
|
618
|
+
}
|
|
307
619
|
|
|
308
620
|
/**
|
|
309
621
|
* THE SERIALISATION POINT: the tail of a promise chain that every public entry
|
|
@@ -415,30 +727,36 @@ export async function createNode(options: NodeOptions = {}): Promise<SlimNode> {
|
|
|
415
727
|
return run;
|
|
416
728
|
}
|
|
417
729
|
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
730
|
+
/**
|
|
731
|
+
* THE DERIVED TRIE (`computeStateRoot: true` only; `undefined`, and never created,
|
|
732
|
+
* otherwise). Built from the WHOLE flat state once the genesis baselines are
|
|
733
|
+
* written and again at the end of `loadState`, and kept current from change
|
|
734
|
+
* sets by {@link currentStateRoot}. See ./derived-trie.ts.
|
|
735
|
+
*/
|
|
736
|
+
let derivedTrie: DerivedStateTrie | undefined;
|
|
423
737
|
|
|
424
738
|
const ZERO_ROOT = ZERO_HASH;
|
|
739
|
+
/**
|
|
740
|
+
* The state root of the flat state AS IT STANDS: the derived trie brought up
|
|
741
|
+
* to date with the OPEN RECORD (every key written since the head was mined)
|
|
742
|
+
* and its root read. Called at the end of each mined block, BEFORE the block's
|
|
743
|
+
* record is taken (so the block's own writes are in it), and by
|
|
744
|
+
* `getStateRoot()` for cheats since the head. Applying the open record twice
|
|
745
|
+
* (a `getStateRoot()`, then the block that includes the same writes) is
|
|
746
|
+
* harmless: the record only NAMES keys, and their values are re-read from the
|
|
747
|
+
* flat state each time. Both callers run inside the serialisation point with
|
|
748
|
+
* no checkpoint open, which the trie insists on. Without
|
|
749
|
+
* `computeStateRoot`: the zero placeholder, and no trie work at all.
|
|
750
|
+
*/
|
|
425
751
|
async function currentStateRoot(): Promise<string> {
|
|
426
|
-
if (
|
|
427
|
-
const
|
|
428
|
-
await
|
|
429
|
-
return hex(
|
|
430
|
-
}
|
|
431
|
-
|
|
432
|
-
// Flush cache into the trie after a direct state mutation (trie mode only) so
|
|
433
|
-
// the next getStateRoot() reflects it. No-op in 'none' mode (the Map IS state).
|
|
434
|
-
async function commitIfTrie(): Promise<void> {
|
|
435
|
-
if (stateMode !== 'trie') return;
|
|
436
|
-
await sm.checkpoint();
|
|
437
|
-
await sm.commit();
|
|
752
|
+
if (derivedTrie === undefined) return ZERO_ROOT;
|
|
753
|
+
const open = sm.peekChangeSet();
|
|
754
|
+
if (open !== undefined) await derivedTrie.apply(open);
|
|
755
|
+
return hex(derivedTrie.root());
|
|
438
756
|
}
|
|
439
757
|
|
|
440
758
|
// Read-modify-write an account (creating an empty one if absent), used by the
|
|
441
|
-
// evm_set* cheat methods.
|
|
759
|
+
// evm_set* cheat methods.
|
|
442
760
|
async function mutateAccount(
|
|
443
761
|
addr: ReturnType<typeof createAddressFromString>,
|
|
444
762
|
fn: (acc: Account) => void,
|
|
@@ -446,13 +764,11 @@ export async function createNode(options: NodeOptions = {}): Promise<SlimNode> {
|
|
|
446
764
|
const acc = (await sm.getAccount(addr)) ?? new Account();
|
|
447
765
|
fn(acc);
|
|
448
766
|
await sm.putAccount(addr, acc);
|
|
449
|
-
touchedAccounts.add(addr.toString());
|
|
450
|
-
await commitIfTrie();
|
|
451
767
|
}
|
|
452
768
|
|
|
453
769
|
// Minimal mock blockchain: `runTx` (inside the default engine) only needs
|
|
454
|
-
// getBlock (for BLOCKHASH) + shallowCopy.
|
|
455
|
-
// canonical state root.
|
|
770
|
+
// getBlock (for BLOCKHASH) + shallowCopy. Without `computeStateRoot` we never
|
|
771
|
+
// compute a canonical state root.
|
|
456
772
|
const blockStore = new Map<number, StoredBlock>();
|
|
457
773
|
const blockByHash = new Map<string, number>();
|
|
458
774
|
const receipts = new Map<string, SerializedReceipt>();
|
|
@@ -499,7 +815,6 @@ export async function createNode(options: NodeOptions = {}): Promise<SlimNode> {
|
|
|
499
815
|
await connectEngine(engine, {
|
|
500
816
|
stateManager: sm,
|
|
501
817
|
common,
|
|
502
|
-
stateMode,
|
|
503
818
|
// Block hashes for BLOCKHASH, read LIVE (no block exists yet at this point)
|
|
504
819
|
// and SYNCHRONOUSLY, because an engine answers BLOCKHASH mid-opcode.
|
|
505
820
|
getBlockHash: (blockNumber: bigint) => {
|
|
@@ -523,15 +838,14 @@ export async function createNode(options: NodeOptions = {}): Promise<SlimNode> {
|
|
|
523
838
|
let latestNumber = 0;
|
|
524
839
|
let parentHash = hexToBytes(ZERO_HASH);
|
|
525
840
|
|
|
526
|
-
// Initial balances FIRST (so the genesis state root,
|
|
841
|
+
// Initial balances FIRST (so the genesis state root, when computed, reflects them).
|
|
527
842
|
if (options.initialBalances) {
|
|
528
843
|
for (const [addr, bal] of Object.entries(options.initialBalances)) {
|
|
529
844
|
await sm.putAccount(createAddressFromString(addr), new Account(0n, bal));
|
|
530
|
-
touchedAccounts.add(createAddressFromString(addr).toString());
|
|
531
845
|
}
|
|
532
846
|
}
|
|
533
847
|
// Full genesis pre-state (balance/nonce/code/storage) — e.g. a GeneralStateTest
|
|
534
|
-
// `pre` section. Applied before block 0 so
|
|
848
|
+
// `pre` section. Applied before block 0 so a computed genesis root reflects
|
|
535
849
|
// it and a post-tx getStateRoot() can be compared to the fixture's hash.
|
|
536
850
|
if (options.initialState) {
|
|
537
851
|
for (const [addr, acc] of Object.entries(options.initialState)) {
|
|
@@ -549,14 +863,13 @@ export async function createNode(options: NodeOptions = {}): Promise<SlimNode> {
|
|
|
549
863
|
hexToBytes(val),
|
|
550
864
|
);
|
|
551
865
|
}
|
|
552
|
-
touchedAccounts.add(address.toString());
|
|
553
|
-
}
|
|
554
|
-
// Commit the cache into the trie so the genesis root is correct in trie mode.
|
|
555
|
-
if (stateMode === 'trie') {
|
|
556
|
-
await sm.checkpoint();
|
|
557
|
-
await sm.commit();
|
|
558
866
|
}
|
|
559
867
|
}
|
|
868
|
+
// THE TRIE IS BUILT FROM THE FULL FLAT STATE, here, once: the baselines above
|
|
869
|
+
// are block 0 and deliberately in NO change set (the record is cleared just
|
|
870
|
+
// after genesis is stored), so a trie kept from change sets alone would omit
|
|
871
|
+
// them and every genesis-dependent root would be wrong.
|
|
872
|
+
if (computeStateRoot) derivedTrie = await DerivedStateTrie.build(sm, common);
|
|
560
873
|
|
|
561
874
|
const blockEnv = options.blockEnv;
|
|
562
875
|
|
|
@@ -589,13 +902,16 @@ export async function createNode(options: NodeOptions = {}): Promise<SlimNode> {
|
|
|
589
902
|
// `number`, `timestamp` and `gasLimit` stay the node's own: genesis IS block 0,
|
|
590
903
|
// and `blockEnv.number` places a MINED block rather than renumbering the chain's
|
|
591
904
|
// genesis (`minedBlockGasLimit` above is likewise the MINED block's limit).
|
|
905
|
+
// Its timestamp is `genesisTimestamp` when given, else the wall clock: a
|
|
906
|
+
// separate option rather than `blockEnv.timestamp`, which pins every MINED
|
|
907
|
+
// block to one value and so cannot also be block 0's (see the option's JSDoc).
|
|
592
908
|
const genesis = createBlock(
|
|
593
909
|
{
|
|
594
910
|
header: {
|
|
595
911
|
number: 0n,
|
|
596
912
|
gasLimit: blockGasLimit,
|
|
597
913
|
baseFeePerGas,
|
|
598
|
-
timestamp: BigInt(Math.floor(Date.now() / 1000)),
|
|
914
|
+
timestamp: genesisTimestamp ?? BigInt(Math.floor(Date.now() / 1000)),
|
|
599
915
|
...(blockEnv?.coinbase
|
|
600
916
|
? {coinbase: createAddressFromString(blockEnv.coinbase)}
|
|
601
917
|
: {}),
|
|
@@ -608,6 +924,9 @@ export async function createNode(options: NodeOptions = {}): Promise<SlimNode> {
|
|
|
608
924
|
{common},
|
|
609
925
|
);
|
|
610
926
|
storeBlock(genesis, [], [], await currentStateRoot());
|
|
927
|
+
// BASELINES ARE NOT HISTORY: `initialBalances` / `initialState` ARE block 0,
|
|
928
|
+
// not a change to it.
|
|
929
|
+
changeSets?.takeChangeSet();
|
|
611
930
|
|
|
612
931
|
// Pending raw txs awaiting the next mined block (manual/interval modes). Each
|
|
613
932
|
// carries its SENDER, decided once at parse time by `parseTx`: the node derives
|
|
@@ -616,6 +935,8 @@ export async function createNode(options: NodeOptions = {}): Promise<SlimNode> {
|
|
|
616
935
|
|
|
617
936
|
// newHeads subscribers.
|
|
618
937
|
const headSubs = new Set<(h: {number: number; hash: string}) => void>();
|
|
938
|
+
// Ports `serveOn` is serving, so `dispose` can stop them all.
|
|
939
|
+
const servedPorts = new Set<ServedProvider>();
|
|
619
940
|
|
|
620
941
|
// THE INTERVAL TIMER IS ARMED AT THE BOTTOM OF THIS FUNCTION, not here, at the
|
|
621
942
|
// `miningConfig.type === 'interval'` block just above the `return`. The reason
|
|
@@ -741,13 +1062,6 @@ export async function createNode(options: NodeOptions = {}): Promise<SlimNode> {
|
|
|
741
1062
|
const from = sender.toString();
|
|
742
1063
|
const to = (tx as any).to ? (tx as any).to.toString() : null;
|
|
743
1064
|
const created = res.createdAddress ? hex(res.createdAddress) : null;
|
|
744
|
-
// Track touched accounts for the trie-mode dump (sender, recipient, created,
|
|
745
|
-
// and any account that emitted a log — that set covers what changed).
|
|
746
|
-
touchedAccounts.add(from);
|
|
747
|
-
if (to) touchedAccounts.add(to);
|
|
748
|
-
if (created) touchedAccounts.add(created);
|
|
749
|
-
for (const log of res.logs) touchedAccounts.add(hex(log.address));
|
|
750
|
-
|
|
751
1065
|
const rcptLogs: SerializedLog[] = res.logs.map((log, i) => {
|
|
752
1066
|
const sl: SerializedLog = {
|
|
753
1067
|
address: hex(log.address),
|
|
@@ -813,13 +1127,45 @@ export async function createNode(options: NodeOptions = {}): Promise<SlimNode> {
|
|
|
813
1127
|
}
|
|
814
1128
|
|
|
815
1129
|
allLogs.push(...blockLogs);
|
|
1130
|
+
// The root BEFORE the record is taken below: `currentStateRoot` applies the
|
|
1131
|
+
// open record, which at this point is exactly what this block changed.
|
|
816
1132
|
storeBlock(block, txHashes, blockLogs, await currentStateRoot());
|
|
1133
|
+
// THE BLOCK'S CHANGE SET is everything written since the previous head was
|
|
1134
|
+
// mined: this block's transactions AND any `evm_set*` cheat issued between
|
|
1135
|
+
// the two blocks, which belongs to this block (the first one that can see
|
|
1136
|
+
// it). Taken with no checkpoint open, which `takeChangeSet` insists on.
|
|
1137
|
+
if (changeSets) headBlockChangeSet = changeSets.takeChangeSet();
|
|
1138
|
+
if (stateHistoryBlocks !== undefined && headBlockChangeSet !== undefined)
|
|
1139
|
+
sealBlock(blockNumber, headBlockChangeSet, stateHistoryBlocks);
|
|
817
1140
|
// emit newHeads
|
|
818
1141
|
for (const cb of headSubs) cb({number: blockNumber, hash: blockHash});
|
|
819
1142
|
|
|
820
1143
|
return {blockNumber, blockHash, txHashes};
|
|
821
1144
|
}
|
|
822
1145
|
|
|
1146
|
+
/**
|
|
1147
|
+
* SEAL the change set just taken as block `number`'s undo record, and EVICT
|
|
1148
|
+
* every record at or below `head - N`: a read at the oldest servable block
|
|
1149
|
+
* `head - N` needs the records of blocks `head - N + 1 .. head` and no other,
|
|
1150
|
+
* so exactly N are kept.
|
|
1151
|
+
*
|
|
1152
|
+
* A block MINED UNDER THE SAME NUMBER as the newest record (a fixed
|
|
1153
|
+
* `blockEnv.number` does that: every mined block replaces the head) is MERGED
|
|
1154
|
+
* into it, the older entry winning, as a checkpoint commit merges: the merged
|
|
1155
|
+
* record then holds each key's value from before the first of them, which is
|
|
1156
|
+
* what a read below that number needs, and the log stays bounded.
|
|
1157
|
+
*/
|
|
1158
|
+
function sealBlock(number: number, changes: ChangeSet, window: number): void {
|
|
1159
|
+
const newest = sealed[sealed.length - 1];
|
|
1160
|
+
if (newest !== undefined && newest.number === number)
|
|
1161
|
+
mergeChangeSetOlderWins(newest.changes, changes);
|
|
1162
|
+
else sealed.push({number, changes});
|
|
1163
|
+
const oldestKept = latestNumber - window;
|
|
1164
|
+
let drop = 0;
|
|
1165
|
+
while (drop < sealed.length && sealed[drop].number <= oldestKept) drop++;
|
|
1166
|
+
if (drop > 0) sealed.splice(0, drop);
|
|
1167
|
+
}
|
|
1168
|
+
|
|
823
1169
|
async function mineBlock() {
|
|
824
1170
|
const batch = pending.splice(0, pending.length);
|
|
825
1171
|
return executeAndMine(batch);
|
|
@@ -1135,7 +1481,28 @@ export async function createNode(options: NodeOptions = {}): Promise<SlimNode> {
|
|
|
1135
1481
|
return r ? receiptToRpc(r) : null;
|
|
1136
1482
|
}
|
|
1137
1483
|
|
|
1484
|
+
/**
|
|
1485
|
+
* The nonce `address` sends with next: its account nonce, advanced past each
|
|
1486
|
+
* CONTIGUOUS queued transaction of its own (manual/interval mining; the queue
|
|
1487
|
+
* is always empty under auto mining). Contiguous, as geth counts it: a queued
|
|
1488
|
+
* nonce beyond a gap does not advance it, because that transaction will not
|
|
1489
|
+
* execute until the gap is filled.
|
|
1490
|
+
*/
|
|
1491
|
+
async function pendingNonce(address: Address): Promise<bigint> {
|
|
1492
|
+
let n = (await sm.getAccount(address))?.nonce ?? 0n;
|
|
1493
|
+
const queued = new Set<bigint>();
|
|
1494
|
+
for (const q of pending)
|
|
1495
|
+
if (q.sender.equals(address)) queued.add(q.tx.nonce);
|
|
1496
|
+
while (queued.has(n)) n++;
|
|
1497
|
+
return n;
|
|
1498
|
+
}
|
|
1499
|
+
|
|
1138
1500
|
// ---------- block lookup helpers ----------
|
|
1501
|
+
/**
|
|
1502
|
+
* A block TAG or NUMBER as a block number. A string that is neither is a
|
|
1503
|
+
* -32602, not a JavaScript `SyntaxError` escaping from `BigInt` (which is what
|
|
1504
|
+
* `eth_getBlockByNumber('yesterday')` used to throw), and not a silent `latest`.
|
|
1505
|
+
*/
|
|
1139
1506
|
function resolveBlockTag(tag: unknown): number {
|
|
1140
1507
|
if (
|
|
1141
1508
|
tag === 'latest' ||
|
|
@@ -1146,9 +1513,285 @@ export async function createNode(options: NodeOptions = {}): Promise<SlimNode> {
|
|
|
1146
1513
|
)
|
|
1147
1514
|
return latestNumber;
|
|
1148
1515
|
if (tag === 'earliest') return 0;
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
|
|
1516
|
+
// A 32-byte hex string is a block HASH, which this parameter does not take:
|
|
1517
|
+
// read as a number it would be ~1e76 and look like "far above the head".
|
|
1518
|
+
if (typeof tag === 'string' && /^0x[0-9a-fA-F]{64}$/.test(tag))
|
|
1519
|
+
throw new RpcError(
|
|
1520
|
+
-32602,
|
|
1521
|
+
`invalid block parameter: ${tag} is a block hash, and this parameter takes a block number or tag.`,
|
|
1522
|
+
);
|
|
1523
|
+
if (typeof tag === 'string' && /^0x[0-9a-fA-F]+$/.test(tag))
|
|
1524
|
+
return Number(BigInt(tag));
|
|
1525
|
+
if (typeof tag === 'number' && Number.isSafeInteger(tag) && tag >= 0)
|
|
1526
|
+
return tag;
|
|
1527
|
+
throw new RpcError(
|
|
1528
|
+
-32602,
|
|
1529
|
+
`invalid block parameter: ${JSON.stringify(tag)}. Expected a block tag (latest, pending, safe, finalized, earliest) or a hex block number.`,
|
|
1530
|
+
);
|
|
1531
|
+
}
|
|
1532
|
+
|
|
1533
|
+
/**
|
|
1534
|
+
* THE GATE FOR EVERY STATE READ AT A BLOCK: `undefined` when the parameter
|
|
1535
|
+
* names the head (read or execute live), the block number K when it names a
|
|
1536
|
+
* block in the `stateHistory` window (served from the undo log), and a refusal
|
|
1537
|
+
* otherwise. Every method that reads STATE at a block (`eth_call`,
|
|
1538
|
+
* `eth_estimateGas`, `eth_getBalance`, `eth_getCode`, `eth_getStorageAt`,
|
|
1539
|
+
* `eth_getTransactionCount`) calls this with its block parameter BEFORE
|
|
1540
|
+
* touching state.
|
|
1541
|
+
*
|
|
1542
|
+
* ## Why this exists
|
|
1543
|
+
*
|
|
1544
|
+
* Those methods used to ignore the block parameter entirely and answer from the
|
|
1545
|
+
* live state, while `eth_getLogs` and `eth_getBlockByNumber` honoured theirs. A
|
|
1546
|
+
* client that pins a PAIR of reads to one block so they describe the same moment
|
|
1547
|
+
* (logs up to block N, and a view call at block N) got logs as of N and storage
|
|
1548
|
+
* as of the head: a position whose move it could not find, with no error. That is
|
|
1549
|
+
* a different answer from every other node, delivered silently, which is the one
|
|
1550
|
+
* outcome that is not acceptable. (This gate was `requireHeadState` while the
|
|
1551
|
+
* node served the head only.)
|
|
1552
|
+
*
|
|
1553
|
+
* ## What the node holds: the head, plus an OPT-IN undo log
|
|
1554
|
+
*
|
|
1555
|
+
* This node holds ONE state: the live one. On every node the
|
|
1556
|
+
* state manager's checkpoint/overlay stack is the journal of the execution in
|
|
1557
|
+
* flight, not a history of blocks. Without `stateHistory` nothing of block N
|
|
1558
|
+
* survives the mining of N+1, and the honest answer to "what was the state at
|
|
1559
|
+
* N" is "not available here".
|
|
1560
|
+
*
|
|
1561
|
+
* WITH `stateHistory: {blocks: N}` the node keeps, per mined block, the value
|
|
1562
|
+
* every key the block changed had BEFORE it (the sealed change sets, an undo
|
|
1563
|
+
* log over the one flat state; see
|
|
1564
|
+
* `docs/adr/0013-bounded-state-history-is-an-undo-log-over-the-flat-state.md`).
|
|
1565
|
+
* The four POINT reads at a block K in the window are answered from it by
|
|
1566
|
+
* {@link historicalValue} with no state manager involvement and no checkpoint.
|
|
1567
|
+
* `eth_call` and `eth_estimateGas` at K EXECUTE: the same records, as K's
|
|
1568
|
+
* state, are written into a checkpoint the request opens and reverts
|
|
1569
|
+
* ({@link historicalEntries}, {@link withStateEntries}), and the engine runs
|
|
1570
|
+
* with K's stored `Block`. That SUPERSEDES the reasoning this comment used to
|
|
1571
|
+
* give for refusing all history (retained copies, or swapping a shared state
|
|
1572
|
+
* manager's root under a read): an undo log needs neither.
|
|
1573
|
+
*
|
|
1574
|
+
* ## What is served, and what is refused
|
|
1575
|
+
*
|
|
1576
|
+
* - SERVED: `latest`, `pending`, `safe`, `finalized`, an absent parameter, and a
|
|
1577
|
+
* number, hash or EIP-1898 `{blockNumber}` / `{blockHash}` object that names
|
|
1578
|
+
* the HEAD block. Pinning to the block you just read is the common case and it
|
|
1579
|
+
* is almost always the head, so it must keep working. (`pending` is the head
|
|
1580
|
+
* because in `manual`/`interval` mining the queued transactions have not
|
|
1581
|
+
* executed yet and this node has no speculative pending state; the one
|
|
1582
|
+
* exception is `eth_getTransactionCount`, which adds the sender's queued
|
|
1583
|
+
* transactions so that two sends before a mine get two nonces. `safe` and
|
|
1584
|
+
* `finalized` are the head because a local chain has no reorgs.) `earliest`
|
|
1585
|
+
* is served while block 0 is the head or in the window.
|
|
1586
|
+
* - SERVED FROM HISTORY: any block from {@link oldestServableBlock} up to the
|
|
1587
|
+
* head, named any of those ways, by all six methods.
|
|
1588
|
+
* - REFUSED, -32000 `historical state not available`: a block below the oldest
|
|
1589
|
+
* servable one (with no `stateHistory`, that is every block below the head),
|
|
1590
|
+
* with a message naming the oldest servable block and the `stateHistory`
|
|
1591
|
+
* option. The same code geth uses for pruned state (`missing trie node`).
|
|
1592
|
+
* - REFUSED, -32000 `header not found`: a number above the head, a hash no
|
|
1593
|
+
* block has (geth's exact wording for both), or a number between two blocks
|
|
1594
|
+
* of a chain whose numbering jumps.
|
|
1595
|
+
* - REFUSED, -32602: a parameter that is not a block at all.
|
|
1596
|
+
*
|
|
1597
|
+
* THE STATE AT THE HEAD INCLUDES `evm_set*` CHEATS applied since it was mined,
|
|
1598
|
+
* exactly as `latest` always has: those mutate live state without a block. They
|
|
1599
|
+
* are in the OPEN record, which a read below the head consults after the sealed
|
|
1600
|
+
* ones, so they are visible at the head and at no older block.
|
|
1601
|
+
*/
|
|
1602
|
+
function historicalBlock(
|
|
1603
|
+
blockParam: unknown,
|
|
1604
|
+
method: string,
|
|
1605
|
+
): number | undefined {
|
|
1606
|
+
const number = pinnedBlockNumber(blockParam, method);
|
|
1607
|
+
if (number === latestNumber) return undefined;
|
|
1608
|
+
const oldest = oldestServableBlock();
|
|
1609
|
+
if (number < oldest) throw beyondHistory(method, number, oldest);
|
|
1610
|
+
// Numbering has gaps only under a `blockEnv.number` that jumps; a number
|
|
1611
|
+
// between two blocks names no block, and is not answered as if it did.
|
|
1612
|
+
if (!blockStore.has(number))
|
|
1613
|
+
throw new RpcError(
|
|
1614
|
+
-32000,
|
|
1615
|
+
`header not found: ${method} was pinned to block ${number}, which this node never mined.`,
|
|
1616
|
+
);
|
|
1617
|
+
return number;
|
|
1618
|
+
}
|
|
1619
|
+
|
|
1620
|
+
/**
|
|
1621
|
+
* The oldest block whose state a point read can be answered at: the head
|
|
1622
|
+
* without `stateHistory`; with it, `head - N`, but never below where the undo
|
|
1623
|
+
* log starts ({@link historyFloor}: genesis, or the head a state was loaded at).
|
|
1624
|
+
*/
|
|
1625
|
+
function oldestServableBlock(): number {
|
|
1626
|
+
if (stateHistoryBlocks === undefined) return latestNumber;
|
|
1627
|
+
return Math.max(historyFloor, latestNumber - stateHistoryBlocks, 0);
|
|
1628
|
+
}
|
|
1629
|
+
|
|
1630
|
+
/** The refusal for a block older than {@link oldestServableBlock}. */
|
|
1631
|
+
function beyondHistory(
|
|
1632
|
+
method: string,
|
|
1633
|
+
number: number,
|
|
1634
|
+
oldest: number,
|
|
1635
|
+
): RpcError {
|
|
1636
|
+
const holds =
|
|
1637
|
+
stateHistoryBlocks === undefined
|
|
1638
|
+
? `but the oldest block whose state this node holds is block ${oldest}, its head, because it was created without stateHistory. ` +
|
|
1639
|
+
`Create the node with stateHistory: {blocks: N} to keep the last N blocks readable, or pin to the head (or 'latest') instead.`
|
|
1640
|
+
: `but the oldest block whose state this node holds is block ${oldest} (head ${latestNumber}, stateHistory: {blocks: ${stateHistoryBlocks}}` +
|
|
1641
|
+
(oldest > latestNumber - stateHistoryBlocks
|
|
1642
|
+
? `; history starts at block ${oldest}, where this node's state was loaded`
|
|
1643
|
+
: '') +
|
|
1644
|
+
`). Pin to a block in that window, or widen stateHistory.blocks.`;
|
|
1645
|
+
return new RpcError(
|
|
1646
|
+
-32000,
|
|
1647
|
+
`historical state not available: ${method} was pinned to block ${number}, ${holds} ` +
|
|
1648
|
+
`It REFUSES rather than answer from a later state than the one you asked for; logs and blocks remain queryable at any height.`,
|
|
1649
|
+
);
|
|
1650
|
+
}
|
|
1651
|
+
|
|
1652
|
+
/**
|
|
1653
|
+
* THE BLOCK NUMBER a state read's block parameter names (the head for a tag),
|
|
1654
|
+
* refusing what is not a block (-32602), a hash no block has and a number above
|
|
1655
|
+
* the head (-32000 `header not found`).
|
|
1656
|
+
*/
|
|
1657
|
+
function pinnedBlockNumber(blockParam: unknown, method: string): number {
|
|
1658
|
+
if (
|
|
1659
|
+
blockParam == null ||
|
|
1660
|
+
blockParam === 'latest' ||
|
|
1661
|
+
blockParam === 'pending' ||
|
|
1662
|
+
blockParam === 'safe' ||
|
|
1663
|
+
blockParam === 'finalized'
|
|
1664
|
+
)
|
|
1665
|
+
return latestNumber;
|
|
1666
|
+
let number: number | undefined;
|
|
1667
|
+
let hash: string | undefined;
|
|
1668
|
+
if (blockParam === 'earliest') number = 0;
|
|
1669
|
+
else if (typeof blockParam === 'number') number = blockParam;
|
|
1670
|
+
else if (typeof blockParam === 'string') {
|
|
1671
|
+
// A 32-byte hex string is a block HASH (geth accepts it bare, EIP-1898).
|
|
1672
|
+
if (/^0x[0-9a-fA-F]{64}$/.test(blockParam)) hash = blockParam;
|
|
1673
|
+
else if (/^0x[0-9a-fA-F]+$/.test(blockParam))
|
|
1674
|
+
number = Number(BigInt(blockParam));
|
|
1675
|
+
} else if (typeof blockParam === 'object') {
|
|
1676
|
+
const o = blockParam as {blockNumber?: unknown; blockHash?: unknown};
|
|
1677
|
+
// EIP-1898: exactly one of the two. geth refuses both at once, and so
|
|
1678
|
+
// does this node rather than silently preferring one.
|
|
1679
|
+
if (o.blockHash != null && o.blockNumber != null)
|
|
1680
|
+
throw new RpcError(
|
|
1681
|
+
-32602,
|
|
1682
|
+
`invalid block parameter for ${method}: pass blockHash or blockNumber, not both.`,
|
|
1683
|
+
);
|
|
1684
|
+
if (typeof o.blockHash === 'string') hash = o.blockHash;
|
|
1685
|
+
else if (typeof o.blockNumber === 'string') {
|
|
1686
|
+
if (o.blockNumber === 'earliest') number = 0;
|
|
1687
|
+
else if (/^0x[0-9a-fA-F]+$/.test(o.blockNumber))
|
|
1688
|
+
number = Number(BigInt(o.blockNumber));
|
|
1689
|
+
else return pinnedBlockNumber(o.blockNumber, method);
|
|
1690
|
+
}
|
|
1691
|
+
}
|
|
1692
|
+
if (hash !== undefined) {
|
|
1693
|
+
const n = blockByHash.get(hash.toLowerCase());
|
|
1694
|
+
if (n === undefined)
|
|
1695
|
+
throw new RpcError(
|
|
1696
|
+
-32000,
|
|
1697
|
+
`header not found: ${method} was pinned to block hash ${hash}, which no block on this node has.`,
|
|
1698
|
+
);
|
|
1699
|
+
number = n;
|
|
1700
|
+
}
|
|
1701
|
+
// A well-formed number too large to be a safe integer names a block far
|
|
1702
|
+
// above the head: that is "not found", not "not a block".
|
|
1703
|
+
if (number !== undefined && number > Number.MAX_SAFE_INTEGER)
|
|
1704
|
+
number = Number.MAX_SAFE_INTEGER;
|
|
1705
|
+
if (number === undefined || !Number.isSafeInteger(number) || number < 0)
|
|
1706
|
+
throw new RpcError(
|
|
1707
|
+
-32602,
|
|
1708
|
+
`invalid block parameter for ${method}: ${JSON.stringify(blockParam)}. ` +
|
|
1709
|
+
`Expected a block tag (latest, pending, safe, finalized, earliest), a hex block number, a block hash, or {blockNumber}/{blockHash}.`,
|
|
1710
|
+
);
|
|
1711
|
+
if (number > latestNumber)
|
|
1712
|
+
throw new RpcError(
|
|
1713
|
+
-32000,
|
|
1714
|
+
`header not found: ${method} was pinned to block ${number}, but the head of this node is block ${latestNumber}.`,
|
|
1715
|
+
);
|
|
1716
|
+
return number;
|
|
1717
|
+
}
|
|
1718
|
+
|
|
1719
|
+
/**
|
|
1720
|
+
* ONE KEY'S VALUE AT THE END OF BLOCK `k` (below the head, in the window): the
|
|
1721
|
+
* FIRST record naming the key among the sealed records of blocks `k+1 .. head`
|
|
1722
|
+
* (oldest first), then the OPEN record (writes since the head was mined:
|
|
1723
|
+
* cheats, and a batch that threw mid-block), else the live value. Each record
|
|
1724
|
+
* holds a key's value from before its block, so the first one after `k` that
|
|
1725
|
+
* names the key holds its value at the end of `k`; a key no record names has
|
|
1726
|
+
* not changed since `k`, so its live value is its value then.
|
|
1727
|
+
*
|
|
1728
|
+
* A PURE LOOKUP: no state manager write, no checkpoint, and nothing an engine
|
|
1729
|
+
* sees, which is why it is safe on both engines and needs nothing from the
|
|
1730
|
+
* serialisation point beyond running inside it (the dispatcher does). `pick`
|
|
1731
|
+
* returns `{value}` when the record names the key, `undefined` when it does
|
|
1732
|
+
* not; `live` reads the head.
|
|
1733
|
+
*/
|
|
1734
|
+
async function historicalValue<T>(
|
|
1735
|
+
k: number,
|
|
1736
|
+
pick: (changes: ChangeSet) => {value: T} | undefined,
|
|
1737
|
+
live: () => Promise<T>,
|
|
1738
|
+
): Promise<T> {
|
|
1739
|
+
for (const record of sealed) {
|
|
1740
|
+
if (record.number <= k) continue;
|
|
1741
|
+
const hit = pick(record.changes);
|
|
1742
|
+
if (hit !== undefined) return hit.value;
|
|
1743
|
+
}
|
|
1744
|
+
const open = changeSets?.peekChangeSet();
|
|
1745
|
+
const hit = open && pick(open);
|
|
1746
|
+
if (hit !== undefined) return hit.value;
|
|
1747
|
+
return live();
|
|
1748
|
+
}
|
|
1749
|
+
|
|
1750
|
+
/** The account at `k`, `undefined` if absent: see {@link historicalValue}. */
|
|
1751
|
+
function accountAt(
|
|
1752
|
+
k: number,
|
|
1753
|
+
address: Address,
|
|
1754
|
+
): Promise<Account | undefined> {
|
|
1755
|
+
const key = address.toString();
|
|
1756
|
+
return historicalValue(
|
|
1757
|
+
k,
|
|
1758
|
+
(cs) =>
|
|
1759
|
+
cs.accounts.has(key) ? {value: cs.accounts.get(key)} : undefined,
|
|
1760
|
+
() => sm.getAccount(address),
|
|
1761
|
+
);
|
|
1762
|
+
}
|
|
1763
|
+
|
|
1764
|
+
/** The code at `k`, empty if none: see {@link historicalValue}. */
|
|
1765
|
+
function codeAt(k: number, address: Address): Promise<Uint8Array> {
|
|
1766
|
+
const key = address.toString();
|
|
1767
|
+
return historicalValue(
|
|
1768
|
+
k,
|
|
1769
|
+
(cs) =>
|
|
1770
|
+
cs.code.has(key)
|
|
1771
|
+
? {value: cs.code.get(key) ?? new Uint8Array(0)}
|
|
1772
|
+
: undefined,
|
|
1773
|
+
() => sm.getCode(address),
|
|
1774
|
+
);
|
|
1775
|
+
}
|
|
1776
|
+
|
|
1777
|
+
/** One storage slot at `k`, empty if unset: see {@link historicalValue}. */
|
|
1778
|
+
function storageAt(
|
|
1779
|
+
k: number,
|
|
1780
|
+
address: Address,
|
|
1781
|
+
slot: Uint8Array,
|
|
1782
|
+
): Promise<Uint8Array> {
|
|
1783
|
+
const addressKey = packAddressKey(address.bytes);
|
|
1784
|
+
const slotKey = packSlotKey(slot);
|
|
1785
|
+
return historicalValue(
|
|
1786
|
+
k,
|
|
1787
|
+
(cs) => {
|
|
1788
|
+
const slots = cs.storage.get(addressKey);
|
|
1789
|
+
return slots?.has(slotKey)
|
|
1790
|
+
? {value: slots.get(slotKey) ?? new Uint8Array(0)}
|
|
1791
|
+
: undefined;
|
|
1792
|
+
},
|
|
1793
|
+
() => sm.getStorage(address, slot),
|
|
1794
|
+
);
|
|
1152
1795
|
}
|
|
1153
1796
|
|
|
1154
1797
|
/**
|
|
@@ -1161,7 +1804,7 @@ export async function createNode(options: NodeOptions = {}): Promise<SlimNode> {
|
|
|
1161
1804
|
* a uniform zero, because nothing tells a consumer which side of the trip they
|
|
1162
1805
|
* are on. Every value below is therefore one a dump carries.
|
|
1163
1806
|
*
|
|
1164
|
-
* `stateRoot` is the one REAL value among the roots (
|
|
1807
|
+
* `stateRoot` is the one REAL value among the roots (with `computeStateRoot: true`);
|
|
1165
1808
|
* `sha3Uncles`, `transactionsRoot` and `receiptsRoot` stay honest zero
|
|
1166
1809
|
* placeholders, named as such in the README, because this node builds no tries
|
|
1167
1810
|
* over its transactions or receipts. `logsBloom` used to be in that list and is
|
|
@@ -1261,6 +1904,296 @@ export async function createNode(options: NodeOptions = {}): Promise<SlimNode> {
|
|
|
1261
1904
|
};
|
|
1262
1905
|
}
|
|
1263
1906
|
|
|
1907
|
+
/**
|
|
1908
|
+
* K'S STATE, AS STATE ENTRIES: for every key the undo log names after block
|
|
1909
|
+
* `k` (the sealed records of blocks `k+1 .. head`, then the OPEN record), the
|
|
1910
|
+
* value it had at the end of `k`, the earliest record winning per key, exactly
|
|
1911
|
+
* the lookup {@link historicalValue} makes for one key. A key no record names
|
|
1912
|
+
* has not changed since `k`, so the live state already holds it and it needs
|
|
1913
|
+
* no entry. O(keys changed since `k`), which is the cost of a historical
|
|
1914
|
+
* `eth_call` on top of the call itself.
|
|
1915
|
+
*
|
|
1916
|
+
* Written into a checkpoint by {@link withStateEntries}, so the engine
|
|
1917
|
+
* executes on K's state with no copy of state and no root swapped. An account
|
|
1918
|
+
* absent at `k` becomes an ABSENT entry, and an account whose storage was
|
|
1919
|
+
* cleared after `k` becomes a `clearStorage` entry: the clear recorded every
|
|
1920
|
+
* slot the account held at that moment, so the union holds every slot it
|
|
1921
|
+
* held at `k`, and clearing first also drops the slots written since.
|
|
1922
|
+
*
|
|
1923
|
+
* THE UNION IS BUILT FRESH. `mergeChangeSetOlderWins` is not reused here
|
|
1924
|
+
* because it CONSUMES its second argument (inner storage maps are handed over
|
|
1925
|
+
* whole), and these are the sealed records themselves, which a later read
|
|
1926
|
+
* still needs intact.
|
|
1927
|
+
*/
|
|
1928
|
+
function historicalEntries(k: number): StateEntry[] {
|
|
1929
|
+
const accounts = new Map<string, Account | undefined>();
|
|
1930
|
+
const code = new Map<string, Uint8Array | undefined>();
|
|
1931
|
+
const storage = new Map<string, Map<string, Uint8Array | undefined>>();
|
|
1932
|
+
const cleared = new Set<string>();
|
|
1933
|
+
const take = (cs: ChangeSet) => {
|
|
1934
|
+
for (const [key, value] of cs.accounts)
|
|
1935
|
+
if (!accounts.has(key)) accounts.set(key, value);
|
|
1936
|
+
for (const [key, value] of cs.code)
|
|
1937
|
+
if (!code.has(key)) code.set(key, value);
|
|
1938
|
+
for (const [addressKey, slots] of cs.storage) {
|
|
1939
|
+
let target = storage.get(addressKey);
|
|
1940
|
+
if (target === undefined) {
|
|
1941
|
+
target = new Map();
|
|
1942
|
+
storage.set(addressKey, target);
|
|
1943
|
+
}
|
|
1944
|
+
for (const [slotKey, value] of slots)
|
|
1945
|
+
if (!target.has(slotKey)) target.set(slotKey, value);
|
|
1946
|
+
}
|
|
1947
|
+
for (const addressKey of cs.storageCleared) cleared.add(addressKey);
|
|
1948
|
+
};
|
|
1949
|
+
for (const record of sealed) if (record.number > k) take(record.changes);
|
|
1950
|
+
const open = changeSets?.peekChangeSet();
|
|
1951
|
+
if (open) take(open);
|
|
1952
|
+
|
|
1953
|
+
// One entry per address, keyed as `address.toString()` gives it (the
|
|
1954
|
+
// account and code maps' key; `unpackAddressKey` yields the same form).
|
|
1955
|
+
const entries = new Map<string, StateEntry>();
|
|
1956
|
+
const entry = (key: string): StateEntry => {
|
|
1957
|
+
let e = entries.get(key);
|
|
1958
|
+
if (e === undefined) {
|
|
1959
|
+
e = {
|
|
1960
|
+
address: createAddressFromString(key),
|
|
1961
|
+
clearStorage: false,
|
|
1962
|
+
slots: [],
|
|
1963
|
+
absent: false,
|
|
1964
|
+
};
|
|
1965
|
+
entries.set(key, e);
|
|
1966
|
+
}
|
|
1967
|
+
return e;
|
|
1968
|
+
};
|
|
1969
|
+
for (const [key, account] of accounts) {
|
|
1970
|
+
const e = entry(key);
|
|
1971
|
+
if (account === undefined) e.absent = true;
|
|
1972
|
+
// A COPY: the engine is handed the object through the state manager, and
|
|
1973
|
+
// the sealed record must go on saying what the account was.
|
|
1974
|
+
else e.account = copyAccount(account);
|
|
1975
|
+
}
|
|
1976
|
+
for (const [key, value] of code)
|
|
1977
|
+
entry(key).code = value ?? new Uint8Array(0);
|
|
1978
|
+
for (const addressKey of cleared)
|
|
1979
|
+
entry(unpackAddressKey(addressKey)).clearStorage = true;
|
|
1980
|
+
for (const [addressKey, slots] of storage) {
|
|
1981
|
+
const e = entry(unpackAddressKey(addressKey));
|
|
1982
|
+
for (const [slotKey, value] of slots)
|
|
1983
|
+
e.slots.push({
|
|
1984
|
+
key: hexToBytes(unpackSlotKey(slotKey)),
|
|
1985
|
+
value: value ?? new Uint8Array(0),
|
|
1986
|
+
});
|
|
1987
|
+
}
|
|
1988
|
+
return [...entries.values()];
|
|
1989
|
+
}
|
|
1990
|
+
|
|
1991
|
+
/**
|
|
1992
|
+
* PARSE geth-STYLE STATE OVERRIDES into {@link StateEntry}s. `eth_call` and
|
|
1993
|
+
* `eth_estimateGas` take them as their third parameter:
|
|
1994
|
+
* `{[address]: {balance?, nonce?, code?, state?, stateDiff?}}`, where `state`
|
|
1995
|
+
* REPLACES the account's whole storage and `stateDiff` patches individual
|
|
1996
|
+
* slots. They used to be ignored, which answered a question the caller had not
|
|
1997
|
+
* asked, with no error.
|
|
1998
|
+
*
|
|
1999
|
+
* PARSED BEFORE ANY LEVEL IS OPENED, into values that cannot fail to apply, so
|
|
2000
|
+
* a malformed override is a -32602 naming it (never a raw `SyntaxError` from
|
|
2001
|
+
* `BigInt`), and a refusal never has a half-applied override to unwind.
|
|
2002
|
+
*
|
|
2003
|
+
* ## What is refused
|
|
2004
|
+
*
|
|
2005
|
+
* `state` and `stateDiff` on the same account (geth refuses that too), and any
|
|
2006
|
+
* field this node does not implement (`movePrecompileToAddress`, for one): a
|
|
2007
|
+
* -32602 naming it, never a call run without it.
|
|
2008
|
+
*/
|
|
2009
|
+
function parseStateOverrides(
|
|
2010
|
+
overrides: unknown,
|
|
2011
|
+
method: string,
|
|
2012
|
+
): StateEntry[] {
|
|
2013
|
+
if (overrides == null) return [];
|
|
2014
|
+
if (typeof overrides !== 'object' || Array.isArray(overrides))
|
|
2015
|
+
throw new RpcError(
|
|
2016
|
+
-32602,
|
|
2017
|
+
`invalid state overrides for ${method}: expected an object keyed by address.`,
|
|
2018
|
+
);
|
|
2019
|
+
const supported = new Set([
|
|
2020
|
+
'balance',
|
|
2021
|
+
'nonce',
|
|
2022
|
+
'code',
|
|
2023
|
+
'state',
|
|
2024
|
+
'stateDiff',
|
|
2025
|
+
]);
|
|
2026
|
+
const bad = (what: string): never => {
|
|
2027
|
+
throw new RpcError(
|
|
2028
|
+
-32602,
|
|
2029
|
+
`invalid state override in ${method}: ${what}.`,
|
|
2030
|
+
);
|
|
2031
|
+
};
|
|
2032
|
+
const quantity = (v: unknown, what: string): bigint => {
|
|
2033
|
+
if (typeof v !== 'string' || !/^0x[0-9a-fA-F]+$/.test(v))
|
|
2034
|
+
return bad(`${what} must be a hex quantity, got ${JSON.stringify(v)}`);
|
|
2035
|
+
const n = BigInt(v);
|
|
2036
|
+
// A 32-byte word is the widest thing any override field names.
|
|
2037
|
+
if (n >= 1n << 256n) return bad(`${what} does not fit in 32 bytes`);
|
|
2038
|
+
return n;
|
|
2039
|
+
};
|
|
2040
|
+
const data = (v: unknown, what: string): Uint8Array => {
|
|
2041
|
+
if (typeof v !== 'string' || !/^0x([0-9a-fA-F]{2})*$/.test(v))
|
|
2042
|
+
return bad(`${what} must be even-length hex, got ${JSON.stringify(v)}`);
|
|
2043
|
+
return hexToBytes(v);
|
|
2044
|
+
};
|
|
2045
|
+
return Object.entries(overrides as Record<string, any>).map(
|
|
2046
|
+
([addr, o]): StateEntry => {
|
|
2047
|
+
if (o == null || typeof o !== 'object' || Array.isArray(o))
|
|
2048
|
+
return bad(`the override for ${addr} must be an object`);
|
|
2049
|
+
for (const k of Object.keys(o))
|
|
2050
|
+
if (!supported.has(k))
|
|
2051
|
+
throw new RpcError(
|
|
2052
|
+
-32602,
|
|
2053
|
+
`state override field not supported by this node: ${addr}.${k} in ${method}. ` +
|
|
2054
|
+
`Supported: balance, nonce, code, state, stateDiff.`,
|
|
2055
|
+
);
|
|
2056
|
+
if (o.state != null && o.stateDiff != null)
|
|
2057
|
+
throw new RpcError(
|
|
2058
|
+
-32602,
|
|
2059
|
+
`account ${addr} has both 'state' and 'stateDiff' in ${method}; pass one.`,
|
|
2060
|
+
);
|
|
2061
|
+
let address: Address;
|
|
2062
|
+
try {
|
|
2063
|
+
address = createAddressFromString(addr);
|
|
2064
|
+
} catch {
|
|
2065
|
+
return bad(`${JSON.stringify(addr)} is not an address`);
|
|
2066
|
+
}
|
|
2067
|
+
const slotsIn = o.state ?? o.stateDiff;
|
|
2068
|
+
if (
|
|
2069
|
+
slotsIn != null &&
|
|
2070
|
+
(typeof slotsIn !== 'object' || Array.isArray(slotsIn))
|
|
2071
|
+
)
|
|
2072
|
+
return bad(
|
|
2073
|
+
`${addr}.${o.state != null ? 'state' : 'stateDiff'} must be an object of slot -> value`,
|
|
2074
|
+
);
|
|
2075
|
+
const slots = Object.entries(slotsIn ?? {}).map(([slot, val]) => {
|
|
2076
|
+
const value = data(val, `${addr} slot ${slot}`);
|
|
2077
|
+
// A slot value is ONE word: a longer one would be silently truncated
|
|
2078
|
+
// to its low 32 bytes by the padding below, so it is refused.
|
|
2079
|
+
if (value.length > 32)
|
|
2080
|
+
return bad(`${addr} slot ${slot} value is longer than 32 bytes`);
|
|
2081
|
+
return {
|
|
2082
|
+
key: setLengthLeft(
|
|
2083
|
+
bigIntToBytes(quantity(slot, `${addr} slot key`)),
|
|
2084
|
+
32,
|
|
2085
|
+
),
|
|
2086
|
+
// SHORTEST FORM, as the EVM itself stores a value: a padded zero
|
|
2087
|
+
// and a missing slot must be the same thing to SSTORE's gas rules.
|
|
2088
|
+
value: unpadBytes(setLengthLeft(value, 32)),
|
|
2089
|
+
};
|
|
2090
|
+
});
|
|
2091
|
+
return {
|
|
2092
|
+
address,
|
|
2093
|
+
balance:
|
|
2094
|
+
o.balance != null
|
|
2095
|
+
? quantity(o.balance, `${addr}.balance`)
|
|
2096
|
+
: undefined,
|
|
2097
|
+
nonce:
|
|
2098
|
+
o.nonce != null ? quantity(o.nonce, `${addr}.nonce`) : undefined,
|
|
2099
|
+
code: o.code != null ? data(o.code, `${addr}.code`) : undefined,
|
|
2100
|
+
clearStorage: o.state != null,
|
|
2101
|
+
slots,
|
|
2102
|
+
absent: false,
|
|
2103
|
+
};
|
|
2104
|
+
},
|
|
2105
|
+
);
|
|
2106
|
+
}
|
|
2107
|
+
|
|
2108
|
+
/**
|
|
2109
|
+
* RUN A READ WITH STATE ENTRIES WRITTEN INTO A CHECKPOINT IT OPENS, and leave
|
|
2110
|
+
* no trace of them. The entries are K's state for a historical call
|
|
2111
|
+
* ({@link historicalEntries}) followed by the caller's state overrides
|
|
2112
|
+
* ({@link parseStateOverrides}), so the overrides land ON TOP of K's state,
|
|
2113
|
+
* as geth applies them on top of the block's. With no entries (a call at the
|
|
2114
|
+
* head without overrides) no level is opened at all.
|
|
2115
|
+
*
|
|
2116
|
+
* ## Why a checkpoint here is safe, given the hazard documented at `serialise`
|
|
2117
|
+
*
|
|
2118
|
+
* The corruption described there needs two executions to interleave on the one
|
|
2119
|
+
* checkpoint stack. This level is opened and closed by the SAME request, inside
|
|
2120
|
+
* the serialisation point, with the read nested strictly inside it: the default
|
|
2121
|
+
* engine's own checkpoint/revert lands on top of this one and pops before this
|
|
2122
|
+
* one does, and revm reads the top of the stack on every access and never
|
|
2123
|
+
* commits. So the stack is `[base, entries, (call)]` and unwinds in order,
|
|
2124
|
+
* whether the read returns, reverts or throws (the `finally`). It is also the
|
|
2125
|
+
* same mechanism `evmCall` already relies on for purity, one level lower. The
|
|
2126
|
+
* derived trie of a `computeStateRoot` node is never involved: change-set recording is
|
|
2127
|
+
* suspended for the read and the level is reverted, so nothing it wrote is
|
|
2128
|
+
* ever named to the trie.
|
|
2129
|
+
*/
|
|
2130
|
+
async function withStateEntries<T>(
|
|
2131
|
+
entries: readonly StateEntry[],
|
|
2132
|
+
read: () => Promise<T>,
|
|
2133
|
+
): Promise<T> {
|
|
2134
|
+
if (entries.length === 0) return read();
|
|
2135
|
+
await sm.checkpoint();
|
|
2136
|
+
try {
|
|
2137
|
+
for (const e of entries) await applyStateEntry(e);
|
|
2138
|
+
return await read();
|
|
2139
|
+
} finally {
|
|
2140
|
+
await sm.revert();
|
|
2141
|
+
}
|
|
2142
|
+
}
|
|
2143
|
+
|
|
2144
|
+
/**
|
|
2145
|
+
* WRITE ONE {@link StateEntry} into the top level. The ONE apply step shared by
|
|
2146
|
+
* the RPC override path and the historical path.
|
|
2147
|
+
*
|
|
2148
|
+
* ORDER MATTERS, and each step re-reads the account so no step writes back a
|
|
2149
|
+
* stale copy over the previous one:
|
|
2150
|
+
*
|
|
2151
|
+
* 1. CODE FIRST. `putCode` rewrites the account's `codeHash`, and CREATES an
|
|
2152
|
+
* empty account when there is none, so it must come before
|
|
2153
|
+
* anything that states the account.
|
|
2154
|
+
* 2. THE ACCOUNT: a whole account replaces what `putCode` left (its `codeHash`
|
|
2155
|
+
* is K's own), and `balance` / `nonce` are a read-modify-write on top.
|
|
2156
|
+
* 3. STORAGE: a clear, then the slots.
|
|
2157
|
+
* 4. ABSENT LAST: an empty code entry, then `deleteAccount`. The account must
|
|
2158
|
+
* read as ABSENT, not as a zero account, and two upstream behaviours stand
|
|
2159
|
+
* in the way: `putCode` creates an account (so the code is emptied BEFORE
|
|
2160
|
+
* the delete), and `deleteAccount` leaves the address's code in the code
|
|
2161
|
+
* map, where `eth_getCode` and the default engine's `EXTCODESIZE` would
|
|
2162
|
+
* still see it (so the code is emptied at all). `deleteAccount` also clears
|
|
2163
|
+
* the storage.
|
|
2164
|
+
*/
|
|
2165
|
+
async function applyStateEntry(e: StateEntry): Promise<void> {
|
|
2166
|
+
if (e.code !== undefined) await sm.putCode(e.address, e.code);
|
|
2167
|
+
if (e.account !== undefined) await sm.putAccount(e.address, e.account);
|
|
2168
|
+
if (e.balance !== undefined || e.nonce !== undefined) {
|
|
2169
|
+
const acc = (await sm.getAccount(e.address)) ?? new Account();
|
|
2170
|
+
if (e.balance !== undefined) acc.balance = e.balance;
|
|
2171
|
+
if (e.nonce !== undefined) acc.nonce = e.nonce;
|
|
2172
|
+
await sm.putAccount(e.address, acc);
|
|
2173
|
+
}
|
|
2174
|
+
if (e.clearStorage) await sm.clearStorage(e.address);
|
|
2175
|
+
for (const {key, value} of e.slots)
|
|
2176
|
+
await sm.putStorage(e.address, key, value);
|
|
2177
|
+
if (e.absent) {
|
|
2178
|
+
await sm.putCode(e.address, new Uint8Array(0));
|
|
2179
|
+
await sm.deleteAccount(e.address);
|
|
2180
|
+
}
|
|
2181
|
+
}
|
|
2182
|
+
|
|
2183
|
+
/**
|
|
2184
|
+
* geth's `eth_call` takes BLOCK overrides as a fourth parameter. This node does
|
|
2185
|
+
* not implement them, so a request carrying them is refused rather than run
|
|
2186
|
+
* against the real block environment.
|
|
2187
|
+
*/
|
|
2188
|
+
function refuseBlockOverrides(blockOverrides: unknown, method: string): void {
|
|
2189
|
+
if (blockOverrides == null) return;
|
|
2190
|
+
throw new RpcError(
|
|
2191
|
+
-32602,
|
|
2192
|
+
`block overrides are not supported by this node (${method}, 4th parameter). ` +
|
|
2193
|
+
`The call would otherwise run against the real block environment.`,
|
|
2194
|
+
);
|
|
2195
|
+
}
|
|
2196
|
+
|
|
1264
2197
|
// ---------- eth_call / estimateGas through the ENGINE's READ half (no signing) ----
|
|
1265
2198
|
/**
|
|
1266
2199
|
* The node's single pure-read helper, and the read half of the engine seam: it
|
|
@@ -1273,9 +2206,18 @@ export async function createNode(options: NodeOptions = {}): Promise<SlimNode> {
|
|
|
1273
2206
|
* `@ethereumjs/evm` engine checkpoints/reverts and resets EIP-2929 warmth
|
|
1274
2207
|
* because that EVM requires it; an engine that cannot commit pays for neither.
|
|
1275
2208
|
* See ./engine.ts.
|
|
2209
|
+
*
|
|
2210
|
+
* `block` is the block the call executes IN: the head's for a call at the
|
|
2211
|
+
* head, block K's stored `Block` for a call pinned to K, so `NUMBER`,
|
|
2212
|
+
* `TIMESTAMP`, `COINBASE`, `PREVRANDAO`, `BASEFEE` and `GASLIMIT` are K's. It
|
|
2213
|
+
* also bounds `BLOCKHASH`: both engines answer zero for any number at or above
|
|
2214
|
+
* the block's own, so a call at K cannot see the hash of K or of anything
|
|
2215
|
+
* after it without any further mechanism (measured on both engines by
|
|
2216
|
+
* `test/historical-call.spec.ts` and its revm twin).
|
|
1276
2217
|
*/
|
|
1277
2218
|
async function evmCall(
|
|
1278
2219
|
params: any,
|
|
2220
|
+
block: Block,
|
|
1279
2221
|
budget?: bigint,
|
|
1280
2222
|
): Promise<ReadCallResult> {
|
|
1281
2223
|
const from = params.from
|
|
@@ -1323,7 +2265,7 @@ export async function createNode(options: NodeOptions = {}): Promise<SlimNode> {
|
|
|
1323
2265
|
data,
|
|
1324
2266
|
value,
|
|
1325
2267
|
gasLimit,
|
|
1326
|
-
block
|
|
2268
|
+
block,
|
|
1327
2269
|
});
|
|
1328
2270
|
}
|
|
1329
2271
|
|
|
@@ -1398,7 +2340,7 @@ export async function createNode(options: NodeOptions = {}): Promise<SlimNode> {
|
|
|
1398
2340
|
* points callers HERE for "the number a transaction needs", so the node must
|
|
1399
2341
|
* never refuse a limit it has just recommended.
|
|
1400
2342
|
*/
|
|
1401
|
-
async function estimateGas(p: any): Promise<bigint> {
|
|
2343
|
+
async function estimateGas(p: any, block: Block): Promise<bigint> {
|
|
1402
2344
|
const dataHex: string = p.data ?? p.input ?? '0x';
|
|
1403
2345
|
const isCreate = !p.to;
|
|
1404
2346
|
const overhead =
|
|
@@ -1410,6 +2352,11 @@ export async function createNode(options: NodeOptions = {}): Promise<SlimNode> {
|
|
|
1410
2352
|
// back a number it will not accept. `gas` on an `eth_estimateGas` request is
|
|
1411
2353
|
// geth's cap on the SEARCH ("do not consider limits above this"), which is a
|
|
1412
2354
|
// different thing from `gas` on an `eth_call` ("let it run this long").
|
|
2355
|
+
//
|
|
2356
|
+
// AT A PAST BLOCK K THE CAP IS STILL THIS ONE, not K's own `gasLimit`,
|
|
2357
|
+
// deliberately (historical-eth-call): the number is for a transaction to be
|
|
2358
|
+
// SUBMITTED now, and what it meets is the limit of the blocks this node
|
|
2359
|
+
// mines, whatever K's was. `block` only decides what the probes execute in.
|
|
1413
2360
|
const supplied = p.gas != null ? BigInt(p.gas) : undefined;
|
|
1414
2361
|
const cap =
|
|
1415
2362
|
supplied === undefined || supplied > minedBlockGasLimit
|
|
@@ -1433,7 +2380,7 @@ export async function createNode(options: NodeOptions = {}): Promise<SlimNode> {
|
|
|
1433
2380
|
);
|
|
1434
2381
|
}
|
|
1435
2382
|
|
|
1436
|
-
const probe = (limit: bigint) => evmCall(p, limit - overhead);
|
|
2383
|
+
const probe = (limit: bigint) => evmCall(p, block, limit - overhead);
|
|
1437
2384
|
|
|
1438
2385
|
// 1) THE UPPER BOUND, run first: a request that cannot succeed with all the
|
|
1439
2386
|
// gas there is cannot succeed at all, and the caller learns that here rather
|
|
@@ -1570,18 +2517,47 @@ export async function createNode(options: NodeOptions = {}): Promise<SlimNode> {
|
|
|
1570
2517
|
return sb ? blockToRpc(sb, Boolean(params[1])) : null;
|
|
1571
2518
|
}
|
|
1572
2519
|
|
|
2520
|
+
// AT THE HEAD OR AT A BLOCK K IN THE `stateHistory` WINDOW, through the one
|
|
2521
|
+
// gate every state read uses (`historicalBlock`). At K the engine runs with
|
|
2522
|
+
// K's stored `Block` on K's state, written into a checkpoint the request
|
|
2523
|
+
// opens and reverts (`historicalEntries`, `withStateEntries`), with the
|
|
2524
|
+
// caller's state overrides applied ON TOP of it.
|
|
1573
2525
|
case 'eth_call': {
|
|
1574
|
-
const
|
|
2526
|
+
const k = historicalBlock(params[1], 'eth_call');
|
|
2527
|
+
refuseBlockOverrides(params[3], 'eth_call');
|
|
2528
|
+
const entries = [
|
|
2529
|
+
...(k === undefined ? [] : historicalEntries(k)),
|
|
2530
|
+
...parseStateOverrides(params[2], 'eth_call'),
|
|
2531
|
+
];
|
|
2532
|
+
const block = blockStore.get(k ?? latestNumber)!.block;
|
|
2533
|
+
const r = await pureRead(() =>
|
|
2534
|
+
withStateEntries(entries, () => evmCall(params[0] ?? {}, block)),
|
|
2535
|
+
);
|
|
1575
2536
|
if (r.error)
|
|
1576
2537
|
throw new RpcError(3, 'execution reverted', hex(r.returnValue));
|
|
1577
2538
|
return hex(r.returnValue);
|
|
1578
2539
|
}
|
|
1579
|
-
case 'eth_estimateGas':
|
|
2540
|
+
case 'eth_estimateGas': {
|
|
1580
2541
|
// THE SMALLEST GAS LIMIT AT WHICH THIS REQUEST SUCCEEDS, found by
|
|
1581
2542
|
// re-executing it. The whole method is {@link estimateGas} above,
|
|
1582
2543
|
// including why it is a search rather than the run-and-measure it used to
|
|
1583
|
-
// be, and where the request's EIP-2930 access list is charged.
|
|
1584
|
-
|
|
2544
|
+
// be, and where the request's EIP-2930 access list is charged. The whole
|
|
2545
|
+
// search runs inside ONE checkpoint holding K's state (and the overrides).
|
|
2546
|
+
const k = historicalBlock(params[1], 'eth_estimateGas');
|
|
2547
|
+
refuseBlockOverrides(params[3], 'eth_estimateGas');
|
|
2548
|
+
const entries = [
|
|
2549
|
+
...(k === undefined ? [] : historicalEntries(k)),
|
|
2550
|
+
...parseStateOverrides(params[2], 'eth_estimateGas'),
|
|
2551
|
+
];
|
|
2552
|
+
const block = blockStore.get(k ?? latestNumber)!.block;
|
|
2553
|
+
return numHex(
|
|
2554
|
+
await pureRead(() =>
|
|
2555
|
+
withStateEntries(entries, () =>
|
|
2556
|
+
estimateGas(params[0] ?? {}, block),
|
|
2557
|
+
),
|
|
2558
|
+
),
|
|
2559
|
+
);
|
|
2560
|
+
}
|
|
1585
2561
|
|
|
1586
2562
|
case 'eth_fillTransaction': {
|
|
1587
2563
|
// Fill the missing fields of a tx request and return {tx, raw} like geth
|
|
@@ -1595,8 +2571,10 @@ export async function createNode(options: NodeOptions = {}): Promise<SlimNode> {
|
|
|
1595
2571
|
: createAddressFromString(
|
|
1596
2572
|
'0x0000000000000000000000000000000000000000',
|
|
1597
2573
|
);
|
|
1598
|
-
|
|
1599
|
-
|
|
2574
|
+
// The PENDING nonce, as geth fills it: the account's nonce advanced past
|
|
2575
|
+
// the transactions it already has queued.
|
|
2576
|
+
const nonce =
|
|
2577
|
+
p.nonce != null ? BigInt(p.nonce) : await pendingNonce(from);
|
|
1600
2578
|
const value = p.value != null ? BigInt(p.value) : 0n;
|
|
1601
2579
|
const dataHex: string = p.data ?? p.input ?? '0x';
|
|
1602
2580
|
const isCreate = !p.to;
|
|
@@ -1614,7 +2592,12 @@ export async function createNode(options: NodeOptions = {}): Promise<SlimNode> {
|
|
|
1614
2592
|
const gas: bigint =
|
|
1615
2593
|
p.gas != null
|
|
1616
2594
|
? BigInt(p.gas)
|
|
1617
|
-
: await
|
|
2595
|
+
: await pureRead(() =>
|
|
2596
|
+
estimateGas(
|
|
2597
|
+
{...p, gas: undefined, accessList: undefined},
|
|
2598
|
+
blockStore.get(latestNumber)!.block,
|
|
2599
|
+
),
|
|
2600
|
+
);
|
|
1618
2601
|
// Fee fields: legacy iff caller passed gasPrice (and no 1559 fields),
|
|
1619
2602
|
// otherwise EIP-1559 with the node's constant fee market.
|
|
1620
2603
|
const isLegacy =
|
|
@@ -1671,29 +2654,60 @@ export async function createNode(options: NodeOptions = {}): Promise<SlimNode> {
|
|
|
1671
2654
|
return {raw: hex(unsigned.serialize()), tx};
|
|
1672
2655
|
}
|
|
1673
2656
|
|
|
2657
|
+
// EVERY state read at a block is gated first: served at the head, served
|
|
2658
|
+
// from the undo log inside the `stateHistory` window (these four only,
|
|
2659
|
+
// through `historicalBlock`), and REFUSED below it, never answered from the
|
|
2660
|
+
// head for a block that is not the head.
|
|
1674
2661
|
case 'eth_getBalance': {
|
|
1675
|
-
const
|
|
2662
|
+
const k = historicalBlock(params[1], 'eth_getBalance');
|
|
2663
|
+
const address = createAddressFromString(params[0]);
|
|
2664
|
+
const acc =
|
|
2665
|
+
k === undefined
|
|
2666
|
+
? await sm.getAccount(address)
|
|
2667
|
+
: await accountAt(k, address);
|
|
1676
2668
|
return numHex(acc?.balance ?? 0n);
|
|
1677
2669
|
}
|
|
1678
2670
|
case 'eth_getTransactionCount': {
|
|
1679
|
-
const
|
|
1680
|
-
|
|
2671
|
+
const k = historicalBlock(params[1], 'eth_getTransactionCount');
|
|
2672
|
+
const address = createAddressFromString(params[0]);
|
|
2673
|
+
if (k !== undefined)
|
|
2674
|
+
return numHex((await accountAt(k, address))?.nonce ?? 0n);
|
|
2675
|
+
// `pending` COUNTS THE QUEUED TRANSACTIONS, the one state read where it
|
|
2676
|
+
// differs from the head: a client picking a nonce asks for `pending`
|
|
2677
|
+
// (viem does by default), and in manual/interval mining two sends
|
|
2678
|
+
// before a mine would otherwise get the SAME nonce, the second then
|
|
2679
|
+
// refused at mine time as a replay.
|
|
2680
|
+
return numHex(
|
|
2681
|
+
params[1] === 'pending'
|
|
2682
|
+
? await pendingNonce(address)
|
|
2683
|
+
: ((await sm.getAccount(address))?.nonce ?? 0n),
|
|
2684
|
+
);
|
|
1681
2685
|
}
|
|
1682
2686
|
case 'eth_getCode': {
|
|
1683
|
-
const
|
|
1684
|
-
|
|
2687
|
+
const k = historicalBlock(params[1], 'eth_getCode');
|
|
2688
|
+
const address = createAddressFromString(params[0]);
|
|
2689
|
+
return hex(
|
|
2690
|
+
k === undefined
|
|
2691
|
+
? await sm.getCode(address)
|
|
2692
|
+
: await codeAt(k, address),
|
|
2693
|
+
);
|
|
1685
2694
|
}
|
|
1686
2695
|
case 'eth_getStorageAt': {
|
|
2696
|
+
const k = historicalBlock(params[2], 'eth_getStorageAt');
|
|
1687
2697
|
const addr = createAddressFromString(params[0]);
|
|
1688
2698
|
const slot = setLengthLeft(bigIntToBytes(BigInt(params[1])), 32);
|
|
1689
|
-
const val =
|
|
2699
|
+
const val =
|
|
2700
|
+
k === undefined
|
|
2701
|
+
? await sm.getStorage(addr, slot)
|
|
2702
|
+
: await storageAt(k, addr, slot);
|
|
1690
2703
|
return hex(setLengthLeft(val, 32));
|
|
1691
2704
|
}
|
|
1692
2705
|
|
|
1693
2706
|
// ---- Runtime state cheats (anvil/hardhat-style; for tests/local tooling) ----
|
|
1694
2707
|
// These MUTATE state directly (no tx). Honest about being non-standard: they
|
|
1695
|
-
// are `evm_*`-namespaced.
|
|
1696
|
-
// getStateRoot()
|
|
2708
|
+
// are `evm_*`-namespaced. They write the flat state only; on a `computeStateRoot` node the
|
|
2709
|
+
// open record names what they wrote, so the next getStateRoot() (or mined
|
|
2710
|
+
// block) rehashes it into the derived trie.
|
|
1697
2711
|
case 'evm_setBalance': {
|
|
1698
2712
|
// params: [address, valueHex]
|
|
1699
2713
|
const addr = createAddressFromString(params[0]);
|
|
@@ -1714,8 +2728,6 @@ export async function createNode(options: NodeOptions = {}): Promise<SlimNode> {
|
|
|
1714
2728
|
// params: [address, codeHex]
|
|
1715
2729
|
const addr = createAddressFromString(params[0]);
|
|
1716
2730
|
await sm.putCode(addr, hexToBytes(params[1]));
|
|
1717
|
-
touchedAccounts.add(addr.toString());
|
|
1718
|
-
await commitIfTrie();
|
|
1719
2731
|
return true;
|
|
1720
2732
|
}
|
|
1721
2733
|
case 'evm_setStorageAt': {
|
|
@@ -1727,8 +2739,6 @@ export async function createNode(options: NodeOptions = {}): Promise<SlimNode> {
|
|
|
1727
2739
|
slot,
|
|
1728
2740
|
setLengthLeft(hexToBytes(params[2]), 32),
|
|
1729
2741
|
);
|
|
1730
|
-
touchedAccounts.add(addr.toString());
|
|
1731
|
-
await commitIfTrie();
|
|
1732
2742
|
return true;
|
|
1733
2743
|
}
|
|
1734
2744
|
case 'evm_setAccount': {
|
|
@@ -1748,7 +2758,6 @@ export async function createNode(options: NodeOptions = {}): Promise<SlimNode> {
|
|
|
1748
2758
|
setLengthLeft(hexToBytes(val as string), 32),
|
|
1749
2759
|
);
|
|
1750
2760
|
}
|
|
1751
|
-
await commitIfTrie();
|
|
1752
2761
|
return true;
|
|
1753
2762
|
}
|
|
1754
2763
|
|
|
@@ -1757,7 +2766,50 @@ export async function createNode(options: NodeOptions = {}): Promise<SlimNode> {
|
|
|
1757
2766
|
case 'eth_maxPriorityFeePerGas':
|
|
1758
2767
|
return numHex(maxPriorityFeePerGas);
|
|
1759
2768
|
case 'eth_feeHistory': {
|
|
1760
|
-
|
|
2769
|
+
// params: [blockCount, newestBlock, rewardPercentiles]. `newestBlock` is
|
|
2770
|
+
// HONOURED (it used to be ignored, and the window always ended at the head),
|
|
2771
|
+
// and the per-block base fee and gas-used ratio are the stored blocks' own.
|
|
2772
|
+
// Only `reward` stays synthetic: this node's priority fee is a constant.
|
|
2773
|
+
const rawCount = params[0] ?? '0x1';
|
|
2774
|
+
const requested =
|
|
2775
|
+
typeof rawCount === 'number' && Number.isSafeInteger(rawCount)
|
|
2776
|
+
? rawCount
|
|
2777
|
+
: typeof rawCount === 'string' && /^0x[0-9a-fA-F]+$/.test(rawCount)
|
|
2778
|
+
? Number(BigInt(rawCount))
|
|
2779
|
+
: NaN;
|
|
2780
|
+
if (!(requested >= 0))
|
|
2781
|
+
throw new RpcError(
|
|
2782
|
+
-32602,
|
|
2783
|
+
`invalid eth_feeHistory blockCount: ${JSON.stringify(rawCount)}. Expected a hex quantity.`,
|
|
2784
|
+
);
|
|
2785
|
+
const newest = resolveBlockTag(params[1] ?? 'latest');
|
|
2786
|
+
if (newest > latestNumber)
|
|
2787
|
+
throw new RpcError(
|
|
2788
|
+
-32000,
|
|
2789
|
+
`request beyond head block: eth_feeHistory newestBlock is ${newest}, but the head of this node is block ${latestNumber}.`,
|
|
2790
|
+
);
|
|
2791
|
+
// geth caps a request at 1024 blocks, and nothing precedes block 0.
|
|
2792
|
+
const count = Math.max(0, Math.min(requested, 1024, newest + 1));
|
|
2793
|
+
const oldest = newest - count + 1;
|
|
2794
|
+
const nextBaseFee = (n: number): string => {
|
|
2795
|
+
const sb = blockStore.get(n);
|
|
2796
|
+
return sb
|
|
2797
|
+
? sb.header.baseFeePerGas
|
|
2798
|
+
: numHex(blockEnv?.baseFeePerGas ?? baseFeePerGas);
|
|
2799
|
+
};
|
|
2800
|
+
const ratio = (n: number): number => {
|
|
2801
|
+
// The header's own `gasUsed` is a documented 0x0 placeholder, so the
|
|
2802
|
+
// block's usage is summed from its receipts, which are real.
|
|
2803
|
+
const sb = blockStore.get(n);
|
|
2804
|
+
if (!sb) return 0;
|
|
2805
|
+
let used = 0n;
|
|
2806
|
+
for (const th of sb.header.transactions) {
|
|
2807
|
+
const r = receipts.get(th);
|
|
2808
|
+
if (r) used += BigInt(r.gasUsed);
|
|
2809
|
+
}
|
|
2810
|
+
const limit = BigInt(sb.header.gasLimit);
|
|
2811
|
+
return limit === 0n ? 0 : Number(used) / Number(limit);
|
|
2812
|
+
};
|
|
1761
2813
|
// `reward` must carry ONE entry per requested percentile, per block. Returning a
|
|
1762
2814
|
// single entry regardless of `rewardPercentiles` breaks any caller that asks for
|
|
1763
2815
|
// several and indexes them: rocketh requests [10, 50, 80] and reads index 1 and 2,
|
|
@@ -1769,11 +2821,20 @@ export async function createNode(options: NodeOptions = {}): Promise<SlimNode> {
|
|
|
1769
2821
|
percentiles.length > 0 ? percentiles : [50]
|
|
1770
2822
|
).map(() => numHex(maxPriorityFeePerGas));
|
|
1771
2823
|
return {
|
|
1772
|
-
|
|
1773
|
-
|
|
1774
|
-
|
|
2824
|
+
// An empty window is geth's empty answer, oldestBlock included.
|
|
2825
|
+
oldestBlock: numHex(count === 0 ? 0 : oldest),
|
|
2826
|
+
// One per block in the window PLUS the block after it, which is the
|
|
2827
|
+
// stored block when there is one and the next block this node will mine
|
|
2828
|
+
// when the window ends at the head.
|
|
2829
|
+
baseFeePerGas:
|
|
2830
|
+
count === 0
|
|
2831
|
+
? []
|
|
2832
|
+
: Array.from({length: count + 1}, (_, i) =>
|
|
2833
|
+
nextBaseFee(oldest + i),
|
|
2834
|
+
),
|
|
2835
|
+
gasUsedRatio: Array.from({length: count}, (_, i) =>
|
|
2836
|
+
ratio(oldest + i),
|
|
1775
2837
|
),
|
|
1776
|
-
gasUsedRatio: Array.from({length: count}, () => 0.5),
|
|
1777
2838
|
reward: Array.from({length: count}, () => rewardPerBlock),
|
|
1778
2839
|
};
|
|
1779
2840
|
}
|
|
@@ -1817,9 +2878,33 @@ export async function createNode(options: NodeOptions = {}): Promise<SlimNode> {
|
|
|
1817
2878
|
// is deferred — see tasks/slim-node-eth-getlogs-index.md. Keep this scan as
|
|
1818
2879
|
// the authoritative semantics if/when an index is added.
|
|
1819
2880
|
const f = params[0] ?? {};
|
|
1820
|
-
|
|
1821
|
-
|
|
1822
|
-
|
|
2881
|
+
// `blockHash` (EIP-234) pins the query to ONE block. It used to be ignored,
|
|
2882
|
+
// which returned the logs of EVERY block. It excludes a range, as in geth,
|
|
2883
|
+
// and a hash no block has is an error rather than an empty result, so a
|
|
2884
|
+
// typo cannot read as "that block emitted nothing".
|
|
2885
|
+
let from: number;
|
|
2886
|
+
let to: number;
|
|
2887
|
+
if (f.blockHash != null) {
|
|
2888
|
+
if (f.fromBlock != null || f.toBlock != null)
|
|
2889
|
+
throw new RpcError(
|
|
2890
|
+
-32602,
|
|
2891
|
+
'invalid eth_getLogs filter: cannot specify both blockHash and fromBlock/toBlock.',
|
|
2892
|
+
);
|
|
2893
|
+
const n = blockByHash.get(String(f.blockHash).toLowerCase());
|
|
2894
|
+
if (n === undefined)
|
|
2895
|
+
throw new RpcError(
|
|
2896
|
+
-32000,
|
|
2897
|
+
`unknown block: eth_getLogs blockHash ${f.blockHash} is not a block on this node.`,
|
|
2898
|
+
);
|
|
2899
|
+
from = n;
|
|
2900
|
+
to = n;
|
|
2901
|
+
} else {
|
|
2902
|
+
// geth's default for an omitted `fromBlock` is `latest`, not 0; this
|
|
2903
|
+
// node has always defaulted to 0 and that is kept (changing it would
|
|
2904
|
+
// silently shrink every existing consumer's results).
|
|
2905
|
+
from = f.fromBlock != null ? resolveBlockTag(f.fromBlock) : 0;
|
|
2906
|
+
to = f.toBlock != null ? resolveBlockTag(f.toBlock) : latestNumber;
|
|
2907
|
+
}
|
|
1823
2908
|
const addrFilter = f.address
|
|
1824
2909
|
? (Array.isArray(f.address) ? f.address : [f.address]).map(
|
|
1825
2910
|
(a: string) => a.toLowerCase(),
|
|
@@ -1885,43 +2970,25 @@ export async function createNode(options: NodeOptions = {}): Promise<SlimNode> {
|
|
|
1885
2970
|
const code: Record<string, string> = {};
|
|
1886
2971
|
const storage: Record<string, Record<string, string>> = {};
|
|
1887
2972
|
|
|
1888
|
-
|
|
1889
|
-
|
|
1890
|
-
|
|
1891
|
-
|
|
1892
|
-
|
|
1893
|
-
|
|
1894
|
-
|
|
1895
|
-
|
|
1896
|
-
|
|
1897
|
-
|
|
1898
|
-
|
|
1899
|
-
|
|
1900
|
-
|
|
1901
|
-
|
|
1902
|
-
|
|
1903
|
-
|
|
1904
|
-
|
|
1905
|
-
|
|
1906
|
-
|
|
1907
|
-
// OVERLAYS, flattened by liveStorage(). Dump both directly (no trie walk).
|
|
1908
|
-
//
|
|
1909
|
-
// THE SERIALISED FORMAT IS NOT THE INTERNAL LAYOUT and must not follow it:
|
|
1910
|
-
// this output is persisted data (IndexedDB, loadState fixtures) with existing
|
|
1911
|
-
// state behind it. It stays `{address: {slot: value}}` in 0x-hex, and
|
|
1912
|
-
// `test/storage-overlay.spec.ts` asserts it byte-identical to a dump taken
|
|
1913
|
-
// from the pre-overlay flat layout.
|
|
1914
|
-
const live = sm as OverlayStorageStateManager;
|
|
1915
|
-
const accMap = live.accountStack[live.accountStack.length - 1];
|
|
1916
|
-
const codeMap = live.codeStack[live.codeStack.length - 1];
|
|
1917
|
-
for (const [addr, acc] of accMap) {
|
|
1918
|
-
if (acc !== undefined) accounts[addr] = hex(acc.serialize());
|
|
1919
|
-
}
|
|
1920
|
-
for (const [addr, c] of codeMap) code[addr] = hex(c);
|
|
1921
|
-
for (const [addr, slots] of live.liveStorage()) {
|
|
1922
|
-
for (const [slot, val] of slots)
|
|
1923
|
-
(storage[addr] ??= {})[slot] = hex(val);
|
|
1924
|
-
}
|
|
2973
|
+
// EVERY NODE DUMPS THE SAME WAY, storage included: the account and code
|
|
2974
|
+
// stacks are plain Maps whose TOP frame is the live set, and storage is
|
|
2975
|
+
// per-account with per-checkpoint OVERLAYS, flattened by liveStorage(). A
|
|
2976
|
+
// `computeStateRoot` node runs on this same flat state, and its derived trie is NOT
|
|
2977
|
+
// serialised: `loadState` rebuilds it from the loaded flat state.
|
|
2978
|
+
//
|
|
2979
|
+
// THE SERIALISED FORMAT IS NOT THE INTERNAL LAYOUT and must not follow it:
|
|
2980
|
+
// this output is persisted data (IndexedDB, loadState fixtures) with existing
|
|
2981
|
+
// state behind it. It stays `{address: {slot: value}}` in 0x-hex, and
|
|
2982
|
+
// `test/storage-overlay.spec.ts` asserts it byte-identical to a dump taken
|
|
2983
|
+
// from the pre-overlay flat layout.
|
|
2984
|
+
const accMap = sm.accountStack[sm.accountStack.length - 1];
|
|
2985
|
+
const codeMap = sm.codeStack[sm.codeStack.length - 1];
|
|
2986
|
+
for (const [addr, acc] of accMap) {
|
|
2987
|
+
if (acc !== undefined) accounts[addr] = hex(acc.serialize());
|
|
2988
|
+
}
|
|
2989
|
+
for (const [addr, c] of codeMap) code[addr] = hex(c);
|
|
2990
|
+
for (const [addr, slots] of sm.liveStorage()) {
|
|
2991
|
+
for (const [slot, val] of slots) (storage[addr] ??= {})[slot] = hex(val);
|
|
1925
2992
|
}
|
|
1926
2993
|
|
|
1927
2994
|
const blocks: SerializedBlock[] = [];
|
|
@@ -1933,16 +3000,86 @@ export async function createNode(options: NodeOptions = {}): Promise<SlimNode> {
|
|
|
1933
3000
|
return {
|
|
1934
3001
|
version: 1,
|
|
1935
3002
|
chainId,
|
|
1936
|
-
stateMode,
|
|
1937
3003
|
accounts,
|
|
1938
3004
|
code,
|
|
1939
3005
|
storage,
|
|
1940
3006
|
blocks,
|
|
1941
3007
|
receipts: Object.fromEntries(receipts),
|
|
1942
3008
|
transactions: Object.fromEntries(transactions),
|
|
3009
|
+
// ADDITIVE: a node without `stateHistory` writes no field at all, so its
|
|
3010
|
+
// dump is exactly what it was before history existed.
|
|
3011
|
+
...(stateHistoryBlocks === undefined ? {} : {history: dumpHistory()}),
|
|
1943
3012
|
};
|
|
1944
3013
|
}
|
|
1945
3014
|
|
|
3015
|
+
/**
|
|
3016
|
+
* THE UNDO LOG AS THE DUMP CARRIES IT: one serialized record per sealed block,
|
|
3017
|
+
* oldest first, with the OPEN record FOLDED INTO THE HEAD'S RECORD (older
|
|
3018
|
+
* entry winning).
|
|
3019
|
+
*
|
|
3020
|
+
* WHY FOLD rather than dump the open record as a field of its own: `loadState`
|
|
3021
|
+
* treats the loaded state as a baseline and CLEARS the open record, so the
|
|
3022
|
+
* writes since the head was mined (`evm_set*` cheats, a batch that threw
|
|
3023
|
+
* mid-block) become part of the loaded head, which reads them live exactly as
|
|
3024
|
+
* it did before the dump. What must not be lost is their prior values, which
|
|
3025
|
+
* every read BELOW the head consults after the sealed records. Folding keeps
|
|
3026
|
+
* the per-key earliest-wins union over `undo[K+1] .. undo[head]` + open
|
|
3027
|
+
* identical for every K below the head: a key the head's record already names
|
|
3028
|
+
* keeps that (older) value; a key it does not name did not change in the head
|
|
3029
|
+
* block, so its value at the end of the block before equals its value at the
|
|
3030
|
+
* end of the head, which is what the open record holds. The alternative, a
|
|
3031
|
+
* separate `open` field restored as the open record, would make the next mined
|
|
3032
|
+
* block after a reload own writes made before it, and would have been a second
|
|
3033
|
+
* new field for the same answers.
|
|
3034
|
+
*
|
|
3035
|
+
* With NO record for the head (the head is where history starts: genesis, or a
|
|
3036
|
+
* loaded head nothing was mined on), no block below the head is servable, and
|
|
3037
|
+
* the open record is dropped with nothing lost.
|
|
3038
|
+
*
|
|
3039
|
+
* Decisions around the format (this fold, the contiguous restore, `[]` vs no
|
|
3040
|
+
* key) are recorded in
|
|
3041
|
+
* `work/notes/observations/state-history-persistence-decisions.md`.
|
|
3042
|
+
*/
|
|
3043
|
+
function dumpHistory(): SerializedHistoryBlock[] {
|
|
3044
|
+
const out = sealed.map((r) => serializeChangeSet(r.number, r.changes));
|
|
3045
|
+
const open = changeSets?.peekChangeSet();
|
|
3046
|
+
const head = out[out.length - 1];
|
|
3047
|
+
if (
|
|
3048
|
+
open !== undefined &&
|
|
3049
|
+
head !== undefined &&
|
|
3050
|
+
head.number === latestNumber
|
|
3051
|
+
)
|
|
3052
|
+
mergeSerializedOlderWins(head, serializeChangeSet(latestNumber, open));
|
|
3053
|
+
return out;
|
|
3054
|
+
}
|
|
3055
|
+
|
|
3056
|
+
/**
|
|
3057
|
+
* RESTORE the undo log from a dump's `history` (see {@link dumpHistory}), after
|
|
3058
|
+
* the rest of the state is loaded and {@link latestNumber} is the dump's head.
|
|
3059
|
+
* Only the CONTIGUOUS run of records ending at the head block is kept, newest
|
|
3060
|
+
* first, stopping at the first gap and at this node's window (a record at or
|
|
3061
|
+
* below `head - N` serves no block in it, so a dump with more history than N
|
|
3062
|
+
* is truncated to N, and one with less serves what it has). A dump the writer
|
|
3063
|
+
* produced has no gap; a hand-edited or foreign one might, and a read below a
|
|
3064
|
+
* gap would silently be answered from a later state, which is the one thing
|
|
3065
|
+
* the history exists not to do. {@link historyFloor} becomes the block before
|
|
3066
|
+
* the oldest record kept, or the head when none is.
|
|
3067
|
+
*/
|
|
3068
|
+
function loadHistory(history: SerializedHistoryBlock[], window: number) {
|
|
3069
|
+
const kept: {number: number; changes: ChangeSet}[] = [];
|
|
3070
|
+
let expected = latestNumber;
|
|
3071
|
+
for (let i = history.length - 1; i >= 0; i--) {
|
|
3072
|
+
const record = history[i];
|
|
3073
|
+
if (record.number !== expected || record.number <= latestNumber - window)
|
|
3074
|
+
break;
|
|
3075
|
+
kept.push({number: record.number, changes: deserializeChangeSet(record)});
|
|
3076
|
+
expected--;
|
|
3077
|
+
}
|
|
3078
|
+
kept.reverse();
|
|
3079
|
+
sealed.push(...kept);
|
|
3080
|
+
historyFloor = kept.length > 0 ? kept[0].number - 1 : latestNumber;
|
|
3081
|
+
}
|
|
3082
|
+
|
|
1946
3083
|
async function loadState(state: SerializedState): Promise<void> {
|
|
1947
3084
|
// Rehydrate accounts/code/storage directly into SimpleStateManager.
|
|
1948
3085
|
for (const [addr, accHex] of Object.entries(state.accounts)) {
|
|
@@ -2028,6 +3165,27 @@ export async function createNode(options: NodeOptions = {}): Promise<SlimNode> {
|
|
|
2028
3165
|
latestNumber = sh.number;
|
|
2029
3166
|
parentHash = block.hash();
|
|
2030
3167
|
}
|
|
3168
|
+
|
|
3169
|
+
// A LOADED STATE IS A BASELINE, NOT HISTORY: the record is cleared, like the
|
|
3170
|
+
// construction baselines at genesis. The dump carries no change set for its
|
|
3171
|
+
// head, so there is none to report for it either.
|
|
3172
|
+
changeSets?.takeChangeSet();
|
|
3173
|
+
headBlockChangeSet = undefined;
|
|
3174
|
+
// ...and the history the node held described the state this load just
|
|
3175
|
+
// replaced, so it goes. What replaces it is the dump's own history, when the
|
|
3176
|
+
// dump carries one and this node keeps any: otherwise history starts again at
|
|
3177
|
+
// the loaded head (an older dump, or a node without `stateHistory`, which
|
|
3178
|
+
// ignores the field).
|
|
3179
|
+
sealed.length = 0;
|
|
3180
|
+
historyFloor = latestNumber;
|
|
3181
|
+
if (stateHistoryBlocks !== undefined && state.history !== undefined)
|
|
3182
|
+
loadHistory(state.history, stateHistoryBlocks);
|
|
3183
|
+
// ...and the derived trie described that replaced state too. It is REBUILT
|
|
3184
|
+
// from the loaded flat state, once, exactly as at construction: the dump
|
|
3185
|
+
// carries no trie (it never has to: the flat state determines it), and a
|
|
3186
|
+
// load is a baseline in no change set, so nothing else would put it there.
|
|
3187
|
+
if (computeStateRoot)
|
|
3188
|
+
derivedTrie = await DerivedStateTrie.build(sm, common);
|
|
2031
3189
|
}
|
|
2032
3190
|
|
|
2033
3191
|
// ---------- persistence auto-load on creation ----------
|
|
@@ -2114,37 +3272,76 @@ export async function createNode(options: NodeOptions = {}): Promise<SlimNode> {
|
|
|
2114
3272
|
// completion, including a `persistence.save()`, and only the timer stops adding
|
|
2115
3273
|
// more. A caller that needs the node quiet should stop issuing requests and await
|
|
2116
3274
|
// the ones it holds.
|
|
2117
|
-
|
|
2118
|
-
|
|
2119
|
-
|
|
3275
|
+
//
|
|
3276
|
+
// `serveOn` is not serialised either, for the reason `dispose` is not: it
|
|
3277
|
+
// touches no state. It only relays `request`, which IS serialised, so a request
|
|
3278
|
+
// arriving over a served port queues exactly like one made directly.
|
|
3279
|
+
const publicRequest = (args: RequestArguments) =>
|
|
3280
|
+
serialise(() => persistingRequest(args));
|
|
3281
|
+
const node: SlimNode = {
|
|
3282
|
+
request: publicRequest,
|
|
2120
3283
|
mine: () => serialise(mineAndPersist),
|
|
2121
3284
|
dumpState: () => serialise(dumpState),
|
|
2122
3285
|
loadState: (state: SerializedState) => serialise(() => loadState(state)),
|
|
2123
|
-
|
|
3286
|
+
computeStateRoot,
|
|
2124
3287
|
senderMode,
|
|
2125
3288
|
// Identity only: the engine object itself stays internal, so the reading is a
|
|
2126
3289
|
// plain value that survives a Worker/comlink boundary unchanged.
|
|
2127
3290
|
engine: {id: engine.id},
|
|
2128
3291
|
async getStateRoot() {
|
|
2129
|
-
if (
|
|
3292
|
+
if (!computeStateRoot) {
|
|
2130
3293
|
throw new RpcError(
|
|
2131
3294
|
-32004,
|
|
2132
|
-
|
|
3295
|
+
'no state root: this node was created without computeStateRoot. Create it with computeStateRoot: true for a real Merkle-Patricia root',
|
|
2133
3296
|
);
|
|
2134
3297
|
}
|
|
2135
|
-
// SERIALISED like the rest, because this one READS BY WRITING:
|
|
2136
|
-
//
|
|
2137
|
-
//
|
|
2138
|
-
//
|
|
3298
|
+
// SERIALISED like the rest, because this one READS BY WRITING: it applies
|
|
3299
|
+
// the open record to the derived trie, which reads the COMMITTED flat state
|
|
3300
|
+
// and refuses to run with a checkpoint open, so a root taken
|
|
3301
|
+
// mid-transaction would report a state no block (or cheat) produced.
|
|
2139
3302
|
return serialise(currentStateRoot);
|
|
2140
3303
|
},
|
|
2141
3304
|
onNewHead(cb) {
|
|
2142
3305
|
headSubs.add(cb);
|
|
2143
3306
|
return () => headSubs.delete(cb);
|
|
2144
3307
|
},
|
|
3308
|
+
// SERVE `request` ON A HANDED PORT, via `@eip-1193/over-port`, so a consumer
|
|
3309
|
+
// on another thread uses this node through `providerOverPort(otherEnd)`.
|
|
3310
|
+
//
|
|
3311
|
+
// IT LIVES ON THE CORE NODE, NOT ONLY IN `webevm/worker-host`, so the two
|
|
3312
|
+
// kinds of node stay interchangeable: `SlimNode` carries it, the one worker
|
|
3313
|
+
// proxy forwards it (its `Required<SlimNode>` annotation demands that), and a
|
|
3314
|
+
// main-thread node answers the same call in place. The price is that the
|
|
3315
|
+
// core imports `@eip-1193/over-port` (about 6 kB of unminified JS, no
|
|
3316
|
+
// runtime dependency of its own), which `webevm/worker-client` and
|
|
3317
|
+
// `webevm/revm` still do not: the client only TRANSFERS the port, and
|
|
3318
|
+
// the consumer brings its own `providerOverPort`.
|
|
3319
|
+
//
|
|
3320
|
+
// Only `request` is served, bound to the SERIALISED entry point above.
|
|
3321
|
+
async serveOn(port: MessagePort) {
|
|
3322
|
+
const server = serveProvider({request: publicRequest}, port);
|
|
3323
|
+
servedPorts.add(server);
|
|
3324
|
+
return {
|
|
3325
|
+
async close() {
|
|
3326
|
+
server.close();
|
|
3327
|
+
servedPorts.delete(server);
|
|
3328
|
+
},
|
|
3329
|
+
};
|
|
3330
|
+
},
|
|
2145
3331
|
async dispose() {
|
|
2146
3332
|
if (intervalTimer) clearInterval(intervalTimer);
|
|
2147
3333
|
headSubs.clear();
|
|
3334
|
+
// A disposed node stops answering on every port it was serving, so a
|
|
3335
|
+
// consumer is not left talking to a node its owner has let go of.
|
|
3336
|
+
for (const server of servedPorts) server.close();
|
|
3337
|
+
servedPorts.clear();
|
|
2148
3338
|
},
|
|
2149
3339
|
};
|
|
3340
|
+
changeSetProbes.set(node, () => ({
|
|
3341
|
+
recording: changeSets !== undefined,
|
|
3342
|
+
sealedBlocks: sealed.map((r) => r.number),
|
|
3343
|
+
headBlock: headBlockChangeSet,
|
|
3344
|
+
open: changeSets?.peekChangeSet(),
|
|
3345
|
+
}));
|
|
3346
|
+
return node;
|
|
2150
3347
|
}
|