Skip to content

Commit

Permalink
update BlobSidecarsByRangeListenerValidatingProxyTest
Browse files Browse the repository at this point in the history
  • Loading branch information
mehdi-aouadi committed Oct 31, 2024
1 parent 6d78272 commit 7ee4070
Showing 1 changed file with 86 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,53 +20,73 @@
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static tech.pegasys.teku.infrastructure.async.SafeFutureAssert.safeJoin;
import static tech.pegasys.teku.infrastructure.unsigned.UInt64.ONE;
import static tech.pegasys.teku.infrastructure.unsigned.UInt64.ZERO;
import static tech.pegasys.teku.networking.eth2.rpc.beaconchain.methods.BlobSidecarsByRootValidatorTest.breakInclusionProof;

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.unsigned.UInt64;
import tech.pegasys.teku.kzg.KZG;
import tech.pegasys.teku.networking.eth2.peers.Eth2Peer;
import tech.pegasys.teku.networking.p2p.rpc.RpcResponseListener;
import tech.pegasys.teku.spec.Spec;
import tech.pegasys.teku.spec.SpecMilestone;
import tech.pegasys.teku.spec.TestSpecContext;
import tech.pegasys.teku.spec.TestSpecFactory;
import tech.pegasys.teku.spec.TestSpecInvocationContextProvider;
import tech.pegasys.teku.spec.datastructures.blobs.versions.deneb.BlobSidecar;
import tech.pegasys.teku.spec.datastructures.blocks.SignedBeaconBlock;
import tech.pegasys.teku.spec.util.DataStructureUtil;

@SuppressWarnings("JavaCase")
@TestSpecContext(milestone = {SpecMilestone.DENEB, SpecMilestone.ELECTRA})
public class BlobSidecarsByRangeListenerValidatingProxyTest {
private final Spec spec = TestSpecFactory.createMainnetDeneb();
private final DataStructureUtil dataStructureUtil = new DataStructureUtil(spec);

private final UInt64 currentForkEpoch = UInt64.valueOf(1);

private Spec spec;
private DataStructureUtil dataStructureUtil;
private UInt64 currentForkFirstSlot;
private BlobSidecarsByRangeListenerValidatingProxy listenerWrapper;
private Integer maxBlobsPerBlock;
private final Eth2Peer peer = mock(Eth2Peer.class);
private final Integer maxBlobsPerBlock = spec.getMaxBlobsPerBlock().orElseThrow();
private final KZG kzg = mock(KZG.class);

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

@BeforeEach
void setUp() {
void setUp(final TestSpecInvocationContextProvider.SpecContext specContext) {
spec =
switch (specContext.getSpecMilestone()) {
case PHASE0 -> throw new IllegalArgumentException("Phase0 is an unsupported milestone");
case ALTAIR -> throw new IllegalArgumentException("Altair is an unsupported milestone");
case BELLATRIX ->
throw new IllegalArgumentException("Bellatrix is an unsupported milestone");
case CAPELLA -> throw new IllegalArgumentException("Capella is an unsupported milestone");
case DENEB -> TestSpecFactory.createMinimalWithDenebForkEpoch(currentForkEpoch);
case ELECTRA -> TestSpecFactory.createMinimalWithElectraForkEpoch(currentForkEpoch);
};
currentForkFirstSlot = spec.computeStartSlotAtEpoch(currentForkEpoch);
dataStructureUtil = new DataStructureUtil(spec);
maxBlobsPerBlock = spec.getMaxBlobsPerBlock().orElseThrow();

when(listener.onResponse(any())).thenReturn(SafeFuture.completedFuture(null));
when(kzg.verifyBlobKzgProof(any(), any(), any())).thenReturn(true);
}

@Test
@TestTemplate
void blobSidecarFailsKzgVerification() {
when(kzg.verifyBlobKzgProof(any(), any(), any())).thenReturn(false);
final UInt64 startSlot = UInt64.valueOf(1);
final UInt64 startSlot = currentForkFirstSlot;
final UInt64 count = UInt64.valueOf(4);
listenerWrapper =
new BlobSidecarsByRangeListenerValidatingProxy(
spec, peer, listener, maxBlobsPerBlock, kzg, startSlot, count);

final BlobSidecar blobSidecar1_0 =
dataStructureUtil.randomBlobSidecarWithValidInclusionProofForBlock(
dataStructureUtil.randomSignedBeaconBlock(ONE), 0);
dataStructureUtil.randomSignedBeaconBlock(currentForkFirstSlot), 0);

final SafeFuture<?> result = listenerWrapper.onResponse(blobSidecar1_0);
assertThat(result).isCompletedExceptionally();
Expand All @@ -79,17 +99,17 @@ void blobSidecarFailsKzgVerification() {
.describe());
}

@Test
@TestTemplate
void blobSidecarFailsInclusionProofVerification() {
final UInt64 startSlot = UInt64.valueOf(1);
final UInt64 startSlot = currentForkFirstSlot;
final UInt64 count = UInt64.valueOf(4);
listenerWrapper =
new BlobSidecarsByRangeListenerValidatingProxy(
spec, peer, listener, maxBlobsPerBlock, kzg, startSlot, count);

final BlobSidecar blobSidecar1_0 =
dataStructureUtil.randomBlobSidecarWithValidInclusionProofForBlock(
dataStructureUtil.randomSignedBeaconBlock(ONE), 0);
dataStructureUtil.randomSignedBeaconBlock(currentForkFirstSlot), 0);
final BlobSidecar blobSidecar1_0_modified = breakInclusionProof(blobSidecar1_0);

final SafeFuture<?> result = listenerWrapper.onResponse(blobSidecar1_0_modified);
Expand All @@ -103,17 +123,17 @@ void blobSidecarFailsInclusionProofVerification() {
.describe());
}

