Skip to content

Commit 22fc185

Browse files
update: utilities to adhere to RFC terminology
1 parent cfe03b6 commit 22fc185

File tree

1 file changed

+13
-13
lines changed

1 file changed

+13
-13
lines changed

packages/enr/src/relay_shard_codec.ts

+13-13
Original file line numberDiff line numberDiff line change
@@ -8,51 +8,51 @@ export const decodeRelayShard = (bytes: Uint8Array): ShardInfo => {
88
if (bytes.length < 3) throw new Error("Insufficient data");
99

1010
const view = new DataView(bytes.buffer);
11-
const cluster = view.getUint16(0);
11+
const clusterId = view.getUint16(0);
1212

13-
const indexList = [];
13+
const shards = [];
1414

1515
if (bytes.length === 130) {
1616
// rsv format (Bit Vector)
1717
for (let i = 0; i < 1024; i++) {
1818
const byteIndex = Math.floor(i / 8) + 2; // Adjusted for the 2-byte cluster field
1919
const bitIndex = 7 - (i % 8);
2020
if (view.getUint8(byteIndex) & (1 << bitIndex)) {
21-
indexList.push(i);
21+
shards.push(i);
2222
}
2323
}
2424
} else {
2525
// rs format (Index List)
2626
const numIndices = view.getUint8(2);
2727
for (let i = 0, offset = 3; i < numIndices; i++, offset += 2) {
2828
if (offset + 1 >= bytes.length) throw new Error("Unexpected end of data");
29-
indexList.push(view.getUint16(offset));
29+
shards.push(view.getUint16(offset));
3030
}
3131
}
3232

33-
return { cluster, indexList };
33+
return { clusterId, shards };
3434
};
3535

3636
export const encodeRelayShard = (shardInfo: ShardInfo): Uint8Array => {
37-
const { cluster, indexList } = shardInfo;
38-
const totalLength = indexList.length >= 64 ? 130 : 3 + 2 * indexList.length;
37+
const { clusterId, shards } = shardInfo;
38+
const totalLength = shards.length >= 64 ? 130 : 3 + 2 * shards.length;
3939
const buffer = new ArrayBuffer(totalLength);
4040
const view = new DataView(buffer);
4141

42-
view.setUint16(0, cluster);
42+
view.setUint16(0, clusterId);
4343

44-
if (indexList.length >= 64) {
44+
if (shards.length >= 64) {
4545
// rsv format (Bit Vector)
46-
for (const index of indexList) {
46+
for (const index of shards) {
4747
const byteIndex = Math.floor(index / 8) + 2; // Adjusted for the 2-byte cluster field
4848
const bitIndex = 7 - (index % 8);
4949
view.setUint8(byteIndex, view.getUint8(byteIndex) | (1 << bitIndex));
5050
}
5151
} else {
5252
// rs format (Index List)
53-
view.setUint8(2, indexList.length);
54-
for (let i = 0, offset = 3; i < indexList.length; i++, offset += 2) {
55-
view.setUint16(offset, indexList[i]);
53+
view.setUint8(2, shards.length);
54+
for (let i = 0, offset = 3; i < shards.length; i++, offset += 2) {
55+
view.setUint16(offset, shards[i]);
5656
}
5757
}
5858

0 commit comments

Comments
 (0)