From 7c1e8a00df680ef56fd07d94f1ff467f14087d68 Mon Sep 17 00:00:00 2001 From: a10zn8 Date: Tue, 14 Mar 2023 15:04:11 +0300 Subject: [PATCH] In case disabled enrichment and native subscription without proxy we should have fallback enrichment of blocks (#159) --- .../dshackle/data/BlockContainer.kt | 26 +++++++++++++++++++ .../ethereum/subscribe/ProduceNewHeads.kt | 20 +------------- 2 files changed, 27 insertions(+), 19 deletions(-) diff --git a/src/main/kotlin/io/emeraldpay/dshackle/data/BlockContainer.kt b/src/main/kotlin/io/emeraldpay/dshackle/data/BlockContainer.kt index 00f57115..a5aa3fef 100644 --- a/src/main/kotlin/io/emeraldpay/dshackle/data/BlockContainer.kt +++ b/src/main/kotlin/io/emeraldpay/dshackle/data/BlockContainer.kt @@ -17,8 +17,13 @@ package io.emeraldpay.dshackle.data import io.emeraldpay.dshackle.Global +import io.emeraldpay.etherjar.domain.Address +import io.emeraldpay.etherjar.domain.BlockHash +import io.emeraldpay.etherjar.domain.Bloom +import io.emeraldpay.etherjar.domain.Wei import io.emeraldpay.etherjar.rpc.json.BlockJson import io.emeraldpay.etherjar.rpc.json.TransactionJson +import io.emeraldpay.etherjar.rpc.json.TransactionRefJson import java.math.BigInteger import java.time.Instant @@ -99,4 +104,25 @@ class BlockContainer( result = 31 * result + hash.hashCode() return result } + + fun toBlock(): BlockJson<*> { + return if (parsed != null) { + parsed as BlockJson + } else if (json != null) { + Global.objectMapper.readValue(json, BlockJson::class.java) + } else { + BlockJson().also { + it.number = height + it.hash = BlockHash.from(hash.value) + it.parentHash = BlockHash.empty() + it.timestamp = timestamp + it.difficulty = difficulty + it.gasLimit = 0 + it.gasUsed = 0 + it.logsBloom = Bloom.empty() + it.miner = Address.empty() + it.baseFeePerGas = Wei.ZERO + } + } + } } diff --git a/src/main/kotlin/io/emeraldpay/dshackle/upstream/ethereum/subscribe/ProduceNewHeads.kt b/src/main/kotlin/io/emeraldpay/dshackle/upstream/ethereum/subscribe/ProduceNewHeads.kt index 68fa7d22..28c90088 100644 --- a/src/main/kotlin/io/emeraldpay/dshackle/upstream/ethereum/subscribe/ProduceNewHeads.kt +++ b/src/main/kotlin/io/emeraldpay/dshackle/upstream/ethereum/subscribe/ProduceNewHeads.kt @@ -15,13 +15,8 @@ */ package io.emeraldpay.dshackle.upstream.ethereum.subscribe -import io.emeraldpay.dshackle.Global -import io.emeraldpay.dshackle.data.BlockContainer import io.emeraldpay.dshackle.upstream.Head import io.emeraldpay.dshackle.upstream.ethereum.subscribe.json.NewHeadMessage -import io.emeraldpay.etherjar.rpc.json.BlockJson -import io.emeraldpay.etherjar.rpc.json.TransactionRefJson -import org.slf4j.LoggerFactory import reactor.core.publisher.Flux /** @@ -33,16 +28,10 @@ class ProduceNewHeads( val head: Head ) { - companion object { - private val log = LoggerFactory.getLogger(ProduceNewHeads::class.java) - } - - private val objectMapper = Global.objectMapper - fun start(): Flux { return head.getFlux() .map { - val block = extractBlock(it) + val block = it.toBlock() NewHeadMessage( block.number, block.hash, @@ -58,11 +47,4 @@ class ProduceNewHeads( ) } } - - private fun extractBlock(blockContainer: BlockContainer): BlockJson = - if (blockContainer.parsed != null) { - blockContainer.parsed as BlockJson - } else { - objectMapper.readValue(blockContainer.json, BlockJson::class.java) - } }