Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

8767 update blobs gossip #8822

Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -960,7 +960,7 @@ public Optional<Integer> getMaxBlobsPerBlock(final UInt64 slot) {
public UInt64 computeSubnetForBlobSidecar(final BlobSidecar blobSidecar) {
return blobSidecar
.getIndex()
.mod(atSlot(blobSidecar.getSlot()).miscHelpers().getBlobSidecarSubnetCount());
.mod(atSlot(blobSidecar.getSlot()).miscHelpers().getBlobSidecarSubnetCount().orElseThrow());
}

public Optional<UInt64> computeFirstSlotWithBlobSupport() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -396,8 +396,8 @@ public int getMaxBlobsPerBlock() {
throw new UnsupportedOperationException("No Blob Sidecars before Deneb");
}

public int getBlobSidecarSubnetCount() {
throw new UnsupportedOperationException("No Blob Sidecars before Deneb");
public Optional<Integer> getBlobSidecarSubnetCount() {
return Optional.empty();
}

public int getBlobKzgCommitmentsCount(final SignedBeaconBlock signedBeaconBlock) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,8 +222,8 @@ public int getMaxBlobsPerBlock() {
}

@Override
public int getBlobSidecarSubnetCount() {
return SpecConfigDeneb.required(specConfig).getBlobSidecarSubnetCount();
public Optional<Integer> getBlobSidecarSubnetCount() {
return Optional.of(SpecConfigDeneb.required(specConfig).getBlobSidecarSubnetCount());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public int getMaxBlobsPerBlock() {
}

@Override
public int getBlobSidecarSubnetCount() {
return SpecConfigElectra.required(specConfig).getBlobSidecarSubnetCountElectra();
public Optional<Integer> getBlobSidecarSubnetCount() {
return Optional.of(SpecConfigElectra.required(specConfig).getBlobSidecarSubnetCountElectra());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
import tech.pegasys.teku.networking.p2p.gossip.GossipNetwork;
import tech.pegasys.teku.networking.p2p.gossip.TopicChannel;
import tech.pegasys.teku.spec.Spec;
import tech.pegasys.teku.spec.SpecMilestone;
import tech.pegasys.teku.spec.SpecVersion;
import tech.pegasys.teku.spec.config.SpecConfigDeneb;
import tech.pegasys.teku.spec.datastructures.blobs.versions.deneb.BlobSidecar;
import tech.pegasys.teku.spec.datastructures.blobs.versions.deneb.BlobSidecarSchema;
import tech.pegasys.teku.spec.datastructures.state.ForkInfo;
Expand Down Expand Up @@ -68,8 +68,11 @@ public static BlobSidecarGossipManager create(
.getBlobSidecarSchema();
final Int2ObjectMap<Eth2TopicHandler<BlobSidecar>> subnetIdToTopicHandler =
new Int2ObjectOpenHashMap<>();
final SpecConfigDeneb specConfigDeneb = SpecConfigDeneb.required(forkSpecVersion.getConfig());
IntStream.range(0, specConfigDeneb.getBlobSidecarSubnetCount())
final SpecMilestone specMilestone = spec.atEpoch(forkInfo.getFork().getEpoch()).getMilestone();
final int blobSidecarSubnetCount =
spec.forMilestone(specMilestone).miscHelpers().getBlobSidecarSubnetCount().orElseThrow();

IntStream.range(0, blobSidecarSubnetCount)
.forEach(
subnetId -> {
final Eth2TopicHandler<BlobSidecar> topicHandler =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import tech.pegasys.teku.networking.eth2.gossip.encoding.GossipEncoding;
import tech.pegasys.teku.networking.p2p.libp2p.gossip.GossipTopicFilter;
import tech.pegasys.teku.spec.Spec;
import tech.pegasys.teku.spec.SpecMilestone;
import tech.pegasys.teku.spec.datastructures.state.ForkInfo;
import tech.pegasys.teku.storage.client.RecentChainData;

Expand Down Expand Up @@ -52,7 +53,9 @@ private Set<String> computeRelevantTopics(
final RecentChainData recentChainData, final GossipEncoding gossipEncoding) {
final ForkInfo forkInfo = recentChainData.getCurrentForkInfo().orElseThrow();
final Bytes4 forkDigest = forkInfo.getForkDigest(spec);
final Set<String> topics = getAllTopics(gossipEncoding, forkDigest, spec);
final SpecMilestone specMilestone =
recentChainData.getMilestoneByForkDigest(forkDigest).orElseThrow();
final Set<String> topics = getAllTopics(gossipEncoding, forkDigest, spec, specMilestone);
spec.getForkSchedule().getForks().stream()
.filter(fork -> fork.getEpoch().isGreaterThanOrEqualTo(forkInfo.getFork().getEpoch()))
.forEach(
Expand All @@ -62,7 +65,7 @@ private Set<String> computeRelevantTopics(
.miscHelpers()
.computeForkDigest(
futureFork.getCurrentVersion(), forkInfo.getGenesisValidatorsRoot());
topics.addAll(getAllTopics(gossipEncoding, futureForkDigest, spec));
topics.addAll(getAllTopics(gossipEncoding, futureForkDigest, spec, specMilestone));
});
return topics;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import tech.pegasys.teku.infrastructure.bytes.Bytes4;
import tech.pegasys.teku.networking.eth2.gossip.encoding.GossipEncoding;
import tech.pegasys.teku.spec.Spec;
import tech.pegasys.teku.spec.SpecMilestone;
import tech.pegasys.teku.spec.constants.NetworkConstants;

/**
Expand Down Expand Up @@ -64,7 +65,10 @@ public static String getBlobSidecarSubnetTopic(
}

public static Set<String> getAllTopics(
final GossipEncoding gossipEncoding, final Bytes4 forkDigest, final Spec spec) {
final GossipEncoding gossipEncoding,
final Bytes4 forkDigest,
final Spec spec,
final SpecMilestone specMilestone) {
final Set<String> topics = new HashSet<>();

for (int i = 0; i < spec.getNetworkingConfig().getAttestationSubnetCount(); i++) {
Expand All @@ -73,11 +77,13 @@ public static Set<String> getAllTopics(
for (int i = 0; i < NetworkConstants.SYNC_COMMITTEE_SUBNET_COUNT; i++) {
topics.add(getSyncCommitteeSubnetTopic(forkDigest, i, gossipEncoding));
}
if (spec.getNetworkingConfigDeneb().isPresent()) {
for (int i = 0; i < spec.getNetworkingConfigDeneb().get().getBlobSidecarSubnetCount(); i++) {
topics.add(getBlobSidecarSubnetTopic(forkDigest, i, gossipEncoding));
}
}

spec.forMilestone(specMilestone)
.miscHelpers()
.getBlobSidecarSubnetCount()
.ifPresent(
integer -> addBlobSidecarSubnetTopics(integer, topics, forkDigest, gossipEncoding));

for (GossipTopicName topicName : GossipTopicName.values()) {
topics.add(GossipTopics.getTopic(forkDigest, topicName, gossipEncoding));
}
Expand All @@ -98,4 +104,14 @@ public static Bytes4 extractForkDigest(final String topic) throws IllegalArgumen

return Bytes4.fromHexString(forkDigest);
}

private static void addBlobSidecarSubnetTopics(
final int blobSidecarSubnetCount,
final Set<String> topics,
final Bytes4 forkDigest,
final GossipEncoding gossipEncoding) {
for (int i = 0; i < blobSidecarSubnetCount; i++) {
topics.add(getBlobSidecarSubnetTopic(forkDigest, i, gossipEncoding));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import org.apache.tuweni.bytes.Bytes;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestTemplate;
import tech.pegasys.teku.infrastructure.async.SafeFuture;
import tech.pegasys.teku.infrastructure.async.StubAsyncRunner;
import tech.pegasys.teku.infrastructure.unsigned.UInt64;
Expand All @@ -41,9 +41,10 @@
import tech.pegasys.teku.networking.p2p.gossip.TopicChannel;
import tech.pegasys.teku.spec.Spec;
import tech.pegasys.teku.spec.SpecMilestone;
import tech.pegasys.teku.spec.TestSpecFactory;
import tech.pegasys.teku.spec.config.SpecConfig;
import tech.pegasys.teku.spec.TestSpecContext;
import tech.pegasys.teku.spec.TestSpecInvocationContextProvider;
import tech.pegasys.teku.spec.config.SpecConfigDeneb;
import tech.pegasys.teku.spec.config.SpecConfigElectra;
import tech.pegasys.teku.spec.datastructures.blobs.versions.deneb.BlobSidecar;
import tech.pegasys.teku.spec.datastructures.state.ForkInfo;
import tech.pegasys.teku.spec.util.DataStructureUtil;
Expand All @@ -52,31 +53,30 @@
import tech.pegasys.teku.storage.storageSystem.InMemoryStorageSystemBuilder;
import tech.pegasys.teku.storage.storageSystem.StorageSystem;

@TestSpecContext(milestone = {SpecMilestone.DENEB, SpecMilestone.ELECTRA})
public class BlobSidecarGossipManagerTest {

private static final Pattern BLOB_SIDECAR_TOPIC_PATTERN = Pattern.compile("blob_sidecar_(\\d+)");

private final Spec spec = TestSpecFactory.createMainnetDeneb();
private final StorageSystem storageSystem = InMemoryStorageSystemBuilder.buildDefault(spec);
private final DataStructureUtil dataStructureUtil = new DataStructureUtil(spec);

@SuppressWarnings("unchecked")
private final OperationProcessor<BlobSidecar> processor = mock(OperationProcessor.class);

private final GossipNetwork gossipNetwork = mock(GossipNetwork.class);
private final GossipEncoding gossipEncoding = GossipEncoding.SSZ_SNAPPY;

private final Map<Integer, TopicChannel> topicChannels = new HashMap<>();

private final StubAsyncRunner asyncRunner = new StubAsyncRunner();

private final ForkInfo forkInfo =
new ForkInfo(spec.fork(UInt64.ZERO), dataStructureUtil.randomBytes32());

private Spec spec;
private DataStructureUtil dataStructureUtil;
private BlobSidecarGossipManager blobSidecarGossipManager;
private SpecMilestone specMilestone;

@BeforeEach
public void setup() {
public void setup(final TestSpecInvocationContextProvider.SpecContext specContext) {
spec = specContext.getSpec();
dataStructureUtil = specContext.getDataStructureUtil();
specMilestone = specContext.getSpecMilestone();
final StorageSystem storageSystem = InMemoryStorageSystemBuilder.buildDefault(spec);
storageSystem.chainUpdater().initializeGenesis();
// return TopicChannel mock for each blob_sidecar_<subnet_id> topic
doAnswer(
Expand All @@ -96,6 +96,8 @@ public void setup() {
.subscribe(any(), any());
when(processor.process(any(), any()))
.thenReturn(SafeFuture.completedFuture(InternalValidationResult.ACCEPT));
final ForkInfo forkInfo =
new ForkInfo(spec.fork(UInt64.ZERO), dataStructureUtil.randomBytes32());
blobSidecarGossipManager =
BlobSidecarGossipManager.create(
storageSystem.recentChainData(),
Expand All @@ -109,7 +111,7 @@ public void setup() {
blobSidecarGossipManager.subscribe();
}

@Test
@TestTemplate
public void testGossipingBlobSidecarPublishesToCorrectSubnet() {
final BlobSidecar blobSidecar =
dataStructureUtil.createRandomBlobSidecarBuilder().index(UInt64.ONE).build();
Expand All @@ -127,27 +129,26 @@ public void testGossipingBlobSidecarPublishesToCorrectSubnet() {
});
}

@Test
@TestTemplate
public void testGossipingBlobSidecarWithLargeIndexGossipToCorrectSubnet() {
final BlobSidecar blobSidecar =
dataStructureUtil.createRandomBlobSidecarBuilder().index(UInt64.valueOf(10)).build();
final Bytes serialized = gossipEncoding.encode(blobSidecar);

safeJoin(blobSidecarGossipManager.publishBlobSidecar(blobSidecar));
final SpecConfig config = spec.forMilestone(SpecMilestone.DENEB).getConfig();
final SpecConfigDeneb specConfigDeneb = SpecConfigDeneb.required(config);
final int blobSidecarSubnetCount = getBlobSidecarSubnetCount();

topicChannels.forEach(
(subnetId, channel) -> {
if (subnetId == 10 % specConfigDeneb.getBlobSidecarSubnetCount()) {
if (subnetId == 10 % blobSidecarSubnetCount) {
verify(channel).gossip(serialized);
} else {
verifyNoInteractions(channel);
}
});
}

@Test
@TestTemplate
public void testUnsubscribingClosesAllChannels() {
blobSidecarGossipManager.unsubscribe();

Expand All @@ -159,7 +160,7 @@ public void testUnsubscribingClosesAllChannels() {
});
}

@Test
@TestTemplate
public void testAcceptingSidecarGossipIfOnTheCorrectTopic() {
// topic handler for blob sidecars with subnet_id 1
final Eth2TopicHandler<BlobSidecar> topicHandler = blobSidecarGossipManager.getTopicHandler(1);
Expand All @@ -175,7 +176,7 @@ public void testAcceptingSidecarGossipIfOnTheCorrectTopic() {
assertThat(validationResult).isEqualTo(InternalValidationResult.ACCEPT);
}

@Test
@TestTemplate
public void testRejectingSidecarGossipIfNotOnTheCorrectTopic() {
// topic handler for blob sidecars with subnet_id 1
final Eth2TopicHandler<BlobSidecar> topicHandler = blobSidecarGossipManager.getTopicHandler(1);
Expand All @@ -190,4 +191,12 @@ public void testRejectingSidecarGossipIfNotOnTheCorrectTopic() {
assertThat(validationResult.getDescription())
.hasValue("blob sidecar with subnet_id 2 does not match the topic subnet_id 1");
}

private int getBlobSidecarSubnetCount() {
return specMilestone.isGreaterThanOrEqualTo(SpecMilestone.ELECTRA)
? SpecConfigElectra.required(spec.forMilestone(SpecMilestone.ELECTRA).getConfig())
.getBlobSidecarSubnetCountElectra()
: SpecConfigDeneb.required(spec.forMilestone(SpecMilestone.DENEB).getConfig())
.getBlobSidecarSubnetCount();
}
}
Loading