Skip to content

Commit

Permalink
verkle: address merge conflicts and refactors
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrocheleau committed Jul 11, 2024
1 parent 71924c4 commit 1b6d696
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 80 deletions.
2 changes: 1 addition & 1 deletion packages/evm/src/opcodes/gas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ export const dynamicGasHandlers: Map<number, AsyncDynamicGasHandler | SyncDynami
coldAccessGas += runState.env.accessWitness!.touchAddressOnReadAndComputeGas(
codeAddress,
0,
VERKLE_BASIC_DATA_LEAF_KEY
VERKLE_CODE_HASH_LEAF_KEY
)

gas += coldAccessGas
Expand Down
10 changes: 6 additions & 4 deletions packages/statemanager/src/statelessVerkleStateManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import {
KECCAK256_NULL_S,
VerkleLeafType,
bigIntToBytes,
bytesToBigInt,
bytesToHex,
bytesToInt32,
decodeVerkleLeafBasicData,
encodeVerkleLeafBasicData,
getVerkleKey,
getVerkleStem,
getVerkleTreeKeyForCodeChunk,
Expand Down Expand Up @@ -549,7 +549,9 @@ export class StatelessVerkleStateManager implements EVMStateManagerInterface {
throw Error(errorMsg)
}

const { version, balance, nonce, codeSize } = decodeLeafBasicData(hexToBytes(basicDataRaw))
const { version, balance, nonce, codeSize } = decodeVerkleLeafBasicData(
hexToBytes(basicDataRaw)
)

const account = Account.fromPartialAccountData({
version,
Expand Down Expand Up @@ -582,7 +584,7 @@ export class StatelessVerkleStateManager implements EVMStateManagerInterface {
if (this._accountCacheSettings.deactivate) {
const stem = getVerkleStem(this.verkleCrypto, address, 0)
const basicDataKey = getVerkleKey(stem, VerkleLeafType.BasicData)
const basicDataBytes = encodeLeafBasicData({
const basicDataBytes = encodeVerkleLeafBasicData({
version: account.version,
balance: account.balance,
nonce: account.nonce,
Expand Down
62 changes: 62 additions & 0 deletions packages/util/src/verkle.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import {
bigIntToBytes,
bytesToBigInt,
bytesToInt32,
concatBytes,
int32ToBytes,
intToBytes,
Expand Down Expand Up @@ -124,6 +126,23 @@ export enum VerkleLeafType {
CodeHash = 1,
}

export type VerkleLeafBasicData = {
version: number
nonce: bigint
balance: bigint
codeSize: number
}

export const VERKLE_VERSION_OFFSET = 0
export const VERKLE_NONCE_OFFSET = 4
export const VERKLE_CODE_SIZE_OFFSET = 12
export const VERKLE_BALANCE_OFFSET = 16

export const VERKLE_VERSION_BYTES_LENGTH = 1
export const VERKLE_NONCE_BYTES_LENGTH = 8
export const VERKLE_CODE_SIZE_BYTES_LENGTH = 4
export const VERKLE_BALANCE_BYTES_LENGTH = 16

export const VERKLE_BASIC_DATA_LEAF_KEY = intToBytes(VerkleLeafType.BasicData)
export const VERKLE_CODE_HASH_LEAF_KEY = intToBytes(VerkleLeafType.CodeHash)

Expand Down Expand Up @@ -202,3 +221,46 @@ export const getVerkleTreeKeyForStorageSlot = async (

return concatBytes(getVerkleStem(verkleCrypto, address, treeIndex), toBytes(subIndex))
}

export function decodeVerkleLeafBasicData(encodedBasicData: Uint8Array): VerkleLeafBasicData {
const versionBytes = encodedBasicData.slice(0, VERKLE_VERSION_BYTES_LENGTH)
const nonceBytes = encodedBasicData.slice(
VERKLE_NONCE_OFFSET,
VERKLE_NONCE_OFFSET + VERKLE_NONCE_BYTES_LENGTH
)
const codeSizeBytes = encodedBasicData.slice(
VERKLE_CODE_SIZE_OFFSET,
VERKLE_CODE_SIZE_OFFSET + VERKLE_CODE_SIZE_BYTES_LENGTH
)
const balanceBytes = encodedBasicData.slice(
VERKLE_BALANCE_OFFSET,
VERKLE_BALANCE_OFFSET + VERKLE_BALANCE_BYTES_LENGTH
)

const version = bytesToInt32(versionBytes, true)
const nonce = bytesToBigInt(nonceBytes, true)
const codeSize = bytesToInt32(codeSizeBytes, true)
const balance = bytesToBigInt(balanceBytes, true)

return { version, nonce, codeSize, balance }
}

export function encodeVerkleLeafBasicData(basicData: VerkleLeafBasicData): Uint8Array {
const encodedVersion = setLengthRight(
int32ToBytes(basicData.version, true),
VERKLE_VERSION_BYTES_LENGTH
)
const encodedNonce = setLengthRight(
bigIntToBytes(basicData.nonce, true),
VERKLE_NONCE_BYTES_LENGTH
)
const encodedCodeSize = setLengthRight(
int32ToBytes(basicData.codeSize, true),
VERKLE_CODE_SIZE_BYTES_LENGTH
)
const encodedBalance = setLengthRight(
bigIntToBytes(basicData.balance, true),
VERKLE_BALANCE_BYTES_LENGTH
)
return concatBytes(encodedVersion, encodedNonce, encodedCodeSize, encodedBalance)
}
24 changes: 0 additions & 24 deletions packages/verkle/src/constants.ts

This file was deleted.

1 change: 0 additions & 1 deletion packages/verkle/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export * from './constants.js'
export * from './db/index.js'
export * from './node/index.js'
export * from './types.js'
Expand Down
51 changes: 1 addition & 50 deletions packages/verkle/src/node/util.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,10 @@
import { RLP } from '@ethereumjs/rlp'
import {
bigIntToBytes,
bytesToBigInt,
bytesToInt32,
concatBytes,
int32ToBytes,
setLengthRight,
} from '@ethereumjs/util'

import {
BALANCE_BYTES_LENGTH,
BALANCE_OFFSET,
CODE_SIZE_BYTES_LENGTH,
CODE_SIZE_OFFSET,
NONCE_BYTES_LENGTH,
NONCE_OFFSET,
VERSION_BYTES_LENGTH,
} from '../constants.js'
import { setLengthRight } from '@ethereumjs/util'

import { InternalNode } from './internalNode.js'
import { LeafNode } from './leafNode.js'
import { VerkleLeafNodeValue, type VerkleNode, VerkleNodeType } from './types.js'

import type { VerkleLeafBasicData } from '../types.js'
import type { VerkleCrypto } from '@ethereumjs/util'

export function decodeRawNode(raw: Uint8Array[], verkleCrypto: VerkleCrypto): VerkleNode {
Expand Down Expand Up @@ -113,34 +95,3 @@ export const createCValues = (values: (Uint8Array | VerkleLeafNodeValue)[]) => {
}
return expandedValues
}

export function decodeLeafBasicData(encodedBasicData: Uint8Array): VerkleLeafBasicData {
const versionBytes = encodedBasicData.slice(0, VERSION_BYTES_LENGTH)
const nonceBytes = encodedBasicData.slice(NONCE_OFFSET, NONCE_OFFSET + NONCE_BYTES_LENGTH)
const codeSizeBytes = encodedBasicData.slice(
CODE_SIZE_OFFSET,
CODE_SIZE_OFFSET + CODE_SIZE_BYTES_LENGTH
)
const balanceBytes = encodedBasicData.slice(BALANCE_OFFSET, BALANCE_OFFSET + BALANCE_BYTES_LENGTH)

const version = bytesToInt32(versionBytes, true)
const nonce = bytesToBigInt(nonceBytes, true)
const codeSize = bytesToInt32(codeSizeBytes, true)
const balance = bytesToBigInt(balanceBytes, true)

return { version, nonce, codeSize, balance }
}

export function encodeLeafBasicData(basicData: VerkleLeafBasicData): Uint8Array {
const encodedVersion = setLengthRight(int32ToBytes(basicData.version, true), VERSION_BYTES_LENGTH)
const encodedNonce = setLengthRight(bigIntToBytes(basicData.nonce, true), NONCE_BYTES_LENGTH)
const encodedCodeSize = setLengthRight(
int32ToBytes(basicData.codeSize, true),
CODE_SIZE_BYTES_LENGTH
)
const encodedBalance = setLengthRight(
bigIntToBytes(basicData.balance, true),
BALANCE_BYTES_LENGTH
)
return concatBytes(encodedVersion, encodedNonce, encodedCodeSize, encodedBalance)
}

0 comments on commit 1b6d696

Please sign in to comment.