Skip to content

Commit

Permalink
so it turns out the test showed a problem... fixed in this commit but…
Browse files Browse the repository at this point in the history
… i cant easily do deserializable type for blockWrapper in this way...

Signed-off-by: Paul Harris <paul.harris@consensys.net>
  • Loading branch information
rolfyone committed Sep 7, 2024
1 parent 85ae056 commit 2096d51
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<BeaconBlock> block, Optional<BeaconBlockHeader> blockHeader) {
public static DeserializableTypeDefinition<BlockWrapper> 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<BlockWrapper> 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<BeaconBlock> getMaybeBlockSchema() {
return block
.<SerializableTypeDefinition<BeaconBlock>>map(
beaconBlock -> beaconBlock.getSchema().getJsonTypeDefinition())
.orElse(null);
}

private SerializableTypeDefinition<BeaconBlockHeader> getMaybeBlockHeaderSchema() {
return blockHeader
.<SerializableTypeDefinition<BeaconBlockHeader>>map(
beaconBlockHeader -> beaconBlockHeader.getSchema().getJsonTypeDefinition())
.orElse(null);
}

static class Builder {
private SpecMilestone milestone;
private Optional<BeaconBlock> block = Optional.empty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 =
Expand Down

0 comments on commit 2096d51

Please sign in to comment.