Skip to content

Commit

Permalink
make the csc encoding updates as per latest spec
Browse files Browse the repository at this point in the history
  • Loading branch information
g11tech committed Sep 5, 2024
1 parent 585165e commit 2833ac0
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 7 deletions.
8 changes: 6 additions & 2 deletions packages/beacon-node/src/network/metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {ForkSeq} from "@lodestar/params";
import {computeStartSlotAtEpoch} from "@lodestar/state-transition";
import {altair, Epoch, phase0, ssz, peerdas} from "@lodestar/types";
import {BeaconConfig} from "@lodestar/config";
import {intToBytes} from "@lodestar/utils";
import {FAR_FUTURE_EPOCH} from "../constants/index.js";
import {getCurrentAndNextFork} from "./forks.js";

Expand Down Expand Up @@ -59,7 +60,10 @@ export class MetadataController {
}

if (this.config.getForkSeq(computeStartSlotAtEpoch(currentEpoch)) >= ForkSeq.peerdas) {
this.onSetValue(ENRKey.csc, ssz.Uint8.serialize(this._metadata.csc));
this.onSetValue(
ENRKey.csc,
intToBytes(this._metadata.csc, Math.ceil(Math.log2(this._metadata.csc + 1) / 8), "be")
);
}
}

Expand Down Expand Up @@ -91,7 +95,7 @@ export class MetadataController {
}

set csc(csc: number) {
this.onSetValue(ENRKey.csc, ssz.Uint8.serialize(csc));
this.onSetValue(ENRKey.csc, intToBytes(this._metadata.csc, Math.ceil(Math.log2(this._metadata.csc + 1) / 8), "be"));
this._metadata.csc = csc;
}

Expand Down
5 changes: 4 additions & 1 deletion packages/beacon-node/src/network/peers/discover.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {pruneSetToMax, sleep} from "@lodestar/utils";
import {ATTESTATION_SUBNET_COUNT, SYNC_COMMITTEE_SUBNET_COUNT} from "@lodestar/params";
import {LoggerNode} from "@lodestar/logger/node";
import {ssz} from "@lodestar/types";
import {bytesToInt} from "@lodestar/utils";
import {NetworkCoreMetrics} from "../core/metrics.js";
import {Libp2p} from "../interface.js";
import {ENRKey, SubnetType} from "../metadata.js";
Expand Down Expand Up @@ -375,7 +376,9 @@ export class PeerDiscovery {
// never throw and treat too long or too short bitfields as zero-ed
const attnets = attnetsBytes ? deserializeEnrSubnets(attnetsBytes, ATTESTATION_SUBNET_COUNT) : zeroAttnets;
const syncnets = syncnetsBytes ? deserializeEnrSubnets(syncnetsBytes, SYNC_COMMITTEE_SUBNET_COUNT) : zeroSyncnets;
const custodySubnetCount = custodySubnetCountBytes ? ssz.Uint8.deserialize(custodySubnetCountBytes) : 4;
const custodySubnetCount = custodySubnetCountBytes
? bytesToInt(custodySubnetCountBytes, "be")
: this.config.CUSTODY_REQUIREMENT;
this.peerIdToCustodySubnetCount.set(peerId.toString(), custodySubnetCount);

const status = this.handleDiscoveredPeer(peerId, multiaddrTCP, attnets, syncnets, custodySubnetCount);
Expand Down
5 changes: 3 additions & 2 deletions packages/cli/src/cmds/beacon/initPeerIdAndEnr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {Multiaddr} from "@multiformats/multiaddr";
import {createPrivateKeyFromPeerId, SignableENR} from "@chainsafe/enr";
import {Logger, fromHex} from "@lodestar/utils";
import {ChainForkConfig} from "@lodestar/config";
import {ssz} from "@lodestar/types";
import {intToBytes} from "@lodestar/utils";

import {exportToJSON, readPeerId} from "../../config/index.js";
import {writeFile600Perm} from "../../util/file.js";
Expand Down Expand Up @@ -89,7 +89,8 @@ export function overwriteEnrWithCliArgs(
}

// csc is big ending but since 1 bytes suffices for now so its the same
enr.set("csc", ssz.Uint8.serialize(Math.max(config.CUSTODY_REQUIREMENT, config.NODE_CUSTODY_REQUIREMENT)));
const csc = Math.max(config.CUSTODY_REQUIREMENT, config.NODE_CUSTODY_REQUIREMENT);
enr.set("csc", intToBytes(csc, Math.ceil(Math.log2(csc + 1) / 8), "be"));

function testMultiaddrForLocal(mu: Multiaddr, ip4: boolean): void {
const isLocal = isLocalMultiAddr(mu);
Expand Down
4 changes: 2 additions & 2 deletions packages/types/src/peerdas/sszTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ import {ssz as phase0Ssz} from "../phase0/index.js";
import {ssz as altariSsz} from "../altair/index.js";
import {ssz as denebSsz} from "../deneb/index.js";

const {BLSSignature, Root, ColumnIndex, Bytes32, Slot, UintNum64, Uint8} = primitiveSsz;
const {BLSSignature, Root, ColumnIndex, Bytes32, Slot, UintNum64} = primitiveSsz;

export const Metadata = new ContainerType(
{
...altariSsz.Metadata.fields,
csc: Uint8,
csc: UintNum64,
},
{typeName: "Metadata", jsonCase: "eth2"}
);
Expand Down

0 comments on commit 2833ac0

Please sign in to comment.