Skip to content

Commit 8e14f6e

Browse files
committed
Replace readable-stream with web stream api
1 parent e0ac781 commit 8e14f6e

File tree

3 files changed

+52
-89
lines changed

3 files changed

+52
-89
lines changed

packages/statemanager/src/stateManager.ts

Lines changed: 22 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -962,21 +962,14 @@ export class DefaultStateManager implements EVMStateManagerInterface {
962962
throw new Error(`dumpStorage f() can only be called for an existing account`)
963963
}
964964
const trie = this._getStorageTrie(address, account)
965+
const storage: StorageDump = {}
966+
const stream = trie.createReadStream()
965967

966-
return new Promise((resolve, reject) => {
967-
const storage: StorageDump = {}
968-
const stream = trie.createReadStream()
968+
for await (const chunk of stream) {
969+
if (chunk !== null) storage[bytesToHex(chunk.key)] = bytesToHex(chunk.value)
970+
}
969971

970-
stream.on('data', (val: any) => {
971-
storage[bytesToHex(val.key)] = bytesToHex(val.value)
972-
})
973-
stream.on('end', () => {
974-
resolve(storage)
975-
})
976-
stream.on('error', (e) => {
977-
reject(e)
978-
})
979-
})
972+
return storage
980973
}
981974

982975
/**
@@ -999,44 +992,33 @@ export class DefaultStateManager implements EVMStateManagerInterface {
999992
throw new Error(`Account does not exist.`)
1000993
}
1001994
const trie = this._getStorageTrie(address, account)
1002-
1003-
return new Promise((resolve, reject) => {
1004-
let inRange = false
1005-
let i = 0
1006-
1007-
/** Object conforming to {@link StorageRange.storage}. */
1008-
const storageMap: StorageRange['storage'] = {}
1009-
const stream = trie.createReadStream()
1010-
1011-
stream.on('data', (val: any) => {
995+
let inRange = false
996+
let i = 0
997+
998+
/** Object conforming to {@link StorageRange.storage}. */
999+
const storageMap: StorageRange['storage'] = {}
1000+
const stream = trie.createReadStream()
1001+
for await (const chunk of stream) {
1002+
if (chunk !== null) {
10121003
if (!inRange) {
10131004
// Check if the key is already in the correct range.
1014-
if (bytesToBigInt(val.key) >= startKey) {
1005+
if (bytesToBigInt(chunk.key) >= startKey) {
10151006
inRange = true
10161007
} else {
1017-
return
1008+
continue
10181009
}
10191010
}
1020-
10211011
if (i < limit) {
1022-
storageMap[bytesToHex(val.key)] = { key: null, value: bytesToHex(val.value) }
1012+
storageMap[bytesToHex(chunk.key)] = { key: null, value: bytesToHex(chunk.value) }
10231013
i++
10241014
} else if (i === limit) {
1025-
resolve({
1015+
return {
10261016
storage: storageMap,
1027-
nextKey: bytesToHex(val.key),
1028-
})
1017+
nextKey: bytesToHex(chunk.key),
1018+
}
10291019
}
1030-
})
1031-
1032-
stream.on('end', () => {
1033-
resolve({
1034-
storage: storageMap,
1035-
nextKey: null,
1036-
})
1037-
})
1038-
stream.on('error', (e) => reject(e))
1039-
})
1020+
}
1021+
}
10401022
}
10411023

10421024
/**

packages/trie/src/trie.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ import type {
4343
import type { OnFound } from './util/asyncWalk.js'
4444
import type { BatchDBOp, DB, PutBatch } from '@ethereumjs/util'
4545
import type { Debugger } from 'debug'
46+
// eslint-disable-next-line implicit-dependencies/no-implicit
47+
import type { ReadableStream } from 'node:stream/web'
4648

4749
interface Path {
4850
node: TrieNode | null
@@ -1080,8 +1082,8 @@ export class Trie {
10801082
* The `data` event is given an `Object` that has two properties; the `key` and the `value`. Both should be Uint8Arrays.
10811083
* @return Returns a [stream](https://nodejs.org/dist/latest-v12.x/docs/api/stream.html#stream_class_stream_readable) of the contents of the `trie`
10821084
*/
1083-
createReadStream(): ReadStream {
1084-
return new ReadStream(this)
1085+
createReadStream(): ReadableStream {
1086+
return ReadStream(this)
10851087
}
10861088

10871089
/**

packages/trie/src/util/readStream.ts

Lines changed: 26 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// eslint-disable-next-line implicit-dependencies/no-implicit
22
import { ReadableStream } from 'node:stream/web'
3-
import { Readable } from 'readable-stream'
43

54
import { BranchNode, LeafNode } from '../node/index.js'
65

@@ -9,53 +8,10 @@ import { nibblestoBytes } from './nibbles.js'
98
import type { Trie } from '../trie.js'
109
import type { FoundNodeFunction } from '../types.js'
1110

12-
export class TrieReadStream extends Readable {
13-
private trie: Trie
14-
private _started: boolean
15-
16-
constructor(trie: Trie) {
17-
super({ objectMode: true })
18-
19-
const s = new ReadableStream()
20-
void s.cancel()
21-
this.trie = trie
22-
this._started = false
23-
}
24-
25-
async _read() {
26-
if (this._started) {
27-
return
28-
}
29-
this._started = true
30-
try {
31-
await this._findValueNodes(async (_, node, key, walkController) => {
32-
if (node !== null) {
33-
this.push({
34-
key: nibblestoBytes(key),
35-
value: node.value(),
36-
})
37-
walkController.allChildren(node, key)
38-
}
39-
})
40-
} catch (error: any) {
41-
if (error.message === 'Missing node in DB') {
42-
// pass
43-
} else {
44-
throw error
45-
}
46-
}
47-
this.push(null)
48-
}
49-
50-
/**
51-
* Finds all nodes that store k,v values
52-
* called by {@link TrieReadStream}
53-
* @private
54-
*/
55-
async _findValueNodes(onFound: FoundNodeFunction): Promise<void> {
11+
export function TrieReadStream(trie: Trie) {
12+
const _findValueNodes = async (onFound: FoundNodeFunction): Promise<void> => {
5613
const outerOnFound: FoundNodeFunction = async (nodeRef, node, key, walkController) => {
5714
let fullKey = key
58-
5915
if (node instanceof LeafNode) {
6016
fullKey = key.concat(node.key())
6117
// found leaf node!
@@ -70,6 +26,29 @@ export class TrieReadStream extends Readable {
7026
}
7127
}
7228
}
73-
await this.trie.walkTrie(this.trie.root(), outerOnFound)
29+
await trie.walkTrie(trie.root(), outerOnFound)
7430
}
31+
32+
return new ReadableStream({
33+
async start(controller) {
34+
try {
35+
await _findValueNodes(async (_, node, key, walkController) => {
36+
if (node !== null) {
37+
controller.enqueue({
38+
key: nibblestoBytes(key),
39+
value: node.value(),
40+
})
41+
walkController.allChildren(node, key)
42+
}
43+
})
44+
} catch (error: any) {
45+
if (error.message === 'Missing node in DB') {
46+
// pass
47+
} else {
48+
throw error
49+
}
50+
}
51+
controller.close()
52+
},
53+
})
7554
}

0 commit comments

Comments
 (0)