From 10f4c6b5fb082a0fa0ff4c3f571f1685eae5a3d3 Mon Sep 17 00:00:00 2001 From: Mike Sizov Date: Fri, 14 Jun 2024 20:35:03 +0700 Subject: [PATCH] Process arbitrum nitro eth_syncing message --- .../arbitrum/json/NitroSyncingJson.java | 47 +++++++++++++++++++ .../json/NitroSyncingJsonDeserializer.java | 28 +++++++++++ .../json/NitroSyncingJsonSerializer.java | 25 ++++++++++ .../ethereum/EthereumUpstreamValidator.kt | 9 +++- 4 files changed, 108 insertions(+), 1 deletion(-) create mode 100644 src/main/java/io/emeraldpay/dshackle/upstream/arbitrum/json/NitroSyncingJson.java create mode 100644 src/main/java/io/emeraldpay/dshackle/upstream/arbitrum/json/NitroSyncingJsonDeserializer.java create mode 100644 src/main/java/io/emeraldpay/dshackle/upstream/arbitrum/json/NitroSyncingJsonSerializer.java diff --git a/src/main/java/io/emeraldpay/dshackle/upstream/arbitrum/json/NitroSyncingJson.java b/src/main/java/io/emeraldpay/dshackle/upstream/arbitrum/json/NitroSyncingJson.java new file mode 100644 index 000000000..9b27acba0 --- /dev/null +++ b/src/main/java/io/emeraldpay/dshackle/upstream/arbitrum/json/NitroSyncingJson.java @@ -0,0 +1,47 @@ +package io.emeraldpay.dshackle.upstream.arbitrum.json; + +import com.fasterxml.jackson.databind.annotation.JsonDeserialize; +import com.fasterxml.jackson.databind.annotation.JsonSerialize; + +@JsonDeserialize(using = NitroSyncingJsonDeserializer.class) +@JsonSerialize(using = NitroSyncingJsonSerializer.class) +public class NitroSyncingJson { + + private boolean syncing; + + private Long syncTargetMsgCount; + private Long feedPendingMessageCount; + private Long msgCount; + + public boolean isSyncing() { + return syncing; + } + + public void setSyncing(boolean syncing) { + this.syncing = syncing; + } + + public Long getSyncTargetMsgCount() { + return syncTargetMsgCount; + } + + public void setSyncTargetMsgCount(Long syncTargetMsgCount) { + this.syncTargetMsgCount = syncTargetMsgCount; + } + + public Long getFeedPendingMessageCount() { + return feedPendingMessageCount; + } + + public void setFeedPendingMessageCount(Long feedPendingMessageCount) { + this.feedPendingMessageCount = feedPendingMessageCount; + } + + public Long getMsgCount() { + return msgCount; + } + + public void setMsgCount(Long msgCount) { + this.msgCount = msgCount; + } +} diff --git a/src/main/java/io/emeraldpay/dshackle/upstream/arbitrum/json/NitroSyncingJsonDeserializer.java b/src/main/java/io/emeraldpay/dshackle/upstream/arbitrum/json/NitroSyncingJsonDeserializer.java new file mode 100644 index 000000000..7bcbcfebf --- /dev/null +++ b/src/main/java/io/emeraldpay/dshackle/upstream/arbitrum/json/NitroSyncingJsonDeserializer.java @@ -0,0 +1,28 @@ +package io.emeraldpay.dshackle.upstream.arbitrum.json; + +import com.fasterxml.jackson.core.JsonParser; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.DeserializationContext; +import com.fasterxml.jackson.databind.JsonNode; +import io.emeraldpay.dshackle.upstream.arbitrum.json.NitroSyncingJson; +import io.emeraldpay.dshackle.upstream.ethereum.json.EtherJsonDeserializer; + +import java.io.IOException; + +public class NitroSyncingJsonDeserializer extends EtherJsonDeserializer { + + @Override + public NitroSyncingJson deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException { + JsonNode node = jp.readValueAsTree(); + NitroSyncingJson resp = new NitroSyncingJson(); + if (node.isBoolean()) { + resp.setSyncing(node.asBoolean()); + } else { + resp.setSyncing(true); + resp.setSyncTargetMsgCount(getLong(node, "syncTargetMsgCount")); + resp.setFeedPendingMessageCount(getLong(node, "feedPendingMessageCount")); + resp.setMsgCount(getLong(node, "msgCount")); + } + return resp; + } +} diff --git a/src/main/java/io/emeraldpay/dshackle/upstream/arbitrum/json/NitroSyncingJsonSerializer.java b/src/main/java/io/emeraldpay/dshackle/upstream/arbitrum/json/NitroSyncingJsonSerializer.java new file mode 100644 index 000000000..be63c51c4 --- /dev/null +++ b/src/main/java/io/emeraldpay/dshackle/upstream/arbitrum/json/NitroSyncingJsonSerializer.java @@ -0,0 +1,25 @@ +package io.emeraldpay.dshackle.upstream.arbitrum.json; + +import com.fasterxml.jackson.core.JsonGenerator; +import com.fasterxml.jackson.databind.SerializerProvider; +import io.emeraldpay.dshackle.upstream.ethereum.json.EtherJsonSerializer; + +import java.io.IOException; + +public class NitroSyncingJsonSerializer extends EtherJsonSerializer { + + @Override + public void serialize(NitroSyncingJson value, JsonGenerator gen, SerializerProvider serializers) throws IOException { + if (value == null) { + gen.writeNull(); + } else if (value.isSyncing()) { + gen.writeStartObject(); + writeField(gen, "syncTargetMsgCount", value.getSyncTargetMsgCount()); + writeField(gen, "feedPendingMessageCount", value.getFeedPendingMessageCount()); + writeField(gen, "msgCount", value.getMsgCount()); + gen.writeEndObject(); + } else { + gen.writeBoolean(false); + } + } +} diff --git a/src/main/kotlin/io/emeraldpay/dshackle/upstream/ethereum/EthereumUpstreamValidator.kt b/src/main/kotlin/io/emeraldpay/dshackle/upstream/ethereum/EthereumUpstreamValidator.kt index c46fad378..956fd5dd2 100644 --- a/src/main/kotlin/io/emeraldpay/dshackle/upstream/ethereum/EthereumUpstreamValidator.kt +++ b/src/main/kotlin/io/emeraldpay/dshackle/upstream/ethereum/EthereumUpstreamValidator.kt @@ -28,6 +28,7 @@ import io.emeraldpay.dshackle.upstream.ChainResponse import io.emeraldpay.dshackle.upstream.Upstream import io.emeraldpay.dshackle.upstream.UpstreamAvailability import io.emeraldpay.dshackle.upstream.ValidateUpstreamSettingsResult +import io.emeraldpay.dshackle.upstream.arbitrum.json.NitroSyncingJson import io.emeraldpay.dshackle.upstream.ethereum.domain.Address import io.emeraldpay.dshackle.upstream.ethereum.hex.HexData import io.emeraldpay.dshackle.upstream.ethereum.json.SyncingJson @@ -58,7 +59,13 @@ open class EthereumUpstreamValidator @JvmOverloads constructor( override fun validateSyncingRequest(): ValidateSyncingRequest { return ValidateSyncingRequest( ChainRequest("eth_syncing", ListParams()), - ) { bytes -> objectMapper.readValue(bytes, SyncingJson::class.java).isSyncing } + ) { bytes -> + if (listOf(Chain.ARBITRUM__MAINNET, Chain.ARBITRUM__SEPOLIA, Chain.ARBITRUM_NOVA__MAINNET).contains(chain)) { + objectMapper.readValue(bytes, NitroSyncingJson::class.java).isSyncing + } else { + objectMapper.readValue(bytes, SyncingJson::class.java).isSyncing + } + } } override fun validatePeersRequest(): ValidatePeersRequest {