Skip to content

Commit

Permalink
statemanager: small refactor to get storage objects for snapsync
Browse files Browse the repository at this point in the history
  • Loading branch information
g11tech authored and holgerd77 committed Sep 20, 2023
1 parent 1f711d0 commit 235a5fe
Showing 1 changed file with 70 additions and 58 deletions.
128 changes: 70 additions & 58 deletions packages/statemanager/src/stateManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import { OriginalStorageCache } from './cache/originalStorageCache.js'

import type { AccountFields, EVMStateManagerInterface, StorageDump } from '@ethereumjs/common'
import type { StorageRange } from '@ethereumjs/common/src'
import type { PrefixedHexString } from '@ethereumjs/util'
import type { DB, PrefixedHexString } from '@ethereumjs/util'
import type { Debugger } from 'debug'
const { debug: createDebugLogger } = debugDefault

Expand Down Expand Up @@ -342,7 +342,7 @@ export class DefaultStateManager implements EVMStateManagerInterface {
}

const key = this._prefixCodeHashes ? concatBytes(CODEHASH_PREFIX, codeHash) : codeHash
await this._trie.database().put(key, value)
await this._getCodeDB().put(key, value)

const keyHex = bytesToUnprefixedHex(key)
this._codeCache[keyHex] = value
Expand Down Expand Up @@ -378,7 +378,7 @@ export class DefaultStateManager implements EVMStateManagerInterface {
if (keyHex in this._codeCache) {
return this._codeCache[keyHex]
} else {
const code = (await this._trie.database().get(key)) ?? new Uint8Array(0)
const code = (await this._getCodeDB().get(key)) ?? new Uint8Array(0)
this._codeCache[keyHex] = code
return code
}
Expand All @@ -389,7 +389,7 @@ export class DefaultStateManager implements EVMStateManagerInterface {
* cache or does a lookup.
* @private
*/
protected async _getStorageTrie(address: Address, account: Account): Promise<Trie> {
protected _getStorageTrie(address: Address, account: Account): Trie {
// from storage cache
const addressHex = bytesToUnprefixedHex(address.bytes)
const storageTrie = this._storageTries[addressHex]
Expand All @@ -406,6 +406,24 @@ export class DefaultStateManager implements EVMStateManagerInterface {
return storageTrie
}

/**
* Gets the storage trie for an account from the storage
* cache or does a lookup.
* @private
*/
protected _getAccountTrie(): Trie {
return this._trie
}

Check warning on line 416 in packages/statemanager/src/stateManager.ts

View check run for this annotation

Codecov / codecov/patch

packages/statemanager/src/stateManager.ts#L415-L416

Added lines #L415 - L416 were not covered by tests

/**
* Gets the storage trie for an account from the storage
* cache or does a lookup.
* @private
*/
protected _getCodeDB(): DB {
return this._trie.database()
}

/**
* Gets the storage value associated with the provided `address` and `key`. This method returns
* the shortest representation of the stored value.
Expand All @@ -431,7 +449,7 @@ export class DefaultStateManager implements EVMStateManagerInterface {
if (!account) {
throw new Error('getContractStorage() called on non-existing account')
}
const trie = await this._getStorageTrie(address, account)
const trie = this._getStorageTrie(address, account)
const value = await trie.get(key)
if (!this._storageCacheSettings.deactivate) {
this._storageCache?.put(address, key, value ?? hexToBytes('0x80'))
Expand All @@ -453,7 +471,7 @@ export class DefaultStateManager implements EVMStateManagerInterface {
): Promise<void> {
// eslint-disable-next-line no-async-promise-executor
return new Promise(async (resolve) => {
const storageTrie = await this._getStorageTrie(address, account)
const storageTrie = this._getStorageTrie(address, account)

modifyTrie(storageTrie, async () => {
// update storage cache
Expand Down Expand Up @@ -654,7 +672,7 @@ export class DefaultStateManager implements EVMStateManagerInterface {
(p) => bytesToHex(p)
)
const storageProof: StorageProof[] = []
const storageTrie = await this._getStorageTrie(address, account)
const storageTrie = this._getStorageTrie(address, account)

for (const storageKey of storageSlots) {
const proof = (await storageTrie.createProof(storageKey)).map((p) => bytesToHex(p))
Expand Down Expand Up @@ -802,23 +820,21 @@ export class DefaultStateManager implements EVMStateManagerInterface {
if (!account) {
throw new Error(`dumpStorage f() can only be called for an existing account`)
}
const trie = this._getStorageTrie(address, account)

return new Promise((resolve, reject) => {
this._getStorageTrie(address, account)
.then((trie) => {
const storage: StorageDump = {}
const stream = trie.createReadStream()
const storage: StorageDump = {}
const stream = trie.createReadStream()

stream.on('data', (val: any) => {
storage[bytesToHex(val.key)] = bytesToHex(val.value)
})
stream.on('end', () => {
resolve(storage)
})
})
.catch((e) => {
reject(e)
})
stream.on('data', (val: any) => {
storage[bytesToHex(val.key)] = bytesToHex(val.value)
})
stream.on('end', () => {
resolve(storage)
})
stream.on('error', (e) => {
reject(e)

Check warning on line 836 in packages/statemanager/src/stateManager.ts

View check run for this annotation

Codecov / codecov/patch

packages/statemanager/src/stateManager.ts#L836

Added line #L836 was not covered by tests
})
})
}

Expand All @@ -841,48 +857,44 @@ export class DefaultStateManager implements EVMStateManagerInterface {
if (!account) {
throw new Error(`Account does not exist.`)
}
const trie = this._getStorageTrie(address, account)

Check warning on line 860 in packages/statemanager/src/stateManager.ts

View check run for this annotation

Codecov / codecov/patch

packages/statemanager/src/stateManager.ts#L860

Added line #L860 was not covered by tests

return new Promise((resolve, reject) => {
this._getStorageTrie(address, account)
.then((trie) => {
let inRange = false
let i = 0

/** Object conforming to {@link StorageRange.storage}. */
const storageMap: StorageRange['storage'] = {}
const stream = trie.createReadStream()

stream.on('data', (val: any) => {
if (!inRange) {
// Check if the key is already in the correct range.
if (bytesToBigInt(val.key) >= startKey) {
inRange = true
} else {
return
}
}

if (i < limit) {
storageMap[bytesToHex(val.key)] = { key: null, value: bytesToHex(val.value) }
i++
} else if (i === limit) {
resolve({
storage: storageMap,
nextKey: bytesToHex(val.key),
})
}
})
let inRange = false
let i = 0

/** Object conforming to {@link StorageRange.storage}. */
const storageMap: StorageRange['storage'] = {}
const stream = trie.createReadStream()

stream.on('data', (val: any) => {
if (!inRange) {
// Check if the key is already in the correct range.
if (bytesToBigInt(val.key) >= startKey) {
inRange = true
} else {
return
}
}

Check warning on line 878 in packages/statemanager/src/stateManager.ts

View check run for this annotation

Codecov / codecov/patch

packages/statemanager/src/stateManager.ts#L863-L878

Added lines #L863 - L878 were not covered by tests

stream.on('end', () => {
resolve({
storage: storageMap,
nextKey: null,
})
if (i < limit) {
storageMap[bytesToHex(val.key)] = { key: null, value: bytesToHex(val.value) }
i++
} else if (i === limit) {
resolve({
storage: storageMap,
nextKey: bytesToHex(val.key),

Check warning on line 886 in packages/statemanager/src/stateManager.ts

View check run for this annotation

Codecov / codecov/patch

packages/statemanager/src/stateManager.ts#L880-L886

Added lines #L880 - L886 were not covered by tests
})
}
})

stream.on('end', () => {
resolve({
storage: storageMap,
nextKey: null,

Check warning on line 894 in packages/statemanager/src/stateManager.ts

View check run for this annotation

Codecov / codecov/patch

packages/statemanager/src/stateManager.ts#L888-L894

Added lines #L888 - L894 were not covered by tests
})
.catch((e) => {
reject(e)
})
})
stream.on('error', (e) => reject(e))

Check warning on line 897 in packages/statemanager/src/stateManager.ts

View check run for this annotation

Codecov / codecov/patch

packages/statemanager/src/stateManager.ts#L896-L897

Added lines #L896 - L897 were not covered by tests
})
}

Expand Down

0 comments on commit 235a5fe

Please sign in to comment.