From 2096d51c5d64bf42e67358385275df41a3917d3f Mon Sep 17 00:00:00 2001 From: Paul Harris Date: Sun, 8 Sep 2024 07:48:06 +1000 Subject: [PATCH] so it turns out the test showed a problem... fixed in this commit but i cant easily do deserializable type for blockWrapper in this way... Signed-off-by: Paul Harris --- .../validator/api/signer/BlockWrapper.java | 39 ++++++++++--------- .../ExternalSignerAltairIntegrationTest.java | 31 +++++++++++++++ 2 files changed, 51 insertions(+), 19 deletions(-) diff --git a/validator/api/src/main/java/tech/pegasys/teku/validator/api/signer/BlockWrapper.java b/validator/api/src/main/java/tech/pegasys/teku/validator/api/signer/BlockWrapper.java index 412cae0c73c..85b3c2272e2 100644 --- a/validator/api/src/main/java/tech/pegasys/teku/validator/api/signer/BlockWrapper.java +++ b/validator/api/src/main/java/tech/pegasys/teku/validator/api/signer/BlockWrapper.java @@ -16,34 +16,35 @@ import static tech.pegasys.teku.infrastructure.json.types.DeserializableTypeDefinition.enumOf; import java.util.Optional; -import tech.pegasys.teku.infrastructure.json.types.DeserializableTypeDefinition; +import tech.pegasys.teku.infrastructure.json.types.SerializableTypeDefinition; import tech.pegasys.teku.spec.SpecMilestone; import tech.pegasys.teku.spec.datastructures.blocks.BeaconBlock; import tech.pegasys.teku.spec.datastructures.blocks.BeaconBlockHeader; -import tech.pegasys.teku.spec.schemas.SchemaDefinitions; public record BlockWrapper( SpecMilestone milestone, Optional block, Optional blockHeader) { - public static DeserializableTypeDefinition getJsonTypeDefinition( - final SchemaDefinitions schemaDefinitions) { - return DeserializableTypeDefinition.object(BlockWrapper.class, Builder.class) - .initializer(Builder::new) - .finisher(Builder::build) - .withField( - "version", enumOf(SpecMilestone.class), BlockWrapper::milestone, Builder::milestone) - .withOptionalField( - SignType.BLOCK.getName(), - schemaDefinitions.getBeaconBlockSchema().getJsonTypeDefinition(), - BlockWrapper::block, - Builder::block) - .withOptionalField( - "block_header", - BeaconBlockHeader.SSZ_SCHEMA.getJsonTypeDefinition(), - BlockWrapper::blockHeader, - Builder::blockHeader) + public SerializableTypeDefinition getJsonTypeDefinition() { + return SerializableTypeDefinition.object(BlockWrapper.class) + .withField("version", enumOf(SpecMilestone.class), BlockWrapper::milestone) + .withOptionalField(SignType.BLOCK.getName(), getMaybeBlockSchema(), BlockWrapper::block) + .withOptionalField("block_header", getMaybeBlockHeaderSchema(), BlockWrapper::blockHeader) .build(); } + private SerializableTypeDefinition getMaybeBlockSchema() { + return block + .>map( + beaconBlock -> beaconBlock.getSchema().getJsonTypeDefinition()) + .orElse(null); + } + + private SerializableTypeDefinition getMaybeBlockHeaderSchema() { + return blockHeader + .>map( + beaconBlockHeader -> beaconBlockHeader.getSchema().getJsonTypeDefinition()) + .orElse(null); + } + static class Builder { private SpecMilestone milestone; private Optional block = Optional.empty(); diff --git a/validator/client/src/integration-test/java/tech/pegasys/teku/validator/client/signer/ExternalSignerAltairIntegrationTest.java b/validator/client/src/integration-test/java/tech/pegasys/teku/validator/client/signer/ExternalSignerAltairIntegrationTest.java index 49cc40c0d4a..c84aa1543ea 100644 --- a/validator/client/src/integration-test/java/tech/pegasys/teku/validator/client/signer/ExternalSignerAltairIntegrationTest.java +++ b/validator/client/src/integration-test/java/tech/pegasys/teku/validator/client/signer/ExternalSignerAltairIntegrationTest.java @@ -31,6 +31,7 @@ import tech.pegasys.teku.infrastructure.unsigned.UInt64; import tech.pegasys.teku.spec.Spec; import tech.pegasys.teku.spec.TestSpecFactory; +import tech.pegasys.teku.spec.datastructures.blocks.BeaconBlock; import tech.pegasys.teku.spec.datastructures.operations.versions.altair.ContributionAndProof; import tech.pegasys.teku.spec.datastructures.operations.versions.altair.SyncAggregatorSelectionData; import tech.pegasys.teku.spec.datastructures.operations.versions.altair.SyncCommitteeContribution; @@ -62,6 +63,36 @@ public Spec getSpec() { return TestSpecFactory.createMinimalAltair(); } + @Test + void shouldSignAltairBlock() throws Exception { + final BeaconBlock block = dataStructureUtil.randomBeaconBlock(10); + final BLSSignature expectedSignature = + BLSSignature.fromBytesCompressed( + Bytes.fromBase64String( + "luIZGEgsjSbFo4MEPVeqaqqm1AnnTODcxFy9gPmdAywVmDIpqkzYed8DJ2l4zx5WAejUTox+NO5HQ4M2APMNovd7FuqnCSVUEftrL4WtJqegPrING2ZCtVTrcaUzFpUQ")); + client.when(request()).respond(response().withBody(expectedSignature.toString())); + + final BLSSignature response = externalSigner.signBlock(block, forkInfo).join(); + assertThat(response).isEqualTo(expectedSignature); + + final ExternalSignerBlockRequestProvider externalSignerBlockRequestProvider = + new ExternalSignerBlockRequestProvider(spec, block); + + final SigningRequestBody signingRequestBody = + new SigningRequestBody( + signingRootUtil.signingRootForSignBlock(block, forkInfo), + externalSignerBlockRequestProvider.getSignType(), + externalSignerBlockRequestProvider.getBlockMetadata(Map.of("fork_info", forkInfo))); + + verifySignRequest( + client, + KEYPAIR.getPublicKey().toString(), + signingRequestBody, + getSpec().getGenesisSchemaDefinitions()); + + validateMetrics(metricsSystem, 1, 0, 0); + } + @Test public void shouldSignSyncCommitteeMessage() throws Exception { final Bytes expectedSigningRoot =