Skip to content

Commit 8928f88

Browse files
jochem-brouweracolytec3
authored andcommitted
Ensure TransientStorage cleanups after Tx-level contract creation (#3625)
* evm: fix bug not clearing transient storage on tx-level create * evm: add tests for tstore cleanup * make cspell happy --------- Co-authored-by: acolytec3 <[email protected]>
1 parent 27e2c02 commit 8928f88

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

packages/evm/src/evm.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -733,6 +733,10 @@ export class EVM implements EVMInterface {
733733
}
734734
}
735735

736+
if (message.depth === 0) {
737+
this.postMessageCleanup()
738+
}
739+
736740
return {
737741
createdAddress: message.to,
738742
execResult: result,

packages/evm/test/transientStorage.spec.ts

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import { Address } from '@ethereumjs/util'
1+
import { Address, equalsBytes, hexToBytes, setLengthLeft, unpadBytes } from '@ethereumjs/util'
22
import { assert, describe, it } from 'vitest'
33

4+
import { EVM } from '../src/index.js'
45
import { TransientStorage } from '../src/transientStorage.js'
56

67
describe('Transient Storage', () => {
@@ -176,4 +177,38 @@ describe('Transient Storage', () => {
176177
transientStorage.revert()
177178
assert.deepEqual(transientStorage.get(address, key), value1)
178179
})
180+
181+
it('should cleanup after a message create', async () => {
182+
const evm = await EVM.create()
183+
// PUSH 1 PUSH 1 TSTORE
184+
const code = hexToBytes('0x600160015D')
185+
const keyBuf = setLengthLeft(new Uint8Array([1]), 32)
186+
const result = await evm.runCall({
187+
data: code,
188+
gasLimit: BigInt(100_000),
189+
})
190+
const created = result.createdAddress!
191+
const stored = evm.transientStorage.get(created, keyBuf)
192+
assert.ok(
193+
equalsBytes(unpadBytes(stored), new Uint8Array()),
194+
'Transient storage has been cleared'
195+
)
196+
})
197+
198+
it('should cleanup after a message call', async () => {
199+
const evm = await EVM.create()
200+
const contractAddress = Address.zero()
201+
// PUSH 1 PUSH 1 TSTORE
202+
const code = hexToBytes('0x600160015D')
203+
await evm.stateManager.putContractCode(contractAddress, code)
204+
const keyBuf = setLengthLeft(new Uint8Array([1]), 32)
205+
await evm.runCall({
206+
gasLimit: BigInt(100_000),
207+
})
208+
const stored = evm.transientStorage.get(contractAddress, keyBuf)
209+
assert.ok(
210+
equalsBytes(unpadBytes(stored), new Uint8Array()),
211+
'Transient storage has been cleared'
212+
)
213+
})
179214
})

0 commit comments

Comments
 (0)