EIP-2718: fixes BlockBody rlp encoding

This commit is contained in:
jangko 2021-05-17 10:07:05 +07:00
parent d0eb2de328
commit ea8530f6a0
No known key found for this signature in database
GPG Key ID: 31702AE10541E6B9
2 changed files with 22 additions and 9 deletions

View File

@ -114,7 +114,7 @@ type
nonce*: BlockNonce
BlockBody* = object
transactions*: seq[Transaction]
transactions*{.rlpCustomSerialization.}: seq[Transaction]
uncles*: seq[BlockHeader]
Log* = object
@ -376,7 +376,7 @@ proc read*(rlp: var Rlp, T: type Transaction): T =
accessListTx: rlp.read(AccessListTx)
)
proc read*(rlp: var Rlp, t: var EthBlock, _: type seq[Transaction]): seq[Transaction] {.inline.} =
proc read*(rlp: var Rlp, t: var (EthBlock | BlockBody), _: type seq[Transaction]): seq[Transaction] {.inline.} =
# EIP 2718/2930: we have to override this field
# for reasons described below in `append` proc
if not rlp.isList:
@ -390,7 +390,7 @@ proc read*(rlp: var Rlp, t: var EthBlock, _: type seq[Transaction]): seq[Transac
var rr = rlpFromBytes(bytes)
result.add rr.read(Transaction)
proc append*(rlpWriter: var RlpWriter, blk: EthBlock, txs: seq[Transaction]) {.inline.} =
proc append*(rlpWriter: var RlpWriter, blk: EthBlock | BlockBody, txs: seq[Transaction]) {.inline.} =
# EIP 2718/2930: the new Tx is rlp(txType || txPlayload) -> one blob/one list elem
# not rlp(txType, txPayload) -> two list elem, wrong!
rlpWriter.startList(txs.len)

View File

@ -5,6 +5,10 @@ import
stew/byteutils,
../../eth/[common, rlp]
type
EthHeader = object
header: BlockHeader
proc `==`(a, b: HashOrStatus): bool =
result = a.isHash == b.isHash
if not result: return
@ -37,13 +41,22 @@ proc loadFile(x: int) =
test fileName:
let n = json.parseFile(fileName)
let data = n["rlp"].getStr()
var bytes = hexToSeqByte(data)
var blk = rlp.decode(bytes, EthBlock)
var bytes1 = hexToSeqByte(data)
var blk1 = rlp.decode(bytes1, EthBlock)
let rlpbytes = rlp.encode(blk)
var blk2 = rlp.decode(rlpbytes, EthBlock)
check blk == blk2
check bytes == rlpbytes
let bytes2 = rlp.encode(blk1)
var blk2 = rlp.decode(bytes2, EthBlock)
check blk1 == blk2
check bytes1 == bytes2
var r = rlpFromBytes(bytes1)
let header = r.read(EthHeader).header
let body = r.readRecordType(BlockBody, false)
let blk3 = EthBlock(header: header, txs: body.transactions, uncles: body.uncles)
let bytes3 = rlp.encode(blk3)
check blk1 == blk3
check bytes1 == bytes3
proc suite1() =
suite "rlp encoding":