From 60e30dea46866e8c117355e59f5ca01c1132eab7 Mon Sep 17 00:00:00 2001 From: harkamal Date: Thu, 11 Apr 2024 14:31:21 +0530 Subject: [PATCH] cleanup and commit the k5 fixes --- .../client/test/rpc/engine/kaustinen5.spec.ts | 7 +- packages/evm/src/opcodes/EIP2929.ts | 3 +- packages/evm/src/opcodes/gas.ts | 107 +++++++++++++++--- 3 files changed, 98 insertions(+), 19 deletions(-) diff --git a/packages/client/test/rpc/engine/kaustinen5.spec.ts b/packages/client/test/rpc/engine/kaustinen5.spec.ts index 3e4d7083be..8d292af018 100644 --- a/packages/client/test/rpc/engine/kaustinen5.spec.ts +++ b/packages/client/test/rpc/engine/kaustinen5.spec.ts @@ -108,8 +108,11 @@ describe(`valid verkle network setup`, async () => { for (let i = startSlot; i <= endSlot; i++) { const execute = await fetchExecutionPayload(process.env.PEER_BEACON_URL, i) it(`run fetched block slot: ${i} number: ${execute.block_number}`, async () => { - await runBlock({ common, chain, rpc }, { parent, execute }, true) - parent = execute + try { + await runBlock({ common, chain, rpc }, { parent, execute }, true) + } finally { + parent = execute + } }) } } diff --git a/packages/evm/src/opcodes/EIP2929.ts b/packages/evm/src/opcodes/EIP2929.ts index d97ef6d4d9..6c16550a18 100644 --- a/packages/evm/src/opcodes/EIP2929.ts +++ b/packages/evm/src/opcodes/EIP2929.ts @@ -28,7 +28,8 @@ export function accessAddressEIP2929( // CREATE, CREATE2 opcodes have the address warmed for free. // selfdestruct beneficiary address reads are charged an *additional* cold access - if (chargeGas) { + // if verkle not activated + if (chargeGas && common.isActivatedEIP(6800) === false) { return common.param('gasPrices', 'coldaccountaccess') } // Warm: (selfdestruct beneficiary address reads are not charged when warm) diff --git a/packages/evm/src/opcodes/gas.ts b/packages/evm/src/opcodes/gas.ts index 29e48fc031..f51eced4e1 100644 --- a/packages/evm/src/opcodes/gas.ts +++ b/packages/evm/src/opcodes/gas.ts @@ -151,23 +151,30 @@ export const dynamicGasHandlers: Map { - if (common.isActivatedEIP(2929) === true) { - const address = runState.stack.peek()[0] - gas += accessAddressEIP2929(runState, addresstoBytes(address), common) - } - + let charge2929Gas = true if (common.isActivatedEIP(6800) === true) { const address = new Address(addresstoBytes(runState.stack.peek()[0])) - gas += runState.env.accessWitness!.touchAddressOnReadAndComputeGas( + + let coldAccessGas = BIGINT_0 + coldAccessGas += runState.env.accessWitness!.touchAddressOnReadAndComputeGas( address, 0, VERSION_LEAF_KEY ) - gas += runState.env.accessWitness!.touchAddressOnReadAndComputeGas( + coldAccessGas += runState.env.accessWitness!.touchAddressOnReadAndComputeGas( address, 0, CODE_SIZE_LEAF_KEY ) + + gas += coldAccessGas + // if cold access gas has been charged 2929 gas shouldn't be charged + charge2929Gas = coldAccessGas === BIGINT_0 + } + + if (common.isActivatedEIP(2929) === true) { + const address = runState.stack.peek()[0] + gas += accessAddressEIP2929(runState, addresstoBytes(address), common, charge2929Gas) } return gas @@ -181,8 +188,29 @@ export const dynamicGasHandlers: Map { const address = addresstoBytes(runState.stack.peek()[0]) - if (common.isActivatedEIP(2929) === true) { - gas += accessAddressEIP2929(runState, address, common) - } + let charge2929Gas = true + if (common.isActivatedEIP(6800) === true) { const codeAddress = new Address(address) - gas += runState.env.accessWitness!.touchAddressOnReadAndComputeGas( + + let coldAccessGas = BIGINT_0 + coldAccessGas += runState.env.accessWitness!.touchAddressOnReadAndComputeGas( codeAddress, 0, CODE_KECCAK_LEAF_KEY ) + + gas += coldAccessGas + charge2929Gas = coldAccessGas === BIGINT_0 } + + if (common.isActivatedEIP(2929) === true) { + gas += accessAddressEIP2929(runState, address, common, charge2929Gas) + } + return gas }, ], @@ -737,12 +774,13 @@ export const dynamicGasHandlers: Map BIGINT_0) { // This technically checks if account is empty or non-existent const account = await runState.stateManager.getAccount(selfdestructToAddress) @@ -762,9 +800,46 @@ export const dynamicGasHandlers: Map BIGINT_0) { + gas += runState.env.accessWitness!.touchAddressOnWriteAndComputeGas( + contractAddress, + 0, + BALANCE_LEAF_KEY + ) + gas += runState.env.accessWitness!.touchAddressOnWriteAndComputeGas( + selfdestructToAddress, + 0, + BALANCE_LEAF_KEY + ) + } + } return gas }, ],