Skip to content

Commit

Permalink
BeaconBlock to BeaconBlockPhase0
Browse files Browse the repository at this point in the history
improvement in BeaconBlock* constructors
  • Loading branch information
tbenr committed Oct 29, 2021
1 parent f687286 commit c8a6093
Show file tree
Hide file tree
Showing 13 changed files with 86 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@
import org.mockito.ArgumentCaptor;
import tech.pegasys.teku.api.SyncDataProvider;
import tech.pegasys.teku.api.ValidatorDataProvider;
import tech.pegasys.teku.api.schema.BeaconBlock;
import tech.pegasys.teku.api.schema.SignedBeaconBlock;
import tech.pegasys.teku.api.schema.ValidatorBlockResult;
import tech.pegasys.teku.api.schema.phase0.BeaconBlockPhase0;
import tech.pegasys.teku.infrastructure.async.SafeFuture;
import tech.pegasys.teku.provider.JsonProvider;
import tech.pegasys.teku.spec.Spec;
Expand Down Expand Up @@ -78,7 +78,7 @@ void shouldReturnBadRequestIfArgumentNotJSON() throws Exception {
@Test
void shouldReturnBadRequestIfArgumentNotSignedBeaconBlock() throws Exception {
final String notASignedBlock =
jsonProvider.objectToJSON(new BeaconBlock(dataStructureUtil.randomBeaconBlock(3)));
jsonProvider.objectToJSON(new BeaconBlockPhase0(dataStructureUtil.randomBeaconBlock(3)));

when(syncDataProvider.isSyncing()).thenReturn(false);
when(context.body()).thenReturn(notASignedBlock);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import tech.pegasys.teku.api.response.v1.validator.GetNewBlockResponse;
import tech.pegasys.teku.api.schema.BLSSignature;
import tech.pegasys.teku.api.schema.BeaconBlock;
import tech.pegasys.teku.api.schema.phase0.BeaconBlockPhase0;
import tech.pegasys.teku.beaconrestapi.schema.BadRequest;
import tech.pegasys.teku.bls.BLSTestUtil;
import tech.pegasys.teku.infrastructure.async.SafeFuture;
Expand Down Expand Up @@ -80,7 +81,8 @@ void shouldReturnBlockWithoutGraffiti() throws Exception {
Map.of(RANDAO_REVEAL, List.of(signature.toHexString()));
Optional<BeaconBlock> optionalBeaconBlock =
Optional.of(
new BeaconBlock(dataStructureUtil.randomBeaconBlock(dataStructureUtil.randomLong())));
new BeaconBlockPhase0(
dataStructureUtil.randomBeaconBlock(dataStructureUtil.randomLong())));
when(context.queryParamMap()).thenReturn(queryParams);
when(context.pathParamMap()).thenReturn(pathParams);
when(provider.getMilestoneAtSlot(UInt64.ONE)).thenReturn(SpecMilestone.PHASE0);
Expand All @@ -105,7 +107,8 @@ void shouldReturnBlockWithGraffiti() throws Exception {
List.of(graffiti.toHexString()));
Optional<BeaconBlock> optionalBeaconBlock =
Optional.of(
new BeaconBlock(dataStructureUtil.randomBeaconBlock(dataStructureUtil.randomLong())));
new BeaconBlockPhase0(
dataStructureUtil.randomBeaconBlock(dataStructureUtil.randomLong())));
when(context.queryParamMap()).thenReturn(params);
when(context.pathParamMap()).thenReturn(Map.of(SLOT, "1"));
when(provider.getMilestoneAtSlot(UInt64.ONE)).thenReturn(SpecMilestone.PHASE0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
import tech.pegasys.teku.api.schema.BeaconBlock;
import tech.pegasys.teku.api.schema.SignedBeaconBlock.SignedBeaconBlockAltair;
import tech.pegasys.teku.api.schema.ValidatorBlockResult;
import tech.pegasys.teku.api.schema.phase0.BeaconBlockPhase0;
import tech.pegasys.teku.bls.BLSTestUtil;
import tech.pegasys.teku.infrastructure.async.SafeFuture;
import tech.pegasys.teku.infrastructure.async.SafeFutureAssert;
Expand Down Expand Up @@ -84,7 +85,7 @@ public class ValidatorDataProviderTest {
new ValidatorDataProvider(spec, validatorApiChannel, combinedChainDataClient);
private final tech.pegasys.teku.spec.datastructures.blocks.BeaconBlock blockInternal =
dataStructureUtil.randomBeaconBlock(123);
private final BeaconBlock block = new BeaconBlock(blockInternal);
private final BeaconBlock block = new BeaconBlockPhase0(blockInternal);
private final tech.pegasys.teku.bls.BLSSignature signatureInternal =
BLSTestUtil.randomSignature(1234);
private final BLSSignature signature = new BLSSignature(signatureInternal);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,13 @@

package tech.pegasys.teku.api.response.v1.validator;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import tech.pegasys.teku.api.schema.BeaconBlock;
import tech.pegasys.teku.api.schema.interfaces.UnsignedBlock;

public class GetNewBlockResponse {
public final UnsignedBlock data;

@JsonCreator
public GetNewBlockResponse(@JsonProperty("data") final BeaconBlock data) {
public GetNewBlockResponse(final BeaconBlock data) {
this.data = data;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ public class BeaconBlock implements UnsignedBlock {
@Schema(type = "string", format = "byte", description = DESCRIPTION_BYTES32)
public final Bytes32 stateRoot;

private final BeaconBlockBody body;
protected final BeaconBlockBody body;

public BeaconBlockBody getBody() {
return body;
}

public BeaconBlock(tech.pegasys.teku.spec.datastructures.blocks.BeaconBlock message) {
protected BeaconBlock(tech.pegasys.teku.spec.datastructures.blocks.BeaconBlock message) {
this.slot = message.getSlot();
this.proposerIndex = message.getProposerIndex();
this.parentRoot = message.getParentRoot();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import tech.pegasys.teku.api.schema.altair.BeaconBlockAltair;
import tech.pegasys.teku.api.schema.interfaces.SignedBlock;
import tech.pegasys.teku.api.schema.merge.BeaconBlockMerge;
import tech.pegasys.teku.api.schema.phase0.BeaconBlockPhase0;
import tech.pegasys.teku.spec.Spec;

@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, property = "type")
Expand Down Expand Up @@ -123,17 +124,17 @@ private SignedBeaconBlockAltair(
}
}

public static class SignedBeaconBlockPhase0 extends SignedBeaconBlock<BeaconBlock> {
public static class SignedBeaconBlockPhase0 extends SignedBeaconBlock<BeaconBlockPhase0> {
@JsonCreator
private SignedBeaconBlockPhase0(
@JsonProperty("message") final BeaconBlock message,
@JsonProperty("message") final BeaconBlockPhase0 message,
@JsonProperty("signature") final BLSSignature signature) {
super(message, signature);
}

private SignedBeaconBlockPhase0(
tech.pegasys.teku.spec.datastructures.blocks.SignedBeaconBlock internalBlock) {
super(internalBlock, new BeaconBlock(internalBlock.getMessage()));
super(internalBlock, new BeaconBlockPhase0(internalBlock.getMessage()));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,14 @@
import tech.pegasys.teku.spec.schemas.SchemaDefinitionsAltair;

public class BeaconBlockAltair extends BeaconBlock implements UnsignedBlock {
private final BeaconBlockBodyAltair body;

public BeaconBlockAltair(tech.pegasys.teku.spec.datastructures.blocks.BeaconBlock message) {
super(message);
this.body = new BeaconBlockBodyAltair(message.getBody().toVersionAltair().orElseThrow());
super(
message.getSlot(),
message.getProposerIndex(),
message.getParentRoot(),
message.getStateRoot(),
new BeaconBlockBodyAltair(message.getBody().toVersionAltair().orElseThrow()));
}

@Override
Expand All @@ -48,7 +51,7 @@ public tech.pegasys.teku.spec.datastructures.blocks.BeaconBlock asInternalBeacon
@JsonProperty("body")
@Override
public BeaconBlockBodyAltair getBody() {
return body;
return (BeaconBlockBodyAltair) body;
}

@JsonCreator
Expand All @@ -59,6 +62,5 @@ public BeaconBlockAltair(
@JsonProperty("state_root") final Bytes32 state_root,
@JsonProperty("body") final BeaconBlockBodyAltair body) {
super(slot, proposer_index, parent_root, state_root, body);
this.body = body;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,14 @@
import tech.pegasys.teku.spec.schemas.SchemaDefinitionsMerge;

public class BeaconBlockMerge extends BeaconBlockAltair {
private final BeaconBlockBodyMerge body;

public BeaconBlockMerge(BeaconBlock message) {
super(message);
this.body = new BeaconBlockBodyMerge(message.getBody().toVersionMerge().orElseThrow());
super(
message.getSlot(),
message.getProposerIndex(),
message.getParentRoot(),
message.getStateRoot(),
new BeaconBlockBodyMerge(message.getBody().toVersionMerge().orElseThrow()));
}

@Override
Expand All @@ -47,7 +50,7 @@ public BeaconBlock asInternalBeaconBlock(Spec spec) {
@JsonProperty("body")
@Override
public BeaconBlockBodyMerge getBody() {
return body;
return (BeaconBlockBodyMerge) body;
}

@JsonCreator
Expand All @@ -58,6 +61,5 @@ public BeaconBlockMerge(
@JsonProperty("state_root") final Bytes32 stateRoot,
@JsonProperty("body") final BeaconBlockBodyMerge body) {
super(slot, proposerIndex, parentRoot, stateRoot, body);
this.body = body;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@
import tech.pegasys.teku.infrastructure.unsigned.UInt64;

public class BeaconBlockPhase0 extends BeaconBlock implements UnsignedBlock {
public BeaconBlockPhase0(tech.pegasys.teku.spec.datastructures.blocks.BeaconBlock message) {
super(
message.getSlot(),
message.getProposerIndex(),
message.getParentRoot(),
message.getStateRoot(),
new BeaconBlockBody(message.getBody()));
}

@JsonCreator
public BeaconBlockPhase0(
@JsonProperty("slot") final UInt64 slot,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright 2021 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.provider;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import tech.pegasys.teku.api.response.v1.validator.GetNewBlockResponse;
import tech.pegasys.teku.api.schema.BeaconBlock;
import tech.pegasys.teku.api.schema.phase0.BeaconBlockPhase0;

public class GetNewBlockResponseV1Deserializer extends JsonDeserializer<GetNewBlockResponse> {
private final ObjectMapper mapper;

public GetNewBlockResponseV1Deserializer(final ObjectMapper mapper) {
this.mapper = mapper;
}

@Override
public GetNewBlockResponse deserialize(final JsonParser jp, final DeserializationContext ctxt)
throws IOException {
JsonNode node = jp.getCodec().readTree(jp);
final BeaconBlock block = mapper.treeToValue(node.findValue("data"), BeaconBlockPhase0.class);
return new GetNewBlockResponse(block);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.fasterxml.jackson.databind.module.SimpleModule;
import org.apache.tuweni.bytes.Bytes;
import org.apache.tuweni.bytes.Bytes32;
import tech.pegasys.teku.api.response.v1.validator.GetNewBlockResponse;
import tech.pegasys.teku.api.response.v2.debug.GetStateResponseV2;
import tech.pegasys.teku.api.response.v2.validator.GetNewBlockResponseV2;
import tech.pegasys.teku.api.schema.BLSPubKey;
Expand Down Expand Up @@ -56,6 +57,9 @@ private void addTekuMappers() {
module.addSerializer(byte[].class, new ByteArraySerializer());
module.addDeserializer(byte[].class, new ByteArrayDeserializer());

module.addDeserializer(
GetNewBlockResponse.class, new GetNewBlockResponseV1Deserializer(objectMapper));

module.addDeserializer(
GetNewBlockResponseV2.class, new GetNewBlockResponseV2Deserializer(objectMapper));
module.addDeserializer(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ public void createUnsignedBlock_WhenFound_ReturnsBlock() {
final tech.pegasys.teku.api.schema.BLSSignature schemaBlsSignature =
new tech.pegasys.teku.api.schema.BLSSignature(blsSignature);
final tech.pegasys.teku.api.schema.BeaconBlock schemaBeaconBlock =
new tech.pegasys.teku.api.schema.BeaconBlock(beaconBlock);
new tech.pegasys.teku.api.schema.phase0.BeaconBlockPhase0(beaconBlock);

when(apiClient.createUnsignedBlock(
eq(beaconBlock.getSlot()), refEq(schemaBlsSignature), eq(graffiti)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import tech.pegasys.teku.api.schema.SubnetSubscription;
import tech.pegasys.teku.api.schema.Validator;
import tech.pegasys.teku.api.schema.altair.SyncCommitteeContribution;
import tech.pegasys.teku.api.schema.phase0.BeaconBlockPhase0;
import tech.pegasys.teku.bls.BLSPublicKey;
import tech.pegasys.teku.infrastructure.unsigned.UInt64;
import tech.pegasys.teku.spec.Spec;
Expand Down Expand Up @@ -88,7 +89,7 @@ public ValidatorResponse validatorResponse(final long index, final BLSPublicKey
}

public BeaconBlock beaconBlock() {
return new BeaconBlock(dataStructureUtil.randomBeaconBlock(UInt64.ONE));
return new BeaconBlockPhase0(dataStructureUtil.randomBeaconBlock(UInt64.ONE));
}

public BeaconBlock beaconBlockAltair() {
Expand Down

0 comments on commit c8a6093

Please sign in to comment.