From dd115d8c9ccefda301cb3ce973da417e7b5c3c8c Mon Sep 17 00:00:00 2001 From: meywood <105049338+meywood@users.noreply.github.com> Date: Mon, 10 Jun 2024 15:27:44 +0100 Subject: [PATCH] issues/279 - EraEndV2 tidy up --- .../com/casper/sdk/model/block/BlockBody.java | 4 +- .../casper/sdk/model/block/BlockBodyV1.java | 16 +----- .../casper/sdk/model/block/BlockHeader.java | 12 +--- .../casper/sdk/model/block/BlockHeaderV2.java | 6 +- .../com/casper/sdk/model/block/BlockV1.java | 12 +--- .../com/casper/sdk/model/block/EraEndV2.java | 36 ------------ .../casper/sdk/model/block/JsonBlockData.java | 8 +-- .../com/casper/sdk/model/era/EraEndV2.java | 55 +++++++++++++++++++ .../sdk/service/CasperServiceTests.java | 3 +- 9 files changed, 70 insertions(+), 82 deletions(-) delete mode 100644 src/main/java/com/casper/sdk/model/block/EraEndV2.java create mode 100644 src/main/java/com/casper/sdk/model/era/EraEndV2.java diff --git a/src/main/java/com/casper/sdk/model/block/BlockBody.java b/src/main/java/com/casper/sdk/model/block/BlockBody.java index 302e5b45..383f1c4e 100644 --- a/src/main/java/com/casper/sdk/model/block/BlockBody.java +++ b/src/main/java/com/casper/sdk/model/block/BlockBody.java @@ -13,9 +13,7 @@ @Setter public abstract class BlockBody { - /** - * @see PublicKey - */ + /** @see PublicKey */ @JsonProperty("proposer") private PublicKey proposer; diff --git a/src/main/java/com/casper/sdk/model/block/BlockBodyV1.java b/src/main/java/com/casper/sdk/model/block/BlockBodyV1.java index e1e0510b..a3f03a32 100644 --- a/src/main/java/com/casper/sdk/model/block/BlockBodyV1.java +++ b/src/main/java/com/casper/sdk/model/block/BlockBodyV1.java @@ -1,13 +1,8 @@ package com.casper.sdk.model.block; import com.casper.sdk.model.common.Digest; -import com.casper.sdk.model.key.PublicKey; import com.fasterxml.jackson.annotation.JsonProperty; -import lombok.AllArgsConstructor; -import lombok.Builder; -import lombok.Getter; -import lombok.NoArgsConstructor; -import lombok.Setter; +import lombok.*; import java.util.List; @@ -26,16 +21,11 @@ @NoArgsConstructor public class BlockBodyV1 extends BlockBody { - /** - * List of Hex-encoded hash digest - */ + /** List of Hex-encoded hash digest */ @JsonProperty("deploy_hashes") private List deployHashes; - /** - * List of Hex-encoded hash digest - */ + /** List of Hex-encoded hash digest */ @JsonProperty("transfer_hashes") private List transferHashes; - } diff --git a/src/main/java/com/casper/sdk/model/block/BlockHeader.java b/src/main/java/com/casper/sdk/model/block/BlockHeader.java index f6a3202a..38356b47 100644 --- a/src/main/java/com/casper/sdk/model/block/BlockHeader.java +++ b/src/main/java/com/casper/sdk/model/block/BlockHeader.java @@ -33,22 +33,16 @@ public class BlockHeader { @JsonProperty("accumulated_seed") private Digest accumulatedSeed; - /** - * Era ID newtype - */ + /** Era ID newtype */ @JsonProperty("era_id") private long eraId; - /** - * Timestamp formatted as per RFC 3339 - */ + /** Timestamp formatted as per RFC 3339 */ @JsonProperty("timestamp") @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'") private Date timeStamp; - /** - * Casper Platform protocol version - */ + /** Casper Platform protocol version */ @JsonProperty("protocol_version") private String protocolVersion; } diff --git a/src/main/java/com/casper/sdk/model/block/BlockHeaderV2.java b/src/main/java/com/casper/sdk/model/block/BlockHeaderV2.java index 86a82324..3ed0926c 100644 --- a/src/main/java/com/casper/sdk/model/block/BlockHeaderV2.java +++ b/src/main/java/com/casper/sdk/model/block/BlockHeaderV2.java @@ -1,12 +1,10 @@ package com.casper.sdk.model.block; import com.casper.sdk.model.common.Digest; -import com.fasterxml.jackson.annotation.JsonFormat; +import com.casper.sdk.model.era.EraEndV2; import com.fasterxml.jackson.annotation.JsonProperty; import lombok.*; -import java.util.Date; - /** * @author ian@meywood.com */ @@ -28,6 +26,4 @@ public class BlockHeaderV2 extends BlockHeader { @JsonProperty("last_switch_block_hash") private Digest lastSwitchBlockHash; - - } diff --git a/src/main/java/com/casper/sdk/model/block/BlockV1.java b/src/main/java/com/casper/sdk/model/block/BlockV1.java index ff4d333a..e93ef1f9 100644 --- a/src/main/java/com/casper/sdk/model/block/BlockV1.java +++ b/src/main/java/com/casper/sdk/model/block/BlockV1.java @@ -20,21 +20,15 @@ @NoArgsConstructor public class BlockV1 extends Block { - /** - * {@link JsonBlockHeader} - */ + /** {@link JsonBlockHeader} */ @JsonProperty("header") private JsonBlockHeader header; - /** - * {@link BlockBodyV1} - */ + /** {@link BlockBodyV1} */ @JsonProperty("body") private BlockBodyV1 body; - /** - * List of {@link JsonProof} - */ + /** List of {@link JsonProof} */ @JsonProperty("proofs") private List proofs; } diff --git a/src/main/java/com/casper/sdk/model/block/EraEndV2.java b/src/main/java/com/casper/sdk/model/block/EraEndV2.java deleted file mode 100644 index f31c323f..00000000 --- a/src/main/java/com/casper/sdk/model/block/EraEndV2.java +++ /dev/null @@ -1,36 +0,0 @@ -package com.casper.sdk.model.block; - -import com.casper.sdk.model.era.ValidatorWeight; -import com.casper.sdk.model.key.PublicKey; -import com.fasterxml.jackson.annotation.JsonIgnoreProperties; -import com.fasterxml.jackson.annotation.JsonProperty; -import lombok.AllArgsConstructor; -import lombok.Getter; -import lombok.NoArgsConstructor; -import lombok.Setter; - -import java.math.BigInteger; -import java.util.List; -import java.util.Map; - -/** - * @author ian@meywood.com - */ -@AllArgsConstructor -@NoArgsConstructor -@Getter -@Setter -@JsonIgnoreProperties(ignoreUnknown = true) -public class EraEndV2 { - - private List equivocators; - @JsonProperty("inactive_validators") - private List inactiveValidators; - @JsonProperty("next_era_validator_weights") - private List nextEraValidatorWeights; - // TODO - Fix deserialization to allow key to be PublicKey - @JsonProperty("rewards") - private Map> rewards; - @JsonProperty("next_era_gas_price") - private int nextEraGasPrice; -} diff --git a/src/main/java/com/casper/sdk/model/block/JsonBlockData.java b/src/main/java/com/casper/sdk/model/block/JsonBlockData.java index 25d3fffd..518ba916 100644 --- a/src/main/java/com/casper/sdk/model/block/JsonBlockData.java +++ b/src/main/java/com/casper/sdk/model/block/JsonBlockData.java @@ -21,15 +21,11 @@ @NoArgsConstructor public class JsonBlockData { - /** - * The RPC API version - */ + /** The RPC API version */ @JsonProperty("api_version") private String apiVersion; - /** - * @see BlockV1 - */ + /** @see BlockV1 */ @JsonProperty("block") private BlockV1 block; } diff --git a/src/main/java/com/casper/sdk/model/era/EraEndV2.java b/src/main/java/com/casper/sdk/model/era/EraEndV2.java new file mode 100644 index 00000000..8f031318 --- /dev/null +++ b/src/main/java/com/casper/sdk/model/era/EraEndV2.java @@ -0,0 +1,55 @@ +package com.casper.sdk.model.era; + +import com.casper.sdk.exception.InvalidKeyBytesException; +import com.casper.sdk.model.key.PublicKey; +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.annotation.JsonSetter; +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; + +import java.math.BigInteger; +import java.security.NoSuchAlgorithmException; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + +/** + * @author ian@meywood.com + */ +@AllArgsConstructor +@NoArgsConstructor +@Getter +@Setter +@JsonIgnoreProperties(ignoreUnknown = true) +public class EraEndV2 { + + private List equivocators; + @JsonProperty("inactive_validators") + private List inactiveValidators; + @JsonProperty("next_era_validator_weights") + private List nextEraValidatorWeights; + @JsonProperty("rewards") + private Map> rewards; + @JsonProperty("next_era_gas_price") + private int nextEraGasPrice; + + @JsonSetter("rewards") + public void setRewards(final Map> rewards) { + + this.rewards = new LinkedHashMap<>(); + + if (rewards != null) { + rewards.forEach((key, value) -> { + try { + // Jackson is not good at deserializing keys so we have to do it manually + this.rewards.put(PublicKey.fromTaggedHexString(key), value); + } catch (NoSuchAlgorithmException e) { + throw new InvalidKeyBytesException(e); + } + }); + } + } +} diff --git a/src/test/java/com/casper/sdk/service/CasperServiceTests.java b/src/test/java/com/casper/sdk/service/CasperServiceTests.java index ff1fc28c..2ea2fa70 100644 --- a/src/test/java/com/casper/sdk/service/CasperServiceTests.java +++ b/src/test/java/com/casper/sdk/service/CasperServiceTests.java @@ -16,6 +16,7 @@ import com.casper.sdk.model.deploy.executabledeploy.ModuleBytes; import com.casper.sdk.model.deploy.executabledeploy.StoredContractByHash; import com.casper.sdk.model.deploy.executionresult.Success; +import com.casper.sdk.model.era.EraEndV2; import com.casper.sdk.model.era.EraInfoData; import com.casper.sdk.model.globalstate.GlobalStateData; import com.casper.sdk.model.key.AlgorithmTag; @@ -568,6 +569,6 @@ void chainGetBlockEraEndV2() throws NoSuchAlgorithmException { assertThat(eraEnd.getNextEraValidatorWeights().get(0).getWeight(), is(new BigInteger("11823890605832274469"))); assertThat(eraEnd.getRewards().size(), is(5)); - assertThat(eraEnd.getRewards().get("010b277da84a12c8814d5723eeb57123ff287f22466fd13faca1bb1fae57d2679b"), hasItems(new BigInteger("4026058477024681"), new BigInteger("402627925137076"))); + assertThat(eraEnd.getRewards().get(PublicKey.fromTaggedHexString("010b277da84a12c8814d5723eeb57123ff287f22466fd13faca1bb1fae57d2679b")), hasItems(new BigInteger("4026058477024681"), new BigInteger("402627925137076"))); } }