From d7a4b063c36249cc581cb1b29da5a59a115623f4 Mon Sep 17 00:00:00 2001 From: Mehdi AOUADI Date: Tue, 16 May 2023 14:21:47 +0200 Subject: [PATCH] refactor test --- .../v1/beacon/DeserializeBlocksTest.java | 27 ++-- .../bellatrix/ExecutionPayloadBellatrix.java | 12 +- .../bellatrix/ExecutionPayloadCommon.java | 132 ++---------------- .../capella/ExecutionPayloadCapella.java | 12 +- .../teku/api/schema/capella/Withdrawal.java | 64 +-------- .../teku/api/schema/deneb/BlockContainer.java | 16 --- 6 files changed, 29 insertions(+), 234 deletions(-) delete mode 100644 data/serializer/src/main/java/tech/pegasys/teku/api/schema/deneb/BlockContainer.java diff --git a/data/beaconrestapi/src/test/java/tech/pegasys/teku/beaconrestapi/handlers/v1/beacon/DeserializeBlocksTest.java b/data/beaconrestapi/src/test/java/tech/pegasys/teku/beaconrestapi/handlers/v1/beacon/DeserializeBlocksTest.java index 050b71ffe8d..c62966d238e 100644 --- a/data/beaconrestapi/src/test/java/tech/pegasys/teku/beaconrestapi/handlers/v1/beacon/DeserializeBlocksTest.java +++ b/data/beaconrestapi/src/test/java/tech/pegasys/teku/beaconrestapi/handlers/v1/beacon/DeserializeBlocksTest.java @@ -36,24 +36,21 @@ public class DeserializeBlocksTest { static Spec spec = TestSpecFactory.createMinimalDeneb(); - public static final DeserializableOneOfTypeDefinition< - tech.pegasys.teku.spec.datastructures.blocks.BlockContainer, BlockContainerBuilder> + public static final DeserializableOneOfTypeDefinition DESERIALIZABLE_ONE_OF_SIGNED_BEACON_BLOCK_OR_SIGNED_BLOCK_CONTENTS = DeserializableOneOfTypeDefinition.object( - tech.pegasys.teku.spec.datastructures.blocks.BlockContainer.class, - BlockContainerBuilder.class) + BlockContainer.class, BlockContainerBuilder.class) .description( "Submit a signed beacon block to the beacon node to be imported." + " The beacon node performs the required validation.") .withType( - tech.pegasys.teku.spec.datastructures.blocks.SignedBeaconBlock.isInstance, + SignedBeaconBlock.isInstance, s -> !s.contains("blob_sidecars"), spec.getGenesisSchemaDefinitions() .getSignedBeaconBlockSchema() .getJsonTypeDefinition()) .withType( - tech.pegasys.teku.spec.datastructures.blocks.versions.deneb.SignedBlockContents - .isInstance, + SignedBlockContents.isInstance, s -> s.contains("blob_sidecars"), SignedBlockContentsSchema.create( spec.getGenesisSpecConfig().toVersionDeneb().orElseThrow(), @@ -67,17 +64,15 @@ public class DeserializeBlocksTest { @Test void shouldDeserializeSignedBlockContents() throws JsonProcessingException { - final tech.pegasys.teku.spec.datastructures.blocks.BlockContainer result = + final BlockContainer result = JsonUtil.parse( readResource("json/signed_block_contents.json"), DESERIALIZABLE_ONE_OF_SIGNED_BEACON_BLOCK_OR_SIGNED_BLOCK_CONTENTS); assertThat(result).isInstanceOf(SignedBlockContents.class); - SignedBlockContents signedBlockContents = (SignedBlockContents) result; - - assertThat(signedBlockContents.getSignedBeaconBlock()).isPresent(); - assertThat(signedBlockContents.getSignedBlobSidecars()).isPresent(); - assertThat(signedBlockContents.getSignedBlobSidecars().get()) + assertThat(result.getSignedBeaconBlock()).isPresent(); + assertThat(result.getSignedBlobSidecars()).isPresent(); + assertThat(result.getSignedBlobSidecars().get()) .hasSize(spec.getMaxBlobsPerBlock().orElseThrow()); } @@ -90,10 +85,8 @@ void shouldDeserializeSignedBeaconBlock() throws JsonProcessingException { assertThat(result).isInstanceOf(SignedBeaconBlock.class); - SignedBeaconBlock signedBeaconBlock = (SignedBeaconBlock) result; - - assertThat(signedBeaconBlock.getSignedBeaconBlock()).isPresent(); - assertThat(signedBeaconBlock.getSignedBlobSidecars()).isEmpty(); + assertThat(result.getSignedBeaconBlock()).isPresent(); + assertThat(result.getSignedBlobSidecars()).isEmpty(); } protected String readResource(final String resource) { diff --git a/data/serializer/src/main/java/tech/pegasys/teku/api/schema/bellatrix/ExecutionPayloadBellatrix.java b/data/serializer/src/main/java/tech/pegasys/teku/api/schema/bellatrix/ExecutionPayloadBellatrix.java index 2b50ac86da1..e2e6833f99b 100644 --- a/data/serializer/src/main/java/tech/pegasys/teku/api/schema/bellatrix/ExecutionPayloadBellatrix.java +++ b/data/serializer/src/main/java/tech/pegasys/teku/api/schema/bellatrix/ExecutionPayloadBellatrix.java @@ -39,7 +39,7 @@ public class ExecutionPayloadBellatrix extends ExecutionPayloadCommon implements ExecutionPayload { @ArraySchema(schema = @Schema(type = "string", format = "byte")) - public List transactions; + public final List transactions; @JsonCreator public ExecutionPayloadBellatrix( @@ -96,8 +96,6 @@ public ExecutionPayloadBellatrix( .collect(Collectors.toList()); } - public ExecutionPayloadBellatrix() {} - public tech.pegasys.teku.spec.datastructures.execution.ExecutionPayload asInternalExecutionPayload(final Spec spec, final UInt64 slot) { return asInternalExecutionPayload(spec.atSlot(slot)); @@ -139,14 +137,6 @@ protected ExecutionPayloadBuilder applyToBuilder( .transactions(transactions); } - public List getTransactions() { - return transactions; - } - - public void setTransactions(List transactions) { - this.transactions = transactions; - } - @Override public Optional toVersionBellatrix() { return Optional.of(this); diff --git a/data/serializer/src/main/java/tech/pegasys/teku/api/schema/bellatrix/ExecutionPayloadCommon.java b/data/serializer/src/main/java/tech/pegasys/teku/api/schema/bellatrix/ExecutionPayloadCommon.java index 878091f3fa7..68932590dc6 100644 --- a/data/serializer/src/main/java/tech/pegasys/teku/api/schema/bellatrix/ExecutionPayloadCommon.java +++ b/data/serializer/src/main/java/tech/pegasys/teku/api/schema/bellatrix/ExecutionPayloadCommon.java @@ -36,7 +36,7 @@ public abstract class ExecutionPayloadCommon { format = "byte", pattern = PATTERN_BYTES32, description = DESCRIPTION_BYTES32) - public Bytes32 parentHash; + public final Bytes32 parentHash; @JsonProperty("fee_recipient") @Schema( @@ -44,7 +44,7 @@ public abstract class ExecutionPayloadCommon { format = "byte", pattern = PATTERN_BYTES20, description = DESCRIPTION_BYTES20) - public Bytes20 feeRecipient; + public final Bytes20 feeRecipient; @JsonProperty("state_root") @Schema( @@ -52,7 +52,7 @@ public abstract class ExecutionPayloadCommon { format = "byte", pattern = PATTERN_BYTES32, description = DESCRIPTION_BYTES32) - public Bytes32 stateRoot; + public final Bytes32 stateRoot; @JsonProperty("receipts_root") @Schema( @@ -60,11 +60,11 @@ public abstract class ExecutionPayloadCommon { format = "byte", pattern = PATTERN_BYTES32, description = DESCRIPTION_BYTES32) - public Bytes32 receiptsRoot; + public final Bytes32 receiptsRoot; @JsonProperty("logs_bloom") @Schema(type = "string", format = "byte") - public Bytes logsBloom; + public final Bytes logsBloom; @JsonProperty("prev_randao") @Schema( @@ -72,30 +72,30 @@ public abstract class ExecutionPayloadCommon { format = "byte", pattern = PATTERN_BYTES32, description = DESCRIPTION_BYTES32) - public Bytes32 prevRandao; + public final Bytes32 prevRandao; @JsonProperty("block_number") @Schema(type = "string", format = "uint64") - public UInt64 blockNumber; + public final UInt64 blockNumber; @JsonProperty("gas_limit") @Schema(type = "string", format = "uint64") - public UInt64 gasLimit; + public final UInt64 gasLimit; @JsonProperty("gas_used") @Schema(type = "string", format = "uint64") - public UInt64 gasUsed; + public final UInt64 gasUsed; @Schema(type = "string", format = "uint64") - public UInt64 timestamp; + public final UInt64 timestamp; @JsonProperty("extra_data") @Schema(type = "string", format = "byte") - public Bytes extraData; + public final Bytes extraData; @JsonProperty("base_fee_per_gas") @Schema(type = "string", format = "uint256") - public UInt256 baseFeePerGas; + public final UInt256 baseFeePerGas; @JsonProperty("block_hash") @Schema( @@ -103,9 +103,7 @@ public abstract class ExecutionPayloadCommon { format = "byte", pattern = PATTERN_BYTES32, description = DESCRIPTION_BYTES32) - public Bytes32 blockHash; - - public ExecutionPayloadCommon() {} + public final Bytes32 blockHash; public ExecutionPayloadCommon( @JsonProperty("parent_hash") Bytes32 parentHash, @@ -136,110 +134,6 @@ public ExecutionPayloadCommon( this.blockHash = blockHash; } - public Bytes32 getParentHash() { - return parentHash; - } - - public void setParentHash(Bytes32 parentHash) { - this.parentHash = parentHash; - } - - public Bytes20 getFeeRecipient() { - return feeRecipient; - } - - public void setFeeRecipient(Bytes20 feeRecipient) { - this.feeRecipient = feeRecipient; - } - - public Bytes32 getStateRoot() { - return stateRoot; - } - - public void setStateRoot(Bytes32 stateRoot) { - this.stateRoot = stateRoot; - } - - public Bytes32 getReceiptsRoot() { - return receiptsRoot; - } - - public void setReceiptsRoot(Bytes32 receiptsRoot) { - this.receiptsRoot = receiptsRoot; - } - - public Bytes getLogsBloom() { - return logsBloom; - } - - public void setLogsBloom(Bytes logsBloom) { - this.logsBloom = logsBloom; - } - - public Bytes32 getPrevRandao() { - return prevRandao; - } - - public void setPrevRandao(Bytes32 prevRandao) { - this.prevRandao = prevRandao; - } - - public UInt64 getBlockNumber() { - return blockNumber; - } - - public void setBlockNumber(UInt64 blockNumber) { - this.blockNumber = blockNumber; - } - - public UInt64 getGasLimit() { - return gasLimit; - } - - public void setGasLimit(UInt64 gasLimit) { - this.gasLimit = gasLimit; - } - - public UInt64 getGasUsed() { - return gasUsed; - } - - public void setGasUsed(UInt64 gasUsed) { - this.gasUsed = gasUsed; - } - - public UInt64 getTimestamp() { - return timestamp; - } - - public void setTimestamp(UInt64 timestamp) { - this.timestamp = timestamp; - } - - public Bytes getExtraData() { - return extraData; - } - - public void setExtraData(Bytes extraData) { - this.extraData = extraData; - } - - public UInt256 getBaseFeePerGas() { - return baseFeePerGas; - } - - public void setBaseFeePerGas(UInt256 baseFeePerGas) { - this.baseFeePerGas = baseFeePerGas; - } - - public Bytes32 getBlockHash() { - return blockHash; - } - - public void setBlockHash(Bytes32 blockHash) { - this.blockHash = blockHash; - } - @Override public boolean equals(final Object o) { if (this == o) { diff --git a/data/serializer/src/main/java/tech/pegasys/teku/api/schema/capella/ExecutionPayloadCapella.java b/data/serializer/src/main/java/tech/pegasys/teku/api/schema/capella/ExecutionPayloadCapella.java index 83d2612cfb9..12264a48aed 100644 --- a/data/serializer/src/main/java/tech/pegasys/teku/api/schema/capella/ExecutionPayloadCapella.java +++ b/data/serializer/src/main/java/tech/pegasys/teku/api/schema/capella/ExecutionPayloadCapella.java @@ -38,7 +38,7 @@ public class ExecutionPayloadCapella extends ExecutionPayloadBellatrix implement @JsonProperty("withdrawals") @ArraySchema(schema = @Schema(type = "string", format = "byte")) - public List withdrawals; + public final List withdrawals; @JsonCreator public ExecutionPayloadCapella( @@ -84,16 +84,6 @@ public ExecutionPayloadCapella( .collect(toList()); } - public ExecutionPayloadCapella() {} - - public List getWithdrawals() { - return withdrawals; - } - - public void setWithdrawals(List withdrawals) { - this.withdrawals = withdrawals; - } - @Override protected ExecutionPayloadBuilder applyToBuilder( final ExecutionPayloadSchema executionPayloadSchema, diff --git a/data/serializer/src/main/java/tech/pegasys/teku/api/schema/capella/Withdrawal.java b/data/serializer/src/main/java/tech/pegasys/teku/api/schema/capella/Withdrawal.java index 57b9c73b504..ac586a46c6f 100644 --- a/data/serializer/src/main/java/tech/pegasys/teku/api/schema/capella/Withdrawal.java +++ b/data/serializer/src/main/java/tech/pegasys/teku/api/schema/capella/Withdrawal.java @@ -16,44 +16,22 @@ import com.fasterxml.jackson.annotation.JsonProperty; import tech.pegasys.teku.ethereum.execution.types.Eth1Address; import tech.pegasys.teku.infrastructure.bytes.Bytes20; -import tech.pegasys.teku.infrastructure.json.types.CoreTypes; -import tech.pegasys.teku.infrastructure.json.types.DeserializableTypeDefinition; import tech.pegasys.teku.infrastructure.unsigned.UInt64; import tech.pegasys.teku.spec.datastructures.execution.versions.capella.WithdrawalSchema; public class Withdrawal { @JsonProperty("index") - private UInt64 index; + private final UInt64 index; @JsonProperty("validator_index") - private UInt64 validatorIndex; + private final UInt64 validatorIndex; @JsonProperty("address") - private Eth1Address address; + private final Eth1Address address; @JsonProperty("amount") - private UInt64 amount; - - public static final DeserializableTypeDefinition ETH1ADDRESS_TYPE = - DeserializableTypeDefinition.string(Eth1Address.class) - .formatter(Eth1Address::toHexString) - .parser(Eth1Address::fromHexString) - .format("byte") - .build(); - - public static final DeserializableTypeDefinition WITHDRAWAL_TYPE = - DeserializableTypeDefinition.object(Withdrawal.class) - .initializer(Withdrawal::new) - .withField("index", CoreTypes.UINT64_TYPE, Withdrawal::getIndex, Withdrawal::setIndex) - .withField( - "validator_index", - CoreTypes.UINT64_TYPE, - Withdrawal::getValidatorIndex, - Withdrawal::setValidatorIndex) - .withField("address", ETH1ADDRESS_TYPE, Withdrawal::getAddress, Withdrawal::setAddress) - .withField("amount", CoreTypes.UINT64_TYPE, Withdrawal::getAmount, Withdrawal::setAmount) - .build(); + private final UInt64 amount; public Withdrawal( @JsonProperty("index") final UInt64 index, @@ -75,40 +53,6 @@ public Withdrawal( this.amount = withdrawal.getAmount(); } - public Withdrawal() {} - - public UInt64 getIndex() { - return index; - } - - public void setIndex(UInt64 index) { - this.index = index; - } - - public UInt64 getValidatorIndex() { - return validatorIndex; - } - - public void setValidatorIndex(UInt64 validatorIndex) { - this.validatorIndex = validatorIndex; - } - - public Eth1Address getAddress() { - return address; - } - - public void setAddress(Eth1Address address) { - this.address = address; - } - - public UInt64 getAmount() { - return amount; - } - - public void setAmount(UInt64 amount) { - this.amount = amount; - } - public tech.pegasys.teku.spec.datastructures.execution.versions.capella.Withdrawal asInternalWithdrawal(final WithdrawalSchema schema) { return schema.create(index, validatorIndex, new Bytes20(address.getWrappedBytes()), amount); diff --git a/data/serializer/src/main/java/tech/pegasys/teku/api/schema/deneb/BlockContainer.java b/data/serializer/src/main/java/tech/pegasys/teku/api/schema/deneb/BlockContainer.java deleted file mode 100644 index 3a3d06b1c99..00000000000 --- a/data/serializer/src/main/java/tech/pegasys/teku/api/schema/deneb/BlockContainer.java +++ /dev/null @@ -1,16 +0,0 @@ -/* - * 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.api.schema.deneb; - -public interface BlockContainer {}