webevm 0.7.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.
Files changed (56) hide show
  1. package/README.md +115 -63
  2. package/dist/derived-trie.d.ts +38 -0
  3. package/dist/derived-trie.d.ts.map +1 -0
  4. package/dist/derived-trie.js +187 -0
  5. package/dist/derived-trie.js.map +1 -0
  6. package/dist/engine.d.ts +1 -2
  7. package/dist/engine.d.ts.map +1 -1
  8. package/dist/engine.js +2 -3
  9. package/dist/engine.js.map +1 -1
  10. package/dist/index.d.ts +1 -1
  11. package/dist/index.d.ts.map +1 -1
  12. package/dist/index.js.map +1 -1
  13. package/dist/node.d.ts +45 -0
  14. package/dist/node.d.ts.map +1 -1
  15. package/dist/node.js +1083 -140
  16. package/dist/node.js.map +1 -1
  17. package/dist/revm-state-store.d.ts +5 -0
  18. package/dist/revm-state-store.d.ts.map +1 -1
  19. package/dist/revm-state-store.js +42 -13
  20. package/dist/revm-state-store.js.map +1 -1
  21. package/dist/revm.d.ts.map +1 -1
  22. package/dist/revm.js +5 -19
  23. package/dist/revm.js.map +1 -1
  24. package/dist/state-manager.d.ts +256 -18
  25. package/dist/state-manager.d.ts.map +1 -1
  26. package/dist/state-manager.js +429 -20
  27. package/dist/state-manager.js.map +1 -1
  28. package/dist/storage-keys.d.ts +8 -0
  29. package/dist/storage-keys.d.ts.map +1 -1
  30. package/dist/storage-keys.js +18 -2
  31. package/dist/storage-keys.js.map +1 -1
  32. package/dist/types.d.ts +150 -33
  33. package/dist/types.d.ts.map +1 -1
  34. package/dist/worker-client.d.ts.map +1 -1
  35. package/dist/worker-client.js +20 -12
  36. package/dist/worker-client.js.map +1 -1
  37. package/dist/worker-host.d.ts.map +1 -1
  38. package/dist/worker-host.js +17 -9
  39. package/dist/worker-host.js.map +1 -1
  40. package/dist/worker-rpc-error.d.ts +21 -0
  41. package/dist/worker-rpc-error.d.ts.map +1 -0
  42. package/dist/worker-rpc-error.js +121 -0
  43. package/dist/worker-rpc-error.js.map +1 -0
  44. package/package.json +3 -1
  45. package/src/derived-trie.ts +214 -0
  46. package/src/engine.ts +2 -3
  47. package/src/index.ts +2 -1
  48. package/src/node.ts +1293 -136
  49. package/src/revm-state-store.ts +47 -13
  50. package/src/revm.ts +11 -25
  51. package/src/state-manager.ts +515 -21
  52. package/src/storage-keys.ts +21 -2
  53. package/src/types.ts +152 -34
  54. package/src/worker-client.ts +22 -12
  55. package/src/worker-host.ts +17 -9
  56. 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 {MerkleStateManager} from '@ethereumjs/statemanager';
38
- import {OverlayStorageStateManager} from './state-manager.js';
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.
@@ -90,6 +105,7 @@ import {
90
105
  type SerializedReceipt,
91
106
  type SerializedLog,
92
107
  type SerializedTx,
108
+ type SerializedHistoryBlock,
93
109
  } from './types.js';
94
110
 
95
111
  const ZERO_HASH = '0x' + '00'.repeat(32);
