Skip to content

Commit

Permalink
fixes for metadata, working locally
Browse files Browse the repository at this point in the history
  • Loading branch information
g11tech committed Aug 12, 2024
1 parent 78e50c6 commit 5fbbdb2
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 2 deletions.
1 change: 1 addition & 0 deletions packages/beacon-node/src/network/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -684,6 +684,7 @@ export class Network implements INetwork {
};

private onPeerConnected = (data: NetworkEventData[NetworkEvent.peerConnected]): void => {
this.logger.warn("onPeerConnected", {peer: data.peer, dataColumns: data.dataColumns.join(",")});
this.connectedPeers.set(data.peer, data.dataColumns);
};

Expand Down
20 changes: 18 additions & 2 deletions packages/beacon-node/src/network/peers/peerManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,11 @@ export class PeerManager {
private onPing(peer: PeerId, seqNumber: phase0.Ping): void {
// if the sequence number is unknown update the peer's metadata
const metadata = this.connectedPeers.get(peer.toString())?.metadata;
this.logger.warn("onPing", {
seqNumber,
metaSeqNumber: metadata?.seqNumber,
cond: !metadata || metadata.seqNumber < seqNumber,
});
if (!metadata || metadata.seqNumber < seqNumber) {
void this.requestMetadata(peer);
}
Expand All @@ -320,7 +325,10 @@ export class PeerManager {
// Store metadata always in case the peer updates attnets but not the sequence number
// Trust that the peer always sends the latest metadata (From Lighthouse)
const peerData = this.connectedPeers.get(peer.toString());
this.logger.warn("onMetadata", {peer: peer.toString(), peerData: peerData !== undefined});
console.log("onMetadata", metadata);
if (peerData) {
const oldMetadata = peerData.metadata;
peerData.metadata = {
seqNumber: metadata.seqNumber,
attnets: metadata.attnets,
Expand All @@ -330,6 +338,9 @@ export class PeerManager {
this.discovery?.["peerIdToCustodySubnetCount"].get(peer.toString()) ??
this.config.CUSTODY_REQUIREMENT,
};
if (oldMetadata === null || oldMetadata.csc < peerData.metadata.csc) {
void this.requestStatus(peer, this.statusCache.get());
}
}
}

Expand Down Expand Up @@ -396,9 +407,10 @@ export class PeerManager {
}
if (getConnection(this.libp2p, peer.toString())) {
const nodeId = peerData?.nodeId ?? computeNodeId(peer);
console.log("onStatus", peerData?.metadata);
const custodySubnetCount = peerData?.metadata?.csc;

const peerCustodySubnetCount = custodySubnetCount ?? this.config.CUSTODY_REQUIREMENT;
const peerCustodySubnetCount = custodySubnetCount ?? 4;
const peerCustodySubnets = getCustodyColumnSubnets(nodeId, peerCustodySubnetCount);

const matchingSubnetsNum = this.custodySubnets.reduce(
Expand All @@ -410,6 +422,7 @@ export class PeerManager {

this.logger.warn(`onStatus ${custodySubnetCount == undefined ? "undefined custody count assuming 4" : ""}`, {
nodeId: toHexString(nodeId),
myNodeId: toHexString(this.nodeId),
peerId: peer.toString(),
custodySubnetCount,
hasAllColumns,
Expand Down Expand Up @@ -441,14 +454,17 @@ export class PeerManager {

private async requestMetadata(peer: PeerId): Promise<void> {
try {
this.logger.warn("requestMetadata", {peer: peer.toString()});
this.onMetadata(peer, await this.reqResp.sendMetadata(peer));
} catch (e) {
console.log("requestMetadata", e);
// TODO: Downvote peer here or in the reqResp layer
}
}

private async requestPing(peer: PeerId): Promise<void> {
try {
this.logger.warn("requestPing", {peer: peer.toString()});
this.onPing(peer, await this.reqResp.sendPing(peer));

// If peer replies a PING request also update lastReceivedMsg
Expand Down Expand Up @@ -663,7 +679,7 @@ export class PeerManager {
this.connectedPeers.set(remotePeer.toString(), peerData);

if (direction === "outbound") {
//this.pingAndStatusTimeouts();
// this.pingAndStatusTimeouts();
void this.requestPing(remotePeer);
void this.requestStatus(remotePeer, this.statusCache.get());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ export class ReqRespBeaconNode extends ReqResp {

if (ForkSeq[fork] >= ForkSeq.electra) {
protocolsAtFork.push(
[protocols.MetadataV3(this.config), this.onMetadata.bind(this)],
[protocols.DataColumnSidecarsByRoot(this.config), this.getHandler(ReqRespMethod.DataColumnSidecarsByRoot)],
[protocols.DataColumnSidecarsByRange(this.config), this.getHandler(ReqRespMethod.DataColumnSidecarsByRange)]
);
Expand Down
6 changes: 6 additions & 0 deletions packages/beacon-node/src/network/reqresp/protocols.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ export const MetadataV2 = toProtocol({
contextBytesType: ContextBytesType.Empty,
});

export const MetadataV3 = toProtocol({
method: ReqRespMethod.Metadata,
version: Version.V3,
contextBytesType: ContextBytesType.Empty,
});

export const Ping = toProtocol({
method: ReqRespMethod.Ping,
version: Version.V1,
Expand Down

0 comments on commit 5fbbdb2

Please sign in to comment.