Skip to content

Commit

Permalink
cleanup and commit the k5 fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
g11tech committed Apr 11, 2024
1 parent 6499ad4 commit 60e30de
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 19 deletions.
7 changes: 5 additions & 2 deletions packages/client/test/rpc/engine/kaustinen5.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
})
}
}
Expand Down
3 changes: 2 additions & 1 deletion packages/evm/src/opcodes/EIP2929.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
107 changes: 91 additions & 16 deletions packages/evm/src/opcodes/gas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,23 +151,30 @@ export const dynamicGasHandlers: Map<number, AsyncDynamicGasHandler | SyncDynami
/* EXTCODESIZE */
0x3b,
async function (runState, gas, common): Promise<bigint> {
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
Expand All @@ -181,8 +188,29 @@ export const dynamicGasHandlers: Map<number, AsyncDynamicGasHandler | SyncDynami

gas += subMemUsage(runState, memOffset, dataLength, common)

let charge2929Gas = true
if (common.isActivatedEIP(6800) === true) {
const address = new Address(addresstoBytes(runState.stack.peek()[0]))

let coldAccessGas = BIGINT_0
coldAccessGas += runState.env.accessWitness!.touchAddressOnReadAndComputeGas(
address,
0,
VERSION_LEAF_KEY
)
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) {
gas += accessAddressEIP2929(runState, addresstoBytes(address), common)
gas += accessAddressEIP2929(runState, addresstoBytes(address), common, charge2929Gas)
}

if (dataLength !== BIGINT_0) {
Expand Down Expand Up @@ -229,17 +257,26 @@ export const dynamicGasHandlers: Map<number, AsyncDynamicGasHandler | SyncDynami
0x3f,
async function (runState, gas, common): Promise<bigint> {
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
},
],
Expand Down Expand Up @@ -737,12 +774,13 @@ export const dynamicGasHandlers: Map<number, AsyncDynamicGasHandler | SyncDynami
const selfdestructToaddressBigInt = runState.stack.peek()[0]

const selfdestructToAddress = new Address(addresstoBytes(selfdestructToaddressBigInt))
const contractAddress = runState.interpreter.getAddress()

let deductGas = false
const balance = await runState.interpreter.getExternalBalance(contractAddress)

if (common.gteHardfork(Hardfork.SpuriousDragon)) {
// EIP-161: State Trie Clearing
const balance = await runState.interpreter.getExternalBalance(
runState.interpreter.getAddress()
)
if (balance > BIGINT_0) {
// This technically checks if account is empty or non-existent
const account = await runState.stateManager.getAccount(selfdestructToAddress)
Expand All @@ -762,9 +800,46 @@ export const dynamicGasHandlers: Map<number, AsyncDynamicGasHandler | SyncDynami
gas += common.param('gasPrices', 'callNewAccount')
}

if (common.isActivatedEIP(2929) === true) {
if (common.isActivatedEIP(2929) === true && common.isActivatedEIP(6800) === false) {
gas += accessAddressEIP2929(runState, selfdestructToAddress.bytes, common, true, true)
}

if (common.isActivatedEIP(6800) === true) {
// read accesses for version and code size
gas += runState.env.accessWitness!.touchAddressOnReadAndComputeGas(
contractAddress,
0,
VERSION_LEAF_KEY
)
gas += runState.env.accessWitness!.touchAddressOnReadAndComputeGas(
contractAddress,
0,
CODE_SIZE_LEAF_KEY
)
gas += runState.env.accessWitness!.touchAddressOnReadAndComputeGas(
contractAddress,
0,
BALANCE_LEAF_KEY
)
gas += runState.env.accessWitness!.touchAddressOnReadAndComputeGas(
selfdestructToAddress,
0,
BALANCE_LEAF_KEY
)

if (balance > BIGINT_0) {
gas += runState.env.accessWitness!.touchAddressOnWriteAndComputeGas(
contractAddress,
0,
BALANCE_LEAF_KEY
)
gas += runState.env.accessWitness!.touchAddressOnWriteAndComputeGas(
selfdestructToAddress,
0,
BALANCE_LEAF_KEY
)
}
}
return gas
},
],
Expand Down

0 comments on commit 60e30de

Please sign in to comment.