From a9bfb7933327c858d871e99317060158d18d7f4f Mon Sep 17 00:00:00 2001 From: bobong Date: Fri, 21 Jun 2024 18:01:19 +0800 Subject: [PATCH] [block meta] add the parents of the block into block meta data, serialize and deserialize --- pom.xml | 2 +- .../java/org/starcoin/bean/BlockMetadata.java | 15 ++++++++++ .../org/starcoin/types/BlockMetadata.java | 30 ++++++++++++++++--- .../java/org/starcoin/types/TraitHelpers.java | 18 +++++++++++ 4 files changed, 60 insertions(+), 5 deletions(-) diff --git a/pom.xml b/pom.xml index fd92b825..3736e7b3 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ org.starcoin sdk - 1.2.3 + 1.2.4 Starcoin Java SDK Starcoin Java SDK diff --git a/src/main/java/org/starcoin/bean/BlockMetadata.java b/src/main/java/org/starcoin/bean/BlockMetadata.java index cd2d3a05..96772a4d 100644 --- a/src/main/java/org/starcoin/bean/BlockMetadata.java +++ b/src/main/java/org/starcoin/bean/BlockMetadata.java @@ -16,6 +16,7 @@ package org.starcoin.bean; import com.alibaba.fastjson.annotation.JSONField; +import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonProperty; public class BlockMetadata { @@ -44,6 +45,11 @@ public class BlockMetadata { @JsonProperty("parent_gas_used") long parentGasUsed; + @JSONField(name = "parents_hash") + @JsonProperty("parents_hash") + @JsonIgnore + String parentsHash; + public String getParentHash() { return parentHash; } @@ -108,6 +114,14 @@ public void setParentGasUsed(long parentGasUsed) { this.parentGasUsed = parentGasUsed; } + public String getParentsHash() { + return parentsHash; + } + + public void setParentsHash(String parentsHash) { + this.parentsHash = parentsHash; + } + @Override public String toString() { return "BlockMetadata{" + @@ -119,6 +133,7 @@ public String toString() { ", number='" + number + '\'' + ", chainId='" + chainId + '\'' + ", parentGasUsed=" + parentGasUsed + + ", parentsHash=" + parentsHash + '}'; } } diff --git a/src/main/java/org/starcoin/types/BlockMetadata.java b/src/main/java/org/starcoin/types/BlockMetadata.java index ca14eeb3..0310739e 100644 --- a/src/main/java/org/starcoin/types/BlockMetadata.java +++ b/src/main/java/org/starcoin/types/BlockMetadata.java @@ -1,6 +1,12 @@ package org.starcoin.types; +import com.novi.serde.Unsigned; + +import java.util.List; +import java.util.Optional; +import java.util.stream.Collectors; + public final class BlockMetadata { public final HashValue parent_hash; public final @com.novi.serde.Unsigned Long timestamp; @@ -10,8 +16,19 @@ public final class BlockMetadata { public final @com.novi.serde.Unsigned Long number; public final ChainId chain_id; public final @com.novi.serde.Unsigned Long parent_gas_used; + public final List parents_hash; - public BlockMetadata(HashValue parent_hash, @com.novi.serde.Unsigned Long timestamp, AccountAddress author, java.util.Optional author_auth_key, @com.novi.serde.Unsigned Long uncles, @com.novi.serde.Unsigned Long number, ChainId chain_id, @com.novi.serde.Unsigned Long parent_gas_used) { + public BlockMetadata( + HashValue parent_hash, + @Unsigned Long timestamp, + AccountAddress author, + Optional author_auth_key, + @Unsigned Long uncles, + @Unsigned Long number, + ChainId chain_id, + @Unsigned Long parent_gas_used, + List parents_hash + ) { java.util.Objects.requireNonNull(parent_hash, "parent_hash must not be null"); java.util.Objects.requireNonNull(timestamp, "timestamp must not be null"); java.util.Objects.requireNonNull(author, "author must not be null"); @@ -28,6 +45,7 @@ public BlockMetadata(HashValue parent_hash, @com.novi.serde.Unsigned Long timest this.number = number; this.chain_id = chain_id; this.parent_gas_used = parent_gas_used; + this.parents_hash = parents_hash; } public static BlockMetadata deserialize(com.novi.serde.Deserializer deserializer) throws com.novi.serde.DeserializationError { @@ -41,7 +59,8 @@ public static BlockMetadata deserialize(com.novi.serde.Deserializer deserializer builder.number = deserializer.deserialize_u64(); builder.chain_id = ChainId.deserialize(deserializer); builder.parent_gas_used = deserializer.deserialize_u64(); - deserializer.decrease_container_depth(); + builder.parents_hash = TraitHelpers.deserialize_vector_hashvalue(deserializer); + return builder.build(); } @@ -67,6 +86,7 @@ public void serialize(com.novi.serde.Serializer serializer) throws com.novi.serd serializer.serialize_u64(number); chain_id.serialize(serializer); serializer.serialize_u64(parent_gas_used); + TraitHelpers.serialize_vector_hashvalue(parents_hash, serializer); serializer.decrease_container_depth(); } @@ -105,7 +125,7 @@ public boolean equals(Object obj) { if (!java.util.Objects.equals(this.parent_gas_used, other.parent_gas_used)) { return false; } - return true; + return java.util.Objects.equals(this.parents_hash, other.parents_hash); } public int hashCode() { @@ -130,6 +150,7 @@ public static final class Builder { public @com.novi.serde.Unsigned Long number; public ChainId chain_id; public @com.novi.serde.Unsigned Long parent_gas_used; + public List parents_hash; public BlockMetadata build() { return new BlockMetadata( @@ -140,7 +161,8 @@ public BlockMetadata build() { uncles, number, chain_id, - parent_gas_used + parent_gas_used, + parents_hash ); } } diff --git a/src/main/java/org/starcoin/types/TraitHelpers.java b/src/main/java/org/starcoin/types/TraitHelpers.java index 5b2eb464..031b6c81 100644 --- a/src/main/java/org/starcoin/types/TraitHelpers.java +++ b/src/main/java/org/starcoin/types/TraitHelpers.java @@ -1,5 +1,7 @@ package org.starcoin.types; +import java.util.List; + final class TraitHelpers { static void serialize_array16_u8_array(java.util.@com.novi.serde.ArrayLen(length = 16) List<@com.novi.serde.Unsigned Byte> value, com.novi.serde.Serializer serializer) throws com.novi.serde.SerializationError { if (value.size() != 16) { @@ -105,6 +107,15 @@ static java.util.List deserialize_vector_str(com.novi.serde.Deserializer return obj; } + static java.util.List deserialize_vector_hashvalue(com.novi.serde.Deserializer deserializer) throws com.novi.serde.DeserializationError { + long length = deserializer.deserialize_len(); + java.util.List obj = new java.util.ArrayList<>((int) length); + for (long i = 0; i < length; i++) { + obj.add(HashValue.deserialize(deserializer)); + } + return obj; + } + static void serialize_option_AuthenticationKey(java.util.Optional value, com.novi.serde.Serializer serializer) throws com.novi.serde.SerializationError { if (value.isPresent()) { serializer.serialize_option_tag(true); @@ -301,5 +312,12 @@ static void serialize_vector_u8(java.util.List<@com.novi.serde.Unsigned Byte> va return obj; } + static void serialize_vector_hashvalue(List hashValueList, com.novi.serde.Serializer serializer) throws com.novi.serde.SerializationError { + serializer.serialize_len(hashValueList.size()); + for (HashValue hash : hashValueList) { + hash.serialize(serializer); + } + } + }