Skip to content

Commit

Permalink
Switch from extra subnet count in discovery to total subnet count lik…
Browse files Browse the repository at this point in the history
…e others do (Consensys#55)
  • Loading branch information
zilm13 authored May 16, 2024
1 parent 0625472 commit 9fd3f7b
Show file tree
Hide file tree
Showing 9 changed files with 26 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public class ActiveEth2P2PNetwork extends DelegatingP2PNetwork<Eth2Peer> impleme
private final SubnetSubscriptionService dataColumnSidecarSubnetService;
private final ProcessedAttestationSubscriptionProvider processedAttestationSubscriptionProvider;
private final AtomicBoolean gossipStarted = new AtomicBoolean(false);
private final int dasExtraCustodySubnetCount;
private final int dasTotalCustodySubnetCount;

private final GossipForkManager gossipForkManager;

Expand All @@ -98,7 +98,7 @@ public ActiveEth2P2PNetwork(
final GossipEncoding gossipEncoding,
final GossipConfigurator gossipConfigurator,
final ProcessedAttestationSubscriptionProvider processedAttestationSubscriptionProvider,
final int dasExtraCustodySubnetCount,
final int dasTotalCustodySubnetCount,
final boolean allTopicsFilterEnabled) {
super(discoveryNetwork);
this.spec = spec;
Expand All @@ -114,7 +114,7 @@ public ActiveEth2P2PNetwork(
this.syncCommitteeSubnetService = syncCommitteeSubnetService;
this.dataColumnSidecarSubnetService = dataColumnSidecarSubnetService;
this.processedAttestationSubscriptionProvider = processedAttestationSubscriptionProvider;
this.dasExtraCustodySubnetCount = dasExtraCustodySubnetCount;
this.dasTotalCustodySubnetCount = dasTotalCustodySubnetCount;
this.allTopicsFilterEnabled = allTopicsFilterEnabled;
}

Expand Down Expand Up @@ -159,10 +159,8 @@ private synchronized void startGossip() {
syncCommitteeSubnetService.subscribeToUpdates(
discoveryNetwork::setSyncCommitteeSubnetSubscriptions);
if (spec.isMilestoneSupported(SpecMilestone.EIP7594)) {
LOG.info("Using extra custody sidecar columns count: {}", dasExtraCustodySubnetCount);
if (dasExtraCustodySubnetCount != 0) {
discoveryNetwork.setDASExtraCustodySubnetCount(dasExtraCustodySubnetCount);
}
LOG.info("Using custody sidecar columns count: {}", dasTotalCustodySubnetCount);
discoveryNetwork.setDASTotalCustodySubnetCount(dasTotalCustodySubnetCount);
}

gossipForkManager.configureGossipForEpoch(recentChainData.getCurrentEpoch().orElseThrow());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,8 @@ public Eth2P2PNetwork build() {

final GossipForkManager gossipForkManager = buildGossipForkManager(gossipEncoding, network);

int dasExtraCustodySubnetCount =
config.getDasExtraCustodySubnetCount(spec.forMilestone(SpecMilestone.EIP7594));
int dasTotalCustodySubnetCount =
config.getTotalCustodySubnetCount(spec.forMilestone(SpecMilestone.EIP7594));

return new ActiveEth2P2PNetwork(
config.getSpec(),
Expand All @@ -214,7 +214,7 @@ public Eth2P2PNetwork build() {
gossipEncoding,
config.getGossipConfigurator(),
processedAttestationSubscriptionProvider,
dasExtraCustodySubnetCount,
dasTotalCustodySubnetCount,
config.isAllTopicsFilterEnabled());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,12 +132,6 @@ public boolean isSubscribeAllSubnetsEnabled() {
return subscribeAllSubnetsEnabled;
}

public int getDasExtraCustodySubnetCount(SpecVersion specVersion) {
SpecConfigEip7594 configEip7594 = SpecConfigEip7594.required(specVersion.getConfig());
int minCustodyRequirement = configEip7594.getCustodyRequirement();
return getTotalCustodySubnetCount(specVersion) - minCustodyRequirement;
}

public int getTotalCustodySubnetCount(SpecVersion specVersion) {
SpecConfigEip7594 configEip7594 = SpecConfigEip7594.required(specVersion.getConfig());
int minCustodyRequirement = configEip7594.getCustodyRequirement();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,20 @@
@FunctionalInterface
public interface NodeIdToDataColumnSidecarSubnetsCalculator {

Optional<SszBitvector> calculateSubnets(UInt256 nodeId, int extraSubnetCount);
Optional<SszBitvector> calculateSubnets(UInt256 nodeId, Optional<Integer> subnetCount);

NodeIdToDataColumnSidecarSubnetsCalculator NOOP = (nodeId, extraSubnetCount) -> Optional.empty();
NodeIdToDataColumnSidecarSubnetsCalculator NOOP = (nodeId, subnetCount) -> Optional.empty();

/** Creates a calculator instance for the specific slot */
private static NodeIdToDataColumnSidecarSubnetsCalculator createAtSlot(
SpecConfigEip7594 config, MiscHelpersEip7594 miscHelpers, UInt64 currentSlot) {
UInt64 currentEpoch = miscHelpers.computeEpochAtSlot(currentSlot);
SszBitvectorSchema<SszBitvector> bitvectorSchema =
SszBitvectorSchema.create(config.getDataColumnSidecarSubnetCount());
return (nodeId, extraSubnetCount) -> {
return (nodeId, subnetCount) -> {
List<UInt64> nodeSubnets =
miscHelpers.computeDataColumnSidecarBackboneSubnets(
nodeId, currentEpoch, config.getCustodyRequirement() + extraSubnetCount);
nodeId, currentEpoch, subnetCount.orElse(config.getCustodyRequirement()));
return Optional.of(
bitvectorSchema.ofBits(nodeSubnets.stream().map(UInt64::intValue).toList()));
};
Expand All @@ -52,7 +52,7 @@ private static NodeIdToDataColumnSidecarSubnetsCalculator createAtSlot(
static NodeIdToDataColumnSidecarSubnetsCalculator create(
Spec spec, Supplier<Optional<UInt64>> currentSlotSupplier) {

return (nodeId, extraSubnetCount) ->
return (nodeId, subnetCount) ->
currentSlotSupplier
.get()
.flatMap(
Expand All @@ -68,7 +68,7 @@ static NodeIdToDataColumnSidecarSubnetsCalculator create(
} else {
calculatorAtSlot = NOOP;
}
return calculatorAtSlot.calculateSubnets(nodeId, extraSubnetCount);
return calculatorAtSlot.calculateSubnets(nodeId, subnetCount);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.OptionalInt;
import java.util.function.Consumer;
import java.util.stream.IntStream;
Expand Down Expand Up @@ -206,9 +207,9 @@ public SszBitvector getDataColumnSidecarSubnetSubscriptions(final NodeId peerId)
}

public SszBitvector getDataColumnSidecarSubnetSubscriptionsByNodeId(
final UInt256 peerId, final int extraSubnetCount) {
final UInt256 peerId, final Optional<Integer> custodySubnetCount) {
return nodeIdToDataColumnSidecarSubnetsCalculator
.calculateSubnets(peerId, extraSubnetCount)
.calculateSubnets(peerId, custodySubnetCount)
.orElse(dataColumnSidecarSubnetSubscriptions.getSubscriptionSchema().getDefault());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public int scoreCandidatePeer(DiscoveryPeer candidate) {
candidate.getPersistentAttestationSubnets(),
candidate.getSyncCommitteeSubnets(),
peerSubnetSubscriptions.getDataColumnSidecarSubnetSubscriptionsByNodeId(
UInt256.fromBytes(candidate.getNodeId()), candidate.getDasExtraCustodySubnetCount()));
UInt256.fromBytes(candidate.getNodeId()), candidate.getDasCustodySubnetCount()));
}

// @Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ public void setSyncCommitteeSubnetSubscriptions(Iterable<Integer> subnetIds) {
.sszSerialize());
}

public void setDASExtraCustodySubnetCount(int count) {
public void setDASTotalCustodySubnetCount(int count) {
discoveryService.updateCustomENRField(
DAS_CUSTODY_SUBNET_COUNT_ENR_FIELD, SszUInt64.of(UInt64.valueOf(count)).sszSerialize());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class DiscoveryPeer {
private final Optional<EnrForkId> enrForkId;
private final SszBitvector persistentAttestationSubnets;
private final SszBitvector syncCommitteeSubnets;
private final int dasExtraCustodySubnetCount;
private final Optional<Integer> dasCustodySubnetCount;

public DiscoveryPeer(
final Bytes publicKey,
Expand All @@ -37,14 +37,14 @@ public DiscoveryPeer(
final Optional<EnrForkId> enrForkId,
final SszBitvector persistentAttestationSubnets,
final SszBitvector syncCommitteeSubnets,
final Optional<Integer> dasExtraCustodySubnetCount) {
final Optional<Integer> dasCustodySubnetCount) {
this.publicKey = publicKey;
this.nodeId = nodeId;
this.nodeAddress = nodeAddress;
this.enrForkId = enrForkId;
this.persistentAttestationSubnets = persistentAttestationSubnets;
this.syncCommitteeSubnets = syncCommitteeSubnets;
this.dasExtraCustodySubnetCount = dasExtraCustodySubnetCount.orElse(0);
this.dasCustodySubnetCount = dasCustodySubnetCount;
}

public Bytes getPublicKey() {
Expand All @@ -71,8 +71,8 @@ public SszBitvector getSyncCommitteeSubnets() {
return syncCommitteeSubnets;
}

public int getDasExtraCustodySubnetCount() {
return dasExtraCustodySubnetCount;
public Optional<Integer> getDasCustodySubnetCount() {
return dasCustodySubnetCount;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ private static DiscoveryPeer socketAddressToDiscoveryPeer(
final SszBitvector syncCommitteeSubnets =
parseField(nodeRecord, SYNC_COMMITTEE_SUBNET_ENR_FIELD, syncnetsSchema::fromBytes)
.orElse(syncnetsSchema.getDefault());
final Optional<Integer> dasExtraCustodySubnetCount =
final Optional<Integer> dasTotalCustodySubnetCount =
parseField(
nodeRecord,
DAS_CUSTODY_SUBNET_COUNT_ENR_FIELD,
Expand All @@ -89,7 +89,7 @@ private static DiscoveryPeer socketAddressToDiscoveryPeer(
enrForkId,
persistentAttestationSubnets,
syncCommitteeSubnets,
dasExtraCustodySubnetCount);
dasTotalCustodySubnetCount);
}

private static <T> Optional<T> parseField(
Expand Down

0 comments on commit 9fd3f7b

Please sign in to comment.