Skip to content

Commit

Permalink
StateManager: Do not throw calling getContractStorage on non-existi…
Browse files Browse the repository at this point in the history
…ng accounts (#3536)

* statemanager: do not throw on getContractStorage on non-existing accounts

* Merge branch 'master' into get-contract-storage-no-error-non-existing

* statemanager: add test

* Merge branch 'master' into get-contract-storage-no-error-non-existing

* Merge remote-tracking branch 'origin/master' into get-contract-storage-no-error-non-existing

* Merge remote-tracking branch 'origin/master' into get-contract-storage-no-error-non-existing [no ci]

* statemanager: fix test

* Merge branch 'master' into get-contract-storage-no-error-non-existing
  • Loading branch information
jochem-brouwer authored Aug 14, 2024
1 parent 1054c4a commit 6d187eb
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
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

0 comments on commit 6d187eb

Please sign in to comment.