revert shard info -> relay shard renaming

This commit is contained in:
fryorcraken 2025-07-19 21:50:51 +10:00
parent e5919a6bd9
commit 0739fd1dd2
No known key found for this signature in database
GPG Key ID: A82ED75A8DFC50A4
14 changed files with 62 additions and 55 deletions

View File

@ -4,7 +4,7 @@ import {
DEFAULT_NUM_SHARDS,
NetworkConfig,
PubsubTopic,
RelayShards
ShardInfo
} from "@waku/interfaces";
import { contentTopicToShardIndex, encodeRelayShard } from "@waku/utils";
import { expect } from "chai";
@ -39,7 +39,7 @@ describe("ShardReader", function () {
numShardsInCluster: DEFAULT_NUM_SHARDS
};
const testRelayShards: RelayShards = {
const testShardInfo: ShardInfo = {
clusterId: testClusterId,
shards: [testShardIndex]
};
@ -98,7 +98,7 @@ describe("ShardReader", function () {
describe("isPeerOnNetwork", function () {
it("should return true when peer is on the same cluster", async function () {
const shardInfoBytes = encodeRelayShard(testRelayShards);
const shardInfoBytes = encodeRelayShard(testShardInfo);
const mockPeer = {
metadata: new Map([["shardInfo", shardInfoBytes]])
};
@ -112,7 +112,7 @@ describe("ShardReader", function () {
});
it("should return false when peer is on different cluster", async function () {
const differentClusterShardInfo: RelayShards = {
const differentClusterShardInfo: ShardInfo = {
clusterId: 5,
shards: [1, 2]
};
@ -129,7 +129,7 @@ describe("ShardReader", function () {
});
it("should return true even if peer has no overlapping shards", async function () {
const noOverlapShardInfo: RelayShards = {
const noOverlapShardInfo: ShardInfo = {
clusterId: testClusterId,
shards: [testShardIndex + 100, testShardIndex + 200] // Use different shards
};
@ -168,7 +168,7 @@ describe("ShardReader", function () {
describe("isPeerOnShard", function () {
it("should return true when peer is on the specified shard", async function () {
const shardInfoBytes = encodeRelayShard(testRelayShards);
const shardInfoBytes = encodeRelayShard(testShardInfo);
const mockPeer = {
metadata: new Map([["shardInfo", shardInfoBytes]])
};
@ -185,7 +185,7 @@ describe("ShardReader", function () {
});
it("should return false when peer is on different cluster", async function () {
const shardInfoBytes = encodeRelayShard(testRelayShards);
const shardInfoBytes = encodeRelayShard(testShardInfo);
const mockPeer = {
metadata: new Map([["shardInfo", shardInfoBytes]])
};
@ -202,7 +202,7 @@ describe("ShardReader", function () {
});
it("should return false when peer is not on the specified shard", async function () {
const shardInfoBytes = encodeRelayShard(testRelayShards);
const shardInfoBytes = encodeRelayShard(testShardInfo);
const mockPeer = {
metadata: new Map([["shardInfo", shardInfoBytes]])
};
@ -233,7 +233,7 @@ describe("ShardReader", function () {
describe("isPeerOnTopic", function () {
it("should return true when peer is on the pubsub topic shard", async function () {
const shardInfoBytes = encodeRelayShard(testRelayShards);
const shardInfoBytes = encodeRelayShard(testShardInfo);
const mockPeer = {
metadata: new Map([["shardInfo", shardInfoBytes]])
};
@ -248,7 +248,7 @@ describe("ShardReader", function () {
});
it("should return false when peer is not on the pubsub topic shard", async function () {
const shardInfoBytes = encodeRelayShard(testRelayShards);
const shardInfoBytes = encodeRelayShard(testShardInfo);
const mockPeer = {
metadata: new Map([["shardInfo", shardInfoBytes]])
};
@ -263,7 +263,7 @@ describe("ShardReader", function () {
});
it("should return false when pubsub topic parsing fails", async function () {
const shardInfoBytes = encodeRelayShard(testRelayShards);
const shardInfoBytes = encodeRelayShard(testShardInfo);
const mockPeer = {
metadata: new Map([["shardInfo", shardInfoBytes]])
};

View File

@ -3,8 +3,8 @@ import type {
ClusterId,
NetworkConfig,
PubsubTopic,
RelayShards,
ShardId
ShardId,
ShardInfo
} from "@waku/interfaces";
import {
decodeRelayShard,
@ -96,7 +96,7 @@ export class ShardReader implements IShardReader {
);
}
private async getRelayShards(id: PeerId): Promise<RelayShards | undefined> {
private async getRelayShards(id: PeerId): Promise<ShardInfo | undefined> {
try {
const peer = await this.libp2p.peerStore.get(id);

View File

@ -7,7 +7,7 @@ import {
type MetadataQueryResult,
type PeerIdStr,
ProtocolError,
type RelayShards
type ShardInfo
} from "@waku/interfaces";
import { proto_metadata } from "@waku/proto";
import { encodeRelayShard, Logger } from "@waku/utils";
@ -25,7 +25,7 @@ export const MetadataCodec = "/vac/waku/metadata/1.0.0";
class Metadata implements IMetadata {
private readonly streamManager: StreamManager;
private readonly libp2pComponents: Libp2pComponents;
protected handshakesConfirmed: Map<PeerIdStr, RelayShards> = new Map();
protected handshakesConfirmed: Map<PeerIdStr, ShardInfo> = new Map();
public readonly multicodec = MetadataCodec;
@ -148,7 +148,7 @@ class Metadata implements IMetadata {
});
const response = proto_metadata.WakuMetadataResponse.decode(
bytes
) as RelayShards;
) as ShardInfo;
if (!response) {
log.error("Error decoding metadata response");
@ -166,16 +166,16 @@ class Metadata implements IMetadata {
private async savePeerShardInfo(
peerId: PeerId,
relayShards: RelayShards
shardInfo: ShardInfo
): Promise<void> {
// add or update the relayShards to peer store
// add or update the shardInfo to peer store
await this.libp2pComponents.peerStore.merge(peerId, {
metadata: {
shardInfo: encodeRelayShard(relayShards)
shardInfo: encodeRelayShard(shardInfo)
}
});
this.handshakesConfirmed.set(peerId.toString(), relayShards);
this.handshakesConfirmed.set(peerId.toString(), shardInfo);
}
}

View File

@ -10,7 +10,7 @@ import type {
import {
type Libp2pComponents,
type PeerExchangeQueryResult,
type RelayShards,
ShardInfo,
Tags
} from "@waku/interfaces";
import { decodeRelayShard, encodeRelayShard, Logger } from "@waku/utils";
@ -279,7 +279,7 @@ export class PeerExchangeDiscovery
private async checkPeerInfoDiff(
peerInfo: PeerInfo,
shardInfo?: RelayShards
shardInfo?: ShardInfo
): Promise<{ hasMultiaddrDiff: boolean; hasShardDiff: boolean }> {
const { id: peerId } = peerInfo;
const peer = await this.components.peerStore.get(peerId);

View File

@ -5,8 +5,8 @@ import type {
ENRValue,
IEnr,
NodeId,
RelayShards,
SequenceNumber
SequenceNumber,
ShardInfo
} from "@waku/interfaces";
import { Logger } from "@waku/utils";
@ -64,7 +64,7 @@ export class ENR extends RawEnr implements IEnr {
protocol: TransportProtocol | TransportProtocolPerIpVersion
) => Multiaddr | undefined = locationMultiaddrFromEnrFields.bind({}, this);
public get shardInfo(): RelayShards | undefined {
public get shardInfo(): ShardInfo | undefined {
if (this.rs && this.rsv) {
log.warn("ENR contains both `rs` and `rsv` fields.");
}

View File

@ -6,8 +6,8 @@ import {
import type {
ENRKey,
ENRValue,
RelayShards,
SequenceNumber,
ShardInfo,
Waku2
} from "@waku/interfaces";
import { decodeRelayShard } from "@waku/utils";
@ -52,13 +52,13 @@ export class RawEnr extends Map<ENRKey, ENRValue> {
}
}
public get rs(): RelayShards | undefined {
public get rs(): ShardInfo | undefined {
const rs = this.get("rs");
if (!rs) return undefined;
return decodeRelayShard(rs);
}
public get rsv(): RelayShards | undefined {
public get rsv(): ShardInfo | undefined {
const rsv = this.get("rsv");
if (!rsv) return undefined;
return decodeRelayShard(rsv);

View File

@ -2,7 +2,7 @@ import type { PeerId } from "@libp2p/interface";
import type { PeerInfo } from "@libp2p/interface";
import type { Multiaddr } from "@multiformats/multiaddr";
import { RelayShards } from "./sharding.js";
import { ShardInfo } from "./sharding.js";
export type ENRKey = string;
export type ENRValue = Uint8Array;
@ -36,7 +36,7 @@ export interface IEnr extends Map<ENRKey, ENRValue> {
multiaddrs?: Multiaddr[];
waku2?: Waku2;
peerInfo: PeerInfo | undefined;
shardInfo?: RelayShards;
shardInfo?: ShardInfo;
/**
* @deprecated: use { @link IEnr.peerInfo } instead.

View File

@ -1,9 +1,9 @@
import type { PeerId } from "@libp2p/interface";
import { ThisOrThat } from "./misc.js";
import type { ClusterId, RelayShards } from "./sharding.js";
import type { ClusterId, ShardInfo } from "./sharding.js";
export type MetadataQueryResult = ThisOrThat<"shardInfo", RelayShards>;
export type MetadataQueryResult = ThisOrThat<"shardInfo", ShardInfo>;
export interface IMetadata {
readonly multicodec: string;

View File

@ -4,7 +4,7 @@
*/
export type NetworkConfig = StaticSharding | AutoSharding;
export type RelayShards = {
export type ShardInfo = {
clusterId: ClusterId;
shards: ShardId[];
};

View File

@ -1,9 +1,11 @@
import { PeerId } from "@libp2p/interface";
import {
ClusterId,
CONNECTION_LOCKED_TAG,
IConnectionManager,
Libp2p,
Protocols
Protocols,
ShardId
} from "@waku/interfaces";
import { expect } from "chai";
import sinon from "sinon";
@ -81,7 +83,12 @@ describe("PeerManager", () => {
pubsubTopics: [TEST_PUBSUB_TOPIC],
getConnectedPeers: async () => peers,
getPeers: async () => peers,
isPeerOnShard: async (_id: PeerId, _topic: string) => true
isPeerOnShard: async (
_id: PeerId,
_clusterId: ClusterId,
_shardId: ShardId
) => true,
isPeerOnTopic: async (_id: PeerId, _topic: string) => true
} as unknown as IConnectionManager;
peerManager = new PeerManager({
libp2p,

View File

@ -5,7 +5,7 @@
* @module
*/
import { AutoSharding, RelayShards } from "@waku/interfaces";
import { AutoSharding, ShardInfo } from "@waku/interfaces";
import { createRoutingInfo } from "@waku/utils";
export const NOISE_KEY_1 = new Uint8Array(
@ -91,7 +91,7 @@ export const DefaultTestNetworkConfig: AutoSharding = {
clusterId: DefaultTestClusterId,
numShardsInCluster: DefaultTestNumShardsInCluster
};
export const DefaultTestRelayShards: RelayShards = {
export const DefaultTestShardInfo: ShardInfo = {
clusterId: DefaultTestClusterId,
shards: [0]
};

View File

@ -3,8 +3,8 @@ import { type PeerId } from "@libp2p/interface";
import { peerIdFromPrivateKey } from "@libp2p/peer-id";
import { multiaddr } from "@multiformats/multiaddr";
import { PeerExchangeDiscovery } from "@waku/discovery";
import { IEnr, LightNode, RelayShards } from "@waku/interfaces";
import { createLightNode } from "@waku/sdk";
import { IEnr, LightNode } from "@waku/interfaces";
import { createLightNode, ShardInfo } from "@waku/sdk";
import { decodeRelayShard } from "@waku/utils";
import { expect } from "chai";
import Sinon from "sinon";
@ -15,7 +15,7 @@ describe("Peer Exchange Continuous Discovery", () => {
let peerId: PeerId;
let randomPeerId: PeerId;
let waku: LightNode;
const relayShards: RelayShards = {
const shardInfo: ShardInfo = {
clusterId: 2,
shards: [1, 2]
};
@ -38,7 +38,7 @@ describe("Peer Exchange Continuous Discovery", () => {
const newPeerInfo = {
ENR: {
peerId,
shardInfo: relayShards,
shardInfo,
peerInfo: {
multiaddrs: newMultiaddrs,
id: peerId
@ -59,14 +59,14 @@ describe("Peer Exchange Continuous Discovery", () => {
});
it("Should update shard info", async () => {
const newRelayShards: RelayShards = {
const newShardInfo: ShardInfo = {
clusterId: 2,
shards: [1, 2, 3]
};
const newPeerInfo = {
ENR: {
peerId,
shardInfo: newRelayShards,
shardInfo: newShardInfo,
peerInfo: {
multiaddrs: multiaddrs,
id: peerId
@ -86,7 +86,7 @@ describe("Peer Exchange Continuous Discovery", () => {
);
const _shardInfo = decodeRelayShard(newPeer.metadata.get("shardInfo")!);
expect(_shardInfo).to.deep.equal(newRelayShards);
expect(_shardInfo).to.deep.equal(newShardInfo);
});
async function discoverPeerOnce(): Promise<void> {
@ -95,7 +95,7 @@ describe("Peer Exchange Continuous Discovery", () => {
const enr: IEnr = {
peerId,
shardInfo: relayShards,
shardInfo,
peerInfo: {
multiaddrs: multiaddrs,
id: peerId
@ -122,6 +122,6 @@ describe("Peer Exchange Continuous Discovery", () => {
multiaddrs[0].toString()
);
const _shardInfo = decodeRelayShard(peer.metadata.get("shardInfo")!);
expect(_shardInfo).to.deep.equal(relayShards);
expect(_shardInfo).to.deep.equal(shardInfo);
}
});

View File

@ -12,7 +12,7 @@ import {
beforeEachCustom,
DefaultTestClusterId,
DefaultTestNetworkConfig,
DefaultTestRelayShards,
DefaultTestShardInfo,
makeLogFileName,
ServiceNode,
tearDownNodes
@ -33,14 +33,14 @@ describe("Peer Exchange", function () {
nwaku2 = new ServiceNode(makeLogFileName(this.ctx) + "2");
await nwaku1.start({
clusterId: DefaultTestClusterId,
shard: DefaultTestRelayShards.shards,
shard: DefaultTestShardInfo.shards,
discv5Discovery: true,
peerExchange: true,
relay: true
});
await nwaku2.start({
clusterId: DefaultTestClusterId,
shard: DefaultTestRelayShards.shards,
shard: DefaultTestShardInfo.shards,
discv5Discovery: true,
peerExchange: true,
discv5BootstrapNode: (await nwaku1.info()).enrUri,
@ -120,7 +120,7 @@ describe("Peer Exchange", function () {
nwaku3 = new ServiceNode(makeLogFileName(this) + "3");
await nwaku3.start({
clusterId: DefaultTestClusterId,
shard: DefaultTestRelayShards.shards,
shard: DefaultTestShardInfo.shards,
discv5Discovery: true,
peerExchange: true,
discv5BootstrapNode: (await nwaku1.info()).enrUri,

View File

@ -1,6 +1,6 @@
import type { RelayShards } from "@waku/interfaces";
import type { ShardInfo } from "@waku/interfaces";
export const decodeRelayShard = (bytes: Uint8Array): RelayShards => {
export const decodeRelayShard = (bytes: Uint8Array): ShardInfo => {
// explicitly converting to Uint8Array to avoid Buffer
// https://github.com/libp2p/js-libp2p/issues/2146
bytes = new Uint8Array(bytes);
@ -33,8 +33,8 @@ export const decodeRelayShard = (bytes: Uint8Array): RelayShards => {
return { clusterId, shards };
};
export const encodeRelayShard = (relayShards: RelayShards): Uint8Array => {
const { clusterId, shards } = relayShards;
export const encodeRelayShard = (shardInfo: ShardInfo): Uint8Array => {
const { clusterId, shards } = shardInfo;
const totalLength = shards.length >= 64 ? 130 : 3 + 2 * shards.length;
const buffer = new ArrayBuffer(totalLength);
const view = new DataView(buffer);