Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add block container #7126

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
"$ref" : "#/components/schemas/BlindedBlockBellatrix"
}, {
"$ref" : "#/components/schemas/BlindedBlockCapella"
}, {
"$ref" : "#/components/schemas/BlindedBlockDeneb"
} ]
},
"version" : {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
"$ref" : "#/components/schemas/BeaconBlockBellatrix"
}, {
"$ref" : "#/components/schemas/BeaconBlockCapella"
}, {
"$ref" : "#/components/schemas/BeaconBlockDeneb"
} ]
},
"version" : {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package tech.pegasys.teku.beaconrestapi.handlers.v1.beacon;

import com.fasterxml.jackson.core.JsonProcessingException;
import java.util.List;
import java.util.Optional;
import java.util.function.BiPredicate;
import java.util.function.Function;
Expand All @@ -38,15 +39,42 @@ SerializableOneOfTypeDefinition<T> getAvailableSchemaDefinitionForAllMilestones(
final String title,
final Function<SchemaDefinitions, Optional<SszSchema<? extends T>>> schemaGetter,
final BiPredicate<T, SpecMilestone> predicate) {
return getAvailableSchemaDefinitions(
schemaDefinitionCache, title, schemaGetter, predicate, List.of(SpecMilestone.values()));
}

public static <T extends SszData>
SerializableOneOfTypeDefinition<T> getAvailableSchemaDefinitionUpToMilestone(
final SchemaDefinitionCache schemaDefinitionCache,
final String title,
final Function<SchemaDefinitions, Optional<SszSchema<? extends T>>> schemaGetter,
final BiPredicate<T, SpecMilestone> predicate,
final SpecMilestone milestone) {
return getAvailableSchemaDefinitions(
schemaDefinitionCache,
title,
schemaGetter,
predicate,
SpecMilestone.getMilestonesUpTo(milestone));
}

private static <T extends SszData>
SerializableOneOfTypeDefinition<T> getAvailableSchemaDefinitions(
final SchemaDefinitionCache schemaDefinitionCache,
final String title,
final Function<SchemaDefinitions, Optional<SszSchema<? extends T>>> schemaGetter,
final BiPredicate<T, SpecMilestone> predicate,
final List<SpecMilestone> milestones) {
final SerializableOneOfTypeDefinitionBuilder<T> builder =
new SerializableOneOfTypeDefinitionBuilder<T>().title(title);
for (SpecMilestone milestone : SpecMilestone.values()) {
for (SpecMilestone milestoneValue : milestones) {
final Optional<SszSchema<? extends T>> schemaDefinition =
schemaGetter.apply(schemaDefinitionCache.getSchemaDefinition(milestone));
schemaGetter.apply(schemaDefinitionCache.getSchemaDefinition(milestoneValue));
schemaDefinition.ifPresent(
sszSchema ->
builder.withType(
value -> predicate.test(value, milestone), sszSchema.getJsonTypeDefinition()));
value -> predicate.test(value, milestoneValue),
sszSchema.getJsonTypeDefinition()));
}
return builder.build();
}
Expand All @@ -61,6 +89,17 @@ SerializableOneOfTypeDefinition<T> getSchemaDefinitionForAllMilestones(
schemaDefinitionCache, title, schemaGetter.andThen(Optional::of), predicate);
}

public static <T extends SszData>
SerializableOneOfTypeDefinition<T> getSchemaDefinitionUpToMilestone(
final SchemaDefinitionCache schemaDefinitionCache,
final String title,
final Function<SchemaDefinitions, SszSchema<? extends T>> schemaGetter,
final BiPredicate<T, SpecMilestone> predicate,
final SpecMilestone milestone) {
return getAvailableSchemaDefinitionUpToMilestone(
schemaDefinitionCache, title, schemaGetter.andThen(Optional::of), predicate, milestone);
}

