-
Notifications
You must be signed in to change notification settings - Fork 759
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
statemanager: refactor logic into capabilities #3554
Changes from all commits
12b45ba
dcae495
ec5f21b
83c899f
cdfdf39
8fb507e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -209,4 +209,35 @@ 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These (so generally it's already too crowded in here and some interfaces do not really belong here) So these Cache interfaces should definitely not be placed here and should go back if it's not absolutely structurally necessary (I guess it's not?). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (maybe this should be stated better at the top of this file) |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import { type AccountFields, CacheType, type StateManagerInterface } from '@ethereumjs/common' | ||
import { Account } from '@ethereumjs/util' | ||
|
||
import { AccountCache, CodeCache, OriginalStorageCache, StorageCache } from './cache/index.js' | ||
|
||
import type { CacheStateManagerOpts } from './types.js' | ||
import type { Address } from '@ethereumjs/util' | ||
|
||
export function checkpointCaches(stateManager: StateManagerInterface): void { | ||
stateManager._accountCache?.checkpoint() | ||
stateManager._storageCache?.checkpoint() | ||
stateManager._codeCache?.checkpoint() | ||
} | ||
|
||
export function commitCaches(stateManager: StateManagerInterface): void { | ||
stateManager._accountCache?.commit() | ||
stateManager._storageCache?.commit() | ||
stateManager._codeCache?.commit() | ||
} | ||
|
||
export function initializeCaches( | ||
stateManager: StateManagerInterface, | ||
options: CacheStateManagerOpts, | ||
): void { | ||
stateManager.originalStorageCache = new OriginalStorageCache( | ||
stateManager.getStorage.bind(stateManager), | ||
) | ||
|
||
stateManager._accountCacheSettings = { | ||
deactivate: options.accountCacheOpts?.deactivate ?? false, | ||
type: options.accountCacheOpts?.type ?? CacheType.ORDERED_MAP, | ||
size: options.accountCacheOpts?.size ?? 100000, | ||
} | ||
|
||
if (stateManager._accountCacheSettings.deactivate === false) { | ||
stateManager._accountCache = new AccountCache({ | ||
size: stateManager._accountCacheSettings.size, | ||
type: stateManager._accountCacheSettings.type, | ||
}) | ||
} | ||
|
||
stateManager._storageCacheSettings = { | ||
deactivate: options.storageCacheOpts?.deactivate ?? false, | ||
type: options.storageCacheOpts?.type ?? CacheType.ORDERED_MAP, | ||
size: options.storageCacheOpts?.size ?? 20000, | ||
} | ||
|
||
if (stateManager._storageCacheSettings.deactivate === false) { | ||
stateManager._storageCache = new StorageCache({ | ||
size: stateManager._storageCacheSettings.size, | ||
type: stateManager._storageCacheSettings.type, | ||
}) | ||
} | ||
|
||
stateManager._codeCacheSettings = { | ||
deactivate: | ||
(options.codeCacheOpts?.deactivate === true || options.codeCacheOpts?.size === 0) ?? false, | ||
type: options.codeCacheOpts?.type ?? CacheType.ORDERED_MAP, | ||
size: options.codeCacheOpts?.size ?? 20000, | ||
} | ||
|
||
if (stateManager._codeCacheSettings.deactivate === false) { | ||
stateManager._codeCache = new CodeCache({ | ||
size: stateManager._codeCacheSettings.size, | ||
type: stateManager._codeCacheSettings.type, | ||
}) | ||
} | ||
} | ||
|
||
export async function modifyAccountFields( | ||
stateManager: StateManagerInterface, | ||
address: Address, | ||
accountFields: AccountFields, | ||
): Promise<void> { | ||
const account = (await stateManager.getAccount(address)) ?? 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 stateManager.putAccount(address, account) | ||
} | ||
|
||
export function revertCaches(stateManager: StateManagerInterface): void { | ||
stateManager._accountCache?.revert() | ||
stateManager._storageCache?.revert() | ||
stateManager._codeCache?.revert() | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, yeah, and the
StateManager
interface should generally not be bloated in any way, that was one big reason for the refactor of the interface we did 1-2 weeks ago.This should remain minimal (also regarding optional properties, though it's a bit less urgent here), so that third-party users are not burdened with the internals of state manager.
So all these properties should definitely go out again.