Skip to content

Commit d4442ed

Browse files
authored
dynamic blob gas calculation (#3354)
* dynamic blob gas calculation * fix tests * fix additional tests * remove constants
1 parent d443601 commit d4442ed

File tree

9 files changed

+35
-28
lines changed

9 files changed

+35
-28
lines changed

execution_chain/common/common.nim

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,16 @@ func toHardFork*(
278278
com: CommonRef, forkDeterminer: ForkDeterminationInfo): HardFork =
279279
toHardFork(com.forkTransitionTable, forkDeterminer)
280280

281+
func toHardFork*(com: CommonRef, timestamp: EthTime): HardFork =
282+
for fork in countdown(com.forkTransitionTable.timeThresholds.high, Shanghai):
283+
if com.forkTransitionTable.timeThresholds[fork].isSome and timestamp >= com.forkTransitionTable.timeThresholds[fork].get:
284+
return fork
285+
286+
func toEVMFork*(com: CommonRef, timestamp: EthTime): EVMFork =
287+
## similar to toHardFork, but produce EVMFork
288+
let fork = com.toHardFork(timestamp)
289+
ToEVMFork[fork]
290+
281291
func toEVMFork*(com: CommonRef, forkDeterminer: ForkDeterminationInfo): EVMFork =
282292
## similar to toFork, but produce EVMFork
283293
let fork = com.toHardFork(forkDeterminer)

execution_chain/constants.nim

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,6 @@ const
8989
MAX_BLOB_GAS_PER_BLOCK* = 786432
9090
MAX_BLOBS_PER_BLOCK* = int(MAX_BLOB_GAS_PER_BLOCK div GAS_PER_BLOB)
9191

92-
MAX_BLOB_GAS_PER_BLOCK_ELECTRA* = 1179648
93-
TARGET_BLOB_GAS_PER_BLOCK_ELECTRA* = 786432
94-
MAX_BLOBS_PER_BLOCK_ELECTRA* = int(MAX_BLOB_GAS_PER_BLOCK_ELECTRA div GAS_PER_BLOB)
95-
9692
# EIP-4788 addresses
9793
# BEACON_ROOTS_ADDRESS is the address where historical beacon roots are stored as per EIP-4788
9894
BEACON_ROOTS_ADDRESS* = address"0x000F3df6D732807Ef1319fB7B8bB8522d0Beac02"

execution_chain/core/eip4844.nim

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,11 @@ proc pointEvaluation*(input: openArray[byte]): Result[void, string] =
8282
ok()
8383

8484
# calcExcessBlobGas implements calc_excess_data_gas from EIP-4844
85-
proc calcExcessBlobGas*(parent: Header, electra: bool): uint64 =
85+
proc calcExcessBlobGas*(com: CommonRef, parent: Header, fork: EVMFork): uint64 =
8686
let
8787
excessBlobGas = parent.excessBlobGas.get(0'u64)
8888
blobGasUsed = parent.blobGasUsed.get(0'u64)
89-
targetBlobGasPerBlock = getTargetBlobGasPerBlock(electra)
89+
targetBlobGasPerBlock = com.getTargetBlobsPerBlock(fork) * GAS_PER_BLOB
9090

9191
if excessBlobGas + blobGasUsed < targetBlobGasPerBlock:
9292
0'u64
@@ -157,12 +157,12 @@ func validateEip4844Header*(
157157
return err("expect EIP-4844 excessBlobGas in block header")
158158

159159
let
160-
electra = com.isPragueOrLater(header.timestamp)
160+
fork = com.toEVMFork(header)
161161
headerBlobGasUsed = header.blobGasUsed.get()
162162
blobGasUsed = blobGasUsed(txs)
163163
headerExcessBlobGas = header.excessBlobGas.get
164-
excessBlobGas = calcExcessBlobGas(parentHeader, electra)
165-
maxBlobGasPerBlock = getMaxBlobGasPerBlock(electra)
164+
excessBlobGas = calcExcessBlobGas(com, parentHeader, fork)
165+
maxBlobGasPerBlock = com.getMaxBlobGasPerBlock(fork)
166166

167167
if blobGasUsed > maxBlobGasPerBlock:
168168
return err("blobGasUsed " & $blobGasUsed & " exceeds maximum allowance " & $maxBlobGasPerBlock)

execution_chain/core/eip7691.nim

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,6 @@ import
1616
../common/evmforks,
1717
../common/common
1818

19-
func getMaxBlobGasPerBlock*(electra: bool): uint64 =
20-
if electra: MAX_BLOB_GAS_PER_BLOCK_ELECTRA.uint64
21-
else: MAX_BLOB_GAS_PER_BLOCK.uint64
22-
23-
func getTargetBlobGasPerBlock*(electra: bool): uint64 =
24-
if electra: TARGET_BLOB_GAS_PER_BLOCK_ELECTRA.uint64
25-
else: TARGET_BLOB_GAS_PER_BLOCK.uint64
26-
2719
const
2820
EVMForkToFork: array[FkCancun..FkLatest, HardFork] = [
2921
Cancun,
@@ -32,9 +24,19 @@ const
3224
]
3325

3426
func getMaxBlobsPerBlock*(com: CommonRef, fork: EVMFork): uint64 =
35-
doAssert(fork >= FkCancun)
27+
if fork < FkCancun:
28+
return 0
3629
com.maxBlobsPerBlock(EVMForkToFork[fork])
3730

31+
func getTargetBlobsPerBlock*(com: CommonRef, fork: EVMFork): uint64 =
32+
if fork < FkCancun:
33+
return 0
34+
com.targetBlobsPerBlock(EVMForkToFork[fork])
35+
3836
func getBlobBaseFeeUpdateFraction*(com: CommonRef, fork: EVMFork): uint64 =
39-
doAssert(fork >= FkCancun)
37+
if fork < FkCancun:
38+
return 0
4039
com.baseFeeUpdateFraction(EVMForkToFork[fork])
40+
41+
func getMaxBlobGasPerBlock*(com: CommonRef, fork: EVMFork): uint64 =
42+
com.getMaxBlobsPerBlock(fork) * GAS_PER_BLOB

execution_chain/core/executor/process_transaction.nim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ proc processTransactionImpl(
9494
# blobGasUsed will be added to vmState.blobGasUsed if the tx is ok.
9595
let
9696
blobGasUsed = tx.getTotalBlobGas
97-
maxBlobGasPerBlock = getMaxBlobGasPerBlock(vmState.fork >= FkPrague)
97+
maxBlobGasPerBlock = getMaxBlobGasPerBlock(vmState.com, vmState.fork)
9898
if vmState.blobGasUsed + blobGasUsed > maxBlobGasPerBlock:
9999
return err("blobGasUsed " & $blobGasUsed &
100100
" exceeds maximum allowance " & $maxBlobGasPerBlock)

execution_chain/core/tx_pool/tx_desc.nim

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ proc setupVMState(com: CommonRef;
8787
pos: PosPayloadAttr,
8888
parentFrame: CoreDbTxRef): BaseVMState =
8989
let
90-
electra = com.isPragueOrLater(pos.timestamp)
90+
fork = com.toEVMFork(pos.timestamp)
9191

9292
BaseVMState.new(
9393
parent = parent,
@@ -98,7 +98,7 @@ proc setupVMState(com: CommonRef;
9898
prevRandao : pos.prevRandao,
9999
difficulty : UInt256.zero(),
100100
coinbase : pos.feeRecipient,
101-
excessBlobGas: calcExcessBlobGas(parent, electra),
101+
excessBlobGas: com.calcExcessBlobGas(parent, fork),
102102
parentHash : parentHash,
103103
),
104104
txFrame = parentFrame.txFrameBegin(),

execution_chain/core/tx_pool/tx_packer.nim

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ proc classifyValidatePacked(vmState: BaseVMState; item: TxItemRef): bool =
7171
baseFee = vmState.blockCtx.baseFeePerGas.get(0.u256)
7272
fork = vmState.fork
7373
gasLimit = vmState.blockCtx.gasLimit
74-
excessBlobGas = calcExcessBlobGas(vmState.parent, fork >= FkPrague)
74+
excessBlobGas = calcExcessBlobGas(vmState.com, vmState.parent, fork)
7575

7676
roDB.validateTransaction(
7777
item.tx, item.sender, gasLimit, baseFee, excessBlobGas, vmState.com, fork).isOk
@@ -159,7 +159,6 @@ proc vmExecGrabItem(pst: var TxPacker; item: TxItemRef, xp: TxPoolRef): bool =
159159
## values are below the maximum block size.
160160
let
161161
vmState = pst.vmState
162-
electra = vmState.fork >= FkPrague
163162

164163
# EIP-4844
165164
if item.tx.txType == TxEip4844:
@@ -169,7 +168,7 @@ proc vmExecGrabItem(pst: var TxPacker; item: TxItemRef, xp: TxPoolRef): bool =
169168

170169
let
171170
blobGasUsed = item.tx.getTotalBlobGas
172-
maxBlobGasPerBlock = getMaxBlobGasPerBlock(electra)
171+
maxBlobGasPerBlock = getMaxBlobGasPerBlock(vmState.com, vmState.fork)
173172
if vmState.blobGasUsed + blobGasUsed > maxBlobGasPerBlock:
174173
return ContinueWithNextAccount
175174

hive_integration/nodocker/engine/cancun/step_newpayloads.nim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ proc verifyPayload(step: NewPayloads,
7373
excessBlobGas: Opt.some(parentExcessBlobGas),
7474
blobGasUsed: Opt.some(parentBlobGasUsed)
7575
)
76-
expectedExcessBlobGas = calcExcessBlobGas(parent, com.isPragueOrLater(payload.timestamp.EthTime))
76+
expectedExcessBlobGas = com.calcExcessBlobGas(parent, com.toEVMFork(payload.timestamp.EthTime))
7777

7878
if com.isCancunOrLater(payload.timestamp.EthTime):
7979
if payload.excessBlobGas.isNone:

tools/t8n/transition.nim

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ proc exec(ctx: TransContext,
351351
if ctx.env.currentExcessBlobGas.isSome:
352352
excessBlobGas = ctx.env.currentExcessBlobGas
353353
elif ctx.env.parentExcessBlobGas.isSome and ctx.env.parentBlobGasUsed.isSome:
354-
excessBlobGas = Opt.some calcExcessBlobGas(vmState.parent, vmState.fork >= FkPrague)
354+
excessBlobGas = Opt.some calcExcessBlobGas(vmState.com, vmState.parent, vmState.fork)
355355
356356
if excessBlobGas.isSome:
357357
result.result.blobGasUsed = Opt.some vmState.blobGasUsed
@@ -535,7 +535,7 @@ proc transitionAction*(ctx: var TransContext, conf: T8NConf) =
535535
# If it is not explicitly defined, but we have the parent values, we try
536536
# to calculate it ourselves.
537537
if parent.excessBlobGas.isSome and parent.blobGasUsed.isSome:
538-
ctx.env.currentExcessBlobGas = Opt.some calcExcessBlobGas(parent, com.isPragueOrLater(ctx.env.currentTimestamp))
538+
ctx.env.currentExcessBlobGas = Opt.some com.calcExcessBlobGas(parent, com.toEVMFork(ctx.env.currentTimestamp))
539539

540540
let header = envToHeader(ctx.env)
541541

0 commit comments

Comments
 (0)