webevm 0.7.0 → 0.9.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 +1108 -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 +1318 -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/dist/node.js 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,12 +37,13 @@
34
37
  * `docs/adr/0012-one-request-at-a-time-the-node-serialises-its-whole-public-surface.md`.
35
38
  */
36
39
  import { createVM } 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 { OverlayStorageStateManager, copyAccount, mergeChangeSetOlderWins, } from './state-manager.js';
42
+ import { packAddressKey, packSlotKey, unpackAddressKey, unpackSlotKey, } from './storage-keys.js';
39
43
  import { Common, Mainnet, Hardfork } from '@ethereumjs/common';
40
44
  import { createBlock } from '@ethereumjs/block';
41
45
  import { createTxFromRLP, createTx } from '@ethereumjs/tx';
42
- import { createAddressFromString, createAccountFromRLP, Account, hexToBytes as hexToBytesStrict, bytesToHex, bytesToBigInt, bigIntToHex, setLengthLeft, bigIntToBytes, } from '@ethereumjs/util';
46
+ import { createAddressFromString, createAccountFromRLP, Account, hexToBytes as hexToBytesStrict, bytesToHex, bytesToBigInt, bigIntToHex, setLengthLeft, bigIntToBytes, unpadBytes, } from '@ethereumjs/util';
43
47
  // `@ethereumjs/util`'s hexToBytes is typed to require a `0x${string}` literal.
44
48
  // Our hex comes from runtime sources (RPC params, serialized state) typed as
45
49
  // plain `string`, so wrap it once with a runtime 0x-guard rather than casting at
