Skip to content

Commit

Permalink
verkle: rename verkle utils and refactor (#3468)
Browse files Browse the repository at this point in the history
* verkle: rename verkle utils

* verkle: rename helper functions and remove duplicate helper files

* statemanager: rename import

* util: remove todo

* verkle: port over some tests to util
  • Loading branch information
gabrocheleau authored Jun 22, 2024
1 parent 05552af commit 22612f2
Show file tree
Hide file tree
Showing 17 changed files with 222 additions and 404 deletions.
4 changes: 2 additions & 2 deletions packages/evm/src/opcodes/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import {
bytesToHex,
concatBytes,
ecrecover,
getTreeIndexesForStorageSlot,
getVerkleTreeIndexesForStorageSlot,
hexToBytes,
publicToAddress,
setLengthLeft,
Expand Down Expand Up @@ -628,7 +628,7 @@ export const handlers: Map<number, OpHandler> = new Map([
const key = setLengthLeft(bigIntToBytes(number % historyServeWindow), 32)

if (common.isActivatedEIP(6800)) {
const { treeIndex, subIndex } = getTreeIndexesForStorageSlot(number)
const { treeIndex, subIndex } = getVerkleTreeIndexesForStorageSlot(number)
// create witnesses and charge gas
const statelessGas = runState.env.accessWitness!.touchAddressOnReadAndComputeGas(
historyAddress,
Expand Down
38 changes: 19 additions & 19 deletions packages/evm/src/opcodes/gas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@ import { Hardfork } from '@ethereumjs/common'
import {
Account,
Address,
BALANCE_LEAF_KEY,
BIGINT_0,
BIGINT_1,
BIGINT_3,
BIGINT_31,
BIGINT_32,
CODE_HASH_LEAF_KEY,
CODE_SIZE_LEAF_KEY,
VERSION_LEAF_KEY,
VERKLE_BALANCE_LEAF_KEY,
VERKLE_CODE_HASH_LEAF_KEY,
VERKLE_CODE_SIZE_LEAF_KEY,
VERKLE_VERSION_LEAF_KEY,
bigIntToBytes,
getTreeIndexesForStorageSlot,
getVerkleTreeIndexesForStorageSlot,
setLengthLeft,
} from '@ethereumjs/util'

Expand Down Expand Up @@ -94,7 +94,7 @@ export const dynamicGasHandlers: Map<number, AsyncDynamicGasHandler | SyncDynami
const coldAccessGas = runState.env.accessWitness!.touchAddressOnReadAndComputeGas(
balanceAddress,
0,
BALANCE_LEAF_KEY
VERKLE_BALANCE_LEAF_KEY
)

gas += coldAccessGas
Expand Down Expand Up @@ -165,12 +165,12 @@ export const dynamicGasHandlers: Map<number, AsyncDynamicGasHandler | SyncDynami
coldAccessGas += runState.env.accessWitness!.touchAddressOnReadAndComputeGas(
address,
0,
VERSION_LEAF_KEY
VERKLE_VERSION_LEAF_KEY
)
coldAccessGas += runState.env.accessWitness!.touchAddressOnReadAndComputeGas(
address,
0,
CODE_SIZE_LEAF_KEY
VERKLE_CODE_SIZE_LEAF_KEY
)

gas += coldAccessGas
Expand Down Expand Up @@ -204,12 +204,12 @@ export const dynamicGasHandlers: Map<number, AsyncDynamicGasHandler | SyncDynami
coldAccessGas += runState.env.accessWitness!.touchAddressOnReadAndComputeGas(
address,
0,
VERSION_LEAF_KEY
VERKLE_VERSION_LEAF_KEY
)
coldAccessGas += runState.env.accessWitness!.touchAddressOnReadAndComputeGas(
address,
0,
CODE_SIZE_LEAF_KEY
VERKLE_CODE_SIZE_LEAF_KEY
)

gas += coldAccessGas
Expand Down Expand Up @@ -273,7 +273,7 @@ export const dynamicGasHandlers: Map<number, AsyncDynamicGasHandler | SyncDynami
coldAccessGas += runState.env.accessWitness!.touchAddressOnReadAndComputeGas(
codeAddress,
0,
CODE_HASH_LEAF_KEY
VERKLE_CODE_HASH_LEAF_KEY
)

gas += coldAccessGas
Expand Down Expand Up @@ -324,7 +324,7 @@ export const dynamicGasHandlers: Map<number, AsyncDynamicGasHandler | SyncDynami
let charge2929Gas = true
if (common.isActivatedEIP(6800)) {
const address = runState.interpreter.getAddress()
const { treeIndex, subIndex } = getTreeIndexesForStorageSlot(key)
const { treeIndex, subIndex } = getVerkleTreeIndexesForStorageSlot(key)
const coldAccessGas = runState.env.accessWitness!.touchAddressOnReadAndComputeGas(
address,
treeIndex,
Expand Down Expand Up @@ -392,7 +392,7 @@ export const dynamicGasHandlers: Map<number, AsyncDynamicGasHandler | SyncDynami
let charge2929Gas = true
if (common.isActivatedEIP(6800)) {
const contract = runState.interpreter.getAddress()
const { treeIndex, subIndex } = getTreeIndexesForStorageSlot(key)
const { treeIndex, subIndex } = getVerkleTreeIndexesForStorageSlot(key)
const coldAccessGas = runState.env.accessWitness!.touchAddressOnWriteAndComputeGas(
contract,
treeIndex,
Expand Down Expand Up @@ -848,40 +848,40 @@ export const dynamicGasHandlers: Map<number, AsyncDynamicGasHandler | SyncDynami
gas += runState.env.accessWitness!.touchAddressOnReadAndComputeGas(
contractAddress,
0,
VERSION_LEAF_KEY
VERKLE_VERSION_LEAF_KEY
)
gas += runState.env.accessWitness!.touchAddressOnReadAndComputeGas(
contractAddress,
0,
CODE_SIZE_LEAF_KEY
VERKLE_CODE_SIZE_LEAF_KEY
)
}

gas += runState.env.accessWitness!.touchAddressOnReadAndComputeGas(
contractAddress,
0,
BALANCE_LEAF_KEY
VERKLE_BALANCE_LEAF_KEY
)
if (balance > BIGINT_0) {
gas += runState.env.accessWitness!.touchAddressOnWriteAndComputeGas(
contractAddress,
0,
BALANCE_LEAF_KEY
VERKLE_BALANCE_LEAF_KEY
)
}

let selfDestructToColdAccessGas =
runState.env.accessWitness!.touchAddressOnReadAndComputeGas(
selfdestructToAddress,
0,
BALANCE_LEAF_KEY
VERKLE_BALANCE_LEAF_KEY
)
if (balance > BIGINT_0) {
selfDestructToColdAccessGas +=
runState.env.accessWitness!.touchAddressOnWriteAndComputeGas(
selfdestructToAddress,
0,
BALANCE_LEAF_KEY
VERKLE_BALANCE_LEAF_KEY
)
}

Expand Down
100 changes: 50 additions & 50 deletions packages/statemanager/src/accessWitness.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import {
BALANCE_LEAF_KEY,
BIGINT_0,
CODE_HASH_LEAF_KEY,
CODE_OFFSET,
CODE_SIZE_LEAF_KEY,
HEADER_STORAGE_OFFSET,
MAIN_STORAGE_OFFSET,
NONCE_LEAF_KEY,
VERKLE_BALANCE_LEAF_KEY,
VERKLE_CODE_HASH_LEAF_KEY,
VERKLE_CODE_OFFSET,
VERKLE_CODE_SIZE_LEAF_KEY,
VERKLE_HEADER_STORAGE_OFFSET,
VERKLE_MAIN_STORAGE_OFFSET,
VERKLE_NODE_WIDTH,
VERSION_LEAF_KEY,
VERKLE_NONCE_LEAF_KEY,
VERKLE_VERSION_LEAF_KEY,
bytesToBigInt,
bytesToHex,
getKey,
getStem,
getTreeIndicesForCodeChunk,
getVerkleKey,
getVerkleStem,
getVerkleTreeIndicesForCodeChunk,
hexToBytes,
intToBytes,
} from '@ethereumjs/util'
Expand Down Expand Up @@ -90,79 +90,79 @@ export class AccessWitness implements AccessWitnessInterface {
touchAndChargeProofOfAbsence(address: Address): bigint {
let gas = BIGINT_0

gas += this.touchAddressOnReadAndComputeGas(address, 0, VERSION_LEAF_KEY)
gas += this.touchAddressOnReadAndComputeGas(address, 0, BALANCE_LEAF_KEY)
gas += this.touchAddressOnReadAndComputeGas(address, 0, CODE_SIZE_LEAF_KEY)
gas += this.touchAddressOnReadAndComputeGas(address, 0, CODE_HASH_LEAF_KEY)
gas += this.touchAddressOnReadAndComputeGas(address, 0, NONCE_LEAF_KEY)
gas += this.touchAddressOnReadAndComputeGas(address, 0, VERKLE_VERSION_LEAF_KEY)
gas += this.touchAddressOnReadAndComputeGas(address, 0, VERKLE_BALANCE_LEAF_KEY)
gas += this.touchAddressOnReadAndComputeGas(address, 0, VERKLE_CODE_SIZE_LEAF_KEY)
gas += this.touchAddressOnReadAndComputeGas(address, 0, VERKLE_CODE_HASH_LEAF_KEY)
gas += this.touchAddressOnReadAndComputeGas(address, 0, VERKLE_NONCE_LEAF_KEY)

return gas
}

touchAndChargeMessageCall(address: Address): bigint {
let gas = BIGINT_0

gas += this.touchAddressOnReadAndComputeGas(address, 0, VERSION_LEAF_KEY)
gas += this.touchAddressOnReadAndComputeGas(address, 0, CODE_SIZE_LEAF_KEY)
gas += this.touchAddressOnReadAndComputeGas(address, 0, VERKLE_VERSION_LEAF_KEY)
gas += this.touchAddressOnReadAndComputeGas(address, 0, VERKLE_CODE_SIZE_LEAF_KEY)

return gas
}

touchAndChargeValueTransfer(caller: Address, target: Address): bigint {
let gas = BIGINT_0

gas += this.touchAddressOnWriteAndComputeGas(caller, 0, BALANCE_LEAF_KEY)
gas += this.touchAddressOnWriteAndComputeGas(target, 0, BALANCE_LEAF_KEY)
gas += this.touchAddressOnWriteAndComputeGas(caller, 0, VERKLE_BALANCE_LEAF_KEY)
gas += this.touchAddressOnWriteAndComputeGas(target, 0, VERKLE_BALANCE_LEAF_KEY)

return gas
}

touchAndChargeContractCreateInit(address: Address): bigint {
let gas = BIGINT_0

gas += this.touchAddressOnWriteAndComputeGas(address, 0, VERSION_LEAF_KEY)
gas += this.touchAddressOnWriteAndComputeGas(address, 0, NONCE_LEAF_KEY)
gas += this.touchAddressOnWriteAndComputeGas(address, 0, VERKLE_VERSION_LEAF_KEY)
gas += this.touchAddressOnWriteAndComputeGas(address, 0, VERKLE_NONCE_LEAF_KEY)

return gas
}

touchAndChargeContractCreateCompleted(address: Address): bigint {
let gas = BIGINT_0

gas += this.touchAddressOnWriteAndComputeGas(address, 0, VERSION_LEAF_KEY)
gas += this.touchAddressOnWriteAndComputeGas(address, 0, BALANCE_LEAF_KEY)
gas += this.touchAddressOnWriteAndComputeGas(address, 0, CODE_SIZE_LEAF_KEY)
gas += this.touchAddressOnWriteAndComputeGas(address, 0, CODE_HASH_LEAF_KEY)
gas += this.touchAddressOnWriteAndComputeGas(address, 0, NONCE_LEAF_KEY)
gas += this.touchAddressOnWriteAndComputeGas(address, 0, VERKLE_VERSION_LEAF_KEY)
gas += this.touchAddressOnWriteAndComputeGas(address, 0, VERKLE_BALANCE_LEAF_KEY)
gas += this.touchAddressOnWriteAndComputeGas(address, 0, VERKLE_CODE_SIZE_LEAF_KEY)
gas += this.touchAddressOnWriteAndComputeGas(address, 0, VERKLE_CODE_HASH_LEAF_KEY)
gas += this.touchAddressOnWriteAndComputeGas(address, 0, VERKLE_NONCE_LEAF_KEY)

return gas
}

touchTxOriginAndComputeGas(origin: Address): bigint {
let gas = BIGINT_0

gas += this.touchAddressOnReadAndComputeGas(origin, 0, VERSION_LEAF_KEY)
gas += this.touchAddressOnReadAndComputeGas(origin, 0, CODE_SIZE_LEAF_KEY)
gas += this.touchAddressOnReadAndComputeGas(origin, 0, CODE_HASH_LEAF_KEY)
gas += this.touchAddressOnReadAndComputeGas(origin, 0, VERKLE_VERSION_LEAF_KEY)
gas += this.touchAddressOnReadAndComputeGas(origin, 0, VERKLE_CODE_SIZE_LEAF_KEY)
gas += this.touchAddressOnReadAndComputeGas(origin, 0, VERKLE_CODE_HASH_LEAF_KEY)

gas += this.touchAddressOnWriteAndComputeGas(origin, 0, NONCE_LEAF_KEY)
gas += this.touchAddressOnWriteAndComputeGas(origin, 0, BALANCE_LEAF_KEY)
gas += this.touchAddressOnWriteAndComputeGas(origin, 0, VERKLE_NONCE_LEAF_KEY)
gas += this.touchAddressOnWriteAndComputeGas(origin, 0, VERKLE_BALANCE_LEAF_KEY)

return gas
}

touchTxTargetAndComputeGas(target: Address, { sendsValue }: { sendsValue?: boolean } = {}) {
let gas = BIGINT_0

gas += this.touchAddressOnReadAndComputeGas(target, 0, VERSION_LEAF_KEY)
gas += this.touchAddressOnReadAndComputeGas(target, 0, CODE_SIZE_LEAF_KEY)
gas += this.touchAddressOnReadAndComputeGas(target, 0, CODE_HASH_LEAF_KEY)
gas += this.touchAddressOnReadAndComputeGas(target, 0, NONCE_LEAF_KEY)
gas += this.touchAddressOnReadAndComputeGas(target, 0, VERKLE_VERSION_LEAF_KEY)
gas += this.touchAddressOnReadAndComputeGas(target, 0, VERKLE_CODE_SIZE_LEAF_KEY)
gas += this.touchAddressOnReadAndComputeGas(target, 0, VERKLE_CODE_HASH_LEAF_KEY)
gas += this.touchAddressOnReadAndComputeGas(target, 0, VERKLE_NONCE_LEAF_KEY)

if (sendsValue === true) {
gas += this.touchAddressOnWriteAndComputeGas(target, 0, BALANCE_LEAF_KEY)
gas += this.touchAddressOnWriteAndComputeGas(target, 0, VERKLE_BALANCE_LEAF_KEY)
} else {
gas += this.touchAddressOnReadAndComputeGas(target, 0, BALANCE_LEAF_KEY)
gas += this.touchAddressOnReadAndComputeGas(target, 0, VERKLE_BALANCE_LEAF_KEY)
}

return gas
Expand All @@ -171,7 +171,7 @@ export class AccessWitness implements AccessWitnessInterface {
touchCodeChunksRangeOnReadAndChargeGas(contact: Address, startPc: number, endPc: number): bigint {
let gas = BIGINT_0
for (let chunkNum = Math.floor(startPc / 31); chunkNum <= Math.floor(endPc / 31); chunkNum++) {
const { treeIndex, subIndex } = getTreeIndicesForCodeChunk(chunkNum)
const { treeIndex, subIndex } = getVerkleTreeIndicesForCodeChunk(chunkNum)
gas += this.touchAddressOnReadAndComputeGas(contact, treeIndex, subIndex)
}
return gas
Expand All @@ -184,7 +184,7 @@ export class AccessWitness implements AccessWitnessInterface {
): bigint {
let gas = BIGINT_0
for (let chunkNum = Math.floor(startPc / 31); chunkNum <= Math.floor(endPc / 31); chunkNum++) {
const { treeIndex, subIndex } = getTreeIndicesForCodeChunk(chunkNum)
const { treeIndex, subIndex } = getVerkleTreeIndicesForCodeChunk(chunkNum)
gas += this.touchAddressOnWriteAndComputeGas(contact, treeIndex, subIndex)
}
return gas
Expand Down Expand Up @@ -259,7 +259,7 @@ export class AccessWitness implements AccessWitnessInterface {
// i.e. no fill cost is charged right now
const chunkFill = false

const accessedStemKey = getStem(this.verkleCrypto, address, treeIndex)
const accessedStemKey = getVerkleStem(this.verkleCrypto, address, treeIndex)
const accessedStemHex = bytesToHex(accessedStemKey)
let accessedStem = this.stems.get(accessedStemHex)
if (accessedStem === undefined) {
Expand All @@ -268,7 +268,7 @@ export class AccessWitness implements AccessWitnessInterface {
this.stems.set(accessedStemHex, accessedStem)
}

const accessedChunkKey = getKey(
const accessedChunkKey = getVerkleKey(
accessedStemKey,
typeof subIndex === 'number' ? intToBytes(subIndex) : subIndex
)
Expand Down Expand Up @@ -368,18 +368,18 @@ export function decodeAccessedState(treeIndex: number | bigint, chunkIndex: numb
case BigInt(4):
return { type: AccessedStateType.CodeSize }
default:
if (position < HEADER_STORAGE_OFFSET) {
throw Error(`No attribute yet stored >=5 and <${HEADER_STORAGE_OFFSET}`)
if (position < VERKLE_HEADER_STORAGE_OFFSET) {
throw Error(`No attribute yet stored >=5 and <${VERKLE_HEADER_STORAGE_OFFSET}`)
}

if (position >= HEADER_STORAGE_OFFSET && position < CODE_OFFSET) {
const slot = position - BigInt(HEADER_STORAGE_OFFSET)
if (position >= VERKLE_HEADER_STORAGE_OFFSET && position < VERKLE_CODE_OFFSET) {
const slot = position - BigInt(VERKLE_HEADER_STORAGE_OFFSET)
return { type: AccessedStateType.Storage, slot }
} else if (position >= CODE_OFFSET && position < MAIN_STORAGE_OFFSET) {
const codeChunkIdx = Number(position) - CODE_OFFSET
} else if (position >= VERKLE_CODE_OFFSET && position < VERKLE_MAIN_STORAGE_OFFSET) {
const codeChunkIdx = Number(position) - VERKLE_CODE_OFFSET
return { type: AccessedStateType.Code, codeOffset: codeChunkIdx * 31 }
} else if (position >= MAIN_STORAGE_OFFSET) {
const slot = BigInt(position - MAIN_STORAGE_OFFSET)
} else if (position >= VERKLE_MAIN_STORAGE_OFFSET) {
const slot = BigInt(position - VERKLE_MAIN_STORAGE_OFFSET)
return { type: AccessedStateType.Storage, slot }
} else {
throw Error(
Expand Down
Loading

0 comments on commit 22612f2

Please sign in to comment.