webevm 0.6.0 → 0.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (56) hide show
  1. package/README.md +148 -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 +1125 -141
  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 +180 -34
  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 +37 -11
  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 +21 -8
  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 +4 -1
  45. package/src/derived-trie.ts +214 -0
  46. package/src/engine.ts +2 -3
  47. package/src/index.ts +3 -1
  48. package/src/node.ts +1335 -138
  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 +183 -35
  54. package/src/worker-client.ts +42 -11
  55. package/src/worker-host.ts +21 -8
  56. package/src/worker-rpc-error.ts +139 -0
@@ -21,9 +21,12 @@
21
21
  * node's own `OverlayStorageStateManager` — `SimpleStateManager`'s two PUBLIC
22
22
  * account/code checkpoint stacks plus our storage OVERLAY stack — so this module
23
23
  * reaches past the interface into that one implementation, exactly as `node.ts`'s
24
- * `dumpState` already does in `'none'` mode. The full cost, the alternatives
25
- * that were rejected, and why `stateMode:'trie'` therefore cannot be served are
26
- * in `docs/adr/0005-revm-reads-the-nodes-state-through-simplestatemanagers-stacks.md`.
24
+ * `dumpState` already does. The full cost and the alternatives that were
25
+ * rejected are in
26
+ * `docs/adr/0005-revm-reads-the-nodes-state-through-simplestatemanagers-stacks.md`.
27
+ * Every node, `computeStateRoot: true` included, runs on this state manager (ADR
28
+ * 0014: a root-computing node's trie is derived from it between blocks), so this store serves them
29
+ * all.
27
30
  *
28
31
  * Three consequences worth having in your head before editing this file:
29
32
  *
@@ -138,6 +141,11 @@ export interface SimpleStateStoreOptions {
138
141
  * So this asserts the storage side by the two ACCESSORS the adapter actually
139
142
  * calls plus the overlay stack's own shape, and `test/storage-overlay.spec.ts`
140
143
  * feeds it a stock `SimpleStateManager` (the flat layout) to prove it refuses one.
144
+ *
145
+ * It also requires the synchronous ACCOUNT and CODE writers (`setAccountAt`,
146
+ * `setCodeAt`, `removeAccountAt`), because the store writes through them so the
147
+ * node's per-block change set sees every revm write; `test/change-set.spec.ts`
148
+ * proves a manager lacking them is refused by name.
141
149
  */
142
150
  export function assertStateShape(sm: OverlayStorageStateManager): void {
143
151
  for (const name of ['accountStack', 'codeStack'] as const) {
@@ -171,6 +179,28 @@ export function assertStateShape(sm: OverlayStorageStateManager): void {
171
179
  'See src/state-manager.ts.',
172
180
  );
173
181
  }
182
+ // THE ACCOUNT AND CODE WRITERS, by key and synchronous. The store must write
183
+ // accounts and code THROUGH them rather than into the maps, because that is
184
+ // where the node records the per-block change set (the open record): a direct
185
+ // map write would change state without the record ever hearing of it, which
186
+ // no read and no gas figure can detect. See the header of src/state-manager.ts.
187
+ const writers = ['setAccountAt', 'setCodeAt', 'removeAccountAt'] as const;
188
+ const missingWriters = writers.filter(
189
+ (name) =>
190
+ typeof (sm as unknown as Record<string, unknown>)[name] !== 'function',
191
+ );
192
+ if (missingWriters.length > 0) {
193
+ throw new Error(
194
+ `webevm/revm: the state manager does not expose ${missingWriters
195
+ .map((name) => `${name}()`)
196
+ .join(', ')} (it must have setAccountAt(), setCodeAt() and ` +
197
+ "removeAccountAt()), so it is not the node's OverlayStorageStateManager. " +
198
+ 'The revm engine writes accounts and code synchronously through those ' +
199
+ 'methods, which is where the node records every write it has to be able to ' +
200
+ 'undo; writing the maps directly would change state behind that record. ' +
201
+ 'See src/state-manager.ts.',
202
+ );
203
+ }
174
204
  const overlays = sm.storageOverlays as unknown;
175
205
  const top = Array.isArray(overlays)
176
206
  ? (overlays[overlays.length - 1] as StorageOverlayShape | undefined)
@@ -354,24 +384,28 @@ export class SimpleStateManagerStore implements StateStore {
354
384
  // storage from a previous life at its address. None of that is re-derived here;
355
385
  // re-deriving it is how a host gets EIP-161 subtly wrong.
356
386
  //
357
- // EACH ONE IS ONE MAP WRITE INTO THE TOP OF THE NODE'S OWN STATE, at the same
387
+ // EACH ONE IS ONE WRITE INTO THE TOP OF THE NODE'S OWN STATE, at the same
358
388
  // depth `putAccount` / `putCode` / `putStorage` write to, so a transaction's
359
389
  // cost is proportional to what it touched and a `revert()` above still drops it.
360
- // They are written against the state manager's REPRESENTATION rather than its
361
- // async interface for the reason in the header: revm's commit runs inside a
362
- // synchronous wasm callback and every `StateManagerInterface` method returns a
363
- // `Promise`.
390
+ // They go through the state manager's SYNCHRONOUS by-key writers
391
+ // (`setAccountAt`, `setCodeAt`, `removeAccountAt`, `setStorageAt`,
392
+ // `clearStorageAt`) rather than its async interface, for the reason in the
393
+ // header: revm's commit runs inside a synchronous wasm callback and every
394
+ // `StateManagerInterface` method returns a `Promise`. And through THOSE rather
395
+ // than straight into the maps, because the writers are where the node records
396
+ // the per-block change set: a map write here would change state behind it.
397
+ // `assertStateShape` refuses a state manager without them.
364
398
 
365
399
  setAccount(address: Address, account: AccountState): void {
366
- const accounts = this.#accounts;
400
+ const sm = this.#connected();
367
401
  const key = addrKey(address);
368
- const existing = accounts.get(key);
402
+ const existing = this.#accounts.get(key);
369
403
  // The storage ROOT is carried over rather than computed: `SimpleStateManager`
370
404
  // implements no state-root logic at all, so this field never reflects storage
371
405
  // on either engine (ADR 0009 records the EIP-7610 consequence). Carrying it
372
406
  // keeps a revm-written account byte-identical to an ethereumjs-written one,
373
407
  // which is what `dumpState` serialises.
374
- accounts.set(
408
+ sm.setAccountAt(
375
409
  key,
376
410
  new Account(
377
411
  account.nonce,
@@ -386,7 +420,7 @@ export class SimpleStateManagerStore implements StateStore {
386
420
  // deposited code for it — never for an account that merely received ether.
387
421
  const hashKey = hexOf(account.codeHash);
388
422
  const code = this.#pendingCode.get(hashKey);
389
- if (code !== undefined) this.#code.set(key, code);
423
+ if (code !== undefined) sm.setCodeAt(key, code);
390
424
  }
391
425
 
392
426
  setCode(codeHash: Bytes32, code: Uint8Array): void {
@@ -432,7 +466,7 @@ export class SimpleStateManagerStore implements StateStore {
432
466
  // learned to clear storage on `deleteAccount` (ADR 0007's 2026-08-10
433
467
  // amendment); before that it left a dead contract's slots readable, and the
434
468
  // two engines' post-state differed on exactly this.
435
- this.#accounts.set(addrKey(address), undefined);
469
+ this.#connected().removeAccountAt(addrKey(address));
436
470
  }
437
471
 
438
472
  /**
package/src/revm.ts CHANGED
@@ -39,10 +39,12 @@
39
39
  * checkpoint stacks plus the node's own storage overlays (see
40
40
  * ./revm-state-store.ts, ADR 0005 for the reach-through and ADR 0010 for the
41
41
  * ownership decision), which is the only synchronous view of the node's state
42
- * that exists — so this engine serves `stateMode:'none'` ONLY, and refuses
43
- * `'trie'` at construction rather than at the first opcode. THE NODE KEEPS
44
- * OWNING STATE: nothing is copied into wasm, and a transaction writes back only
45
- * the accounts it touched and the slots that changed.
42
+ * that exists. EVERY node runs on that state, `computeStateRoot: true` included:
43
+ * the trie there is DERIVED from the flat state between blocks and no engine
44
+ * ever reads it (ADR 0014, which lifted this engine's former refusal of trie
45
+ * mode, as a root-computing node was then called).
46
+ * THE NODE KEEPS OWNING STATE: nothing is copied into wasm, and a transaction
47
+ * writes back only the accounts it touched and the slots that changed.
46
48
  *
47
49
  * AND WHICH FORKS. It serves the hardforks whose transaction costing the node's
48
50
  * own arithmetic reproduces AND the PROTOCOL agrees with
@@ -238,26 +240,10 @@ export async function createRevmEngine(
238
240
  id: REVM_ENGINE_ID,
239
241
 
240
242
  connect(context: EngineContext): void {
241
- // A MODE THIS ENGINE CANNOT SERVE, refused out loud and at construction.
242
- // `MerkleStateManager` has no synchronous view of state at any depth, and
243
- // revm's reads must be synchronous, so there is nothing to reach through
244
- // to. A consumer who asked for revm and silently got something else would
245
- // measure the wrong thing forever, so this throws out of `createNode()`
246
- // rather than at the first opcode. See ADR 0005.
247
- if (context.stateMode !== 'none') {
248
- throw new Error(
249
- `webevm/revm: the revm engine cannot serve stateMode:'${context.stateMode}'. ` +
250
- `It reads the node's state SYNCHRONOUSLY through SimpleStateManager's ` +
251
- `checkpoint stacks (stateMode:'none'), and MerkleStateManager has no ` +
252
- `synchronous view at any depth — revm's interpreter has no suspension ` +
253
- `point to await a trie read at. Use stateMode:'none' with this engine, ` +
254
- `or the default @ethereumjs/evm engine with stateMode:'trie'. See ` +
255
- `docs/adr/0005-revm-reads-the-nodes-state-through-simplestatemanagers-stacks.md.`,
256
- );
257
- }
258
- // A HARDFORK THIS ENGINE CANNOT COST, refused the same way and for the same
259
- // reason as the state mode above: revm would run it and charge rules the
260
- // node's own arithmetic does not implement. Unreachable while the node is
243
+ // A HARDFORK THIS ENGINE CANNOT COST, refused out loud and at construction:
244
+ // revm would run it and charge rules the node's own arithmetic does not
245
+ // implement, and a consumer who asked for revm and silently got something
246
+ // else would measure the wrong thing forever. Unreachable while the node is
261
247
  // pinned to Cancun, which is the point — it fires the day that moves,
262
248
  // rather than letting `eth_estimateGas` return a number this engine would
263
249
  // then reject.
@@ -284,7 +270,7 @@ export async function createRevmEngine(
284
270
  chainId = context.common.chainId();
285
271
  nodeCommon = context.common;
286
272
  // The one cast: `StateManagerInterface` does not declare the stacks, but
287
- // `'none'` mode IS the node's `OverlayStorageStateManager`. Cast to the REAL
273
+ // every node's state manager IS its `OverlayStorageStateManager`. Cast to the REAL
288
274
  // type (never to `any`) so every field access below is still typechecked and
289
275
  // a rename in `@ethereumjs/statemanager` — or in our own state manager — is a
290
276
  // compile error here. `bind` also asserts the shape at runtime, which is the