Add test vectors for history content block bodies and receipts (#1525)
This commit is contained in:
parent
4c19a0e48e
commit
0b960f323c
|
@ -74,7 +74,7 @@ func decodeSsz*(input: openArray[byte], T: type): Result[T, string] =
|
|||
|
||||
## Calls to go from SSZ decoded types to RLP fully decoded types
|
||||
|
||||
func fromPortalBlockBody(
|
||||
func fromPortalBlockBody*(
|
||||
T: type BlockBody, body: BlockBodySSZ): Result[T, string] =
|
||||
## Get the full decoded BlockBody from the SSZ-decoded `PortalBlockBody`.
|
||||
try:
|
||||
|
@ -88,7 +88,7 @@ func fromPortalBlockBody(
|
|||
except RlpError as e:
|
||||
err("RLP decoding failed: " & e.msg)
|
||||
|
||||
func fromReceipts(
|
||||
func fromReceipts*(
|
||||
T: type seq[Receipt], receipts: ReceiptsSSZ): Result[T, string] =
|
||||
## Get the full decoded seq[Receipt] from the SSZ-decoded `Receipts`.
|
||||
try:
|
||||
|
|
|
@ -18,13 +18,13 @@ import
|
|||
../../test_history_util
|
||||
|
||||
type
|
||||
JsonHeaderContent* = object
|
||||
JsonPortalContent* = object
|
||||
content_key*: string
|
||||
content_value*: string
|
||||
|
||||
JsonHeaderContentTable* = Table[string, JsonHeaderContent]
|
||||
JsonPortalContentTable* = Table[string, JsonPortalContent]
|
||||
|
||||
suite "Headers with Proof":
|
||||
suite "History Content Encoding":
|
||||
test "HeaderWithProof Decoding and Verifying":
|
||||
const dataFile =
|
||||
"./vendor/portal-spec-tests/tests/mainnet/history/headers_with_proof/1000001-1000010.json"
|
||||
|
@ -35,18 +35,23 @@ suite "Headers with Proof":
|
|||
except SszError as err:
|
||||
raiseAssert "Invalid baked-in accumulator: " & err.msg
|
||||
|
||||
let res = readJsonType(dataFile, JsonHeaderContentTable)
|
||||
let res = readJsonType(dataFile, JsonPortalContentTable)
|
||||
check res.isOk()
|
||||
let headerContent = res.get()
|
||||
let content = res.get()
|
||||
|
||||
for k, v in headerContent:
|
||||
for k, v in content:
|
||||
# TODO: strange assignment failure when using try/except ValueError
|
||||
# for the hexToSeqByte() here.
|
||||
let
|
||||
contentKeyEncoded = v.content_key.hexToSeqByte()
|
||||
contentEncoded = v.content_value.hexToSeqByte()
|
||||
|
||||
# Decode content
|
||||
let
|
||||
# TODO: strange assignment failure when using try/except ValueError
|
||||
# for the hexToSeqByte() here.
|
||||
contentKey = decodeSsz(
|
||||
v.content_key.hexToSeqByte(), ContentKey)
|
||||
contentKeyEncoded, ContentKey)
|
||||
contentValue = decodeSsz(
|
||||
v.content_value.hexToSeqByte(), BlockHeaderWithProof)
|
||||
contentEncoded, BlockHeaderWithProof)
|
||||
|
||||
check:
|
||||
contentKey.isOk()
|
||||
|
@ -60,6 +65,11 @@ suite "Headers with Proof":
|
|||
|
||||
check accumulator.verifyHeader(header, blockHeaderWithProof.proof).isOk()
|
||||
|
||||
# Encode content
|
||||
check:
|
||||
SSZ.encode(blockHeaderWithProof) == contentEncoded
|
||||
encode(contentKey.get()).asSeq() == contentKeyEncoded
|
||||
|
||||
test "HeaderWithProof Building and Encoding":
|
||||
const
|
||||
headerFile = "./vendor/portal-spec-tests/tests/mainnet/history/headers/1000001-1000010.e2s"
|
||||
|
@ -75,9 +85,9 @@ suite "Headers with Proof":
|
|||
buildHeadersWithProof(blockHeaders, epochAccumulator).valueOr:
|
||||
raiseAssert "Could not build headers with proof"
|
||||
|
||||
let res = readJsonType(headersWithProofFile, JsonHeaderContentTable)
|
||||
let res = readJsonType(headersWithProofFile, JsonPortalContentTable)
|
||||
check res.isOk()
|
||||
let headerContent = res.get()
|
||||
let content = res.get()
|
||||
|
||||
# Go over all content keys and headers with generated proofs and compare
|
||||
# them with the ones from the test vectors.
|
||||
|
@ -85,10 +95,77 @@ suite "Headers with Proof":
|
|||
let
|
||||
blockNumber = blockHeaders[i].blockNumber
|
||||
contentKey =
|
||||
headerContent[blockNumber.toString()].content_key.hexToSeqByte()
|
||||
content[blockNumber.toString()].content_key.hexToSeqByte()
|
||||
contentValue =
|
||||
headerContent[blockNumber.toString()].content_value.hexToSeqByte()
|
||||
content[blockNumber.toString()].content_value.hexToSeqByte()
|
||||
|
||||
check:
|
||||
contentKey == headerContentKey
|
||||
contentValue == headerWithProof
|
||||
|
||||
test "Block Body Encoding":
|
||||
const dataFile =
|
||||
"./vendor/portal-spec-tests/tests/mainnet/history/bodies/14764013.json"
|
||||
|
||||
let res = readJsonType(dataFile, JsonPortalContentTable)
|
||||
check res.isOk()
|
||||
let content = res.get()
|
||||
|
||||
for k, v in content:
|
||||
let
|
||||
contentKeyEncoded = v.content_key.hexToSeqByte()
|
||||
contentEncoded = v.content_value.hexToSeqByte()
|
||||
|
||||
# Decode content
|
||||
let
|
||||
contentKey = decodeSsz(contentKeyEncoded, ContentKey)
|
||||
contentValue = decodeSsz(contentEncoded, BlockBodySSZ)
|
||||
|
||||
check:
|
||||
contentKey.isOk()
|
||||
contentValue.isOk()
|
||||
|
||||
let portalBlockBody = contentValue.get()
|
||||
|
||||
let res = BlockBody.fromPortalBlockBody(portalBlockBody)
|
||||
check res.isOk()
|
||||
let blockBody = res.get()
|
||||
|
||||
# Encode content
|
||||
check:
|
||||
encode(blockBody) == contentEncoded
|
||||
encode(contentKey.get()).asSeq() == contentKeyEncoded
|
||||
|
||||
|
||||
test "Receipts Encoding":
|
||||
const dataFile =
|
||||
"./vendor/portal-spec-tests/tests/mainnet/history/receipts/14764013.json"
|
||||
|
||||
let res = readJsonType(dataFile, JsonPortalContentTable)
|
||||
check res.isOk()
|
||||
let content = res.get()
|
||||
|
||||
for k, v in content:
|
||||
let
|
||||
contentKeyEncoded = v.content_key.hexToSeqByte()
|
||||
contentEncoded = v.content_value.hexToSeqByte()
|
||||
|
||||
# Decode content
|
||||
let
|
||||
contentKey = decodeSsz(contentKeyEncoded, ContentKey)
|
||||
contentValue = decodeSsz(contentEncoded, ReceiptsSSZ)
|
||||
|
||||
check:
|
||||
contentKey.isOk()
|
||||
contentValue.isOk()
|
||||
|
||||
let portalReceipts = contentValue.get()
|
||||
|
||||
let res = seq[Receipt].fromReceipts(portalReceipts)
|
||||
check res.isOk()
|
||||
let receipts = res.get()
|
||||
|
||||
# Encode content
|
||||
check:
|
||||
encode(receipts) == contentEncoded
|
||||
encode(contentKey.get()).asSeq() == contentKeyEncoded
|
||||
|
|
|
@ -1 +1 @@
|
|||
Subproject commit ee863fecdc6ec9cc81effe355e74459cd5dda28d
|
||||
Subproject commit 81fb07a6ea35560d1ab7525f2f35c5c175a7ae90
|
Loading…
Reference in New Issue