Skip to content
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

Implement a more performant code cache #3022

Merged
merged 40 commits into from
Sep 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
7297aaa
Setup boilerplace code for code cache
scorbajio Sep 11, 2023
7df6e30
Fix linting error
scorbajio Sep 11, 2023
f851dda
Export code cache
scorbajio Sep 11, 2023
7080bf9
Add tests for code cache
scorbajio Sep 11, 2023
7878b9b
Integrate code cache with statemanager
scorbajio Sep 12, 2023
8936967
Fix code cache integration in state manager
scorbajio Sep 12, 2023
19aff46
Add checkpointing tests for code cache
scorbajio Sep 12, 2023
cb7f9f2
Use address as key for code cache
scorbajio Sep 14, 2023
d68e04b
Use bytesToUnprefixedHex
scorbajio Sep 14, 2023
503da62
Implement put, get, and flush for code cache
scorbajio Sep 14, 2023
e8cf449
Merge branch 'master' into code-cache
scorbajio Sep 14, 2023
5bee767
Flush code cache before other caches in statemanager
scorbajio Sep 17, 2023
4165d35
Merge branch 'code-cache' of github.com:ethereumjs/ethereumjs-monorep…
scorbajio Sep 17, 2023
d3b7758
Update code root of account after putting code
scorbajio Sep 18, 2023
dd1da65
Put code into cache even if the code is empty
scorbajio Sep 19, 2023
6c04cac
Fix linting issues
scorbajio Sep 19, 2023
58fb5d3
Fix diffCache state saving
scorbajio Sep 19, 2023
9161532
Merge branch 'master' into code-cache
scorbajio Sep 20, 2023
87c043a
Merge branch 'master' of github.com:ethereumjs/ethereumjs-monorepo in…
scorbajio Sep 21, 2023
690e578
Merge branch 'code-cache' of github.com:ethereumjs/ethereumjs-monorep…
scorbajio Sep 21, 2023
0a51c47
Do not throw error on code deletion
scorbajio Sep 21, 2023
6e2f726
stateManager: save known prestate
jochem-brouwer Sep 25, 2023
1f9c0b3
Save prestate using current value in db
scorbajio Sep 25, 2023
db5bf79
Fix getContractcode
scorbajio Sep 25, 2023
1b26359
Merge branch 'master' into code-cache
scorbajio Sep 25, 2023
cc565f9
Rename variables to be more consistent with naming conventions used i…
scorbajio Sep 26, 2023
cfada23
Clarify docs and clean up naming
scorbajio Sep 26, 2023
d6a237b
Remove old code cache
scorbajio Sep 26, 2023
72de80e
Simplify _saveCachePreState
scorbajio Sep 26, 2023
d707fcd
Merge branch 'code-cache' of github.com:ethereumjs/ethereumjs-monorep…
scorbajio Sep 26, 2023
28f6c52
Fix lint error
scorbajio Sep 26, 2023
721629f
Merge branch 'master' into code-cache
scorbajio Sep 27, 2023
581a2a9
Add code cache CLI option to client
scorbajio Sep 27, 2023
e677da5
Fix code cache flushing
scorbajio Sep 28, 2023
3b29cd6
Merge branch 'master' into code-cache
holgerd77 Sep 28, 2023
1e9becb
Merge branch 'master' into code-cache
holgerd77 Sep 28, 2023
3946894
Merge branch 'master' into code-cache
scorbajio Sep 28, 2023
795800c
Fix code cache size function
scorbajio Sep 28, 2023
57b9e5b
Do not call stat function of cache if it is disactivated
scorbajio Sep 28, 2023
4f03988
Merge branch 'master' into code-cache
holgerd77 Sep 29, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions packages/client/bin/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,11 @@ const args: ClientOpts = yargs(hideBin(process.argv))
number: true,
default: Config.STORAGE_CACHE,
})
.option('codeCache', {
describe: 'Size for the code cache (max number of contracts)',
number: true,
default: Config.CODE_CACHE,
})
.option('trieCache', {
describe: 'Size for the trie cache (max number of trie nodes)',
number: true,
Expand Down Expand Up @@ -802,6 +807,7 @@ async function run() {
numBlocksPerIteration: args.numBlocksPerIteration,
accountCache: args.accountCache,
storageCache: args.storageCache,
codeCache: args.codeCache,
trieCache: args.trieCache,
dnsNetworks: args.dnsNetworks,
extIP: args.extIP,
Expand Down
8 changes: 8 additions & 0 deletions packages/client/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,11 @@ export interface ConfigOptions {
*/
storageCache?: number

/**
* Size for the code cache (max number of contracts)
*/
codeCache?: number

/**
* Size for the trie cache (max number of trie nodes)
*/
Expand Down Expand Up @@ -337,6 +342,7 @@ export class Config {
public static readonly NUM_BLOCKS_PER_ITERATION = 100
public static readonly ACCOUNT_CACHE = 400000
public static readonly STORAGE_CACHE = 200000
public static readonly CODE_CACHE = 200000
public static readonly TRIE_CACHE = 200000
public static readonly DEBUGCODE_DEFAULT = false
public static readonly SAFE_REORG_DISTANCE = 100
Expand Down Expand Up @@ -374,6 +380,7 @@ export class Config {
public readonly numBlocksPerIteration: number
public readonly accountCache: number
public readonly storageCache: number
public readonly codeCache: number
public readonly trieCache: number
public readonly debugCode: boolean
public readonly discDns: boolean
Expand Down Expand Up @@ -437,6 +444,7 @@ export class Config {
this.numBlocksPerIteration = options.numBlocksPerIteration ?? Config.NUM_BLOCKS_PER_ITERATION
this.accountCache = options.accountCache ?? Config.ACCOUNT_CACHE
this.storageCache = options.storageCache ?? Config.STORAGE_CACHE
this.codeCache = options.codeCache ?? Config.CODE_CACHE
this.trieCache = options.trieCache ?? Config.TRIE_CACHE
this.debugCode = options.debugCode ?? Config.DEBUGCODE_DEFAULT
this.mine = options.mine ?? false
Expand Down
22 changes: 19 additions & 3 deletions packages/client/src/execution/vmexecution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export class VMExecution extends Execution {

this.config.logger.info(`Initializing account cache size=${this.config.accountCache}`)
this.config.logger.info(`Initializing storage cache size=${this.config.storageCache}`)
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({
trie,
Expand All @@ -71,6 +72,11 @@ export class VMExecution extends Execution {
type: CacheType.LRU,
size: this.config.storageCache,
},
codeCacheOpts: {
deactivate: false,
type: CacheType.LRU,
size: this.config.codeCache,
},
})

this.vm = new (VM as any)({
Expand Down Expand Up @@ -680,15 +686,25 @@ export class VMExecution extends Execution {
stats(vm: VM) {
this.statsCount += 1
if (this.statsCount === this.STATS_NUM_BLOCKS) {
let stats = (vm.stateManager as any)._accountCache.stats()
const sm = vm.stateManager as any
const disactivatedStats = { size: 0, reads: 0, hits: 0, writes: 0 }
let stats
// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
stats = !sm._accountCacheSettings.deactivate ? sm._accountCache.stats() : disactivatedStats
this.config.logger.info(
`Account cache stats size=${stats.size} reads=${stats.reads} hits=${stats.hits} writes=${stats.writes}`
)
stats = (vm.stateManager as any)._storageCache.stats()
// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
stats = !sm._storageCacheSettings.deactivate ? sm._storageCache.stats() : disactivatedStats
this.config.logger.info(
`Storage cache stats size=${stats.size} reads=${stats.reads} hits=${stats.hits} writes=${stats.writes}`
)
const tStats = ((vm.stateManager as any)._trie as Trie).database().stats()
// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
stats = !sm._codeCacheSettings.deactivate ? sm._codeCache.stats() : disactivatedStats
this.config.logger.info(
`Code cache stats size=${stats.size} reads=${stats.reads} hits=${stats.hits} writes=${stats.writes}`
)
const tStats = (sm._trie as Trie).database().stats()
this.config.logger.info(
`Trie cache stats size=${tStats.size} reads=${tStats.cache.reads} hits=${tStats.cache.hits} ` +
`writes=${tStats.cache.writes} readsDB=${tStats.db.reads} hitsDB=${tStats.db.hits} writesDB=${tStats.db.writes}`
Expand Down
1 change: 1 addition & 0 deletions packages/client/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ export interface ClientOpts {
numBlocksPerIteration?: number
accountCache?: number
storageCache?: number
codeCache?: number
trieCache?: number
dnsNetworks?: string[]
executeBlocks?: string
Expand Down
15 changes: 15 additions & 0 deletions packages/client/test/cli/cli.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,21 @@ describe('[CLI]', () => {
}
await clientRunHelper(cliArgs, onData)
}, 30000)
it('should start client with custom input for code cache size', async () => {
const cliArgs = ['--codeCache=2000', '--port=30313']
const onData = async (
message: string,
child: ChildProcessWithoutNullStreams,
resolve: Function
) => {
if (message.includes('code cache')) {
assert.ok(message.includes('2000'), 'code cache option works')
child.kill(9)
resolve(undefined)
}
}
await clientRunHelper(cliArgs, onData)
}, 30000)
it('should start client with custom input for trie cache size', async () => {
const cliArgs = ['--trieCache=2000', '--port=30312']
const onData = async (
Expand Down
Loading