Skip to content

Commit

Permalink
fix EIP-4844 bugs in genesis, evmstate and blobFee
Browse files Browse the repository at this point in the history
  • Loading branch information
jangko committed Jul 20, 2023
1 parent ab79cfc commit 3aff8d0
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 7 deletions.
4 changes: 4 additions & 0 deletions nimbus/common/chain_config.nim
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ type
gasUser* : GasInt
parentHash* : Hash256
baseFeePerGas*: Option[UInt256]
dataGasUsed* : Option[uint64] # EIP-4844
excessDataGas*: Option[uint64] # EIP-4844

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

const
CustomNet* = 0.NetworkId
Expand Down
4 changes: 4 additions & 0 deletions nimbus/common/genesis.nim
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ proc toGenesisHeader*(
if fork >= Shanghai:
result.withdrawalsRoot = some(EMPTY_ROOT_HASH)

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

proc toGenesisHeader*(
genesis: Genesis;
fork: HardFork;
Expand Down
17 changes: 11 additions & 6 deletions nimbus/transaction/call_common.nim
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,6 @@ func intrinsicGas*(call: CallParams, vmState: BaseVMState): GasInt {.inline.} =
gas += ACCESS_LIST_ADDRESS_COST
gas += account.storageKeys.len * ACCESS_LIST_STORAGE_KEY_COST

# EIP-4844
if fork >= FkCancun:
gas += calcDataFee(call.versionedHashes.len,
vmState.parent.excessDataGas).GasInt

return gas

proc initialAccessListEIP2929(call: CallParams) =
Expand Down Expand Up @@ -237,9 +232,19 @@ proc prepareToRunComputation(host: TransactionHost, call: CallParams) =

# Charge for gas.
if not call.noGasCharge:
host.vmState.mutateStateDB:
let
vmState = host.vmState
fork = vmState.fork

vmState.mutateStateDB:
db.subBalance(call.sender, call.gasLimit.u256 * call.gasPrice.u256)

# EIP-4844
if fork >= FkCancun:
let blobFee = calcDataFee(call.versionedHashes.len,
vmState.parent.excessDataGas).GasInt
db.subBalance(call.sender, blobFee.u256)

proc calculateAndPossiblyRefundGas(host: TransactionHost, call: CallParams): GasInt =
let c = host.computation

Expand Down
10 changes: 9 additions & 1 deletion tools/evmstate/helpers.nim
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ proc fromJson(T: type AccessList, n: JsonNode): AccessList =
ap.storageKeys.add hexToByteArray(sk.getStr, 32)
result.add ap

proc fromJson(T: type VersionedHashes, list: JsonNode): VersionedHashes =
for x in list:
result.add Hash256.fromJson(x)

template required(T: type, nField: string): auto =
fromJson(T, n[nField])

Expand All @@ -92,6 +96,8 @@ template optional(T: type, nField: string): auto =
none(T)

proc txType(n: JsonNode): TxType =
if "blobVersionedHashes" in n:
return TxEip4844
if "gasPrice" notin n:
return TxEip1559
if "accessLists" in n:
Expand Down Expand Up @@ -121,7 +127,9 @@ proc parseTx*(n: JsonNode, dataIndex, gasIndex, valueIndex: int): Transaction =
gasPrice: omitZero(GasInt, "gasPrice"),
maxFee : omitZero(GasInt, "maxFeePerGas"),
accessList: omitZero(AccessList, "accessLists", dataIndex),
maxPriorityFee: omitZero(GasInt, "maxPriorityFeePerGas")
maxPriorityFee: omitZero(GasInt, "maxPriorityFeePerGas"),
maxFeePerDataGas: omitZero(GasInt, "maxFeePerDataGas"),
versionedHashes: omitZero(VersionedHashes, "blobVersionedHashes")
)

let rawTo = n["to"].getStr
Expand Down

0 comments on commit 3aff8d0

Please sign in to comment.