@Test
@TestTemplate
void blobSidecarSlotSmallerThanFromSlot() {
final UInt64 startSlot = UInt64.valueOf(1);
final UInt64 startSlot = currentForkFirstSlot.plus(1);
final UInt64 count = UInt64.valueOf(4);
listenerWrapper =
new BlobSidecarsByRangeListenerValidatingProxy(
spec, peer, listener, maxBlobsPerBlock, kzg, startSlot, count);

final BlobSidecar blobSidecar0_0 =
dataStructureUtil.randomBlobSidecarWithValidInclusionProofForBlock(
dataStructureUtil.randomSignedBeaconBlock(ZERO), 0);
dataStructureUtil.randomSignedBeaconBlock(currentForkFirstSlot), 0);

final SafeFuture<?> result = listenerWrapper.onResponse(blobSidecar0_0);
assertThat(result).isCompletedExceptionally();
Expand All @@ -126,28 +146,35 @@ void blobSidecarSlotSmallerThanFromSlot() {
.describe());
}

@Test
@TestTemplate
void blobSidecarsSlotsAreCorrect() {
final UInt64 startSlot = UInt64.valueOf(1);
final UInt64 startSlot = currentForkFirstSlot;
final UInt64 count = UInt64.valueOf(4);
listenerWrapper =
new BlobSidecarsByRangeListenerValidatingProxy(
spec, peer, listener, maxBlobsPerBlock, kzg, startSlot, count);

final SignedBeaconBlock block1 = dataStructureUtil.randomSignedBeaconBlock(ONE);
final SignedBeaconBlock block1 =
dataStructureUtil.randomSignedBeaconBlock(currentForkFirstSlot);
final BlobSidecar blobSidecar1_0 =
dataStructureUtil.randomBlobSidecarWithValidInclusionProofForBlock(block1, 0);
final BlobSidecar blobSidecar1_1 =
dataStructureUtil.randomBlobSidecarWithValidInclusionProofForBlock(block1, 1);
final BlobSidecar blobSidecar2_0 =
dataStructureUtil.randomBlobSidecarWithValidInclusionProofForBlock(
dataStructureUtil.randomSignedBeaconBlock(2, block1.getRoot()), 0);
dataStructureUtil.randomSignedBeaconBlock(
currentForkFirstSlot.plus(1).longValue(), block1.getRoot()),
0);
final BlobSidecar blobSidecar3_0 =
dataStructureUtil.randomBlobSidecarWithValidInclusionProofForBlock(
dataStructureUtil.randomSignedBeaconBlock(3, blobSidecar2_0.getBlockRoot()), 0);
dataStructureUtil.randomSignedBeaconBlock(
currentForkFirstSlot.plus(2).longValue(), blobSidecar2_0.getBlockRoot()),
0);
final BlobSidecar blobSidecar4_0 =
dataStructureUtil.randomBlobSidecarWithValidInclusionProofForBlock(
dataStructureUtil.randomSignedBeaconBlock(4, blobSidecar3_0.getBlockRoot()), 0);
dataStructureUtil.randomSignedBeaconBlock(
currentForkFirstSlot.plus(3).longValue(), blobSidecar3_0.getBlockRoot()),
0);

