Skip to content

Commit

Permalink
VM: exit early on non-existing system contracts (#3614)
Browse files Browse the repository at this point in the history
* VM: exit early on non-existing system contracts

* correctly exit early on requests

* vm: correctly exit early on system contracts, un-bumping system account nonce

* Merge branch 'master' into early-fail-system
  • Loading branch information
jochem-brouwer authored Sep 2, 2024
1 parent 0639af1 commit db0e8b2
Showing 1 changed file with 24 additions and 32 deletions.
56 changes: 24 additions & 32 deletions packages/vm/src/requests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,25 +66,28 @@ const accumulateEIP7002Requests = async (
)
const withdrawalsAddress = createAddressFromString(bytesToHex(addressBytes))

const code = await vm.stateManager.getCode(withdrawalsAddress)

if (code.length === 0) {
throw new Error(
'Attempt to accumulate EIP-7002 requests failed: the contract does not exist. Ensure the deployment tx has been run, or that the required contract code is stored',
)
}

const systemAddressBytes = bigIntToAddressBytes(vm.common.param('systemAddress'))
const systemAddress = createAddressFromString(bytesToHex(systemAddressBytes))
const systemAccount = await vm.stateManager.getAccount(systemAddress)

const originalAccount = await vm.stateManager.getAccount(withdrawalsAddress)

const originalAccount = await vm.stateManager.getAccount(systemAddress)
if (originalAccount === undefined) {
return
}

const results = await vm.evm.runCall({
caller: systemAddress,
gasLimit: BigInt(1_000_000),
to: withdrawalsAddress,
})

if (systemAccount === undefined) {
await vm.stateManager.deleteAccount(systemAddress)
} else {
await vm.stateManager.putAccount(systemAddress, systemAccount)
}

const resultsBytes = results.execResult.returnValue
if (resultsBytes.length > 0) {
// Each request is 76 bytes
Expand All @@ -96,13 +99,6 @@ const accumulateEIP7002Requests = async (
requests.push(createWithdrawalRequest({ sourceAddress, validatorPubkey, amount }))
}
}

if (originalAccount === undefined) {
await vm.stateManager.deleteAccount(systemAddress)
} else {
// Restore the original account (the `runCall` updates the nonce)
await vm.stateManager.putAccount(systemAddress, originalAccount)
}
}

const accumulateEIP7251Requests = async (
Expand All @@ -116,25 +112,28 @@ const accumulateEIP7251Requests = async (
)
const consolidationsAddress = createAddressFromString(bytesToHex(addressBytes))

const code = await vm.stateManager.getCode(consolidationsAddress)

if (code.length === 0) {
throw new Error(
'Attempt to accumulate EIP-7251 requests failed: the contract does not exist. Ensure the deployment tx has been run, or that the required contract code is stored',
)
}

const systemAddressBytes = bigIntToAddressBytes(vm.common.param('systemAddress'))
const systemAddress = createAddressFromString(bytesToHex(systemAddressBytes))
const systemAccount = await vm.stateManager.getAccount(systemAddress)

const originalAccount = await vm.stateManager.getAccount(consolidationsAddress)

const originalAccount = await vm.stateManager.getAccount(systemAddress)
if (originalAccount === undefined) {
return
}

const results = await vm.evm.runCall({
caller: systemAddress,
gasLimit: BigInt(1_000_000),
to: consolidationsAddress,
})

if (systemAccount === undefined) {
await vm.stateManager.deleteAccount(systemAddress)
} else {
await vm.stateManager.putAccount(systemAddress, systemAccount)
}

const resultsBytes = results.execResult.returnValue
if (resultsBytes.length > 0) {
// Each request is 116 bytes
Expand All @@ -146,13 +145,6 @@ const accumulateEIP7251Requests = async (
requests.push(createConsolidationRequest({ sourceAddress, sourcePubkey, targetPubkey }))
}
}

if (originalAccount === undefined) {
await vm.stateManager.deleteAccount(systemAddress)
} else {
// Restore the original account (the `runCall` updates the nonce)
await vm.stateManager.putAccount(systemAddress, originalAccount)
}
}

const accumulateDeposits = async (
Expand Down

0 comments on commit db0e8b2

Please sign in to comment.