webevm 0.6.0 → 0.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +148 -63
- package/dist/derived-trie.d.ts +38 -0
- package/dist/derived-trie.d.ts.map +1 -0
- package/dist/derived-trie.js +187 -0
- package/dist/derived-trie.js.map +1 -0
- package/dist/engine.d.ts +1 -2
- package/dist/engine.d.ts.map +1 -1
- package/dist/engine.js +2 -3
- package/dist/engine.js.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js.map +1 -1
- package/dist/node.d.ts +45 -0
- package/dist/node.d.ts.map +1 -1
- package/dist/node.js +1125 -141
- package/dist/node.js.map +1 -1
- package/dist/revm-state-store.d.ts +5 -0
- package/dist/revm-state-store.d.ts.map +1 -1
- package/dist/revm-state-store.js +42 -13
- package/dist/revm-state-store.js.map +1 -1
- package/dist/revm.d.ts.map +1 -1
- package/dist/revm.js +5 -19
- package/dist/revm.js.map +1 -1
- package/dist/state-manager.d.ts +256 -18
- package/dist/state-manager.d.ts.map +1 -1
- package/dist/state-manager.js +429 -20
- package/dist/state-manager.js.map +1 -1
- package/dist/storage-keys.d.ts +8 -0
- package/dist/storage-keys.d.ts.map +1 -1
- package/dist/storage-keys.js +18 -2
- package/dist/storage-keys.js.map +1 -1
- package/dist/types.d.ts +180 -34
- package/dist/types.d.ts.map +1 -1
- package/dist/worker-client.d.ts.map +1 -1
- package/dist/worker-client.js +37 -11
- package/dist/worker-client.js.map +1 -1
- package/dist/worker-host.d.ts.map +1 -1
- package/dist/worker-host.js +21 -8
- package/dist/worker-host.js.map +1 -1
- package/dist/worker-rpc-error.d.ts +21 -0
- package/dist/worker-rpc-error.d.ts.map +1 -0
- package/dist/worker-rpc-error.js +121 -0
- package/dist/worker-rpc-error.js.map +1 -0
- package/package.json +4 -1
- package/src/derived-trie.ts +214 -0
- package/src/engine.ts +2 -3
- package/src/index.ts +3 -1
- package/src/node.ts +1335 -138
- package/src/revm-state-store.ts +47 -13
- package/src/revm.ts +11 -25
- package/src/state-manager.ts +515 -21
- package/src/storage-keys.ts +21 -2
- package/src/types.ts +183 -35
- package/src/worker-client.ts +42 -11
- package/src/worker-host.ts +21 -8
- package/src/worker-rpc-error.ts +139 -0
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* worker-rpc-error.ts: HOW AN `RpcError` CROSSES THE WORKER BOUNDARY WHOLE.
|
|
3
|
+
* Shared by the two ends of it: `./worker-host.ts` (the node's thread) turns a
|
|
4
|
+
* thrown `RpcError` into a plain {@link WireRpcError}, and `./worker-client.ts`
|
|
5
|
+
* (the caller's thread) turns that back into a real `RpcError`.
|
|
6
|
+
*
|
|
7
|
+
* WHY IT IS NEEDED. comlink carries a thrown `Error` across as
|
|
8
|
+
* `{message, name, stack}` and nothing else (its built-in `throw` transfer
|
|
9
|
+
* handler), so every `RpcError` a worker-hosted node raised used to reach the
|
|
10
|
+
* caller with `code === undefined` and no `data`: a `-32000` refusal, a `-32601`
|
|
11
|
+
* method not found, and above all a `3 execution reverted` whose `data` is the
|
|
12
|
+
* revert payload a viem client decodes into a custom error. A node served over a
|
|
13
|
+
* port (`node.serveOn`, through `@eip-1193/over-port`) never had the gap, so the
|
|
14
|
+
* two worker transports disagreed, and a worker node was not interchangeable
|
|
15
|
+
* with a main-thread one for anything that branches on `code`.
|
|
16
|
+
*
|
|
17
|
+
* THE CHOICE: THROW A PLAIN OBJECT, NOT AN `Error`, AND TOUCH NO COMLINK STATE.
|
|
18
|
+
* comlink's `throw` handler passes a thrown NON-Error value through structured
|
|
19
|
+
* clone unchanged and rethrows it as-is on the other side. So the host throws
|
|
20
|
+
* `{name: 'RpcError', code, message, data, stack}` in place of the `RpcError`,
|
|
21
|
+
* which crosses with every field, and the client rebuilds the `RpcError` from it.
|
|
22
|
+
* Rejected alternatives, and why:
|
|
23
|
+
*
|
|
24
|
+
* - A custom comlink transfer handler (or replacing the built-in `throw` one).
|
|
25
|
+
* comlink's `transferHandlers` is a MODULE-GLOBAL map, so registering one
|
|
26
|
+
* changes error handling for every other comlink use in the consumer's app,
|
|
27
|
+
* which this package does not own (and a consumer's own `throw` handler, set
|
|
28
|
+
* before or after ours, would silently win or lose).
|
|
29
|
+
* - An envelope (`{ok: value}` / `{error: {...}}`) returned by every proxied
|
|
30
|
+
* method. Also global-state-free, but it rewraps EVERY SUCCESSFUL answer and
|
|
31
|
+
* changes every method's return type on the wire, which breaks the
|
|
32
|
+
* `Required<SlimNode>` typing of the host proxy the client relies on. The
|
|
33
|
+
* thrown object only changes the failure path, and only for `RpcError`.
|
|
34
|
+
*
|
|
35
|
+
* WHAT A DIRECT COMLINK CONSUMER SEES. Someone who `wrap()`s the worker-host api
|
|
36
|
+
* themselves instead of using `createWorkerNode` now gets the plain object as the
|
|
37
|
+
* rejection rather than an `Error` with only a message: it carries `code`,
|
|
38
|
+
* `message` and `data` at the top level, so code reading `e.code` / `e.message`
|
|
39
|
+
* works, and `e instanceof Error` does not. Every other error (a plain `Error`, a
|
|
40
|
+
* `RangeError` from malformed input, the misused-`createEngine` refusal) is
|
|
41
|
+
* left to comlink's own handling, exactly as before.
|
|
42
|
+
*
|
|
43
|
+
* `data` MUST BE STRUCTURED-CLONEABLE, as any value crossing a thread is. Every
|
|
44
|
+
* `data` this package raises is (a hex string, or absent); a non-cloneable one
|
|
45
|
+
* makes comlink report its own "Unserializable return value" instead.
|
|
46
|
+
*/
|
|
47
|
+
import { RpcError } from './types.js';
|
|
48
|
+
/**
|
|
49
|
+
* An `RpcError`, by class or by shape: a consumer's engine may throw one built
|
|
50
|
+
* from ANOTHER copy of this package, which fails `instanceof` but is the same
|
|
51
|
+
* error. The name AND a numeric code are required, since a numeric `code` alone
|
|
52
|
+
* is also what a `DOMException` has.
|
|
53
|
+
*/
|
|
54
|
+
function isRpcError(e) {
|
|
55
|
+
return (e instanceof RpcError ||
|
|
56
|
+
(e instanceof Error &&
|
|
57
|
+
e.name === 'RpcError' &&
|
|
58
|
+
typeof e.code === 'number'));
|
|
59
|
+
}
|
|
60
|
+
function isWireRpcError(e) {
|
|
61
|
+
return (typeof e === 'object' &&
|
|
62
|
+
e !== null &&
|
|
63
|
+
e.webevmRpcError === true &&
|
|
64
|
+
typeof e.code === 'number' &&
|
|
65
|
+
typeof e.message === 'string');
|
|
66
|
+
}
|
|
67
|
+
/** HOST SIDE: an `RpcError` becomes a {@link WireRpcError}; anything else is unchanged. */
|
|
68
|
+
function toWire(e) {
|
|
69
|
+
if (!isRpcError(e))
|
|
70
|
+
return e;
|
|
71
|
+
const wire = {
|
|
72
|
+
webevmRpcError: true,
|
|
73
|
+
name: 'RpcError',
|
|
74
|
+
code: e.code,
|
|
75
|
+
message: e.message,
|
|
76
|
+
data: e.data,
|
|
77
|
+
};
|
|
78
|
+
if (typeof e.stack === 'string')
|
|
79
|
+
wire.stack = e.stack;
|
|
80
|
+
return wire;
|
|
81
|
+
}
|
|
82
|
+
/** CLIENT SIDE: a {@link WireRpcError} becomes a real `RpcError`; anything else is unchanged. */
|
|
83
|
+
function fromWire(e) {
|
|
84
|
+
if (!isWireRpcError(e))
|
|
85
|
+
return e;
|
|
86
|
+
const error = new RpcError(e.code, e.message, e.data);
|
|
87
|
+
// The worker's stack, as comlink keeps it for a plain Error: it is where the
|
|
88
|
+
// error was raised, which is what a reader of the stack wants.
|
|
89
|
+
if (typeof e.stack === 'string')
|
|
90
|
+
error.stack = e.stack;
|
|
91
|
+
return error;
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* HOST SIDE: `fn`, rejecting with a {@link WireRpcError} wherever it would have
|
|
95
|
+
* rejected with an `RpcError`. Every other rejection passes through untouched.
|
|
96
|
+
*/
|
|
97
|
+
export function sendingRpcErrors(fn) {
|
|
98
|
+
return async (...args) => {
|
|
99
|
+
try {
|
|
100
|
+
return await fn(...args);
|
|
101
|
+
}
|
|
102
|
+
catch (e) {
|
|
103
|
+
throw toWire(e);
|
|
104
|
+
}
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* CLIENT SIDE: `fn`, rejecting with a real `RpcError` wherever the worker sent a
|
|
109
|
+
* {@link WireRpcError}. Every other rejection passes through untouched.
|
|
110
|
+
*/
|
|
111
|
+
export function receivingRpcErrors(fn) {
|
|
112
|
+
return async (...args) => {
|
|
113
|
+
try {
|
|
114
|
+
return await fn(...args);
|
|
115
|
+
}
|
|
116
|
+
catch (e) {
|
|
117
|
+
throw fromWire(e);
|
|
118
|
+
}
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
//# sourceMappingURL=worker-rpc-error.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"worker-rpc-error.js","sourceRoot":"","sources":["../src/worker-rpc-error.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6CG;AACH,OAAO,EAAC,QAAQ,EAAC,MAAM,YAAY,CAAC;AAapC;;;;;GAKG;AACH,SAAS,UAAU,CAAC,CAAU;IAC7B,OAAO,CACN,CAAC,YAAY,QAAQ;QACrB,CAAC,CAAC,YAAY,KAAK;YAClB,CAAC,CAAC,IAAI,KAAK,UAAU;YACrB,OAAQ,CAAsB,CAAC,IAAI,KAAK,QAAQ,CAAC,CAClD,CAAC;AACH,CAAC;AAED,SAAS,cAAc,CAAC,CAAU;IACjC,OAAO,CACN,OAAO,CAAC,KAAK,QAAQ;QACrB,CAAC,KAAK,IAAI;QACT,CAAgC,CAAC,cAAc,KAAK,IAAI;QACzD,OAAQ,CAAsB,CAAC,IAAI,KAAK,QAAQ;QAChD,OAAQ,CAAyB,CAAC,OAAO,KAAK,QAAQ,CACtD,CAAC;AACH,CAAC;AAED,2FAA2F;AAC3F,SAAS,MAAM,CAAC,CAAU;IACzB,IAAI,CAAC,UAAU,CAAC,CAAC,CAAC;QAAE,OAAO,CAAC,CAAC;IAC7B,MAAM,IAAI,GAAiB;QAC1B,cAAc,EAAE,IAAI;QACpB,IAAI,EAAE,UAAU;QAChB,IAAI,EAAE,CAAC,CAAC,IAAI;QACZ,OAAO,EAAE,CAAC,CAAC,OAAO;QAClB,IAAI,EAAE,CAAC,CAAC,IAAI;KACZ,CAAC;IACF,IAAI,OAAO,CAAC,CAAC,KAAK,KAAK,QAAQ;QAAE,IAAI,CAAC,KAAK,GAAG,CAAC,CAAC,KAAK,CAAC;IACtD,OAAO,IAAI,CAAC;AACb,CAAC;AAED,iGAAiG;AACjG,SAAS,QAAQ,CAAC,CAAU;IAC3B,IAAI,CAAC,cAAc,CAAC,CAAC,CAAC;QAAE,OAAO,CAAC,CAAC;IACjC,MAAM,KAAK,GAAG,IAAI,QAAQ,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC,CAAC,OAAO,EAAE,CAAC,CAAC,IAAI,CAAC,CAAC;IACtD,6EAA6E;IAC7E,+DAA+D;IAC/D,IAAI,OAAO,CAAC,CAAC,KAAK,KAAK,QAAQ;QAAE,KAAK,CAAC,KAAK,GAAG,CAAC,CAAC,KAAK,CAAC;IACvD,OAAO,KAAK,CAAC;AACd,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,gBAAgB,CAC/B,EAA8B;IAE9B,OAAO,KAAK,EAAE,GAAG,IAAI,EAAE,EAAE;QACxB,IAAI,CAAC;YACJ,OAAO,MAAM,EAAE,CAAC,GAAG,IAAI,CAAC,CAAC;QAC1B,CAAC;QAAC,OAAO,CAAC,EAAE,CAAC;YACZ,MAAM,MAAM,CAAC,CAAC,CAAC,CAAC;QACjB,CAAC;IACF,CAAC,CAAC;AACH,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,kBAAkB,CACjC,EAA8B;IAE9B,OAAO,KAAK,EAAE,GAAG,IAAI,EAAE,EAAE;QACxB,IAAI,CAAC;YACJ,OAAO,MAAM,EAAE,CAAC,GAAG,IAAI,CAAC,CAAC;QAC1B,CAAC;QAAC,OAAO,CAAC,EAAE,CAAC;YACZ,MAAM,QAAQ,CAAC,CAAC,CAAC,CAAC;QACnB,CAAC;IACF,CAAC,CAAC;AACH,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "webevm",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.8.0",
|
|
4
4
|
"description": "Slim, execution-only in-browser EIP-1193 Ethereum node on @ethereumjs/vm. Transport-agnostic core + optional comlink Worker helpers + IndexedDB persistence.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"ethereum",
|
|
@@ -54,9 +54,12 @@
|
|
|
54
54
|
"src"
|
|
55
55
|
],
|
|
56
56
|
"dependencies": {
|
|
57
|
+
"@eip-1193/over-port": "^0.1.0",
|
|
57
58
|
"@ethereumjs/block": "^10.1.2",
|
|
58
59
|
"@ethereumjs/common": "^10.1.2",
|
|
59
60
|
"@ethereumjs/evm": "^10.1.2",
|
|
61
|
+
"@ethereumjs/mpt": "^10.1.2",
|
|
62
|
+
"@ethereumjs/rlp": "^10.1.2",
|
|
60
63
|
"@ethereumjs/statemanager": "^10.1.2",
|
|
61
64
|
"@ethereumjs/tx": "^10.1.2",
|
|
62
65
|
"@ethereumjs/util": "^10.1.2",
|
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* derived-trie.ts: the Merkle-Patricia trie a `computeStateRoot: true` node DERIVES
|
|
3
|
+
* from its flat state, and nothing else.
|
|
4
|
+
*
|
|
5
|
+
* Every node runs on ONE state representation: the flat
|
|
6
|
+
* `OverlayStorageStateManager` (./state-manager.ts). A node with `computeStateRoot: true`
|
|
7
|
+
* ADDITIONALLY keeps this object, which mirrors that state as a real Ethereum
|
|
8
|
+
* state trie so the node can report a real state root. The decision and what it
|
|
9
|
+
* replaced (`MerkleStateManager` as the node's state manager) are in
|
|
10
|
+
* `docs/adr/0014-the-trie-is-derived-from-the-flat-state-not-a-state-manager.md`.
|
|
11
|
+
*
|
|
12
|
+
* ## The shape, which is Ethereum's
|
|
13
|
+
*
|
|
14
|
+
* - ONE account trie, keyed by `keccak(address)` (the trie hashes the key
|
|
15
|
+
* itself: `useKeyHashing`), whose value is `RLP([nonce, balance, storageRoot,
|
|
16
|
+
* codeHash])`.
|
|
17
|
+
* - ONE storage trie per account, keyed by `keccak(slot)`, whose value is
|
|
18
|
+
* `RLP(value)` with the value in shortest form; a zero value is ABSENT.
|
|
19
|
+
* - An account's `storageRoot` is taken from ITS storage trie at the moment the
|
|
20
|
+
* account is written into the account trie. The flat state's own `Account`
|
|
21
|
+
* objects never carry a real `storageRoot` (`SimpleStateManager` has no trie),
|
|
22
|
+
* and that is deliberate: the collision rule every engine applies reads it, and
|
|
23
|
+
* every node follows the reference spec there (see the ADR).
|
|
24
|
+
*
|
|
25
|
+
* ## The flat state is the source, the change set is the index
|
|
26
|
+
*
|
|
27
|
+
* {@link DerivedStateTrie.build} reads the WHOLE flat state once (at
|
|
28
|
+
* construction, after the genesis baselines, and at the end of `loadState`).
|
|
29
|
+
* From then on {@link DerivedStateTrie.apply} takes a change set (the open record
|
|
30
|
+
* of ./state-manager.ts) and uses it ONLY AS A LIST OF KEYS: every value is read
|
|
31
|
+
* from the flat state as it stands now, never from the record, whose values are
|
|
32
|
+
* the PRIOR ones. That makes `apply` idempotent, which is what lets the node
|
|
33
|
+
* apply the open record for a `getStateRoot()` between blocks and again, as part
|
|
34
|
+
* of the block's record, when the next block is mined.
|
|
35
|
+
*
|
|
36
|
+
* A storage CLEAR (`storageCleared`) replaces the account's storage trie with an
|
|
37
|
+
* empty one before its named slots are re-read: the clear recorded every slot
|
|
38
|
+
* the account held and every later write records its own slot, so the slots the
|
|
39
|
+
* record names are a superset of what can be live afterwards.
|
|
40
|
+
*
|
|
41
|
+
* ## It never runs during execution
|
|
42
|
+
*
|
|
43
|
+
* Both entry points require NO checkpoint to be open (they read the bottom of
|
|
44
|
+
* the flat state's stacks) and the node calls them only at the end of a mined
|
|
45
|
+
* block and inside `getStateRoot()`, both inside the serialisation point. The
|
|
46
|
+
* trie is async (`@ethereumjs/mpt`), and that costs no opcode anything because
|
|
47
|
+
* no engine ever reads it: this is why revm can run on a root-computing node.
|
|
48
|
+
*
|
|
49
|
+
* ## Old nodes are pruned
|
|
50
|
+
*
|
|
51
|
+
* Each trie has its OWN in-memory database and runs with `useNodePruning`, so a
|
|
52
|
+
* node replaced by a write is deleted rather than retained. The node reports the
|
|
53
|
+
* root of its head and nothing older (history is `stateHistory`'s undo log, not
|
|
54
|
+
* old tries), so a retained node could never be read again; keeping them would
|
|
55
|
+
* make a long-lived in-browser node's memory grow with every block. A dropped
|
|
56
|
+
* storage trie (account deleted, storage cleared) is simply released.
|
|
57
|
+
*/
|
|
58
|
+
import {MerklePatriciaTrie} from '@ethereumjs/mpt';
|
|
59
|
+
import {RLP} from '@ethereumjs/rlp';
|
|
60
|
+
import type {Common} from '@ethereumjs/common';
|
|
61
|
+
import {Account, equalsBytes, hexToBytes, unpadBytes} from '@ethereumjs/util';
|
|
62
|
+
import type {
|
|
63
|
+
ChangeSet,
|
|
64
|
+
OverlayStorageStateManager,
|
|
65
|
+
PackedAddressKey,
|
|
66
|
+
} from './state-manager.js';
|
|
67
|
+
import {
|
|
68
|
+
packAddressKey,
|
|
69
|
+
packedKeyBytes,
|
|
70
|
+
unpackAddressKey,
|
|
71
|
+
} from './storage-keys.js';
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* How many {@link DerivedStateTrie} objects this module has ever created. TEST
|
|
75
|
+
* ONLY: it is how a node without `computeStateRoot` is PROVEN to do no trie work (not one is created
|
|
76
|
+
* by a node that does not compute roots), which a flag on the node could only
|
|
77
|
+
* claim.
|
|
78
|
+
*/
|
|
79
|
+
let created = 0;
|
|
80
|
+
|
|
81
|
+
/** See {@link created}. Not exported from `src/index.ts`. */
|
|
82
|
+
export function derivedTriesCreatedForTests(): number {
|
|
83
|
+
return created;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export class DerivedStateTrie {
|
|
87
|
+
private readonly accounts: MerklePatriciaTrie;
|
|
88
|
+
/** Only accounts whose storage is non-empty have an entry. */
|
|
89
|
+
private readonly storage = new Map<PackedAddressKey, MerklePatriciaTrie>();
|
|
90
|
+
|
|
91
|
+
private constructor(
|
|
92
|
+
private readonly flat: OverlayStorageStateManager,
|
|
93
|
+
private readonly common: Common,
|
|
94
|
+
) {
|
|
95
|
+
created++;
|
|
96
|
+
this.accounts = this.newTrie();
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Build the trie from the WHOLE flat state. O(state), paid once per
|
|
101
|
+
* construction or load, never per block.
|
|
102
|
+
*/
|
|
103
|
+
static async build(
|
|
104
|
+
flat: OverlayStorageStateManager,
|
|
105
|
+
common: Common,
|
|
106
|
+
): Promise<DerivedStateTrie> {
|
|
107
|
+
const trie = new DerivedStateTrie(flat, common);
|
|
108
|
+
trie.requireNoCheckpoint('build');
|
|
109
|
+
// Storage first, so every account is written with its final storageRoot.
|
|
110
|
+
// The bottom overlay IS committed storage and holds no tombstones
|
|
111
|
+
// (./state-manager.ts), so its `written` map is the whole of it.
|
|
112
|
+
for (const [addressKey, slots] of flat.storageOverlays[0].written) {
|
|
113
|
+
const st = trie.newTrie();
|
|
114
|
+
for (const [slotKey, value] of slots)
|
|
115
|
+
await putSlot(st, packedKeyBytes(slotKey), value);
|
|
116
|
+
if (!isEmptyTrie(st)) trie.storage.set(addressKey, st);
|
|
117
|
+
}
|
|
118
|
+
for (const [addressHex, account] of flat.accountStack[0])
|
|
119
|
+
if (account !== undefined) await trie.writeAccount(addressHex);
|
|
120
|
+
return trie;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Bring the trie up to date with every key `changes` names, reading each
|
|
125
|
+
* key's CURRENT value from the flat state. Idempotent. O(keys named).
|
|
126
|
+
*/
|
|
127
|
+
async apply(changes: ChangeSet): Promise<void> {
|
|
128
|
+
this.requireNoCheckpoint('apply');
|
|
129
|
+
const dirty = new Set<string>();
|
|
130
|
+
for (const addressHex of changes.accounts.keys()) dirty.add(addressHex);
|
|
131
|
+
for (const addressHex of changes.code.keys()) dirty.add(addressHex);
|
|
132
|
+
for (const addressKey of changes.storageCleared) {
|
|
133
|
+
this.storage.delete(addressKey);
|
|
134
|
+
dirty.add(unpackAddressKey(addressKey));
|
|
135
|
+
}
|
|
136
|
+
for (const [addressKey, slots] of changes.storage) {
|
|
137
|
+
const st = this.storage.get(addressKey) ?? this.newTrie();
|
|
138
|
+
for (const slotKey of slots.keys()) {
|
|
139
|
+
const value = this.flat.storageAt(addressKey, slotKey);
|
|
140
|
+
await putSlot(st, packedKeyBytes(slotKey), value);
|
|
141
|
+
}
|
|
142
|
+
if (isEmptyTrie(st)) this.storage.delete(addressKey);
|
|
143
|
+
else this.storage.set(addressKey, st);
|
|
144
|
+
dirty.add(unpackAddressKey(addressKey));
|
|
145
|
+
}
|
|
146
|
+
for (const addressHex of dirty) await this.writeAccount(addressHex);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/** The state root: the account trie's root. */
|
|
150
|
+
root(): Uint8Array {
|
|
151
|
+
return this.accounts.root();
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Write one account into the account trie as the flat state holds it now,
|
|
156
|
+
* with its `storageRoot` from its storage trie, or delete it when the flat
|
|
157
|
+
* state has none.
|
|
158
|
+
*/
|
|
159
|
+
private async writeAccount(addressHex: string): Promise<void> {
|
|
160
|
+
const key = hexToBytes(addressHex as `0x${string}`);
|
|
161
|
+
const account = this.flat.accountStack[0].get(addressHex as `0x${string}`);
|
|
162
|
+
if (account === undefined) {
|
|
163
|
+
await this.accounts.del(key);
|
|
164
|
+
return;
|
|
165
|
+
}
|
|
166
|
+
const st = this.storage.get(packAddressKey(key));
|
|
167
|
+
const derived = new Account(
|
|
168
|
+
account.nonce,
|
|
169
|
+
account.balance,
|
|
170
|
+
st === undefined ? undefined : st.root(),
|
|
171
|
+
account.codeHash,
|
|
172
|
+
);
|
|
173
|
+
await this.accounts.put(key, derived.serialize());
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
private newTrie(): MerklePatriciaTrie {
|
|
177
|
+
return new MerklePatriciaTrie({
|
|
178
|
+
useKeyHashing: true,
|
|
179
|
+
useNodePruning: true,
|
|
180
|
+
common: this.common,
|
|
181
|
+
});
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* The trie reads the BOTTOM of the flat state's stacks, which is committed
|
|
186
|
+
* state only while no checkpoint is open. Anything else would put a
|
|
187
|
+
* mid-execution state into the root, so it is refused rather than guessed at.
|
|
188
|
+
*/
|
|
189
|
+
private requireNoCheckpoint(what: string): void {
|
|
190
|
+
if (
|
|
191
|
+
this.flat.accountStack.length !== 1 ||
|
|
192
|
+
this.flat.storageOverlays.length !== 1
|
|
193
|
+
)
|
|
194
|
+
throw new Error(
|
|
195
|
+
`webevm: the derived state trie was asked to ${what} with a checkpoint ` +
|
|
196
|
+
'open; a state root is computed only between executions.',
|
|
197
|
+
);
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/** One slot into a storage trie: shortest form, RLP-encoded; zero deletes. */
|
|
202
|
+
async function putSlot(
|
|
203
|
+
trie: MerklePatriciaTrie,
|
|
204
|
+
slot: Uint8Array,
|
|
205
|
+
value: Uint8Array | undefined,
|
|
206
|
+
): Promise<void> {
|
|
207
|
+
const short = value === undefined ? undefined : unpadBytes(value);
|
|
208
|
+
if (short === undefined || short.length === 0) await trie.del(slot);
|
|
209
|
+
else await trie.put(slot, RLP.encode(short));
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
function isEmptyTrie(trie: MerklePatriciaTrie): boolean {
|
|
213
|
+
return equalsBytes(trie.root(), trie.EMPTY_TRIE_ROOT);
|
|
214
|
+
}
|
package/src/engine.ts
CHANGED
|
@@ -237,8 +237,7 @@ function effectiveGasPrice(tx: TypedTransaction, blockBaseFee: bigint): bigint {
|
|
|
237
237
|
* engine to mine on, so this is a missing capability the node cannot supply;
|
|
238
238
|
* 3. its `connect(context)` throws, either because it cannot initialise (no
|
|
239
239
|
* wasm, no memory) or because it refuses this node's configuration (the
|
|
240
|
-
* revm engine refuses
|
|
241
|
-
* `MerkleStateManager` to read through).
|
|
240
|
+
* revm engine refuses a hardfork it cannot cost, ADR 0008).
|
|
242
241
|
*
|
|
243
242
|
* The engine's own message is preserved verbatim inside the thrown error's
|
|
244
243
|
* message (not only as `cause`), because the engine is the only party that
|
|
@@ -279,7 +278,7 @@ export async function connectEngine(
|
|
|
279
278
|
throw new Error(
|
|
280
279
|
`webevm: the engine '${engine.id}' could not be connected, so the node was NOT created. ` +
|
|
281
280
|
`It is deliberately NOT replaced by the default @ethereumjs/evm engine: that node would work, return correct results, and run at a completely different speed from the one you asked for, silently. ` +
|
|
282
|
-
`Fix the configuration
|
|
281
|
+
`Fix the engine's configuration or pass a different engine. Cause: ${message(err)}`,
|
|
283
282
|
{cause: err},
|
|
284
283
|
);
|
|
285
284
|
}
|
package/src/index.ts
CHANGED
|
@@ -12,10 +12,11 @@ export {
|
|
|
12
12
|
} from './persistence.js';
|
|
13
13
|
export type {
|
|
14
14
|
SlimNode,
|
|
15
|
+
ServedPort,
|
|
15
16
|
NodeOptions,
|
|
16
17
|
MiningConfig,
|
|
17
|
-
StateMode,
|
|
18
18
|
SenderMode,
|
|
19
|
+
StateHistoryOptions,
|
|
19
20
|
// The engine seam: implement `Engine` to put a different EVM behind the node —
|
|
20
21
|
// its reads (`call`) and its transactions (`transact`). Types only, so the core
|
|
21
22
|
// never imports a non-default engine.
|
|
@@ -32,6 +33,7 @@ export type {
|
|
|
32
33
|
PersistenceAdapter,
|
|
33
34
|
RequestArguments,
|
|
34
35
|
SerializedState,
|
|
36
|
+
SerializedHistoryBlock,
|
|
35
37
|
} from './types.js';
|
|
36
38
|
export {RpcError} from './types.js';
|
|
37
39
|
// The revm engine is intentionally NOT re-exported here either, for the same
|