-
-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✅ Test: MOre stateManager test coverage
- Loading branch information
William Cory
authored and
William Cory
committed
Jul 10, 2024
1 parent
372d408
commit 882aa2e
Showing
18 changed files
with
717 additions
and
21 deletions.
There are no files selected for viewing
32 changes: 32 additions & 0 deletions
32
packages/state/src/actions/__snapshots__/commit.spec.ts.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// Bun Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`commit should clear all storage entries for the account corresponding to `address` 1`] = ` | ||
[ | ||
Map { | ||
"0000000000000000000000000000000004277dc9" => undefined, | ||
} | ||
, | ||
Map {}, | ||
] | ||
`; | ||
|
||
exports[`commit should clear all storage entries for the account corresponding to `address` 2`] = ` | ||
[ | ||
Map { | ||
"0000000000000000000000000000000004277dc9" => undefined, | ||
} | ||
, | ||
Map {}, | ||
] | ||
`; | ||
|
||
exports[`commit should clear all storage entries for the account corresponding to `address` 3`] = ` | ||
[ | ||
Map {}, | ||
Map {}, | ||
] | ||
`; | ||
|
||
exports[`commit should clear all storage entries for the account corresponding to `address` 4`] = `"0x886f43e0144bf4f5748e999d0178ed7e4edea8ad708e0bf26a61341e8ae91d1e"`; |
6 changes: 6 additions & 0 deletions
6
packages/state/src/actions/__snapshots__/deepCopy.spec.ts.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
// Bun Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`deepCopy should throw if uncommitted state 1`] = `[InternalError: Attempted to deepCopy state with uncommitted checkpoints | ||
Docs: https://tevm.sh/reference/tevm/errors/classes/internalerror/ | ||
Version: 1.1.0.next-73]`; |
8 changes: 8 additions & 0 deletions
8
packages/state/src/actions/__snapshots__/generateCannonicalGenesis.spec.ts.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
// Bun Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`generateCanonicalGenesis should successfully generate canonical genesis 1`] = `{}`; | ||
|
||
exports[`generateCanonicalGenesis should throw if there are uncommitted checkpoints 1`] = `[InternalError: Attempted to generateCanonicalGenesis state with uncommitted checkpoints | ||
Docs: https://tevm.sh/reference/tevm/errors/classes/internalerror/ | ||
Version: 1.1.0.next-73]`; |
159 changes: 159 additions & 0 deletions
159
packages/state/src/actions/__snapshots__/getAccount.spec.ts.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
// Bun Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`getAccount forking Should fetch account from remote provider if not in cache and fork transport is provided 1`] = ` | ||
Account { | ||
"balance": 121136012589291788664n, | ||
"codeHash": Uint8Array [ | ||
197, | ||
210, | ||
70, | ||
1, | ||
134, | ||
247, | ||
35, | ||
60, | ||
146, | ||
126, | ||
125, | ||
178, | ||
220, | ||
199, | ||
3, | ||
192, | ||
229, | ||
0, | ||
182, | ||
83, | ||
202, | ||
130, | ||
39, | ||
59, | ||
123, | ||
250, | ||
216, | ||
4, | ||
93, | ||
133, | ||
164, | ||
112, | ||
], | ||
"nonce": 654430n, | ||
"storageRoot": Uint8Array [ | ||
86, | ||
232, | ||
31, | ||
23, | ||
27, | ||
204, | ||
85, | ||
166, | ||
255, | ||
131, | ||
69, | ||
230, | ||
146, | ||
192, | ||
248, | ||
110, | ||
91, | ||
72, | ||
224, | ||
27, | ||
153, | ||
108, | ||
173, | ||
192, | ||
1, | ||
98, | ||
47, | ||
181, | ||
227, | ||
99, | ||
180, | ||
33, | ||
], | ||
} | ||
`; | ||
|
||
exports[`getAccount forking Should fetch account from remote provider if not in cache and fork transport is provided 2`] = ` | ||
Account { | ||
"balance": 121136012589291788664n, | ||
"codeHash": Uint8Array [ | ||
197, | ||
210, | ||
70, | ||
1, | ||
134, | ||
247, | ||
35, | ||
60, | ||
146, | ||
126, | ||
125, | ||
178, | ||
220, | ||
199, | ||
3, | ||
192, | ||
229, | ||
0, | ||
182, | ||
83, | ||
202, | ||
130, | ||
39, | ||
59, | ||
123, | ||
250, | ||
216, | ||
4, | ||
93, | ||
133, | ||
164, | ||
112, | ||
], | ||
"nonce": 654430n, | ||
"storageRoot": Uint8Array [ | ||
86, | ||
232, | ||
31, | ||
23, | ||
27, | ||
204, | ||
85, | ||
166, | ||
255, | ||
131, | ||
69, | ||
230, | ||
146, | ||
192, | ||
248, | ||
110, | ||
91, | ||
72, | ||
224, | ||
27, | ||
153, | ||
108, | ||
173, | ||
192, | ||
1, | ||
98, | ||
47, | ||
181, | ||
227, | ||
99, | ||
180, | ||
33, | ||
], | ||
} | ||
`; | ||
|
||
exports[`getAccount forking Should return undefined and cache as undefined for empty remote account 1`] = ` | ||
{ | ||
"accountRLP": undefined, | ||
} | ||
`; | ||
|
||
exports[`getAccount forking Should return undefined and cache as undefined for empty remote account 2`] = `{}`; |
14 changes: 14 additions & 0 deletions
14
packages/state/src/actions/__snapshots__/getContractStorage.spec.ts.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// Bun Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`getContractStorage should throw an error if the key is not 32 bytes long 1`] = ` | ||
"Storage key must be 32 bytes long. Received 2. If using numberToHex make the length 32. | ||
Docs: https://tevm.sh/reference/tevm/errors/classes/internalerror/ | ||
Version: 1.1.0.next-73" | ||
`; | ||
|
||
exports[`getContractStorage forking should fetch storage from remote provider if not in cache and fork transport is provided 1`] = ` | ||
Uint8Array [ | ||
0, | ||
] | ||
`; |
11 changes: 11 additions & 0 deletions
11
packages/state/src/actions/__snapshots__/putContractStorage.spec.ts.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// Bun Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`putContractStorage should throw an error if the key is not 32 bytes long 1`] = `[InternalError: Storage key must be 32 bytes long. Received 18,52 | ||
Docs: https://tevm.sh/reference/tevm/errors/classes/internalerror/ | ||
Version: 1.1.0.next-73]`; | ||
|
||
exports[`putContractStorage should throw an error if the account does not exist 1`] = `[InternalError: cannot putContractStorage on non existing acccount! Consider checking if account exists first | ||
Docs: https://tevm.sh/reference/tevm/errors/classes/internalerror/ | ||
Version: 1.1.0.next-73]`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,105 @@ | ||
import { describe, expect, it } from 'bun:test' | ||
import { beforeEach, describe, expect, it, jest } from 'bun:test' | ||
import { createAddress } from '@tevm/address' | ||
import { EthjsAccount } from '@tevm/utils' | ||
import { createBaseState } from '../createBaseState.js' | ||
import { checkpoint } from './checkpoint.js' | ||
import { commit } from './commit.js' | ||
import { putAccount } from './putAccount.js' | ||
|
||
describe(commit.name, () => { | ||
it('should clear all storage entries for the account corresponding to `address`', async () => { | ||
// No mocks | ||
const baseState = createBaseState({ | ||
loggingLevel: 'warn', | ||
}) | ||
await putAccount(baseState)(createAddress(69696969), EthjsAccount.fromAccountData({ balance: 20n })) | ||
expect(baseState.caches.storage._checkpoints).toBe(0) | ||
expect(baseState.caches.accounts._checkpoints).toBe(0) | ||
expect(baseState.caches.contracts._checkpoints).toBe(0) | ||
await checkpoint(baseState)() | ||
expect(baseState.caches.storage._checkpoints).toBe(1) | ||
expect(baseState.caches.accounts._checkpoints).toBe(1) | ||
expect(baseState.caches.contracts._checkpoints).toBe(1) | ||
expect(baseState.caches.accounts._diffCache).toEqual([new Map(), new Map()]) | ||
expect(baseState.caches.accounts._diffCache).toEqual([ | ||
new Map( | ||
Object.entries({ | ||
'0000000000000000000000000000000004277dc9': undefined, | ||
}), | ||
), | ||
new Map(), | ||
]) | ||
expect(baseState.caches.storage._diffCache).toEqual([new Map(), new Map()]) | ||
await commit(baseState)(true) | ||
expect(baseState.caches.storage._checkpoints).toBe(1) | ||
expect(baseState.caches.accounts._checkpoints).toBe(1) | ||
expect(baseState.caches.contracts._checkpoints).toBe(1) | ||
expect(baseState.caches.accounts._diffCache).toEqual([ | ||
new Map( | ||
Object.entries({ | ||
'0000000000000000000000000000000004277dc9': undefined, | ||
}), | ||
), | ||
new Map(), | ||
]) | ||
expect(baseState.caches.storage._diffCache).toEqual([new Map(), new Map()]) | ||
expect(baseState.getCurrentStateRoot()).toEqual( | ||
'0x886f43e0144bf4f5748e999d0178ed7e4edea8ad708e0bf26a61341e8ae91d1e', | ||
) | ||
}) | ||
|
||
let baseState: ReturnType<typeof createBaseState> | ||
|
||
beforeEach(() => { | ||
baseState = createBaseState({ | ||
loggingLevel: 'warn', | ||
}) | ||
baseState.getCurrentStateRoot = jest.fn(() => 'existingStateRoot') as any | ||
baseState.setCurrentStateRoot = jest.fn() | ||
baseState.logger.debug = jest.fn() | ||
baseState.options.onCommit = jest.fn() | ||
baseState.stateRoots.set = jest.fn() | ||
baseState.caches.accounts.commit = jest.fn() | ||
baseState.caches.contracts.commit = jest.fn() | ||
baseState.caches.storage.commit = jest.fn() | ||
}) | ||
|
||
it('should commit to existing state root', async () => { | ||
await checkpoint(baseState)() | ||
await commit(baseState)() | ||
expect(baseState.caches.storage._checkpoints).toBe(0) | ||
expect(baseState.caches.accounts._checkpoints).toBe(0) | ||
expect(baseState.caches.contracts._checkpoints).toBe(0) | ||
expect(baseState.caches.accounts._diffCache).toEqual([new Map()]) | ||
expect(baseState.caches.storage._diffCache).toEqual([new Map()]) | ||
|
||
expect(baseState.getCurrentStateRoot).toHaveBeenCalled() | ||
expect(baseState.setCurrentStateRoot).toHaveBeenCalledWith('existingStateRoot') | ||
expect(baseState.logger.debug).toHaveBeenCalledWith('Comitting to existing state root...') | ||
expect(baseState.stateRoots.set).toHaveBeenCalledWith('existingStateRoot', expect.any(Object)) | ||
expect(baseState.caches.accounts.commit).toHaveBeenCalled() | ||
expect(baseState.caches.contracts.commit).toHaveBeenCalled() | ||
expect(baseState.caches.storage.commit).toHaveBeenCalled() | ||
expect(baseState.options.onCommit).toHaveBeenCalledWith(baseState) | ||
}) | ||
|
||
it('should commit to a new state root', async () => { | ||
await checkpoint(baseState)() | ||
await commit(baseState)(true) | ||
|
||
expect(baseState.setCurrentStateRoot).toHaveBeenCalledWith(expect.any(String)) | ||
expect(baseState.logger.debug).toHaveBeenCalledWith( | ||
expect.objectContaining({ root: expect.any(String) }), | ||
'Committing to new state root...', | ||
) | ||
expect(baseState.stateRoots.set).toHaveBeenCalledWith(expect.any(String), expect.any(Object)) | ||
expect(baseState.caches.accounts.commit).toHaveBeenCalled() | ||
expect(baseState.caches.contracts.commit).toHaveBeenCalled() | ||
expect(baseState.caches.storage.commit).toHaveBeenCalled() | ||
expect(baseState.options.onCommit).toHaveBeenCalledWith(baseState) | ||
}) | ||
|
||
it('should handle onCommit callback correctly', async () => { | ||
const onCommitMock = jest.fn() | ||
baseState.options.onCommit = onCommitMock | ||
|
||
await checkpoint(baseState)() | ||
await commit(baseState)() | ||
|
||
expect(onCommitMock).toHaveBeenCalledWith(baseState) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.