assertDoesNotThrow(() -> listenerWrapper.onResponse(blobSidecar1_0).join());
assertDoesNotThrow(() -> listenerWrapper.onResponse(blobSidecar1_1).join());
Expand All @@ -156,9 +183,9 @@ void blobSidecarsSlotsAreCorrect() {
assertDoesNotThrow(() -> listenerWrapper.onResponse(blobSidecar4_0).join());
}

@Test
@TestTemplate
void blobSidecarSlotGreaterThanToSlot() {
final UInt64 startSlot = UInt64.valueOf(1);
final UInt64 startSlot = currentForkFirstSlot;
final UInt64 count = UInt64.valueOf(8);
// This requests 8 slots (1, 2, 3, 4, 5, 6, 7, 8) so 9 will be unexpected.
listenerWrapper =
Expand All @@ -167,19 +194,21 @@ void blobSidecarSlotGreaterThanToSlot() {

final BlobSidecar blobSidecar1_0 =
dataStructureUtil.randomBlobSidecarWithValidInclusionProofForBlock(
dataStructureUtil.randomSignedBeaconBlock(1), 0);
dataStructureUtil.randomSignedBeaconBlock(currentForkFirstSlot), 0);
final BlobSidecar blobSidecar3_0 =
dataStructureUtil.randomBlobSidecarWithValidInclusionProofForBlock(
dataStructureUtil.randomSignedBeaconBlock(3), 0);
dataStructureUtil.randomSignedBeaconBlock(currentForkFirstSlot.plus(2)), 0);
final BlobSidecar blobSidecar5_0 =
dataStructureUtil.randomBlobSidecarWithValidInclusionProofForBlock(
dataStructureUtil.randomSignedBeaconBlock(5), 0);
dataStructureUtil.randomSignedBeaconBlock(currentForkFirstSlot.plus(4)), 0);
final BlobSidecar blobSidecar8_0 =
dataStructureUtil.randomBlobSidecarWithValidInclusionProofForBlock(
dataStructureUtil.randomSignedBeaconBlock(8), 0);
dataStructureUtil.randomSignedBeaconBlock(currentForkFirstSlot.plus(7)), 0);
final BlobSidecar blobSidecar9_0 =
dataStructureUtil.randomBlobSidecarWithValidInclusionProofForBlock(
dataStructureUtil.randomSignedBeaconBlock(9, blobSidecar8_0.getBlockRoot()), 0);
dataStructureUtil.randomSignedBeaconBlock(
currentForkFirstSlot.plus(8).longValue(), blobSidecar8_0.getBlockRoot()),
0);

safeJoin(listenerWrapper.onResponse(blobSidecar1_0));
safeJoin(listenerWrapper.onResponse(blobSidecar3_0));
Expand All @@ -197,20 +226,20 @@ void blobSidecarSlotGreaterThanToSlot() {
.describe());
}

