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

Fix nonce problem #2404

Merged
merged 4 commits into from
Dec 2, 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
19 changes: 14 additions & 5 deletions packages/evm/src/evm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -687,6 +687,7 @@ export class EVM implements EVMInterface {
*/
async runCall(opts: EVMRunCallOpts): Promise<EVMResult> {
let message = opts.message
let callerAccount
if (!message) {
this._block = opts.block ?? defaultBlock()
this._tx = {
Expand All @@ -698,7 +699,7 @@ export class EVM implements EVMInterface {

const value = opts.value ?? BigInt(0)
if (opts.skipBalance === true) {
const callerAccount = await this.eei.getAccount(caller)
callerAccount = await this.eei.getAccount(caller)
if (callerAccount.balance < value) {
// if skipBalance and balance less than value, set caller balance to `value` to ensure sufficient funds
callerAccount.balance = value
Expand All @@ -722,6 +723,17 @@ export class EVM implements EVMInterface {
})
}

if (message.depth === 0) {
if (!callerAccount) {
callerAccount = await this.eei.getAccount(message.caller)
}
callerAccount.nonce++
await this.eei.putAccount(message.caller, callerAccount)
if (this.DEBUG) {
debug(`Update fromAccount (caller) nonce (-> ${callerAccount.nonce}))`)
}
}

await this._emit('beforeMessage', message)

if (!message.to && this._common.isActivatedEIP(2929) === true) {
Expand Down Expand Up @@ -884,10 +896,7 @@ export class EVM implements EVMInterface {
addr = generateAddress2(message.caller.buf, message.salt, message.code as Buffer)
} else {
const acc = await this.eei.getAccount(message.caller)
let newNonce = acc.nonce
if (message.depth > 0) {
newNonce--
}
const newNonce = acc.nonce - BigInt(1)
addr = generateAddress(message.caller.buf, bigIntToBuffer(newNonce))
}
return new Address(addr)
Expand Down
4 changes: 1 addition & 3 deletions packages/evm/src/opcodes/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -514,9 +514,7 @@ export const handlers: Map<number, OpHandler> = new Map([
const addressBigInt = runState.stack.pop()
const address = new Address(addressToBuffer(addressBigInt))
const account = await runState.eei.getAccount(address)
const empty = account.isEmpty()
const origin = runState.interpreter.getTxOrigin()
if (empty && origin !== addressBigInt) {
if (account.isEmpty()) {
runState.stack.push(BigInt(0))
return
}
Expand Down
10 changes: 3 additions & 7 deletions packages/evm/src/opcodes/gas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -333,11 +333,7 @@ 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)) &&
toAddr !== runState.interpreter.getTxOrigin()
) {
if ((await runState.eei.getAccount(toAddress)).isEmpty() && !(value === BigInt(0))) {
gas += common.param('gasPrices', 'callNewAccount')
}
} else if (!(await runState.eei.accountExists(toAddress))) {
Expand Down Expand Up @@ -523,7 +519,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() && addr !== runState.interpreter.getTxOrigin()) {
if (account.isEmpty()) {
gas += common.param('gasPrices', 'callNewAccount')
}
}
Expand Down Expand Up @@ -599,7 +595,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 && selfdestructToaddressBigInt !== runState.interpreter.getTxOrigin()) {
if (empty) {
deductGas = true
}
}
Expand Down
7 changes: 1 addition & 6 deletions packages/vm/src/runTx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ async function _runTx(this: VM, opts: RunTxOpts): Promise<RunTxResult> {
}
await state.putAccount(caller, fromAccount)
if (this.DEBUG) {
debug(`Update fromAccount (caller) balance(-> ${fromAccount.balance})`)
debug(`Update fromAccount (caller) balance (-> ${fromAccount.balance}))`)
}

/*
Expand Down Expand Up @@ -337,11 +337,6 @@ async function _runTx(this: VM, opts: RunTxOpts): Promise<RunTxResult> {
data,
})) as RunTxResult

// After running the call, increment the nonce
const acc = await state.getAccount(caller)
acc.nonce++
await state.putAccount(caller, acc)

if (this.DEBUG) {
debug(`Update fromAccount (caller) nonce (-> ${fromAccount.nonce})`)
}
Expand Down