Skip to content

Commit

Permalink
Ensure TransientStorage cleanups after Tx-level contract creation (#3625
Browse files Browse the repository at this point in the history
)

* 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]>
  • Loading branch information
2 people authored and holgerd77 committed Sep 9, 2024
1 parent 27e2c02 commit 8928f88
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
4 changes: 4 additions & 0 deletions packages/evm/src/evm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -733,6 +733,10 @@ export class EVM implements EVMInterface {
}
}

if (message.depth === 0) {
this.postMessageCleanup()
}

return {
createdAddress: message.to,
execResult: result,
Expand Down
37 changes: 36 additions & 1 deletion packages/evm/test/transientStorage.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Address } from '@ethereumjs/util'
import { Address, equalsBytes, hexToBytes, setLengthLeft, unpadBytes } from '@ethereumjs/util'
import { assert, describe, it } from 'vitest'

import { EVM } from '../src/index.js'
import { TransientStorage } from '../src/transientStorage.js'

describe('Transient Storage', () => {
Expand Down Expand Up @@ -176,4 +177,38 @@ describe('Transient Storage', () => {
transientStorage.revert()
assert.deepEqual(transientStorage.get(address, key), value1)
})

it('should cleanup after a message create', async () => {
const evm = await EVM.create()
// PUSH 1 PUSH 1 TSTORE
const code = hexToBytes('0x600160015D')
const keyBuf = setLengthLeft(new Uint8Array([1]), 32)
const result = await evm.runCall({
data: code,
gasLimit: BigInt(100_000),
})
const created = result.createdAddress!
const stored = evm.transientStorage.get(created, keyBuf)
assert.ok(
equalsBytes(unpadBytes(stored), new Uint8Array()),
'Transient storage has been cleared'
)
})

it('should cleanup after a message call', async () => {
const evm = await EVM.create()
const contractAddress = Address.zero()
// PUSH 1 PUSH 1 TSTORE
const code = hexToBytes('0x600160015D')
await evm.stateManager.putContractCode(contractAddress, code)
const keyBuf = setLengthLeft(new Uint8Array([1]), 32)
await evm.runCall({
gasLimit: BigInt(100_000),
})
const stored = evm.transientStorage.get(contractAddress, keyBuf)
assert.ok(
equalsBytes(unpadBytes(stored), new Uint8Array()),
'Transient storage has been cleared'
)
})
})

0 comments on commit 8928f88

Please sign in to comment.