@Test
@TestTemplate
void blobSidecarParentRootDoesNotMatch() {
final UInt64 startSlot = UInt64.valueOf(1);
final UInt64 startSlot = currentForkFirstSlot;
final UInt64 count = UInt64.valueOf(4);
listenerWrapper =
new BlobSidecarsByRangeListenerValidatingProxy(
spec, peer, listener, maxBlobsPerBlock, kzg, startSlot, count);

final BlobSidecar blobSidecar1_0 =
dataStructureUtil.randomBlobSidecarWithValidInclusionProofForBlock(
dataStructureUtil.randomSignedBeaconBlock(1), 0);
dataStructureUtil.randomSignedBeaconBlock(currentForkFirstSlot), 0);
final BlobSidecar blobSidecar2_0 =
dataStructureUtil.randomBlobSidecarWithValidInclusionProofForBlock(
dataStructureUtil.randomSignedBeaconBlock(2), 0);
dataStructureUtil.randomSignedBeaconBlock(currentForkFirstSlot.plus(1)), 0);

safeJoin(listenerWrapper.onResponse(blobSidecar1_0));

Expand All @@ -225,17 +254,17 @@ void blobSidecarParentRootDoesNotMatch() {
.describe());
}

@Test
@TestTemplate
void blobSidecarIndexIsGreaterOrEqualThanMaxBlobs() {
final UInt64 startSlot = UInt64.valueOf(1);
final UInt64 startSlot = currentForkFirstSlot;
final UInt64 count = UInt64.valueOf(4);

listenerWrapper =
new BlobSidecarsByRangeListenerValidatingProxy(
spec, peer, listener, maxBlobsPerBlock, kzg, startSlot, count);

final SignedBeaconBlock block1 =
dataStructureUtil.randomSignedBeaconBlockWithCommitments(ONE, 7);
dataStructureUtil.randomSignedBeaconBlockWithCommitments(currentForkFirstSlot, 7);
final BlobSidecar blobSidecar1_0 =
dataStructureUtil.randomBlobSidecarWithValidInclusionProofForBlock(block1, 0);
final BlobSidecar blobSidecar1_1 =
Expand Down Expand Up @@ -269,17 +298,17 @@ void blobSidecarIndexIsGreaterOrEqualThanMaxBlobs() {
.describe());
}

@Test
@TestTemplate
void blobSidecarIndexIsInTheSameBlockButNotNext() {
final UInt64 startSlot = UInt64.valueOf(1);
final UInt64 startSlot = currentForkFirstSlot;
final UInt64 count = UInt64.valueOf(4);

listenerWrapper =
new BlobSidecarsByRangeListenerValidatingProxy(
spec, peer, listener, maxBlobsPerBlock, kzg, startSlot, count);

final SignedBeaconBlock block1 =
dataStructureUtil.randomSignedBeaconBlockWithCommitments(ONE, 3);
dataStructureUtil.randomSignedBeaconBlockWithCommitments(currentForkFirstSlot, 3);
final BlobSidecar blobSidecar1_0 =
dataStructureUtil.randomBlobSidecarWithValidInclusionProofForBlock(block1, 0);
final BlobSidecar blobSidecar1_2 =
Expand All @@ -298,40 +327,41 @@ void blobSidecarIndexIsInTheSameBlockButNotNext() {
.describe());
}

@Test
@TestTemplate
void isFirstBlobSidecarAfterAnEmptyBlobsBlock() {
final UInt64 startSlot = UInt64.valueOf(1);
final UInt64 startSlot = currentForkFirstSlot;
final UInt64 count = UInt64.valueOf(4);

listenerWrapper =
new BlobSidecarsByRangeListenerValidatingProxy(
spec, peer, listener, maxBlobsPerBlock, kzg, startSlot, count);

final SignedBeaconBlock block1 = dataStructureUtil.randomSignedBeaconBlock(ONE);
final SignedBeaconBlock block1 =
dataStructureUtil.randomSignedBeaconBlock(currentForkFirstSlot);
final BlobSidecar blobSidecar1_0 =
dataStructureUtil.randomBlobSidecarWithValidInclusionProofForBlock(block1, 0);
final BlobSidecar blobSidecar1_1 =
dataStructureUtil.randomBlobSidecarWithValidInclusionProofForBlock(block1, 1);
final BlobSidecar blobSidecar3_0 =
dataStructureUtil.randomBlobSidecarWithValidInclusionProofForBlock(
dataStructureUtil.randomSignedBeaconBlock(3), 0);
dataStructureUtil.randomSignedBeaconBlock(currentForkFirstSlot.plus(3)), 0);

