Skip to content

Commit

Permalink
statemanager: remove cache opt from key naming
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrocheleau committed Aug 10, 2024
1 parent f30288a commit ba00837
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 42 deletions.
6 changes: 3 additions & 3 deletions packages/client/src/execution/vmexecution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,17 +166,17 @@ export class VMExecution extends Execution {
trie,
prefixStorageTrieKeys: this.config.prefixStorageTrieKeys,
caches: new Caches({
accountCacheOpts: {
account: {
deactivate: false,
type: CacheType.LRU,
size: this.config.accountCache,
},
storageCacheOpts: {
storage: {
deactivate: false,
type: CacheType.LRU,
size: this.config.storageCache,
},
codeCacheOpts: {
code: {
deactivate: false,
type: CacheType.LRU,
size: this.config.codeCache,
Expand Down
21 changes: 9 additions & 12 deletions packages/statemanager/src/cache/caches.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,20 @@ export class Caches {

constructor(opts: CachesStateManagerOpts = {}) {
const accountSettings = {
deactivate:
(opts.accountCacheOpts?.deactivate === true || opts.accountCacheOpts?.size === 0) ?? false,
type: opts.accountCacheOpts?.type ?? CacheType.ORDERED_MAP,
size: opts.accountCacheOpts?.size ?? 100000,
deactivate: (opts.account?.deactivate === true || opts.account?.size === 0) ?? false,
type: opts.account?.type ?? CacheType.ORDERED_MAP,
size: opts.account?.size ?? 100000,
}
const storageSettings = {
deactivate:
(opts.storageCacheOpts?.deactivate === true || opts.storageCacheOpts?.size === 0) ?? false,
type: opts.storageCacheOpts?.type ?? CacheType.ORDERED_MAP,
size: opts.storageCacheOpts?.size ?? 20000,
deactivate: (opts.storage?.deactivate === true || opts.storage?.size === 0) ?? false,
type: opts.storage?.type ?? CacheType.ORDERED_MAP,
size: opts.storage?.size ?? 20000,
}

const codeSettings = {
deactivate:
(opts.codeCacheOpts?.deactivate === true || opts.codeCacheOpts?.size === 0) ?? false,
type: opts.codeCacheOpts?.type ?? CacheType.ORDERED_MAP,
size: opts.codeCacheOpts?.size ?? 20000,
deactivate: (opts.code?.deactivate === true || opts.code?.size === 0) ?? false,
type: opts.code?.type ?? CacheType.ORDERED_MAP,
size: opts.code?.size ?? 20000,
}

this.settings = {
Expand Down
6 changes: 3 additions & 3 deletions packages/statemanager/src/cache/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export interface CacheStateManagerOpts {
}

export interface CachesStateManagerOpts {
accountCacheOpts?: CacheStateManagerOpts
codeCacheOpts?: CacheStateManagerOpts
storageCacheOpts?: CacheStateManagerOpts
account?: CacheStateManagerOpts
code?: CacheStateManagerOpts
storage?: CacheStateManagerOpts
}
6 changes: 5 additions & 1 deletion packages/statemanager/src/stateManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -939,7 +939,11 @@ export class DefaultStateManager implements StateManagerInterface {
trie,
prefixStorageTrieKeys,
prefixCodeHashes,
caches: new Caches({ accountCacheOpts, codeCacheOpts, storageCacheOpts }),
caches: new Caches({
account: accountCacheOpts,
code: codeCacheOpts,
storage: storageCacheOpts,
}),
})
}

Expand Down
24 changes: 18 additions & 6 deletions packages/statemanager/test/stateManager.account.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ describe('StateManager -> General/Account', () => {
{ deactivate: false, size: 0 },
]) {
it(`should set the state root to empty`, async () => {
const stateManager = new DefaultStateManager({ caches: new Caches({ accountCacheOpts }) })
const stateManager = new DefaultStateManager({
caches: new Caches({ account: accountCacheOpts }),
})
assert.ok(equalsBytes(stateManager['_trie'].root(), KECCAK256_RLP), 'it has default root')

// commit some data to the trie
Expand All @@ -32,7 +34,9 @@ describe('StateManager -> General/Account', () => {
})

it(`should clear the cache when the state root is set`, async () => {
const stateManager = new DefaultStateManager({ caches: new Caches({ accountCacheOpts }) })
const stateManager = new DefaultStateManager({
caches: new Caches({ account: accountCacheOpts }),
})
const address = new Address(hexToBytes('0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b'))
const account = createAccountWithDefaults()

Expand Down Expand Up @@ -75,7 +79,9 @@ describe('StateManager -> General/Account', () => {
})

it('should put and get account, and add to the underlying cache if the account is not found', async () => {
const stateManager = new DefaultStateManager({ caches: new Caches({ accountCacheOpts }) })
const stateManager = new DefaultStateManager({
caches: new Caches({ account: accountCacheOpts }),
})
const account = createAccountWithDefaults()
const address = new Address(hexToBytes('0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b'))

Expand All @@ -94,7 +100,9 @@ describe('StateManager -> General/Account', () => {
})

it(`should return undefined for a non-existent account`, async () => {
const stateManager = new DefaultStateManager({ caches: new Caches({ accountCacheOpts }) })
const stateManager = new DefaultStateManager({
caches: new Caches({ account: accountCacheOpts }),
})
const address = new Address(hexToBytes('0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b'))

const res = (await stateManager.getAccount(address)) === undefined
Expand All @@ -103,7 +111,9 @@ describe('StateManager -> General/Account', () => {
})

it(`should return undefined for an existent account`, async () => {
const stateManager = new DefaultStateManager({ caches: new Caches({ accountCacheOpts }) })
const stateManager = new DefaultStateManager({
caches: new Caches({ account: accountCacheOpts }),
})
const account = createAccountWithDefaults(BigInt(0x1), BigInt(0x1))
const address = new Address(hexToBytes('0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b'))

Expand All @@ -115,7 +125,9 @@ describe('StateManager -> General/Account', () => {
})

it(`should modify account fields correctly`, async () => {
const stateManager = new DefaultStateManager({ caches: new Caches({ accountCacheOpts }) })
const stateManager = new DefaultStateManager({
caches: new Caches({ account: accountCacheOpts }),
})
const account = createAccountWithDefaults()
const address = new Address(hexToBytes('0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b'))
await stateManager.putAccount(address, account)
Expand Down
24 changes: 18 additions & 6 deletions packages/statemanager/test/stateManager.code.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,12 @@ describe('StateManager -> Code', () => {
*/

// Setup
const stateManager = new DefaultStateManager({ caches: new Caches({ accountCacheOpts }) })
const codeStateManager = new DefaultStateManager({ caches: new Caches({ accountCacheOpts }) })
const stateManager = new DefaultStateManager({
caches: new Caches({ account: accountCacheOpts }),
})
const codeStateManager = new DefaultStateManager({
caches: new Caches({ account: accountCacheOpts }),
})
const address1 = new Address(hexToBytes('0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b'))
const account = createAccountWithDefaults()
const key1 = hexToBytes(`0x${'00'.repeat(32)}`)
Expand Down Expand Up @@ -87,7 +91,9 @@ describe('StateManager -> Code', () => {
})

it(`should set and get code`, async () => {
const stateManager = new DefaultStateManager({ caches: new Caches({ accountCacheOpts }) })
const stateManager = new DefaultStateManager({
caches: new Caches({ account: accountCacheOpts }),
})
const address = new Address(hexToBytes('0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b'))
const code = hexToBytes(
'0x73095e7baea6a6c7c4c2dfeb977efac326af552d873173095e7baea6a6c7c4c2dfeb977efac326af552d873157',
Expand All @@ -105,7 +111,9 @@ describe('StateManager -> Code', () => {
})

it(`should not get code if is not contract`, async () => {
const stateManager = new DefaultStateManager({ caches: new Caches({ accountCacheOpts }) })
const stateManager = new DefaultStateManager({
caches: new Caches({ account: accountCacheOpts }),
})
const address = new Address(hexToBytes('0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b'))
const raw: AccountData = {
nonce: '0x0',
Expand All @@ -118,7 +126,9 @@ describe('StateManager -> Code', () => {
})

it(`should set empty code`, async () => {
const stateManager = new DefaultStateManager({ caches: new Caches({ accountCacheOpts }) })
const stateManager = new DefaultStateManager({
caches: new Caches({ account: accountCacheOpts }),
})
const address = new Address(hexToBytes('0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b'))
const raw: AccountData = {
nonce: '0x0',
Expand All @@ -133,7 +143,9 @@ describe('StateManager -> Code', () => {
})

it(`should prefix codehashes by default`, async () => {
const stateManager = new DefaultStateManager({ caches: new Caches({ accountCacheOpts }) })
const stateManager = new DefaultStateManager({
caches: new Caches({ account: accountCacheOpts }),
})
const address = new Address(hexToBytes('0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b'))
const code = hexToBytes('0x80')
await stateManager.putCode(address, code)
Expand Down
4 changes: 2 additions & 2 deletions packages/statemanager/test/stateManager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,10 @@ describe('StateManager -> General', () => {
sm = new DefaultStateManager({
trie,
caches: new Caches({
accountCacheOpts: {
account: {
type: CacheType.LRU,
},
storageCacheOpts: {
storage: {
type: CacheType.LRU,
},
}),
Expand Down
14 changes: 7 additions & 7 deletions packages/statemanager/test/stateManager.storage.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ describe('StateManager -> Storage', () => {
it.skipIf(isBrowser() === true)(`should dump storage`, async () => {
const stateManager = new DefaultStateManager({
prefixStorageTrieKeys,
caches: new Caches({ storageCacheOpts }),
caches: new Caches({ storage: storageCacheOpts }),
})
const address = new Address(hexToBytes('0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b'))
const account = createAccountWithDefaults()
Expand All @@ -45,7 +45,7 @@ describe('StateManager -> Storage', () => {
it("should validate the key's length when modifying a contract's storage", async () => {
const stateManager = new DefaultStateManager({
prefixStorageTrieKeys,
caches: new Caches({ storageCacheOpts }),
caches: new Caches({ storage: storageCacheOpts }),
})
const address = new Address(hexToBytes('0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b'))
const account = createAccountWithDefaults()
Expand All @@ -64,7 +64,7 @@ describe('StateManager -> Storage', () => {
it("should validate the key's length when reading a contract's storage", async () => {
const stateManager = new DefaultStateManager({
prefixStorageTrieKeys,
caches: new Caches({ storageCacheOpts }),
caches: new Caches({ storage: storageCacheOpts }),
})
const address = new Address(hexToBytes('0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b'))
const account = createAccountWithDefaults()
Expand All @@ -83,7 +83,7 @@ describe('StateManager -> Storage', () => {
it(`should throw on storage values larger than 32 bytes`, async () => {
const stateManager = new DefaultStateManager({
prefixStorageTrieKeys,
caches: new Caches({ storageCacheOpts }),
caches: new Caches({ storage: storageCacheOpts }),
})
const address = createZeroAddress()
const account = createAccountWithDefaults()
Expand All @@ -102,7 +102,7 @@ describe('StateManager -> Storage', () => {
it(`should strip zeros of storage values`, async () => {
const stateManager = new DefaultStateManager({
prefixStorageTrieKeys,
caches: new Caches({ storageCacheOpts }),
caches: new Caches({ storage: storageCacheOpts }),
})
const address = createZeroAddress()
const account = createAccountWithDefaults()
Expand Down Expand Up @@ -135,7 +135,7 @@ describe('StateManager -> Storage', () => {
for (const length of zeroLengths) {
const stateManager = new DefaultStateManager({
prefixStorageTrieKeys,
caches: new Caches({ storageCacheOpts }),
caches: new Caches({ storage: storageCacheOpts }),
})
const account = createAccountWithDefaults()
await stateManager.putAccount(address, account)
Expand All @@ -158,7 +158,7 @@ describe('StateManager -> Storage', () => {
it(`should not strip trailing zeros`, async () => {
const stateManager = new DefaultStateManager({
prefixStorageTrieKeys,
caches: new Caches({ storageCacheOpts }),
caches: new Caches({ storage: storageCacheOpts }),
})
const address = createZeroAddress()
const account = createAccountWithDefaults()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,10 @@ describe('StatelessVerkleStateManager: Kaustinen Verkle Block', () => {
it(`copy()`, async () => {
const stateManager = new StatelessVerkleStateManager({
caches: new Caches({
accountCacheOpts: {
account: {
type: CacheType.ORDERED_MAP,
},
storageCacheOpts: {
storage: {
type: CacheType.ORDERED_MAP,
},
}),
Expand Down

0 comments on commit ba00837

Please sign in to comment.