From 6d187eb73d46f1f43e58c1f143bbd949879f6535 Mon Sep 17 00:00:00 2001 From: Jochem Brouwer Date: Wed, 14 Aug 2024 15:45:00 +0200 Subject: [PATCH] StateManager: Do not throw calling `getContractStorage` on non-existing 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 --- packages/statemanager/src/stateManager.ts | 3 +-- packages/statemanager/test/stateManager.spec.ts | 12 ++++++++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/packages/statemanager/src/stateManager.ts b/packages/statemanager/src/stateManager.ts index e973137b95..7717544676 100644 --- a/packages/statemanager/src/stateManager.ts +++ b/packages/statemanager/src/stateManager.ts @@ -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) @@ -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', diff --git a/packages/statemanager/test/stateManager.spec.ts b/packages/statemanager/test/stateManager.spec.ts index 2ec8bbb856..f3f466a8da 100644 --- a/packages/statemanager/test/stateManager.spec.ts +++ b/packages/statemanager/test/stateManager.spec.ts @@ -11,6 +11,7 @@ import { intToBytes, setLengthLeft, utf8ToBytes, + zeros, } from '@ethereumjs/util' import { assert, describe, it } from 'vitest' @@ -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()