Skip to content

Commit

Permalink
rename data gas to blob gas
Browse files Browse the repository at this point in the history
  • Loading branch information
jangko committed Jul 28, 2023
1 parent 31cff91 commit f047394
Show file tree
Hide file tree
Showing 24 changed files with 135 additions and 127 deletions.
8 changes: 4 additions & 4 deletions nimbus/common/chain_config.nim
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ type
gasUser* : GasInt
parentHash* : Hash256
baseFeePerGas*: Option[UInt256]
dataGasUsed* : Option[uint64] # EIP-4844
excessDataGas*: Option[uint64] # EIP-4844
blobGasUsed* : Option[uint64] # EIP-4844
excessBlobGas*: Option[uint64] # EIP-4844

GenesisAlloc* = Table[EthAddress, GenesisAccount]
GenesisAccount* = object
Expand Down Expand Up @@ -67,8 +67,8 @@ type
gasUser* : GasInt
parentHash* : Hash256
baseFeePerGas*: Option[UInt256]
dataGasUsed* : Option[uint64] # EIP-4844
excessDataGas*: Option[uint64] # EIP-4844
blobGasUsed* : Option[uint64] # EIP-4844
excessBlobGas*: Option[uint64] # EIP-4844

const
CustomNet* = 0.NetworkId
Expand Down
4 changes: 2 additions & 2 deletions nimbus/common/genesis.nim
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ proc toGenesisHeader*(
result.withdrawalsRoot = some(EMPTY_ROOT_HASH)

if fork >= Cancun:
result.dataGasUsed = g.dataGasUsed
result.excessDataGas = g.excessDataGas
result.blobGasUsed = g.blobGasUsed
result.excessBlobGas = g.excessBlobGas

proc toGenesisHeader*(
genesis: Genesis;
Expand Down
12 changes: 6 additions & 6 deletions nimbus/constants.nim
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,11 @@ const
MAX_TX_WRAP_COMMITMENTS* = 1 shl 12 # 2^12
BLOB_COMMITMENT_VERSION_KZG* = 0x01.byte
FIELD_ELEMENTS_PER_BLOB* = 4096
DATA_GAS_PER_BLOB* = (1 shl 17).uint64 # 2^17
TARGET_DATA_GAS_PER_BLOCK* = (1 shl 18).uint64 # 2^18
MIN_DATA_GASPRICE* = 1'u64
DATA_GASPRICE_UPDATE_FRACTION* = 2225652'u64
MAX_DATA_GAS_PER_BLOCK* = (1 shl 19).uint64 # 2^19
MaxAllowedBlob* = MAX_DATA_GAS_PER_BLOCK div DATA_GAS_PER_BLOB
GAS_PER_BLOB* = (1 shl 17).uint64 # 2^17
TARGET_BLOB_GAS_PER_BLOCK* = (1 shl 18).uint64 # 2^18
MIN_BLOB_GASPRICE* = 1'u64
BLOB_GASPRICE_UPDATE_FRACTION* = 2225652'u64
MAX_BLOB_GAS_PER_BLOCK* = (1 shl 19).uint64 # 2^19
MaxAllowedBlob* = MAX_BLOB_GAS_PER_BLOCK div GAS_PER_BLOB

# End
82 changes: 41 additions & 41 deletions nimbus/core/eip4844.nim
Original file line number Diff line number Diff line change
Expand Up @@ -77,16 +77,16 @@ proc pointEvaluation*(input: openArray[byte]): Result[void, string] =

ok()

# calcExcessDataGas implements calc_excess_data_gas from EIP-4844
proc calcExcessDataGas*(parent: BlockHeader): uint64 =
# calcExcessBlobGas implements calc_excess_data_gas from EIP-4844
proc calcExcessBlobGas*(parent: BlockHeader): uint64 =
let
excessDataGas = parent.excessDataGas.get(0'u64)
dataGasUsed = parent.dataGasUsed.get(0'u64)
excessBlobGas = parent.excessBlobGas.get(0'u64)
blobGasUsed = parent.blobGasUsed.get(0'u64)

if excessDataGas + dataGasUsed < TARGET_DATA_GAS_PER_BLOCK:
if excessBlobGas + blobGasUsed < TARGET_BLOB_GAS_PER_BLOCK:
0'u64
else:
excessDataGas + dataGasUsed - TARGET_DATA_GAS_PER_BLOCK
excessBlobGas + blobGasUsed - TARGET_BLOB_GAS_PER_BLOCK

# fakeExponential approximates factor * e ** (num / denom) using a taylor expansion
# as described in the EIP-4844 spec.
Expand All @@ -103,68 +103,68 @@ func fakeExponential*(factor, numerator, denominator: uint64): uint64 =

output div denominator

proc getTotalDataGas*(tx: Transaction): uint64 =
DATA_GAS_PER_BLOB * tx.versionedHashes.len.uint64
proc getTotalBlobGas*(tx: Transaction): uint64 =
GAS_PER_BLOB * tx.versionedHashes.len.uint64

proc getTotalDataGas*(versionedHashesLen: int): uint64 =
DATA_GAS_PER_BLOB * versionedHasheslen.uint64
proc getTotalBlobGas*(versionedHashesLen: int): uint64 =
GAS_PER_BLOB * versionedHasheslen.uint64

# getDataGasPrice implements get_data_gas_price from EIP-4844
func getDataGasprice*(parentExcessDataGas: uint64): uint64 =
# getBlobGasPrice implements get_data_gas_price from EIP-4844
func getBlobGasprice*(parentExcessBlobGas: uint64): uint64 =
fakeExponential(
MIN_DATA_GASPRICE,
parentExcessDataGas,
DATA_GASPRICE_UPDATE_FRACTION
MIN_BLOB_GASPRICE,
parentExcessBlobGas,
BLOB_GASPRICE_UPDATE_FRACTION
)

proc calcDataFee*(tx: Transaction,
parentExcessDataGas: Option[uint64]): uint64 =
tx.getTotalDataGas *
getDataGasprice(parentExcessDataGas.get(0'u64))
parentExcessBlobGas: Option[uint64]): uint64 =
tx.getTotalBlobGas *
getBlobGasprice(parentExcessBlobGas.get(0'u64))

proc calcDataFee*(versionedHashesLen: int,
parentExcessDataGas: Option[uint64]): uint64 =
getTotalDataGas(versionedHashesLen) *
getDataGasprice(parentExcessDataGas.get(0'u64))
parentExcessBlobGas: Option[uint64]): uint64 =
getTotalBlobGas(versionedHashesLen) *
getBlobGasprice(parentExcessBlobGas.get(0'u64))

func dataGasUsed(txs: openArray[Transaction]): uint64 =
func blobGasUsed(txs: openArray[Transaction]): uint64 =
for tx in txs:
result += tx.getTotalDataGas
result += tx.getTotalBlobGas

# https://eips.ethereum.org/EIPS/eip-4844
func validateEip4844Header*(
com: CommonRef, header, parentHeader: BlockHeader,
txs: openArray[Transaction]): Result[void, string] {.raises: [].} =

if not com.forkGTE(Cancun):
if header.dataGasUsed.isSome:
return err("unexpected EIP-4844 dataGasUsed in block header")
if header.blobGasUsed.isSome:
return err("unexpected EIP-4844 blobGasUsed in block header")

if header.excessDataGas.isSome:
return err("unexpected EIP-4844 excessDataGas in block header")
if header.excessBlobGas.isSome:
return err("unexpected EIP-4844 excessBlobGas in block header")

return ok()

if header.dataGasUsed.isNone:
return err("expect EIP-4844 dataGasUsed in block header")
if header.blobGasUsed.isNone:
return err("expect EIP-4844 blobGasUsed in block header")

if header.excessDataGas.isNone:
return err("expect EIP-4844 excessDataGas in block header")
if header.excessBlobGas.isNone:
return err("expect EIP-4844 excessBlobGas in block header")

let
headerDataGasUsed = header.dataGasUsed.get()
dataGasUsed = dataGasUsed(txs)
headerExcessDataGas = header.excessDataGas.get
excessDataGas = calcExcessDataGas(parentHeader)
headerBlobGasUsed = header.blobGasUsed.get()
blobGasUsed = blobGasUsed(txs)
headerExcessBlobGas = header.excessBlobGas.get
excessBlobGas = calcExcessBlobGas(parentHeader)

if dataGasUsed > MAX_DATA_GAS_PER_BLOCK:
return err("dataGasUsed " & $dataGasUsed & " exceeds maximum allowance " & $MAX_DATA_GAS_PER_BLOCK)
if blobGasUsed > MAX_BLOB_GAS_PER_BLOCK:
return err("blobGasUsed " & $blobGasUsed & " exceeds maximum allowance " & $MAX_BLOB_GAS_PER_BLOCK)

if headerDataGasUsed != dataGasUsed:
return err("calculated dataGas not equal header.dataGasUsed")
if headerBlobGasUsed != blobGasUsed:
return err("calculated blobGas not equal header.blobGasUsed")

if headerExcessDataGas != excessDataGas:
return err("calculated excessDataGas not equal header.excessDataGas")
if headerExcessBlobGas != excessBlobGas:
return err("calculated excessBlobGas not equal header.excessBlobGas")

return ok()

Expand Down
4 changes: 2 additions & 2 deletions nimbus/core/executor/process_transaction.nim
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ proc asyncProcessTransactionImpl(
baseFee = baseFee256.truncate(GasInt)
tx = eip1559TxNormalization(tx, baseFee, fork)
priorityFee = min(tx.maxPriorityFee, tx.maxFee - baseFee)
excessDataGas = vmState.parent.excessDataGas.get(0'u64)
excessBlobGas = vmState.parent.excessBlobGas.get(0'u64)

# Return failure unless explicitely set `ok()`
var res: Result[GasInt, string] = err("")
Expand All @@ -100,7 +100,7 @@ proc asyncProcessTransactionImpl(
# before leaving is crucial for some unit tests that us a direct/deep call
# of the `processTransaction()` function. So there is no `return err()`
# statement, here.
let txRes = roDB.validateTransaction(tx, sender, header.gasLimit, baseFee256, excessDataGas, fork)
let txRes = roDB.validateTransaction(tx, sender, header.gasLimit, baseFee256, excessBlobGas, fork)
if txRes.isOk:

# EIP-1153
Expand Down
24 changes: 12 additions & 12 deletions nimbus/core/tx_pool/tx_chain.nim
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ type
profit: UInt256 ## Net reward (w/o PoW specific block rewards)
txRoot: Hash256 ## `rootHash` after packing
stateRoot: Hash256 ## `stateRoot` after packing
dataGasUsed:
Option[uint64] ## EIP-4844 block dataGasUsed
excessDataGas:
Option[uint64] ## EIP-4844 block excessDataGas
blobGasUsed:
Option[uint64] ## EIP-4844 block blobGasUsed
excessBlobGas:
Option[uint64] ## EIP-4844 block excessBlobGas

TxChainRef* = ref object ##\
## State cache of the transaction environment for creating a new\
Expand Down Expand Up @@ -144,8 +144,8 @@ proc resetTxEnv(dh: TxChainRef; parent: BlockHeader; fee: Option[UInt256])

dh.txEnv.txRoot = EMPTY_ROOT_HASH
dh.txEnv.stateRoot = dh.txEnv.vmState.parent.stateRoot
dh.txEnv.dataGasUsed = none(uint64)
dh.txEnv.excessDataGas = none(uint64)
dh.txEnv.blobGasUsed = none(uint64)
dh.txEnv.excessBlobGas = none(uint64)

proc update(dh: TxChainRef; parent: BlockHeader)
{.gcsafe,raises: [CatchableError].} =
Expand Down Expand Up @@ -224,8 +224,8 @@ proc getHeader*(dh: TxChainRef): BlockHeader
# mixDigest: Hash256 # mining hash for given difficulty
# nonce: BlockNonce # mining free vaiable
fee: dh.txEnv.vmState.fee,
dataGasUsed: dh.txEnv.dataGasUsed,
excessDataGas: dh.txEnv.excessDataGas)
blobGasUsed: dh.txEnv.blobGasUsed,
excessBlobGas: dh.txEnv.excessBlobGas)

if dh.com.forkGTE(Shanghai):
result.withdrawalsRoot = some(calcWithdrawalsRoot(dh.withdrawals))
Expand Down Expand Up @@ -378,13 +378,13 @@ proc `txRoot=`*(dh: TxChainRef; val: Hash256) =
proc `withdrawals=`*(dh: TxChainRef, val: sink seq[Withdrawal]) =
dh.withdrawals = system.move(val)

proc `excessDataGas=`*(dh: TxChainRef; val: Option[uint64]) =
proc `excessBlobGas=`*(dh: TxChainRef; val: Option[uint64]) =
## Setter
dh.txEnv.excessDataGas = val
dh.txEnv.excessBlobGas = val

proc `dataGasUsed=`*(dh: TxChainRef; val: Option[uint64]) =
proc `blobGasUsed=`*(dh: TxChainRef; val: Option[uint64]) =
## Setter
dh.txEnv.dataGasUsed = val
dh.txEnv.blobGasUsed = val

# ------------------------------------------------------------------------------
# End
Expand Down
4 changes: 2 additions & 2 deletions nimbus/core/tx_pool/tx_tasks/tx_classify.nim
Original file line number Diff line number Diff line change
Expand Up @@ -233,9 +233,9 @@ proc classifyValidatePacked*(xp: TxPoolRef;
else:
xp.chain.limits.trgLimit
tx = item.tx.eip1559TxNormalization(xp.chain.baseFee.GasInt, fork)
excessDataGas = vmState.parent.excessDataGas.get(0'u64)
excessBlobGas = vmState.parent.excessBlobGas.get(0'u64)

roDB.validateTransaction(tx, item.sender, gasLimit, baseFee, excessDataGas, fork).isOk
roDB.validateTransaction(tx, item.sender, gasLimit, baseFee, excessBlobGas, fork).isOk

proc classifyPacked*(xp: TxPoolRef; gasBurned, moreBurned: GasInt): bool =
## Classifier for *packing* (i.e. adding up `gasUsed` values after executing
Expand Down
12 changes: 6 additions & 6 deletions nimbus/core/tx_pool/tx_tasks/tx_packer.nim
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ type
tr: HexaryTrie
cleanState: bool
balance: UInt256
dataGasUsed: uint64
blobGasUsed: uint64

const
receiptsExtensionSize = ##\
Expand Down Expand Up @@ -138,9 +138,9 @@ proc runTxCommit(pst: TxPackerStateRef; item: TxItemRef; gasBurned: GasInt)
vmState.cumulativeGasUsed += gasBurned
vmState.receipts[inx] = vmState.makeReceipt(item.tx.txType)

# EIP-4844, count dataGasUsed
# EIP-4844, count blobGasUsed
if item.tx.txType >= TxEip4844:
pst.dataGasUsed += item.tx.getTotalDataGas
pst.blobGasUsed += item.tx.getTotalBlobGas

# Update txRoot
pst.tr.put(rlp.encode(inx), rlp.encode(item.tx.removeNetworkPayload))
Expand Down Expand Up @@ -251,9 +251,9 @@ proc vmExecCommit(pst: TxPackerStateRef)

if vmState.com.forkGTE(Cancun):
# EIP-4844
let excessDataGas = calcExcessDataGas(vmState.parent)
xp.chain.excessDataGas = some(excessDataGas)
xp.chain.dataGasUsed = some(pst.dataGasUsed)
let excessBlobGas = calcExcessBlobGas(vmState.parent)
xp.chain.excessBlobGas = some(excessBlobGas)
xp.chain.blobGasUsed = some(pst.blobGasUsed)

proc balanceDelta: UInt256 =
let postBalance = vmState.readOnlyStateDB.getBalance(xp.chain.feeRecipient)
Expand Down
12 changes: 6 additions & 6 deletions nimbus/core/validate.nim
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ proc validateUncles(com: CommonRef; header: BlockHeader;

func gasCost(tx: Transaction): UInt256 =
if tx.txType >= TxEip4844:
tx.gasLimit.u256 * tx.maxFee.u256 + tx.getTotalDataGas.u256 * tx.maxFeePerDataGas.u256
tx.gasLimit.u256 * tx.maxFee.u256 + tx.getTotalBlobGas.u256 * tx.maxFeePerBlobGas.u256
elif tx.txType >= TxEip1559:
tx.gasLimit.u256 * tx.maxFee.u256
else:
Expand All @@ -246,7 +246,7 @@ proc validateTransaction*(
sender: EthAddress; ## tx.getSender or tx.ecRecover
maxLimit: GasInt; ## gasLimit from block header
baseFee: UInt256; ## baseFee from block header
excessDataGas: uint64; ## excessDataGas from parent block header
excessBlobGas: uint64; ## excessBlobGas from parent block header
fork: EVMFork): Result[void, string] =

let
Expand Down Expand Up @@ -359,10 +359,10 @@ proc validateTransaction*(
"get=$1, expect=$2" % [$bv.data[0].int, $BLOB_COMMITMENT_VERSION_KZG.int])

# ensure that the user was willing to at least pay the current data gasprice
let dataGasPrice = getDataGasPrice(excessDataGas)
if tx.maxFeePerDataGas.uint64 < dataGasPrice:
return err("invalid tx: maxFeePerDataGas smaller than dataGasPrice. " &
"maxFeePerDataGas=$1, dataGasPrice=$2" % [$tx.maxFeePerDataGas, $dataGasPrice])
let blobGasPrice = getBlobGasPrice(excessBlobGas)
if tx.maxFeePerBlobGas.uint64 < blobGasPrice:
return err("invalid tx: maxFeePerBlobGas smaller than blobGasPrice. " &
"maxFeePerBlobGas=$1, blobGasPrice=$2" % [$tx.maxFeePerBlobGas, $blobGasPrice])

except CatchableError as ex:
return err(ex.msg)
Expand Down
6 changes: 3 additions & 3 deletions nimbus/evm/async/data_sources/json_rpc_data_source.nim
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ proc makeAnRpcClient*(web3Url: string): Future[RpcClient] {.async.} =
uncles*: seq[Hash256] # list of uncle hashes.
baseFeePerGas*: Option[UInt256] # EIP-1559
withdrawalsRoot*: Option[Hash256] # EIP-4895
excessDataGas*: Option[UInt256] # EIP-4844
excessBlobGas*: Option[UInt256] # EIP-4844
]#

func fromQty(x: Option[Quantity]): Option[uint64] =
Expand Down Expand Up @@ -108,8 +108,8 @@ func blockHeaderFromBlockObject(o: BlockObject): BlockHeader =
nonce: nonce,
fee: o.baseFeePerGas,
withdrawalsRoot: o.withdrawalsRoot.map(toHash),
dataGasUsed: fromQty(o.dataGasUsed),
excessDataGas: fromQty(o.excessDataGas)
blobGasUsed: fromQty(o.blobGasUsed),
excessBlobGas: fromQty(o.excessBlobGas)
)

proc fetchBlockHeaderWithHash*(rpcClient: RpcClient, h: Hash256): Future[BlockHeader] {.async.} =
Expand Down
2 changes: 1 addition & 1 deletion nimbus/transaction/call_common.nim
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ proc prepareToRunComputation(host: TransactionHost, call: CallParams) =
# EIP-4844
if fork >= FkCancun:
let blobFee = calcDataFee(call.versionedHashes.len,
vmState.parent.excessDataGas).GasInt
vmState.parent.excessBlobGas).GasInt
db.subBalance(call.sender, blobFee.u256)

proc calculateAndPossiblyRefundGas(host: TransactionHost, call: CallParams): GasInt =
Expand Down
8 changes: 4 additions & 4 deletions nimbus/utils/debug.nim
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ proc debug*(h: BlockHeader): string =
result.add "fee : " & $h.fee.get() & "\n"
if h.withdrawalsRoot.isSome:
result.add "withdrawalsRoot: " & $h.withdrawalsRoot.get() & "\n"
if h.dataGasUsed.isSome:
result.add "dataGasUsed : " & $h.dataGasUsed.get() & "\n"
if h.excessDataGas.isSome:
result.add "excessDataGas : " & $h.excessDataGas.get() & "\n"
if h.blobGasUsed.isSome:
result.add "blobGasUsed : " & $h.blobGasUsed.get() & "\n"
if h.excessBlobGas.isSome:
result.add "excessBlobGas : " & $h.excessBlobGas.get() & "\n"
result.add "blockHash : " & $blockHash(h) & "\n"

proc dumpAccount(stateDB: AccountsCache, address: EthAddress): JsonNode =
Expand Down
4 changes: 2 additions & 2 deletions tests/replay/pp.nim
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ proc pp*(h: BlockHeader; sep = " "): string =
&"stateRoot={h.stateRoot.pp}{sep}" &
&"baseFee={h.baseFee}{sep}" &
&"withdrawalsRoot={h.withdrawalsRoot.get(EMPTY_ROOT_HASH)}{sep}" &
&"dataGasUsed={h.dataGasUsed.get(0'u64)}" &
&"excessDataGas={h.excessDataGas.get(0'u64)}"
&"blobGasUsed={h.blobGasUsed.get(0'u64)}" &
&"excessBlobGas={h.excessBlobGas.get(0'u64)}"

proc pp*(g: Genesis; sep = " "): string =
"" &
Expand Down
Loading

0 comments on commit f047394

Please sign in to comment.