Skip to content

Commit 57b4614

Browse files
committed
refactor: use SubnetID type from CL spec instead of number
1 parent 48f6c94 commit 57b4614

File tree

28 files changed

+94
-84
lines changed

28 files changed

+94
-84
lines changed

packages/beacon-node/src/api/impl/validator/utils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import {routes} from "@lodestar/api";
22
import {ATTESTATION_SUBNET_COUNT} from "@lodestar/params";
33
import {BeaconStateAllForks, computeSlotsSinceEpochStart} from "@lodestar/state-transition";
4-
import {BLSPubkey, CommitteeIndex, ProducedBlockSource, Slot, ValidatorIndex} from "@lodestar/types";
4+
import {BLSPubkey, CommitteeIndex, ProducedBlockSource, Slot, SubnetID, ValidatorIndex} from "@lodestar/types";
55
import {MAX_BUILDER_BOOST_FACTOR} from "@lodestar/validator";
66
import {BlockSelectionResult, BuilderBlockSelectionReason, EngineBlockSelectionReason} from "./index.js";
77

88
export function computeSubnetForCommitteesAtSlot(
99
slot: Slot,
1010
committeesAtSlot: number,
1111
committeeIndex: CommitteeIndex
12-
): number {
12+
): SubnetID {
1313
const slotsSinceEpochStart = computeSlotsSinceEpochStart(slot);
1414
const committeesSinceEpochStart = committeesAtSlot * slotsSinceEpochStart;
1515
return (committeesSinceEpochStart + committeeIndex) % ATTESTATION_SUBNET_COUNT;

packages/beacon-node/src/chain/errors/blobSidecarError.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {RootHex, Slot, ValidatorIndex} from "@lodestar/types";
1+
import {RootHex, Slot, SubnetID, ValidatorIndex} from "@lodestar/types";
22
import {GossipActionError} from "./gossipValidation.js";
33

44
export enum BlobSidecarErrorCode {
@@ -26,7 +26,7 @@ export enum BlobSidecarErrorCode {
2626
}
2727

2828
export type BlobSidecarErrorType =
29-
| {code: BlobSidecarErrorCode.INVALID_INDEX; blobIdx: number; subnet: number}
29+
| {code: BlobSidecarErrorCode.INVALID_INDEX; blobIdx: number; subnet: SubnetID}
3030
| {code: BlobSidecarErrorCode.INVALID_KZG; blobIdx: number}
3131
| {code: BlobSidecarErrorCode.INVALID_KZG_TXS}
3232
| {code: BlobSidecarErrorCode.INCORRECT_SLOT; blockSlot: Slot; blobSlot: Slot; blobIdx: number}

packages/beacon-node/src/chain/opPools/syncCommitteeMessagePool.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ function aggregateSignatureInto(
146146
* Format `signature` into an efficient `contribution` to add more signatures in with aggregateSignatureInto()
147147
*/
148148
function signatureToAggregate(
149-
subnet: number,
149+
subnet: Subnet,
150150
signature: altair.SyncCommitteeMessage,
151151
indexInSubcommittee: number
152152
): ContributionFast {

packages/beacon-node/src/chain/seenCache/seenAttestationData.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {BitArray} from "@chainsafe/ssz";
2-
import {CommitteeIndex, RootHex, Slot, phase0} from "@lodestar/types";
2+
import {CommitteeIndex, RootHex, Slot, SubnetID, phase0} from "@lodestar/types";
33
import {MapDef} from "@lodestar/utils";
44
import {Metrics} from "../../metrics/metrics.js";
55
import {InsertOutcome} from "../opPools/types.js";
@@ -23,7 +23,7 @@ export type AttestationDataCacheEntry = {
2323
// caching this for 3 slots take 600 instances max, this is nothing compared to attestations processed per slot
2424
// for example in a mainnet node subscribing to all subnets, attestations are processed up to 20k per slot
2525
attestationData: phase0.AttestationData;
26-
subnet: number;
26+
subnet: SubnetID;
2727
};
2828

2929
export enum RejectReason {

packages/beacon-node/src/chain/seenCache/seenCommittee.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,6 @@ export class SeenSyncCommitteeMessages {
3838
}
3939
}
4040

41-
function seenCacheKey(subnet: number, validatorIndex: ValidatorIndex): ValidatorSubnetKey {
41+
function seenCacheKey(subnet: SubcommitteeIndex, validatorIndex: ValidatorIndex): ValidatorSubnetKey {
4242
return `${subnet}-${validatorIndex}`;
4343
}

packages/beacon-node/src/chain/validation/attestation.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import {
2727
Root,
2828
RootHex,
2929
Slot,
30+
SubnetID,
3031
electra,
3132
isElectraAttestation,
3233
phase0,
@@ -58,7 +59,7 @@ export type BatchResult = {
5859
export type AttestationValidationResult = {
5960
attestation: Attestation;
6061
indexedAttestation: IndexedAttestation;
61-
subnet: number;
62+
subnet: SubnetID;
6263
attDataRootHex: RootHex;
6364
committeeIndex: CommitteeIndex;
6465
};
@@ -93,7 +94,7 @@ export async function validateGossipAttestationsSameAttData(
9394
fork: ForkName,
9495
chain: IBeaconChain,
9596
attestationOrBytesArr: GossipAttestation[],
96-
subnet: number,
97+
subnet: SubnetID,
9798
// for unit test, consumers do not need to pass this
9899
step0ValidationFn = validateAttestationNoSignatureCheck
99100
): Promise<BatchResult> {
@@ -232,7 +233,7 @@ async function validateAttestationNoSignatureCheck(
232233
chain: IBeaconChain,
233234
attestationOrBytes: AttestationOrBytes,
234235
/** Optional, to allow verifying attestations through API with unknown subnet */
235-
subnet: number | null
236+
subnet: SubnetID | null
236237
): Promise<Step0Result> {
237238
// Do checks in this order:
238239
// - do early checks (w/o indexed attestation)
@@ -342,7 +343,7 @@ async function validateAttestationNoSignatureCheck(
342343

343344
let committeeValidatorIndices: Uint32Array;
344345
let getSigningRoot: () => Uint8Array;
345-
let expectedSubnet: number;
346+
let expectedSubnet: SubnetID;
346347
if (attestationOrCache.cache) {
347348
committeeValidatorIndices = attestationOrCache.cache.committeeValidatorIndices;
348349
const signingRoot = attestationOrCache.cache.signingRoot;
@@ -762,7 +763,7 @@ export function getCommitteeIndices(
762763
/**
763764
* Compute the correct subnet for a slot/committee index
764765
*/
765-
export function computeSubnetForSlot(shuffling: EpochShuffling, slot: number, committeeIndex: number): number {
766+
export function computeSubnetForSlot(shuffling: EpochShuffling, slot: number, committeeIndex: number): SubnetID {
766767
const slotsSinceEpochStart = slot % SLOTS_PER_EPOCH;
767768
const committeesSinceEpochStart = shuffling.committeesPerSlot * slotsSinceEpochStart;
768769
return (committeesSinceEpochStart + committeeIndex) % ATTESTATION_SUBNET_COUNT;

packages/beacon-node/src/chain/validation/blobSidecar.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {ChainConfig} from "@lodestar/config";
22
import {KZG_COMMITMENT_INCLUSION_PROOF_DEPTH, KZG_COMMITMENT_SUBTREE_INDEX0} from "@lodestar/params";
33
import {computeStartSlotAtEpoch, getBlockHeaderProposerSignatureSet} from "@lodestar/state-transition";
4-
import {BlobIndex, Root, Slot, deneb, ssz} from "@lodestar/types";
4+
import {BlobIndex, Root, Slot, SubnetID, deneb, ssz} from "@lodestar/types";
55
import {toRootHex, verifyMerkleBranch} from "@lodestar/utils";
66

77
import {byteArrayEquals} from "../../util/bytes.js";
@@ -14,7 +14,7 @@ import {RegenCaller} from "../regen/index.js";
1414
export async function validateGossipBlobSidecar(
1515
chain: IBeaconChain,
1616
blobSidecar: deneb.BlobSidecar,
17-
subnet: number
17+
subnet: SubnetID
1818
): Promise<void> {
1919
const blobSlot = blobSidecar.signedBlockHeader.message.slot;
2020

@@ -227,6 +227,6 @@ function validateInclusionProof(blobSidecar: deneb.BlobSidecar): boolean {
227227
);
228228
}
229229

230-
function computeSubnetForBlobSidecar(blobIndex: BlobIndex, config: ChainConfig): number {
230+
function computeSubnetForBlobSidecar(blobIndex: BlobIndex, config: ChainConfig): SubnetID {
231231
return blobIndex % config.BLOB_SIDECAR_SUBNET_COUNT;
232232
}

packages/beacon-node/src/chain/validation/syncCommittee.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {SYNC_COMMITTEE_SUBNET_COUNT, SYNC_COMMITTEE_SUBNET_SIZE} from "@lodestar/params";
22
import {CachedBeaconStateAllForks} from "@lodestar/state-transition";
3-
import {altair} from "@lodestar/types";
3+
import {SubnetID, altair} from "@lodestar/types";
44
import {toRootHex} from "@lodestar/utils";
55
import {GossipAction, SyncCommitteeError, SyncCommitteeErrorCode} from "../errors/index.js";
66
import {IBeaconChain} from "../interface.js";
@@ -14,7 +14,7 @@ type IndexInSubcommittee = number;
1414
export async function validateGossipSyncCommittee(
1515
chain: IBeaconChain,
1616
syncCommittee: altair.SyncCommitteeMessage,
17-
subnet: number
17+
subnet: SubnetID
1818
): Promise<{indexInSubcommittee: IndexInSubcommittee}> {
1919
const {slot, validatorIndex, beaconBlockRoot} = syncCommittee;
2020
const messageRoot = toRootHex(beaconBlockRoot);
@@ -103,7 +103,7 @@ async function validateSyncCommitteeSigOnly(
103103
export function validateGossipSyncCommitteeExceptSig(
104104
chain: IBeaconChain,
105105
headState: CachedBeaconStateAllForks,
106-
subnet: number,
106+
subnet: SubnetID,
107107
data: Pick<altair.SyncCommitteeMessage, "slot" | "validatorIndex">
108108
): IndexInSubcommittee {
109109
const {slot, validatorIndex} = data;
@@ -144,7 +144,7 @@ export function validateGossipSyncCommitteeExceptSig(
144144
*/
145145
function getIndexInSubcommittee(
146146
headState: CachedBeaconStateAllForks,
147-
subnet: number,
147+
subnet: SubnetID,
148148
data: Pick<altair.SyncCommitteeMessage, "slot" | "validatorIndex">
149149
): IndexInSubcommittee | null {
150150
const syncCommittee = headState.epochCtx.getIndexedSyncCommittee(data.slot);

packages/beacon-node/src/metrics/validatorMonitor.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
parseAttesterFlags,
1111
parseParticipationFlags,
1212
} from "@lodestar/state-transition";
13-
import {BeaconBlock, RootHex, altair, deneb} from "@lodestar/types";
13+
import {BeaconBlock, RootHex, SubnetID, altair, deneb} from "@lodestar/types";
1414
import {Epoch, Slot, ValidatorIndex} from "@lodestar/types";
1515
import {IndexedAttestation, SignedAggregateAndProof} from "@lodestar/types";
1616
import {LogData, LogHandler, LogLevel, Logger, MapDef, MapDefMax, toRootHex} from "@lodestar/utils";
@@ -52,7 +52,7 @@ export type ValidatorMonitor = {
5252
onPoolSubmitUnaggregatedAttestation(
5353
seenTimestampSec: number,
5454
indexedAttestation: IndexedAttestation,
55-
subnet: number,
55+
subnet: SubnetID,
5656
sentPeers: number
5757
): void;
5858
onPoolSubmitAggregatedAttestation(

packages/beacon-node/src/network/core/metrics.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import {SubnetID} from "@lodestar/types";
12
import {RegistryMetricCreator} from "../../metrics/utils/registryMetricCreator.js";
23
import {SubnetType} from "../metadata.js";
34
import {DiscoveredPeerStatus} from "../peers/discover.js";
@@ -194,14 +195,14 @@ export function createNetworkCoreMetrics(register: RegistryMetricCreator) {
194195
name: "lodestar_attnets_service_committee_subscriptions_total",
195196
help: "Count of committee subscriptions",
196197
}),
197-
subscriptionsCommitteeMeshPeers: register.histogram<{subnet: number}>({
198+
subscriptionsCommitteeMeshPeers: register.histogram<{subnet: SubnetID}>({
198199
name: "lodestar_attnets_service_committee_subscriptions_mesh_peers",
199200
help: "Histogram of mesh peers per committee subscription",
200201
labelNames: ["subnet"],
201202
// Dlow = 6, D = 8, DHi = 12 plus 2 more buckets
202203
buckets: [0, 4, 6, 8, 12],
203204
}),
204-
subscriptionsCommitteeTimeToStableMesh: register.histogram<{subnet: number}>({
205+
subscriptionsCommitteeTimeToStableMesh: register.histogram<{subnet: SubnetID}>({
205206
name: "lodestar_attnets_service_committee_subscriptions_time_to_stable_mesh_seconds",
206207
help: "Histogram of time until committee subscription is considered healthy (>= 6 mesh peers)",
207208
labelNames: ["subnet"],
@@ -216,12 +217,12 @@ export function createNetworkCoreMetrics(register: RegistryMetricCreator) {
216217
name: "lodestar_attnets_service_long_lived_subscriptions_total",
217218
help: "Count of long lived subscriptions",
218219
}),
219-
subscribeSubnets: register.gauge<{subnet: number; src: SubnetSource}>({
220+
subscribeSubnets: register.gauge<{subnet: SubnetID; src: SubnetSource}>({
220221
name: "lodestar_attnets_service_subscribe_subnets_total",
221222
help: "Count of subscribe_subnets calls",
222223
labelNames: ["subnet", "src"],
223224
}),
224-
unsubscribeSubnets: register.gauge<{subnet: number; src: SubnetSource}>({
225+
unsubscribeSubnets: register.gauge<{subnet: SubnetID; src: SubnetSource}>({
225226
name: "lodestar_attnets_service_unsubscribe_subnets_total",
226227
help: "Count of unsubscribe_subnets calls",
227228
labelNames: ["subnet", "src"],
@@ -237,12 +238,12 @@ export function createNetworkCoreMetrics(register: RegistryMetricCreator) {
237238
name: "lodestar_syncnets_service_committee_subscriptions_total",
238239
help: "Count of syncnet committee subscriptions",
239240
}),
240-
subscribeSubnets: register.gauge<{subnet: number}>({
241+
subscribeSubnets: register.gauge<{subnet: SubnetID}>({
241242
name: "lodestar_syncnets_service_subscribe_subnets_total",
242243
help: "Count of syncnet subscribe_subnets calls",
243244
labelNames: ["subnet"],
244245
}),
245-
unsubscribeSubnets: register.gauge<{subnet: number}>({
246+
unsubscribeSubnets: register.gauge<{subnet: SubnetID}>({
246247
name: "lodestar_syncnets_service_unsubscribe_subnets_total",
247248
help: "Count of syncnet unsubscribe_subnets calls",
248249
labelNames: ["subnet"],

0 commit comments

Comments
 (0)