safeJoin(listenerWrapper.onResponse(blobSidecar1_0));
safeJoin(listenerWrapper.onResponse(blobSidecar1_1));
safeJoin(listenerWrapper.onResponse(blobSidecar3_0));
}

@Test
@TestTemplate
void firstBlobSidecarIndexIsINotZero() {
final UInt64 startSlot = UInt64.valueOf(1);
final UInt64 startSlot = currentForkFirstSlot;
final UInt64 count = UInt64.valueOf(4);
listenerWrapper =
new BlobSidecarsByRangeListenerValidatingProxy(
spec, peer, listener, maxBlobsPerBlock, kzg, startSlot, count);

final BlobSidecar blobSidecar1_1 =
dataStructureUtil.randomBlobSidecarWithValidInclusionProofForBlock(
dataStructureUtil.randomSignedBeaconBlock(2), 1);
dataStructureUtil.randomSignedBeaconBlock(currentForkFirstSlot.plus(2)), 1);

final SafeFuture<?> result = listenerWrapper.onResponse(blobSidecar1_1);
assertThat(result).isCompletedExceptionally();
Expand All @@ -344,20 +374,22 @@ void firstBlobSidecarIndexIsINotZero() {
.describe());
}

@Test
@TestTemplate
void firstBlobSidecarIndexInNextBlockIsNotZero() {
final UInt64 startSlot = UInt64.valueOf(1);
final UInt64 startSlot = currentForkFirstSlot;
final UInt64 count = UInt64.valueOf(4);
listenerWrapper =
new BlobSidecarsByRangeListenerValidatingProxy(
spec, peer, listener, maxBlobsPerBlock, kzg, startSlot, count);

final BlobSidecar blobSidecar1_0 =
dataStructureUtil.randomBlobSidecarWithValidInclusionProofForBlock(
dataStructureUtil.randomSignedBeaconBlock(1), 0);
dataStructureUtil.randomSignedBeaconBlock(currentForkFirstSlot), 0);
final BlobSidecar blobSidecar2_1 =
dataStructureUtil.randomBlobSidecarWithValidInclusionProofForBlock(
dataStructureUtil.randomSignedBeaconBlock(2, blobSidecar1_0.getBlockRoot(), true), 1);
dataStructureUtil.randomSignedBeaconBlock(
currentForkFirstSlot.plus(1).longValue(), blobSidecar1_0.getBlockRoot(), true),
1);

assertDoesNotThrow(() -> listenerWrapper.onResponse(blobSidecar1_0).join());

Expand All @@ -372,20 +404,20 @@ void firstBlobSidecarIndexInNextBlockIsNotZero() {
.describe());
}

@Test
@TestTemplate
void blobSidecarUnexpectedSlot() {
final UInt64 startSlot = UInt64.valueOf(1);
final UInt64 startSlot = currentForkFirstSlot;
final UInt64 count = UInt64.valueOf(4);
listenerWrapper =
new BlobSidecarsByRangeListenerValidatingProxy(
spec, peer, listener, maxBlobsPerBlock, kzg, startSlot, count);

final BlobSidecar blobSidecar2_0 =
dataStructureUtil.randomBlobSidecarWithValidInclusionProofForBlock(
dataStructureUtil.randomSignedBeaconBlock(2), 0);
dataStructureUtil.randomSignedBeaconBlock(currentForkFirstSlot.plus(1)), 0);
final BlobSidecar blobSidecar1_0 =
dataStructureUtil.randomBlobSidecarWithValidInclusionProofForBlock(
dataStructureUtil.randomSignedBeaconBlock(1), 0);
dataStructureUtil.randomSignedBeaconBlock(currentForkFirstSlot), 0);

safeJoin(listenerWrapper.onResponse(blobSidecar2_0));
final SafeFuture<?> result = listenerWrapper.onResponse(blobSidecar1_0);
Expand Down

0 comments on commit 7ee4070

Please sign in to comment.