public static <T extends SszData> DeserializableTypeDefinition<? extends T> slotBasedSelector(
final String json,
final SchemaDefinitionCache schemaDefinitionCache,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
import static tech.pegasys.teku.beaconrestapi.BeaconRestApiTypes.GRAFFITI_PARAMETER;
import static tech.pegasys.teku.beaconrestapi.BeaconRestApiTypes.RANDAO_PARAMETER;
import static tech.pegasys.teku.beaconrestapi.BeaconRestApiTypes.SLOT_PARAMETER;
import static tech.pegasys.teku.beaconrestapi.handlers.v1.beacon.MilestoneDependentTypesUtil.getAvailableSchemaDefinitionForAllMilestones;
import static tech.pegasys.teku.beaconrestapi.handlers.v1.beacon.MilestoneDependentTypesUtil.getSchemaDefinitionForAllMilestones;
import static tech.pegasys.teku.ethereum.json.types.EthereumTypes.MILESTONE_TYPE;
import static tech.pegasys.teku.ethereum.json.types.EthereumTypes.sszResponseType;
import static tech.pegasys.teku.infrastructure.http.HttpStatusCodes.SC_OK;
Expand All @@ -32,6 +30,7 @@
import org.jetbrains.annotations.NotNull;
import tech.pegasys.teku.api.DataProvider;
import tech.pegasys.teku.api.ValidatorDataProvider;
import tech.pegasys.teku.beaconrestapi.handlers.v1.beacon.MilestoneDependentTypesUtil;
import tech.pegasys.teku.bls.BLSSignature;
import tech.pegasys.teku.infrastructure.async.SafeFuture;
import tech.pegasys.teku.infrastructure.json.types.SerializableOneOfTypeDefinition;
Expand All @@ -46,6 +45,7 @@
import tech.pegasys.teku.spec.Spec;
import tech.pegasys.teku.spec.SpecMilestone;
import tech.pegasys.teku.spec.datastructures.blocks.BeaconBlock;
import tech.pegasys.teku.spec.datastructures.blocks.BlockContainer;
import tech.pegasys.teku.spec.datastructures.blocks.versions.deneb.BlindedBlockContents;
import tech.pegasys.teku.spec.schemas.SchemaDefinitionCache;
import tech.pegasys.teku.spec.schemas.SchemaDefinitions;
Expand Down Expand Up @@ -132,10 +132,11 @@ private static Function<SszData, SpecMilestone> getMilestoneSelector(final Spec
};
}

