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

View File

@ -5,6 +5,10 @@ import
stew/byteutils, stew/byteutils,
../../eth/[common, rlp] ../../eth/[common, rlp]
type
EthHeader = object
header: BlockHeader
proc `==`(a, b: HashOrStatus): bool = proc `==`(a, b: HashOrStatus): bool =
result = a.isHash == b.isHash result = a.isHash == b.isHash
if not result: return if not result: return
@ -37,13 +41,22 @@ proc loadFile(x: int) =
test fileName: test fileName:
let n = json.parseFile(fileName) let n = json.parseFile(fileName)
let data = n["rlp"].getStr() let data = n["rlp"].getStr()
var bytes = hexToSeqByte(data) var bytes1 = hexToSeqByte(data)
var blk = rlp.decode(bytes, EthBlock) var blk1 = rlp.decode(bytes1, EthBlock)
let rlpbytes = rlp.encode(blk) let bytes2 = rlp.encode(blk1)
var blk2 = rlp.decode(rlpbytes, EthBlock) var blk2 = rlp.decode(bytes2, EthBlock)
check blk == blk2 check blk1 == blk2
check bytes == rlpbytes 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() = proc suite1() =
suite "rlp encoding": suite "rlp encoding":