Skip to content

Commit

Permalink
schema definitions for Event types that aren't just standard objects (#…
Browse files Browse the repository at this point in the history
…5294)

These were missing from the schema definitions we use from swagger.

Signed-off-by: Paul Harris <[email protected]>

Signed-off-by: Paul Harris <[email protected]>
  • Loading branch information
rolfyone authored Apr 5, 2022
1 parent 9113454 commit c294974
Show file tree
Hide file tree
Showing 15 changed files with 279 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
public class BlockEvent extends Event<BlockEvent.BlockData> {
private static final SerializableTypeDefinition<BlockData> BLOCK_EVENT_TYPE =
SerializableTypeDefinition.object(BlockData.class)
.name("BlockEvent")
.withField("slot", UINT64_TYPE, BlockData::getSlot)
.withField("block", BYTES32_TYPE, BlockData::getBlock)
// TODO #5264
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public class ChainReorgEvent extends Event<ChainReorgEvent.ChainReorgData> {

public static final SerializableTypeDefinition<ChainReorgData> CHAIN_REORG_EVENT_TYPE =
SerializableTypeDefinition.object(ChainReorgData.class)
.name("ChainReorgEvent")
.withField("slot", UINT64_TYPE, ChainReorgData::getSlot)
.withField("depth", UINT64_TYPE, ChainReorgData::getDepth)
.withField("old_head_block", BYTES32_TYPE, ChainReorgData::getOldHeadBlock)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,16 @@
public class FinalizedCheckpointEvent
extends Event<FinalizedCheckpointEvent.FinalizedCheckpointData> {

private static final SerializableTypeDefinition<FinalizedCheckpointData>
FINALIZED_CHECKPOINT_EVENT_TYPE =
SerializableTypeDefinition.object(FinalizedCheckpointData.class)
.withField("block", BYTES32_TYPE, FinalizedCheckpointData::getBlock)
.withField("state", BYTES32_TYPE, FinalizedCheckpointData::getState)
.withField("epoch", UINT64_TYPE, FinalizedCheckpointData::getEpoch)
// TODO #5264
// .withOptionalField("execution_optimistic", BOOLEAN_TYPE,
// FinalizedCheckpointData::getExecutionOptimistic)
.build();
static final SerializableTypeDefinition<FinalizedCheckpointData> FINALIZED_CHECKPOINT_EVENT_TYPE =
SerializableTypeDefinition.object(FinalizedCheckpointData.class)
.name("FinalizedCheckpointEvent")
.withField("block", BYTES32_TYPE, FinalizedCheckpointData::getBlock)
.withField("state", BYTES32_TYPE, FinalizedCheckpointData::getState)
.withField("epoch", UINT64_TYPE, FinalizedCheckpointData::getEpoch)
// TODO #5264
// .withOptionalField("execution_optimistic", BOOLEAN_TYPE,
// FinalizedCheckpointData::getExecutionOptimistic)
.build();

FinalizedCheckpointEvent(
final Bytes32 block,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public class HeadEvent extends Event<HeadEvent.HeadData> {

static final SerializableTypeDefinition<HeadData> HEAD_EVENT_TYPE =
SerializableTypeDefinition.object(HeadData.class)
.name("HeadEvent")
.withField("slot", UINT64_TYPE, HeadData::getSlot)
.withField("block", BYTES32_TYPE, HeadData::getBlock)
.withField("state", BYTES32_TYPE, HeadData::getState)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@

public class SyncStateChangeEvent extends Event<String> {

private static final SerializableTypeDefinition<String> SYNC_STATE_CHANGE_EVENT_TYPE =
static final SerializableTypeDefinition<String> SYNC_STATE_CHANGE_EVENT_TYPE =
SerializableTypeDefinition.object(String.class)
.name("SyncStateChangeEvent")
.withField("sync_state", STRING_TYPE, Function.identity())
.build();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright 2022 ConsenSys AG.
*
* 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.beaconrestapi.handlers.v1.events;

import com.fasterxml.jackson.core.JsonProcessingException;
import java.util.stream.Stream;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import tech.pegasys.teku.infrastructure.json.types.OpenApiTypeDefinition;
import tech.pegasys.teku.infrastructure.restapi.OpenApiTestUtil;
import tech.pegasys.teku.spec.Spec;
import tech.pegasys.teku.spec.TestSpecFactory;
import tech.pegasys.teku.spec.util.DataStructureUtil;

public class EventTest {
private final OpenApiTestUtil<EventTest> util = new OpenApiTestUtil<>(EventTest.class);

@ParameterizedTest(name = "{0}")
@MethodSource("getEventTypes")
void shouldHaveConsistentSchema(final String testName, final OpenApiTypeDefinition apiDefinition)
throws JsonProcessingException {
util.compareToKnownDefinition(apiDefinition);
}

public static Stream<Arguments> getEventTypes() {

final Spec spec = TestSpecFactory.createMinimalPhase0();
final DataStructureUtil dataStructureUtil = new DataStructureUtil(spec);
return Stream.of(
Arguments.of(
"AttestationEvent",
new AttestationEvent(dataStructureUtil.randomAttestation()).getJsonTypeDefinition()),
Arguments.of(
"BlockEvent",
new BlockEvent(dataStructureUtil.randomSignedBeaconBlock(1), false)
.getJsonTypeDefinition()),
Arguments.of("ChainReorgEvent", ChainReorgEvent.CHAIN_REORG_EVENT_TYPE),
Arguments.of(
"FinalizedCheckpointEvent", FinalizedCheckpointEvent.FINALIZED_CHECKPOINT_EVENT_TYPE),
Arguments.of("HeadEvent", HeadEvent.HEAD_EVENT_TYPE),
Arguments.of("SyncStateChangeEvent", SyncStateChangeEvent.SYNC_STATE_CHANGE_EVENT_TYPE),
Arguments.of(
"VoluntaryExitEvent",
new VoluntaryExitEvent(dataStructureUtil.randomSignedVoluntaryExit())
.getJsonTypeDefinition()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"title" : "Attestation",
"type" : "object",
"required" : [ "aggregation_bits", "data", "signature" ],
"properties" : {
"aggregation_bits" : {
"type" : "string",
"pattern" : "^0x[a-fA-F0-9]{2,}$",
"description" : "SSZ hexadecimal",
"format" : "bytes"
},
"data" : {
"$ref" : "#/components/schemas/AttestationData"
},
"signature" : {
"type" : "string",
"pattern" : "^0x[a-fA-F0-9]{2,}$",
"description" : "SSZ hexadecimal",
"format" : "bytes"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"title" : "BlockEvent",
"type" : "object",
"required" : [ "slot", "block" ],
"properties" : {
"slot" : {
"type" : "string",
"description" : "unsigned 64 bit integer",
"example" : "1",
"format" : "uint64"
},
"block" : {
"type" : "string",
"description" : "Bytes32 hexadecimal",
"example" : "0xcf8e0d4e9587369b2301d0790347320302cc0943d5a1884560367e8208d920f2",
"format" : "byte"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{
"title" : "ChainReorgEvent",
"type" : "object",
"required" : [ "slot", "depth", "old_head_block", "new_head_block", "old_head_state", "new_head_state", "epoch" ],
"properties" : {
"slot" : {
"type" : "string",
"description" : "unsigned 64 bit integer",
"example" : "1",
"format" : "uint64"
},
"depth" : {
"type" : "string",
"description" : "unsigned 64 bit integer",
"example" : "1",
"format" : "uint64"
},
"old_head_block" : {
"type" : "string",
"description" : "Bytes32 hexadecimal",
"example" : "0xcf8e0d4e9587369b2301d0790347320302cc0943d5a1884560367e8208d920f2",
"format" : "byte"
},
"new_head_block" : {
"type" : "string",
"description" : "Bytes32 hexadecimal",
"example" : "0xcf8e0d4e9587369b2301d0790347320302cc0943d5a1884560367e8208d920f2",
"format" : "byte"
},
"old_head_state" : {
"type" : "string",
"description" : "Bytes32 hexadecimal",
"example" : "0xcf8e0d4e9587369b2301d0790347320302cc0943d5a1884560367e8208d920f2",
"format" : "byte"
},
"new_head_state" : {
"type" : "string",
"description" : "Bytes32 hexadecimal",
"example" : "0xcf8e0d4e9587369b2301d0790347320302cc0943d5a1884560367e8208d920f2",
"format" : "byte"
},
"epoch" : {
"type" : "string",
"description" : "unsigned 64 bit integer",
"example" : "1",
"format" : "uint64"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"title" : "FinalizedCheckpointEvent",
"type" : "object",
"required" : [ "block", "state", "epoch" ],
"properties" : {
"block" : {
"type" : "string",
"description" : "Bytes32 hexadecimal",
"example" : "0xcf8e0d4e9587369b2301d0790347320302cc0943d5a1884560367e8208d920f2",
"format" : "byte"
},
"state" : {
"type" : "string",
"description" : "Bytes32 hexadecimal",
"example" : "0xcf8e0d4e9587369b2301d0790347320302cc0943d5a1884560367e8208d920f2",
"format" : "byte"
},
"epoch" : {
"type" : "string",
"description" : "unsigned 64 bit integer",
"example" : "1",
"format" : "uint64"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"title" : "HeadEvent",
"type" : "object",
"required" : [ "slot", "block", "state", "epoch_transition", "previous_duty_dependent_root", "current_duty_dependent_root" ],
"properties" : {
"slot" : {
"type" : "string",
"description" : "unsigned 64 bit integer",
"example" : "1",
"format" : "uint64"
},
"block" : {
"type" : "string",
"description" : "Bytes32 hexadecimal",
"example" : "0xcf8e0d4e9587369b2301d0790347320302cc0943d5a1884560367e8208d920f2",
"format" : "byte"
},
"state" : {
"type" : "string",
"description" : "Bytes32 hexadecimal",
"example" : "0xcf8e0d4e9587369b2301d0790347320302cc0943d5a1884560367e8208d920f2",
"format" : "byte"
},
"epoch_transition" : {
"type" : "boolean"
},
"previous_duty_dependent_root" : {
"type" : "string",
"description" : "Bytes32 hexadecimal",
"example" : "0xcf8e0d4e9587369b2301d0790347320302cc0943d5a1884560367e8208d920f2",
"format" : "byte"
},
"current_duty_dependent_root" : {
"type" : "string",
"description" : "Bytes32 hexadecimal",
"example" : "0xcf8e0d4e9587369b2301d0790347320302cc0943d5a1884560367e8208d920f2",
"format" : "byte"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"title" : "SignedVoluntaryExit",
"type" : "object",
"required" : [ "message", "signature" ],
"properties" : {
"message" : {
"$ref" : "#/components/schemas/VoluntaryExit"
},
"signature" : {
"type" : "string",
"pattern" : "^0x[a-fA-F0-9]{2,}$",
"description" : "SSZ hexadecimal",
"format" : "bytes"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"title" : "SyncStateChangeEvent",
"type" : "object",
"required" : [ "sync_state" ],
"properties" : {
"sync_state" : {
"type" : "string"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import org.junit.jupiter.api.Test;

public class EventTypeTest {

@Test
void shouldParseEventTypes() {
List<EventType> topics =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.assertj.core.api.Assertions;
import org.assertj.core.api.Fail;
import tech.pegasys.teku.infrastructure.json.JsonUtil;
import tech.pegasys.teku.infrastructure.json.types.OpenApiTypeDefinition;
import tech.pegasys.teku.infrastructure.restapi.endpoints.RestApiEndpoint;

public class OpenApiTestUtil<TObject> {
Expand Down Expand Up @@ -82,6 +83,29 @@ public void compareToKnownDefinitions(
checkAllExpectedFilesExist(tempDir, path);
}

public void compareToKnownDefinition(final OpenApiTypeDefinition schema)
throws JsonProcessingException {
final String filename = schema.getTypeName().orElseThrow() + ".json";
final String openApiType = JsonUtil.serialize(schema::serializeOpenApiType);
final JsonNode node = mapper.readTree(openApiType);
final String namespace = "schema";
JsonNode expectedNode = null;
try {
expectedNode = loadResourceFile(namespace + "/" + filename);
} catch (Exception e) {
Fail.fail(
String.format(
"Expected to find %s with content %s, does it need to be added?",
namespace + "/" + filename, prettyJson(node)));
}
assertThat(node)
.describedAs(
String.format("Structure of %s -> (%s)", clazz.getName(), namespace + "/" + filename))
.withFailMessage(
String.format("Expected: %s\nbut was: %s", prettyJson(expectedNode), prettyJson(node)))
.isEqualTo(expectedNode);
}

/**
* Test swagger references are all defined within the JsonNode
*
Expand Down

0 comments on commit c294974

Please sign in to comment.