Skip to content

Commit

Permalink
Fix test, refactor invalid proof blobSidecar creation
Browse files Browse the repository at this point in the history
  • Loading branch information
zilm13 committed Oct 23, 2024
1 parent c560084 commit 57e37f2
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
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 java.util.stream.IntStream;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import tech.pegasys.teku.infrastructure.async.SafeFuture;
Expand All @@ -35,7 +35,6 @@
import tech.pegasys.teku.spec.TestSpecFactory;
import tech.pegasys.teku.spec.datastructures.blobs.versions.deneb.BlobSidecar;
import tech.pegasys.teku.spec.datastructures.blocks.SignedBeaconBlock;
import tech.pegasys.teku.spec.schemas.SchemaDefinitionsDeneb;
import tech.pegasys.teku.spec.util.DataStructureUtil;

@SuppressWarnings("JavaCase")
Expand Down Expand Up @@ -91,29 +90,7 @@ void blobSidecarFailsInclusionProofVerification() {
final BlobSidecar blobSidecar1_0 =
dataStructureUtil.randomBlobSidecarWithValidInclusionProofForBlock(
dataStructureUtil.randomSignedBeaconBlock(ONE), 0);
final BlobSidecar blobSidecar1_0_modified =
SchemaDefinitionsDeneb.required(spec.getGenesisSchemaDefinitions())
.getBlobSidecarSchema()
.create(
blobSidecar1_0.getIndex(),
blobSidecar1_0.getBlob(),
blobSidecar1_0.getKZGCommitment(),
blobSidecar1_0.getKZGProof(),
blobSidecar1_0.getSignedBeaconBlockHeader(),
IntStream.range(0, blobSidecar1_0.getKzgCommitmentInclusionProof().size())
.mapToObj(
index -> {
if (index == 0) {
return blobSidecar1_0
.getKzgCommitmentInclusionProof()
.get(index)
.get()
.not();
} else {
return blobSidecar1_0.getKzgCommitmentInclusionProof().get(index).get();
}
})
.toList());
final BlobSidecar blobSidecar1_0_modified = breakInclusionProof(spec, blobSidecar1_0);

final SafeFuture<?> result = listenerWrapper.onResponse(blobSidecar1_0_modified);
assertThat(result).isCompletedExceptionally();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static tech.pegasys.teku.networking.eth2.rpc.beaconchain.methods.BlobSidecarsByRootValidatorTest.breakInclusionProof;

import java.util.List;
import org.junit.jupiter.api.BeforeEach;
Expand Down Expand Up @@ -69,11 +70,16 @@ void blobSidecarsAreCorrect() {
listenerWrapper =
new BlobSidecarsByRootListenerValidatingProxy(peer, spec, listener, kzg, blobIdentifiers);

final BlobSidecar blobSidecar1_0 = dataStructureUtil.randomBlobSidecarForBlock(block1, 0);
final BlobSidecar blobSidecar1_1 = dataStructureUtil.randomBlobSidecarForBlock(block1, 1);
final BlobSidecar blobSidecar2_0 = dataStructureUtil.randomBlobSidecarForBlock(block2, 0);
final BlobSidecar blobSidecar3_0 = dataStructureUtil.randomBlobSidecarForBlock(block3, 0);
final BlobSidecar blobSidecar4_0 = dataStructureUtil.randomBlobSidecarForBlock(block4, 0);
final BlobSidecar blobSidecar1_0 =
dataStructureUtil.randomBlobSidecarWithValidInclusionProofForBlock(block1, 0);
final BlobSidecar blobSidecar1_1 =
dataStructureUtil.randomBlobSidecarWithValidInclusionProofForBlock(block1, 1);
final BlobSidecar blobSidecar2_0 =
dataStructureUtil.randomBlobSidecarWithValidInclusionProofForBlock(block2, 0);
final BlobSidecar blobSidecar3_0 =
dataStructureUtil.randomBlobSidecarWithValidInclusionProofForBlock(block3, 0);
final BlobSidecar blobSidecar4_0 =
dataStructureUtil.randomBlobSidecarWithValidInclusionProofForBlock(block4, 0);

assertDoesNotThrow(() -> listenerWrapper.onResponse(blobSidecar1_0).join());
assertDoesNotThrow(() -> listenerWrapper.onResponse(blobSidecar1_1).join());
Expand All @@ -93,9 +99,12 @@ void blobSidecarIdentifierNotRequested() {
listenerWrapper =
new BlobSidecarsByRootListenerValidatingProxy(peer, spec, listener, kzg, blobIdentifiers);

final BlobSidecar blobSidecar1_0 = dataStructureUtil.randomBlobSidecarForBlock(block1, 0);
final BlobSidecar blobSidecar1_1 = dataStructureUtil.randomBlobSidecarForBlock(block1, 1);
final BlobSidecar blobSidecar2_0 = dataStructureUtil.randomBlobSidecarForBlock(block2, 0);
final BlobSidecar blobSidecar1_0 =
dataStructureUtil.randomBlobSidecarWithValidInclusionProofForBlock(block1, 0);
final BlobSidecar blobSidecar1_1 =
dataStructureUtil.randomBlobSidecarWithValidInclusionProofForBlock(block1, 1);
final BlobSidecar blobSidecar2_0 =
dataStructureUtil.randomBlobSidecarWithValidInclusionProofForBlock(block2, 0);

assertDoesNotThrow(() -> listenerWrapper.onResponse(blobSidecar1_0).join());
assertDoesNotThrow(() -> listenerWrapper.onResponse(blobSidecar1_1).join());
Expand All @@ -119,7 +128,8 @@ void blobSidecarFailsKzgVerification() {
new BlobSidecarsByRootListenerValidatingProxy(
peer, spec, listener, kzg, List.of(blobIdentifier));

final BlobSidecar blobSidecar1_0 = dataStructureUtil.randomBlobSidecarForBlock(block1, 0);
final BlobSidecar blobSidecar1_0 =
dataStructureUtil.randomBlobSidecarWithValidInclusionProofForBlock(block1, 0);

final SafeFuture<?> result = listenerWrapper.onResponse(blobSidecar1_0);
assertThat(result).isCompletedExceptionally();
Expand All @@ -131,4 +141,27 @@ void blobSidecarFailsKzgVerification() {
.BLOB_SIDECAR_KZG_VERIFICATION_FAILED
.describe());
}

@Test
void blobSidecarFailsInclusionProofVerification() {
final SignedBeaconBlock block1 = dataStructureUtil.randomSignedBeaconBlock(UInt64.ONE);
final BlobIdentifier blobIdentifier = new BlobIdentifier(block1.getRoot(), UInt64.ZERO);
listenerWrapper =
new BlobSidecarsByRootListenerValidatingProxy(
peer, spec, listener, kzg, List.of(blobIdentifier));

final BlobSidecar blobSidecar1_0 =
dataStructureUtil.randomBlobSidecarWithValidInclusionProofForBlock(block1, 0);
final BlobSidecar blobSidecar1_0_modified = breakInclusionProof(spec, blobSidecar1_0);

final SafeFuture<?> result = listenerWrapper.onResponse(blobSidecar1_0_modified);
assertThat(result).isCompletedExceptionally();
assertThatThrownBy(result::get)
.hasCauseExactlyInstanceOf(BlobSidecarsResponseInvalidResponseException.class);
assertThatThrownBy(result::get)
.hasMessageContaining(
BlobSidecarsResponseInvalidResponseException.InvalidResponseType
.BLOB_SIDECAR_INCLUSION_PROOF_VERIFICATION_FAILED
.describe());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,33 @@ void setUp() {
when(kzg.verifyBlobKzgProof(any(), any(), any())).thenReturn(true);
}

public static BlobSidecar breakInclusionProof(final Spec spec, final BlobSidecar blobSidecar) {
final BlobSidecar blobSidecarModified =
SchemaDefinitionsDeneb.required(spec.getGenesisSchemaDefinitions())
.getBlobSidecarSchema()
.create(
blobSidecar.getIndex(),
blobSidecar.getBlob(),
blobSidecar.getKZGCommitment(),
blobSidecar.getKZGProof(),
blobSidecar.getSignedBeaconBlockHeader(),
IntStream.range(0, blobSidecar.getKzgCommitmentInclusionProof().size())
.mapToObj(
index -> {
if (index == 0) {
return blobSidecar
.getKzgCommitmentInclusionProof()
.get(index)
.get()
.not();
} else {
return blobSidecar.getKzgCommitmentInclusionProof().get(index).get();
}
})
.toList());
return blobSidecarModified;
}

@Test
void blobSidecarIsCorrect() {
final SignedBeaconBlock block1 = dataStructureUtil.randomSignedBeaconBlock(UInt64.ONE);
Expand Down Expand Up @@ -98,29 +125,7 @@ void blobSidecarFailsInclusionProofVerification() {
final BlobIdentifier blobIdentifier1_0 = new BlobIdentifier(block1.getRoot(), UInt64.ZERO);
final BlobSidecar blobSidecar1_0 =
dataStructureUtil.randomBlobSidecarWithValidInclusionProofForBlock(block1, 0);
final BlobSidecar blobSidecar1_0_modified =
SchemaDefinitionsDeneb.required(spec.getGenesisSchemaDefinitions())
.getBlobSidecarSchema()
.create(
blobSidecar1_0.getIndex(),
blobSidecar1_0.getBlob(),
blobSidecar1_0.getKZGCommitment(),
blobSidecar1_0.getKZGProof(),
blobSidecar1_0.getSignedBeaconBlockHeader(),
IntStream.range(0, blobSidecar1_0.getKzgCommitmentInclusionProof().size())
.mapToObj(
index -> {
if (index == 0) {
return blobSidecar1_0
.getKzgCommitmentInclusionProof()
.get(index)
.get()
.not();
} else {
return blobSidecar1_0.getKzgCommitmentInclusionProof().get(index).get();
}
})
.toList());
final BlobSidecar blobSidecar1_0_modified = breakInclusionProof(spec, blobSidecar1_0);

validator = new BlobSidecarsByRootValidator(peer, spec, kzg, List.of(blobIdentifier1_0));
assertThatThrownBy(() -> validator.validate(blobSidecar1_0_modified))
Expand Down

0 comments on commit 57e37f2

Please sign in to comment.