2020-04-27 13:16:11 +00:00
|
|
|
{.used.}
|
|
|
|
|
2021-04-06 11:33:24 +00:00
|
|
|
import
|
2021-12-11 18:12:55 +00:00
|
|
|
std/[os, json],
|
|
|
|
unittest2,
|
2021-05-14 14:48:21 +00:00
|
|
|
stew/byteutils,
|
2021-04-06 11:33:24 +00:00
|
|
|
../../eth/[common, rlp]
|
2019-02-05 10:10:36 +00:00
|
|
|
|
2021-05-17 03:07:05 +00:00
|
|
|
type
|
|
|
|
EthHeader = object
|
|
|
|
header: BlockHeader
|
|
|
|
|
2021-05-14 14:48:21 +00:00
|
|
|
func `==`(a, b: ChainId): bool =
|
|
|
|
a.uint64 == b.uint64
|
2019-02-05 10:10:36 +00:00
|
|
|
|
2021-05-14 14:48:21 +00:00
|
|
|
proc loadFile(x: int) =
|
|
|
|
let fileName = "tests" / "rlp" / "eip2718" / "acl_block_" & $x & ".json"
|
|
|
|
test fileName:
|
|
|
|
let n = json.parseFile(fileName)
|
|
|
|
let data = n["rlp"].getStr()
|
2021-05-17 03:07:05 +00:00
|
|
|
var bytes1 = hexToSeqByte(data)
|
|
|
|
var blk1 = rlp.decode(bytes1, EthBlock)
|
|
|
|
|
|
|
|
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)
|
2021-05-14 14:48:21 +00:00
|
|
|
|
2021-05-17 03:07:05 +00:00
|
|
|
let blk3 = EthBlock(header: header, txs: body.transactions, uncles: body.uncles)
|
|
|
|
let bytes3 = rlp.encode(blk3)
|
|
|
|
check blk1 == blk3
|
|
|
|
check bytes1 == bytes3
|
2021-05-14 14:48:21 +00:00
|
|
|
|
|
|
|
proc suite1() =
|
|
|
|
suite "rlp encoding":
|
|
|
|
test "receipt roundtrip":
|
|
|
|
let a = Receipt(
|
2021-06-26 08:18:42 +00:00
|
|
|
receiptType: LegacyReceipt,
|
|
|
|
isHash: false,
|
|
|
|
status: false,
|
|
|
|
cumulativeGasUsed: 51000
|
2021-05-14 14:48:21 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
let hash = rlpHash(a)
|
|
|
|
let b = Receipt(
|
2021-06-26 08:18:42 +00:00
|
|
|
receiptType: LegacyReceipt,
|
|
|
|
isHash: true,
|
|
|
|
hash: hash,
|
|
|
|
cumulativeGasUsed: 21000
|
2021-05-14 14:48:21 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
let abytes = rlp.encode(a)
|
|
|
|
let bbytes = rlp.encode(b)
|
|
|
|
let aa = rlp.decode(abytes, Receipt)
|
|
|
|
let bb = rlp.decode(bbytes, Receipt)
|
|
|
|
check aa == a
|
|
|
|
check bb == b
|
2019-02-05 10:10:36 +00:00
|
|
|
|
2021-06-26 08:18:42 +00:00
|
|
|
test "EIP 2930 receipt":
|
2021-05-14 14:48:21 +00:00
|
|
|
let a = Receipt(
|
2021-06-26 08:18:42 +00:00
|
|
|
receiptType: Eip2930Receipt,
|
|
|
|
status: true
|
2021-05-14 14:48:21 +00:00
|
|
|
)
|
2019-02-05 10:10:36 +00:00
|
|
|
|
2021-05-14 14:48:21 +00:00
|
|
|
let b = Receipt(
|
2021-06-26 08:18:42 +00:00
|
|
|
receiptType: Eip2930Receipt,
|
|
|
|
status: false,
|
|
|
|
cumulativeGasUsed: 21000
|
2021-05-14 14:48:21 +00:00
|
|
|
)
|
2019-02-05 10:10:36 +00:00
|
|
|
|
2021-05-14 14:48:21 +00:00
|
|
|
let abytes = rlp.encode(a)
|
|
|
|
let bbytes = rlp.encode(b)
|
|
|
|
let aa = rlp.decode(abytes, Receipt)
|
|
|
|
let bb = rlp.decode(bbytes, Receipt)
|
|
|
|
check aa == a
|
|
|
|
check bb == b
|
2019-02-05 10:10:36 +00:00
|
|
|
|
2021-05-14 14:48:21 +00:00
|
|
|
proc suite2() =
|
|
|
|
suite "eip 2718 transaction":
|
|
|
|
for i in 0..<10:
|
|
|
|
loadFile(i)
|
2019-02-05 10:10:36 +00:00
|
|
|
|
2021-06-26 08:18:42 +00:00
|
|
|
test "rlp roundtrip EIP1559":
|
|
|
|
var h: BlockHeader
|
|
|
|
let xy = rlp.encode(h)
|
|
|
|
let hh = rlp.decode(xy, BlockHeader)
|
|
|
|
check h == hh
|
|
|
|
|
2021-05-14 14:48:21 +00:00
|
|
|
suite1()
|
2021-06-26 08:18:42 +00:00
|
|
|
suite2()
|