From 073c1ee95d87b0954e5924eb18d835bc5c5dbb61 Mon Sep 17 00:00:00 2001 From: Jochem Brouwer Date: Wed, 24 Jul 2024 00:51:11 +0200 Subject: [PATCH 1/3] statemanager: do not throw on getContractStorage on non-existing accounts --- packages/client/src/rpc/modules/eth.ts | 4 ---- packages/statemanager/src/stateManager.ts | 3 +-- 2 files changed, 1 insertion(+), 6 deletions(-) diff --git a/packages/client/src/rpc/modules/eth.ts b/packages/client/src/rpc/modules/eth.ts index 9afdf2c69b..447305dced 100644 --- a/packages/client/src/rpc/modules/eth.ts +++ b/packages/client/src/rpc/modules/eth.ts @@ -779,10 +779,6 @@ export class Eth { await vm.stateManager.setStateRoot(block.header.stateRoot) const address = Address.fromString(addressHex) - const account = await vm.stateManager.getAccount(address) - if (account === undefined) { - return EMPTY_SLOT - } const key = setLengthLeft(hexToBytes(keyHex), 32) const storage = await vm.stateManager.getContractStorage(address, key) return storage !== null && storage !== undefined diff --git a/packages/statemanager/src/stateManager.ts b/packages/statemanager/src/stateManager.ts index 84a0da6e8a..b9af1b480c 100644 --- a/packages/statemanager/src/stateManager.ts +++ b/packages/statemanager/src/stateManager.ts @@ -497,7 +497,7 @@ export class DefaultStateManager implements EVMStateManagerInterface { const account = await this.getAccount(address) if (!account) { - throw new Error('getContractStorage() called on non-existing account') + return new Uint8Array() } const trie = this._getStorageTrie(address, account) const value = await trie.get(key) @@ -732,7 +732,6 @@ export class DefaultStateManager implements EVMStateManagerInterface { 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', From acd01d3179a26ff3e039b7801aa76201b79f8858 Mon Sep 17 00:00:00 2001 From: Jochem Brouwer Date: Fri, 26 Jul 2024 12:35:08 +0200 Subject: [PATCH 2/3] statemanager: add test --- packages/statemanager/test/stateManager.spec.ts | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/packages/statemanager/test/stateManager.spec.ts b/packages/statemanager/test/stateManager.spec.ts index 9c1b019f3d..02d09e04ea 100644 --- a/packages/statemanager/test/stateManager.spec.ts +++ b/packages/statemanager/test/stateManager.spec.ts @@ -9,6 +9,7 @@ import { intToBytes, setLengthLeft, utf8ToBytes, + zeros, } from '@ethereumjs/util' import { assert, describe, it } from 'vitest' @@ -41,6 +42,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.getContractStorage(Address.zero(), zeros(32)) + assert.ok(equalsBytes(storage, new Uint8Array())) + } catch { + assert.fail('should not throw') + } + }) + it(`should clear contract storage`, async () => { const sm = new DefaultStateManager() From 68a96b83296bc306108639f999dfcea41351d14a Mon Sep 17 00:00:00 2001 From: Jochem Brouwer Date: Wed, 14 Aug 2024 14:04:12 +0200 Subject: [PATCH 3/3] statemanager: fix test --- packages/statemanager/test/stateManager.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/statemanager/test/stateManager.spec.ts b/packages/statemanager/test/stateManager.spec.ts index bb532e7896..f3f466a8da 100644 --- a/packages/statemanager/test/stateManager.spec.ts +++ b/packages/statemanager/test/stateManager.spec.ts @@ -48,7 +48,7 @@ describe('StateManager -> General', () => { const sm = new DefaultStateManager() try { - const storage = await sm.getContractStorage(Address.zero(), zeros(32)) + const storage = await sm.getStorage(createZeroAddress(), zeros(32)) assert.ok(equalsBytes(storage, new Uint8Array())) } catch { assert.fail('should not throw')