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
fix t8n transaction decoding
  • Loading branch information
jangko committed Jun 9, 2023
commit 14fa8555c4643db76ad44f224e8cc6f74b5b8735
4 changes: 2 additions & 2 deletions tools/t8n/helpers.nim
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ proc parseTx(n: JsonNode, chainId: ChainID): Transaction =
proc parseTxLegacy(item: var Rlp): Result[Transaction, string] =
try:
var tx: Transaction
item.readTxLegacy(tx)
item.decodeTxLegacy(tx)
return ok(tx)
except RlpError as x:
return err(x.msg)
Expand All @@ -260,7 +260,7 @@ proc parseTxTyped(item: var Rlp): Result[Transaction, string] =
try:
var tx: Transaction
var rr = rlpFromBytes(item.read(Blob))
rr.readTxTyped(tx)
rr.decodeTxTyped(tx)
return ok(tx)
except RlpError as x:
return err(x.msg)
Expand Down
99 changes: 6 additions & 93 deletions tools/t8n/txpriv.nim
Original file line number Diff line number Diff line change
Expand Up @@ -8,99 +8,12 @@
# at your option. This file may not be copied, modified, or distributed except
# according to those terms.

import
eth/common
include eth/common/eth_types_rlp

from stew/objects
import checkedEnumAssign
# reexport private procs

# these procs are duplicates of nim-eth/eth_types_rlp.nim
# both `readTxLegacy` and `readTxTyped` are exported here
template decodeTxLegacy*(rlp: var Rlp, tx: var Transaction) =
readTxLegacy(rlp, tx)

template read[T](rlp: var Rlp, val: var T)=
val = rlp.read(type val)

proc read[T](rlp: var Rlp, val: var Option[T])=
if rlp.blobLen != 0:
val = some(rlp.read(T))
else:
rlp.skipElem

proc readTxLegacy*(rlp: var Rlp, tx: var Transaction)=
tx.txType = TxLegacy
rlp.tryEnterList()
rlp.read(tx.nonce)
rlp.read(tx.gasPrice)
rlp.read(tx.gasLimit)
rlp.read(tx.to)
rlp.read(tx.value)
rlp.read(tx.payload)
rlp.read(tx.V)
rlp.read(tx.R)
rlp.read(tx.S)

proc readTxEip2930(rlp: var Rlp, tx: var Transaction)=
tx.txType = TxEip2930
rlp.tryEnterList()
tx.chainId = rlp.read(uint64).ChainId
rlp.read(tx.nonce)
rlp.read(tx.gasPrice)
rlp.read(tx.gasLimit)
rlp.read(tx.to)
rlp.read(tx.value)
rlp.read(tx.payload)
rlp.read(tx.accessList)
rlp.read(tx.V)
rlp.read(tx.R)
rlp.read(tx.S)

proc readTxEip1559(rlp: var Rlp, tx: var Transaction)=
tx.txType = TxEip1559
rlp.tryEnterList()
tx.chainId = rlp.read(uint64).ChainId
rlp.read(tx.nonce)
rlp.read(tx.maxPriorityFee)
rlp.read(tx.maxFee)
rlp.read(tx.gasLimit)
rlp.read(tx.to)
rlp.read(tx.value)
rlp.read(tx.payload)
rlp.read(tx.accessList)
rlp.read(tx.V)
rlp.read(tx.R)
rlp.read(tx.S)

proc readTxTyped*(rlp: var Rlp, tx: var Transaction) {.inline.} =
# EIP-2718: We MUST decode the first byte as a byte, not `rlp.read(int)`.
# If decoded with `rlp.read(int)`, bad transaction data (from the network)
# or even just incorrectly framed data for other reasons fails with
# any of these misleading error messages:
# - "Message too large to fit in memory"
# - "Number encoded with a leading zero"
# - "Read past the end of the RLP stream"
# - "Small number encoded in a non-canonical way"
# - "Attempt to read an Int value past the RLP end"
# - "The RLP contains a larger than expected Int value"
if not rlp.isSingleByte:
if not rlp.hasData:
raise newException(MalformedRlpError,
"Transaction expected but source RLP is empty")
raise newException(MalformedRlpError,
"TypedTransaction type byte is out of range, must be 0x00 to 0x7f")
let txType = rlp.getByteValue
rlp.position += 1

var txVal: TxType
if checkedEnumAssign(txVal, txType):
case txVal:
of TxEip2930:
rlp.readTxEip2930(tx)
return
of TxEip1559:
rlp.readTxEip1559(tx)
return
else:
discard

raise newException(UnsupportedRlpError,
"TypedTransaction type must be 1 or 2 in this version, got " & $txType)
template decodeTxTyped*(rlp: var Rlp, tx: var Transaction) =
readTxTyped(rlp, tx)