Skip to content

Commit a669a35

Browse files
committed
Adjust content id derivation to differentiate body and receipts
1 parent bdce27e commit a669a35

File tree

2 files changed

+20
-19
lines changed

2 files changed

+20
-19
lines changed

portal/network/history/content/content_keys.nim

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ const
8787
OFFSET_BITS = 256 - CYCLE_BITS # 240
8888
REVERSED_OFFSET_BITS = 64 - CYCLE_BITS # 48
8989

90-
func toContentId*(blockNumber: uint64): UInt256 =
90+
func toContentId*(blockNumber: uint64, contentType: ContentType): UInt256 =
9191
## Returns the content id for a given block number
9292
let
9393
cycleBits = blockNumber mod (1'u64 shl CYCLE_BITS)
@@ -96,16 +96,17 @@ func toContentId*(blockNumber: uint64): UInt256 =
9696
reversedOffsetBits = reverseBits(offsetBits, REVERSED_OFFSET_BITS)
9797

9898
(cycleBits.stuint(256) shl OFFSET_BITS) or
99-
(reversedOffsetBits.stuint(256) shl (OFFSET_BITS - REVERSED_OFFSET_BITS))
99+
(reversedOffsetBits.stuint(256) shl (OFFSET_BITS - REVERSED_OFFSET_BITS)) or
100+
ord(contentType).stuint(256)
100101

101102
func toContentId*(contentKey: ContentKey): ContentId =
102103
case contentKey.contentType
103104
of unused:
104105
raiseAssert "ContentKey may not have unused value as content type"
105106
of blockBody:
106-
toContentId(contentKey.blockBodyKey.blockNumber)
107+
toContentId(contentKey.blockBodyKey.blockNumber, contentKey.contentType)
107108
of receipts:
108-
toContentId(contentKey.receiptsKey.blockNumber)
109+
toContentId(contentKey.receiptsKey.blockNumber, contentKey.contentType)
109110

110111
func toContentId*(bytes: ContentKeyByteList): Opt[ContentId] =
111112
let contentKey = ?bytes.decode()

portal/tests/history_network_tests/test_history_content_keys.nim

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

2828
# Output
2929
const contentIds = [
30-
"0001000000000000000000000000000000000000000000000000000000000000",
31-
"03e8000000000000000000000000000000000000000000000000000000000000",
32-
"614e3d0000000000000000000000000000000000000000000000000000000000",
33-
"ffffffffffffffff000000000000000000000000000000000000000000000000",
34-
"fffeffffffffffff000000000000000000000000000000000000000000000000",
35-
"fffffffffffffffe000000000000000000000000000000000000000000000000",
36-
"0000000000000008000000000000000000000000000000000000000000000000",
37-
"5555aaaaaaaaaaaa000000000000000000000000000000000000000000000000",
38-
"aaaa555555555555000000000000000000000000000000000000000000000000",
39-
"a0a0050505050505000000000000000000000000000000000000000000000000",
30+
"0001000000000000000000000000000000000000000000000000000000000009",
31+
"03e8000000000000000000000000000000000000000000000000000000000009",
32+
"614e3d0000000000000000000000000000000000000000000000000000000009",
33+
"ffffffffffffffff000000000000000000000000000000000000000000000009",
34+
"fffeffffffffffff000000000000000000000000000000000000000000000009",
35+
"fffffffffffffffe000000000000000000000000000000000000000000000009",
36+
"0000000000000008000000000000000000000000000000000000000000000009",
37+
"5555aaaaaaaaaaaa000000000000000000000000000000000000000000000009",
38+
"aaaa555555555555000000000000000000000000000000000000000000000009",
39+
"a0a0050505050505000000000000000000000000000000000000000000000009",
4040
]
4141

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

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

@@ -52,10 +52,10 @@ suite "History Content Keys":
5252
const
5353
contentKeyHex = "0x094e61bc0000000000"
5454
contentId =
55-
"44012581390156707874310974263613699127815223388818970640389075388176810377216"
55+
"44012581390156707874310974263613699127815223388818970640389075388176810377225"
5656
# or
5757
contentIdHexBE =
58-
"614e3d0000000000000000000000000000000000000000000000000000000000"
58+
"614e3d0000000000000000000000000000000000000000000000000000000009"
5959

6060
let contentKey = blockBodyContentKey(blockNumber)
6161

@@ -81,10 +81,10 @@ suite "History Content Keys":
8181
const
8282
contentKeyHex = "0x0a4e61bc0000000000"
8383
contentId =
84-
"44012581390156707874310974263613699127815223388818970640389075388176810377216"
84+
"44012581390156707874310974263613699127815223388818970640389075388176810377226"
8585
# or
8686
contentIdHexBE =
87-
"614e3d0000000000000000000000000000000000000000000000000000000000"
87+
"614e3d000000000000000000000000000000000000000000000000000000000a"
8888

8989
let contentKey = receiptsContentKey(blockNumber)
9090

0 commit comments

Comments
 (0)