private static SerializableOneOfTypeDefinition<Object> getResponseTypes(
private static SerializableOneOfTypeDefinition<BlockContainer> getResponseTypes(
final SchemaDefinitionCache schemaDefinitionCache) {
final SerializableOneOfTypeDefinitionBuilder<Object> builder =
new SerializableOneOfTypeDefinitionBuilder<>().description("Request successful");
final SerializableOneOfTypeDefinitionBuilder<BlockContainer> builder =
new SerializableOneOfTypeDefinitionBuilder<BlockContainer>()
.description("Request successful");
builder.withType(
value -> value instanceof BeaconBlock, getBeaconBlockResponseType(schemaDefinitionCache));
builder.withType(
Expand All @@ -150,12 +151,13 @@ private static SerializableTypeDefinition<BeaconBlock> getBeaconBlockResponseTyp
.name("GetNewBlindedBlockResponse")
.withField(
"data",
getSchemaDefinitionForAllMilestones(
MilestoneDependentTypesUtil.getSchemaDefinitionUpToMilestone(
schemaDefinitionCache,
"BlindedBlock",
SchemaDefinitions::getBlindedBeaconBlockSchema,
(beaconBlock, milestone) ->
schemaDefinitionCache.milestoneAtSlot(beaconBlock.getSlot()).equals(milestone)),
schemaDefinitionCache.milestoneAtSlot(beaconBlock.getSlot()).equals(milestone),
SpecMilestone.CAPELLA),
Function.identity())
.withField(
"version",
Expand All @@ -170,7 +172,7 @@ private static SerializableTypeDefinition<BeaconBlock> getBeaconBlockResponseTyp
.name("GetNewBlindedBlockContentsResponse")
.withField(
"data",
getAvailableSchemaDefinitionForAllMilestones(
MilestoneDependentTypesUtil.getAvailableSchemaDefinitionForAllMilestones(
schemaDefinitionCache,
"BlindedBlockContents",
schemaDefinitions ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
import static tech.pegasys.teku.beaconrestapi.BeaconRestApiTypes.GRAFFITI_PARAMETER;
import static tech.pegasys.teku.beaconrestapi.BeaconRestApiTypes.RANDAO_PARAMETER;
import static tech.pegasys.teku.beaconrestapi.BeaconRestApiTypes.SLOT_PARAMETER;
import static tech.pegasys.teku.beaconrestapi.handlers.v1.beacon.MilestoneDependentTypesUtil.getAvailableSchemaDefinitionForAllMilestones;
import static tech.pegasys.teku.beaconrestapi.handlers.v1.beacon.MilestoneDependentTypesUtil.getSchemaDefinitionForAllMilestones;
import static tech.pegasys.teku.ethereum.json.types.EthereumTypes.MILESTONE_TYPE;
import static tech.pegasys.teku.ethereum.json.types.EthereumTypes.sszResponseType;
import static tech.pegasys.teku.infrastructure.http.HttpStatusCodes.SC_OK;
Expand All @@ -32,6 +30,7 @@
import org.jetbrains.annotations.NotNull;
import tech.pegasys.teku.api.DataProvider;
import tech.pegasys.teku.api.ValidatorDataProvider;
import tech.pegasys.teku.beaconrestapi.handlers.v1.beacon.MilestoneDependentTypesUtil;
import tech.pegasys.teku.bls.BLSSignature;
import tech.pegasys.teku.infrastructure.async.SafeFuture;
import tech.pegasys.teku.infrastructure.json.types.SerializableOneOfTypeDefinition;
Expand All @@ -46,6 +45,7 @@
import tech.pegasys.teku.spec.Spec;
import tech.pegasys.teku.spec.SpecMilestone;
import tech.pegasys.teku.spec.datastructures.blocks.BeaconBlock;
import tech.pegasys.teku.spec.datastructures.blocks.BlockContainer;
import tech.pegasys.teku.spec.datastructures.blocks.versions.deneb.BlindedBlockContents;
import tech.pegasys.teku.spec.datastructures.blocks.versions.deneb.BlockContents;
import tech.pegasys.teku.spec.schemas.SchemaDefinitionCache;
Expand Down Expand Up @@ -110,10 +110,11 @@ public void handleRequest(RestApiRequest request) throws JsonProcessingException
.orElseThrow(ChainDataUnavailableException::new)));
}

private static SerializableOneOfTypeDefinition<Object> getResponseTypes(
private static SerializableOneOfTypeDefinition<BlockContainer> getResponseTypes(
final SchemaDefinitionCache schemaDefinitionCache) {
final SerializableOneOfTypeDefinitionBuilder<Object> builder =
new SerializableOneOfTypeDefinitionBuilder<>().description("Request successful");
final SerializableOneOfTypeDefinitionBuilder<BlockContainer> builder =
new SerializableOneOfTypeDefinitionBuilder<BlockContainer>()
.description("Request successful");
builder.withType(
value -> value instanceof BeaconBlock, getBeaconBlockResponseType(schemaDefinitionCache));
builder.withType(
Expand All @@ -128,12 +129,13 @@ private static SerializableTypeDefinition<BeaconBlock> getBeaconBlockResponseTyp
.name("ProduceBlockV2Response")
.withField(
"data",
getSchemaDefinitionForAllMilestones(
MilestoneDependentTypesUtil.getSchemaDefinitionUpToMilestone(
schemaDefinitionCache,
"Block",
SchemaDefinitions::getBeaconBlockSchema,
(beaconBlock, milestone) ->
schemaDefinitionCache.milestoneAtSlot(beaconBlock.getSlot()).equals(milestone)),
schemaDefinitionCache.milestoneAtSlot(beaconBlock.getSlot()).equals(milestone),
SpecMilestone.CAPELLA),
Function.identity())
.withField(
"version",
Expand All @@ -148,7 +150,7 @@ private static SerializableTypeDefinition<BlockContents> getBlockContentsRespons
.name("ProduceBlockContentsResponse")
.withField(
"data",
getAvailableSchemaDefinitionForAllMilestones(
MilestoneDependentTypesUtil.getAvailableSchemaDefinitionForAllMilestones(
schemaDefinitionCache,
"BlockContents",
schemaDefinitions ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public static List<SpecMilestone> getAllMilestonesFrom(final SpecMilestone miles
* @param milestone The milestone being inspected
* @return An ordered list of all milestones up to and included the specified milestone
*/
static List<SpecMilestone> getMilestonesUpTo(final SpecMilestone milestone) {
public static List<SpecMilestone> getMilestonesUpTo(final SpecMilestone milestone) {
final List<SpecMilestone> allMilestones = Arrays.asList(SpecMilestone.values());
final int milestoneIndex = allMilestones.indexOf(milestone);
return allMilestones.subList(0, milestoneIndex + 1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

public class BeaconBlock
extends Container5<BeaconBlock, SszUInt64, SszUInt64, SszBytes32, SszBytes32, BeaconBlockBody>
implements BeaconBlockSummary {
implements BeaconBlockSummary, BlockContainer {

BeaconBlock(final BeaconBlockSchema type, TreeNode backingNode) {
super(type, backingNode);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
* 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;

public interface BlockContainer {}
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@
import tech.pegasys.teku.infrastructure.ssz.tree.TreeNode;
import tech.pegasys.teku.spec.datastructures.blobs.versions.deneb.BlindedBlobSidecars;
import tech.pegasys.teku.spec.datastructures.blocks.BeaconBlock;
import tech.pegasys.teku.spec.datastructures.blocks.BlockContainer;

public class BlindedBlockContents
extends Container2<BlindedBlockContents, BeaconBlock, BlindedBlobSidecars> {
extends Container2<BlindedBlockContents, BeaconBlock, BlindedBlobSidecars>
implements BlockContainer {

BlindedBlockContents(final BlindedBlockContentsSchema type, final TreeNode backingNode) {
super(type, backingNode);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
import tech.pegasys.teku.infrastructure.ssz.tree.TreeNode;
import tech.pegasys.teku.spec.datastructures.blobs.versions.deneb.BlobSidecars;
import tech.pegasys.teku.spec.datastructures.blocks.BeaconBlock;
import tech.pegasys.teku.spec.datastructures.blocks.BlockContainer;

public class BlockContents extends Container2<BlockContents, BeaconBlock, BlobSidecars> {
public class BlockContents extends Container2<BlockContents, BeaconBlock, BlobSidecars>
implements BlockContainer {

BlockContents(final BlockContentsSchema type, final TreeNode backingNode) {
super(type, backingNode);
Expand Down