2024-02-05 22:22:15 +00:00
|
|
|
# Copyright (c) 2019-2024 Status Research & Development GmbH
|
|
|
|
# Licensed and distributed under either of
|
|
|
|
# * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT).
|
|
|
|
# * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0).
|
|
|
|
# at your option. This file may not be copied, modified, or distributed except according to those terms.
|
|
|
|
|
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
|
|
|
proc loadFile(x: int) =
|
2024-02-09 16:30:24 +00:00
|
|
|
let fileName = "tests" / "common" / "eip2718" / "acl_block_" & $x & ".json"
|
2021-05-14 14:48:21 +00:00
|
|
|
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
|
|
|
|
2024-06-08 17:25:25 +00:00
|
|
|
let blk3 = EthBlock(header: header, transactions: body.transactions, uncles: body.uncles)
|
2021-05-17 03:07:05 +00:00
|
|
|
let bytes3 = rlp.encode(blk3)
|
|
|
|
check blk1 == blk3
|
|
|
|
check bytes1 == bytes3
|
2021-05-14 14:48:21 +00:00
|
|
|
|
|
|
|
proc suite1() =
|
2024-02-05 22:22:15 +00:00
|
|
|
suite "RLP encoding":
|
|
|
|
test "Receipt roundtrip":
|
2021-05-14 14:48:21 +00:00
|
|
|
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
|
|
|
|
2024-02-05 22:22:15 +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
|
|
|
|
2024-02-05 22:22:15 +00:00
|
|
|
test "EIP-4895 roundtrip":
|
2022-11-25 10:07:51 +00:00
|
|
|
let a = Withdrawal(
|
|
|
|
index: 1,
|
|
|
|
validatorIndex: 2,
|
|
|
|
address: EthAddress [
|
|
|
|
0.byte, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
|
|
|
|
11, 12, 13, 14, 15, 16, 17, 18, 19],
|
2023-01-14 14:39:23 +00:00
|
|
|
amount: 4)
|
2022-11-25 10:07:51 +00:00
|
|
|
|
|
|
|
let abytes = rlp.encode(a)
|
|
|
|
let aa = rlp.decode(abytes, Withdrawal)
|
|
|
|
check aa == a
|
|
|
|
|
2021-05-14 14:48:21 +00:00
|
|
|
proc suite2() =
|
2024-02-05 22:22:15 +00:00
|
|
|
suite "EIP-2718 transaction / receipt":
|
2021-05-14 14:48:21 +00:00
|
|
|
for i in 0..<10:
|
|
|
|
loadFile(i)
|
2019-02-05 10:10:36 +00:00
|
|
|
|
2024-02-05 22:22:15 +00:00
|
|
|
test "BlockHeader: rlp roundtrip EIP-1559 / EIP-4895 / EIP-4844":
|
2022-12-13 19:53:05 +00:00
|
|
|
proc doTest(h: BlockHeader) =
|
|
|
|
let xy = rlp.encode(h)
|
|
|
|
let hh = rlp.decode(xy, BlockHeader)
|
|
|
|
check h == hh
|
|
|
|
|
2021-06-26 08:18:42 +00:00
|
|
|
var h: BlockHeader
|
2022-12-13 19:53:05 +00:00
|
|
|
doTest h
|
|
|
|
|
|
|
|
# EIP-1559
|
2024-06-11 17:13:11 +00:00
|
|
|
h.baseFeePerGas = Opt.some 1234.u256
|
2022-12-13 19:53:05 +00:00
|
|
|
doTest h
|
|
|
|
|
|
|
|
# EIP-4895
|
2024-06-11 17:13:11 +00:00
|
|
|
h.withdrawalsRoot = Opt.some Hash256.fromHex(
|
2022-12-13 19:53:05 +00:00
|
|
|
"0x7a64245f7f95164f6176d90bd4903dbdd3e5433d555dd1385e81787f9672c588")
|
|
|
|
doTest h
|
|
|
|
|
|
|
|
# EIP-4844
|
2024-06-11 17:13:11 +00:00
|
|
|
h.blobGasUsed = Opt.some 1234'u64
|
|
|
|
h.excessBlobGas = Opt.some 1234'u64
|
2022-12-13 19:53:05 +00:00
|
|
|
doTest h
|
2021-06-26 08:18:42 +00:00
|
|
|
|
2024-02-05 22:22:15 +00:00
|
|
|
test "Receipts EIP-2718 + EIP-2976 encoding":
|
|
|
|
const
|
|
|
|
# Test payload from
|
|
|
|
# https://github.com/ethereum/go-ethereum/blob/253447a4f5e5f7f65c0605d490360bb58fb5f8e0/core/types/receipt_test.go#L370
|
|
|
|
payload = "f9043eb9010c01f90108018262d4b9010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000c0b9010c01f901080182cd14b9010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000c0b9010d01f901090183013754b9010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000c0b9010d01f90109018301a194b9010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000c0"
|
|
|
|
receiptsBytes = hexToSeqByte(payload)
|
|
|
|
let receipts = rlp.decode(receiptsBytes, seq[Receipt])
|
|
|
|
|
|
|
|
check receipts.len() == 4
|
|
|
|
for receipt in receipts:
|
|
|
|
check receipt.receiptType == TxEip2930
|
|
|
|
|
|
|
|
let encoded = rlp.encode(receipts)
|
|
|
|
|
|
|
|
check receiptsBytes == encoded
|
|
|
|
|
|
|
|
test "Receipts EIP-2718 encoding - invalid - empty":
|
|
|
|
let receiptBytes: seq[byte] = @[]
|
|
|
|
expect MalformedRlpError:
|
|
|
|
let _ = rlp.decode(receiptBytes, Receipt)
|
|
|
|
|
|
|
|
test "Receipts EIP-2718 encoding - invalid - unsupported tx type":
|
2024-09-13 06:37:43 +00:00
|
|
|
let receiptBytes: seq[byte] = @[0x05]
|
2024-02-05 22:22:15 +00:00
|
|
|
expect UnsupportedRlpError:
|
|
|
|
let _ = rlp.decode(receiptBytes, Receipt)
|
|
|
|
|
|
|
|
test "Receipts EIP-2718 encoding - invalid - legacy tx type":
|
|
|
|
let receiptBytes: seq[byte] = @[0x00]
|
|
|
|
expect MalformedRlpError:
|
|
|
|
let _ = rlp.decode(receiptBytes, Receipt)
|
|
|
|
|
|
|
|
test "Receipts EIP-2718 encoding - invalid - out of bounds tx type":
|
|
|
|
let receiptBytes: seq[byte] = @[0x81, 0x80]
|
|
|
|
expect MalformedRlpError:
|
|
|
|
let _ = rlp.decode(receiptBytes, Receipt)
|
|
|
|
|
|
|
|
test "Receipts EIP-2718 encoding - invalid - empty receipt payload":
|
|
|
|
let receiptBytes: seq[byte] = @[0x02]
|
|
|
|
expect RlpTypeMismatch:
|
|
|
|
let _ = rlp.decode(receiptBytes, Receipt)
|
|
|
|
|
|
|
|
test "Receipt legacy":
|
|
|
|
const
|
|
|
|
# Test payload from
|
|
|
|
# https://github.com/ethereum/go-ethereum/blob/253447a4f5e5f7f65c0605d490360bb58fb5f8e0/core/types/receipt_test.go#L417
|
|
|
|
payload = "f901c58001b9010000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000500000000000000000000000000000000000014000000000000000000000000000000000000000000000000000000000000000000000000000010000080000000000000000000004000000000000000000000000000040000000000000000000000000000800000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000f8bef85d940000000000000000000000000000000000000011f842a0000000000000000000000000000000000000000000000000000000000000deada0000000000000000000000000000000000000000000000000000000000000beef830100fff85d940000000000000000000000000000000000000111f842a0000000000000000000000000000000000000000000000000000000000000deada0000000000000000000000000000000000000000000000000000000000000beef830100ff"
|
|
|
|
receiptsBytes = hexToSeqByte(payload)
|
|
|
|
|
|
|
|
let receipt = rlp.decode(receiptsBytes, Receipt)
|
|
|
|
check receipt.receiptType == LegacyReceipt
|
|
|
|
let encoded = rlp.encode(receipt)
|
|
|
|
check receiptsBytes == encoded
|
|
|
|
|
|
|
|
test "Receipt EIP-2930":
|
|
|
|
const
|
|
|
|
# Test payload from
|
|
|
|
# https://github.com/ethereum/go-ethereum/blob/253447a4f5e5f7f65c0605d490360bb58fb5f8e0/core/types/receipt_test.go#L435
|
|
|
|
payload = "01f901c58001b9010000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000500000000000000000000000000000000000014000000000000000000000000000000000000000000000000000000000000000000000000000010000080000000000000000000004000000000000000000000000000040000000000000000000000000000800000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000f8bef85d940000000000000000000000000000000000000011f842a0000000000000000000000000000000000000000000000000000000000000deada0000000000000000000000000000000000000000000000000000000000000beef830100fff85d940000000000000000000000000000000000000111f842a0000000000000000000000000000000000000000000000000000000000000deada0000000000000000000000000000000000000000000000000000000000000beef830100ff"
|
|
|
|
receiptsBytes = hexToSeqByte(payload)
|
|
|
|
|
|
|
|
let receipt = rlp.decode(receiptsBytes, Receipt)
|
|
|
|
check receipt.receiptType == Eip2930Receipt
|
|
|
|
let encoded = rlp.encode(receipt)
|
|
|
|
check receiptsBytes == encoded
|
|
|
|
|
|
|
|
test "Receipt EIP-1559":
|
|
|
|
const
|
|
|
|
# Test payload from
|
|
|
|
# https://github.com/ethereum/go-ethereum/blob/253447a4f5e5f7f65c0605d490360bb58fb5f8e0/core/types/receipt_test.go#L453
|
|
|
|
payload = "02f901c58001b9010000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000500000000000000000000000000000000000014000000000000000000000000000000000000000000000000000000000000000000000000000010000080000000000000000000004000000000000000000000000000040000000000000000000000000000800000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000f8bef85d940000000000000000000000000000000000000011f842a0000000000000000000000000000000000000000000000000000000000000deada0000000000000000000000000000000000000000000000000000000000000beef830100fff85d940000000000000000000000000000000000000111f842a0000000000000000000000000000000000000000000000000000000000000deada0000000000000000000000000000000000000000000000000000000000000beef830100ff"
|
|
|
|
receiptsBytes = hexToSeqByte(payload)
|
|
|
|
|
|
|
|
let receipt = rlp.decode(receiptsBytes, Receipt)
|
|
|
|
check receipt.receiptType == Eip1559Receipt
|
|
|
|
let encoded = rlp.encode(receipt)
|
|
|
|
check receiptsBytes == encoded
|
|
|
|
|
2021-05-14 14:48:21 +00:00
|
|
|
suite1()
|
2021-06-26 08:18:42 +00:00
|
|
|
suite2()
|