@@ -149,6 +153,82 @@ function hex(b) {
149
153
  function numHex(n) {
150
154
  return bigIntToHex(BigInt(n));
151
155
  }
156
+ /**
157
+ * A change set as the dump carries it ({@link SerializedHistoryBlock}): keys in
158
+ * `0x` hex, values hex, `null` for absent. Reads `changes` and does not keep a
159
+ * reference to anything in it.
160
+ */
161
+ function serializeChangeSet(number, changes) {
162
+ const accounts = {};
163
+ for (const [key, account] of changes.accounts)
164
+ accounts[key] = account === undefined ? null : hex(account.serialize());
165
+ const code = {};
166
+ for (const [key, value] of changes.code)
167
+ code[key] = value === undefined ? null : hex(value);
168
+ const storage = {};
169
+ for (const [addressKey, slots] of changes.storage) {
170
+ const out = {};
171
+ for (const [slotKey, value] of slots)
172
+ out[unpackSlotKey(slotKey)] = value === undefined ? null : hex(value);
173
+ storage[unpackAddressKey(addressKey)] = out;
174
+ }
175
+ return {
176
+ number,
177
+ accounts,
178
+ code,
179
+ storage,
180
+ storageCleared: [...changes.storageCleared].map(unpackAddressKey),
181
+ };
182
+ }
183
+ /**
184
+ * Merge serialized record `newer` INTO `older`, the OLDER entry winning for a
185
+ * key both name: the serialized twin of `mergeChangeSetOlderWins`, used by
186
+ * `dumpState` to fold the open record into the head's record (see there).
187
+ */
188
+ function mergeSerializedOlderWins(older, newer) {
189
+ for (const [key, value] of Object.entries(newer.accounts))
190
+ if (!(key in older.accounts))
191
+ older.accounts[key] = value;
192
+ for (const [key, value] of Object.entries(newer.code))
193
+ if (!(key in older.code))
194
+ older.code[key] = value;
195
+ for (const [address, slots] of Object.entries(newer.storage)) {
196
+ const target = (older.storage[address] ??= {});
197
+ for (const [slot, value] of Object.entries(slots))
198
+ if (!(slot in target))
199
+ target[slot] = value;
200
+ }
201
+ for (const address of newer.storageCleared)
202
+ if (!older.storageCleared.includes(address))
203
+ older.storageCleared.push(address);
204
+ }
205
+ /**
206
+ * The inverse of {@link serializeChangeSet}: keys back to the representation's
207
+ * own (`address.toString()` for accounts and code, PACKED for storage), `null`
208
+ * back to `undefined`. Addresses are lowercased, so a hand-edited checksummed
209
+ * key still names the account the maps hold.
210
+ */
211
+ function deserializeChangeSet(record) {
212
+ const accounts = new Map();
213
+ for (const [key, value] of Object.entries(record.accounts))
214
+ accounts.set(key.toLowerCase(), value === null ? undefined : createAccountFromRLP(hexToBytes(value)));
215
+ const code = new Map();
216
+ for (const [key, value] of Object.entries(record.code))
217
+ code.set(key.toLowerCase(), value === null ? undefined : hexToBytes(value));
218
+ const storage = new Map();
219
+ for (const [address, slots] of Object.entries(record.storage)) {
220
+ const out = new Map();
221
+ for (const [slot, value] of Object.entries(slots))
222
+ out.set(packSlotKey(hexToBytes(slot)), value === null ? undefined : hexToBytes(value));
223
+ storage.set(packAddressKey(hexToBytes(address)), out);
224
+ }
225
+ return {
226
+ accounts,
227
+ code,
228
+ storage,
229
+ storageCleared: new Set(record.storageCleared.map((a) => packAddressKey(hexToBytes(a)))),
230
+ };
231
+ }
152
232
  function txHashOf(tx) {
153
233
  return hex(tx.hash());
154
234
  }
@@ -212,30 +292,151 @@ function upfrontCost(tx) {
212
292
  * been delivered to whoever awaited that request.
213
293
  */
214
294
  function swallow() { }
295
+ const changeSetProbes = new WeakMap();
296
+ /**
297
+ * TEST-ONLY ACCESSOR for a node's change sets, named as such: it hands out the
298
+ * node's own record objects (read-only by contract) so a test can hold them
299
+ * against the state it reads through the public surface. Not part of
300
+ * `SlimNode` (the worker proxy's shape test would otherwise demand it cross a
301
+ * comlink boundary) and not exported from `src/index.ts`.
302
+ */
303
+ export function changeSetsForTests(node) {
304
+ const probe = changeSetProbes.get(node);
305
+ if (probe === undefined)
306
+ throw new Error('webevm: changeSetsForTests() was handed an object createNode() did not return.');
307
+ return probe();
308
+ }
309
+ /**
310
+ * Validate {@link NodeOptions.stateHistory}: ABSENT (`undefined`) is off and
311
+ * returns `undefined`; `{blocks: N}` with N a positive safe integer returns N;
312
+ * anything else throws.
313
+ *
314
+ * It composes with `computeStateRoot: true`: every node runs on the same flat state,
315
+ * which is what the undo log records, and the derived trie only reads it. (It
316
+ * was refused there while trie mode, as a root-computing node was then
317
+ * called, ran on `MerkleStateManager`, which recorded
318
+ * nothing; `trie-derived-from-the-flat-state` lifted that.)
319
+ */
320
+ function validateStateHistory(options) {
321
+ if (options.stateHistory === undefined)
322
+ return undefined;
323
+ const value = options.stateHistory;
324
+ const blocks = typeof value === 'object' && value !== null
325
+ ? value.blocks
326
+ : undefined;
327
+ if (typeof blocks !== 'number' ||
328
+ !Number.isSafeInteger(blocks) ||
329
+ blocks <= 0)
330
+ throw new Error(`webevm: stateHistory must be {blocks: N} with N a positive safe integer ` +
331
+ `(how many blocks below the head stay readable), or absent for no history; ` +
332
+ `got ${describeOption(value)}.`);
333
+ return blocks;
334
+ }
335
+ /**
336
+ * Validate {@link NodeOptions.genesisTimestamp}: ABSENT (`undefined`) returns
337
+ * `undefined` (block 0 takes the wall clock); a non-negative bigint returns
338
+ * itself; anything else throws. A NUMBER is refused rather than converted, even
339
+ * a whole one: every other timestamp the node takes (`blockEnv.timestamp`) is a
340
+ * bigint, and accepting both would make the option mean two types.
341
+ */
342
+ function validateGenesisTimestamp(options) {
343
+ if (options.genesisTimestamp === undefined)
344
+ return undefined;
345
+ const value = options.genesisTimestamp;
346
+ if (typeof value !== 'bigint' || value < 0n)
347
+ throw new Error(`webevm: genesisTimestamp must be a non-negative bigint (seconds since the ` +
348
+ `epoch, e.g. 1700000000n), or absent for the wall clock; got ` +
349
+ `${typeof value === 'bigint' ? `${value}n` : `${describeOption(value)} (${typeof value})`}.`);
350
+ return value;
351
+ }
352
+ function describeOption(value) {
353
+ try {
354
+ const s = JSON.stringify(value);
355
+ return s === undefined ? String(value) : s;
356
+ }
357
+ catch {
358
+ return String(value);
359
+ }
360
+ }
215
361
  export async function createNode(options = {}) {
362
+ return createNodeWithInternals(options, {});
363
+ }
364
+ /** {@link createNode} with {@link NodeInternals}; internal, see there. */
365
+ export async function createNodeWithInternals(options, internals) {
216
366
  const chainId = options.chainId ?? 31337;
217
- const stateMode = options.stateMode ?? 'none';
218
367
  const senderMode = options.senderMode ?? 'recover';
219
368
  const miningConfig = options.miningConfig ?? { type: 'auto' };
220
369
  const baseFeePerGas = options.baseFeePerGas ?? 1000000000n;
221
370
  const gasPrice = options.gasPrice ?? 1000000000n;
222
371
  const maxPriorityFeePerGas = options.maxPriorityFeePerGas ?? 1000000000n;
223
372
  const blockGasLimit = options.blockGasLimit ?? 30000000n;
373
+ // Validated before anything is built, so a bad option costs nothing.
374
+ const stateHistoryBlocks = validateStateHistory(options);
375
+ const genesisTimestamp = validateGenesisTimestamp(options);
376
+ /**
377
+ * `computeStateRoot: true`: derive a trie from the flat state, report its
378
+ * root. Only `true` turns it on (see {@link NodeOptions.computeStateRoot}).
379
+ */
380
+ const computeStateRoot = options.computeStateRoot === true;
224
381
  const common = new Common({
225
382
  chain: { ...Mainnet, chainId, name: 'webevm' },
226
383
  hardfork: Hardfork.Cancun,
227
384
  customCrypto: { keccak256: (m) => keccak_256(m) },
228
385
  });
229
- // State backing: SimpleStateManager (no trie, fast — default) or
230
- // MerkleStateManager (real trie + state root — opt-in, slower, conformance-able).
231
- // The 'none' manager is our SUBCLASS: storage is per-account with per-checkpoint
232
- // OVERLAYS (upstream copies the whole flat storage map on every message frame),
233
- // and it implements the `clearStorage(address)` that upstream ships as an empty
234
- // no-op — without which a contract created at an address that already holds
235
- // storage inherits it. See ./state-manager.ts.
236
- const sm = stateMode === 'trie'
237
- ? new MerkleStateManager()
238
- : new OverlayStorageStateManager();
386
+ // State backing, ONE for every node: our `SimpleStateManager` SUBCLASS, plain
387
+ // Maps with no trie. Storage is per-account with per-checkpoint OVERLAYS
388
+ // (upstream copies the whole flat storage map on every message frame), and it
389
+ // implements the `clearStorage(address)` that upstream ships as an empty no-op,
390
+ // without which a contract created at an address that already holds storage
391
+ // inherits it. See ./state-manager.ts. `computeStateRoot` does NOT change it:
392
+ // it adds a trie DERIVED from this state (`derivedTrie` below), which no engine
393
+ // ever reads.
394
+ const sm = new OverlayStorageStateManager();
395
+ // THE CHANGE SET, switched on BEFORE the first write so that the construction
396
+ // baselines below really go through the recorder and really get cleared when
397
+ // genesis is stored (a baseline left in the record would reconstruct EMPTY
398
+ // state at genesis). `changeSets` is the manager when recording, else
399
+ // `undefined`, so every use below is one optional call.
400
+ let changeSets;
401
+ if (internals.recordChangeSets ||
402
+ stateHistoryBlocks !== undefined ||
403
+ computeStateRoot) {
404
+ sm.enableChangeSets();
405
+ changeSets = sm;
406
+ }
407
+ /**
408
+ * The change set of the HEAD block, taken from the open record when the block
409
+ * was mined. With `stateHistory` it is also the newest entry of {@link sealed};
410
+ * this slot is what the test probe reports as `headBlock`, with or without it.
411
+ */
412
+ let headBlockChangeSet;
413
+ /**
414
+ * THE UNDO LOG (`stateHistory`): the SEALED change set of each of the last N
415
+ * mined blocks, oldest first. `sealed[i].changes` holds, for every key block
416
+ * `sealed[i].number` changed, the value it had at the END of the block before.
417
+ * Appended in `executeAndMine`, evicted there (anything at or below `head - N`
418
+ * serves no block in the window), and cleared by `loadState`. Always empty on a
419
+ * node without the option. See {@link historicalValue} for how a read uses it,
420
+ * and `docs/adr/0013-bounded-state-history-is-an-undo-log-over-the-flat-state.md`
421
+ * for why it is an undo log.
422
+ */
423
+ const sealed = [];
424
+ /**
425
+ * The oldest block whose state the undo log can reconstruct, WHATEVER the
426
+ * window: genesis at construction, the loaded head after `loadState` (a loaded
427
+ * dump is a baseline and carries no history). The window's oldest servable
428
+ * block is the later of this and `head - N`.
429
+ */
430
+ let historyFloor = 0;
431
+ /**
432
+ * Run a PURE READ (`eth_call`, `eth_estimateGas`, `eth_fillTransaction`'s
433
+ * estimate, with or without state overrides) with change-set recording
434
+ * suspended: every level it opens is reverted, so recording there is wasted
435
+ * work. A cost rule; see `withChangeSetsSuspended` in ./state-manager.ts.
436
+ */
437
+ function pureRead(read) {
438
+ return changeSets ? changeSets.withChangeSetsSuspended(read) : read();
439
+ }
239
440
  /**
240
441
  * THE SERIALISATION POINT: the tail of a promise chain that every public entry
241
442
  * point queues its work onto, so this node runs exactly ONE piece of work at a
@@ -345,39 +546,44 @@ export async function createNode(options = {}) {
345
546
  tail = run.then(swallow, swallow);
346
547
  return run;
347
548
  }
348
- // Touched-account set for the trie-mode dump (storage is read back via the
349
- // trie's own dumpStorage). We record the addresses each tx touches at the NODE
350
- // level (sender / to / created) rather than monkeypatching the state manager —
351
- // MerkleStateManager's cache/flush path is sensitive to method wrapping.
352
- const touchedAccounts = new Set();
549
+ /**
550
+ * THE DERIVED TRIE (`computeStateRoot: true` only; `undefined`, and never created,
551
+ * otherwise). Built from the WHOLE flat state once the genesis baselines are
552
+ * written and again at the end of `loadState`, and kept current from change
553
+ * sets by {@link currentStateRoot}. See ./derived-trie.ts.
554
+ */
555
+ let derivedTrie;
353
556
  const ZERO_ROOT = ZERO_HASH;
557
+ /**
558
+ * The state root of the flat state AS IT STANDS: the derived trie brought up
559
+ * to date with the OPEN RECORD (every key written since the head was mined)
560
+ * and its root read. Called at the end of each mined block, BEFORE the block's
561
+ * record is taken (so the block's own writes are in it), and by
562
+ * `getStateRoot()` for cheats since the head. Applying the open record twice
563
+ * (a `getStateRoot()`, then the block that includes the same writes) is
564
+ * harmless: the record only NAMES keys, and their values are re-read from the
565
+ * flat state each time. Both callers run inside the serialisation point with
566
+ * no checkpoint open, which the trie insists on. Without
567
+ * `computeStateRoot`: the zero placeholder, and no trie work at all.
568
+ */
354
569
  async function currentStateRoot() {
355
- if (stateMode !== 'trie')
570
+ if (derivedTrie === undefined)
356
571
  return ZERO_ROOT;
357
- const msm = sm;
358
- await msm.flush(); // write cached trie nodes before reading the root
359
- return hex(await msm.getStateRoot());
360
- }
361
- // Flush cache into the trie after a direct state mutation (trie mode only) so
362
- // the next getStateRoot() reflects it. No-op in 'none' mode (the Map IS state).
363
- async function commitIfTrie() {
364
- if (stateMode !== 'trie')
365
- return;
366
- await sm.checkpoint();
367
- await sm.commit();
572
+ const open = sm.peekChangeSet();
573
+ if (open !== undefined)
574
+ await derivedTrie.apply(open);
575
+ return hex(derivedTrie.root());
368
576
  }
369
577
  // Read-modify-write an account (creating an empty one if absent), used by the
370
- // evm_set* cheat methods. Commits into the trie in trie mode.
578
+ // evm_set* cheat methods.
371
579
  async function mutateAccount(addr, fn) {
372
580
  const acc = (await sm.getAccount(addr)) ?? new Account();
373
581
  fn(acc);
374
582
  await sm.putAccount(addr, acc);
375
- touchedAccounts.add(addr.toString());
376
- await commitIfTrie();
377
583
  }
378
584
  // Minimal mock blockchain: `runTx` (inside the default engine) only needs
379
- // getBlock (for BLOCKHASH) + shallowCopy. In 'none' mode we never compute a
380
- // canonical state root.
585
+ // getBlock (for BLOCKHASH) + shallowCopy. Without `computeStateRoot` we never
586
+ // compute a canonical state root.
381
587
  const blockStore = new Map();
382
588
  const blockByHash = new Map();
383
589
  const receipts = new Map();
@@ -422,7 +628,6 @@ export async function createNode(options = {}) {
422
628
  await connectEngine(engine, {
423
629
  stateManager: sm,
424
630
  common,
425
- stateMode,
426
631
  // Block hashes for BLOCKHASH, read LIVE (no block exists yet at this point)
427
632
  // and SYNCHRONOUSLY, because an engine answers BLOCKHASH mid-opcode.
428
633
  getBlockHash: (blockNumber) => {
@@ -442,15 +647,14 @@ export async function createNode(options = {}) {
442
647
  : undefined;
443
648
  let latestNumber = 0;
444
649
  let parentHash = hexToBytes(ZERO_HASH);
445
- // Initial balances FIRST (so the genesis state root, in trie mode, reflects them).
650
+ // Initial balances FIRST (so the genesis state root, when computed, reflects them).
446
651
  if (options.initialBalances) {
447
652
  for (const [addr, bal] of Object.entries(options.initialBalances)) {
448
653
  await sm.putAccount(createAddressFromString(addr), new Account(0n, bal));
449
- touchedAccounts.add(createAddressFromString(addr).toString());
450
654
  }
451
655
  }
452
656
  // Full genesis pre-state (balance/nonce/code/storage) — e.g. a GeneralStateTest
453
- // `pre` section. Applied before block 0 so the trie-mode genesis root reflects
657
+ // `pre` section. Applied before block 0 so a computed genesis root reflects
454
658
  // it and a post-tx getStateRoot() can be compared to the fixture's hash.
455
659
  if (options.initialState) {
456
660
  for (const [addr, acc] of Object.entries(options.initialState)) {
@@ -461,14 +665,14 @@ export async function createNode(options = {}) {
461
665
  for (const [slot, val] of Object.entries(acc.storage ?? {})) {
462
666
  await sm.putStorage(address, setLengthLeft(bigIntToBytes(BigInt(slot)), 32), hexToBytes(val));
463
667
  }
464
- touchedAccounts.add(address.toString());
465
- }
466
- // Commit the cache into the trie so the genesis root is correct in trie mode.
467
- if (stateMode === 'trie') {
468
- await sm.checkpoint();
469
- await sm.commit();
470
668
  }
471
669
  }
670
+ // THE TRIE IS BUILT FROM THE FULL FLAT STATE, here, once: the baselines above
671
+ // are block 0 and deliberately in NO change set (the record is cleared just
672
+ // after genesis is stored), so a trie kept from change sets alone would omit
673
+ // them and every genesis-dependent root would be wrong.
674
+ if (computeStateRoot)
675
+ derivedTrie = await DerivedStateTrie.build(sm, common);
472
676
  const blockEnv = options.blockEnv;
473
677
  /**
474
678
  * THE GAS LIMIT OF EVERY BLOCK THIS NODE BUILDS, named once so that the block
@@ -498,12 +702,15 @@ export async function createNode(options = {}) {
498
702
  // `number`, `timestamp` and `gasLimit` stay the node's own: genesis IS block 0,
499
703
  // and `blockEnv.number` places a MINED block rather than renumbering the chain's
500
704
  // genesis (`minedBlockGasLimit` above is likewise the MINED block's limit).
705
+ // Its timestamp is `genesisTimestamp` when given, else the wall clock: a
706
+ // separate option rather than `blockEnv.timestamp`, which pins every MINED
707
+ // block to one value and so cannot also be block 0's (see the option's JSDoc).
501
708
  const genesis = createBlock({
502
709
  header: {
503
710
  number: 0n,
504
711
  gasLimit: blockGasLimit,
505
712
  baseFeePerGas,
506
- timestamp: BigInt(Math.floor(Date.now() / 1000)),
713
+ timestamp: genesisTimestamp ?? BigInt(Math.floor(Date.now() / 1000)),
507
714
  ...(blockEnv?.coinbase
508
715
  ? { coinbase: createAddressFromString(blockEnv.coinbase) }
509
716
  : {}),
@@ -514,6 +721,9 @@ export async function createNode(options = {}) {
514
721
  },
515
722
  }, { common });
516
723
  storeBlock(genesis, [], [], await currentStateRoot());
724
+ // BASELINES ARE NOT HISTORY: `initialBalances` / `initialState` ARE block 0,
725
+ // not a change to it.
726
+ changeSets?.takeChangeSet();
517
727
  // Pending raw txs awaiting the next mined block (manual/interval modes). Each
518
728
  // carries its SENDER, decided once at parse time by `parseTx`: the node derives
519
729
  // the sender (or is told it), never the engine.
@@ -632,15 +842,6 @@ export async function createNode(options = {}) {
632
842
  const from = sender.toString();
633
843
  const to = tx.to ? tx.to.toString() : null;
634
844
  const created = res.createdAddress ? hex(res.createdAddress) : null;
635
- // Track touched accounts for the trie-mode dump (sender, recipient, created,
636
- // and any account that emitted a log — that set covers what changed).
637
- touchedAccounts.add(from);
638
- if (to)
639
- touchedAccounts.add(to);
640
- if (created)
641
- touchedAccounts.add(created);
642
- for (const log of res.logs)
643
- touchedAccounts.add(hex(log.address));
644
845
  const rcptLogs = res.logs.map((log, i) => {
645
846
  const sl = {
646
847
  address: hex(log.address),
@@ -700,12 +901,47 @@ export async function createNode(options = {}) {
700
901
  txIndex++;
701
902
  }
702
903
  allLogs.push(...blockLogs);
904
+ // The root BEFORE the record is taken below: `currentStateRoot` applies the
905
+ // open record, which at this point is exactly what this block changed.
703
906
  storeBlock(block, txHashes, blockLogs, await currentStateRoot());
907
+ // THE BLOCK'S CHANGE SET is everything written since the previous head was
908
+ // mined: this block's transactions AND any `evm_set*` cheat issued between
909
+ // the two blocks, which belongs to this block (the first one that can see
910
+ // it). Taken with no checkpoint open, which `takeChangeSet` insists on.
911
+ if (changeSets)
912
+ headBlockChangeSet = changeSets.takeChangeSet();
913
+ if (stateHistoryBlocks !== undefined && headBlockChangeSet !== undefined)
914
+ sealBlock(blockNumber, headBlockChangeSet, stateHistoryBlocks);
704
915
  // emit newHeads
705
916
  for (const cb of headSubs)
706
917
  cb({ number: blockNumber, hash: blockHash });
707
918
  return { blockNumber, blockHash, txHashes };
708
919
  }
920
+ /**
921
+ * SEAL the change set just taken as block `number`'s undo record, and EVICT
922
+ * every record at or below `head - N`: a read at the oldest servable block
923
+ * `head - N` needs the records of blocks `head - N + 1 .. head` and no other,
924
+ * so exactly N are kept.
925
+ *
926
+ * A block MINED UNDER THE SAME NUMBER as the newest record (a fixed
927
+ * `blockEnv.number` does that: every mined block replaces the head) is MERGED
928
+ * into it, the older entry winning, as a checkpoint commit merges: the merged
929
+ * record then holds each key's value from before the first of them, which is
930
+ * what a read below that number needs, and the log stays bounded.
931
+ */
932
+ function sealBlock(number, changes, window) {
933
+ const newest = sealed[sealed.length - 1];
934
+ if (newest !== undefined && newest.number === number)
935
+ mergeChangeSetOlderWins(newest.changes, changes);
936
+ else
937
+ sealed.push({ number, changes });
938
+ const oldestKept = latestNumber - window;
939
+ let drop = 0;
940
+ while (drop < sealed.length && sealed[drop].number <= oldestKept)
941
+ drop++;
942
+ if (drop > 0)
943
+ sealed.splice(0, drop);
944
+ }
709
945
  async function mineBlock() {
710
946
  const batch = pending.splice(0, pending.length);
711
947
  return executeAndMine(batch);
@@ -996,7 +1232,29 @@ export async function createNode(options = {}) {
996
1232
  const r = receipts.get(h);
997
1233
  return r ? receiptToRpc(r) : null;
998
1234
  }
1235
+ /**
1236
+ * The nonce `address` sends with next: its account nonce, advanced past each
1237
+ * CONTIGUOUS queued transaction of its own (manual/interval mining; the queue
1238
+ * is always empty under auto mining). Contiguous, as geth counts it: a queued
1239
+ * nonce beyond a gap does not advance it, because that transaction will not
1240
+ * execute until the gap is filled.
1241
+ */
1242
+ async function pendingNonce(address) {
1243
+ let n = (await sm.getAccount(address))?.nonce ?? 0n;
1244
+ const queued = new Set();
1245
+ for (const q of pending)
1246
+ if (q.sender.equals(address))
1247
+ queued.add(q.tx.nonce);
1248
+ while (queued.has(n))
1249
+ n++;
1250
+ return n;
1251
+ }
999
1252
  // ---------- block lookup helpers ----------
1253
+ /**
1254
+ * A block TAG or NUMBER as a block number. A string that is neither is a
1255
+ * -32602, not a JavaScript `SyntaxError` escaping from `BigInt` (which is what
1256
+ * `eth_getBlockByNumber('yesterday')` used to throw), and not a silent `latest`.
1257
+ */
1000
1258
  function resolveBlockTag(tag) {
1001
1259
  if (tag === 'latest' ||
1002
1260
  tag === 'pending' ||
@@ -1006,11 +1264,231 @@ export async function createNode(options = {}) {
1006
1264
  return latestNumber;
1007
1265
  if (tag === 'earliest')
1008
1266
  return 0;
1009
- if (typeof tag === 'string')
1267
+ // A 32-byte hex string is a block HASH, which this parameter does not take:
1268
+ // read as a number it would be ~1e76 and look like "far above the head".
1269
+ if (typeof tag === 'string' && /^0x[0-9a-fA-F]{64}$/.test(tag))
1270
+ throw new RpcError(-32602, `invalid block parameter: ${tag} is a block hash, and this parameter takes a block number or tag.`);
1271
+ if (typeof tag === 'string' && /^0x[0-9a-fA-F]+$/.test(tag))
1010
1272
  return Number(BigInt(tag));
1011
- if (typeof tag === 'number')
1273
+ if (typeof tag === 'number' && Number.isSafeInteger(tag) && tag >= 0)
1012
1274
  return tag;
1013
- return latestNumber;
1275
+ throw new RpcError(-32602, `invalid block parameter: ${JSON.stringify(tag)}. Expected a block tag (latest, pending, safe, finalized, earliest) or a hex block number.`);
1276
+ }
1277
+ /**
1278
+ * THE GATE FOR EVERY STATE READ AT A BLOCK: `undefined` when the parameter
1279
+ * names the head (read or execute live), the block number K when it names a
1280
+ * block in the `stateHistory` window (served from the undo log), and a refusal
1281
+ * otherwise. Every method that reads STATE at a block (`eth_call`,
1282
+ * `eth_estimateGas`, `eth_getBalance`, `eth_getCode`, `eth_getStorageAt`,
1283
+ * `eth_getTransactionCount`) calls this with its block parameter BEFORE
1284
+ * touching state.
1285
+ *
1286
+ * ## Why this exists
1287
+ *
1288
+ * Those methods used to ignore the block parameter entirely and answer from the
1289
+ * live state, while `eth_getLogs` and `eth_getBlockByNumber` honoured theirs. A
1290
+ * client that pins a PAIR of reads to one block so they describe the same moment
1291
+ * (logs up to block N, and a view call at block N) got logs as of N and storage
1292
+ * as of the head: a position whose move it could not find, with no error. That is
1293
+ * a different answer from every other node, delivered silently, which is the one
1294
+ * outcome that is not acceptable. (This gate was `requireHeadState` while the
1295
+ * node served the head only.)
1296
+ *
1297
+ * ## What the node holds: the head, plus an OPT-IN undo log
1298
+ *
1299
+ * This node holds ONE state: the live one. On every node the
1300
+ * state manager's checkpoint/overlay stack is the journal of the execution in
1301
+ * flight, not a history of blocks. Without `stateHistory` nothing of block N
1302
+ * survives the mining of N+1, and the honest answer to "what was the state at
1303
+ * N" is "not available here".
1304
+ *
1305
+ * WITH `stateHistory: {blocks: N}` the node keeps, per mined block, the value
1306
+ * every key the block changed had BEFORE it (the sealed change sets, an undo
1307
+ * log over the one flat state; see
1308
+ * `docs/adr/0013-bounded-state-history-is-an-undo-log-over-the-flat-state.md`).
1309
+ * The four POINT reads at a block K in the window are answered from it by
1310
+ * {@link historicalValue} with no state manager involvement and no checkpoint.
1311
+ * `eth_call` and `eth_estimateGas` at K EXECUTE: the same records, as K's
1312
+ * state, are written into a checkpoint the request opens and reverts
1313
+ * ({@link historicalEntries}, {@link withStateEntries}), and the engine runs
1314
+ * with K's stored `Block`. That SUPERSEDES the reasoning this comment used to
1315
+ * give for refusing all history (retained copies, or swapping a shared state
1316
+ * manager's root under a read): an undo log needs neither.
1317
+ *
1318
+ * ## What is served, and what is refused
1319
+ *
1320
+ * - SERVED: `latest`, `pending`, `safe`, `finalized`, an absent parameter, and a
1321
+ * number, hash or EIP-1898 `{blockNumber}` / `{blockHash}` object that names
1322
+ * the HEAD block. Pinning to the block you just read is the common case and it
1323
+ * is almost always the head, so it must keep working. (`pending` is the head
1324
+ * because in `manual`/`interval` mining the queued transactions have not
1325
+ * executed yet and this node has no speculative pending state; the one
1326
+ * exception is `eth_getTransactionCount`, which adds the sender's queued
1327
+ * transactions so that two sends before a mine get two nonces. `safe` and
1328
+ * `finalized` are the head because a local chain has no reorgs.) `earliest`
1329
+ * is served while block 0 is the head or in the window.
1330
+ * - SERVED FROM HISTORY: any block from {@link oldestServableBlock} up to the
1331
+ * head, named any of those ways, by all six methods.
1332
+ * - REFUSED, -32000 `historical state not available`: a block below the oldest
1333
+ * servable one (with no `stateHistory`, that is every block below the head),
1334
+ * with a message naming the oldest servable block and the `stateHistory`
1335
+ * option. The same code geth uses for pruned state (`missing trie node`).
1336
+ * - REFUSED, -32000 `header not found`: a number above the head, a hash no
1337
+ * block has (geth's exact wording for both), or a number between two blocks
1338
+ * of a chain whose numbering jumps.
1339
+ * - REFUSED, -32602: a parameter that is not a block at all.
1340
+ *
1341
+ * THE STATE AT THE HEAD INCLUDES `evm_set*` CHEATS applied since it was mined,
1342
+ * exactly as `latest` always has: those mutate live state without a block. They
1343
+ * are in the OPEN record, which a read below the head consults after the sealed
1344
+ * ones, so they are visible at the head and at no older block.
1345
+ */
1346
+ function historicalBlock(blockParam, method) {
1347
+ const number = pinnedBlockNumber(blockParam, method);
1348
+ if (number === latestNumber)
1349
+ return undefined;
1350
+ const oldest = oldestServableBlock();
1351
+ if (number < oldest)
1352
+ throw beyondHistory(method, number, oldest);
1353
+ // Numbering has gaps only under a `blockEnv.number` that jumps; a number
1354
+ // between two blocks names no block, and is not answered as if it did.
1355
+ if (!blockStore.has(number))
1356
+ throw new RpcError(-32000, `header not found: ${method} was pinned to block ${number}, which this node never mined.`);
1357
+ return number;
1358
+ }
1359
+ /**
1360
+ * The oldest block whose state a point read can be answered at: the head
1361
+ * without `stateHistory`; with it, `head - N`, but never below where the undo
1362
+ * log starts ({@link historyFloor}: genesis, or the head a state was loaded at).
1363
+ */
1364
+ function oldestServableBlock() {
1365
+ if (stateHistoryBlocks === undefined)
1366
+ return latestNumber;
1367
+ return Math.max(historyFloor, latestNumber - stateHistoryBlocks, 0);
1368
+ }
1369
+ /** The refusal for a block older than {@link oldestServableBlock}. */
1370
+ function beyondHistory(method, number, oldest) {
1371
+ const holds = stateHistoryBlocks === undefined
1372
+ ? `but the oldest block whose state this node holds is block ${oldest}, its head, because it was created without stateHistory. ` +
1373
+ `Create the node with stateHistory: {blocks: N} to keep the last N blocks readable, or pin to the head (or 'latest') instead.`
1374
+ : `but the oldest block whose state this node holds is block ${oldest} (head ${latestNumber}, stateHistory: {blocks: ${stateHistoryBlocks}}` +
1375
+ (oldest > latestNumber - stateHistoryBlocks
1376
+ ? `; history starts at block ${oldest}, where this node's state was loaded`
1377
+ : '') +
1378
+ `). Pin to a block in that window, or widen stateHistory.blocks.`;
1379
+ return new RpcError(-32000, `historical state not available: ${method} was pinned to block ${number}, ${holds} ` +
1380
+ `It REFUSES rather than answer from a later state than the one you asked for; logs and blocks remain queryable at any height.`);
1381
+ }
1382
+ /**
1383
+ * THE BLOCK NUMBER a state read's block parameter names (the head for a tag),
1384
+ * refusing what is not a block (-32602), a hash no block has and a number above
1385
+ * the head (-32000 `header not found`).
1386
+ */
1387
+ function pinnedBlockNumber(blockParam, method) {
1388
+ if (blockParam == null ||
1389
+ blockParam === 'latest' ||
1390
+ blockParam === 'pending' ||
1391
+ blockParam === 'safe' ||
1392
+ blockParam === 'finalized')
1393
+ return latestNumber;
1394
+ let number;
1395
+ let hash;
1396
+ if (blockParam === 'earliest')
1397
+ number = 0;
1398
+ else if (typeof blockParam === 'number')
1399
+ number = blockParam;
1400
+ else if (typeof blockParam === 'string') {
1401
+ // A 32-byte hex string is a block HASH (geth accepts it bare, EIP-1898).
1402
+ if (/^0x[0-9a-fA-F]{64}$/.test(blockParam))
1403
+ hash = blockParam;
1404
+ else if (/^0x[0-9a-fA-F]+$/.test(blockParam))
1405
+ number = Number(BigInt(blockParam));
1406
+ }
1407
+ else if (typeof blockParam === 'object') {
1408
+ const o = blockParam;
1409
+ // EIP-1898: exactly one of the two. geth refuses both at once, and so
1410
+ // does this node rather than silently preferring one.
1411
+ if (o.blockHash != null && o.blockNumber != null)
1412
+ throw new RpcError(-32602, `invalid block parameter for ${method}: pass blockHash or blockNumber, not both.`);
1413
+ if (typeof o.blockHash === 'string')
1414
+ hash = o.blockHash;
1415
+ else if (typeof o.blockNumber === 'string') {
1416
+ if (o.blockNumber === 'earliest')
1417
+ number = 0;
1418
+ else if (/^0x[0-9a-fA-F]+$/.test(o.blockNumber))
1419
+ number = Number(BigInt(o.blockNumber));
1420
+ else
1421
+ return pinnedBlockNumber(o.blockNumber, method);
1422
+ }
1423
+ }
1424
+ if (hash !== undefined) {
1425
+ const n = blockByHash.get(hash.toLowerCase());
1426
+ if (n === undefined)
1427
+ throw new RpcError(-32000, `header not found: ${method} was pinned to block hash ${hash}, which no block on this node has.`);
1428
+ number = n;
1429
+ }
1430
+ // A well-formed number too large to be a safe integer names a block far
1431
+ // above the head: that is "not found", not "not a block".
1432
+ if (number !== undefined && number > Number.MAX_SAFE_INTEGER)
1433
+ number = Number.MAX_SAFE_INTEGER;
1434
+ if (number === undefined || !Number.isSafeInteger(number) || number < 0)
1435
+ throw new RpcError(-32602, `invalid block parameter for ${method}: ${JSON.stringify(blockParam)}. ` +
1436
+ `Expected a block tag (latest, pending, safe, finalized, earliest), a hex block number, a block hash, or {blockNumber}/{blockHash}.`);
1437
+ if (number > latestNumber)
1438
+ throw new RpcError(-32000, `header not found: ${method} was pinned to block ${number}, but the head of this node is block ${latestNumber}.`);
1439
+ return number;
1440
+ }
1441
+ /**
1442
+ * ONE KEY'S VALUE AT THE END OF BLOCK `k` (below the head, in the window): the
1443
+ * FIRST record naming the key among the sealed records of blocks `k+1 .. head`
1444
+ * (oldest first), then the OPEN record (writes since the head was mined:
1445
+ * cheats, and a batch that threw mid-block), else the live value. Each record
1446
+ * holds a key's value from before its block, so the first one after `k` that
1447
+ * names the key holds its value at the end of `k`; a key no record names has
1448
+ * not changed since `k`, so its live value is its value then.
1449
+ *
1450
+ * A PURE LOOKUP: no state manager write, no checkpoint, and nothing an engine
1451
+ * sees, which is why it is safe on both engines and needs nothing from the
1452
+ * serialisation point beyond running inside it (the dispatcher does). `pick`
1453
+ * returns `{value}` when the record names the key, `undefined` when it does
1454
+ * not; `live` reads the head.
1455
+ */
1456
+ async function historicalValue(k, pick, live) {
1457
+ for (const record of sealed) {
1458
+ if (record.number <= k)
1459
+ continue;
1460
+ const hit = pick(record.changes);
1461
+ if (hit !== undefined)
1462
+ return hit.value;
1463
+ }
1464
+ const open = changeSets?.peekChangeSet();
1465
+ const hit = open && pick(open);
1466
+ if (hit !== undefined)
1467
+ return hit.value;
1468
+ return live();
1469
+ }
1470
+ /** The account at `k`, `undefined` if absent: see {@link historicalValue}. */
1471
+ function accountAt(k, address) {
1472
+ const key = address.toString();
1473
+ return historicalValue(k, (cs) => cs.accounts.has(key) ? { value: cs.accounts.get(key) } : undefined, () => sm.getAccount(address));
1474
+ }
1475
+ /** The code at `k`, empty if none: see {@link historicalValue}. */
1476
+ function codeAt(k, address) {
1477
+ const key = address.toString();
1478
+ return historicalValue(k, (cs) => cs.code.has(key)
1479
+ ? { value: cs.code.get(key) ?? new Uint8Array(0) }
1480
+ : undefined, () => sm.getCode(address));
1481
+ }
1482
+ /** One storage slot at `k`, empty if unset: see {@link historicalValue}. */
1483
+ function storageAt(k, address, slot) {
1484
+ const addressKey = packAddressKey(address.bytes);
1485
+ const slotKey = packSlotKey(slot);
1486
+ return historicalValue(k, (cs) => {
1487
+ const slots = cs.storage.get(addressKey);
1488
+ return slots?.has(slotKey)
1489
+ ? { value: slots.get(slotKey) ?? new Uint8Array(0) }
1490
+ : undefined;
1491
+ }, () => sm.getStorage(address, slot));
1014
1492
  }
1015
1493
  /**
1016
1494
  * The stored block, as `eth_getBlockByNumber` / `eth_getBlockByHash` report it.
@@ -1022,7 +1500,7 @@ export async function createNode(options = {}) {
1022
1500
  * a uniform zero, because nothing tells a consumer which side of the trip they
1023
1501
  * are on. Every value below is therefore one a dump carries.
1024
1502
  *
1025
- * `stateRoot` is the one REAL value among the roots (in `'trie'` mode);
1503
+ * `stateRoot` is the one REAL value among the roots (with `computeStateRoot: true`);
1026
1504
  * `sha3Uncles`, `transactionsRoot` and `receiptsRoot` stay honest zero
1027
1505
  * placeholders, named as such in the README, because this node builds no tries
1028
1506
  * over its transactions or receipts. `logsBloom` used to be in that list and is
@@ -1105,19 +1583,317 @@ export async function createNode(options = {}) {
1105
1583
  logsBloom: r.logsBloom,
1106
1584
  };
1107
1585
  }
1586
+ /**
1587
+ * One log as RPC serves it, from `eth_getLogs` and inside receipts alike.
1588
+ *
1589
+ * `blockTimestamp` (execution-apis#639, served by geth, reth, besu, erigon,
1590
+ * anvil and Hardhat's EDR; some indexers refuse a log without it) is LOOKED UP
1591
+ * from the log's block here rather than stored on {@link SerializedLog}. The
1592
+ * block's header is the one source of truth for the timestamp, so a lookup
1593
+ * cannot disagree with `eth_getBlockBy*`, and the block store is only ever
1594
+ * cleared to be refilled by `loadState`. Storing it
1595
+ * instead would duplicate the value on every log, change the persisted dump's
1596
+ * shape, and leave a dump written without it needing a fallback to this very
1597
+ * lookup anyway; as it is, every dump, old or new, answers with it.
1598
+ */
1108
1599
  function logToRpc(l) {
1600
+ const stored = blockStore.get(l.blockNumber);
1601
+ if (!stored) {
1602
+ // NOT unreachable today: when a manual/interval batch has a transaction
1603
+ // refused mid-batch, the receipts of the transactions before it are
1604
+ // already recorded, naming a block that is never stored (hole 2 of the
1605
+ // backlog task `two-remaining-holes-in-the-submit-and-mine-refusal-path`).
1606
+ // Such a receipt names a block that does not exist, so it is refused
1607
+ // loudly here rather than served without a timestamp, or with whatever
1608
+ // block later reuses the number. Closing that hole makes this unreachable.
1609
+ throw new Error(`log names block ${l.blockNumber}, which the node lacks`);
1610
+ }
1109
1611
  return {
1110
1612
  address: l.address,
1111
1613
  topics: l.topics,
1112
1614
  data: l.data,
1113
1615
  blockNumber: numHex(l.blockNumber),
1114
1616
  blockHash: l.blockHash,
1617
+ blockTimestamp: numHex(stored.header.timestamp),
1115
1618
  transactionHash: l.transactionHash,
1116
1619
  transactionIndex: numHex(l.transactionIndex),
1117
1620
  logIndex: numHex(l.logIndex),
1118
1621
  removed: false,
1119
1622
  };
1120
1623
  }
1624
+ /**
1625
+ * K'S STATE, AS STATE ENTRIES: for every key the undo log names after block
1626
+ * `k` (the sealed records of blocks `k+1 .. head`, then the OPEN record), the
1627
+ * value it had at the end of `k`, the earliest record winning per key, exactly
1628
+ * the lookup {@link historicalValue} makes for one key. A key no record names
1629
+ * has not changed since `k`, so the live state already holds it and it needs
1630
+ * no entry. O(keys changed since `k`), which is the cost of a historical
1631
+ * `eth_call` on top of the call itself.
1632
+ *
1633
+ * Written into a checkpoint by {@link withStateEntries}, so the engine
1634
+ * executes on K's state with no copy of state and no root swapped. An account
1635
+ * absent at `k` becomes an ABSENT entry, and an account whose storage was
1636
+ * cleared after `k` becomes a `clearStorage` entry: the clear recorded every
1637
+ * slot the account held at that moment, so the union holds every slot it
1638
+ * held at `k`, and clearing first also drops the slots written since.
1639
+ *
1640
+ * THE UNION IS BUILT FRESH. `mergeChangeSetOlderWins` is not reused here
1641
+ * because it CONSUMES its second argument (inner storage maps are handed over
1642
+ * whole), and these are the sealed records themselves, which a later read
1643
+ * still needs intact.
1644
+ */
1645
+ function historicalEntries(k) {
1646
+ const accounts = new Map();
1647
+ const code = new Map();
1648
+ const storage = new Map();
1649
+ const cleared = new Set();
1650
+ const take = (cs) => {
1651
+ for (const [key, value] of cs.accounts)
1652
+ if (!accounts.has(key))
1653
+ accounts.set(key, value);
1654
+ for (const [key, value] of cs.code)
1655
+ if (!code.has(key))
1656
+ code.set(key, value);
1657
+ for (const [addressKey, slots] of cs.storage) {
1658
+ let target = storage.get(addressKey);
1659
+ if (target === undefined) {
1660
+ target = new Map();
1661
+ storage.set(addressKey, target);
1662
+ }
1663
+ for (const [slotKey, value] of slots)
1664
+ if (!target.has(slotKey))
1665
+ target.set(slotKey, value);
1666
+ }
1667
+ for (const addressKey of cs.storageCleared)
1668
+ cleared.add(addressKey);
1669
+ };
1670
+ for (const record of sealed)
1671
+ if (record.number > k)
1672
+ take(record.changes);
1673
+ const open = changeSets?.peekChangeSet();
1674
+ if (open)
1675
+ take(open);
1676
+ // One entry per address, keyed as `address.toString()` gives it (the
1677
+ // account and code maps' key; `unpackAddressKey` yields the same form).
1678
+ const entries = new Map();
1679
+ const entry = (key) => {
1680
+ let e = entries.get(key);
1681
+ if (e === undefined) {
1682
+ e = {
1683
+ address: createAddressFromString(key),
1684
+ clearStorage: false,
1685
+ slots: [],
1686
+ absent: false,
1687
+ };
1688
+ entries.set(key, e);
1689
+ }
1690
+ return e;
1691
+ };
1692
+ for (const [key, account] of accounts) {
1693
+ const e = entry(key);
1694
+ if (account === undefined)
1695
+ e.absent = true;
1696
+ // A COPY: the engine is handed the object through the state manager, and
1697
+ // the sealed record must go on saying what the account was.
1698
+ else
1699
+ e.account = copyAccount(account);
1700
+ }
1701
+ for (const [key, value] of code)
1702
+ entry(key).code = value ?? new Uint8Array(0);
1703
+ for (const addressKey of cleared)
1704
+ entry(unpackAddressKey(addressKey)).clearStorage = true;
1705
+ for (const [addressKey, slots] of storage) {
1706
+ const e = entry(unpackAddressKey(addressKey));
1707
+ for (const [slotKey, value] of slots)
1708
+ e.slots.push({
1709
+ key: hexToBytes(unpackSlotKey(slotKey)),
1710
+ value: value ?? new Uint8Array(0),
1711
+ });
1712
+ }
1713
+ return [...entries.values()];
1714
+ }
1715
+ /**
1716
+ * PARSE geth-STYLE STATE OVERRIDES into {@link StateEntry}s. `eth_call` and
1717
+ * `eth_estimateGas` take them as their third parameter:
1718
+ * `{[address]: {balance?, nonce?, code?, state?, stateDiff?}}`, where `state`
1719
+ * REPLACES the account's whole storage and `stateDiff` patches individual
1720
+ * slots. They used to be ignored, which answered a question the caller had not
1721
+ * asked, with no error.
1722
+ *
1723
+ * PARSED BEFORE ANY LEVEL IS OPENED, into values that cannot fail to apply, so
1724
+ * a malformed override is a -32602 naming it (never a raw `SyntaxError` from
1725
+ * `BigInt`), and a refusal never has a half-applied override to unwind.
1726
+ *
1727
+ * ## What is refused
1728
+ *
1729
+ * `state` and `stateDiff` on the same account (geth refuses that too), and any
1730
+ * field this node does not implement (`movePrecompileToAddress`, for one): a
1731
+ * -32602 naming it, never a call run without it.
1732
+ */
1733
+ function parseStateOverrides(overrides, method) {
1734
+ if (overrides == null)
1735
+ return [];
1736
+ if (typeof overrides !== 'object' || Array.isArray(overrides))
1737
+ throw new RpcError(-32602, `invalid state overrides for ${method}: expected an object keyed by address.`);
1738
+ const supported = new Set([
1739
+ 'balance',
1740
+ 'nonce',
1741
+ 'code',
1742
+ 'state',
1743
+ 'stateDiff',
1744
+ ]);
1745
+ const bad = (what) => {
1746
+ throw new RpcError(-32602, `invalid state override in ${method}: ${what}.`);
1747
+ };
1748
+ const quantity = (v, what) => {
1749
+ if (typeof v !== 'string' || !/^0x[0-9a-fA-F]+$/.test(v))
1750
+ return bad(`${what} must be a hex quantity, got ${JSON.stringify(v)}`);
1751
+ const n = BigInt(v);
1752
+ // A 32-byte word is the widest thing any override field names.
1753
+ if (n >= 1n << 256n)
1754
+ return bad(`${what} does not fit in 32 bytes`);
1755
+ return n;
1756
+ };
1757
+ const data = (v, what) => {
1758
+ if (typeof v !== 'string' || !/^0x([0-9a-fA-F]{2})*$/.test(v))
1759
+ return bad(`${what} must be even-length hex, got ${JSON.stringify(v)}`);
1760
+ return hexToBytes(v);
1761
+ };
1762
+ return Object.entries(overrides).map(([addr, o]) => {
1763
+ if (o == null || typeof o !== 'object' || Array.isArray(o))
1764
+ return bad(`the override for ${addr} must be an object`);
1765
+ for (const k of Object.keys(o))
1766
+ if (!supported.has(k))
1767
+ throw new RpcError(-32602, `state override field not supported by this node: ${addr}.${k} in ${method}. ` +
1768
+ `Supported: balance, nonce, code, state, stateDiff.`);
1769
+ if (o.state != null && o.stateDiff != null)
1770
+ throw new RpcError(-32602, `account ${addr} has both 'state' and 'stateDiff' in ${method}; pass one.`);
1771
+ let address;
1772
+ try {
1773
+ address = createAddressFromString(addr);
1774
+ }
1775
+ catch {
1776
+ return bad(`${JSON.stringify(addr)} is not an address`);
1777
+ }
1778
+ const slotsIn = o.state ?? o.stateDiff;
1779
+ if (slotsIn != null &&
1780
+ (typeof slotsIn !== 'object' || Array.isArray(slotsIn)))
1781
+ return bad(`${addr}.${o.state != null ? 'state' : 'stateDiff'} must be an object of slot -> value`);
1782
+ const slots = Object.entries(slotsIn ?? {}).map(([slot, val]) => {
1783
+ const value = data(val, `${addr} slot ${slot}`);
1784
+ // A slot value is ONE word: a longer one would be silently truncated
1785
+ // to its low 32 bytes by the padding below, so it is refused.
1786
+ if (value.length > 32)
1787
+ return bad(`${addr} slot ${slot} value is longer than 32 bytes`);
1788
+ return {
1789
+ key: setLengthLeft(bigIntToBytes(quantity(slot, `${addr} slot key`)), 32),
1790
+ // SHORTEST FORM, as the EVM itself stores a value: a padded zero
1791
+ // and a missing slot must be the same thing to SSTORE's gas rules.
1792
+ value: unpadBytes(setLengthLeft(value, 32)),
1793
+ };
1794
+ });
1795
+ return {
1796
+ address,
1797
+ balance: o.balance != null
1798
+ ? quantity(o.balance, `${addr}.balance`)
1799
+ : undefined,
1800
+ nonce: o.nonce != null ? quantity(o.nonce, `${addr}.nonce`) : undefined,
1801
+ code: o.code != null ? data(o.code, `${addr}.code`) : undefined,
1802
+ clearStorage: o.state != null,
1803
+ slots,
1804
+ absent: false,
1805
+ };
1806
+ });
1807
+ }
1808
+ /**
1809
+ * RUN A READ WITH STATE ENTRIES WRITTEN INTO A CHECKPOINT IT OPENS, and leave
1810
+ * no trace of them. The entries are K's state for a historical call
1811
+ * ({@link historicalEntries}) followed by the caller's state overrides
1812
+ * ({@link parseStateOverrides}), so the overrides land ON TOP of K's state,
1813
+ * as geth applies them on top of the block's. With no entries (a call at the
1814
+ * head without overrides) no level is opened at all.
1815
+ *
1816
+ * ## Why a checkpoint here is safe, given the hazard documented at `serialise`
1817
+ *
1818
+ * The corruption described there needs two executions to interleave on the one
1819
+ * checkpoint stack. This level is opened and closed by the SAME request, inside
1820
+ * the serialisation point, with the read nested strictly inside it: the default
1821
+ * engine's own checkpoint/revert lands on top of this one and pops before this
1822
+ * one does, and revm reads the top of the stack on every access and never
1823
+ * commits. So the stack is `[base, entries, (call)]` and unwinds in order,
1824
+ * whether the read returns, reverts or throws (the `finally`). It is also the
1825
+ * same mechanism `evmCall` already relies on for purity, one level lower. The
1826
+ * derived trie of a `computeStateRoot` node is never involved: change-set recording is
1827
+ * suspended for the read and the level is reverted, so nothing it wrote is
1828
+ * ever named to the trie.
1829
+ */
1830
+ async function withStateEntries(entries, read) {
1831
+ if (entries.length === 0)
1832
+ return read();
1833
+ await sm.checkpoint();
1834
+ try {
1835
+ for (const e of entries)
1836
+ await applyStateEntry(e);
1837
+ return await read();
1838
+ }
1839
+ finally {
1840
+ await sm.revert();
1841
+ }
1842
+ }
1843
+ /**
1844
+ * WRITE ONE {@link StateEntry} into the top level. The ONE apply step shared by
1845
+ * the RPC override path and the historical path.
1846
+ *
1847
+ * ORDER MATTERS, and each step re-reads the account so no step writes back a
1848
+ * stale copy over the previous one:
1849
+ *
1850
+ * 1. CODE FIRST. `putCode` rewrites the account's `codeHash`, and CREATES an
1851
+ * empty account when there is none, so it must come before
1852
+ * anything that states the account.
1853
+ * 2. THE ACCOUNT: a whole account replaces what `putCode` left (its `codeHash`
1854
+ * is K's own), and `balance` / `nonce` are a read-modify-write on top.
1855
+ * 3. STORAGE: a clear, then the slots.
1856
+ * 4. ABSENT LAST: an empty code entry, then `deleteAccount`. The account must
1857
+ * read as ABSENT, not as a zero account, and two upstream behaviours stand
1858
+ * in the way: `putCode` creates an account (so the code is emptied BEFORE
1859
+ * the delete), and `deleteAccount` leaves the address's code in the code
1860
+ * map, where `eth_getCode` and the default engine's `EXTCODESIZE` would
1861
+ * still see it (so the code is emptied at all). `deleteAccount` also clears
1862
+ * the storage.
1863
+ */
1864
+ async function applyStateEntry(e) {
1865
+ if (e.code !== undefined)
1866
+ await sm.putCode(e.address, e.code);
1867
+ if (e.account !== undefined)
1868
+ await sm.putAccount(e.address, e.account);
1869
+ if (e.balance !== undefined || e.nonce !== undefined) {
1870
+ const acc = (await sm.getAccount(e.address)) ?? new Account();
1871
+ if (e.balance !== undefined)
1872
+ acc.balance = e.balance;
1873
+ if (e.nonce !== undefined)
1874
+ acc.nonce = e.nonce;
1875
+ await sm.putAccount(e.address, acc);
1876
+ }
1877
+ if (e.clearStorage)
1878
+ await sm.clearStorage(e.address);
1879
+ for (const { key, value } of e.slots)
1880
+ await sm.putStorage(e.address, key, value);
1881
+ if (e.absent) {
1882
+ await sm.putCode(e.address, new Uint8Array(0));
1883
+ await sm.deleteAccount(e.address);
1884
+ }
1885
+ }
1886
+ /**
1887
+ * geth's `eth_call` takes BLOCK overrides as a fourth parameter. This node does
1888
+ * not implement them, so a request carrying them is refused rather than run
1889
+ * against the real block environment.
1890
+ */
1891
+ function refuseBlockOverrides(blockOverrides, method) {
1892
+ if (blockOverrides == null)
1893
+ return;
1894
+ throw new RpcError(-32602, `block overrides are not supported by this node (${method}, 4th parameter). ` +
1895
+ `The call would otherwise run against the real block environment.`);
1896
+ }
1121
1897
  // ---------- eth_call / estimateGas through the ENGINE's READ half (no signing) ----
1122
1898
  /**
1123
1899
  * The node's single pure-read helper, and the read half of the engine seam: it
@@ -1130,8 +1906,16 @@ export async function createNode(options = {}) {
1130
1906
  * `@ethereumjs/evm` engine checkpoints/reverts and resets EIP-2929 warmth
1131
1907
  * because that EVM requires it; an engine that cannot commit pays for neither.
1132
1908
  * See ./engine.ts.
1909
+ *
1910
+ * `block` is the block the call executes IN: the head's for a call at the
1911
+ * head, block K's stored `Block` for a call pinned to K, so `NUMBER`,
1912
+ * `TIMESTAMP`, `COINBASE`, `PREVRANDAO`, `BASEFEE` and `GASLIMIT` are K's. It
1913
+ * also bounds `BLOCKHASH`: both engines answer zero for any number at or above
1914
+ * the block's own, so a call at K cannot see the hash of K or of anything
1915
+ * after it without any further mechanism (measured on both engines by
1916
+ * `test/historical-call.spec.ts` and its revm twin).
1133
1917
  */
1134
- async function evmCall(params, budget) {
1918
+ async function evmCall(params, block, budget) {
1135
1919
  const from = params.from
1136
1920
  ? createAddressFromString(params.from)
1137
1921
  : createAddressFromString('0x0000000000000000000000000000000000000000');
@@ -1176,7 +1960,7 @@ export async function createNode(options = {}) {
1176
1960
  data,
1177
1961
  value,
1178
1962
  gasLimit,
1179
- block: blockStore.get(latestNumber).block,
1963
+ block,
1180
1964
  });
1181
1965
  }
1182
1966
  /**
@@ -1250,7 +2034,7 @@ export async function createNode(options = {}) {
1250
2034
  * points callers HERE for "the number a transaction needs", so the node must
1251
2035
  * never refuse a limit it has just recommended.
1252
2036
  */
1253
- async function estimateGas(p) {
2037
+ async function estimateGas(p, block) {
1254
2038
  const dataHex = p.data ?? p.input ?? '0x';
1255
2039
  const isCreate = !p.to;
1256
2040
  const overhead = intrinsicGas(dataHex, isCreate, common) + accessListGas(p.accessList);
@@ -1260,6 +2044,11 @@ export async function createNode(options = {}) {
1260
2044
  // back a number it will not accept. `gas` on an `eth_estimateGas` request is
1261
2045
  // geth's cap on the SEARCH ("do not consider limits above this"), which is a
1262
2046
  // different thing from `gas` on an `eth_call` ("let it run this long").
2047
+ //
2048
+ // AT A PAST BLOCK K THE CAP IS STILL THIS ONE, not K's own `gasLimit`,
2049
+ // deliberately (historical-eth-call): the number is for a transaction to be
2050
+ // SUBMITTED now, and what it meets is the limit of the blocks this node
2051
+ // mines, whatever K's was. `block` only decides what the probes execute in.
1263
2052
  const supplied = p.gas != null ? BigInt(p.gas) : undefined;
1264
2053
  const cap = supplied === undefined || supplied > minedBlockGasLimit
1265
2054
  ? minedBlockGasLimit
@@ -1277,7 +2066,7 @@ export async function createNode(options = {}) {
1277
2066
  ? `The allowance is the \`gas\` you passed on the request; raise it or omit it.`
1278
2067
  : `The allowance is this node's block gas limit; raise it with createNode({blockGasLimit: ...}).`));
1279
2068
  }
1280
- const probe = (limit) => evmCall(p, limit - overhead);
2069
+ const probe = (limit) => evmCall(p, block, limit - overhead);
1281
2070
  // 1) THE UPPER BOUND, run first: a request that cannot succeed with all the
1282
2071
  // gas there is cannot succeed at all, and the caller learns that here rather
1283
2072
  // than from a receipt.
@@ -1403,18 +2192,39 @@ export async function createNode(options = {}) {
1403
2192
  const sb = num != null ? blockStore.get(num) : undefined;
1404
2193
  return sb ? blockToRpc(sb, Boolean(params[1])) : null;
1405
2194
  }
2195
+ // AT THE HEAD OR AT A BLOCK K IN THE `stateHistory` WINDOW, through the one
2196
+ // gate every state read uses (`historicalBlock`). At K the engine runs with
2197
+ // K's stored `Block` on K's state, written into a checkpoint the request
2198
+ // opens and reverts (`historicalEntries`, `withStateEntries`), with the
2199
+ // caller's state overrides applied ON TOP of it.
1406
2200
  case 'eth_call': {
1407
- const r = await evmCall(params[0] ?? {});
2201
+ const k = historicalBlock(params[1], 'eth_call');
2202
+ refuseBlockOverrides(params[3], 'eth_call');
2203
+ const entries = [
2204
+ ...(k === undefined ? [] : historicalEntries(k)),
2205
+ ...parseStateOverrides(params[2], 'eth_call'),
2206
+ ];
2207
+ const block = blockStore.get(k ?? latestNumber).block;
2208
+ const r = await pureRead(() => withStateEntries(entries, () => evmCall(params[0] ?? {}, block)));
1408
2209
  if (r.error)
1409
2210
  throw new RpcError(3, 'execution reverted', hex(r.returnValue));
1410
2211
  return hex(r.returnValue);
1411
2212
  }
1412
- case 'eth_estimateGas':
2213
+ case 'eth_estimateGas': {
1413
2214
  // THE SMALLEST GAS LIMIT AT WHICH THIS REQUEST SUCCEEDS, found by
1414
2215
  // re-executing it. The whole method is {@link estimateGas} above,
1415
2216
  // including why it is a search rather than the run-and-measure it used to
1416
- // be, and where the request's EIP-2930 access list is charged.
1417
- return numHex(await estimateGas(params[0] ?? {}));
2217
+ // be, and where the request's EIP-2930 access list is charged. The whole
2218
+ // search runs inside ONE checkpoint holding K's state (and the overrides).
2219
+ const k = historicalBlock(params[1], 'eth_estimateGas');
2220
+ refuseBlockOverrides(params[3], 'eth_estimateGas');
2221
+ const entries = [
2222
+ ...(k === undefined ? [] : historicalEntries(k)),
2223
+ ...parseStateOverrides(params[2], 'eth_estimateGas'),
2224
+ ];
2225
+ const block = blockStore.get(k ?? latestNumber).block;
2226
+ return numHex(await pureRead(() => withStateEntries(entries, () => estimateGas(params[0] ?? {}, block))));
2227
+ }
1418
2228
  case 'eth_fillTransaction': {
1419
2229
  // Fill the missing fields of a tx request and return {tx, raw} like geth
1420
2230
  // does (the `raw` is the UNSIGNED serialization — the node has no keys; a
@@ -1425,8 +2235,9 @@ export async function createNode(options = {}) {
1425
2235
  const from = p.from
1426
2236
  ? createAddressFromString(p.from)
1427
2237
  : createAddressFromString('0x0000000000000000000000000000000000000000');
1428
- const acc = await sm.getAccount(from);
1429
- const nonce = p.nonce != null ? BigInt(p.nonce) : (acc?.nonce ?? 0n);
2238
+ // The PENDING nonce, as geth fills it: the account's nonce advanced past
2239
+ // the transactions it already has queued.
2240
+ const nonce = p.nonce != null ? BigInt(p.nonce) : await pendingNonce(from);
1430
2241
  const value = p.value != null ? BigInt(p.value) : 0n;
1431
2242
  const dataHex = p.data ?? p.input ?? '0x';
1432
2243
  const isCreate = !p.to;
@@ -1443,7 +2254,7 @@ export async function createNode(options = {}) {
1443
2254
  // `accessListGas` in ./intrinsic-gas.ts.
1444
2255
  const gas = p.gas != null
1445
2256
  ? BigInt(p.gas)
1446
- : await estimateGas({ ...p, gas: undefined, accessList: undefined });
2257
+ : await pureRead(() => estimateGas({ ...p, gas: undefined, accessList: undefined }, blockStore.get(latestNumber).block));
1447
2258
  // Fee fields: legacy iff caller passed gasPrice (and no 1559 fields),
1448
2259
  // otherwise EIP-1559 with the node's constant fee market.
1449
2260
  const isLegacy = p.gasPrice != null &&
@@ -1499,28 +2310,53 @@ export async function createNode(options = {}) {
1499
2310
  };
1500
2311
  return { raw: hex(unsigned.serialize()), tx };
1501
2312
  }
2313
+ // EVERY state read at a block is gated first: served at the head, served
2314
+ // from the undo log inside the `stateHistory` window (these four only,
2315
+ // through `historicalBlock`), and REFUSED below it, never answered from the
2316
+ // head for a block that is not the head.
1502
2317
  case 'eth_getBalance': {
1503
- const acc = await sm.getAccount(createAddressFromString(params[0]));
2318
+ const k = historicalBlock(params[1], 'eth_getBalance');
2319
+ const address = createAddressFromString(params[0]);
2320
+ const acc = k === undefined
2321
+ ? await sm.getAccount(address)
2322
+ : await accountAt(k, address);
1504
2323
  return numHex(acc?.balance ?? 0n);
1505
2324
  }
1506
2325
  case 'eth_getTransactionCount': {
1507
- const acc = await sm.getAccount(createAddressFromString(params[0]));
1508
- return numHex(acc?.nonce ?? 0n);
2326
+ const k = historicalBlock(params[1], 'eth_getTransactionCount');
2327
+ const address = createAddressFromString(params[0]);
2328
+ if (k !== undefined)
2329
+ return numHex((await accountAt(k, address))?.nonce ?? 0n);
2330
+ // `pending` COUNTS THE QUEUED TRANSACTIONS, the one state read where it
2331
+ // differs from the head: a client picking a nonce asks for `pending`
2332
+ // (viem does by default), and in manual/interval mining two sends
2333
+ // before a mine would otherwise get the SAME nonce, the second then
2334
+ // refused at mine time as a replay.
2335
+ return numHex(params[1] === 'pending'
2336
+ ? await pendingNonce(address)
2337
+ : ((await sm.getAccount(address))?.nonce ?? 0n));
1509
2338
  }
1510
2339
  case 'eth_getCode': {
1511
- const code = await sm.getCode(createAddressFromString(params[0]));
1512
- return hex(code);
2340
+ const k = historicalBlock(params[1], 'eth_getCode');
2341
+ const address = createAddressFromString(params[0]);
2342
+ return hex(k === undefined
2343
+ ? await sm.getCode(address)
2344
+ : await codeAt(k, address));
1513
2345
  }
1514
2346
  case 'eth_getStorageAt': {
2347
+ const k = historicalBlock(params[2], 'eth_getStorageAt');
1515
2348
  const addr = createAddressFromString(params[0]);
1516
2349
  const slot = setLengthLeft(bigIntToBytes(BigInt(params[1])), 32);
1517
- const val = await sm.getStorage(addr, slot);
2350
+ const val = k === undefined
2351
+ ? await sm.getStorage(addr, slot)
2352
+ : await storageAt(k, addr, slot);
1518
2353
  return hex(setLengthLeft(val, 32));
1519
2354
  }
1520
2355
  // ---- Runtime state cheats (anvil/hardhat-style; for tests/local tooling) ----
1521
2356
  // These MUTATE state directly (no tx). Honest about being non-standard: they
1522
- // are `evm_*`-namespaced. In trie mode each commits into the trie so the next
1523
- // getStateRoot() reflects the change.
2357
+ // are `evm_*`-namespaced. They write the flat state only; on a `computeStateRoot` node the
2358
+ // open record names what they wrote, so the next getStateRoot() (or mined
2359
+ // block) rehashes it into the derived trie.
1524
2360
  case 'evm_setBalance': {
1525
2361
  // params: [address, valueHex]
1526
2362
  const addr = createAddressFromString(params[0]);
@@ -1541,8 +2377,6 @@ export async function createNode(options = {}) {
1541
2377
  // params: [address, codeHex]
1542
2378
  const addr = createAddressFromString(params[0]);
1543
2379
  await sm.putCode(addr, hexToBytes(params[1]));
1544
- touchedAccounts.add(addr.toString());
1545
- await commitIfTrie();
1546
2380
  return true;
1547
2381
  }
1548
2382
  case 'evm_setStorageAt': {
@@ -1550,8 +2384,6 @@ export async function createNode(options = {}) {
1550
2384
  const addr = createAddressFromString(params[0]);
1551
2385
  const slot = setLengthLeft(bigIntToBytes(BigInt(params[1])), 32);
1552
2386
  await sm.putStorage(addr, slot, setLengthLeft(hexToBytes(params[2]), 32));
1553
- touchedAccounts.add(addr.toString());
1554
- await commitIfTrie();
1555
2387
  return true;
1556
2388
  }
1557
2389
  case 'evm_setAccount': {
@@ -1569,7 +2401,6 @@ export async function createNode(options = {}) {
1569
2401
  for (const [slot, val] of Object.entries(a.storage ?? {})) {
1570
2402
  await sm.putStorage(addr, setLengthLeft(bigIntToBytes(BigInt(slot)), 32), setLengthLeft(hexToBytes(val), 32));
1571
2403
  }
1572
- await commitIfTrie();
1573
2404
  return true;
1574
2405
  }
1575
2406
  case 'eth_gasPrice':
@@ -1577,7 +2408,45 @@ export async function createNode(options = {}) {
1577
2408
  case 'eth_maxPriorityFeePerGas':
1578
2409
  return numHex(maxPriorityFeePerGas);
1579
2410
  case 'eth_feeHistory': {
1580
- const count = Number(BigInt(params[0] ?? '0x1'));
2411
+ // params: [blockCount, newestBlock, rewardPercentiles]. `newestBlock` is
2412
+ // HONOURED (it used to be ignored, and the window always ended at the head),
2413
+ // and the per-block base fee and gas-used ratio are the stored blocks' own.
2414
+ // Only `reward` stays synthetic: this node's priority fee is a constant.
2415
+ const rawCount = params[0] ?? '0x1';
2416
+ const requested = typeof rawCount === 'number' && Number.isSafeInteger(rawCount)
2417
+ ? rawCount
2418
+ : typeof rawCount === 'string' && /^0x[0-9a-fA-F]+$/.test(rawCount)
2419
+ ? Number(BigInt(rawCount))
2420
+ : NaN;
2421
+ if (!(requested >= 0))
2422
+ throw new RpcError(-32602, `invalid eth_feeHistory blockCount: ${JSON.stringify(rawCount)}. Expected a hex quantity.`);
2423
+ const newest = resolveBlockTag(params[1] ?? 'latest');
2424
+ if (newest > latestNumber)
2425
+ throw new RpcError(-32000, `request beyond head block: eth_feeHistory newestBlock is ${newest}, but the head of this node is block ${latestNumber}.`);
2426
+ // geth caps a request at 1024 blocks, and nothing precedes block 0.
2427
+ const count = Math.max(0, Math.min(requested, 1024, newest + 1));
2428
+ const oldest = newest - count + 1;
2429
+ const nextBaseFee = (n) => {
2430
+ const sb = blockStore.get(n);
2431
+ return sb
2432
+ ? sb.header.baseFeePerGas
2433
+ : numHex(blockEnv?.baseFeePerGas ?? baseFeePerGas);
2434
+ };
2435
+ const ratio = (n) => {
2436
+ // The header's own `gasUsed` is a documented 0x0 placeholder, so the
2437
+ // block's usage is summed from its receipts, which are real.
2438
+ const sb = blockStore.get(n);
2439
+ if (!sb)
2440
+ return 0;
2441
+ let used = 0n;
2442
+ for (const th of sb.header.transactions) {
2443
+ const r = receipts.get(th);
2444
+ if (r)
2445
+ used += BigInt(r.gasUsed);
2446
+ }
2447
+ const limit = BigInt(sb.header.gasLimit);
2448
+ return limit === 0n ? 0 : Number(used) / Number(limit);
2449
+ };
1581
2450
  // `reward` must carry ONE entry per requested percentile, per block. Returning a
1582
2451
  // single entry regardless of `rewardPercentiles` breaks any caller that asks for
1583
2452
  // several and indexes them: rocketh requests [10, 50, 80] and reads index 1 and 2,
@@ -1587,9 +2456,15 @@ export async function createNode(options = {}) {
1587
2456
  const percentiles = Array.isArray(params[2]) ? params[2] : [];
1588
2457
  const rewardPerBlock = (percentiles.length > 0 ? percentiles : [50]).map(() => numHex(maxPriorityFeePerGas));
1589
2458
  return {
1590
- oldestBlock: numHex(Math.max(0, latestNumber - count + 1)),
1591
- baseFeePerGas: Array.from({ length: count + 1 }, () => numHex(baseFeePerGas)),
1592
- gasUsedRatio: Array.from({ length: count }, () => 0.5),
2459
+ // An empty window is geth's empty answer, oldestBlock included.
2460
+ oldestBlock: numHex(count === 0 ? 0 : oldest),
2461
+ // One per block in the window PLUS the block after it, which is the
2462
+ // stored block when there is one and the next block this node will mine
2463
+ // when the window ends at the head.
2464
+ baseFeePerGas: count === 0
2465
+ ? []
2466
+ : Array.from({ length: count + 1 }, (_, i) => nextBaseFee(oldest + i)),
2467
+ gasUsedRatio: Array.from({ length: count }, (_, i) => ratio(oldest + i)),
1593
2468
  reward: Array.from({ length: count }, () => rewardPerBlock),
1594
2469
  };
1595
2470
  }
@@ -1629,8 +2504,28 @@ export async function createNode(options = {}) {
1629
2504
  // is deferred — see tasks/slim-node-eth-getlogs-index.md. Keep this scan as
1630
2505
  // the authoritative semantics if/when an index is added.
1631
2506
  const f = params[0] ?? {};
1632
- const from = f.fromBlock != null ? resolveBlockTag(f.fromBlock) : 0;
1633
- const to = f.toBlock != null ? resolveBlockTag(f.toBlock) : latestNumber;
2507
+ // `blockHash` (EIP-234) pins the query to ONE block. It used to be ignored,
2508
+ // which returned the logs of EVERY block. It excludes a range, as in geth,
2509
+ // and a hash no block has is an error rather than an empty result, so a
2510
+ // typo cannot read as "that block emitted nothing".
2511
+ let from;
2512
+ let to;
2513
+ if (f.blockHash != null) {
2514
+ if (f.fromBlock != null || f.toBlock != null)
2515
+ throw new RpcError(-32602, 'invalid eth_getLogs filter: cannot specify both blockHash and fromBlock/toBlock.');
2516
+ const n = blockByHash.get(String(f.blockHash).toLowerCase());
2517
+ if (n === undefined)
2518
+ throw new RpcError(-32000, `unknown block: eth_getLogs blockHash ${f.blockHash} is not a block on this node.`);
2519
+ from = n;
2520
+ to = n;
2521
+ }
2522
+ else {
2523
+ // geth's default for an omitted `fromBlock` is `latest`, not 0; this
2524
+ // node has always defaulted to 0 and that is kept (changing it would
2525
+ // silently shrink every existing consumer's results).
2526
+ from = f.fromBlock != null ? resolveBlockTag(f.fromBlock) : 0;
2527
+ to = f.toBlock != null ? resolveBlockTag(f.toBlock) : latestNumber;
2528
+ }
1634
2529
  const addrFilter = f.address
1635
2530
  ? (Array.isArray(f.address) ? f.address : [f.address]).map((a) => a.toLowerCase())
1636
2531
  : null;
@@ -1686,48 +2581,28 @@ export async function createNode(options = {}) {
1686
2581
  const accounts = {};
1687
2582
  const code = {};
1688
2583
  const storage = {};
1689
- if (stateMode === 'trie') {
1690
- // Trie mode dumps accounts + code via the touched-account set. NOTE: it does
1691
- // NOT dump contract STORAGE — the EVM journals storage on an internal
1692
- // shallowCopy of the state manager that bypasses any interception, and the
1693
- // trie's own dumpStorage exposes only keccak-HASHED slot keys (not the raw
1694
- // slots loadState needs). Full-storage persistence is a 'none'-mode feature
1695
- // (where the live Map IS the committed state). Trie mode is for the REAL
1696
- // state root (conformance block roots), not for IndexedDB persist.
1697
- for (const addr of touchedAccounts) {
1698
- const address = createAddressFromString(addr);
1699
- const acc = await sm.getAccount(address);
1700
- if (acc === undefined)
1701
- continue;
2584
+ // EVERY NODE DUMPS THE SAME WAY, storage included: the account and code
2585
+ // stacks are plain Maps whose TOP frame is the live set, and storage is
2586
+ // per-account with per-checkpoint OVERLAYS, flattened by liveStorage(). A
2587
+ // `computeStateRoot` node runs on this same flat state, and its derived trie is NOT
2588
+ // serialised: `loadState` rebuilds it from the loaded flat state.
2589
+ //
2590
+ // THE SERIALISED FORMAT IS NOT THE INTERNAL LAYOUT and must not follow it:
2591
+ // this output is persisted data (IndexedDB, loadState fixtures) with existing
2592
+ // state behind it. It stays `{address: {slot: value}}` in 0x-hex, and
2593
+ // `test/storage-overlay.spec.ts` asserts it byte-identical to a dump taken
2594
+ // from the pre-overlay flat layout.
2595
+ const accMap = sm.accountStack[sm.accountStack.length - 1];
2596
+ const codeMap = sm.codeStack[sm.codeStack.length - 1];
2597
+ for (const [addr, acc] of accMap) {
2598
+ if (acc !== undefined)
1702
2599
  accounts[addr] = hex(acc.serialize());
1703
- const c = await sm.getCode(address);
1704
- if (c.length > 0)
1705
- code[addr] = hex(c);
1706
- }
1707
2600
  }
1708
- else {
1709
- // 'none' mode: the account and code stacks are still plain Maps whose TOP
1710
- // frame is the live set, and storage is per-account with per-checkpoint
1711
- // OVERLAYS, flattened by liveStorage(). Dump both directly (no trie walk).
1712
- //
1713
- // THE SERIALISED FORMAT IS NOT THE INTERNAL LAYOUT and must not follow it:
1714
- // this output is persisted data (IndexedDB, loadState fixtures) with existing
1715
- // state behind it. It stays `{address: {slot: value}}` in 0x-hex, and
1716
- // `test/storage-overlay.spec.ts` asserts it byte-identical to a dump taken
1717
- // from the pre-overlay flat layout.
1718
- const live = sm;
1719
- const accMap = live.accountStack[live.accountStack.length - 1];
1720
- const codeMap = live.codeStack[live.codeStack.length - 1];
1721
- for (const [addr, acc] of accMap) {
1722
- if (acc !== undefined)
1723
- accounts[addr] = hex(acc.serialize());
1724
- }
1725
- for (const [addr, c] of codeMap)
1726
- code[addr] = hex(c);
1727
- for (const [addr, slots] of live.liveStorage()) {
1728
- for (const [slot, val] of slots)
1729
- (storage[addr] ??= {})[slot] = hex(val);
1730
- }
2601
+ for (const [addr, c] of codeMap)
2602
+ code[addr] = hex(c);
2603
+ for (const [addr, slots] of sm.liveStorage()) {
2604
+ for (const [slot, val] of slots)
2605
+ (storage[addr] ??= {})[slot] = hex(val);
1731
2606
  }
1732
2607
  const blocks = [];
1733
2608
  for (let i = 0; i <= latestNumber; i++) {
@@ -1738,15 +2613,81 @@ export async function createNode(options = {}) {
1738
2613
  return {
1739
2614
  version: 1,
1740
2615
  chainId,
1741
- stateMode,
1742
2616
  accounts,
1743
2617
  code,
1744
2618
  storage,
1745
2619
  blocks,
1746
2620
  receipts: Object.fromEntries(receipts),
1747
2621
  transactions: Object.fromEntries(transactions),
2622
+ // ADDITIVE: a node without `stateHistory` writes no field at all, so its
2623
+ // dump is exactly what it was before history existed.
2624
+ ...(stateHistoryBlocks === undefined ? {} : { history: dumpHistory() }),
1748
2625
  };
1749
2626
  }
2627
+ /**
2628
+ * THE UNDO LOG AS THE DUMP CARRIES IT: one serialized record per sealed block,
2629
+ * oldest first, with the OPEN record FOLDED INTO THE HEAD'S RECORD (older
2630
+ * entry winning).
2631
+ *
2632
+ * WHY FOLD rather than dump the open record as a field of its own: `loadState`
2633
+ * treats the loaded state as a baseline and CLEARS the open record, so the
2634
+ * writes since the head was mined (`evm_set*` cheats, a batch that threw
2635
+ * mid-block) become part of the loaded head, which reads them live exactly as
2636
+ * it did before the dump. What must not be lost is their prior values, which
2637
+ * every read BELOW the head consults after the sealed records. Folding keeps
2638
+ * the per-key earliest-wins union over `undo[K+1] .. undo[head]` + open
2639
+ * identical for every K below the head: a key the head's record already names
2640
+ * keeps that (older) value; a key it does not name did not change in the head
2641
+ * block, so its value at the end of the block before equals its value at the
2642
+ * end of the head, which is what the open record holds. The alternative, a
2643
+ * separate `open` field restored as the open record, would make the next mined
2644
+ * block after a reload own writes made before it, and would have been a second
2645
+ * new field for the same answers.
2646
+ *
2647
+ * With NO record for the head (the head is where history starts: genesis, or a
2648
+ * loaded head nothing was mined on), no block below the head is servable, and
2649
+ * the open record is dropped with nothing lost.
2650
+ *
2651
+ * Decisions around the format (this fold, the contiguous restore, `[]` vs no
2652
+ * key) are recorded in
2653
+ * `work/notes/observations/state-history-persistence-decisions.md`.
2654
+ */
2655
+ function dumpHistory() {
2656
+ const out = sealed.map((r) => serializeChangeSet(r.number, r.changes));
2657
+ const open = changeSets?.peekChangeSet();
2658
+ const head = out[out.length - 1];
2659
+ if (open !== undefined &&
2660
+ head !== undefined &&
2661
+ head.number === latestNumber)
2662
+ mergeSerializedOlderWins(head, serializeChangeSet(latestNumber, open));
2663
+ return out;
2664
+ }
2665
+ /**
2666
+ * RESTORE the undo log from a dump's `history` (see {@link dumpHistory}), after
2667
+ * the rest of the state is loaded and {@link latestNumber} is the dump's head.
2668
+ * Only the CONTIGUOUS run of records ending at the head block is kept, newest
2669
+ * first, stopping at the first gap and at this node's window (a record at or
2670
+ * below `head - N` serves no block in it, so a dump with more history than N
2671
+ * is truncated to N, and one with less serves what it has). A dump the writer
2672
+ * produced has no gap; a hand-edited or foreign one might, and a read below a
2673
+ * gap would silently be answered from a later state, which is the one thing
2674
+ * the history exists not to do. {@link historyFloor} becomes the block before
2675
+ * the oldest record kept, or the head when none is.
2676
+ */
2677
+ function loadHistory(history, window) {
2678
+ const kept = [];
2679
+ let expected = latestNumber;
2680
+ for (let i = history.length - 1; i >= 0; i--) {
2681
+ const record = history[i];
2682
+ if (record.number !== expected || record.number <= latestNumber - window)
2683
+ break;
2684
+ kept.push({ number: record.number, changes: deserializeChangeSet(record) });
2685
+ expected--;
2686
+ }
2687
+ kept.reverse();
2688
+ sealed.push(...kept);
2689
+ historyFloor = kept.length > 0 ? kept[0].number - 1 : latestNumber;
2690
+ }
1750
2691
  async function loadState(state) {
1751
2692
  // Rehydrate accounts/code/storage directly into SimpleStateManager.
1752
2693
  for (const [addr, accHex] of Object.entries(state.accounts)) {
@@ -1824,6 +2765,26 @@ export async function createNode(options = {}) {
1824
2765
  latestNumber = sh.number;
1825
2766
  parentHash = block.hash();
1826
2767
  }
2768
+ // A LOADED STATE IS A BASELINE, NOT HISTORY: the record is cleared, like the
2769
+ // construction baselines at genesis. The dump carries no change set for its
2770
+ // head, so there is none to report for it either.
2771
+ changeSets?.takeChangeSet();
2772
+ headBlockChangeSet = undefined;
2773
+ // ...and the history the node held described the state this load just
2774
+ // replaced, so it goes. What replaces it is the dump's own history, when the
2775
+ // dump carries one and this node keeps any: otherwise history starts again at
2776
+ // the loaded head (an older dump, or a node without `stateHistory`, which
2777
+ // ignores the field).
2778
+ sealed.length = 0;
2779
+ historyFloor = latestNumber;
2780
+ if (stateHistoryBlocks !== undefined && state.history !== undefined)
2781
+ loadHistory(state.history, stateHistoryBlocks);
2782
+ // ...and the derived trie described that replaced state too. It is REBUILT
2783
+ // from the loaded flat state, once, exactly as at construction: the dump
2784
+ // carries no trie (it never has to: the flat state determines it), and a
2785
+ // load is a baseline in no change set, so nothing else would put it there.
2786
+ if (computeStateRoot)
2787
+ derivedTrie = await DerivedStateTrie.build(sm, common);
1827
2788
  }
1828
2789
  // ---------- persistence auto-load on creation ----------
1829
2790
  if (options.persistence) {
@@ -1910,24 +2871,24 @@ export async function createNode(options = {}) {
1910
2871
  // touches no state. It only relays `request`, which IS serialised, so a request
1911
2872
  // arriving over a served port queues exactly like one made directly.
1912
2873
  const publicRequest = (args) => serialise(() => persistingRequest(args));
1913
- return {
2874
+ const node = {
1914
2875
  request: publicRequest,
1915
2876
  mine: () => serialise(mineAndPersist),
1916
2877
  dumpState: () => serialise(dumpState),
1917
2878
  loadState: (state) => serialise(() => loadState(state)),
1918
- stateMode,
2879
+ computeStateRoot,
1919
2880
  senderMode,
1920
2881
  // Identity only: the engine object itself stays internal, so the reading is a
1921
2882
  // plain value that survives a Worker/comlink boundary unchanged.
1922
2883
  engine: { id: engine.id },
1923
2884
  async getStateRoot() {
1924
- if (stateMode !== 'trie') {
1925
- throw new RpcError(-32004, "no state root in 'none' mode — create the node with stateMode:'trie' for a real Merkle-Patricia root");
2885
+ if (!computeStateRoot) {
2886
+ throw new RpcError(-32004, 'no state root: this node was created without computeStateRoot. Create it with computeStateRoot: true for a real Merkle-Patricia root');
1926
2887
  }
1927
- // SERIALISED like the rest, because this one READS BY WRITING: in trie mode
1928
- // `currentStateRoot` flushes the state manager's cache into the trie, so a
1929
- // root taken mid-transaction would both report an uncommitted state and
1930
- // push it into the trie on the way.
2888
+ // SERIALISED like the rest, because this one READS BY WRITING: it applies
2889
+ // the open record to the derived trie, which reads the COMMITTED flat state
2890
+ // and refuses to run with a checkpoint open, so a root taken
2891
+ // mid-transaction would report a state no block (or cheat) produced.
1931
2892
  return serialise(currentStateRoot);
1932
2893
  },
1933
2894
  onNewHead(cb) {
@@ -1968,5 +2929,12 @@ export async function createNode(options = {}) {
1968
2929
  servedPorts.clear();
1969
2930
  },
1970
2931
  };
2932
+ changeSetProbes.set(node, () => ({
2933
+ recording: changeSets !== undefined,
2934
+ sealedBlocks: sealed.map((r) => r.number),
2935
+ headBlock: headBlockChangeSet,
2936
+ open: changeSets?.peekChangeSet(),
2937
+ }));
2938
+ return node;
1971
2939
  }
1972
2940
  //# sourceMappingURL=node.js.map