Skip to content

Commit 22612f2

Browse files
authored
verkle: rename verkle utils and refactor (#3468)
* 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
1 parent 05552af commit 22612f2

File tree

17 files changed

+222
-404
lines changed

17 files changed

+222
-404
lines changed

packages/evm/src/opcodes/functions.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import {
2626
bytesToHex,
2727
concatBytes,
2828
ecrecover,
29-
getTreeIndexesForStorageSlot,
29+
getVerkleTreeIndexesForStorageSlot,
3030
hexToBytes,
3131
publicToAddress,
3232
setLengthLeft,
@@ -628,7 +628,7 @@ export const handlers: Map<number, OpHandler> = new Map([
628628
const key = setLengthLeft(bigIntToBytes(number % historyServeWindow), 32)
629629

630630
if (common.isActivatedEIP(6800)) {
631-
const { treeIndex, subIndex } = getTreeIndexesForStorageSlot(number)
631+
const { treeIndex, subIndex } = getVerkleTreeIndexesForStorageSlot(number)
632632
// create witnesses and charge gas
633633
const statelessGas = runState.env.accessWitness!.touchAddressOnReadAndComputeGas(
634634
historyAddress,

packages/evm/src/opcodes/gas.ts

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@ import { Hardfork } from '@ethereumjs/common'
22
import {
33
Account,
44
Address,
5-
BALANCE_LEAF_KEY,
65
BIGINT_0,
76
BIGINT_1,
87
BIGINT_3,
98
BIGINT_31,
109
BIGINT_32,
11-
CODE_HASH_LEAF_KEY,
12-
CODE_SIZE_LEAF_KEY,
13-
VERSION_LEAF_KEY,
10+
VERKLE_BALANCE_LEAF_KEY,
11+
VERKLE_CODE_HASH_LEAF_KEY,
12+
VERKLE_CODE_SIZE_LEAF_KEY,
13+
VERKLE_VERSION_LEAF_KEY,
1414
bigIntToBytes,
15-
getTreeIndexesForStorageSlot,
15+
getVerkleTreeIndexesForStorageSlot,
1616
setLengthLeft,
1717
} from '@ethereumjs/util'
1818

@@ -94,7 +94,7 @@ export const dynamicGasHandlers: Map<number, AsyncDynamicGasHandler | SyncDynami
9494
const coldAccessGas = runState.env.accessWitness!.touchAddressOnReadAndComputeGas(
9595
balanceAddress,
9696
0,
97-
BALANCE_LEAF_KEY
97+
VERKLE_BALANCE_LEAF_KEY
9898
)
9999

100100
gas += coldAccessGas
@@ -165,12 +165,12 @@ export const dynamicGasHandlers: Map<number, AsyncDynamicGasHandler | SyncDynami
165165
coldAccessGas += runState.env.accessWitness!.touchAddressOnReadAndComputeGas(
166166
address,
167167
0,
168-
VERSION_LEAF_KEY
168+
VERKLE_VERSION_LEAF_KEY
169169
)
170170
coldAccessGas += runState.env.accessWitness!.touchAddressOnReadAndComputeGas(
171171
address,
172172
0,
173-
CODE_SIZE_LEAF_KEY
173+
VERKLE_CODE_SIZE_LEAF_KEY
174174
)
175175

176176
gas += coldAccessGas
@@ -204,12 +204,12 @@ export const dynamicGasHandlers: Map<number, AsyncDynamicGasHandler | SyncDynami
204204
coldAccessGas += runState.env.accessWitness!.touchAddressOnReadAndComputeGas(
205205
address,
206206
0,
207-
VERSION_LEAF_KEY
207+
VERKLE_VERSION_LEAF_KEY
208208
)
209209
coldAccessGas += runState.env.accessWitness!.touchAddressOnReadAndComputeGas(
210210
address,
211211
0,
212-
CODE_SIZE_LEAF_KEY
212+
VERKLE_CODE_SIZE_LEAF_KEY
213213
)
214214

215215
gas += coldAccessGas
@@ -273,7 +273,7 @@ export const dynamicGasHandlers: Map<number, AsyncDynamicGasHandler | SyncDynami
273273
coldAccessGas += runState.env.accessWitness!.touchAddressOnReadAndComputeGas(
274274
codeAddress,
275275
0,
276-
CODE_HASH_LEAF_KEY
276+
VERKLE_CODE_HASH_LEAF_KEY
277277
)
278278

279279
gas += coldAccessGas
@@ -324,7 +324,7 @@ export const dynamicGasHandlers: Map<number, AsyncDynamicGasHandler | SyncDynami
324324
let charge2929Gas = true
325325
if (common.isActivatedEIP(6800)) {
326326
const address = runState.interpreter.getAddress()
327-
const { treeIndex, subIndex } = getTreeIndexesForStorageSlot(key)
327+
const { treeIndex, subIndex } = getVerkleTreeIndexesForStorageSlot(key)
328328
const coldAccessGas = runState.env.accessWitness!.touchAddressOnReadAndComputeGas(
329329
address,
330330
treeIndex,
@@ -392,7 +392,7 @@ export const dynamicGasHandlers: Map<number, AsyncDynamicGasHandler | SyncDynami
392392
let charge2929Gas = true
393393
if (common.isActivatedEIP(6800)) {
394394
const contract = runState.interpreter.getAddress()
395-
const { treeIndex, subIndex } = getTreeIndexesForStorageSlot(key)
395+
const { treeIndex, subIndex } = getVerkleTreeIndexesForStorageSlot(key)
396396
const coldAccessGas = runState.env.accessWitness!.touchAddressOnWriteAndComputeGas(
397397
contract,
398398
treeIndex,
@@ -848,40 +848,40 @@ export const dynamicGasHandlers: Map<number, AsyncDynamicGasHandler | SyncDynami
848848
gas += runState.env.accessWitness!.touchAddressOnReadAndComputeGas(
849849
contractAddress,
850850
0,
851-
VERSION_LEAF_KEY
851+
VERKLE_VERSION_LEAF_KEY
852852
)
853853
gas += runState.env.accessWitness!.touchAddressOnReadAndComputeGas(
854854
contractAddress,
855855
0,
856-
CODE_SIZE_LEAF_KEY
856+
VERKLE_CODE_SIZE_LEAF_KEY
857857
)
858858
}
859859

860860
gas += runState.env.accessWitness!.touchAddressOnReadAndComputeGas(
861861
contractAddress,
862862
0,
863-
BALANCE_LEAF_KEY
863+
VERKLE_BALANCE_LEAF_KEY
864864
)
865865
if (balance > BIGINT_0) {
866866
gas += runState.env.accessWitness!.touchAddressOnWriteAndComputeGas(
867867
contractAddress,
868868
0,
869-
BALANCE_LEAF_KEY
869+
VERKLE_BALANCE_LEAF_KEY
870870
)
871871
}
872872

873873
let selfDestructToColdAccessGas =
874874
runState.env.accessWitness!.touchAddressOnReadAndComputeGas(
875875
selfdestructToAddress,
876876
0,
877-
BALANCE_LEAF_KEY
877+
VERKLE_BALANCE_LEAF_KEY
878878
)
879879
if (balance > BIGINT_0) {
880880
selfDestructToColdAccessGas +=
881881
runState.env.accessWitness!.touchAddressOnWriteAndComputeGas(
882882
selfdestructToAddress,
883883
0,
884-
BALANCE_LEAF_KEY
884+
VERKLE_BALANCE_LEAF_KEY
885885
)
886886
}
887887

packages/statemanager/src/accessWitness.ts

Lines changed: 50 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
import {
2-
BALANCE_LEAF_KEY,
32
BIGINT_0,
4-
CODE_HASH_LEAF_KEY,
5-
CODE_OFFSET,
6-
CODE_SIZE_LEAF_KEY,
7-
HEADER_STORAGE_OFFSET,
8-
MAIN_STORAGE_OFFSET,
9-
NONCE_LEAF_KEY,
3+
VERKLE_BALANCE_LEAF_KEY,
4+
VERKLE_CODE_HASH_LEAF_KEY,
5+
VERKLE_CODE_OFFSET,
6+
VERKLE_CODE_SIZE_LEAF_KEY,
7+
VERKLE_HEADER_STORAGE_OFFSET,
8+
VERKLE_MAIN_STORAGE_OFFSET,
109
VERKLE_NODE_WIDTH,
11-
VERSION_LEAF_KEY,
10+
VERKLE_NONCE_LEAF_KEY,
11+
VERKLE_VERSION_LEAF_KEY,
1212
bytesToBigInt,
1313
bytesToHex,
14-
getKey,
15-
getStem,
16-
getTreeIndicesForCodeChunk,
14+
getVerkleKey,
15+
getVerkleStem,
16+
getVerkleTreeIndicesForCodeChunk,
1717
hexToBytes,
1818
intToBytes,
1919
} from '@ethereumjs/util'
@@ -90,79 +90,79 @@ export class AccessWitness implements AccessWitnessInterface {
9090
touchAndChargeProofOfAbsence(address: Address): bigint {
9191
let gas = BIGINT_0
9292

93-
gas += this.touchAddressOnReadAndComputeGas(address, 0, VERSION_LEAF_KEY)
94-
gas += this.touchAddressOnReadAndComputeGas(address, 0, BALANCE_LEAF_KEY)
95-
gas += this.touchAddressOnReadAndComputeGas(address, 0, CODE_SIZE_LEAF_KEY)
96-
gas += this.touchAddressOnReadAndComputeGas(address, 0, CODE_HASH_LEAF_KEY)
97-
gas += this.touchAddressOnReadAndComputeGas(address, 0, NONCE_LEAF_KEY)
93+
gas += this.touchAddressOnReadAndComputeGas(address, 0, VERKLE_VERSION_LEAF_KEY)
94+
gas += this.touchAddressOnReadAndComputeGas(address, 0, VERKLE_BALANCE_LEAF_KEY)
95+
gas += this.touchAddressOnReadAndComputeGas(address, 0, VERKLE_CODE_SIZE_LEAF_KEY)
96+
gas += this.touchAddressOnReadAndComputeGas(address, 0, VERKLE_CODE_HASH_LEAF_KEY)
97+
gas += this.touchAddressOnReadAndComputeGas(address, 0, VERKLE_NONCE_LEAF_KEY)
9898

9999
return gas
100100
}
101101

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

105-
gas += this.touchAddressOnReadAndComputeGas(address, 0, VERSION_LEAF_KEY)
106-
gas += this.touchAddressOnReadAndComputeGas(address, 0, CODE_SIZE_LEAF_KEY)
105+
gas += this.touchAddressOnReadAndComputeGas(address, 0, VERKLE_VERSION_LEAF_KEY)
106+
gas += this.touchAddressOnReadAndComputeGas(address, 0, VERKLE_CODE_SIZE_LEAF_KEY)
107107

108108
return gas
109109
}
110110

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

114-
gas += this.touchAddressOnWriteAndComputeGas(caller, 0, BALANCE_LEAF_KEY)
115-
gas += this.touchAddressOnWriteAndComputeGas(target, 0, BALANCE_LEAF_KEY)
114+
gas += this.touchAddressOnWriteAndComputeGas(caller, 0, VERKLE_BALANCE_LEAF_KEY)
115+
gas += this.touchAddressOnWriteAndComputeGas(target, 0, VERKLE_BALANCE_LEAF_KEY)
116116

117117
return gas
118118
}
119119

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

123-
gas += this.touchAddressOnWriteAndComputeGas(address, 0, VERSION_LEAF_KEY)
124-
gas += this.touchAddressOnWriteAndComputeGas(address, 0, NONCE_LEAF_KEY)
123+
gas += this.touchAddressOnWriteAndComputeGas(address, 0, VERKLE_VERSION_LEAF_KEY)
124+
gas += this.touchAddressOnWriteAndComputeGas(address, 0, VERKLE_NONCE_LEAF_KEY)
125125

126126
return gas
127127
}
128128

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

132-
gas += this.touchAddressOnWriteAndComputeGas(address, 0, VERSION_LEAF_KEY)
133-
gas += this.touchAddressOnWriteAndComputeGas(address, 0, BALANCE_LEAF_KEY)
134-
gas += this.touchAddressOnWriteAndComputeGas(address, 0, CODE_SIZE_LEAF_KEY)
135-
gas += this.touchAddressOnWriteAndComputeGas(address, 0, CODE_HASH_LEAF_KEY)
136-
gas += this.touchAddressOnWriteAndComputeGas(address, 0, NONCE_LEAF_KEY)
132+
gas += this.touchAddressOnWriteAndComputeGas(address, 0, VERKLE_VERSION_LEAF_KEY)
133+
gas += this.touchAddressOnWriteAndComputeGas(address, 0, VERKLE_BALANCE_LEAF_KEY)
134+
gas += this.touchAddressOnWriteAndComputeGas(address, 0, VERKLE_CODE_SIZE_LEAF_KEY)
135+
gas += this.touchAddressOnWriteAndComputeGas(address, 0, VERKLE_CODE_HASH_LEAF_KEY)
136+
gas += this.touchAddressOnWriteAndComputeGas(address, 0, VERKLE_NONCE_LEAF_KEY)
137137

138138
return gas
139139
}
140140

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

144-
gas += this.touchAddressOnReadAndComputeGas(origin, 0, VERSION_LEAF_KEY)
145-
gas += this.touchAddressOnReadAndComputeGas(origin, 0, CODE_SIZE_LEAF_KEY)
146-
gas += this.touchAddressOnReadAndComputeGas(origin, 0, CODE_HASH_LEAF_KEY)
144+
gas += this.touchAddressOnReadAndComputeGas(origin, 0, VERKLE_VERSION_LEAF_KEY)
145+
gas += this.touchAddressOnReadAndComputeGas(origin, 0, VERKLE_CODE_SIZE_LEAF_KEY)
146+
gas += this.touchAddressOnReadAndComputeGas(origin, 0, VERKLE_CODE_HASH_LEAF_KEY)
147147

148-
gas += this.touchAddressOnWriteAndComputeGas(origin, 0, NONCE_LEAF_KEY)
149-
gas += this.touchAddressOnWriteAndComputeGas(origin, 0, BALANCE_LEAF_KEY)
148+
gas += this.touchAddressOnWriteAndComputeGas(origin, 0, VERKLE_NONCE_LEAF_KEY)
149+
gas += this.touchAddressOnWriteAndComputeGas(origin, 0, VERKLE_BALANCE_LEAF_KEY)
150150

151151
return gas
152152
}
153153

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

157-
gas += this.touchAddressOnReadAndComputeGas(target, 0, VERSION_LEAF_KEY)
158-
gas += this.touchAddressOnReadAndComputeGas(target, 0, CODE_SIZE_LEAF_KEY)
159-
gas += this.touchAddressOnReadAndComputeGas(target, 0, CODE_HASH_LEAF_KEY)
160-
gas += this.touchAddressOnReadAndComputeGas(target, 0, NONCE_LEAF_KEY)
157+
gas += this.touchAddressOnReadAndComputeGas(target, 0, VERKLE_VERSION_LEAF_KEY)
158+
gas += this.touchAddressOnReadAndComputeGas(target, 0, VERKLE_CODE_SIZE_LEAF_KEY)
159+
gas += this.touchAddressOnReadAndComputeGas(target, 0, VERKLE_CODE_HASH_LEAF_KEY)
160+
gas += this.touchAddressOnReadAndComputeGas(target, 0, VERKLE_NONCE_LEAF_KEY)
161161

162162
if (sendsValue === true) {
163-
gas += this.touchAddressOnWriteAndComputeGas(target, 0, BALANCE_LEAF_KEY)
163+
gas += this.touchAddressOnWriteAndComputeGas(target, 0, VERKLE_BALANCE_LEAF_KEY)
164164
} else {
165-
gas += this.touchAddressOnReadAndComputeGas(target, 0, BALANCE_LEAF_KEY)
165+
gas += this.touchAddressOnReadAndComputeGas(target, 0, VERKLE_BALANCE_LEAF_KEY)
166166
}
167167

168168
return gas
@@ -171,7 +171,7 @@ export class AccessWitness implements AccessWitnessInterface {
171171
touchCodeChunksRangeOnReadAndChargeGas(contact: Address, startPc: number, endPc: number): bigint {
172172
let gas = BIGINT_0
173173
for (let chunkNum = Math.floor(startPc / 31); chunkNum <= Math.floor(endPc / 31); chunkNum++) {
174-
const { treeIndex, subIndex } = getTreeIndicesForCodeChunk(chunkNum)
174+
const { treeIndex, subIndex } = getVerkleTreeIndicesForCodeChunk(chunkNum)
175175
gas += this.touchAddressOnReadAndComputeGas(contact, treeIndex, subIndex)
176176
}
177177
return gas
@@ -184,7 +184,7 @@ export class AccessWitness implements AccessWitnessInterface {
184184
): bigint {
185185
let gas = BIGINT_0
186186
for (let chunkNum = Math.floor(startPc / 31); chunkNum <= Math.floor(endPc / 31); chunkNum++) {
187-
const { treeIndex, subIndex } = getTreeIndicesForCodeChunk(chunkNum)
187+
const { treeIndex, subIndex } = getVerkleTreeIndicesForCodeChunk(chunkNum)
188188
gas += this.touchAddressOnWriteAndComputeGas(contact, treeIndex, subIndex)
189189
}
190190
return gas
@@ -259,7 +259,7 @@ export class AccessWitness implements AccessWitnessInterface {
259259
// i.e. no fill cost is charged right now
260260
const chunkFill = false
261261

262-
const accessedStemKey = getStem(this.verkleCrypto, address, treeIndex)
262+
const accessedStemKey = getVerkleStem(this.verkleCrypto, address, treeIndex)
263263
const accessedStemHex = bytesToHex(accessedStemKey)
264264
let accessedStem = this.stems.get(accessedStemHex)
265265
if (accessedStem === undefined) {
@@ -268,7 +268,7 @@ export class AccessWitness implements AccessWitnessInterface {
268268
this.stems.set(accessedStemHex, accessedStem)
269269
}
270270

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

375-
if (position >= HEADER_STORAGE_OFFSET && position < CODE_OFFSET) {
376-
const slot = position - BigInt(HEADER_STORAGE_OFFSET)
375+
if (position >= VERKLE_HEADER_STORAGE_OFFSET && position < VERKLE_CODE_OFFSET) {
376+
const slot = position - BigInt(VERKLE_HEADER_STORAGE_OFFSET)
377377
return { type: AccessedStateType.Storage, slot }
378-
} else if (position >= CODE_OFFSET && position < MAIN_STORAGE_OFFSET) {
379-
const codeChunkIdx = Number(position) - CODE_OFFSET
378+
} else if (position >= VERKLE_CODE_OFFSET && position < VERKLE_MAIN_STORAGE_OFFSET) {
379+
const codeChunkIdx = Number(position) - VERKLE_CODE_OFFSET
380380
return { type: AccessedStateType.Code, codeOffset: codeChunkIdx * 31 }
381-
} else if (position >= MAIN_STORAGE_OFFSET) {
382-
const slot = BigInt(position - MAIN_STORAGE_OFFSET)
381+
} else if (position >= VERKLE_MAIN_STORAGE_OFFSET) {
382+
const slot = BigInt(position - VERKLE_MAIN_STORAGE_OFFSET)
383383
return { type: AccessedStateType.Storage, slot }
384384
} else {
385385
throw Error(

0 commit comments

Comments
 (0)