Skip to content

Commit

Permalink
add blinded blocks and block contents types
Browse files Browse the repository at this point in the history
  • Loading branch information
mehdi-aouadi committed May 16, 2023
1 parent 28bbc6a commit a56139c
Show file tree
Hide file tree
Showing 9 changed files with 149 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,16 @@
import tech.pegasys.teku.infrastructure.json.types.DeserializableOneOfTypeDefinition;
import tech.pegasys.teku.spec.Spec;
import tech.pegasys.teku.spec.TestSpecFactory;
import tech.pegasys.teku.spec.datastructures.blobs.versions.deneb.BlindedBlobSidecarSchema;
import tech.pegasys.teku.spec.datastructures.blobs.versions.deneb.BlobSchema;
import tech.pegasys.teku.spec.datastructures.blobs.versions.deneb.BlobSidecarSchema;
import tech.pegasys.teku.spec.datastructures.blobs.versions.deneb.SignedBlindedBlobSidecarSchema;
import tech.pegasys.teku.spec.datastructures.blobs.versions.deneb.SignedBlobSidecarSchema;
import tech.pegasys.teku.spec.datastructures.blocks.BlockContainer;
import tech.pegasys.teku.spec.datastructures.blocks.SignedBeaconBlock;
import tech.pegasys.teku.spec.datastructures.blocks.SignedBlindedBlockContainer;
import tech.pegasys.teku.spec.datastructures.blocks.SignedBlockContainer;
import tech.pegasys.teku.spec.datastructures.blocks.versions.deneb.SignedBlindedBlockContents;
import tech.pegasys.teku.spec.datastructures.blocks.versions.deneb.SignedBlindedBlockContentsSchema;
import tech.pegasys.teku.spec.datastructures.blocks.versions.deneb.SignedBlockContents;
import tech.pegasys.teku.spec.datastructures.blocks.versions.deneb.SignedBlockContentsSchema;
import tech.pegasys.teku.spec.util.DataStructureUtil;
Expand All @@ -34,18 +39,18 @@ public class DeserializeBlocksTest {

static Spec spec = TestSpecFactory.createMinimalDeneb();
DataStructureUtil denebData = new DataStructureUtil(spec);
public static final DeserializableOneOfTypeDefinition<BlockContainer, BlockContainerBuilder>
public static final DeserializableOneOfTypeDefinition<SignedBlockContainer, BlockContainerBuilder>
DESERIALIZABLE_ONE_OF_SIGNED_BEACON_BLOCK_OR_SIGNED_BLOCK_CONTENTS =
DeserializableOneOfTypeDefinition.object(
BlockContainer.class, BlockContainerBuilder.class)
SignedBlockContainer.class, BlockContainerBuilder.class)
.withType(
SignedBeaconBlock.isInstance,
SignedBeaconBlock.isSignedBeaconBlockInstance,
s -> !s.contains("blob_sidecars"),
spec.getGenesisSchemaDefinitions()
.getSignedBeaconBlockSchema()
.getJsonTypeDefinition())
.withType(
SignedBlockContents.isInstance,
SignedBlockContents.isSignedBlockContentsInstance,
s -> s.contains("blob_sidecars"),
SignedBlockContentsSchema.create(
spec.getGenesisSpecConfig().toVersionDeneb().orElseThrow(),
Expand All @@ -57,6 +62,27 @@ public class DeserializeBlocksTest {
.getJsonTypeDefinition())
.build();

public static final DeserializableOneOfTypeDefinition<
SignedBlindedBlockContainer, BlockContainerBuilder>
DESERIALIZABLE_ONE_OF_SIGNED_BLINDED_BEACON_BLOCK_OR_SIGNED_BLINDED_BLOCK_CONTENTS =
DeserializableOneOfTypeDefinition.object(
SignedBlindedBlockContainer.class, BlockContainerBuilder.class)
.withType(
SignedBeaconBlock.isSignedBlindedBeaconBlockInstance,
s -> !s.contains("blob_sidecars"),
spec.getGenesisSchemaDefinitions()
.getSignedBeaconBlockSchema()
.getJsonTypeDefinition())
.withType(
SignedBlindedBlockContents.isSignedBlindedBlockContentsInstance,
s -> s.contains("blob_sidecars"),
SignedBlindedBlockContentsSchema.create(
spec.getGenesisSpecConfig().toVersionDeneb().orElseThrow(),
SignedBlindedBlobSidecarSchema.create(BlindedBlobSidecarSchema.create()),
spec.getGenesisSchemaDefinitions().getSignedBeaconBlockSchema())
.getJsonTypeDefinition())
.build();

@Test
void shouldDeserializeSignedBeaconBlock() throws JsonProcessingException {

Expand All @@ -67,7 +93,7 @@ void shouldDeserializeSignedBeaconBlock() throws JsonProcessingException {
randomSignedBeaconBlock,
DESERIALIZABLE_ONE_OF_SIGNED_BEACON_BLOCK_OR_SIGNED_BLOCK_CONTENTS);

final BlockContainer result =
final SignedBlockContainer result =
JsonUtil.parse(
serializedSignedBeaconBlock,
DESERIALIZABLE_ONE_OF_SIGNED_BEACON_BLOCK_OR_SIGNED_BLOCK_CONTENTS);
Expand All @@ -88,7 +114,7 @@ void shouldDeserializeSignedBlockContents() throws JsonProcessingException {
randomSignedBlockContents,
DESERIALIZABLE_ONE_OF_SIGNED_BEACON_BLOCK_OR_SIGNED_BLOCK_CONTENTS);

final BlockContainer result =
final SignedBlockContainer result =
JsonUtil.parse(
serializedSignedBlockContents,
DESERIALIZABLE_ONE_OF_SIGNED_BEACON_BLOCK_OR_SIGNED_BLOCK_CONTENTS);
Expand All @@ -100,5 +126,50 @@ void shouldDeserializeSignedBlockContents() throws JsonProcessingException {
.hasSize(spec.getMaxBlobsPerBlock().orElseThrow());
}

@Test
void shouldDeserializeSignedBlindedBeaconBlock() throws JsonProcessingException {

SignedBeaconBlock randomBlindedBeaconBlock = denebData.randomSignedBeaconBlock();

String serializedSignedBeaconBlock =
JsonUtil.serialize(
randomBlindedBeaconBlock,
DESERIALIZABLE_ONE_OF_SIGNED_BLINDED_BEACON_BLOCK_OR_SIGNED_BLINDED_BLOCK_CONTENTS);

final SignedBlindedBlockContainer result =
JsonUtil.parse(
serializedSignedBeaconBlock,
DESERIALIZABLE_ONE_OF_SIGNED_BLINDED_BEACON_BLOCK_OR_SIGNED_BLINDED_BLOCK_CONTENTS);

assertThat(result).isInstanceOf(SignedBeaconBlock.class);

assertThat(result.getSignedBeaconBlock()).isPresent();
assertThat(result.getSignedBlindedBlobSidecars()).isEmpty();
}

@Test
void shouldDeserializeBlindedBlockContents() throws JsonProcessingException {

SignedBlindedBlockContents randomSignedBlindedBlockContents =
denebData.randomSignedBlindedBlockContents();

String serializedSignedBlindedBlockContents =
JsonUtil.serialize(
randomSignedBlindedBlockContents,
DESERIALIZABLE_ONE_OF_SIGNED_BLINDED_BEACON_BLOCK_OR_SIGNED_BLINDED_BLOCK_CONTENTS);

final SignedBlindedBlockContainer result =
JsonUtil.parse(
serializedSignedBlindedBlockContents,
DESERIALIZABLE_ONE_OF_SIGNED_BLINDED_BEACON_BLOCK_OR_SIGNED_BLINDED_BLOCK_CONTENTS);

assertThat(result).isInstanceOf(SignedBlindedBlockContents.class);

assertThat(result.getSignedBeaconBlock()).isPresent();
assertThat(result.getSignedBlindedBlobSidecars()).isPresent();
assertThat(result.getSignedBlindedBlobSidecars().get())
.hasSize(spec.getMaxBlobsPerBlock().orElseThrow());
}

private static class BlockContainerBuilder {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,11 @@

package tech.pegasys.teku.spec.datastructures.blocks;

import java.util.List;
import java.util.Optional;
import tech.pegasys.teku.spec.datastructures.blobs.versions.deneb.SignedBlobSidecar;

public interface BlockContainer {

default Optional<SignedBeaconBlock> getSignedBeaconBlock() {
return Optional.empty();
}

default Optional<List<SignedBlobSidecar>> getSignedBlobSidecars() {
return Optional.empty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
import tech.pegasys.teku.spec.schemas.SchemaDefinitions;

public class SignedBeaconBlock extends Container2<SignedBeaconBlock, BeaconBlock, SszSignature>
implements BeaconBlockSummary, BlockContainer {
implements BeaconBlockSummary, SignedBlockContainer, SignedBlindedBlockContainer {

SignedBeaconBlock(SignedBeaconBlockSchema type, TreeNode backingNode) {
super(type, backingNode);
Expand Down Expand Up @@ -187,6 +187,9 @@ public Bytes32 getRoot() {
return getMessage().hashTreeRoot();
}

public static Predicate<BlockContainer> isInstance =
public static Predicate<SignedBlockContainer> isSignedBeaconBlockInstance =
signedBeaconBlock -> signedBeaconBlock instanceof SignedBeaconBlock;

public static Predicate<SignedBlindedBlockContainer> isSignedBlindedBeaconBlockInstance =
signedBlindedBeaconBlock -> signedBlindedBeaconBlock instanceof SignedBeaconBlock;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright ConsenSys Software Inc., 2023
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/

package tech.pegasys.teku.spec.datastructures.blocks;

import java.util.List;
import java.util.Optional;
import tech.pegasys.teku.spec.datastructures.blobs.versions.deneb.SignedBlindedBlobSidecar;

public interface SignedBlindedBlockContainer extends BlockContainer {

default Optional<List<SignedBlindedBlobSidecar>> getSignedBlindedBlobSidecars() {
return Optional.empty();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright ConsenSys Software Inc., 2023
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/

package tech.pegasys.teku.spec.datastructures.blocks;

import java.util.List;
import java.util.Optional;
import tech.pegasys.teku.spec.datastructures.blobs.versions.deneb.SignedBlobSidecar;

public interface SignedBlockContainer extends BlockContainer {

default Optional<List<SignedBlobSidecar>> getSignedBlobSidecars() {
return Optional.empty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,18 @@

import java.util.List;
import java.util.Optional;
import java.util.function.Predicate;
import tech.pegasys.teku.infrastructure.ssz.SszList;
import tech.pegasys.teku.infrastructure.ssz.containers.Container2;
import tech.pegasys.teku.infrastructure.ssz.tree.TreeNode;
import tech.pegasys.teku.spec.datastructures.blobs.versions.deneb.SignedBlindedBlobSidecar;
import tech.pegasys.teku.spec.datastructures.blocks.BlockContainer;
import tech.pegasys.teku.spec.datastructures.blocks.SignedBeaconBlock;
import tech.pegasys.teku.spec.datastructures.blocks.SignedBlindedBlockContainer;

public class SignedBlindedBlockContents
extends Container2<
SignedBlindedBlockContents, SignedBeaconBlock, SszList<SignedBlindedBlobSidecar>>
implements BlockContainer {
implements SignedBlindedBlockContainer {

SignedBlindedBlockContents(
final SignedBlindedBlockContentsSchema type, final TreeNode backingNode) {
Expand All @@ -47,7 +48,12 @@ public Optional<SignedBeaconBlock> getSignedBeaconBlock() {
return Optional.of(getField0());
}

public List<SignedBlindedBlobSidecar> getSignedBlindedBlobSidecars() {
return getField1().asList();
@Override
public Optional<List<SignedBlindedBlobSidecar>> getSignedBlindedBlobSidecars() {
return Optional.of(getField1().asList());
}

public static Predicate<SignedBlindedBlockContainer> isSignedBlindedBlockContentsInstance =
signedBlindedBlockContents ->
signedBlindedBlockContents instanceof SignedBlindedBlockContents;
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ public class SignedBlindedBlockContentsSchema

public static SignedBlindedBlockContentsSchema create(
final SpecConfigDeneb specConfig,
final SignedBeaconBlockSchema signedBeaconBlockSchema,
final SignedBlindedBlobSidecarSchema signedBlindedBlobSidecarSchema) {
final SignedBlindedBlobSidecarSchema signedBlindedBlobSidecarSchema,
final SignedBeaconBlockSchema signedBeaconBlockSchema) {
return new SignedBlindedBlockContentsSchema(
specConfig, signedBeaconBlockSchema, signedBlindedBlobSidecarSchema);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@
import tech.pegasys.teku.infrastructure.ssz.containers.Container2;
import tech.pegasys.teku.infrastructure.ssz.tree.TreeNode;
import tech.pegasys.teku.spec.datastructures.blobs.versions.deneb.SignedBlobSidecar;
import tech.pegasys.teku.spec.datastructures.blocks.BlockContainer;
import tech.pegasys.teku.spec.datastructures.blocks.SignedBeaconBlock;
import tech.pegasys.teku.spec.datastructures.blocks.SignedBlockContainer;

public class SignedBlockContents
extends Container2<SignedBlockContents, SignedBeaconBlock, SszList<SignedBlobSidecar>>
implements BlockContainer {
implements SignedBlockContainer {

SignedBlockContents(final SignedBlockContentsSchema type, final TreeNode backingNode) {
super(type, backingNode);
Expand All @@ -51,6 +51,6 @@ public Optional<List<SignedBlobSidecar>> getSignedBlobSidecars() {
return Optional.of(getField1().asList());
}

public static Predicate<BlockContainer> isInstance =
public static Predicate<SignedBlockContainer> isSignedBlockContentsInstance =
signedBlockContent -> signedBlockContent instanceof SignedBlockContents;
}
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public SchemaDefinitionsDeneb(final SpecConfigDeneb specConfig) {
BlindedBlockContentsSchema.create(specConfig, blindedBlobSidecarSchema, beaconBlockSchema);
this.signedBlindedBlockContentsSchema =
SignedBlindedBlockContentsSchema.create(
specConfig, signedBeaconBlockSchema, signedBlindedBlobSidecarSchema);
specConfig, signedBlindedBlobSidecarSchema, signedBeaconBlockSchema);
}

public static SchemaDefinitionsDeneb required(final SchemaDefinitions schemaDefinitions) {
Expand Down

0 comments on commit a56139c

Please sign in to comment.