Skip to content

Commit

Permalink
Revert "statemanager: refactor logic into capabilities (#3554)"
Browse files Browse the repository at this point in the history
This reverts commit d7d1dab.
  • Loading branch information
gabrocheleau committed Aug 7, 2024
1 parent 8fb507e commit 51ec58c
Show file tree
Hide file tree
Showing 18 changed files with 211 additions and 188 deletions.
8 changes: 6 additions & 2 deletions packages/client/src/execution/vmexecution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@ import {
DBSetHashToNumber,
DBSetTD,
} from '@ethereumjs/blockchain'
import { CacheType, ConsensusType, Hardfork } from '@ethereumjs/common'
import { ConsensusType, Hardfork } from '@ethereumjs/common'
import { MCLBLS } from '@ethereumjs/evm'
import { getGenesis } from '@ethereumjs/genesis'
import { DefaultStateManager, StatelessVerkleStateManager } from '@ethereumjs/statemanager'
import {
CacheType,
DefaultStateManager,
StatelessVerkleStateManager,
} from '@ethereumjs/statemanager'
import { createTrie } from '@ethereumjs/trie'
import {
BIGINT_0,
Expand Down
2 changes: 1 addition & 1 deletion packages/client/src/rpc/modules/eth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ import type { EthProtocol } from '../../net/protocol/index.js'
import type { FullEthereumService, Service } from '../../service/index.js'
import type { RpcTx } from '../types.js'
import type { Block, JsonRpcBlock } from '@ethereumjs/block'
import type { Proof } from '@ethereumjs/common'
import type { Log } from '@ethereumjs/evm'
import type { Proof } from '@ethereumjs/statemanager'
import type {
FeeMarketEIP1559Transaction,
LegacyTransaction,
Expand Down
31 changes: 0 additions & 31 deletions packages/common/src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,35 +209,4 @@ export interface StateManagerInterface {
*/
clearCaches(): void
shallowCopy(downlevelCaches?: boolean): StateManagerInterface

/*
* Cache properties
*/
_accountCache?: Cache
_storageCache?: Cache
_codeCache?: Cache

_accountCacheSettings?: CacheSettings
_storageCacheSettings?: CacheSettings
_codeCacheSettings?: CacheSettings
}

/**
* Cache related
*/
export enum CacheType {
LRU = 'lru',
ORDERED_MAP = 'ordered_map',
}

export type CacheSettings = {
deactivate: boolean
type: CacheType
size: number
}

interface Cache {
checkpoint(): void
commit(): void
revert(): void
}
2 changes: 1 addition & 1 deletion packages/statemanager/src/cache/account.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { CacheType } from '@ethereumjs/common'
import { bytesToUnprefixedHex } from '@ethereumjs/util'
import { OrderedMap } from '@js-sdsl/ordered-map'
import debugDefault from 'debug'
import { LRUCache } from 'lru-cache'

import { Cache } from './cache.js'
import { CacheType } from './types.js'

import type { CacheOpts } from './types.js'
import type { Account, Address } from '@ethereumjs/util'
Expand Down
2 changes: 1 addition & 1 deletion packages/statemanager/src/cache/code.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { CacheType } from '@ethereumjs/common'
import { bytesToUnprefixedHex } from '@ethereumjs/util'
import { OrderedMap } from '@js-sdsl/ordered-map'
import debugDefault from 'debug'
import { LRUCache } from 'lru-cache'

import { Cache } from './cache.js'
import { CacheType } from './types.js'

import type { CacheOpts } from './types.js'
import type { Address } from '@ethereumjs/util'
Expand Down
2 changes: 1 addition & 1 deletion packages/statemanager/src/cache/storage.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { CacheType } from '@ethereumjs/common'
import { bytesToUnprefixedHex, hexToBytes } from '@ethereumjs/util'
import { OrderedMap } from '@js-sdsl/ordered-map'
import debugDefault from 'debug'
import { LRUCache } from 'lru-cache'

import { Cache } from './cache.js'
import { CacheType } from './types.js'

import type { CacheOpts } from './types.js'
import type { Address } from '@ethereumjs/util'
Expand Down
5 changes: 4 additions & 1 deletion packages/statemanager/src/cache/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import type { CacheType } from '@ethereumjs/common'
export enum CacheType {
LRU = 'lru',
ORDERED_MAP = 'ordered_map',
}

export interface CacheOpts {
size: number
Expand Down
88 changes: 0 additions & 88 deletions packages/statemanager/src/capabilities.ts

This file was deleted.

29 changes: 17 additions & 12 deletions packages/statemanager/src/rpcStateManager.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CacheType, Common, Mainnet } from '@ethereumjs/common'
import { Common, Mainnet } from '@ethereumjs/common'
import { RLP } from '@ethereumjs/rlp'
import { verifyTrieProof } from '@ethereumjs/trie'
import {
Expand All @@ -16,26 +16,23 @@ import {
import debugDefault from 'debug'
import { keccak256 } from 'ethereum-cryptography/keccak.js'

import { AccountCache, OriginalStorageCache, StorageCache } from './cache/index.js'
import * as Capabilities from './capabilities.js'
import { AccountCache, CacheType, OriginalStorageCache, StorageCache } from './cache/index.js'

import type { RPCStateManagerOpts } from './index.js'
import type { AccountFields, Proof, StateManagerInterface, StorageDump } from '@ethereumjs/common'
import type { Proof, RPCStateManagerOpts } from './index.js'
import type { AccountFields, StateManagerInterface, StorageDump } from '@ethereumjs/common'
import type { Address, PrefixedHexString } from '@ethereumjs/util'
import type { Debugger } from 'debug'

const KECCAK256_RLP_EMPTY_ACCOUNT = RLP.encode(new Account().serialize()).slice(2)

export class RPCStateManager implements StateManagerInterface {
protected _provider: string
protected _contractCache: Map<string, Uint8Array>
protected _storageCache: StorageCache
protected _blockTag: string

_accountCache: AccountCache
_storageCache: StorageCache
_contractCache: Map<string, Uint8Array>

protected _accountCache: AccountCache
originalStorageCache: OriginalStorageCache
_debug: Debugger
protected _debug: Debugger
protected DEBUG: boolean
private keccakFunction: Function
public readonly common: Common
Expand Down Expand Up @@ -320,7 +317,15 @@ export class RPCStateManager implements StateManagerInterface {
),
)
}
await Capabilities.modifyAccountFields(this, address, accountFields)
let account = await this.getAccount(address)
if (!account) {
account = new Account()
}
account.nonce = accountFields.nonce ?? account.nonce
account.balance = accountFields.balance ?? account.balance
account.storageRoot = accountFields.storageRoot ?? account.storageRoot
account.codeHash = accountFields.codeHash ?? account.codeHash
await this.putAccount(address, account)
}

/**
Expand Down
11 changes: 9 additions & 2 deletions packages/statemanager/src/simpleStateManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { Account, bytesToHex } from '@ethereumjs/util'
import { keccak256 } from 'ethereum-cryptography/keccak.js'

import { OriginalStorageCache } from './cache/originalStorageCache.js'
import * as Capabilities from './capabilities.js'

import type { SimpleStateManagerOpts } from './index.js'
import type { AccountFields, Common, StateManagerInterface } from '@ethereumjs/common'
Expand Down Expand Up @@ -79,7 +78,15 @@ export class SimpleStateManager implements StateManagerInterface {
}

async modifyAccountFields(address: Address, accountFields: AccountFields): Promise<void> {
await Capabilities.modifyAccountFields(this, address, accountFields)
let account = await this.getAccount(address)
if (!account) {
account = new Account()
}
account.nonce = accountFields.nonce ?? account.nonce
account.balance = accountFields.balance ?? account.balance
account.storageRoot = accountFields.storageRoot ?? account.storageRoot
account.codeHash = accountFields.codeHash ?? account.codeHash
await this.putAccount(address, account)
}

async getCode(address: Address): Promise<Uint8Array> {
Expand Down
Loading

0 comments on commit 51ec58c

Please sign in to comment.