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.
- package/README.md +115 -63
- package/dist/derived-trie.d.ts +38 -0
- package/dist/derived-trie.d.ts.map +1 -0
- package/dist/derived-trie.js +187 -0
- package/dist/derived-trie.js.map +1 -0
- package/dist/engine.d.ts +1 -2
- package/dist/engine.d.ts.map +1 -1
- package/dist/engine.js +2 -3
- package/dist/engine.js.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js.map +1 -1
- package/dist/node.d.ts +45 -0
- package/dist/node.d.ts.map +1 -1
- package/dist/node.js +1083 -140
- package/dist/node.js.map +1 -1
- package/dist/revm-state-store.d.ts +5 -0
- package/dist/revm-state-store.d.ts.map +1 -1
- package/dist/revm-state-store.js +42 -13
- package/dist/revm-state-store.js.map +1 -1
- package/dist/revm.d.ts.map +1 -1
- package/dist/revm.js +5 -19
- package/dist/revm.js.map +1 -1
- package/dist/state-manager.d.ts +256 -18
- package/dist/state-manager.d.ts.map +1 -1
- package/dist/state-manager.js +429 -20
- package/dist/state-manager.js.map +1 -1
- package/dist/storage-keys.d.ts +8 -0
- package/dist/storage-keys.d.ts.map +1 -1
- package/dist/storage-keys.js +18 -2
- package/dist/storage-keys.js.map +1 -1
- package/dist/types.d.ts +150 -33
- package/dist/types.d.ts.map +1 -1
- package/dist/worker-client.d.ts.map +1 -1
- package/dist/worker-client.js +20 -12
- package/dist/worker-client.js.map +1 -1
- package/dist/worker-host.d.ts.map +1 -1
- package/dist/worker-host.js +17 -9
- package/dist/worker-host.js.map +1 -1
- package/dist/worker-rpc-error.d.ts +21 -0
- package/dist/worker-rpc-error.d.ts.map +1 -0
- package/dist/worker-rpc-error.js +121 -0
- package/dist/worker-rpc-error.js.map +1 -0
- package/package.json +3 -1
- package/src/derived-trie.ts +214 -0
- package/src/engine.ts +2 -3
- package/src/index.ts +2 -1
- package/src/node.ts +1293 -136
- package/src/revm-state-store.ts +47 -13
- package/src/revm.ts +11 -25
- package/src/state-manager.ts +515 -21
- package/src/storage-keys.ts +21 -2
- package/src/types.ts +152 -34
- package/src/worker-client.ts +22 -12
- package/src/worker-host.ts +17 -9
- 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 {
|
|
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
|
|
230
|
-
//
|
|
231
|
-
//
|
|
232
|
-
//
|
|
233
|
-
//
|
|
234
|
-
//
|
|
235
|
-
//
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
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
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
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 (
|
|
570
|
+
if (derivedTrie === undefined)
|
|
356
571
|
return ZERO_ROOT;
|
|
357
|
-
const
|
|
358
|
-
|
|
359
|
-
|
|
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.
|
|
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.
|
|
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,
|
|
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
|
|
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
|
-
|
|
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
|
-
|
|
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 (
|
|
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
|
|
@@ -1118,6 +1596,279 @@ export async function createNode(options = {}) {
|
|
|
1118
1596
|
removed: false,
|
|
1119
1597
|
};
|
|
1120
1598
|
}
|
|
1599
|
+
/**
|
|
1600
|
+
* K'S STATE, AS STATE ENTRIES: for every key the undo log names after block
|
|
1601
|
+
* `k` (the sealed records of blocks `k+1 .. head`, then the OPEN record), the
|
|
1602
|
+
* value it had at the end of `k`, the earliest record winning per key, exactly
|
|
1603
|
+
* the lookup {@link historicalValue} makes for one key. A key no record names
|
|
1604
|
+
* has not changed since `k`, so the live state already holds it and it needs
|
|
1605
|
+
* no entry. O(keys changed since `k`), which is the cost of a historical
|
|
1606
|
+
* `eth_call` on top of the call itself.
|
|
1607
|
+
*
|
|
1608
|
+
* Written into a checkpoint by {@link withStateEntries}, so the engine
|
|
1609
|
+
* executes on K's state with no copy of state and no root swapped. An account
|
|
1610
|
+
* absent at `k` becomes an ABSENT entry, and an account whose storage was
|
|
1611
|
+
* cleared after `k` becomes a `clearStorage` entry: the clear recorded every
|
|
1612
|
+
* slot the account held at that moment, so the union holds every slot it
|
|
1613
|
+
* held at `k`, and clearing first also drops the slots written since.
|
|
1614
|
+
*
|
|
1615
|
+
* THE UNION IS BUILT FRESH. `mergeChangeSetOlderWins` is not reused here
|
|
1616
|
+
* because it CONSUMES its second argument (inner storage maps are handed over
|
|
1617
|
+
* whole), and these are the sealed records themselves, which a later read
|
|
1618
|
+
* still needs intact.
|
|
1619
|
+
*/
|
|
1620
|
+
function historicalEntries(k) {
|
|
1621
|
+
const accounts = new Map();
|
|
1622
|
+
const code = new Map();
|
|
1623
|
+
const storage = new Map();
|
|
1624
|
+
const cleared = new Set();
|
|
1625
|
+
const take = (cs) => {
|
|
1626
|
+
for (const [key, value] of cs.accounts)
|
|
1627
|
+
if (!accounts.has(key))
|
|
1628
|
+
accounts.set(key, value);
|
|
1629
|
+
for (const [key, value] of cs.code)
|
|
1630
|
+
if (!code.has(key))
|
|
1631
|
+
code.set(key, value);
|
|
1632
|
+
for (const [addressKey, slots] of cs.storage) {
|
|
1633
|
+
let target = storage.get(addressKey);
|
|
1634
|
+
if (target === undefined) {
|
|
1635
|
+
target = new Map();
|
|
1636
|
+
storage.set(addressKey, target);
|
|
1637
|
+
}
|
|
1638
|
+
for (const [slotKey, value] of slots)
|
|
1639
|
+
if (!target.has(slotKey))
|
|
1640
|
+
target.set(slotKey, value);
|
|
1641
|
+
}
|
|
1642
|
+
for (const addressKey of cs.storageCleared)
|
|
1643
|
+
cleared.add(addressKey);
|
|
1644
|
+
};
|
|
1645
|
+
for (const record of sealed)
|
|
1646
|
+
if (record.number > k)
|
|
1647
|
+
take(record.changes);
|
|
1648
|
+
const open = changeSets?.peekChangeSet();
|
|
1649
|
+
if (open)
|
|
1650
|
+
take(open);
|
|
1651
|
+
// One entry per address, keyed as `address.toString()` gives it (the
|
|
1652
|
+
// account and code maps' key; `unpackAddressKey` yields the same form).
|
|
1653
|
+
const entries = new Map();
|
|
1654
|
+
const entry = (key) => {
|
|
1655
|
+
let e = entries.get(key);
|
|
1656
|
+
if (e === undefined) {
|
|
1657
|
+
e = {
|
|
1658
|
+
address: createAddressFromString(key),
|
|
1659
|
+
clearStorage: false,
|
|
1660
|
+
slots: [],
|
|
1661
|
+
absent: false,
|
|
1662
|
+
};
|
|
1663
|
+
entries.set(key, e);
|
|
1664
|
+
}
|
|
1665
|
+
return e;
|
|
1666
|
+
};
|
|
1667
|
+
for (const [key, account] of accounts) {
|
|
1668
|
+
const e = entry(key);
|
|
1669
|
+
if (account === undefined)
|
|
1670
|
+
e.absent = true;
|
|
1671
|
+
// A COPY: the engine is handed the object through the state manager, and
|
|
1672
|
+
// the sealed record must go on saying what the account was.
|
|
1673
|
+
else
|
|
1674
|
+
e.account = copyAccount(account);
|
|
1675
|
+
}
|
|
1676
|
+
for (const [key, value] of code)
|
|
1677
|
+
entry(key).code = value ?? new Uint8Array(0);
|
|
1678
|
+
for (const addressKey of cleared)
|
|
1679
|
+
entry(unpackAddressKey(addressKey)).clearStorage = true;
|
|
1680
|
+
for (const [addressKey, slots] of storage) {
|
|
1681
|
+
const e = entry(unpackAddressKey(addressKey));
|
|
1682
|
+
for (const [slotKey, value] of slots)
|
|
1683
|
+
e.slots.push({
|
|
1684
|
+
key: hexToBytes(unpackSlotKey(slotKey)),
|
|
1685
|
+
value: value ?? new Uint8Array(0),
|
|
1686
|
+
});
|
|
1687
|
+
}
|
|
1688
|
+
return [...entries.values()];
|
|
1689
|
+
}
|
|
1690
|
+
/**
|
|
1691
|
+
* PARSE geth-STYLE STATE OVERRIDES into {@link StateEntry}s. `eth_call` and
|
|
1692
|
+
* `eth_estimateGas` take them as their third parameter:
|
|
1693
|
+
* `{[address]: {balance?, nonce?, code?, state?, stateDiff?}}`, where `state`
|
|
1694
|
+
* REPLACES the account's whole storage and `stateDiff` patches individual
|
|
1695
|
+
* slots. They used to be ignored, which answered a question the caller had not
|
|
1696
|
+
* asked, with no error.
|
|
1697
|
+
*
|
|
1698
|
+
* PARSED BEFORE ANY LEVEL IS OPENED, into values that cannot fail to apply, so
|
|
1699
|
+
* a malformed override is a -32602 naming it (never a raw `SyntaxError` from
|
|
1700
|
+
* `BigInt`), and a refusal never has a half-applied override to unwind.
|
|
1701
|
+
*
|
|
1702
|
+
* ## What is refused
|
|
1703
|
+
*
|
|
1704
|
+
* `state` and `stateDiff` on the same account (geth refuses that too), and any
|
|
1705
|
+
* field this node does not implement (`movePrecompileToAddress`, for one): a
|
|
1706
|
+
* -32602 naming it, never a call run without it.
|
|
1707
|
+
*/
|
|
1708
|
+
function parseStateOverrides(overrides, method) {
|
|
1709
|
+
if (overrides == null)
|
|
1710
|
+
return [];
|
|
1711
|
+
if (typeof overrides !== 'object' || Array.isArray(overrides))
|
|
1712
|
+
throw new RpcError(-32602, `invalid state overrides for ${method}: expected an object keyed by address.`);
|
|
1713
|
+
const supported = new Set([
|
|
1714
|
+
'balance',
|
|
1715
|
+
'nonce',
|
|
1716
|
+
'code',
|
|
1717
|
+
'state',
|
|
1718
|
+
'stateDiff',
|
|
1719
|
+
]);
|
|
1720
|
+
const bad = (what) => {
|
|
1721
|
+
throw new RpcError(-32602, `invalid state override in ${method}: ${what}.`);
|
|
1722
|
+
};
|
|
1723
|
+
const quantity = (v, what) => {
|
|
1724
|
+
if (typeof v !== 'string' || !/^0x[0-9a-fA-F]+$/.test(v))
|
|
1725
|
+
return bad(`${what} must be a hex quantity, got ${JSON.stringify(v)}`);
|
|
1726
|
+
const n = BigInt(v);
|
|
1727
|
+
// A 32-byte word is the widest thing any override field names.
|
|
1728
|
+
if (n >= 1n << 256n)
|
|
1729
|
+
return bad(`${what} does not fit in 32 bytes`);
|
|
1730
|
+
return n;
|
|
1731
|
+
};
|
|
1732
|
+
const data = (v, what) => {
|
|
1733
|
+
if (typeof v !== 'string' || !/^0x([0-9a-fA-F]{2})*$/.test(v))
|
|
1734
|
+
return bad(`${what} must be even-length hex, got ${JSON.stringify(v)}`);
|
|
1735
|
+
return hexToBytes(v);
|
|
1736
|
+
};
|
|
1737
|
+
return Object.entries(overrides).map(([addr, o]) => {
|
|
1738
|
+
if (o == null || typeof o !== 'object' || Array.isArray(o))
|
|
1739
|
+
return bad(`the override for ${addr} must be an object`);
|
|
1740
|
+
for (const k of Object.keys(o))
|
|
1741
|
+
if (!supported.has(k))
|
|
1742
|
+
throw new RpcError(-32602, `state override field not supported by this node: ${addr}.${k} in ${method}. ` +
|
|
1743
|
+
`Supported: balance, nonce, code, state, stateDiff.`);
|
|
1744
|
+
if (o.state != null && o.stateDiff != null)
|
|
1745
|
+
throw new RpcError(-32602, `account ${addr} has both 'state' and 'stateDiff' in ${method}; pass one.`);
|
|
1746
|
+
let address;
|
|
1747
|
+
try {
|
|
1748
|
+
address = createAddressFromString(addr);
|
|
1749
|
+
}
|
|
1750
|
+
catch {
|
|
1751
|
+
return bad(`${JSON.stringify(addr)} is not an address`);
|
|
1752
|
+
}
|
|
1753
|
+
const slotsIn = o.state ?? o.stateDiff;
|
|
1754
|
+
if (slotsIn != null &&
|
|
1755
|
+
(typeof slotsIn !== 'object' || Array.isArray(slotsIn)))
|
|
1756
|
+
return bad(`${addr}.${o.state != null ? 'state' : 'stateDiff'} must be an object of slot -> value`);
|
|
1757
|
+
const slots = Object.entries(slotsIn ?? {}).map(([slot, val]) => {
|
|
1758
|
+
const value = data(val, `${addr} slot ${slot}`);
|
|
1759
|
+
// A slot value is ONE word: a longer one would be silently truncated
|
|
1760
|
+
// to its low 32 bytes by the padding below, so it is refused.
|
|
1761
|
+
if (value.length > 32)
|
|
1762
|
+
return bad(`${addr} slot ${slot} value is longer than 32 bytes`);
|
|
1763
|
+
return {
|
|
1764
|
+
key: setLengthLeft(bigIntToBytes(quantity(slot, `${addr} slot key`)), 32),
|
|
1765
|
+
// SHORTEST FORM, as the EVM itself stores a value: a padded zero
|
|
1766
|
+
// and a missing slot must be the same thing to SSTORE's gas rules.
|
|
1767
|
+
value: unpadBytes(setLengthLeft(value, 32)),
|
|
1768
|
+
};
|
|
1769
|
+
});
|
|
1770
|
+
return {
|
|
1771
|
+
address,
|
|
1772
|
+
balance: o.balance != null
|
|
1773
|
+
? quantity(o.balance, `${addr}.balance`)
|
|
1774
|
+
: undefined,
|
|
1775
|
+
nonce: o.nonce != null ? quantity(o.nonce, `${addr}.nonce`) : undefined,
|
|
1776
|
+
code: o.code != null ? data(o.code, `${addr}.code`) : undefined,
|
|
1777
|
+
clearStorage: o.state != null,
|
|
1778
|
+
slots,
|
|
1779
|
+
absent: false,
|
|
1780
|
+
};
|
|
1781
|
+
});
|
|
1782
|
+
}
|
|
1783
|
+
/**
|
|
1784
|
+
* RUN A READ WITH STATE ENTRIES WRITTEN INTO A CHECKPOINT IT OPENS, and leave
|
|
1785
|
+
* no trace of them. The entries are K's state for a historical call
|
|
1786
|
+
* ({@link historicalEntries}) followed by the caller's state overrides
|
|
1787
|
+
* ({@link parseStateOverrides}), so the overrides land ON TOP of K's state,
|
|
1788
|
+
* as geth applies them on top of the block's. With no entries (a call at the
|
|
1789
|
+
* head without overrides) no level is opened at all.
|
|
1790
|
+
*
|
|
1791
|
+
* ## Why a checkpoint here is safe, given the hazard documented at `serialise`
|
|
1792
|
+
*
|
|
1793
|
+
* The corruption described there needs two executions to interleave on the one
|
|
1794
|
+
* checkpoint stack. This level is opened and closed by the SAME request, inside
|
|
1795
|
+
* the serialisation point, with the read nested strictly inside it: the default
|
|
1796
|
+
* engine's own checkpoint/revert lands on top of this one and pops before this
|
|
1797
|
+
* one does, and revm reads the top of the stack on every access and never
|
|
1798
|
+
* commits. So the stack is `[base, entries, (call)]` and unwinds in order,
|
|
1799
|
+
* whether the read returns, reverts or throws (the `finally`). It is also the
|
|
1800
|
+
* same mechanism `evmCall` already relies on for purity, one level lower. The
|
|
1801
|
+
* derived trie of a `computeStateRoot` node is never involved: change-set recording is
|
|
1802
|
+
* suspended for the read and the level is reverted, so nothing it wrote is
|
|
1803
|
+
* ever named to the trie.
|
|
1804
|
+
*/
|
|
1805
|
+
async function withStateEntries(entries, read) {
|
|
1806
|
+
if (entries.length === 0)
|
|
1807
|
+
return read();
|
|
1808
|
+
await sm.checkpoint();
|
|
1809
|
+
try {
|
|
1810
|
+
for (const e of entries)
|
|
1811
|
+
await applyStateEntry(e);
|
|
1812
|
+
return await read();
|
|
1813
|
+
}
|
|
1814
|
+
finally {
|
|
1815
|
+
await sm.revert();
|
|
1816
|
+
}
|
|
1817
|
+
}
|
|
1818
|
+
/**
|
|
1819
|
+
* WRITE ONE {@link StateEntry} into the top level. The ONE apply step shared by
|
|
1820
|
+
* the RPC override path and the historical path.
|
|
1821
|
+
*
|
|
1822
|
+
* ORDER MATTERS, and each step re-reads the account so no step writes back a
|
|
1823
|
+
* stale copy over the previous one:
|
|
1824
|
+
*
|
|
1825
|
+
* 1. CODE FIRST. `putCode` rewrites the account's `codeHash`, and CREATES an
|
|
1826
|
+
* empty account when there is none, so it must come before
|
|
1827
|
+
* anything that states the account.
|
|
1828
|
+
* 2. THE ACCOUNT: a whole account replaces what `putCode` left (its `codeHash`
|
|
1829
|
+
* is K's own), and `balance` / `nonce` are a read-modify-write on top.
|
|
1830
|
+
* 3. STORAGE: a clear, then the slots.
|
|
1831
|
+
* 4. ABSENT LAST: an empty code entry, then `deleteAccount`. The account must
|
|
1832
|
+
* read as ABSENT, not as a zero account, and two upstream behaviours stand
|
|
1833
|
+
* in the way: `putCode` creates an account (so the code is emptied BEFORE
|
|
1834
|
+
* the delete), and `deleteAccount` leaves the address's code in the code
|
|
1835
|
+
* map, where `eth_getCode` and the default engine's `EXTCODESIZE` would
|
|
1836
|
+
* still see it (so the code is emptied at all). `deleteAccount` also clears
|
|
1837
|
+
* the storage.
|
|
1838
|
+
*/
|
|
1839
|
+
async function applyStateEntry(e) {
|
|
1840
|
+
if (e.code !== undefined)
|
|
1841
|
+
await sm.putCode(e.address, e.code);
|
|
1842
|
+
if (e.account !== undefined)
|
|
1843
|
+
await sm.putAccount(e.address, e.account);
|
|
1844
|
+
if (e.balance !== undefined || e.nonce !== undefined) {
|
|
1845
|
+
const acc = (await sm.getAccount(e.address)) ?? new Account();
|
|
1846
|
+
if (e.balance !== undefined)
|
|
1847
|
+
acc.balance = e.balance;
|
|
1848
|
+
if (e.nonce !== undefined)
|
|
1849
|
+
acc.nonce = e.nonce;
|
|
1850
|
+
await sm.putAccount(e.address, acc);
|
|
1851
|
+
}
|
|
1852
|
+
if (e.clearStorage)
|
|
1853
|
+
await sm.clearStorage(e.address);
|
|
1854
|
+
for (const { key, value } of e.slots)
|
|
1855
|
+
await sm.putStorage(e.address, key, value);
|
|
1856
|
+
if (e.absent) {
|
|
1857
|
+
await sm.putCode(e.address, new Uint8Array(0));
|
|
1858
|
+
await sm.deleteAccount(e.address);
|
|
1859
|
+
}
|
|
1860
|
+
}
|
|
1861
|
+
/**
|
|
1862
|
+
* geth's `eth_call` takes BLOCK overrides as a fourth parameter. This node does
|
|
1863
|
+
* not implement them, so a request carrying them is refused rather than run
|
|
1864
|
+
* against the real block environment.
|
|
1865
|
+
*/
|
|
1866
|
+
function refuseBlockOverrides(blockOverrides, method) {
|
|
1867
|
+
if (blockOverrides == null)
|
|
1868
|
+
return;
|
|
1869
|
+
throw new RpcError(-32602, `block overrides are not supported by this node (${method}, 4th parameter). ` +
|
|
1870
|
+
`The call would otherwise run against the real block environment.`);
|
|
1871
|
+
}
|
|
1121
1872
|
// ---------- eth_call / estimateGas through the ENGINE's READ half (no signing) ----
|
|
1122
1873
|
/**
|
|
1123
1874
|
* The node's single pure-read helper, and the read half of the engine seam: it
|
|
@@ -1130,8 +1881,16 @@ export async function createNode(options = {}) {
|
|
|
1130
1881
|
* `@ethereumjs/evm` engine checkpoints/reverts and resets EIP-2929 warmth
|
|
1131
1882
|
* because that EVM requires it; an engine that cannot commit pays for neither.
|
|
1132
1883
|
* See ./engine.ts.
|
|
1884
|
+
*
|
|
1885
|
+
* `block` is the block the call executes IN: the head's for a call at the
|
|
1886
|
+
* head, block K's stored `Block` for a call pinned to K, so `NUMBER`,
|
|
1887
|
+
* `TIMESTAMP`, `COINBASE`, `PREVRANDAO`, `BASEFEE` and `GASLIMIT` are K's. It
|
|
1888
|
+
* also bounds `BLOCKHASH`: both engines answer zero for any number at or above
|
|
1889
|
+
* the block's own, so a call at K cannot see the hash of K or of anything
|
|
1890
|
+
* after it without any further mechanism (measured on both engines by
|
|
1891
|
+
* `test/historical-call.spec.ts` and its revm twin).
|
|
1133
1892
|
*/
|
|
1134
|
-
async function evmCall(params, budget) {
|
|
1893
|
+
async function evmCall(params, block, budget) {
|
|
1135
1894
|
const from = params.from
|
|
1136
1895
|
? createAddressFromString(params.from)
|
|
1137
1896
|
: createAddressFromString('0x0000000000000000000000000000000000000000');
|
|
@@ -1176,7 +1935,7 @@ export async function createNode(options = {}) {
|
|
|
1176
1935
|
data,
|
|
1177
1936
|
value,
|
|
1178
1937
|
gasLimit,
|
|
1179
|
-
block
|
|
1938
|
+
block,
|
|
1180
1939
|
});
|
|
1181
1940
|
}
|
|
1182
1941
|
/**
|
|
@@ -1250,7 +2009,7 @@ export async function createNode(options = {}) {
|
|
|
1250
2009
|
* points callers HERE for "the number a transaction needs", so the node must
|
|
1251
2010
|
* never refuse a limit it has just recommended.
|
|
1252
2011
|
*/
|
|
1253
|
-
async function estimateGas(p) {
|
|
2012
|
+
async function estimateGas(p, block) {
|
|
1254
2013
|
const dataHex = p.data ?? p.input ?? '0x';
|
|
1255
2014
|
const isCreate = !p.to;
|
|
1256
2015
|
const overhead = intrinsicGas(dataHex, isCreate, common) + accessListGas(p.accessList);
|
|
@@ -1260,6 +2019,11 @@ export async function createNode(options = {}) {
|
|
|
1260
2019
|
// back a number it will not accept. `gas` on an `eth_estimateGas` request is
|
|
1261
2020
|
// geth's cap on the SEARCH ("do not consider limits above this"), which is a
|
|
1262
2021
|
// different thing from `gas` on an `eth_call` ("let it run this long").
|
|
2022
|
+
//
|
|
2023
|
+
// AT A PAST BLOCK K THE CAP IS STILL THIS ONE, not K's own `gasLimit`,
|
|
2024
|
+
// deliberately (historical-eth-call): the number is for a transaction to be
|
|
2025
|
+
// SUBMITTED now, and what it meets is the limit of the blocks this node
|
|
2026
|
+
// mines, whatever K's was. `block` only decides what the probes execute in.
|
|
1263
2027
|
const supplied = p.gas != null ? BigInt(p.gas) : undefined;
|
|
1264
2028
|
const cap = supplied === undefined || supplied > minedBlockGasLimit
|
|
1265
2029
|
? minedBlockGasLimit
|
|
@@ -1277,7 +2041,7 @@ export async function createNode(options = {}) {
|
|
|
1277
2041
|
? `The allowance is the \`gas\` you passed on the request; raise it or omit it.`
|
|
1278
2042
|
: `The allowance is this node's block gas limit; raise it with createNode({blockGasLimit: ...}).`));
|
|
1279
2043
|
}
|
|
1280
|
-
const probe = (limit) => evmCall(p, limit - overhead);
|
|
2044
|
+
const probe = (limit) => evmCall(p, block, limit - overhead);
|
|
1281
2045
|
// 1) THE UPPER BOUND, run first: a request that cannot succeed with all the
|
|
1282
2046
|
// gas there is cannot succeed at all, and the caller learns that here rather
|
|
1283
2047
|
// than from a receipt.
|
|
@@ -1403,18 +2167,39 @@ export async function createNode(options = {}) {
|
|
|
1403
2167
|
const sb = num != null ? blockStore.get(num) : undefined;
|
|
1404
2168
|
return sb ? blockToRpc(sb, Boolean(params[1])) : null;
|
|
1405
2169
|
}
|
|
2170
|
+
// AT THE HEAD OR AT A BLOCK K IN THE `stateHistory` WINDOW, through the one
|
|
2171
|
+
// gate every state read uses (`historicalBlock`). At K the engine runs with
|
|
2172
|
+
// K's stored `Block` on K's state, written into a checkpoint the request
|
|
2173
|
+
// opens and reverts (`historicalEntries`, `withStateEntries`), with the
|
|
2174
|
+
// caller's state overrides applied ON TOP of it.
|
|
1406
2175
|
case 'eth_call': {
|
|
1407
|
-
const
|
|
2176
|
+
const k = historicalBlock(params[1], 'eth_call');
|
|
2177
|
+
refuseBlockOverrides(params[3], 'eth_call');
|
|
2178
|
+
const entries = [
|
|
2179
|
+
...(k === undefined ? [] : historicalEntries(k)),
|
|
2180
|
+
...parseStateOverrides(params[2], 'eth_call'),
|
|
2181
|
+
];
|
|
2182
|
+
const block = blockStore.get(k ?? latestNumber).block;
|
|
2183
|
+
const r = await pureRead(() => withStateEntries(entries, () => evmCall(params[0] ?? {}, block)));
|
|
1408
2184
|
if (r.error)
|
|
1409
2185
|
throw new RpcError(3, 'execution reverted', hex(r.returnValue));
|
|
1410
2186
|
return hex(r.returnValue);
|
|
1411
2187
|
}
|
|
1412
|
-
case 'eth_estimateGas':
|
|
2188
|
+
case 'eth_estimateGas': {
|
|
1413
2189
|
// THE SMALLEST GAS LIMIT AT WHICH THIS REQUEST SUCCEEDS, found by
|
|
1414
2190
|
// re-executing it. The whole method is {@link estimateGas} above,
|
|
1415
2191
|
// 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
|
-
|
|
2192
|
+
// be, and where the request's EIP-2930 access list is charged. The whole
|
|
2193
|
+
// search runs inside ONE checkpoint holding K's state (and the overrides).
|
|
2194
|
+
const k = historicalBlock(params[1], 'eth_estimateGas');
|
|
2195
|
+
refuseBlockOverrides(params[3], 'eth_estimateGas');
|
|
2196
|
+
const entries = [
|
|
2197
|
+
...(k === undefined ? [] : historicalEntries(k)),
|
|
2198
|
+
...parseStateOverrides(params[2], 'eth_estimateGas'),
|
|
2199
|
+
];
|
|
2200
|
+
const block = blockStore.get(k ?? latestNumber).block;
|
|
2201
|
+
return numHex(await pureRead(() => withStateEntries(entries, () => estimateGas(params[0] ?? {}, block))));
|
|
2202
|
+
}
|
|
1418
2203
|
case 'eth_fillTransaction': {
|
|
1419
2204
|
// Fill the missing fields of a tx request and return {tx, raw} like geth
|
|
1420
2205
|
// does (the `raw` is the UNSIGNED serialization — the node has no keys; a
|
|
@@ -1425,8 +2210,9 @@ export async function createNode(options = {}) {
|
|
|
1425
2210
|
const from = p.from
|
|
1426
2211
|
? createAddressFromString(p.from)
|
|
1427
2212
|
: createAddressFromString('0x0000000000000000000000000000000000000000');
|
|
1428
|
-
|
|
1429
|
-
|
|
2213
|
+
// The PENDING nonce, as geth fills it: the account's nonce advanced past
|
|
2214
|
+
// the transactions it already has queued.
|
|
2215
|
+
const nonce = p.nonce != null ? BigInt(p.nonce) : await pendingNonce(from);
|
|
1430
2216
|
const value = p.value != null ? BigInt(p.value) : 0n;
|
|
1431
2217
|
const dataHex = p.data ?? p.input ?? '0x';
|
|
1432
2218
|
const isCreate = !p.to;
|
|
@@ -1443,7 +2229,7 @@ export async function createNode(options = {}) {
|
|
|
1443
2229
|
// `accessListGas` in ./intrinsic-gas.ts.
|
|
1444
2230
|
const gas = p.gas != null
|
|
1445
2231
|
? BigInt(p.gas)
|
|
1446
|
-
: await estimateGas({ ...p, gas: undefined, accessList: undefined });
|
|
2232
|
+
: await pureRead(() => estimateGas({ ...p, gas: undefined, accessList: undefined }, blockStore.get(latestNumber).block));
|
|
1447
2233
|
// Fee fields: legacy iff caller passed gasPrice (and no 1559 fields),
|
|
1448
2234
|
// otherwise EIP-1559 with the node's constant fee market.
|
|
1449
2235
|
const isLegacy = p.gasPrice != null &&
|
|
@@ -1499,28 +2285,53 @@ export async function createNode(options = {}) {
|
|
|
1499
2285
|
};
|
|
1500
2286
|
return { raw: hex(unsigned.serialize()), tx };
|
|
1501
2287
|
}
|
|
2288
|
+
// EVERY state read at a block is gated first: served at the head, served
|
|
2289
|
+
// from the undo log inside the `stateHistory` window (these four only,
|
|
2290
|
+
// through `historicalBlock`), and REFUSED below it, never answered from the
|
|
2291
|
+
// head for a block that is not the head.
|
|
1502
2292
|
case 'eth_getBalance': {
|
|
1503
|
-
const
|
|
2293
|
+
const k = historicalBlock(params[1], 'eth_getBalance');
|
|
2294
|
+
const address = createAddressFromString(params[0]);
|
|
2295
|
+
const acc = k === undefined
|
|
2296
|
+
? await sm.getAccount(address)
|
|
2297
|
+
: await accountAt(k, address);
|
|
1504
2298
|
return numHex(acc?.balance ?? 0n);
|
|
1505
2299
|
}
|
|
1506
2300
|
case 'eth_getTransactionCount': {
|
|
1507
|
-
const
|
|
1508
|
-
|
|
2301
|
+
const k = historicalBlock(params[1], 'eth_getTransactionCount');
|
|
2302
|
+
const address = createAddressFromString(params[0]);
|
|
2303
|
+
if (k !== undefined)
|
|
2304
|
+
return numHex((await accountAt(k, address))?.nonce ?? 0n);
|
|
2305
|
+
// `pending` COUNTS THE QUEUED TRANSACTIONS, the one state read where it
|
|
2306
|
+
// differs from the head: a client picking a nonce asks for `pending`
|
|
2307
|
+
// (viem does by default), and in manual/interval mining two sends
|
|
2308
|
+
// before a mine would otherwise get the SAME nonce, the second then
|
|
2309
|
+
// refused at mine time as a replay.
|
|
2310
|
+
return numHex(params[1] === 'pending'
|
|
2311
|
+
? await pendingNonce(address)
|
|
2312
|
+
: ((await sm.getAccount(address))?.nonce ?? 0n));
|
|
1509
2313
|
}
|
|
1510
2314
|
case 'eth_getCode': {
|
|
1511
|
-
const
|
|
1512
|
-
|
|
2315
|
+
const k = historicalBlock(params[1], 'eth_getCode');
|
|
2316
|
+
const address = createAddressFromString(params[0]);
|
|
2317
|
+
return hex(k === undefined
|
|
2318
|
+
? await sm.getCode(address)
|
|
2319
|
+
: await codeAt(k, address));
|
|
1513
2320
|
}
|
|
1514
2321
|
case 'eth_getStorageAt': {
|
|
2322
|
+
const k = historicalBlock(params[2], 'eth_getStorageAt');
|
|
1515
2323
|
const addr = createAddressFromString(params[0]);
|
|
1516
2324
|
const slot = setLengthLeft(bigIntToBytes(BigInt(params[1])), 32);
|
|
1517
|
-
const val =
|
|
2325
|
+
const val = k === undefined
|
|
2326
|
+
? await sm.getStorage(addr, slot)
|
|
2327
|
+
: await storageAt(k, addr, slot);
|
|
1518
2328
|
return hex(setLengthLeft(val, 32));
|
|
1519
2329
|
}
|
|
1520
2330
|
// ---- Runtime state cheats (anvil/hardhat-style; for tests/local tooling) ----
|
|
1521
2331
|
// These MUTATE state directly (no tx). Honest about being non-standard: they
|
|
1522
|
-
// are `evm_*`-namespaced.
|
|
1523
|
-
// getStateRoot()
|
|
2332
|
+
// are `evm_*`-namespaced. They write the flat state only; on a `computeStateRoot` node the
|
|
2333
|
+
// open record names what they wrote, so the next getStateRoot() (or mined
|
|
2334
|
+
// block) rehashes it into the derived trie.
|
|
1524
2335
|
case 'evm_setBalance': {
|
|
1525
2336
|
// params: [address, valueHex]
|
|
1526
2337
|
const addr = createAddressFromString(params[0]);
|
|
@@ -1541,8 +2352,6 @@ export async function createNode(options = {}) {
|
|
|
1541
2352
|
// params: [address, codeHex]
|
|
1542
2353
|
const addr = createAddressFromString(params[0]);
|
|
1543
2354
|
await sm.putCode(addr, hexToBytes(params[1]));
|
|
1544
|
-
touchedAccounts.add(addr.toString());
|
|
1545
|
-
await commitIfTrie();
|
|
1546
2355
|
return true;
|
|
1547
2356
|
}
|
|
1548
2357
|
case 'evm_setStorageAt': {
|
|
@@ -1550,8 +2359,6 @@ export async function createNode(options = {}) {
|
|
|
1550
2359
|
const addr = createAddressFromString(params[0]);
|
|
1551
2360
|
const slot = setLengthLeft(bigIntToBytes(BigInt(params[1])), 32);
|
|
1552
2361
|
await sm.putStorage(addr, slot, setLengthLeft(hexToBytes(params[2]), 32));
|
|
1553
|
-
touchedAccounts.add(addr.toString());
|
|
1554
|
-
await commitIfTrie();
|
|
1555
2362
|
return true;
|
|
1556
2363
|
}
|
|
1557
2364
|
case 'evm_setAccount': {
|
|
@@ -1569,7 +2376,6 @@ export async function createNode(options = {}) {
|
|
|
1569
2376
|
for (const [slot, val] of Object.entries(a.storage ?? {})) {
|
|
1570
2377
|
await sm.putStorage(addr, setLengthLeft(bigIntToBytes(BigInt(slot)), 32), setLengthLeft(hexToBytes(val), 32));
|
|
1571
2378
|
}
|
|
1572
|
-
await commitIfTrie();
|
|
1573
2379
|
return true;
|
|
1574
2380
|
}
|
|
1575
2381
|
case 'eth_gasPrice':
|
|
@@ -1577,7 +2383,45 @@ export async function createNode(options = {}) {
|
|
|
1577
2383
|
case 'eth_maxPriorityFeePerGas':
|
|
1578
2384
|
return numHex(maxPriorityFeePerGas);
|
|
1579
2385
|
case 'eth_feeHistory': {
|
|
1580
|
-
|
|
2386
|
+
// params: [blockCount, newestBlock, rewardPercentiles]. `newestBlock` is
|
|
2387
|
+
// HONOURED (it used to be ignored, and the window always ended at the head),
|
|
2388
|
+
// and the per-block base fee and gas-used ratio are the stored blocks' own.
|
|
2389
|
+
// Only `reward` stays synthetic: this node's priority fee is a constant.
|
|
2390
|
+
const rawCount = params[0] ?? '0x1';
|
|
2391
|
+
const requested = typeof rawCount === 'number' && Number.isSafeInteger(rawCount)
|
|
2392
|
+
? rawCount
|
|
2393
|
+
: typeof rawCount === 'string' && /^0x[0-9a-fA-F]+$/.test(rawCount)
|
|
2394
|
+
? Number(BigInt(rawCount))
|
|
2395
|
+
: NaN;
|
|
2396
|
+
if (!(requested >= 0))
|
|
2397
|
+
throw new RpcError(-32602, `invalid eth_feeHistory blockCount: ${JSON.stringify(rawCount)}. Expected a hex quantity.`);
|
|
2398
|
+
const newest = resolveBlockTag(params[1] ?? 'latest');
|
|
2399
|
+
if (newest > latestNumber)
|
|
2400
|
+
throw new RpcError(-32000, `request beyond head block: eth_feeHistory newestBlock is ${newest}, but the head of this node is block ${latestNumber}.`);
|
|
2401
|
+
// geth caps a request at 1024 blocks, and nothing precedes block 0.
|
|
2402
|
+
const count = Math.max(0, Math.min(requested, 1024, newest + 1));
|
|
2403
|
+
const oldest = newest - count + 1;
|
|
2404
|
+
const nextBaseFee = (n) => {
|
|
2405
|
+
const sb = blockStore.get(n);
|
|
2406
|
+
return sb
|
|
2407
|
+
? sb.header.baseFeePerGas
|
|
2408
|
+
: numHex(blockEnv?.baseFeePerGas ?? baseFeePerGas);
|
|
2409
|
+
};
|
|
2410
|
+
const ratio = (n) => {
|
|
2411
|
+
// The header's own `gasUsed` is a documented 0x0 placeholder, so the
|
|
2412
|
+
// block's usage is summed from its receipts, which are real.
|
|
2413
|
+
const sb = blockStore.get(n);
|
|
2414
|
+
if (!sb)
|
|
2415
|
+
return 0;
|
|
2416
|
+
let used = 0n;
|
|
2417
|
+
for (const th of sb.header.transactions) {
|
|
2418
|
+
const r = receipts.get(th);
|
|
2419
|
+
if (r)
|
|
2420
|
+
used += BigInt(r.gasUsed);
|
|
2421
|
+
}
|
|
2422
|
+
const limit = BigInt(sb.header.gasLimit);
|
|
2423
|
+
return limit === 0n ? 0 : Number(used) / Number(limit);
|
|
2424
|
+
};
|
|
1581
2425
|
// `reward` must carry ONE entry per requested percentile, per block. Returning a
|
|
1582
2426
|
// single entry regardless of `rewardPercentiles` breaks any caller that asks for
|
|
1583
2427
|
// several and indexes them: rocketh requests [10, 50, 80] and reads index 1 and 2,
|
|
@@ -1587,9 +2431,15 @@ export async function createNode(options = {}) {
|
|
|
1587
2431
|
const percentiles = Array.isArray(params[2]) ? params[2] : [];
|
|
1588
2432
|
const rewardPerBlock = (percentiles.length > 0 ? percentiles : [50]).map(() => numHex(maxPriorityFeePerGas));
|
|
1589
2433
|
return {
|
|
1590
|
-
|
|
1591
|
-
|
|
1592
|
-
|
|
2434
|
+
// An empty window is geth's empty answer, oldestBlock included.
|
|
2435
|
+
oldestBlock: numHex(count === 0 ? 0 : oldest),
|
|
2436
|
+
// One per block in the window PLUS the block after it, which is the
|
|
2437
|
+
// stored block when there is one and the next block this node will mine
|
|
2438
|
+
// when the window ends at the head.
|
|
2439
|
+
baseFeePerGas: count === 0
|
|
2440
|
+
? []
|
|
2441
|
+
: Array.from({ length: count + 1 }, (_, i) => nextBaseFee(oldest + i)),
|
|
2442
|
+
gasUsedRatio: Array.from({ length: count }, (_, i) => ratio(oldest + i)),
|
|
1593
2443
|
reward: Array.from({ length: count }, () => rewardPerBlock),
|
|
1594
2444
|
};
|
|
1595
2445
|
}
|
|
@@ -1629,8 +2479,28 @@ export async function createNode(options = {}) {
|
|
|
1629
2479
|
// is deferred — see tasks/slim-node-eth-getlogs-index.md. Keep this scan as
|
|
1630
2480
|
// the authoritative semantics if/when an index is added.
|
|
1631
2481
|
const f = params[0] ?? {};
|
|
1632
|
-
|
|
1633
|
-
|
|
2482
|
+
// `blockHash` (EIP-234) pins the query to ONE block. It used to be ignored,
|
|
2483
|
+
// which returned the logs of EVERY block. It excludes a range, as in geth,
|
|
2484
|
+
// and a hash no block has is an error rather than an empty result, so a
|
|
2485
|
+
// typo cannot read as "that block emitted nothing".
|
|
2486
|
+
let from;
|
|
2487
|
+
let to;
|
|
2488
|
+
if (f.blockHash != null) {
|
|
2489
|
+
if (f.fromBlock != null || f.toBlock != null)
|
|
2490
|
+
throw new RpcError(-32602, 'invalid eth_getLogs filter: cannot specify both blockHash and fromBlock/toBlock.');
|
|
2491
|
+
const n = blockByHash.get(String(f.blockHash).toLowerCase());
|
|
2492
|
+
if (n === undefined)
|
|
2493
|
+
throw new RpcError(-32000, `unknown block: eth_getLogs blockHash ${f.blockHash} is not a block on this node.`);
|
|
2494
|
+
from = n;
|
|
2495
|
+
to = n;
|
|
2496
|
+
}
|
|
2497
|
+
else {
|
|
2498
|
+
// geth's default for an omitted `fromBlock` is `latest`, not 0; this
|
|
2499
|
+
// node has always defaulted to 0 and that is kept (changing it would
|
|
2500
|
+
// silently shrink every existing consumer's results).
|
|
2501
|
+
from = f.fromBlock != null ? resolveBlockTag(f.fromBlock) : 0;
|
|
2502
|
+
to = f.toBlock != null ? resolveBlockTag(f.toBlock) : latestNumber;
|
|
2503
|
+
}
|
|
1634
2504
|
const addrFilter = f.address
|
|
1635
2505
|
? (Array.isArray(f.address) ? f.address : [f.address]).map((a) => a.toLowerCase())
|
|
1636
2506
|
: null;
|
|
@@ -1686,48 +2556,28 @@ export async function createNode(options = {}) {
|
|
|
1686
2556
|
const accounts = {};
|
|
1687
2557
|
const code = {};
|
|
1688
2558
|
const storage = {};
|
|
1689
|
-
|
|
1690
|
-
|
|
1691
|
-
|
|
1692
|
-
|
|
1693
|
-
|
|
1694
|
-
|
|
1695
|
-
|
|
1696
|
-
|
|
1697
|
-
|
|
1698
|
-
|
|
1699
|
-
|
|
1700
|
-
|
|
1701
|
-
|
|
2559
|
+
// EVERY NODE DUMPS THE SAME WAY, storage included: the account and code
|
|
2560
|
+
// stacks are plain Maps whose TOP frame is the live set, and storage is
|
|
2561
|
+
// per-account with per-checkpoint OVERLAYS, flattened by liveStorage(). A
|
|
2562
|
+
// `computeStateRoot` node runs on this same flat state, and its derived trie is NOT
|
|
2563
|
+
// serialised: `loadState` rebuilds it from the loaded flat state.
|
|
2564
|
+
//
|
|
2565
|
+
// THE SERIALISED FORMAT IS NOT THE INTERNAL LAYOUT and must not follow it:
|
|
2566
|
+
// this output is persisted data (IndexedDB, loadState fixtures) with existing
|
|
2567
|
+
// state behind it. It stays `{address: {slot: value}}` in 0x-hex, and
|
|
2568
|
+
// `test/storage-overlay.spec.ts` asserts it byte-identical to a dump taken
|
|
2569
|
+
// from the pre-overlay flat layout.
|
|
2570
|
+
const accMap = sm.accountStack[sm.accountStack.length - 1];
|
|
2571
|
+
const codeMap = sm.codeStack[sm.codeStack.length - 1];
|
|
2572
|
+
for (const [addr, acc] of accMap) {
|
|
2573
|
+
if (acc !== undefined)
|
|
1702
2574
|
accounts[addr] = hex(acc.serialize());
|
|
1703
|
-
const c = await sm.getCode(address);
|
|
1704
|
-
if (c.length > 0)
|
|
1705
|
-
code[addr] = hex(c);
|
|
1706
|
-
}
|
|
1707
2575
|
}
|
|
1708
|
-
|
|
1709
|
-
|
|
1710
|
-
|
|
1711
|
-
|
|
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
|
-
}
|
|
2576
|
+
for (const [addr, c] of codeMap)
|
|
2577
|
+
code[addr] = hex(c);
|
|
2578
|
+
for (const [addr, slots] of sm.liveStorage()) {
|
|
2579
|
+
for (const [slot, val] of slots)
|
|
2580
|
+
(storage[addr] ??= {})[slot] = hex(val);
|
|
1731
2581
|
}
|
|
1732
2582
|
const blocks = [];
|
|
1733
2583
|
for (let i = 0; i <= latestNumber; i++) {
|
|
@@ -1738,15 +2588,81 @@ export async function createNode(options = {}) {
|
|
|
1738
2588
|
return {
|
|
1739
2589
|
version: 1,
|
|
1740
2590
|
chainId,
|
|
1741
|
-
stateMode,
|
|
1742
2591
|
accounts,
|
|
1743
2592
|
code,
|
|
1744
2593
|
storage,
|
|
1745
2594
|
blocks,
|
|
1746
2595
|
receipts: Object.fromEntries(receipts),
|
|
1747
2596
|
transactions: Object.fromEntries(transactions),
|
|
2597
|
+
// ADDITIVE: a node without `stateHistory` writes no field at all, so its
|
|
2598
|
+
// dump is exactly what it was before history existed.
|
|
2599
|
+
...(stateHistoryBlocks === undefined ? {} : { history: dumpHistory() }),
|
|
1748
2600
|
};
|
|
1749
2601
|
}
|
|
2602
|
+
/**
|
|
2603
|
+
* THE UNDO LOG AS THE DUMP CARRIES IT: one serialized record per sealed block,
|
|
2604
|
+
* oldest first, with the OPEN record FOLDED INTO THE HEAD'S RECORD (older
|
|
2605
|
+
* entry winning).
|
|
2606
|
+
*
|
|
2607
|
+
* WHY FOLD rather than dump the open record as a field of its own: `loadState`
|
|
2608
|
+
* treats the loaded state as a baseline and CLEARS the open record, so the
|
|
2609
|
+
* writes since the head was mined (`evm_set*` cheats, a batch that threw
|
|
2610
|
+
* mid-block) become part of the loaded head, which reads them live exactly as
|
|
2611
|
+
* it did before the dump. What must not be lost is their prior values, which
|
|
2612
|
+
* every read BELOW the head consults after the sealed records. Folding keeps
|
|
2613
|
+
* the per-key earliest-wins union over `undo[K+1] .. undo[head]` + open
|
|
2614
|
+
* identical for every K below the head: a key the head's record already names
|
|
2615
|
+
* keeps that (older) value; a key it does not name did not change in the head
|
|
2616
|
+
* block, so its value at the end of the block before equals its value at the
|
|
2617
|
+
* end of the head, which is what the open record holds. The alternative, a
|
|
2618
|
+
* separate `open` field restored as the open record, would make the next mined
|
|
2619
|
+
* block after a reload own writes made before it, and would have been a second
|
|
2620
|
+
* new field for the same answers.
|
|
2621
|
+
*
|
|
2622
|
+
* With NO record for the head (the head is where history starts: genesis, or a
|
|
2623
|
+
* loaded head nothing was mined on), no block below the head is servable, and
|
|
2624
|
+
* the open record is dropped with nothing lost.
|
|
2625
|
+
*
|
|
2626
|
+
* Decisions around the format (this fold, the contiguous restore, `[]` vs no
|
|
2627
|
+
* key) are recorded in
|
|
2628
|
+
* `work/notes/observations/state-history-persistence-decisions.md`.
|
|
2629
|
+
*/
|
|
2630
|
+
function dumpHistory() {
|
|
2631
|
+
const out = sealed.map((r) => serializeChangeSet(r.number, r.changes));
|
|
2632
|
+
const open = changeSets?.peekChangeSet();
|
|
2633
|
+
const head = out[out.length - 1];
|
|
2634
|
+
if (open !== undefined &&
|
|
2635
|
+
head !== undefined &&
|
|
2636
|
+
head.number === latestNumber)
|
|
2637
|
+
mergeSerializedOlderWins(head, serializeChangeSet(latestNumber, open));
|
|
2638
|
+
return out;
|
|
2639
|
+
}
|
|
2640
|
+
/**
|
|
2641
|
+
* RESTORE the undo log from a dump's `history` (see {@link dumpHistory}), after
|
|
2642
|
+
* the rest of the state is loaded and {@link latestNumber} is the dump's head.
|
|
2643
|
+
* Only the CONTIGUOUS run of records ending at the head block is kept, newest
|
|
2644
|
+
* first, stopping at the first gap and at this node's window (a record at or
|
|
2645
|
+
* below `head - N` serves no block in it, so a dump with more history than N
|
|
2646
|
+
* is truncated to N, and one with less serves what it has). A dump the writer
|
|
2647
|
+
* produced has no gap; a hand-edited or foreign one might, and a read below a
|
|
2648
|
+
* gap would silently be answered from a later state, which is the one thing
|
|
2649
|
+
* the history exists not to do. {@link historyFloor} becomes the block before
|
|
2650
|
+
* the oldest record kept, or the head when none is.
|
|
2651
|
+
*/
|
|
2652
|
+
function loadHistory(history, window) {
|
|
2653
|
+
const kept = [];
|
|
2654
|
+
let expected = latestNumber;
|
|
2655
|
+
for (let i = history.length - 1; i >= 0; i--) {
|
|
2656
|
+
const record = history[i];
|
|
2657
|
+
if (record.number !== expected || record.number <= latestNumber - window)
|
|
2658
|
+
break;
|
|
2659
|
+
kept.push({ number: record.number, changes: deserializeChangeSet(record) });
|
|
2660
|
+
expected--;
|
|
2661
|
+
}
|
|
2662
|
+
kept.reverse();
|
|
2663
|
+
sealed.push(...kept);
|
|
2664
|
+
historyFloor = kept.length > 0 ? kept[0].number - 1 : latestNumber;
|
|
2665
|
+
}
|
|
1750
2666
|
async function loadState(state) {
|
|
1751
2667
|
// Rehydrate accounts/code/storage directly into SimpleStateManager.
|
|
1752
2668
|
for (const [addr, accHex] of Object.entries(state.accounts)) {
|
|
@@ -1824,6 +2740,26 @@ export async function createNode(options = {}) {
|
|
|
1824
2740
|
latestNumber = sh.number;
|
|
1825
2741
|
parentHash = block.hash();
|
|
1826
2742
|
}
|
|
2743
|
+
// A LOADED STATE IS A BASELINE, NOT HISTORY: the record is cleared, like the
|
|
2744
|
+
// construction baselines at genesis. The dump carries no change set for its
|
|
2745
|
+
// head, so there is none to report for it either.
|
|
2746
|
+
changeSets?.takeChangeSet();
|
|
2747
|
+
headBlockChangeSet = undefined;
|
|
2748
|
+
// ...and the history the node held described the state this load just
|
|
2749
|
+
// replaced, so it goes. What replaces it is the dump's own history, when the
|
|
2750
|
+
// dump carries one and this node keeps any: otherwise history starts again at
|
|
2751
|
+
// the loaded head (an older dump, or a node without `stateHistory`, which
|
|
2752
|
+
// ignores the field).
|
|
2753
|
+
sealed.length = 0;
|
|
2754
|
+
historyFloor = latestNumber;
|
|
2755
|
+
if (stateHistoryBlocks !== undefined && state.history !== undefined)
|
|
2756
|
+
loadHistory(state.history, stateHistoryBlocks);
|
|
2757
|
+
// ...and the derived trie described that replaced state too. It is REBUILT
|
|
2758
|
+
// from the loaded flat state, once, exactly as at construction: the dump
|
|
2759
|
+
// carries no trie (it never has to: the flat state determines it), and a
|
|
2760
|
+
// load is a baseline in no change set, so nothing else would put it there.
|
|
2761
|
+
if (computeStateRoot)
|
|
2762
|
+
derivedTrie = await DerivedStateTrie.build(sm, common);
|
|
1827
2763
|
}
|
|
1828
2764
|
// ---------- persistence auto-load on creation ----------
|
|
1829
2765
|
if (options.persistence) {
|
|
@@ -1910,24 +2846,24 @@ export async function createNode(options = {}) {
|
|
|
1910
2846
|
// touches no state. It only relays `request`, which IS serialised, so a request
|
|
1911
2847
|
// arriving over a served port queues exactly like one made directly.
|
|
1912
2848
|
const publicRequest = (args) => serialise(() => persistingRequest(args));
|
|
1913
|
-
|
|
2849
|
+
const node = {
|
|
1914
2850
|
request: publicRequest,
|
|
1915
2851
|
mine: () => serialise(mineAndPersist),
|
|
1916
2852
|
dumpState: () => serialise(dumpState),
|
|
1917
2853
|
loadState: (state) => serialise(() => loadState(state)),
|
|
1918
|
-
|
|
2854
|
+
computeStateRoot,
|
|
1919
2855
|
senderMode,
|
|
1920
2856
|
// Identity only: the engine object itself stays internal, so the reading is a
|
|
1921
2857
|
// plain value that survives a Worker/comlink boundary unchanged.
|
|
1922
2858
|
engine: { id: engine.id },
|
|
1923
2859
|
async getStateRoot() {
|
|
1924
|
-
if (
|
|
1925
|
-
throw new RpcError(-32004,
|
|
2860
|
+
if (!computeStateRoot) {
|
|
2861
|
+
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
2862
|
}
|
|
1927
|
-
// SERIALISED like the rest, because this one READS BY WRITING:
|
|
1928
|
-
//
|
|
1929
|
-
//
|
|
1930
|
-
//
|
|
2863
|
+
// SERIALISED like the rest, because this one READS BY WRITING: it applies
|
|
2864
|
+
// the open record to the derived trie, which reads the COMMITTED flat state
|
|
2865
|
+
// and refuses to run with a checkpoint open, so a root taken
|
|
2866
|
+
// mid-transaction would report a state no block (or cheat) produced.
|
|
1931
2867
|
return serialise(currentStateRoot);
|
|
1932
2868
|
},
|
|
1933
2869
|
onNewHead(cb) {
|
|
@@ -1968,5 +2904,12 @@ export async function createNode(options = {}) {
|
|
|
1968
2904
|
servedPorts.clear();
|
|
1969
2905
|
},
|
|
1970
2906
|
};
|
|
2907
|
+
changeSetProbes.set(node, () => ({
|
|
2908
|
+
recording: changeSets !== undefined,
|
|
2909
|
+
sealedBlocks: sealed.map((r) => r.number),
|
|
2910
|
+
headBlock: headBlockChangeSet,
|
|
2911
|
+
open: changeSets?.peekChangeSet(),
|
|
2912
|
+
}));
|
|
2913
|
+
return node;
|
|
1971
2914
|
}
|
|
1972
2915
|
//# sourceMappingURL=node.js.map
|