Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EVM: non-empty "empty" account bugfixes #2383

Merged
merged 3 commits into from
Oct 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions packages/evm/src/opcodes/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -513,15 +513,15 @@ export const handlers: Map<number, OpHandler> = new Map([
async function (runState) {
const addressBigInt = runState.stack.pop()
const address = new Address(addressToBuffer(addressBigInt))
const empty = (await runState.eei.getAccount(address)).isEmpty()
if (empty) {
const account = await runState.eei.getAccount(address)
const empty = account.isEmpty()
const origin = runState.interpreter.getTxOrigin()
if (empty && origin !== addressBigInt) {
runState.stack.push(BigInt(0))
return
}

const codeHash = (await runState.eei.getAccount(new Address(addressToBuffer(addressBigInt))))
.codeHash
runState.stack.push(BigInt('0x' + codeHash.toString('hex')))
runState.stack.push(BigInt('0x' + account.codeHash.toString('hex')))
},
],
// 0x3d: RETURNDATASIZE
Expand Down
10 changes: 7 additions & 3 deletions packages/evm/src/opcodes/gas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,11 @@ export const dynamicGasHandlers: Map<number, AsyncDynamicGasHandler | SyncDynami
if (common.gteHardfork(Hardfork.SpuriousDragon)) {
// We are at or after Spurious Dragon
// Call new account gas: account is DEAD and we transfer nonzero value
if ((await runState.eei.getAccount(toAddress)).isEmpty() && !(value === BigInt(0))) {
if (
(await runState.eei.getAccount(toAddress)).isEmpty() &&
!(value === BigInt(0)) &&
toAddr !== runState.interpreter.getTxOrigin()
) {
gas += common.param('gasPrices', 'callNewAccount')
}
} else if (!(await runState.eei.accountExists(toAddress))) {
Expand Down Expand Up @@ -509,7 +513,7 @@ export const dynamicGasHandlers: Map<number, AsyncDynamicGasHandler | SyncDynami
if (value > BigInt(0)) {
gas += common.param('gasPrices', 'authcallValueTransfer')
const account = await runState.eei.getAccount(toAddress)
if (account.isEmpty()) {
if (account.isEmpty() && addr !== runState.interpreter.getTxOrigin()) {
gas += common.param('gasPrices', 'callNewAccount')
}
}
Expand Down Expand Up @@ -585,7 +589,7 @@ export const dynamicGasHandlers: Map<number, AsyncDynamicGasHandler | SyncDynami
if (balance > BigInt(0)) {
// This technically checks if account is empty or non-existent
const empty = (await runState.eei.getAccount(selfdestructToAddress)).isEmpty()
if (empty) {
if (empty && selfdestructToaddressBigInt !== runState.interpreter.getTxOrigin()) {
deductGas = true
}
}
Expand Down
104 changes: 103 additions & 1 deletion packages/vm/test/api/runTx.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Block } from '@ethereumjs/block'
import { Blockchain } from '@ethereumjs/blockchain'
import { Chain, Common, Hardfork } from '@ethereumjs/common'
import { FeeMarketEIP1559Transaction, Transaction, TransactionFactory } from '@ethereumjs/tx'
import { Account, Address, MAX_INTEGER } from '@ethereumjs/util'
import { Account, Address, KECCAK256_NULL, MAX_INTEGER } from '@ethereumjs/util'
import * as tape from 'tape'

import { VM } from '../../src/vm'
Expand Down Expand Up @@ -680,3 +680,105 @@ tape('runTx() -> skipBalance behavior', async (t) => {
t.equal(res.execResult.exceptionError, undefined, 'no exceptionError with skipBalance')
}
})

tape(
'Validate EXTCODEHASH puts KECCAK256_NULL on stack if calling account has no balance and zero nonce (but it did exist)',
async (t) => {
const common = new Common({ chain: Chain.Mainnet, hardfork: Hardfork.Berlin })
const vm = await VM.create({ common })

const pkey = Buffer.alloc(32, 1)

// CALLER EXTCODEHASH PUSH 0 SSTORE STOP
// Puts EXTCODEHASH of CALLER into slot 0
const code = Buffer.from('333F60005500', 'hex')
const codeAddr = Address.fromString('0x' + '20'.repeat(20))
await vm.stateManager.putContractCode(codeAddr, code)

const tx: Transaction = Transaction.fromTxData({
gasLimit: 100000,
gasPrice: 1,
to: codeAddr,
}).sign(pkey)

const addr = Address.fromPrivateKey(pkey)
const acc = await vm.eei.getAccount(addr)
acc.balance = BigInt(tx.gasLimit * tx.gasPrice)
await vm.eei.putAccount(addr, acc)
await vm.runTx({ tx })

const hash = await vm.stateManager.getContractStorage(
codeAddr,
Buffer.from('00'.repeat(32), 'hex')
)
t.ok(hash.equals(KECCAK256_NULL), 'hash ok')

t.end()
}
)

tape(
'Validate CALL does not charge new account gas when calling CALLER and caller is non-empty',
async (t) => {
const common = new Common({ chain: Chain.Mainnet, hardfork: Hardfork.Berlin })
const vm = await VM.create({ common })

const pkey = Buffer.alloc(32, 1)

// PUSH 0 DUP DUP DUP
// CALLVALUE CALLER GAS
// CALL
// STOP

// Calls CALLER and sends back the ETH just sent with the transaction
const code = Buffer.from('600080808034335AF100', 'hex')
const codeAddr = Address.fromString('0x' + '20'.repeat(20))
await vm.stateManager.putContractCode(codeAddr, code)

const tx: Transaction = Transaction.fromTxData({
gasLimit: 100000,
gasPrice: 1,
value: 1,
to: codeAddr,
}).sign(pkey)

const addr = Address.fromPrivateKey(pkey)
const acc = await vm.eei.getAccount(addr)
acc.balance = BigInt(tx.gasLimit * tx.gasPrice + tx.value)
await vm.eei.putAccount(addr, acc)
t.equals((await vm.runTx({ tx })).totalGasSpent, BigInt(27818), 'did not charge callNewAccount')

t.end()
}
)

tape(
'Validate SELFDESTRUCT does not charge new account gas when calling CALLER and caller is non-empty',
async (t) => {
const common = new Common({ chain: Chain.Mainnet, hardfork: Hardfork.Berlin })
const vm = await VM.create({ common })

const pkey = Buffer.alloc(32, 1)

// CALLER EXTCODEHASH PUSH 0 SSTORE STOP
// Puts EXTCODEHASH of CALLER into slot 0
const code = Buffer.from('33FF', 'hex')
const codeAddr = Address.fromString('0x' + '20'.repeat(20))
await vm.stateManager.putContractCode(codeAddr, code)

const tx: Transaction = Transaction.fromTxData({
gasLimit: 100000,
gasPrice: 1,
value: 1,
to: codeAddr,
}).sign(pkey)

const addr = Address.fromPrivateKey(pkey)
const acc = await vm.eei.getAccount(addr)
acc.balance = BigInt(tx.gasLimit * tx.gasPrice + tx.value)
await vm.eei.putAccount(addr, acc)
t.equals((await vm.runTx({ tx })).totalGasSpent, BigInt(13001), 'did not charge callNewAccount')

t.end()
}
)