diff --git a/eth/common/eth_types.nim b/eth/common/eth_types.nim index e9d8e69..f281327 100644 --- a/eth/common/eth_types.nim +++ b/eth/common/eth_types.nim @@ -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) diff --git a/tests/rlp/test_common.nim b/tests/rlp/test_common.nim index 4f68596..ed8b113 100644 --- a/tests/rlp/test_common.nim +++ b/tests/rlp/test_common.nim @@ -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":