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: add EIP-4844 support to t8n tool
  • Loading branch information
jangko committed Jun 8, 2023
commit 47ffa0498ddf2d321f05e12833b945e7a011f6d5
28 changes: 26 additions & 2 deletions tools/t8n/helpers.nim
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,16 @@ template fromJson(T: type GasInt, n: JsonNode, field: string): GasInt =
template fromJson(T: type ChainId, n: JsonNode, field: string): ChainId =
parseHexOrInt[uint64](n[field].getStr()).ChainId

proc fromJson(T: type Hash256, n: JsonNode, field: string): Hash256 =
var num = n[field].getStr()
proc fromJson(T: type Hash256, n: JsonNode): Hash256 =
var num = n.getStr()
num.removePrefix("0x")
if num.len < 64:
num = repeat('0', 64 - num.len) & num
Hash256(data: hexToByteArray(num, 32))

proc fromJson(T: type Hash256, n: JsonNode, field: string): Hash256 =
fromJson(T, n[field])

template fromJson(T: type EthTime, n: JsonNode, field: string): EthTime =
fromUnix(parseHexOrInt[int64](n[field].getStr()))

Expand Down Expand Up @@ -99,6 +102,11 @@ proc fromJson(T: type Withdrawal, n: JsonNode): Withdrawal =
amount: fromJson(uint64, n, "amount")
)

proc fromJson(T: type VersionedHashes, n: JsonNode, field: string): VersionedHashes =
let list = n[field]
for x in list:
result.add Hash256.fromJson(x)

template `gas=`(tx: var Transaction, x: GasInt) =
tx.gasLimit = x

Expand All @@ -120,6 +128,9 @@ template `maxPriorityFeePerGas=`(tx: var Transaction, x: GasInt) =
template `maxFeePerGas=`(tx: var Transaction, x: GasInt) =
tx.maxFee = x

template `blobVersionedHashes=`(tx: var Transaction, x: VersionedHashes) =
tx.versionedHashes = x

template required(o: untyped, T: type, oField: untyped) =
const fName = astToStr(oField)
if not n.hasKey(fName):
Expand Down Expand Up @@ -168,6 +179,8 @@ proc parseEnv*(ctx: var TransContext, n: JsonNode) =
optional(ctx.env, UInt256, parentBaseFee)
optional(ctx.env, GasInt, parentGasUsed)
optional(ctx.env, GasInt, parentGasLimit)
optional(ctx.env, uint64, parentDataGasUsed)
optional(ctx.env, uint64, parentExcessDataGas)

if n.hasKey("blockHashes"):
let w = n["blockHashes"]
Expand Down Expand Up @@ -216,6 +229,13 @@ proc parseTx(n: JsonNode, chainId: ChainID): Transaction =
required(tx, GasInt, maxPriorityFeePerGas)
required(tx, GasInt, maxFeePerGas)
omitZero(tx, AccessList, accessList)
of TxEip4844:
required(tx, ChainId, chainId)
required(tx, GasInt, maxPriorityFeePerGas)
required(tx, GasInt, maxFeePerGas)
omitZero(tx, AccessList, accessList)
required(tx, GasInt, maxFeePerDataGas)
required(tx, VersionedHashes, blobVersionedHashes)

var eip155 = true
if n.hasKey("protected"):
Expand Down Expand Up @@ -420,3 +440,7 @@ proc `@@`*(x: ExecutionResult): JsonNode =
result["currentBaseFee"] = @@(x.currentBaseFee)
if x.withdrawalsRoot.isSome:
result["withdrawalsRoot"] = @@(x.withdrawalsRoot)
if x.dataGasUsed.isSome:
result["dataGasUsed"] = @@(x.dataGasUsed)
if x.excessDataGas.isSome:
result["excessDataGas"] = @@(x.excessDataGas)
24 changes: 22 additions & 2 deletions tools/t8n/transition.nim
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ import
../../nimbus/utils/utils,
../../nimbus/core/pow/difficulty,
../../nimbus/core/dao,
../../nimbus/core/executor/[process_transaction, executor_helpers]
../../nimbus/core/executor/[process_transaction, executor_helpers],
../../nimbus/core/eip4844

import stew/byteutils
const
Expand Down Expand Up @@ -203,6 +204,7 @@ proc exec(ctx: var TransContext,
vmState.receipts = newSeqOfCap[Receipt](txList.len)
vmState.cumulativeGasUsed = 0

var dataGasUsed = 0'u64
for txIndex, txRes in txList:
if txRes.isErr:
rejected.add RejectedTx(
Expand Down Expand Up @@ -239,6 +241,7 @@ proc exec(ctx: var TransContext,
rec, tx, sender, txIndex, gasUsed
)
includedTx.add tx
dataGasUsed += tx.getTotalDataGas

# Add mining reward? (-1 means rewards are disabled)
if stateReward.isSome and stateReward.get >= 0:
Expand Down Expand Up @@ -286,6 +289,10 @@ proc exec(ctx: var TransContext,
withdrawalsRoot: header.withdrawalsRoot
)

if fork >= FkCancun:
result.result.dataGasUsed = some dataGasUsed
result.result.excessDataGas = some calcExcessDataGas(vmState.parent)

template wrapException(body: untyped) =
when wrapExceptionEnabled:
try:
Expand Down Expand Up @@ -403,7 +410,9 @@ proc transitionAction*(ctx: var TransContext, conf: T8NConf) =
timestamp: ctx.env.parentTimestamp,
difficulty: ctx.env.parentDifficulty.get(0.u256),
ommersHash: uncleHash,
blockNumber: ctx.env.currentNumber - 1.toBlockNumber
blockNumber: ctx.env.currentNumber - 1.toBlockNumber,
dataGasUsed: ctx.env.parentDataGasUsed,
excessDataGas: ctx.env.parentExcessDataGas
)

# Sanity check, to not `panic` in state_transition
Expand All @@ -419,6 +428,17 @@ proc transitionAction*(ctx: var TransContext, conf: T8NConf) =
if com.isShanghaiOrLater(ctx.env.currentTimestamp) and ctx.env.withdrawals.isNone:
raise newError(ErrorConfig, "Shanghai config but missing 'withdrawals' in env section")

if com.isCancunOrLater(ctx.env.currentTimestamp):
if ctx.env.parentDataGasUsed.isNone:
raise newError(ErrorConfig, "Cancun config but missing 'parentDataGasUsed' env section")

if ctx.env.parentExcessDataGas.isNone:
raise newError(ErrorConfig, "Cancun config but missing 'parentExcessDataGas' env section")

let res = loadKzgTrustedSetup()
if res.isErr:
raise newError(ErrorConfig, res.error)

if com.forkGTE(MergeFork):
if ctx.env.currentRandom.isNone:
raise newError(ErrorConfig, "post-merge requires currentRandom to be defined in env")
Expand Down
4 changes: 4 additions & 0 deletions tools/t8n/types.nim
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ type
parentGasUsed*: Option[GasInt]
parentGasLimit*: Option[GasInt]
withdrawals*: Option[seq[Withdrawal]]
parentDataGasUsed*: Option[uint64]
parentExcessDataGas*: Option[uint64]

TxsType* = enum
TxsNone
Expand Down Expand Up @@ -92,6 +94,8 @@ type
gasUsed*: GasInt
currentBaseFee*: Option[UInt256]
withdrawalsRoot*: Option[Hash256]
dataGasUsed*: Option[uint64]
excessDataGas*: Option[uint64]

const
ErrorEVM* = 2.T8NExitCode
Expand Down