Skip to content

Commit

Permalink
Rename DefaultStateManager to MerkleStateManager (#3641)
Browse files Browse the repository at this point in the history
* Rename DefaultStateManager to MerkleStateManager

* Make naming change consistent throughout monorepo

* Rename DefaultSateManagerOpts to MerkleStateManagerOpts

* Rename stateManager.ts to merkleStateManager.ts

* Fix lint issues

* Update packages/statemanager/test/statelessVerkleStateManager.spec.ts

* statemanager: cleanup

---------

Co-authored-by: Gabriel Rocheleau <[email protected]>
  • Loading branch information
scorbajio and gabrocheleau authored Sep 8, 2024
1 parent 4e4082a commit 8a0afb0
Show file tree
Hide file tree
Showing 44 changed files with 243 additions and 241 deletions.
10 changes: 5 additions & 5 deletions packages/client/src/execution/vmexecution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { getGenesis } from '@ethereumjs/genesis'
import {
CacheType,
Caches,
DefaultStateManager,
MerkleStateManager,
StatelessVerkleStateManager,
} from '@ethereumjs/statemanager'
import { createTrie } from '@ethereumjs/trie'
Expand Down Expand Up @@ -161,7 +161,7 @@ export class VMExecution extends Execution {
this.config.logger.info(`Initializing code cache size=${this.config.codeCache}`)
this.config.logger.info(`Initializing trie cache size=${this.config.trieCache}`)

const stateManager = new DefaultStateManager({
const stateManager = new MerkleStateManager({
trie,
prefixStorageTrieKeys: this.config.prefixStorageTrieKeys,
caches: new Caches({
Expand Down Expand Up @@ -683,7 +683,7 @@ export class VMExecution extends Execution {
(!this.config.execCommon.gteHardfork(Hardfork.Osaka) &&
typeof this.vm.stateManager.initVerkleExecutionWitness === 'function') ||
(this.config.execCommon.gteHardfork(Hardfork.Osaka) &&
this.vm.stateManager instanceof DefaultStateManager)
this.vm.stateManager instanceof MerkleStateManager)
) {
throw Error(
`Invalid vm stateManager type=${typeof this.vm.stateManager} for fork=${
Expand Down Expand Up @@ -1028,8 +1028,8 @@ export class VMExecution extends Execution {
}

stats() {
if (this._statsVM instanceof DefaultStateManager) {
const sm = this._statsVM.stateManager as DefaultStateManager
if (this._statsVM instanceof MerkleStateManager) {
const sm = this._statsVM.stateManager as MerkleStateManager
const deactivatedStats = { size: 0, reads: 0, hits: 0, writes: 0 }
let stats
stats = sm['_caches']?.account?.stats() ?? deactivatedStats
Expand Down
8 changes: 4 additions & 4 deletions packages/client/src/sync/fetcher/accountfetcher.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DefaultStateManager } from '@ethereumjs/statemanager'
import { MerkleStateManager } from '@ethereumjs/statemanager'
import { verifyTrieRangeProof } from '@ethereumjs/trie'
import {
BIGINT_0,
Expand Down Expand Up @@ -54,7 +54,7 @@ export interface AccountFetcherOptions extends FetcherOptions {
/** Destroy fetcher once all tasks are done */
destroyWhenDone?: boolean

stateManager?: DefaultStateManager
stateManager?: MerkleStateManager

fetcherDoneFlags?: SnapFetcherDoneFlags
}
Expand All @@ -69,7 +69,7 @@ export type JobTask = {

export class AccountFetcher extends Fetcher<JobTask, AccountData[], AccountData> {
protected debug: Debugger
stateManager: DefaultStateManager
stateManager: MerkleStateManager
accountTrie: Trie

root: Uint8Array
Expand All @@ -96,7 +96,7 @@ export class AccountFetcher extends Fetcher<JobTask, AccountData[], AccountData>
this.first = options.first
this.count = options.count ?? BIGINT_2EXP256 - this.first

this.stateManager = options.stateManager ?? new DefaultStateManager()
this.stateManager = options.stateManager ?? new MerkleStateManager()
this.accountTrie = this.stateManager['_getAccountTrie']()

this.debug = debugDefault('client:AccountFetcher')
Expand Down
8 changes: 4 additions & 4 deletions packages/client/src/sync/fetcher/bytecodefetcher.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CODEHASH_PREFIX, DefaultStateManager } from '@ethereumjs/statemanager'
import { CODEHASH_PREFIX, MerkleStateManager } from '@ethereumjs/statemanager'
import {
BIGINT_0,
bytesToHex,
Expand Down Expand Up @@ -26,7 +26,7 @@ type ByteCodeDataResponse = Uint8Array[] & { completed?: boolean }
*/
export interface ByteCodeFetcherOptions extends FetcherOptions {
hashes: Uint8Array[]
stateManager?: DefaultStateManager
stateManager?: MerkleStateManager
fetcherDoneFlags?: SnapFetcherDoneFlags

/** Destroy fetcher once all tasks are done */
Expand All @@ -40,7 +40,7 @@ export type JobTask = {

export class ByteCodeFetcher extends Fetcher<JobTask, Uint8Array[], Uint8Array> {
protected debug: Debugger
stateManager: DefaultStateManager
stateManager: MerkleStateManager
fetcherDoneFlags: SnapFetcherDoneFlags
codeDB: DB

Expand All @@ -54,7 +54,7 @@ export class ByteCodeFetcher extends Fetcher<JobTask, Uint8Array[], Uint8Array>
constructor(options: ByteCodeFetcherOptions) {
super(options)
this.hashes = options.hashes ?? []
this.stateManager = options.stateManager ?? new DefaultStateManager()
this.stateManager = options.stateManager ?? new MerkleStateManager()
this.fetcherDoneFlags = options.fetcherDoneFlags ?? getInitFetcherDoneFlags()
this.fetcherDoneFlags.byteCodeFetcher.count = BigInt(this.hashes.length)
this.codeDB = this.stateManager['_getCodeDB']()
Expand Down
8 changes: 4 additions & 4 deletions packages/client/src/sync/fetcher/storagefetcher.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DefaultStateManager } from '@ethereumjs/statemanager'
import { MerkleStateManager } from '@ethereumjs/statemanager'
import { verifyTrieRangeProof } from '@ethereumjs/trie'
import {
BIGINT_0,
Expand Down Expand Up @@ -56,7 +56,7 @@ export interface StorageFetcherOptions extends FetcherOptions {
/** Destroy fetcher once all tasks are done */
destroyWhenDone?: boolean

stateManager: DefaultStateManager
stateManager: MerkleStateManager

fetcherDoneFlags: SnapFetcherDoneFlags
}
Expand All @@ -69,7 +69,7 @@ export type JobTask = {
export class StorageFetcher extends Fetcher<JobTask, StorageData[][], StorageData[]> {
protected debug: Debugger
root: Uint8Array
stateManager: DefaultStateManager
stateManager: MerkleStateManager
fetcherDoneFlags: SnapFetcherDoneFlags

/** The accounts to fetch storage data for */
Expand All @@ -88,7 +88,7 @@ export class StorageFetcher extends Fetcher<JobTask, StorageData[][], StorageDat
this.fragmentedRequests = []

this.root = options.root
this.stateManager = options.stateManager ?? new DefaultStateManager()
this.stateManager = options.stateManager ?? new MerkleStateManager()
this.fetcherDoneFlags = options.fetcherDoneFlags ?? getInitFetcherDoneFlags()
this.storageRequests = options.storageRequests ?? []
this.fetcherDoneFlags.storageFetcher.count = BigInt(this.storageRequests.length)
Expand Down
8 changes: 4 additions & 4 deletions packages/client/src/sync/fetcher/trienodefetcher.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DefaultStateManager } from '@ethereumjs/statemanager'
import { MerkleStateManager } from '@ethereumjs/statemanager'
import {
BranchNode,
ExtensionNode,
Expand Down Expand Up @@ -38,7 +38,7 @@ type TrieNodesResponse = Uint8Array[] & { completed?: boolean }
export interface TrieNodeFetcherOptions extends FetcherOptions {
root: Uint8Array
accountToStorageTrie?: Map<String, Trie>
stateManager?: DefaultStateManager
stateManager?: MerkleStateManager

/** Destroy fetcher once all tasks are done */
destroyWhenDone?: boolean
Expand Down Expand Up @@ -69,7 +69,7 @@ export class TrieNodeFetcher extends Fetcher<JobTask, Uint8Array[], Uint8Array>
protected debug: Debugger
root: Uint8Array

stateManager: DefaultStateManager
stateManager: MerkleStateManager
fetcherDoneFlags: SnapFetcherDoneFlags
accountTrie: Trie
codeDB: DB
Expand Down Expand Up @@ -105,7 +105,7 @@ export class TrieNodeFetcher extends Fetcher<JobTask, Uint8Array[], Uint8Array>
this.requestedNodeToPath = new Map<string, string>()
this.fetchedAccountNodes = new Map<string, FetchedNodeData>()

this.stateManager = options.stateManager ?? new DefaultStateManager()
this.stateManager = options.stateManager ?? new MerkleStateManager()
this.accountTrie = this.stateManager['_getAccountTrie']()
this.codeDB = this.stateManager['_getCodeDB']()

Expand Down
4 changes: 2 additions & 2 deletions packages/client/src/sync/snapsync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import type { Peer } from '../net/peer/peer.js'
import type { Skeleton } from '../service/skeleton.js'
import type { SnapFetcherDoneFlags } from './fetcher/types.js'
import type { SynchronizerOptions } from './sync.js'
import type { DefaultStateManager } from '@ethereumjs/statemanager'
import type { MerkleStateManager } from '@ethereumjs/statemanager'

interface SnapSynchronizerOptions extends SynchronizerOptions {
/** Skeleton chain */
Expand Down Expand Up @@ -228,7 +228,7 @@ export class SnapSynchronizer extends Synchronizer {
this.fetcher = new AccountFetcher({
config: this.config,
pool: this.pool,
stateManager: this.execution.vm.stateManager as DefaultStateManager,
stateManager: this.execution.vm.stateManager as MerkleStateManager,
root: stateRoot,
// This needs to be determined from the current state of the MPT dump
first: BigInt(0),
Expand Down
4 changes: 2 additions & 2 deletions packages/client/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { SyncMode } from './index.js'
import type { Peer } from './net/peer/index.js'
import type { Server } from './net/server/index.js'
import type { Block, BlockHeader } from '@ethereumjs/block'
import type { DefaultStateManager } from '@ethereumjs/statemanager'
import type { MerkleStateManager } from '@ethereumjs/statemanager'
import type { Address } from '@ethereumjs/util'
import type { Multiaddr } from '@multiformats/multiaddr'
import type * as promClient from 'prom-client'
Expand Down Expand Up @@ -41,7 +41,7 @@ export interface EventParams {
[Event.SYNC_FETCHED_BLOCKS]: [blocks: Block[]]
[Event.SYNC_FETCHED_HEADERS]: [headers: BlockHeader[]]
[Event.SYNC_SYNCHRONIZED]: [chainHeight: bigint]
[Event.SYNC_SNAPSYNC_COMPLETE]: [stateRoot: Uint8Array, stateManager: DefaultStateManager]
[Event.SYNC_SNAPSYNC_COMPLETE]: [stateRoot: Uint8Array, stateManager: MerkleStateManager]
[Event.SYNC_ERROR]: [syncError: Error]
[Event.SYNC_FETCHER_ERROR]: [fetchError: Error, task: any, peer: Peer | null | undefined]
[Event.PEER_CONNECTED]: [connectedPeer: Peer]
Expand Down
4 changes: 2 additions & 2 deletions packages/client/src/util/debug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import { Common } from '@ethereumjs/common'
import { Block } from '@ethereumjs/block'
import { VM, runBlock, createVM } from './src'
import { Trie } from '@ethereumjs/trie'
import { DefaultStateManager } from './src/state'
import { MerkleStateManager } from './src/state'
import { Blockchain } from '@ethereumjs/blockchain'
const main = async () => {
Expand All @@ -41,7 +41,7 @@ const main = async () => {
const stateDB = new Level('${execution.config.getDataDirectory(DataDirectory.State)}')
const trie = new Trie({ db: stateDB, useKeyHashing: true })
const stateManager = new DefaultStateManager({ trie, common })
const stateManager = new MerkleStateManager({ trie, common })
// Ensure we run on the right root
stateManager.setStateRoot(hexToBytes('${bytesToHex(
await execution.vm.stateManager.getStateRoot(),
Expand Down
14 changes: 7 additions & 7 deletions packages/client/test/integration/fullethereumservice.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { createBlock } from '@ethereumjs/block'
import { createBlockchain } from '@ethereumjs/blockchain'
import { Hardfork } from '@ethereumjs/common'
import { DefaultStateManager } from '@ethereumjs/statemanager'
import { MerkleStateManager } from '@ethereumjs/statemanager'
import { createFeeMarket1559TxFromRLP } from '@ethereumjs/tx'
import { Account, bytesToHex, equalsBytes, hexToBytes, toBytes } from '@ethereumjs/util'
import * as td from 'testdouble'
Expand All @@ -18,10 +18,10 @@ import { destroy } from './util.js'
const config = new Config({ accountCache: 10000, storageCache: 1000 })

// Stub out setStateRoot since correct state root doesn't exist in mock state.
const ogSetStateRoot = DefaultStateManager.prototype.setStateRoot
DefaultStateManager.prototype.setStateRoot = (): any => {}
const originalStateManagerCopy = DefaultStateManager.prototype.shallowCopy
DefaultStateManager.prototype.shallowCopy = function () {
const ogSetStateRoot = MerkleStateManager.prototype.setStateRoot
MerkleStateManager.prototype.setStateRoot = (): any => {}
const originalStateManagerCopy = MerkleStateManager.prototype.shallowCopy
MerkleStateManager.prototype.shallowCopy = function () {
return this
}
async function setup(): Promise<[MockServer, FullEthereumService]> {
Expand Down Expand Up @@ -128,6 +128,6 @@ describe('should handle LES requests', async () => {
await destroy(server, service)

// unstub setStateRoot
DefaultStateManager.prototype.setStateRoot = ogSetStateRoot
DefaultStateManager.prototype.shallowCopy = originalStateManagerCopy
MerkleStateManager.prototype.setStateRoot = ogSetStateRoot
MerkleStateManager.prototype.shallowCopy = originalStateManagerCopy
}, 30000)
4 changes: 2 additions & 2 deletions packages/client/test/miner/miner.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
createCommonFromGethGenesis,
createCustomCommon,
} from '@ethereumjs/common'
import { DefaultStateManager } from '@ethereumjs/statemanager'
import { MerkleStateManager } from '@ethereumjs/statemanager'
import { createFeeMarket1559Tx, createLegacyTx } from '@ethereumjs/tx'
import { Address, equalsBytes, hexToBytes } from '@ethereumjs/util'
import { AbstractLevel } from 'abstract-level'
Expand Down Expand Up @@ -45,7 +45,7 @@ BlockHeader.prototype['_consensusFormatValidation'] = vi.fn()

// Stub out setStateRoot so txPool.validate checks will pass since correct state root
// doesn't exist in fakeChain state anyway
DefaultStateManager.prototype.setStateRoot = vi.fn()
MerkleStateManager.prototype.setStateRoot = vi.fn()

class FakeChain {
open() {}
Expand Down
4 changes: 2 additions & 2 deletions packages/client/test/miner/pendingBlock.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { BlockHeader, createBlockHeader } from '@ethereumjs/block'
import { createBlockchain } from '@ethereumjs/blockchain'
import { Common, Goerli, Hardfork, Mainnet, createCommonFromGethGenesis } from '@ethereumjs/common'
import { DefaultStateManager } from '@ethereumjs/statemanager'
import { MerkleStateManager } from '@ethereumjs/statemanager'
import { createBlob4844Tx, createFeeMarket1559Tx, createLegacyTx } from '@ethereumjs/tx'
import {
Account,
Expand Down Expand Up @@ -97,7 +97,7 @@ describe('[PendingBlock]', async () => {
}
})

DefaultStateManager.prototype.setStateRoot = vi.fn()
MerkleStateManager.prototype.setStateRoot = vi.fn()

const createTx = (
from = A,
Expand Down
26 changes: 13 additions & 13 deletions packages/client/test/rpc/engine/getPayloadBodiesByHashV1.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createBlock, createBlockHeader } from '@ethereumjs/block'
import { Hardfork } from '@ethereumjs/common'
import { DefaultStateManager } from '@ethereumjs/statemanager'
import { MerkleStateManager } from '@ethereumjs/statemanager'
import { createTxFromTxData } from '@ethereumjs/tx'
import {
Account,
Expand Down Expand Up @@ -32,10 +32,10 @@ describe(method, () => {

it('call with valid parameters', async () => {
// Disable stateroot validation in TxPool since valid state root isn't available
const originalSetStateRoot = DefaultStateManager.prototype.setStateRoot
const originalStateManagerCopy = DefaultStateManager.prototype.shallowCopy
DefaultStateManager.prototype.setStateRoot = function (): any {}
DefaultStateManager.prototype.shallowCopy = function () {
const originalSetStateRoot = MerkleStateManager.prototype.setStateRoot
const originalStateManagerCopy = MerkleStateManager.prototype.shallowCopy
MerkleStateManager.prototype.setStateRoot = function (): any {}
MerkleStateManager.prototype.shallowCopy = function () {
return this
}
const { chain, service, server, common } = await setupChain(genesisJSON, 'post-merge', {
Expand Down Expand Up @@ -108,16 +108,16 @@ describe(method, () => {
assert.equal(res.result.length, 3, 'length of response matches number of block hashes sent')

// Restore setStateRoot
DefaultStateManager.prototype.setStateRoot = originalSetStateRoot
DefaultStateManager.prototype.shallowCopy = originalStateManagerCopy
MerkleStateManager.prototype.setStateRoot = originalSetStateRoot
MerkleStateManager.prototype.shallowCopy = originalStateManagerCopy
})

it('call with valid parameters on pre-Shanghai block', async () => {
// Disable stateroot validation in TxPool since valid state root isn't available
const originalSetStateRoot = DefaultStateManager.prototype.setStateRoot
const originalStateManagerCopy = DefaultStateManager.prototype.shallowCopy
DefaultStateManager.prototype.setStateRoot = function (): any {}
DefaultStateManager.prototype.shallowCopy = function () {
const originalSetStateRoot = MerkleStateManager.prototype.setStateRoot
const originalStateManagerCopy = MerkleStateManager.prototype.shallowCopy
MerkleStateManager.prototype.setStateRoot = function (): any {}
MerkleStateManager.prototype.shallowCopy = function () {
return this
}
const { chain, service, server, common } = await setupChain(
Expand Down Expand Up @@ -192,7 +192,7 @@ describe(method, () => {
)

// Restore setStateRoot
DefaultStateManager.prototype.setStateRoot = originalSetStateRoot
DefaultStateManager.prototype.shallowCopy = originalStateManagerCopy
MerkleStateManager.prototype.setStateRoot = originalSetStateRoot
MerkleStateManager.prototype.shallowCopy = originalStateManagerCopy
})
})
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createBlock, createBlockHeader } from '@ethereumjs/block'
import { Hardfork } from '@ethereumjs/common'
import { DefaultStateManager } from '@ethereumjs/statemanager'
import { MerkleStateManager } from '@ethereumjs/statemanager'
import { createTxFromTxData } from '@ethereumjs/tx'
import { Account, bytesToHex, createAddressFromPrivateKey, hexToBytes } from '@ethereumjs/util'
import { assert, describe, it, vi } from 'vitest'
Expand Down Expand Up @@ -30,8 +30,8 @@ describe(method, () => {
})

it('call with valid parameters', async () => {
DefaultStateManager.prototype.setStateRoot = vi.fn()
DefaultStateManager.prototype.shallowCopy = function () {
MerkleStateManager.prototype.setStateRoot = vi.fn()
MerkleStateManager.prototype.shallowCopy = function () {
return this
}
const { chain, service, server, common } = await setupChain(genesisJSON, 'post-merge', {
Expand Down Expand Up @@ -112,8 +112,8 @@ describe(method, () => {
})

it('call with valid parameters on pre-Shanghai hardfork', async () => {
DefaultStateManager.prototype.setStateRoot = vi.fn()
DefaultStateManager.prototype.shallowCopy = function () {
MerkleStateManager.prototype.setStateRoot = vi.fn()
MerkleStateManager.prototype.shallowCopy = function () {
return this
}
const { chain, service, server, common } = await setupChain(preShanghaiGenesisJSON, 'london', {
Expand Down
Loading

0 comments on commit 8a0afb0

Please sign in to comment.