@@ -182,6 +198,96 @@ function hex(b: Uint8Array): string {
182
198
  function numHex(n: number | bigint): string {
183
199
  return bigIntToHex(BigInt(n));
184
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
+ }
185
291
  function txHashOf(tx: TypedTransaction): string {
186
292
  return hex(tx.hash());
187
293
  }
@@ -264,6 +370,32 @@ interface StoredBlock {
264
370
  */
265
371
  function swallow(): void {}
266
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
+
267
399
  /**
268
400
  * ONE SUBMITTED TRANSACTION, as the node carries it from `eth_sendRawTransaction*`
269
401
  * (or the `evm_*As` cheats) to the block it is mined in: the parsed transaction,
@@ -283,15 +415,143 @@ interface SubmittedTx {
283
415
  readonly sender: Address;
284
416
  }
285
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
+
286
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> {
287
540
  const chainId = options.chainId ?? 31337;
288
- const stateMode = options.stateMode ?? 'none';
289
541
  const senderMode: SenderMode = options.senderMode ?? 'recover';
290
542
  const miningConfig = options.miningConfig ?? {type: 'auto'};
291
543
  const baseFeePerGas = options.baseFeePerGas ?? 1_000_000_000n;
292
544
  const gasPrice = options.gasPrice ?? 1_000_000_000n;
293
545
  const maxPriorityFeePerGas = options.maxPriorityFeePerGas ?? 1_000_000_000n;
294
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;
295
555
 
296
556
  const common = new Common({
297
557
  chain: {...Mainnet, chainId, name: 'webevm'},
@@ -299,17 +559,63 @@ export async function createNode(options: NodeOptions = {}): Promise<SlimNode> {
299
559
  customCrypto: {keccak256: (m: Uint8Array) => keccak_256(m)},
300
560
  });
301
561
 
302
- // State backing: SimpleStateManager (no trie, fast — default) or
303
- // MerkleStateManager (real trie + state root — opt-in, slower, conformance-able).
304
- // The 'none' manager is our SUBCLASS: storage is per-account with per-checkpoint
305
- // OVERLAYS (upstream copies the whole flat storage map on every message frame),
306
- // and it implements the `clearStorage(address)` that upstream ships as an empty
307
- // no-op — without which a contract created at an address that already holds
308
- // storage inherits it. See ./state-manager.ts.
309
- const sm =
310
- stateMode === 'trie'
311
- ? new MerkleStateManager()
312
- : new OverlayStorageStateManager();
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
+ }
313
619
 
314
620
  /**
315
621
  * THE SERIALISATION POINT: the tail of a promise chain that every public entry
@@ -421,30 +727,36 @@ export async function createNode(options: NodeOptions = {}): Promise<SlimNode> {
421
727
  return run;
422
728
  }
423
729
 
424
- // Touched-account set for the trie-mode dump (storage is read back via the
425
- // trie's own dumpStorage). We record the addresses each tx touches at the NODE
426
- // level (sender / to / created) rather than monkeypatching the state manager —
427
- // MerkleStateManager's cache/flush path is sensitive to method wrapping.
428
- const touchedAccounts = new Set<string>();
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;
429
737
 
430
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
+ */
431
751
  async function currentStateRoot(): Promise<string> {
432
- if (stateMode !== 'trie') return ZERO_ROOT;
433
- const msm = sm as MerkleStateManager;
434
- await msm.flush(); // write cached trie nodes before reading the root
435
- return hex(await msm.getStateRoot());
436
- }
437
-
438
- // Flush cache into the trie after a direct state mutation (trie mode only) so
439
- // the next getStateRoot() reflects it. No-op in 'none' mode (the Map IS state).
440
- async function commitIfTrie(): Promise<void> {
441
- if (stateMode !== 'trie') return;
442
- await sm.checkpoint();
443
- 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());
444
756
  }
445
757
 
446
758
  // Read-modify-write an account (creating an empty one if absent), used by the
447
- // evm_set* cheat methods. Commits into the trie in trie mode.
759
+ // evm_set* cheat methods.
448
760
  async function mutateAccount(
449
761
  addr: ReturnType<typeof createAddressFromString>,
450
762
  fn: (acc: Account) => void,
@@ -452,13 +764,11 @@ export async function createNode(options: NodeOptions = {}): Promise<SlimNode> {
452
764
  const acc = (await sm.getAccount(addr)) ?? new Account();
453
765
  fn(acc);
454
766
  await sm.putAccount(addr, acc);
455
- touchedAccounts.add(addr.toString());
456
- await commitIfTrie();
457
767
  }
458
768
 
459
769
  // Minimal mock blockchain: `runTx` (inside the default engine) only needs
460
- // getBlock (for BLOCKHASH) + shallowCopy. In 'none' mode we never compute a
461
- // canonical state root.
770
+ // getBlock (for BLOCKHASH) + shallowCopy. Without `computeStateRoot` we never
771
+ // compute a canonical state root.
462
772
  const blockStore = new Map<number, StoredBlock>();
463
773
  const blockByHash = new Map<string, number>();
464
774
  const receipts = new Map<string, SerializedReceipt>();
@@ -505,7 +815,6 @@ export async function createNode(options: NodeOptions = {}): Promise<SlimNode> {
505
815
  await connectEngine(engine, {
506
816
  stateManager: sm,
507
817
  common,
508
- stateMode,
509
818
  // Block hashes for BLOCKHASH, read LIVE (no block exists yet at this point)
510
819
  // and SYNCHRONOUSLY, because an engine answers BLOCKHASH mid-opcode.
511
820
  getBlockHash: (blockNumber: bigint) => {
@@ -529,15 +838,14 @@ export async function createNode(options: NodeOptions = {}): Promise<SlimNode> {
529
838
  let latestNumber = 0;
530
839
  let parentHash = hexToBytes(ZERO_HASH);
531
840
 
532
- // Initial balances FIRST (so the genesis state root, in trie mode, reflects them).
841
+ // Initial balances FIRST (so the genesis state root, when computed, reflects them).
533
842
  if (options.initialBalances) {
534
843
  for (const [addr, bal] of Object.entries(options.initialBalances)) {
535
844
  await sm.putAccount(createAddressFromString(addr), new Account(0n, bal));
536
- touchedAccounts.add(createAddressFromString(addr).toString());
537
845
  }
538
846
  }
539
847
  // Full genesis pre-state (balance/nonce/code/storage) — e.g. a GeneralStateTest
540
- // `pre` section. Applied before block 0 so the trie-mode genesis root reflects
848
+ // `pre` section. Applied before block 0 so a computed genesis root reflects
541
849
  // it and a post-tx getStateRoot() can be compared to the fixture's hash.
542
850
  if (options.initialState) {
543
851
  for (const [addr, acc] of Object.entries(options.initialState)) {
@@ -555,14 +863,13 @@ export async function createNode(options: NodeOptions = {}): Promise<SlimNode> {
555
863
  hexToBytes(val),
556
864
  );
557
865
  }
558
- touchedAccounts.add(address.toString());
559
- }
560
- // Commit the cache into the trie so the genesis root is correct in trie mode.
561
- if (stateMode === 'trie') {
562
- await sm.checkpoint();
563
- await sm.commit();
564
866
  }
565
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);
566
873
 
567
874
  const blockEnv = options.blockEnv;
568
875
 
@@ -595,13 +902,16 @@ export async function createNode(options: NodeOptions = {}): Promise<SlimNode> {
595
902
  // `number`, `timestamp` and `gasLimit` stay the node's own: genesis IS block 0,
596
903
  // and `blockEnv.number` places a MINED block rather than renumbering the chain's
597
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).
598
908
  const genesis = createBlock(
599
909
  {
600
910
  header: {
601
911
  number: 0n,
602
912
  gasLimit: blockGasLimit,
603
913
  baseFeePerGas,
604
- timestamp: BigInt(Math.floor(Date.now() / 1000)),
914
+ timestamp: genesisTimestamp ?? BigInt(Math.floor(Date.now() / 1000)),
605
915
  ...(blockEnv?.coinbase
606
916
  ? {coinbase: createAddressFromString(blockEnv.coinbase)}
607
917
  : {}),
@@ -614,6 +924,9 @@ export async function createNode(options: NodeOptions = {}): Promise<SlimNode> {
614
924
  {common},
615
925
  );
616
926
  storeBlock(genesis, [], [], await currentStateRoot());
927
+ // BASELINES ARE NOT HISTORY: `initialBalances` / `initialState` ARE block 0,
928
+ // not a change to it.
929
+ changeSets?.takeChangeSet();
617
930
 
618
931
  // Pending raw txs awaiting the next mined block (manual/interval modes). Each
619
932
  // carries its SENDER, decided once at parse time by `parseTx`: the node derives
@@ -749,13 +1062,6 @@ export async function createNode(options: NodeOptions = {}): Promise<SlimNode> {
749
1062
  const from = sender.toString();
750
1063
  const to = (tx as any).to ? (tx as any).to.toString() : null;
751
1064
  const created = res.createdAddress ? hex(res.createdAddress) : null;
752
- // Track touched accounts for the trie-mode dump (sender, recipient, created,
753
- // and any account that emitted a log — that set covers what changed).
754
- touchedAccounts.add(from);
755
- if (to) touchedAccounts.add(to);
756
- if (created) touchedAccounts.add(created);
757
- for (const log of res.logs) touchedAccounts.add(hex(log.address));
758
-
759
1065
  const rcptLogs: SerializedLog[] = res.logs.map((log, i) => {
760
1066
  const sl: SerializedLog = {
761
1067
  address: hex(log.address),
@@ -821,13 +1127,45 @@ export async function createNode(options: NodeOptions = {}): Promise<SlimNode> {
821
1127
  }
822
1128
 
823
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.
824
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);
825
1140
  // emit newHeads
826
1141
  for (const cb of headSubs) cb({number: blockNumber, hash: blockHash});
827
1142
 
828
1143
  return {blockNumber, blockHash, txHashes};
829
1144
  }
830
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
+
831
1169
  async function mineBlock() {
832
1170
  const batch = pending.splice(0, pending.length);
833
1171
  return executeAndMine(batch);
@@ -1143,7 +1481,28 @@ export async function createNode(options: NodeOptions = {}): Promise<SlimNode> {
1143
1481
  return r ? receiptToRpc(r) : null;
1144
1482
  }
1145
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
+
1146
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
+ */
1147
1506
  function resolveBlockTag(tag: unknown): number {
1148
1507
  if (
1149
1508
  tag === 'latest' ||
@@ -1154,9 +1513,285 @@ export async function createNode(options: NodeOptions = {}): Promise<SlimNode> {
1154
1513
  )
1155
1514
  return latestNumber;
1156
1515
  if (tag === 'earliest') return 0;
1157
- if (typeof tag === 'string') return Number(BigInt(tag));
1158
- if (typeof tag === 'number') return tag;
1159
- return latestNumber;
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
+ );
1160
1795
  }
1161
1796
 
1162
1797
  /**
@@ -1169,7 +1804,7 @@ export async function createNode(options: NodeOptions = {}): Promise<SlimNode> {
1169
1804
  * a uniform zero, because nothing tells a consumer which side of the trip they
1170
1805
  * are on. Every value below is therefore one a dump carries.
1171
1806
  *
1172
- * `stateRoot` is the one REAL value among the roots (in `'trie'` mode);
1807
+ * `stateRoot` is the one REAL value among the roots (with `computeStateRoot: true`);
1173
1808
  * `sha3Uncles`, `transactionsRoot` and `receiptsRoot` stay honest zero
1174
1809
  * placeholders, named as such in the README, because this node builds no tries
1175
1810
  * over its transactions or receipts. `logsBloom` used to be in that list and is
@@ -1269,6 +1904,296 @@ export async function createNode(options: NodeOptions = {}): Promise<SlimNode> {
1269
1904
  };
1270
1905
  }
1271
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
+
1272
2197
  // ---------- eth_call / estimateGas through the ENGINE's READ half (no signing) ----
1273
2198
  /**
1274
2199
  * The node's single pure-read helper, and the read half of the engine seam: it
@@ -1281,9 +2206,18 @@ export async function createNode(options: NodeOptions = {}): Promise<SlimNode> {
1281
2206
  * `@ethereumjs/evm` engine checkpoints/reverts and resets EIP-2929 warmth
1282
2207
  * because that EVM requires it; an engine that cannot commit pays for neither.
1283
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).
1284
2217
  */
1285
2218
  async function evmCall(
1286
2219
  params: any,
2220
+ block: Block,
1287
2221
  budget?: bigint,
1288
2222
  ): Promise<ReadCallResult> {
1289
2223
  const from = params.from
@@ -1331,7 +2265,7 @@ export async function createNode(options: NodeOptions = {}): Promise<SlimNode> {
1331
2265
  data,
1332
2266
  value,
1333
2267
  gasLimit,
1334
- block: blockStore.get(latestNumber)!.block,
2268
+ block,
1335
2269
  });
1336
2270
  }
1337
2271
 
@@ -1406,7 +2340,7 @@ export async function createNode(options: NodeOptions = {}): Promise<SlimNode> {
1406
2340
  * points callers HERE for "the number a transaction needs", so the node must
1407
2341
  * never refuse a limit it has just recommended.
1408
2342
  */
1409
- async function estimateGas(p: any): Promise<bigint> {
2343
+ async function estimateGas(p: any, block: Block): Promise<bigint> {
1410
2344
  const dataHex: string = p.data ?? p.input ?? '0x';
1411
2345
  const isCreate = !p.to;
1412
2346
  const overhead =
@@ -1418,6 +2352,11 @@ export async function createNode(options: NodeOptions = {}): Promise<SlimNode> {
1418
2352
  // back a number it will not accept. `gas` on an `eth_estimateGas` request is
1419
2353
  // geth's cap on the SEARCH ("do not consider limits above this"), which is a
1420
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.
1421
2360
  const supplied = p.gas != null ? BigInt(p.gas) : undefined;
1422
2361
  const cap =
1423
2362
  supplied === undefined || supplied > minedBlockGasLimit
@@ -1441,7 +2380,7 @@ export async function createNode(options: NodeOptions = {}): Promise<SlimNode> {
1441
2380
  );
1442
2381
  }
1443
2382
 
1444
- const probe = (limit: bigint) => evmCall(p, limit - overhead);
2383
+ const probe = (limit: bigint) => evmCall(p, block, limit - overhead);
1445
2384
 
1446
2385
  // 1) THE UPPER BOUND, run first: a request that cannot succeed with all the
1447
2386
  // gas there is cannot succeed at all, and the caller learns that here rather
@@ -1578,18 +2517,47 @@ export async function createNode(options: NodeOptions = {}): Promise<SlimNode> {
1578
2517
  return sb ? blockToRpc(sb, Boolean(params[1])) : null;
1579
2518
  }
1580
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.
1581
2525
  case 'eth_call': {
1582
- const r = await evmCall(params[0] ?? {});
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
+ );
1583
2536
  if (r.error)
1584
2537
  throw new RpcError(3, 'execution reverted', hex(r.returnValue));
1585
2538
  return hex(r.returnValue);
1586
2539
  }
1587
- case 'eth_estimateGas':
2540
+ case 'eth_estimateGas': {
1588
2541
  // THE SMALLEST GAS LIMIT AT WHICH THIS REQUEST SUCCEEDS, found by
1589
2542
  // re-executing it. The whole method is {@link estimateGas} above,
1590
2543
  // including why it is a search rather than the run-and-measure it used to
1591
- // be, and where the request's EIP-2930 access list is charged.
1592
- return numHex(await estimateGas(params[0] ?? {}));
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
+ }
1593
2561
 
1594
2562
  case 'eth_fillTransaction': {
1595
2563
  // Fill the missing fields of a tx request and return {tx, raw} like geth
@@ -1603,8 +2571,10 @@ export async function createNode(options: NodeOptions = {}): Promise<SlimNode> {
1603
2571
  : createAddressFromString(
1604
2572
  '0x0000000000000000000000000000000000000000',
1605
2573
  );
1606
- const acc = await sm.getAccount(from);
1607
- const nonce = p.nonce != null ? BigInt(p.nonce) : (acc?.nonce ?? 0n);
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);
1608
2578
  const value = p.value != null ? BigInt(p.value) : 0n;
1609
2579
  const dataHex: string = p.data ?? p.input ?? '0x';
1610
2580
  const isCreate = !p.to;
@@ -1622,7 +2592,12 @@ export async function createNode(options: NodeOptions = {}): Promise<SlimNode> {
1622
2592
  const gas: bigint =
1623
2593
  p.gas != null
1624
2594
  ? BigInt(p.gas)
1625
- : await estimateGas({...p, gas: undefined, accessList: undefined});
2595
+ : await pureRead(() =>
2596
+ estimateGas(
2597
+ {...p, gas: undefined, accessList: undefined},
2598
+ blockStore.get(latestNumber)!.block,
2599
+ ),
2600
+ );
1626
2601
  // Fee fields: legacy iff caller passed gasPrice (and no 1559 fields),
1627
2602
  // otherwise EIP-1559 with the node's constant fee market.
1628
2603
  const isLegacy =
@@ -1679,29 +2654,60 @@ export async function createNode(options: NodeOptions = {}): Promise<SlimNode> {
1679
2654
  return {raw: hex(unsigned.serialize()), tx};
1680
2655
  }
1681
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.
1682
2661
  case 'eth_getBalance': {
1683
- const acc = await sm.getAccount(createAddressFromString(params[0]));
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);
1684
2668
  return numHex(acc?.balance ?? 0n);
1685
2669
  }
1686
2670
  case 'eth_getTransactionCount': {
1687
- const acc = await sm.getAccount(createAddressFromString(params[0]));
1688
- return numHex(acc?.nonce ?? 0n);
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
+ );
1689
2685
  }
1690
2686
  case 'eth_getCode': {
1691
- const code = await sm.getCode(createAddressFromString(params[0]));
1692
- return hex(code);
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
+ );
1693
2694
  }
1694
2695
  case 'eth_getStorageAt': {
2696
+ const k = historicalBlock(params[2], 'eth_getStorageAt');
1695
2697
  const addr = createAddressFromString(params[0]);
1696
2698
  const slot = setLengthLeft(bigIntToBytes(BigInt(params[1])), 32);
1697
- const val = await sm.getStorage(addr, slot);
2699
+ const val =
2700
+ k === undefined
2701
+ ? await sm.getStorage(addr, slot)
2702
+ : await storageAt(k, addr, slot);
1698
2703
  return hex(setLengthLeft(val, 32));
1699
2704
  }
1700
2705
 
1701
2706
  // ---- Runtime state cheats (anvil/hardhat-style; for tests/local tooling) ----
1702
2707
  // These MUTATE state directly (no tx). Honest about being non-standard: they
1703
- // are `evm_*`-namespaced. In trie mode each commits into the trie so the next
1704
- // getStateRoot() reflects the change.
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.
1705
2711
  case 'evm_setBalance': {
1706
2712
  // params: [address, valueHex]
1707
2713
  const addr = createAddressFromString(params[0]);
@@ -1722,8 +2728,6 @@ export async function createNode(options: NodeOptions = {}): Promise<SlimNode> {
1722
2728
  // params: [address, codeHex]
1723
2729
  const addr = createAddressFromString(params[0]);
1724
2730
  await sm.putCode(addr, hexToBytes(params[1]));
1725
- touchedAccounts.add(addr.toString());
1726
- await commitIfTrie();
1727
2731
  return true;
1728
2732
  }
1729
2733
  case 'evm_setStorageAt': {
@@ -1735,8 +2739,6 @@ export async function createNode(options: NodeOptions = {}): Promise<SlimNode> {
1735
2739
  slot,
1736
2740
  setLengthLeft(hexToBytes(params[2]), 32),
1737
2741
  );
1738
- touchedAccounts.add(addr.toString());
1739
- await commitIfTrie();
1740
2742
  return true;
1741
2743
  }
1742
2744
  case 'evm_setAccount': {
@@ -1756,7 +2758,6 @@ export async function createNode(options: NodeOptions = {}): Promise<SlimNode> {
1756
2758
  setLengthLeft(hexToBytes(val as string), 32),
1757
2759
  );
1758
2760
  }
1759
- await commitIfTrie();
1760
2761
  return true;
1761
2762
  }
1762
2763
 
@@ -1765,7 +2766,50 @@ export async function createNode(options: NodeOptions = {}): Promise<SlimNode> {
1765
2766
  case 'eth_maxPriorityFeePerGas':
1766
2767
  return numHex(maxPriorityFeePerGas);
1767
2768
  case 'eth_feeHistory': {
1768
- const count = Number(BigInt(params[0] ?? '0x1'));
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
+ };
1769
2813
  // `reward` must carry ONE entry per requested percentile, per block. Returning a
1770
2814
  // single entry regardless of `rewardPercentiles` breaks any caller that asks for
1771
2815
  // several and indexes them: rocketh requests [10, 50, 80] and reads index 1 and 2,
@@ -1777,11 +2821,20 @@ export async function createNode(options: NodeOptions = {}): Promise<SlimNode> {
1777
2821
  percentiles.length > 0 ? percentiles : [50]
1778
2822
  ).map(() => numHex(maxPriorityFeePerGas));
1779
2823
  return {
1780
- oldestBlock: numHex(Math.max(0, latestNumber - count + 1)),
1781
- baseFeePerGas: Array.from({length: count + 1}, () =>
1782
- numHex(baseFeePerGas),
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),
1783
2837
  ),
1784
- gasUsedRatio: Array.from({length: count}, () => 0.5),
1785
2838
  reward: Array.from({length: count}, () => rewardPerBlock),
1786
2839
  };
1787
2840
  }
@@ -1825,9 +2878,33 @@ export async function createNode(options: NodeOptions = {}): Promise<SlimNode> {
1825
2878
  // is deferred — see tasks/slim-node-eth-getlogs-index.md. Keep this scan as
1826
2879
  // the authoritative semantics if/when an index is added.
1827
2880
  const f = params[0] ?? {};
1828
- const from = f.fromBlock != null ? resolveBlockTag(f.fromBlock) : 0;
1829
- const to =
1830
- f.toBlock != null ? resolveBlockTag(f.toBlock) : latestNumber;
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
+ }
1831
2908
  const addrFilter = f.address
1832
2909
  ? (Array.isArray(f.address) ? f.address : [f.address]).map(
1833
2910
  (a: string) => a.toLowerCase(),
@@ -1893,43 +2970,25 @@ export async function createNode(options: NodeOptions = {}): Promise<SlimNode> {
1893
2970
  const code: Record<string, string> = {};
1894
2971
  const storage: Record<string, Record<string, string>> = {};
1895
2972
 
1896
- if (stateMode === 'trie') {
1897
- // Trie mode dumps accounts + code via the touched-account set. NOTE: it does
1898
- // NOT dump contract STORAGE — the EVM journals storage on an internal
1899
- // shallowCopy of the state manager that bypasses any interception, and the
1900
- // trie's own dumpStorage exposes only keccak-HASHED slot keys (not the raw
1901
- // slots loadState needs). Full-storage persistence is a 'none'-mode feature
1902
- // (where the live Map IS the committed state). Trie mode is for the REAL
1903
- // state root (conformance block roots), not for IndexedDB persist.
1904
- for (const addr of touchedAccounts) {
1905
- const address = createAddressFromString(addr);
1906
- const acc = await sm.getAccount(address);
1907
- if (acc === undefined) continue;
1908
- accounts[addr] = hex(acc.serialize());
1909
- const c = await sm.getCode(address);
1910
- if (c.length > 0) code[addr] = hex(c);
1911
- }
1912
- } else {
1913
- // 'none' mode: the account and code stacks are still plain Maps whose TOP
1914
- // frame is the live set, and storage is per-account with per-checkpoint
1915
- // OVERLAYS, flattened by liveStorage(). Dump both directly (no trie walk).
1916
- //
1917
- // THE SERIALISED FORMAT IS NOT THE INTERNAL LAYOUT and must not follow it:
1918
- // this output is persisted data (IndexedDB, loadState fixtures) with existing
1919
- // state behind it. It stays `{address: {slot: value}}` in 0x-hex, and
1920
- // `test/storage-overlay.spec.ts` asserts it byte-identical to a dump taken
1921
- // from the pre-overlay flat layout.
1922
- const live = sm as OverlayStorageStateManager;
1923
- const accMap = live.accountStack[live.accountStack.length - 1];
1924
- const codeMap = live.codeStack[live.codeStack.length - 1];
1925
- for (const [addr, acc] of accMap) {
1926
- if (acc !== undefined) accounts[addr] = hex(acc.serialize());
1927
- }
1928
- for (const [addr, c] of codeMap) code[addr] = hex(c);
1929
- for (const [addr, slots] of live.liveStorage()) {
1930
- for (const [slot, val] of slots)
1931
- (storage[addr] ??= {})[slot] = hex(val);
1932
- }
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);
1933
2992
  }
1934
2993
 
1935
2994
  const blocks: SerializedBlock[] = [];
@@ -1941,16 +3000,86 @@ export async function createNode(options: NodeOptions = {}): Promise<SlimNode> {
1941
3000
  return {
1942
3001
  version: 1,
1943
3002
  chainId,
1944
- stateMode,
1945
3003
  accounts,
1946
3004
  code,
1947
3005
  storage,
1948
3006
  blocks,
1949
3007
  receipts: Object.fromEntries(receipts),
1950
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()}),
1951
3012
  };
1952
3013
  }
1953
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
+
1954
3083
  async function loadState(state: SerializedState): Promise<void> {
1955
3084
  // Rehydrate accounts/code/storage directly into SimpleStateManager.
1956
3085
  for (const [addr, accHex] of Object.entries(state.accounts)) {
@@ -2036,6 +3165,27 @@ export async function createNode(options: NodeOptions = {}): Promise<SlimNode> {
2036
3165
  latestNumber = sh.number;
2037
3166
  parentHash = block.hash();
2038
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);
2039
3189
  }
2040
3190
 
2041
3191
  // ---------- persistence auto-load on creation ----------
@@ -2128,27 +3278,27 @@ export async function createNode(options: NodeOptions = {}): Promise<SlimNode> {
2128
3278
  // arriving over a served port queues exactly like one made directly.
2129
3279
  const publicRequest = (args: RequestArguments) =>
2130
3280
  serialise(() => persistingRequest(args));
2131
- return {
3281
+ const node: SlimNode = {
2132
3282
  request: publicRequest,
2133
3283
  mine: () => serialise(mineAndPersist),
2134
3284
  dumpState: () => serialise(dumpState),
2135
3285
  loadState: (state: SerializedState) => serialise(() => loadState(state)),
2136
- stateMode,
3286
+ computeStateRoot,
2137
3287
  senderMode,
2138
3288
  // Identity only: the engine object itself stays internal, so the reading is a
2139
3289
  // plain value that survives a Worker/comlink boundary unchanged.
2140
3290
  engine: {id: engine.id},
2141
3291
  async getStateRoot() {
2142
- if (stateMode !== 'trie') {
3292
+ if (!computeStateRoot) {
2143
3293
  throw new RpcError(
2144
3294
  -32004,
2145
- "no state root in 'none' mode — create the node with stateMode:'trie' for a real Merkle-Patricia root",
3295
+ 'no state root: this node was created without computeStateRoot. Create it with computeStateRoot: true for a real Merkle-Patricia root',
2146
3296
  );
2147
3297
  }
2148
- // SERIALISED like the rest, because this one READS BY WRITING: in trie mode
2149
- // `currentStateRoot` flushes the state manager's cache into the trie, so a
2150
- // root taken mid-transaction would both report an uncommitted state and
2151
- // push it into the trie on the way.
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.
2152
3302
  return serialise(currentStateRoot);
2153
3303
  },
2154
3304
  onNewHead(cb) {
@@ -2187,4 +3337,11 @@ export async function createNode(options: NodeOptions = {}): Promise<SlimNode> {
2187
3337
  servedPorts.clear();
2188
3338
  },
2189
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;
2190
3347
  }