In case disabled enrichment and native subscription without proxy we should have fallback enrichment of blocks (#159)

This commit is contained in:
a10zn8
2023-03-14 15:04:11 +03:00
committed by GitHub
parent 3a48eceaed
commit 7c1e8a00df
2 changed files with 27 additions and 19 deletions

View File

@@ -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<TransactionRefJson>
} else if (json != null) {
Global.objectMapper.readValue(json, BlockJson::class.java)
} else {
BlockJson<TransactionRefJson>().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
}
}
}
}

View File

@@ -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<NewHeadMessage> {
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<out TransactionRefJson> =
if (blockContainer.parsed != null) {
blockContainer.parsed as BlockJson<TransactionRefJson>
} else {
objectMapper.readValue(blockContainer.json, BlockJson::class.java)
}
}