Skip to content

Commit d9112d9

Browse files
committed
Change content id prefixes to 0x00 and 0x01 as per spec change
1 parent 9a02ae8 commit d9112d9

File tree

4 files changed

+22
-59
lines changed

4 files changed

+22
-59
lines changed

portal/network/history/content/content_keys.nim

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,14 @@ export ssz_serialization, common_types, results
1313

1414
type
1515
ContentType* = enum
16-
# Note: Need to add this unused value as a case object with an enum without
17-
# a 0 valueis not allowed: "low(contentType) must be 0 for discriminant".
18-
# For prefix values that are in the enum gap, the deserialization will fail
19-
# at runtime as is wanted.
20-
# In the future it might be possible that this will fail at compile time for
21-
# the SSZ Union type, but currently it is allowed in the implementation, and
22-
# the SSZ spec is not explicit about disallowing this.
23-
unused = 0x00
24-
blockBody = 0x09
25-
receipts = 0x0A
16+
blockBody = 0x00
17+
receipts = 0x01
2618

2719
BlockNumberKey* = object
2820
blockNumber*: uint64
2921

3022
ContentKey* = object
3123
case contentType*: ContentType
32-
of unused:
33-
discard
3424
of blockBody:
3525
blockBodyKey*: BlockNumberKey
3626
of receipts:
@@ -49,22 +39,15 @@ func receiptsContentKey*(blockNumber: uint64): ContentKey =
4939
template blockNumber*(contentKey: ContentKey): uint64 =
5040
## Returns the block number for the given content key
5141
case contentKey.contentType
52-
of unused:
53-
raiseAssert "ContentKey may not have unused value as content type"
54-
of blockBody:
55-
contentKey.blockBodyKey.blockNumber
56-
of receipts:
57-
contentKey.receiptsKey.blockNumber
42+
of blockBody: contentKey.blockBodyKey.blockNumber
43+
of receipts: contentKey.receiptsKey.blockNumber
5844

5945
proc readSszBytes*(data: openArray[byte], val: var ContentKey) {.raises: [SszError].} =
6046
mixin readSszValue
61-
if data.len() > 0 and data[0] == ord(unused):
62-
raise newException(MalformedSszError, "SSZ selector is unused value")
6347

6448
readSszValue(data, val)
6549

6650
func encode*(contentKey: ContentKey): ContentKeyByteList =
67-
doAssert(contentKey.contentType != unused)
6851
ContentKeyByteList.init(SSZ.encode(contentKey))
6952

7053
func decode*(contentKey: ContentKeyByteList): Opt[ContentKey] =
@@ -101,8 +84,6 @@ func toContentId*(blockNumber: uint64, contentType: ContentType): UInt256 =
10184

10285
func toContentId*(contentKey: ContentKey): ContentId =
10386
case contentKey.contentType
104-
of unused:
105-
raiseAssert "ContentKey may not have unused value as content type"
10687
of blockBody:
10788
toContentId(contentKey.blockBodyKey.blockNumber, contentKey.contentType)
10889
of receipts:
@@ -119,8 +100,6 @@ func `$`*(x: ContentKey): string =
119100
var res = "(type: " & $x.contentType & ", "
120101

121102
case x.contentType
122-
of unused:
123-
raiseAssert "ContentKey may not have unused value as content type"
124103
of blockBody:
125104
res.add($x.blockBodyKey)
126105
of receipts:

portal/network/history/history_network.nim

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,6 @@ proc validateContent(
168168
let header = ?(await n.getHeader(contentKey.blockNumber()))
169169

170170
case contentKey.contentType
171-
of unused:
172-
raiseAssert("ContentKey contentType: unused")
173171
of blockBody:
174172
let blockBody = decodeRlp(content, BlockBody).valueOr:
175173
return err("Error decoding block body: " & error)

portal/network/history/history_validation.nim

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,6 @@ func validateContent*(
7272
): Result[void, string] =
7373
## Validate the encoded content against the header.
7474
case key.contentType
75-
of unused:
76-
raiseAssert("ContentKey contentType: unused")
7775
of blockBody:
7876
let content = ?decodeRlp(contentBytes, BlockBody)
7977
validateBlockBody(content, header)

portal/tests/history_network_tests/test_history_content_keys.nim

Lines changed: 18 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,20 @@ suite "History Content Keys":
2727

2828
# Output
2929
const contentIds = [
30-
"0001000000000000000000000000000000000000000000000000000000000009",
31-
"03e8000000000000000000000000000000000000000000000000000000000009",
32-
"614e3d0000000000000000000000000000000000000000000000000000000009",
33-
"ffffffffffffffff000000000000000000000000000000000000000000000009",
34-
"fffeffffffffffff000000000000000000000000000000000000000000000009",
35-
"fffffffffffffffe000000000000000000000000000000000000000000000009",
36-
"0000000000000008000000000000000000000000000000000000000000000009",
37-
"5555aaaaaaaaaaaa000000000000000000000000000000000000000000000009",
38-
"aaaa555555555555000000000000000000000000000000000000000000000009",
39-
"a0a0050505050505000000000000000000000000000000000000000000000009",
30+
"0001000000000000000000000000000000000000000000000000000000000001",
31+
"03e8000000000000000000000000000000000000000000000000000000000001",
32+
"614e3d0000000000000000000000000000000000000000000000000000000001",
33+
"ffffffffffffffff000000000000000000000000000000000000000000000001",
34+
"fffeffffffffffff000000000000000000000000000000000000000000000001",
35+
"fffffffffffffffe000000000000000000000000000000000000000000000001",
36+
"0000000000000008000000000000000000000000000000000000000000000001",
37+
"5555aaaaaaaaaaaa000000000000000000000000000000000000000000000001",
38+
"aaaa555555555555000000000000000000000000000000000000000000000001",
39+
"a0a0050505050505000000000000000000000000000000000000000000000001",
4040
]
4141

4242
for i in 0 ..< blockNumbers.len():
43-
let contentId = toContentId(blockNumbers[i], ContentType.blockBody)
43+
let contentId = toContentId(blockNumbers[i], ContentType.receipts)
4444

4545
check contentIds[i] == contentId.dumpHex()
4646

@@ -50,12 +50,12 @@ suite "History Content Keys":
5050

5151
# Output
5252
const
53-
contentKeyHex = "0x094e61bc0000000000"
53+
contentKeyHex = "0x004e61bc0000000000"
5454
contentId =
55-
"44012581390156707874310974263613699127815223388818970640389075388176810377225"
55+
"44012581390156707874310974263613699127815223388818970640389075388176810377216"
5656
# or
5757
contentIdHexBE =
58-
"614e3d0000000000000000000000000000000000000000000000000000000009"
58+
"614e3d0000000000000000000000000000000000000000000000000000000000"
5959

6060
let contentKey = blockBodyContentKey(blockNumber)
6161

@@ -79,12 +79,12 @@ suite "History Content Keys":
7979

8080
# Output
8181
const
82-
contentKeyHex = "0x0a4e61bc0000000000"
82+
contentKeyHex = "0x014e61bc0000000000"
8383
contentId =
84-
"44012581390156707874310974263613699127815223388818970640389075388176810377226"
84+
"44012581390156707874310974263613699127815223388818970640389075388176810377217"
8585
# or
8686
contentIdHexBE =
87-
"614e3d000000000000000000000000000000000000000000000000000000000a"
87+
"614e3d0000000000000000000000000000000000000000000000000000000001"
8888

8989
let contentKey = receiptsContentKey(blockNumber)
9090

@@ -102,20 +102,8 @@ suite "History Content Keys":
102102
# In stint this does BE hex string
103103
toContentId(contentKey).toHex() == contentIdHexBE
104104

105-
test "Invalid prefix - 0 value":
106-
let encoded = ContentKeyByteList.init(@[byte 0x00])
107-
let decoded = decode(encoded)
108-
109-
check decoded.isErr()
110-
111-
test "Invalid prefix - before valid range":
112-
let encoded = ContentKeyByteList.init(@[byte 0x08])
113-
let decoded = decode(encoded)
114-
115-
check decoded.isErr()
116-
117105
test "Invalid prefix - after valid range":
118-
let encoded = ContentKeyByteList.init(@[byte 0x0B])
106+
let encoded = ContentKeyByteList.init(@[byte 0x02])
119107
let decoded = decode(encoded)
120108

121109
check decoded.isErr()

0 commit comments

Comments
 (0)