Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implementation of EIP-4844: Shard Blob Transactions #1440

Merged
merged 15 commits into from
Jun 24, 2023
Merged
Prev Previous commit
Next Next commit
EIP-4844: txPool support excessDataGas calculation
  • Loading branch information
jangko committed Jun 8, 2023
commit 2adbc4c2bf8dd11040659ccbeeece2458ab48136
18 changes: 17 additions & 1 deletion nimbus/core/tx_pool/tx_chain.nim
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +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

TxChainRef* = ref object ##\
## State cache of the transaction environment for creating a new\
Expand Down Expand Up @@ -139,6 +143,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)

proc update(dh: TxChainRef; parent: BlockHeader)
{.gcsafe,raises: [CatchableError].} =
Expand Down Expand Up @@ -216,7 +222,9 @@ proc getHeader*(dh: TxChainRef): BlockHeader
# extraData: Blob # signing data
# mixDigest: Hash256 # mining hash for given difficulty
# nonce: BlockNonce # mining free vaiable
fee: dh.txEnv.vmState.fee)
fee: dh.txEnv.vmState.fee,
dataGasUsed: dh.txEnv.dataGasUsed,
excessDataGas: dh.txEnv.excessDataGas)

if dh.com.forkGTE(Shanghai):
result.withdrawalsRoot = some(calcWithdrawalsRoot(dh.withdrawals))
Expand Down Expand Up @@ -369,6 +377,14 @@ 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]) =
## Setter
dh.txEnv.excessDataGas = val

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

# ------------------------------------------------------------------------------
# End
# ------------------------------------------------------------------------------
12 changes: 11 additions & 1 deletion nimbus/core/tx_pool/tx_tasks/tx_packer.nim
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import
std/[sets, tables],
../../../db/accounts_cache,
../../../common/common,
"../.."/[dao, executor, validate],
"../.."/[dao, executor, validate, eip4844],
../../../transaction/call_evm,
../../../transaction,
../../../vm_state,
Expand All @@ -43,6 +43,7 @@ type
tr: HexaryTrie
cleanState: bool
balance: UInt256
dataGasUsed: uint64

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

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

# Update txRoot
pst.tr.put(rlp.encode(inx), rlp.encode(item.tx))

Expand Down Expand Up @@ -241,6 +246,11 @@ proc vmExecCommit(pst: TxPackerStateRef)
xp.chain.txRoot = pst.tr.rootHash
xp.chain.stateRoot = vmState.stateDB.rootHash

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

proc balanceDelta: UInt256 =
let postBalance = vmState.readOnlyStateDB.getBalance(xp.chain.feeRecipient)
if pst.balance < postBalance:
Expand Down