Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 22 additions & 3 deletions packages/api/src/beacon/routes/node.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {ContainerType, ValueOf} from "@chainsafe/ssz";
import {ChainForkConfig} from "@lodestar/config";
import {ssz, stringType} from "@lodestar/types";
import {fulu, ssz, stringType} from "@lodestar/types";
import {
ArrayOf,
EmptyArgs,
Expand All @@ -24,6 +24,7 @@ export const NetworkIdentityType = new ContainerType(
enr: stringType,
p2pAddresses: ArrayOf(stringType),
discoveryAddresses: ArrayOf(stringType),
// TODO Fulu: replace with `ssz.fulu.Metadata` once `custody_group_count` is more widely supported
/** Based on Ethereum Consensus [Metadata object](https://github.com/ethereum/consensus-specs/blob/v1.1.10/specs/phase0/p2p-interface.md#metadata) */
metadata: ssz.altair.Metadata,
},
Expand Down Expand Up @@ -56,7 +57,9 @@ export const SyncingStatusType = new ContainerType(
{jsonCase: "eth2"}
);

export type NetworkIdentity = ValueOf<typeof NetworkIdentityType>;
export type NetworkIdentity = ValueOf<typeof NetworkIdentityType> & {
metadata: Partial<fulu.Metadata>;
};

export type PeerState = "disconnected" | "connecting" | "connected" | "disconnecting";
export type PeerDirection = "inbound" | "outbound";
Expand Down Expand Up @@ -190,7 +193,23 @@ export function getDefinitions(_config: ChainForkConfig): RouteDefinitions<Endpo
req: EmptyRequestCodec,
resp: {
onlySupport: WireFormat.json,
data: NetworkIdentityType,
// TODO Fulu: clean this up
data: {
...JsonOnlyResponseCodec.data,
toJson: (data) => {
const json = NetworkIdentityType.toJson(data);
(json as {metadata: {custody_group_count: number | undefined}}).metadata.custody_group_count =
data.metadata.custodyGroupCount;
return json;
},
fromJson: (json) => {
const data = NetworkIdentityType.fromJson(json);
(data.metadata as Partial<fulu.Metadata>).custodyGroupCount = (
json as {metadata: {custody_group_count: number | undefined}}
).metadata.custody_group_count;
return data;
},
},
meta: EmptyMetaCodec,
},
},
Expand Down
2 changes: 1 addition & 1 deletion packages/api/test/unit/beacon/testData/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export const testData: GenericServerTestCases<Endpoints> = {
enr: "enr",
p2pAddresses: ["p2pAddresses"],
discoveryAddresses: ["discoveryAddresses"],
metadata: ssz.altair.Metadata.defaultValue(),
metadata: ssz.fulu.Metadata.defaultValue(),
},
},
},
Expand Down
2 changes: 1 addition & 1 deletion packages/beacon-node/src/network/core/networkCore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ export class NetworkCore implements INetworkCore {

async setAdvertisedGroupCount(count: number): Promise<void> {
this.networkConfig.setAdvertisedGroupCount(count);
this.metadata.cgc = count;
this.metadata.custodyGroupCount = count;
}

// REST API queries
Expand Down
16 changes: 8 additions & 8 deletions packages/beacon-node/src/network/metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export class MetadataController {
this.onSetValue = modules.onSetValue;
this._metadata = opts.metadata ?? {
...ssz.fulu.Metadata.defaultValue(),
cgc: modules.networkConfig.getCustodyConfig().advertisedCustodyGroupCount,
custodyGroupCount: modules.networkConfig.getCustodyConfig().advertisedCustodyGroupCount,
};
}

Expand All @@ -63,7 +63,7 @@ export class MetadataController {
}

// Set CGC regardless of fork. It may be useful to clients before Fulu, and will be ignored otherwise.
this.onSetValue(ENRKey.cgc, serializeCgc(this._metadata.cgc));
this.onSetValue(ENRKey.cgc, serializeCgc(this._metadata.custodyGroupCount));
}

