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

StateManager: Do not throw calling getContractStorage on non-existing accounts #3536

Merged
merged 8 commits into from
Aug 14, 2024
3 changes: 1 addition & 2 deletions packages/statemanager/src/stateManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ export class DefaultStateManager implements StateManagerInterface {

const account = await this.getAccount(address)
if (!account) {
throw new Error('getStorage() called on non-existing account')
return new Uint8Array()
}
const trie = this._getStorageTrie(address, account)
const value = await trie.get(key)
Expand Down Expand Up @@ -544,7 +544,6 @@ export class DefaultStateManager implements StateManagerInterface {
await this.flush()
const account = await this.getAccount(address)
if (!account) {
// throw new Error(`getProof() can only be called for an existing account`)
const returnValue: Proof = {
address: address.toString(),
balance: '0x0',
Expand Down
12 changes: 12 additions & 0 deletions packages/statemanager/test/stateManager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
intToBytes,
setLengthLeft,
utf8ToBytes,
zeros,
} from '@ethereumjs/util'
import { assert, describe, it } from 'vitest'

Expand Down Expand Up @@ -43,6 +44,17 @@ describe('StateManager -> General', () => {
assert.deepEqual(res, KECCAK256_RLP, 'it has default root')
})

it('should not throw on getContractStorage() on non-existing accounts', async () => {
const sm = new DefaultStateManager()

try {
const storage = await sm.getStorage(createZeroAddress(), zeros(32))
assert.ok(equalsBytes(storage, new Uint8Array()))
} catch {
assert.fail('should not throw')
}
})

it(`should clear contract storage`, async () => {
const sm = new DefaultStateManager()

Expand Down
Loading