Skip to content

Commit

Permalink
Remove duplicate code
Browse files Browse the repository at this point in the history
  • Loading branch information
acolytec3 committed Jul 19, 2024
1 parent c7a5e09 commit fad5592
Showing 1 changed file with 10 additions and 22 deletions.
32 changes: 10 additions & 22 deletions packages/statemanager/src/stateManager.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import { Chain, Common } from '@ethereumjs/common'
import { RLP } from '@ethereumjs/rlp'
import { LeafNode, Trie, createTrieFromProof, verifyTrieProof } from '@ethereumjs/trie'
import {
LeafNode,
Trie,
createTrieFromProof,
nibbleTypeToPackedBytes,
verifyTrieProof,
} from '@ethereumjs/trie'
import {
Account,
Address,
Expand Down Expand Up @@ -985,15 +991,6 @@ export class DefaultStateManager implements EVMStateManagerInterface {
* Both are represented as hex strings without the `0x` prefix.
*/
async dumpStorage(address: Address): Promise<StorageDump> {
function nibblestoBytes(arr: Nibbles): Uint8Array {
const buf = new Uint8Array(arr.length / 2)
for (let i = 0; i < buf.length; i++) {
let q = i * 2
buf[i] = (arr[q] << 4) + arr[++q]
}
return buf
}

await this.flush()
const account = await this.getAccount(address)
if (!account) {
Expand All @@ -1007,7 +1004,7 @@ export class DefaultStateManager implements EVMStateManagerInterface {
return trie
.walkAllValueNodes(async (node: TrieNode, _) => {
if (node instanceof LeafNode) {
storage[bytesToHex(nibblestoBytes(node._nibbles))] = bytesToHex(node._value)
storage[bytesToHex(nibbleTypeToPackedBytes(node._nibbles))] = bytesToHex(node._value)
}
})
.then((_) => resolve(storage))
Expand All @@ -1024,15 +1021,6 @@ export class DefaultStateManager implements EVMStateManagerInterface {
The object will also contain `nextKey`, the next (hashed) storage key after the range included in `storage`.
*/
async dumpStorageRange(address: Address, startKey: bigint, limit: number): Promise<StorageRange> {
function nibblestoBytes(arr: Nibbles): Uint8Array {
const buf = new Uint8Array(arr.length / 2)
for (let i = 0; i < buf.length; i++) {
let q = i * 2
buf[i] = (arr[q] << 4) + arr[++q]
}
return buf
}

if (!Number.isSafeInteger(limit) || limit < 0) {
throw new Error(`Limit is not a proper uint.`)
}
Expand All @@ -1053,11 +1041,11 @@ export class DefaultStateManager implements EVMStateManagerInterface {
const storageMap: StorageRange['storage'] = {}

return trie
.walkAllValueNodes(async (node: TrieNode, currentKey: Number[]) => {
.walkAllValueNodes(async (node: TrieNode, currentKey: number[]) => {
if (node instanceof LeafNode) {
// storage[bytesToHex(nibblestoBytes(node._nibbles))] = bytesToHex(node._value)

const keyBytes = nibblestoBytes(currentKey.concat(node.key()))
const keyBytes = nibbleTypeToPackedBytes(currentKey.concat(node.key()))
if (!inRange) {
// Check if the key is already in the correct range.
if (bytesToBigInt(keyBytes) >= startKey) {
Expand Down

0 comments on commit fad5592

Please sign in to comment.