get seqNumber(): bigint {
Expand All @@ -89,16 +89,16 @@ export class MetadataController {
this._metadata.attnets = attnets;
}

get cgc(): number {
return this._metadata.cgc;
get custodyGroupCount(): number {
return this._metadata.custodyGroupCount;
}

set cgc(cgc: number) {
if (cgc === this._metadata.cgc) {
set custodyGroupCount(custodyGroupCount: number) {
if (custodyGroupCount === this._metadata.custodyGroupCount) {
return;
}
this.onSetValue(ENRKey.cgc, serializeCgc(cgc));
this._metadata.cgc = cgc;
this.onSetValue(ENRKey.cgc, serializeCgc(custodyGroupCount));
this._metadata.custodyGroupCount = custodyGroupCount;
}

/** Consumers that need the phase0.Metadata type can just ignore the .syncnets property */
Expand Down
19 changes: 10 additions & 9 deletions packages/beacon-node/src/network/peers/peerManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -328,27 +328,28 @@ export class PeerManager {
this.logger.warn("onMetadata", {
peer: peer.toString(),
peerData: peerData !== undefined,
cgc: (metadata as Partial<fulu.Metadata>).cgc,
custodyGroupCount: (metadata as Partial<fulu.Metadata>).custodyGroupCount,
});
if (peerData) {
const oldMetadata = peerData.metadata;
const cgc = (metadata as Partial<fulu.Metadata>).cgc ?? this.config.CUSTODY_REQUIREMENT;
const custodyGroupCount =
(metadata as Partial<fulu.Metadata>).custodyGroupCount ?? this.config.CUSTODY_REQUIREMENT;
const nodeId = peerData?.nodeId ?? computeNodeId(peer);
const custodyGroups =
oldMetadata == null || oldMetadata.custodyGroups == null || cgc !== oldMetadata.cgc
? getCustodyGroups(nodeId, cgc)
oldMetadata == null || oldMetadata.custodyGroups == null || custodyGroupCount !== oldMetadata.custodyGroupCount
? getCustodyGroups(nodeId, custodyGroupCount)
: oldMetadata.custodyGroups;
peerData.metadata = {
seqNumber: metadata.seqNumber,
attnets: metadata.attnets,
syncnets: (metadata as Partial<fulu.Metadata>).syncnets ?? BitArray.fromBitLen(SYNC_COMMITTEE_SUBNET_COUNT),
cgc:
(metadata as Partial<fulu.Metadata>).cgc ??
custodyGroupCount:
(metadata as Partial<fulu.Metadata>).custodyGroupCount ??
// TODO: spec says that Clients MAY reject peers with a value less than CUSTODY_REQUIREMENT
this.config.CUSTODY_REQUIREMENT,
custodyGroups,
};
if (oldMetadata === null || oldMetadata.cgc !== peerData.metadata.cgc) {
if (oldMetadata === null || oldMetadata.custodyGroupCount !== peerData.metadata.custodyGroupCount) {
void this.requestStatus(peer, this.statusCache.get());
}
}
Expand Down Expand Up @@ -417,7 +418,7 @@ export class PeerManager {
}
if (getConnection(this.libp2p, peer.toString())) {
const nodeId = peerData?.nodeId ?? computeNodeId(peer);
const custodyGroupCount = peerData?.metadata?.cgc;
const custodyGroupCount = peerData?.metadata?.custodyGroupCount;

const peerCustodyGroupCount = custodyGroupCount ?? this.config.CUSTODY_REQUIREMENT;
const dataColumns = getDataColumns(nodeId, peerCustodyGroupCount);
Expand Down Expand Up @@ -805,7 +806,7 @@ export class PeerManager {

// TODO: Consider optimizing by doing observe in batch
metrics.peerLongLivedAttnets.observe(attnets ? attnets.getTrueBitIndexes().length : 0);
metrics.peerColumnGroupCount.observe(peerData?.metadata?.cgc ?? 0);
metrics.peerColumnGroupCount.observe(peerData?.metadata?.custodyGroupCount ?? 0);
metrics.peerScoreByClient.observe({client}, this.peerRpcScores.getScore(peerId));
metrics.peerGossipScoreByClient.observe({client}, this.peerRpcScores.getGossipScore(peerId));
metrics.peerConnectionLength.observe((now - openCnx.timeline.open) / 1000);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ describe("data serialization through worker boundary", () => {
enr: "test-enr",
p2pAddresses: ["/ip4/1.2.3.4/tcp/0"],
discoveryAddresses: ["/ip4/1.2.3.4/tcp/0"],
metadata: ssz.altair.Metadata.defaultValue(),
metadata: ssz.fulu.Metadata.defaultValue(),
},
subscribeGossipCoreTopics: null,
unsubscribeGossipCoreTopics: null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,13 +188,13 @@ describe("network / peers / PeerManager", () => {

// Simulate peer1 returning a PING and STATUS message
const remoteStatus = statusCache.get();
const cgc = config.CUSTODY_REQUIREMENT;
const custodyGroupCount = config.CUSTODY_REQUIREMENT;
const remoteMetadata: NonNullable<ReturnType<PeerManager["connectedPeers"]["get"]>>["metadata"] = {
seqNumber: BigInt(1),
attnets: getAttnets(),
syncnets: getSyncnets(),
cgc,
custodyGroups: getCustodyGroups(computeNodeId(peerId1), cgc),
custodyGroupCount,
custodyGroups: getCustodyGroups(computeNodeId(peerId1), custodyGroupCount),
};
reqResp.sendPing.mockResolvedValue(remoteMetadata.seqNumber);
reqResp.sendStatus.mockResolvedValue(remoteStatus);
Expand Down
2 changes: 1 addition & 1 deletion packages/beacon-node/test/unit/network/metadata.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ describe("network / metadata", () => {
const onSetValue = vi.fn();
const networkConfig = new NetworkConfig(getValidPeerId(), config);
const metadata = new MetadataController({}, {onSetValue, networkConfig});
metadata.cgc = 128;
metadata.custodyGroupCount = 128;
expect(onSetValue).toHaveBeenCalledWith(ENRKey.cgc, serializeCgc(128));
});
});
Expand Down
2 changes: 1 addition & 1 deletion packages/types/src/fulu/sszTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export const Blob = denebSsz.Blob;
export const Metadata = new ContainerType(
{
...altariSsz.Metadata.fields,
cgc: UintNum64,
custodyGroupCount: UintNum64,
},
{typeName: "Metadata", jsonCase: "eth2"}
);
Expand Down
Loading