diff --git a/src/main/kotlin/io/emeraldpay/dshackle/Config.kt b/src/main/kotlin/io/emeraldpay/dshackle/Config.kt index e199ec56..3ec7a475 100644 --- a/src/main/kotlin/io/emeraldpay/dshackle/Config.kt +++ b/src/main/kotlin/io/emeraldpay/dshackle/Config.kt @@ -73,20 +73,6 @@ open class Config( return target } - @Bean - open fun objectMapper(): ObjectMapper { - val module = SimpleModule("EmeraldDshackle", Version(1, 0, 0, null, null, null)) - - val objectMapper = ObjectMapper() - objectMapper.registerModule(module) - objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false) - objectMapper - .setDateFormat(SimpleDateFormat("yyyy-MM-dd\'T\'HH:mm:ss.SSS")) - .setTimeZone(TimeZone.getTimeZone("UTC")) - - return objectMapper - } - @Bean @Qualifier("upstreamScheduler") open fun upstreamScheduler(): Scheduler { return Schedulers.fromExecutorService(Executors.newFixedThreadPool(16)) diff --git a/src/main/kotlin/io/emeraldpay/dshackle/Global.kt b/src/main/kotlin/io/emeraldpay/dshackle/Global.kt new file mode 100644 index 00000000..a97adcc7 --- /dev/null +++ b/src/main/kotlin/io/emeraldpay/dshackle/Global.kt @@ -0,0 +1,47 @@ +/** + * Copyright (c) 2020 EmeraldPay, Inc + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.emeraldpay.dshackle + +import com.fasterxml.jackson.core.Version +import com.fasterxml.jackson.databind.DeserializationFeature +import com.fasterxml.jackson.databind.ObjectMapper +import com.fasterxml.jackson.databind.module.SimpleModule +import java.text.SimpleDateFormat +import java.util.* + +class Global { + + companion object { + + @JvmStatic + val objectMapper: ObjectMapper = createObjectMapper() + + private fun createObjectMapper(): ObjectMapper { + val module = SimpleModule("EmeraldDshackle", Version(1, 0, 0, null, null, null)) + + val objectMapper = ObjectMapper() + objectMapper.registerModule(module) + objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false) + objectMapper + .setDateFormat(SimpleDateFormat("yyyy-MM-dd\'T\'HH:mm:ss.SSS")) + .setTimeZone(TimeZone.getTimeZone("UTC")) + + return objectMapper + } + + } + +} \ No newline at end of file diff --git a/src/main/kotlin/io/emeraldpay/dshackle/cache/Caches.kt b/src/main/kotlin/io/emeraldpay/dshackle/cache/Caches.kt index 8b23723e..d2eb15d5 100644 --- a/src/main/kotlin/io/emeraldpay/dshackle/cache/Caches.kt +++ b/src/main/kotlin/io/emeraldpay/dshackle/cache/Caches.kt @@ -16,6 +16,7 @@ package io.emeraldpay.dshackle.cache import com.fasterxml.jackson.databind.ObjectMapper +import io.emeraldpay.dshackle.Global import io.emeraldpay.dshackle.data.BlockContainer import io.emeraldpay.dshackle.data.BlockId import io.emeraldpay.dshackle.data.TxContainer @@ -34,8 +35,7 @@ open class Caches( private val blocksByHeight: HeightCache, private val memTxsByHash: TxMemCache, private val redisBlocksByHash: BlocksRedisCache?, - private val redisTxsByHash: TxRedisCache?, - private val objectMapper: ObjectMapper + private val redisTxsByHash: TxRedisCache? ) { companion object { @@ -47,8 +47,8 @@ open class Caches( } @JvmStatic - fun default(objectMapper: ObjectMapper): Caches { - return newBuilder().setObjectMapper(objectMapper).build() + fun default(): Caches { + return newBuilder().build() } } @@ -113,10 +113,10 @@ open class Caches( var blockOnlyContainer: BlockContainer? = null var jsonValue: BlockJson<*>? = null if (block.full) { - jsonValue = objectMapper.readValue>(block.json, BlockJson::class.java) + jsonValue = Global.objectMapper.readValue>(block.json, BlockJson::class.java) //shouldn't cache block json with transactions, separate txes and blocks with refs val blockOnly = jsonValue.withoutTransactionDetails() - blockOnlyContainer = BlockContainer.from(blockOnly, objectMapper) + blockOnlyContainer = BlockContainer.from(blockOnly) } else { blockOnlyContainer = block } @@ -128,7 +128,7 @@ open class Caches( val plainTransactions = jsonValue.transactions.filterIsInstance() if (plainTransactions.isNotEmpty()) { val transactions = plainTransactions.map { tx -> - TxContainer.from(tx, objectMapper) + TxContainer.from(tx) } transactions.forEach { cache(Tag.REQUESTED, it) @@ -159,11 +159,11 @@ open class Caches( } fun getFullBlocks(): Reader { - return EthereumFullBlocksReader(objectMapper, blocksByHash, txsByHash) + return EthereumFullBlocksReader(blocksByHash, txsByHash) } fun getFullBlocksByHeight(): Reader { - return BlockByHeight(blocksByHeight, EthereumFullBlocksReader(objectMapper, blocksByHash, txsByHash)) + return BlockByHeight(blocksByHeight, EthereumFullBlocksReader(blocksByHash, txsByHash)) } enum class Tag { @@ -184,7 +184,6 @@ open class Caches( private var txsByHash: TxMemCache? = null private var redisBlocksByHash: BlocksRedisCache? = null private var redisTxsByHash: TxRedisCache? = null - private var objectMapper: ObjectMapper? = null fun setBlockByHash(cache: BlocksMemCache): Builder { blocksByHash = cache @@ -211,11 +210,6 @@ open class Caches( return this } - fun setObjectMapper(value: ObjectMapper): Builder { - objectMapper = value - return this - } - fun build(): Caches { if (blocksByHash == null) { blocksByHash = BlocksMemCache() @@ -226,10 +220,7 @@ open class Caches( if (txsByHash == null) { txsByHash = TxMemCache() } - if (objectMapper == null) { - throw IllegalStateException("ObjectMapper is not set") - } - return Caches(blocksByHash!!, blocksByHeight!!, txsByHash!!, redisBlocksByHash, redisTxsByHash, objectMapper!!) + return Caches(blocksByHash!!, blocksByHeight!!, txsByHash!!, redisBlocksByHash, redisTxsByHash) } } } \ No newline at end of file diff --git a/src/main/kotlin/io/emeraldpay/dshackle/cache/CachesFactory.kt b/src/main/kotlin/io/emeraldpay/dshackle/cache/CachesFactory.kt index cfbec762..6bf3e9fd 100644 --- a/src/main/kotlin/io/emeraldpay/dshackle/cache/CachesFactory.kt +++ b/src/main/kotlin/io/emeraldpay/dshackle/cache/CachesFactory.kt @@ -33,7 +33,6 @@ import javax.annotation.PostConstruct @Repository class CachesFactory( - @Autowired private val objectMapper: ObjectMapper, @Autowired private val cacheConfig: CacheConfig ) { @@ -74,7 +73,6 @@ class CachesFactory( private fun initCache(chain: Chain): Caches { val caches = Caches.newBuilder() - .setObjectMapper(objectMapper) redis?.let { redis -> caches.setBlockByHash(BlocksRedisCache(redis.reactive(), chain)) caches.setTxByHash(TxRedisCache(redis.reactive(), chain)) diff --git a/src/main/kotlin/io/emeraldpay/dshackle/data/BlockContainer.kt b/src/main/kotlin/io/emeraldpay/dshackle/data/BlockContainer.kt index 61e42bcc..5f4de9be 100644 --- a/src/main/kotlin/io/emeraldpay/dshackle/data/BlockContainer.kt +++ b/src/main/kotlin/io/emeraldpay/dshackle/data/BlockContainer.kt @@ -16,11 +16,9 @@ */ package io.emeraldpay.dshackle.data -import com.fasterxml.jackson.databind.ObjectMapper +import io.emeraldpay.dshackle.Global import io.infinitape.etherjar.rpc.json.BlockJson import io.infinitape.etherjar.rpc.json.TransactionJson -import io.infinitape.etherjar.rpc.json.TransactionRefJson -import org.apache.commons.codec.binary.Hex import java.math.BigInteger import java.time.Instant @@ -52,13 +50,13 @@ class BlockContainer( } @JvmStatic - fun from(block: BlockJson<*>, objectMapper: ObjectMapper): BlockContainer { - return from(block, objectMapper.writeValueAsBytes(block)) + fun from(block: BlockJson<*>): BlockContainer { + return from(block, Global.objectMapper.writeValueAsBytes(block)) } @JvmStatic - fun from(raw: ByteArray, objectMapper: ObjectMapper): BlockContainer { - val block = objectMapper.readValue(raw, BlockJson::class.java) + fun from(raw: ByteArray): BlockContainer { + val block = Global.objectMapper.readValue(raw, BlockJson::class.java) return from(block, raw) } } diff --git a/src/main/kotlin/io/emeraldpay/dshackle/data/TxContainer.kt b/src/main/kotlin/io/emeraldpay/dshackle/data/TxContainer.kt index 2e9ec38b..3519c3f7 100644 --- a/src/main/kotlin/io/emeraldpay/dshackle/data/TxContainer.kt +++ b/src/main/kotlin/io/emeraldpay/dshackle/data/TxContainer.kt @@ -17,6 +17,7 @@ package io.emeraldpay.dshackle.data import com.fasterxml.jackson.databind.ObjectMapper +import io.emeraldpay.dshackle.Global import io.infinitape.etherjar.rpc.json.TransactionJson class TxContainer( @@ -29,8 +30,8 @@ class TxContainer( companion object { @JvmStatic - fun from(tx: TransactionJson, objectMapper: ObjectMapper): TxContainer { - return from(tx, objectMapper.writeValueAsBytes(tx)) + fun from(tx: TransactionJson): TxContainer { + return from(tx, Global.objectMapper.writeValueAsBytes(tx)) } fun from(tx: TransactionJson, raw: ByteArray): TxContainer { diff --git a/src/main/kotlin/io/emeraldpay/dshackle/proxy/ReadRpcJson.kt b/src/main/kotlin/io/emeraldpay/dshackle/proxy/ReadRpcJson.kt index 30f934bd..ffb04461 100644 --- a/src/main/kotlin/io/emeraldpay/dshackle/proxy/ReadRpcJson.kt +++ b/src/main/kotlin/io/emeraldpay/dshackle/proxy/ReadRpcJson.kt @@ -19,6 +19,7 @@ package io.emeraldpay.dshackle.proxy import com.fasterxml.jackson.databind.ObjectMapper import com.google.protobuf.ByteString import io.emeraldpay.api.proto.BlockchainOuterClass +import io.emeraldpay.dshackle.Global import io.infinitape.etherjar.rpc.RpcException import io.infinitape.etherjar.rpc.RpcResponseError import io.infinitape.etherjar.rpc.json.RequestJson @@ -36,7 +37,6 @@ import java.util.stream.Collectors */ @Service open class ReadRpcJson( - @Autowired private val objectMapper: ObjectMapper ) : Function { companion object { @@ -45,6 +45,7 @@ open class ReadRpcJson( } private val jsonExtractor: Function, RequestJson> + private val objectMapper: ObjectMapper = Global.objectMapper init { jsonExtractor = Function { json -> diff --git a/src/main/kotlin/io/emeraldpay/dshackle/proxy/WriteRpcJson.kt b/src/main/kotlin/io/emeraldpay/dshackle/proxy/WriteRpcJson.kt index 9557177f..4afa8fcc 100644 --- a/src/main/kotlin/io/emeraldpay/dshackle/proxy/WriteRpcJson.kt +++ b/src/main/kotlin/io/emeraldpay/dshackle/proxy/WriteRpcJson.kt @@ -18,6 +18,7 @@ package io.emeraldpay.dshackle.proxy import com.fasterxml.jackson.databind.ObjectMapper import io.emeraldpay.api.proto.BlockchainOuterClass +import io.emeraldpay.dshackle.Global import io.infinitape.etherjar.rpc.RpcResponseError import io.infinitape.etherjar.rpc.json.ResponseJson import org.slf4j.LoggerFactory @@ -33,14 +34,14 @@ import java.util.function.Function * Writer for JSON RPC requests */ @Service -open class WriteRpcJson( - @Autowired private val objectMapper: ObjectMapper -) { +open class WriteRpcJson() { companion object { private val log = LoggerFactory.getLogger(WriteRpcJson::class.java) } + private val objectMapper: ObjectMapper = Global.objectMapper + /** * Convert Dshackle protobuf based responses to JSON RPC formatted as strings */ diff --git a/src/main/kotlin/io/emeraldpay/dshackle/quorum/BroadcastQuorum.kt b/src/main/kotlin/io/emeraldpay/dshackle/quorum/BroadcastQuorum.kt index f87a0da7..e974fdcc 100644 --- a/src/main/kotlin/io/emeraldpay/dshackle/quorum/BroadcastQuorum.kt +++ b/src/main/kotlin/io/emeraldpay/dshackle/quorum/BroadcastQuorum.kt @@ -22,9 +22,8 @@ import io.emeraldpay.dshackle.upstream.Upstream import io.infinitape.etherjar.rpc.JacksonRpcConverter open class BroadcastQuorum( - objectMapper: ObjectMapper, val quorum: Int = 3 -) : CallQuorum, ValueAwareQuorum(objectMapper, String::class.java) { +) : CallQuorum, ValueAwareQuorum(String::class.java) { private var result: ByteArray? = null private var txid: String? = null diff --git a/src/main/kotlin/io/emeraldpay/dshackle/quorum/NonEmptyQuorum.kt b/src/main/kotlin/io/emeraldpay/dshackle/quorum/NonEmptyQuorum.kt index e2804696..4752c71d 100644 --- a/src/main/kotlin/io/emeraldpay/dshackle/quorum/NonEmptyQuorum.kt +++ b/src/main/kotlin/io/emeraldpay/dshackle/quorum/NonEmptyQuorum.kt @@ -23,9 +23,8 @@ import io.infinitape.etherjar.rpc.JacksonRpcConverter import io.infinitape.etherjar.rpc.RpcException open class NonEmptyQuorum( - objectMapper: ObjectMapper, val maxTries: Int = 3 -) : CallQuorum, ValueAwareQuorum(objectMapper, Any::class.java) { +) : CallQuorum, ValueAwareQuorum(Any::class.java) { private var result: ByteArray? = null private var tries: Int = 0 diff --git a/src/main/kotlin/io/emeraldpay/dshackle/quorum/NonceQuorum.kt b/src/main/kotlin/io/emeraldpay/dshackle/quorum/NonceQuorum.kt index d40f4beb..5275b7d5 100644 --- a/src/main/kotlin/io/emeraldpay/dshackle/quorum/NonceQuorum.kt +++ b/src/main/kotlin/io/emeraldpay/dshackle/quorum/NonceQuorum.kt @@ -26,9 +26,8 @@ import java.util.concurrent.locks.ReentrantLock import kotlin.concurrent.withLock open class NonceQuorum( - objectMapper: ObjectMapper, val tries: Int = 3 -) : CallQuorum, ValueAwareQuorum(objectMapper, String::class.java) { +) : CallQuorum, ValueAwareQuorum(String::class.java) { private val lock = ReentrantLock() private var resultValue = 0L diff --git a/src/main/kotlin/io/emeraldpay/dshackle/quorum/ValueAwareQuorum.kt b/src/main/kotlin/io/emeraldpay/dshackle/quorum/ValueAwareQuorum.kt index 3bed9837..938dd8ef 100644 --- a/src/main/kotlin/io/emeraldpay/dshackle/quorum/ValueAwareQuorum.kt +++ b/src/main/kotlin/io/emeraldpay/dshackle/quorum/ValueAwareQuorum.kt @@ -17,20 +17,20 @@ package io.emeraldpay.dshackle.quorum import com.fasterxml.jackson.databind.ObjectMapper +import io.emeraldpay.dshackle.Global import io.emeraldpay.dshackle.upstream.Upstream import io.infinitape.etherjar.rpc.JacksonRpcConverter import io.infinitape.etherjar.rpc.RpcException import org.slf4j.LoggerFactory abstract class ValueAwareQuorum( - val objectMapper: ObjectMapper, val clazz: Class ): CallQuorum { private val log = LoggerFactory.getLogger(ValueAwareQuorum::class.java) fun extractValue(response: ByteArray, clazz: Class): T? { - return objectMapper.readValue(response.inputStream(), clazz) + return Global.objectMapper.readValue(response.inputStream(), clazz) } override fun record(response: ByteArray, upstream: Upstream): Boolean { diff --git a/src/main/kotlin/io/emeraldpay/dshackle/rpc/NativeCall.kt b/src/main/kotlin/io/emeraldpay/dshackle/rpc/NativeCall.kt index 078f8a66..c7e9504e 100644 --- a/src/main/kotlin/io/emeraldpay/dshackle/rpc/NativeCall.kt +++ b/src/main/kotlin/io/emeraldpay/dshackle/rpc/NativeCall.kt @@ -20,6 +20,7 @@ import com.fasterxml.jackson.databind.ObjectMapper import com.google.protobuf.ByteString import io.emeraldpay.api.proto.BlockchainOuterClass import io.emeraldpay.dshackle.BlockchainType +import io.emeraldpay.dshackle.Global import io.emeraldpay.dshackle.SilentException import io.emeraldpay.dshackle.upstream.* import io.emeraldpay.dshackle.quorum.AlwaysQuorum @@ -42,11 +43,11 @@ import java.lang.Exception @Service open class NativeCall( - @Autowired private val multistreamHolder: MultistreamHolder, - @Autowired private val objectMapper: ObjectMapper + @Autowired private val multistreamHolder: MultistreamHolder ) { private val log = LoggerFactory.getLogger(NativeCall::class.java) + private val objectMapper: ObjectMapper = Global.objectMapper var quorumReaderFactory: QuorumReaderFactory = QuorumReaderFactory.default() diff --git a/src/main/kotlin/io/emeraldpay/dshackle/startup/ConfiguredUpstreams.kt b/src/main/kotlin/io/emeraldpay/dshackle/startup/ConfiguredUpstreams.kt index 8a62f67b..b3cf793b 100644 --- a/src/main/kotlin/io/emeraldpay/dshackle/startup/ConfiguredUpstreams.kt +++ b/src/main/kotlin/io/emeraldpay/dshackle/startup/ConfiguredUpstreams.kt @@ -19,6 +19,7 @@ package io.emeraldpay.dshackle.startup import com.fasterxml.jackson.databind.ObjectMapper import io.emeraldpay.dshackle.BlockchainType import io.emeraldpay.dshackle.FileResolver +import io.emeraldpay.dshackle.Global import io.emeraldpay.dshackle.cache.CachesFactory import io.emeraldpay.dshackle.config.UpstreamsConfig import io.emeraldpay.dshackle.reader.Reader @@ -44,7 +45,6 @@ import kotlin.collections.HashMap @Repository open class ConfiguredUpstreams( - @Autowired private val objectMapper: ObjectMapper, @Autowired private val currentUpstreams: CurrentMultistreamHolder, @Autowired private val fileResolver: FileResolver, @Autowired private val config: UpstreamsConfig, @@ -146,7 +146,7 @@ open class ConfiguredUpstreams( val upstream = BitcoinUpstream(config.id ?: "bitcoin-${seq.getAndIncrement()}", chain, directApi, options, QuorumForLabels.QuorumItem(1, config.labels), - objectMapper, methods) + methods) upstream.start() currentUpstreams.update(UpstreamChange(chain, upstream, UpstreamChange.ChangeType.ADDED)) @@ -171,8 +171,7 @@ open class ConfiguredUpstreams( val wsFactoryApi: EthereumWsFactory? = conn.ws?.let { endpoint -> val wsApi = EthereumWsFactory( endpoint.url, - endpoint.origin ?: URI("http://localhost"), - objectMapper + endpoint.origin ?: URI("http://localhost") ) endpoint.basicAuth?.let { auth -> wsApi.basicAuth = auth @@ -186,8 +185,7 @@ open class ConfiguredUpstreams( config.id!!, chain, directApi, wsFactoryApi, options, QuorumForLabels.QuorumItem(1, config.labels), - methods, - objectMapper + methods ) ethereumUpstream.start() currentUpstreams.update(UpstreamChange(chain, ethereumUpstream, UpstreamChange.ChangeType.ADDED)) @@ -199,7 +197,6 @@ open class ConfiguredUpstreams( config.id!!, endpoint.host!!, endpoint.port ?: 2449, - objectMapper, endpoint.auth, fileResolver ).apply { @@ -225,7 +222,6 @@ open class ConfiguredUpstreams( urls.add(endpoint.url) JsonRpcHttpClient( endpoint.url.toString(), - objectMapper, conn.rpc?.basicAuth, tls ) diff --git a/src/main/kotlin/io/emeraldpay/dshackle/upstream/CurrentMultistreamHolder.kt b/src/main/kotlin/io/emeraldpay/dshackle/upstream/CurrentMultistreamHolder.kt index efe076e6..d8ffaed1 100644 --- a/src/main/kotlin/io/emeraldpay/dshackle/upstream/CurrentMultistreamHolder.kt +++ b/src/main/kotlin/io/emeraldpay/dshackle/upstream/CurrentMultistreamHolder.kt @@ -18,6 +18,7 @@ package io.emeraldpay.dshackle.upstream import com.fasterxml.jackson.databind.ObjectMapper import io.emeraldpay.dshackle.BlockchainType +import io.emeraldpay.dshackle.Global import io.emeraldpay.dshackle.cache.CachesEnabled import io.emeraldpay.dshackle.cache.CachesFactory import io.emeraldpay.dshackle.startup.UpstreamChange @@ -43,12 +44,13 @@ import kotlin.concurrent.withLock @Repository class CurrentMultistreamHolder( - @Autowired private val objectMapper: ObjectMapper, @Autowired private val cachesFactory: CachesFactory ) : MultistreamHolder { private val log = LoggerFactory.getLogger(CurrentMultistreamHolder::class.java) + private val objectMapper: ObjectMapper = Global.objectMapper + private val chainMapping = ConcurrentHashMap() private val chainsBus = TopicProcessor.create() private val callTargets = HashMap() @@ -62,7 +64,7 @@ class CurrentMultistreamHolder( val up = change.upstream.cast(EthereumUpstream::class.java) val current = chainMapping[chain] as Multistream? val factory = Callable { - EthereumMultistream(chain, ArrayList(), cachesFactory.getCaches(chain), objectMapper) as Multistream + EthereumMultistream(chain, ArrayList(), cachesFactory.getCaches(chain)) as Multistream } processUpdate(change, up, current, factory) } @@ -70,7 +72,7 @@ class CurrentMultistreamHolder( val up = change.upstream.cast(BitcoinUpstream::class.java) val current = chainMapping[chain] as Multistream? val factory = Callable { - BitcoinMultistream(chain, ArrayList(), cachesFactory.getCaches(chain), objectMapper) as Multistream + BitcoinMultistream(chain, ArrayList(), cachesFactory.getCaches(chain)) as Multistream } processUpdate(change, up, current, factory) } @@ -135,8 +137,8 @@ class CurrentMultistreamHolder( fun setupDefaultMethods(chain: Chain): CallMethods { val created = when (BlockchainType.fromBlockchain(chain)) { - BlockchainType.ETHEREUM -> DefaultEthereumMethods(objectMapper, chain) - BlockchainType.BITCOIN -> DefaultBitcoinMethods(objectMapper) + BlockchainType.ETHEREUM -> DefaultEthereumMethods(chain) + BlockchainType.BITCOIN -> DefaultBitcoinMethods() else -> throw IllegalStateException("Unsupported chain: $chain") } callTargets[chain] = created diff --git a/src/main/kotlin/io/emeraldpay/dshackle/upstream/bitcoin/BitcoinMultistream.kt b/src/main/kotlin/io/emeraldpay/dshackle/upstream/bitcoin/BitcoinMultistream.kt index 6a75e7d9..f2652534 100644 --- a/src/main/kotlin/io/emeraldpay/dshackle/upstream/bitcoin/BitcoinMultistream.kt +++ b/src/main/kotlin/io/emeraldpay/dshackle/upstream/bitcoin/BitcoinMultistream.kt @@ -31,8 +31,7 @@ import reactor.core.publisher.Mono open class BitcoinMultistream( chain: Chain, val upstreams: MutableList, - caches: Caches, - private val objectMapper: ObjectMapper + caches: Caches ) : Multistream(chain, upstreams as MutableList, caches), Lifecycle { companion object { @@ -40,7 +39,7 @@ open class BitcoinMultistream( } private var head: Head? = null - private var reader = BitcoinReader(this, EmptyHead(), objectMapper) + private var reader = BitcoinReader(this, EmptyHead()) override fun init() { if (upstreams.size > 0) { @@ -84,7 +83,7 @@ open class BitcoinMultistream( override fun setHead(head: Head) { this.head = head - reader = BitcoinReader(this, head, objectMapper) + reader = BitcoinReader(this, head) } override fun getHead(): Head { diff --git a/src/main/kotlin/io/emeraldpay/dshackle/upstream/bitcoin/BitcoinReader.kt b/src/main/kotlin/io/emeraldpay/dshackle/upstream/bitcoin/BitcoinReader.kt index 86f5ad3d..e0b58aeb 100644 --- a/src/main/kotlin/io/emeraldpay/dshackle/upstream/bitcoin/BitcoinReader.kt +++ b/src/main/kotlin/io/emeraldpay/dshackle/upstream/bitcoin/BitcoinReader.kt @@ -16,6 +16,7 @@ package io.emeraldpay.dshackle.upstream.bitcoin import com.fasterxml.jackson.databind.ObjectMapper +import io.emeraldpay.dshackle.Global import io.emeraldpay.dshackle.upstream.Head import io.emeraldpay.dshackle.upstream.Selector import io.emeraldpay.dshackle.upstream.rpcclient.JsonRpcRequest @@ -27,15 +28,15 @@ import reactor.kotlin.core.publisher.cast open class BitcoinReader( private val upstreams: BitcoinMultistream, - head: Head, - private val objectMapper: ObjectMapper + head: Head ) : Lifecycle { companion object { private val log = LoggerFactory.getLogger(BitcoinReader::class.java) } - private val mempool = CachingMempoolData(upstreams, head, objectMapper) + private val objectMapper: ObjectMapper = Global.objectMapper + private val mempool = CachingMempoolData(upstreams, head) open fun getMempool(): CachingMempoolData { return mempool diff --git a/src/main/kotlin/io/emeraldpay/dshackle/upstream/bitcoin/BitcoinUpstream.kt b/src/main/kotlin/io/emeraldpay/dshackle/upstream/bitcoin/BitcoinUpstream.kt index 30a46bb3..b657a0c6 100644 --- a/src/main/kotlin/io/emeraldpay/dshackle/upstream/bitcoin/BitcoinUpstream.kt +++ b/src/main/kotlin/io/emeraldpay/dshackle/upstream/bitcoin/BitcoinUpstream.kt @@ -35,7 +35,6 @@ open class BitcoinUpstream( private val directApi: Reader, options: UpstreamsConfig.Options, val node: QuorumForLabels.QuorumItem, - private val objectMapper: ObjectMapper, callMethods: CallMethods ) : DefaultUpstream(id, options, callMethods), Lifecycle { @@ -49,7 +48,7 @@ open class BitcoinUpstream( private fun createHead(): Head { return BitcoinRpcHead( directApi, - ExtractBlock(objectMapper) + ExtractBlock() ) } diff --git a/src/main/kotlin/io/emeraldpay/dshackle/upstream/bitcoin/CachingMempoolData.kt b/src/main/kotlin/io/emeraldpay/dshackle/upstream/bitcoin/CachingMempoolData.kt index d18356ba..a22fa9c7 100644 --- a/src/main/kotlin/io/emeraldpay/dshackle/upstream/bitcoin/CachingMempoolData.kt +++ b/src/main/kotlin/io/emeraldpay/dshackle/upstream/bitcoin/CachingMempoolData.kt @@ -16,6 +16,7 @@ package io.emeraldpay.dshackle.upstream.bitcoin import com.fasterxml.jackson.databind.ObjectMapper +import io.emeraldpay.dshackle.Global import io.emeraldpay.dshackle.upstream.Head import io.emeraldpay.dshackle.upstream.Selector import io.emeraldpay.dshackle.upstream.rpcclient.JsonRpcRequest @@ -31,8 +32,7 @@ import java.util.concurrent.locks.ReentrantLock open class CachingMempoolData( private val upstreams: BitcoinMultistream, - private val head: Head, - private val objectMapper: ObjectMapper + private val head: Head ) : Lifecycle { companion object { @@ -40,6 +40,8 @@ open class CachingMempoolData( private val TTL = Duration.ofSeconds(15) } + private val objectMapper: ObjectMapper = Global.objectMapper + private val current = AtomicReference(Container.empty()) private val updateLock = ReentrantLock() private var headListener: Disposable? = null diff --git a/src/main/kotlin/io/emeraldpay/dshackle/upstream/bitcoin/ExtractBlock.kt b/src/main/kotlin/io/emeraldpay/dshackle/upstream/bitcoin/ExtractBlock.kt index daa3dab5..dfdeb421 100644 --- a/src/main/kotlin/io/emeraldpay/dshackle/upstream/bitcoin/ExtractBlock.kt +++ b/src/main/kotlin/io/emeraldpay/dshackle/upstream/bitcoin/ExtractBlock.kt @@ -16,6 +16,7 @@ package io.emeraldpay.dshackle.upstream.bitcoin import com.fasterxml.jackson.databind.ObjectMapper +import io.emeraldpay.dshackle.Global import io.emeraldpay.dshackle.data.BlockContainer import io.emeraldpay.dshackle.data.BlockId import io.emeraldpay.dshackle.data.TxId @@ -24,9 +25,7 @@ import org.slf4j.LoggerFactory import java.math.BigInteger import java.time.Instant -class ExtractBlock( - private val objectMapper: ObjectMapper -) { +class ExtractBlock() { companion object { private val log = LoggerFactory.getLogger(ExtractBlock::class.java) @@ -50,6 +49,8 @@ class ExtractBlock( } } + private val objectMapper: ObjectMapper = Global.objectMapper + fun extract(json: ByteArray): BlockContainer { val data = objectMapper.readValue(json, Map::class.java) as Map diff --git a/src/main/kotlin/io/emeraldpay/dshackle/upstream/calls/DefaultBitcoinMethods.kt b/src/main/kotlin/io/emeraldpay/dshackle/upstream/calls/DefaultBitcoinMethods.kt index 4308041a..79172776 100644 --- a/src/main/kotlin/io/emeraldpay/dshackle/upstream/calls/DefaultBitcoinMethods.kt +++ b/src/main/kotlin/io/emeraldpay/dshackle/upstream/calls/DefaultBitcoinMethods.kt @@ -20,9 +20,7 @@ import io.emeraldpay.dshackle.quorum.* import io.infinitape.etherjar.rpc.RpcException import java.util.* -class DefaultBitcoinMethods( - private val objectMapper: ObjectMapper -) : CallMethods { +class DefaultBitcoinMethods() : CallMethods { private val anyResponseMethods = listOf( "getblock", @@ -50,7 +48,7 @@ class DefaultBitcoinMethods( Collections.binarySearch(hardcodedMethods, method) >= 0 -> AlwaysQuorum() Collections.binarySearch(anyResponseMethods, method) >= 0 -> NotLaggingQuorum(2) Collections.binarySearch(headVerifiedMethods, method) >= 0 -> NotLaggingQuorum(0) - Collections.binarySearch(broadcastMethods, method) >= 0 -> BroadcastQuorum(objectMapper) + Collections.binarySearch(broadcastMethods, method) >= 0 -> BroadcastQuorum() else -> AlwaysQuorum() } } diff --git a/src/main/kotlin/io/emeraldpay/dshackle/upstream/calls/DefaultEthereumMethods.kt b/src/main/kotlin/io/emeraldpay/dshackle/upstream/calls/DefaultEthereumMethods.kt index 3eca4c3c..4365e3ab 100644 --- a/src/main/kotlin/io/emeraldpay/dshackle/upstream/calls/DefaultEthereumMethods.kt +++ b/src/main/kotlin/io/emeraldpay/dshackle/upstream/calls/DefaultEthereumMethods.kt @@ -28,7 +28,6 @@ import java.util.* * hardcoded results for base methods, such as `net_version`, `web3_clientVersion` and similar */ class DefaultEthereumMethods( - private val objectMapper: ObjectMapper, private val chain: Chain ) : CallMethods { @@ -88,9 +87,9 @@ class DefaultEthereumMethods( headVerifiedMethods.contains(method) -> NotLaggingQuorum(1) specialMethods.contains(method) -> { when (method) { - "eth_getTransactionCount" -> NonceQuorum(objectMapper) + "eth_getTransactionCount" -> NonceQuorum() "eth_getBalance" -> NotLaggingQuorum(1) - "eth_sendRawTransaction" -> BroadcastQuorum(objectMapper) + "eth_sendRawTransaction" -> BroadcastQuorum() else -> AlwaysQuorum() } } diff --git a/src/main/kotlin/io/emeraldpay/dshackle/upstream/ethereum/EthereumFullBlocksReader.kt b/src/main/kotlin/io/emeraldpay/dshackle/upstream/ethereum/EthereumFullBlocksReader.kt index 1db6bec6..b30ff025 100644 --- a/src/main/kotlin/io/emeraldpay/dshackle/upstream/ethereum/EthereumFullBlocksReader.kt +++ b/src/main/kotlin/io/emeraldpay/dshackle/upstream/ethereum/EthereumFullBlocksReader.kt @@ -16,6 +16,7 @@ package io.emeraldpay.dshackle.upstream.ethereum import com.fasterxml.jackson.databind.ObjectMapper +import io.emeraldpay.dshackle.Global import io.emeraldpay.dshackle.data.BlockContainer import io.emeraldpay.dshackle.data.BlockId import io.emeraldpay.dshackle.data.TxContainer @@ -37,7 +38,6 @@ import reactor.core.publisher.Mono * If any of the expected block transactions is not available it returns empty */ class EthereumFullBlocksReader( - private val objectMapper: ObjectMapper, private val blocks: Reader, private val txes: Reader ) : Reader { @@ -48,7 +48,7 @@ class EthereumFullBlocksReader( override fun read(key: BlockId): Mono { return blocks.read(key).flatMap { block -> - val block = objectMapper.readValue(block.json, BlockJson::class.java) as BlockJson + val block = Global.objectMapper.readValue(block.json, BlockJson::class.java) as BlockJson val fullBlock = if (block.transactions == null || block.transactions.isEmpty()) { // in fact it's not necessary to create a copy, made just for code clarity but it may be a performance loss val fullBlock = BlockJson() @@ -66,7 +66,7 @@ class EthereumFullBlocksReader( val fullBlock = BlockJson() BeanUtils.copyProperties(block, fullBlock) fullBlock.transactions = list.map { - objectMapper.readValue(it.json, TransactionJson::class.java) + Global.objectMapper.readValue(it.json, TransactionJson::class.java) } Mono.just(fullBlock) } @@ -75,7 +75,7 @@ class EthereumFullBlocksReader( fullBlock .map { block -> BlockContainer(block.number, BlockId.from(block.hash), block.totalDifficulty, block.timestamp, true, - objectMapper.writeValueAsBytes(block), + Global.objectMapper.writeValueAsBytes(block), block.transactions.map { tx -> TxId.from(tx) } ) } diff --git a/src/main/kotlin/io/emeraldpay/dshackle/upstream/ethereum/EthereumMultistream.kt b/src/main/kotlin/io/emeraldpay/dshackle/upstream/ethereum/EthereumMultistream.kt index c607b503..66e7a3eb 100644 --- a/src/main/kotlin/io/emeraldpay/dshackle/upstream/ethereum/EthereumMultistream.kt +++ b/src/main/kotlin/io/emeraldpay/dshackle/upstream/ethereum/EthereumMultistream.kt @@ -17,6 +17,7 @@ package io.emeraldpay.dshackle.upstream.ethereum import com.fasterxml.jackson.databind.ObjectMapper +import io.emeraldpay.dshackle.Global import io.emeraldpay.dshackle.cache.Caches import io.emeraldpay.dshackle.config.UpstreamsConfig import io.emeraldpay.dshackle.reader.Reader @@ -31,17 +32,17 @@ import reactor.core.publisher.Mono open class EthereumMultistream( chain: Chain, val upstreams: MutableList, - caches: Caches, - private val objectMapper: ObjectMapper + caches: Caches ) : Multistream(chain, upstreams as MutableList, caches) { companion object { private val log = LoggerFactory.getLogger(EthereumMultistream::class.java) } + private val objectMapper: ObjectMapper = Global.objectMapper private var head: Head? = null - private val reader: EthereumReader = EthereumReader(this, this.caches, objectMapper) + private val reader: EthereumReader = EthereumReader(this, this.caches) init { this.init() @@ -119,7 +120,7 @@ open class EthereumMultistream( } override fun getRoutedApi(matcher: Selector.Matcher): Mono> { - return Mono.just(NativeCallRouter(objectMapper, reader, getMethods())) + return Mono.just(NativeCallRouter(reader, getMethods())) } } \ No newline at end of file diff --git a/src/main/kotlin/io/emeraldpay/dshackle/upstream/ethereum/EthereumReader.kt b/src/main/kotlin/io/emeraldpay/dshackle/upstream/ethereum/EthereumReader.kt index 8dadd1a9..6c142b12 100644 --- a/src/main/kotlin/io/emeraldpay/dshackle/upstream/ethereum/EthereumReader.kt +++ b/src/main/kotlin/io/emeraldpay/dshackle/upstream/ethereum/EthereumReader.kt @@ -17,6 +17,7 @@ package io.emeraldpay.dshackle.upstream.ethereum import com.fasterxml.jackson.databind.ObjectMapper import io.emeraldpay.dshackle.Defaults +import io.emeraldpay.dshackle.Global import io.emeraldpay.dshackle.cache.Caches import io.emeraldpay.dshackle.cache.CurrentBlockCache import io.emeraldpay.dshackle.data.* @@ -46,14 +47,14 @@ import java.util.function.Function open class EthereumReader( private val up: Multistream, - private val caches: Caches, - private val objectMapper: ObjectMapper + private val caches: Caches ) : Lifecycle { companion object { private val log = LoggerFactory.getLogger(EthereumReader::class.java) } + private val objectMapper: ObjectMapper = Global.objectMapper private val balanceCache = CurrentBlockCache() val extractBlock = Function> { block -> @@ -80,10 +81,10 @@ open class EthereumReader( } val blockAsContainer = Function, BlockContainer> { block -> - BlockContainer.from(block.withoutTransactionDetails(), objectMapper) + BlockContainer.from(block.withoutTransactionDetails()) } val txAsContainer = Function { tx -> - TxContainer.from(tx, objectMapper) + TxContainer.from(tx) } private val blocksDirect: Reader @@ -150,9 +151,13 @@ open class EthereumReader( .timeout(Defaults.timeoutInternal, Mono.error(TimeoutException("Tx not read $key"))) .map(directResponseBytes) .retryWhen(Retry.backoff(3, Duration.ofSeconds(1))) - .map { txbytes -> + .flatMap { txbytes -> val tx = objectMapper.readValue(txbytes, TransactionJson::class.java) - TxContainer.from(tx, txbytes) + if (tx == null) { + Mono.empty() + } else { + Mono.just(TxContainer.from(tx, txbytes)) + } } .doOnNext { tx -> if (tx.blockId != null) { diff --git a/src/main/kotlin/io/emeraldpay/dshackle/upstream/ethereum/EthereumRpcHead.kt b/src/main/kotlin/io/emeraldpay/dshackle/upstream/ethereum/EthereumRpcHead.kt index 334ddf66..e0d1135d 100644 --- a/src/main/kotlin/io/emeraldpay/dshackle/upstream/ethereum/EthereumRpcHead.kt +++ b/src/main/kotlin/io/emeraldpay/dshackle/upstream/ethereum/EthereumRpcHead.kt @@ -35,8 +35,7 @@ import java.time.Duration import java.util.concurrent.Executors class EthereumRpcHead( - private val api: Reader, - private val objectMapper: ObjectMapper, + private val api: Reader, private val interval: Duration = Duration.ofSeconds(10) ): DefaultEthereumHead(), Lifecycle { @@ -72,7 +71,7 @@ class EthereumRpcHead( .timeout(Defaults.timeout, Mono.error(Exception("Block data not received"))) } .map { - BlockContainer.from(it.getResult(), objectMapper) + BlockContainer.from(it.getResult()) } .onErrorContinue { err, _ -> log.debug("RPC error ${err.message}") diff --git a/src/main/kotlin/io/emeraldpay/dshackle/upstream/ethereum/EthereumUpstream.kt b/src/main/kotlin/io/emeraldpay/dshackle/upstream/ethereum/EthereumUpstream.kt index 556757b2..e7773292 100644 --- a/src/main/kotlin/io/emeraldpay/dshackle/upstream/ethereum/EthereumUpstream.kt +++ b/src/main/kotlin/io/emeraldpay/dshackle/upstream/ethereum/EthereumUpstream.kt @@ -41,13 +41,12 @@ open class EthereumUpstream( private val ethereumWsFactory: EthereumWsFactory? = null, options: UpstreamsConfig.Options, val node: QuorumForLabels.QuorumItem, - targets: CallMethods, - private val objectMapper: ObjectMapper + targets: CallMethods ) : DefaultUpstream(id, options, targets), Upstream, CachesEnabled, Lifecycle { - constructor(id: String, chain: Chain, api: Reader, objectMapper: ObjectMapper) : this(id, chain, api, null, + constructor(id: String, chain: Chain, api: Reader) : this(id, chain, api, null, UpstreamsConfig.Options.getDefaults(), QuorumForLabels.QuorumItem(1, UpstreamsConfig.Labels()), - DirectCallMethods(), objectMapper) + DirectCallMethods()) private val log = LoggerFactory.getLogger(EthereumUpstream::class.java) @@ -68,7 +67,7 @@ open class EthereumUpstream( this.setLag(0) this.setStatus(UpstreamAvailability.OK) } else { - val validator = EthereumUpstreamValidator(this, getOptions(), objectMapper) + val validator = EthereumUpstreamValidator(this, getOptions()) validatorSubscription = validator.start() .subscribe(this::setStatus) } @@ -95,7 +94,7 @@ open class EthereumUpstream( start() } // receive bew blocks through WebSockets, but also periodically verify with RPC in case if WS failed - val rpcHead = EthereumRpcHead(getApi(), objectMapper, Duration.ofSeconds(60)).apply { + val rpcHead = EthereumRpcHead(getApi(), Duration.ofSeconds(60)).apply { start() } MergedHead(listOf(rpcHead, wsHead)).apply { @@ -103,7 +102,7 @@ open class EthereumUpstream( } } else { log.warn("Setting up upstream ${this.getId()} with RPC-only access, less effective than WS+RPC") - EthereumRpcHead(getApi(), objectMapper).apply { + EthereumRpcHead(getApi()).apply { start() } } 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 488dccb1..654afc39 100644 --- a/src/main/kotlin/io/emeraldpay/dshackle/upstream/ethereum/EthereumUpstreamValidator.kt +++ b/src/main/kotlin/io/emeraldpay/dshackle/upstream/ethereum/EthereumUpstreamValidator.kt @@ -18,6 +18,7 @@ package io.emeraldpay.dshackle.upstream.ethereum import com.fasterxml.jackson.databind.ObjectMapper import io.emeraldpay.dshackle.Defaults +import io.emeraldpay.dshackle.Global import io.emeraldpay.dshackle.config.UpstreamsConfig import io.emeraldpay.dshackle.upstream.UpstreamAvailability import io.emeraldpay.dshackle.upstream.rpcclient.JsonRpcRequest @@ -33,14 +34,15 @@ import java.util.concurrent.Executors class EthereumUpstreamValidator( private val upstream: EthereumUpstream, - private val options: UpstreamsConfig.Options, - private val objectMapper: ObjectMapper + private val options: UpstreamsConfig.Options ) { companion object { private val log = LoggerFactory.getLogger(EthereumUpstreamValidator::class.java) val scheduler = Schedulers.fromExecutor(Executors.newCachedThreadPool(CustomizableThreadFactory("ethereum-validator"))) } + private val objectMapper: ObjectMapper = Global.objectMapper + fun validate(): Mono { return upstream .getApi() diff --git a/src/main/kotlin/io/emeraldpay/dshackle/upstream/ethereum/EthereumWsFactory.kt b/src/main/kotlin/io/emeraldpay/dshackle/upstream/ethereum/EthereumWsFactory.kt index 8a54be99..295fbd56 100644 --- a/src/main/kotlin/io/emeraldpay/dshackle/upstream/ethereum/EthereumWsFactory.kt +++ b/src/main/kotlin/io/emeraldpay/dshackle/upstream/ethereum/EthereumWsFactory.kt @@ -18,6 +18,7 @@ package io.emeraldpay.dshackle.upstream.ethereum import com.fasterxml.jackson.databind.ObjectMapper import io.emeraldpay.dshackle.Defaults +import io.emeraldpay.dshackle.Global import io.emeraldpay.dshackle.SilentException import io.emeraldpay.dshackle.config.AuthConfig import io.emeraldpay.dshackle.data.BlockContainer @@ -37,21 +38,19 @@ import java.time.Duration class EthereumWsFactory( private val uri: URI, - private val origin: URI, - private val objectMapper: ObjectMapper + private val origin: URI ) { var basicAuth: AuthConfig.ClientBasicAuth? = null fun create(upstream: EthereumUpstream): EthereumWs { - return EthereumWs(uri, origin, upstream, objectMapper, basicAuth) + return EthereumWs(uri, origin, upstream, basicAuth) } class EthereumWs( private val uri: URI, private val origin: URI, private val upstream: EthereumUpstream, - private val objectMapper: ObjectMapper, private val basicAuth: AuthConfig.ClientBasicAuth? ) { @@ -96,7 +95,7 @@ class EthereumWsFactory( } } .flatMap(JsonRpcResponse::requireResult) - .map { BlockContainer.from(it, objectMapper) } + .map { BlockContainer.from(it) } }.repeatWhenEmpty { n -> Repeat.times(5) .exponentialBackoff(Duration.ofMillis(50), Duration.ofMillis(500)) @@ -107,7 +106,7 @@ class EthereumWsFactory( .subscribe(topic::onNext) } else { - topic.onNext(BlockContainer.from(block, objectMapper)) + topic.onNext(BlockContainer.from(block)) } } diff --git a/src/main/kotlin/io/emeraldpay/dshackle/upstream/ethereum/NativeCallRouter.kt b/src/main/kotlin/io/emeraldpay/dshackle/upstream/ethereum/NativeCallRouter.kt index 52d3f2f7..72c0046f 100644 --- a/src/main/kotlin/io/emeraldpay/dshackle/upstream/ethereum/NativeCallRouter.kt +++ b/src/main/kotlin/io/emeraldpay/dshackle/upstream/ethereum/NativeCallRouter.kt @@ -16,6 +16,7 @@ package io.emeraldpay.dshackle.upstream.ethereum import com.fasterxml.jackson.databind.ObjectMapper +import io.emeraldpay.dshackle.Global import io.emeraldpay.dshackle.data.BlockId import io.emeraldpay.dshackle.data.TxId import io.emeraldpay.dshackle.reader.Reader @@ -30,7 +31,6 @@ import reactor.core.publisher.Mono import java.math.BigInteger class NativeCallRouter( - private val objectMapper: ObjectMapper, private val reader: EthereumReader, private val methods: CallMethods ) : Reader { @@ -40,7 +40,6 @@ class NativeCallRouter( } private val fullBlocksReader = EthereumFullBlocksReader( - objectMapper, reader.blocksByIdAsCont(), reader.txByHashAsCont() ) diff --git a/src/main/kotlin/io/emeraldpay/dshackle/upstream/grpc/EthereumGrpcUpstream.kt b/src/main/kotlin/io/emeraldpay/dshackle/upstream/grpc/EthereumGrpcUpstream.kt index 18bd641d..d473b4ce 100644 --- a/src/main/kotlin/io/emeraldpay/dshackle/upstream/grpc/EthereumGrpcUpstream.kt +++ b/src/main/kotlin/io/emeraldpay/dshackle/upstream/grpc/EthereumGrpcUpstream.kt @@ -56,7 +56,6 @@ open class EthereumGrpcUpstream( private val parentId: String, private val chain: Chain, private val blockchainStub: ReactorBlockchainGrpc.ReactorBlockchainStub, - private val objectMapper: ObjectMapper, private val client: JsonRpcGrpcClient ) : DefaultUpstream( "$parentId/${chain.chainCode}", @@ -125,7 +124,7 @@ open class EthereumGrpcUpstream( defaultReader.read(JsonRpcRequest("eth_getBlockByHash", listOf(it.hash.toHexWithPrefix(), false))) .flatMap(JsonRpcResponse::requireResult) .map { - BlockContainer.from(it, objectMapper) + BlockContainer.from(it) } .timeout(timeout, Mono.error(TimeoutException("Timeout from upstream"))) .doOnError { t -> diff --git a/src/main/kotlin/io/emeraldpay/dshackle/upstream/grpc/GrpcUpstreams.kt b/src/main/kotlin/io/emeraldpay/dshackle/upstream/grpc/GrpcUpstreams.kt index 4e0e3934..d7b4f7c9 100644 --- a/src/main/kotlin/io/emeraldpay/dshackle/upstream/grpc/GrpcUpstreams.kt +++ b/src/main/kotlin/io/emeraldpay/dshackle/upstream/grpc/GrpcUpstreams.kt @@ -46,7 +46,6 @@ class GrpcUpstreams( private val id: String, private val host: String, private val port: Int, - private val objectMapper: ObjectMapper, private val auth: AuthConfig.ClientTlsAuth? = null, private val fileResolver: FileResolver ) { @@ -157,8 +156,8 @@ class GrpcUpstreams( lock.withLock { val current = known[chain] return if (current == null) { - val rpcClient = JsonRpcGrpcClient(client!!, chain, objectMapper) - val created = EthereumGrpcUpstream(id, chain, client!!, objectMapper, rpcClient) + val rpcClient = JsonRpcGrpcClient(client!!, chain) + val created = EthereumGrpcUpstream(id, chain, client!!, rpcClient) created.timeout = this.timeout known[chain] = created created.start() diff --git a/src/main/kotlin/io/emeraldpay/dshackle/upstream/rpcclient/JsonRpcGrpcClient.kt b/src/main/kotlin/io/emeraldpay/dshackle/upstream/rpcclient/JsonRpcGrpcClient.kt index a2ba1fe2..a2fc023e 100644 --- a/src/main/kotlin/io/emeraldpay/dshackle/upstream/rpcclient/JsonRpcGrpcClient.kt +++ b/src/main/kotlin/io/emeraldpay/dshackle/upstream/rpcclient/JsonRpcGrpcClient.kt @@ -19,6 +19,7 @@ import com.fasterxml.jackson.databind.ObjectMapper import com.google.protobuf.ByteString import io.emeraldpay.api.proto.BlockchainOuterClass import io.emeraldpay.api.proto.ReactorBlockchainGrpc +import io.emeraldpay.dshackle.Global import io.emeraldpay.dshackle.reader.Reader import io.emeraldpay.dshackle.upstream.Selector import io.emeraldpay.grpc.Chain @@ -30,8 +31,7 @@ import reactor.core.publisher.Mono class JsonRpcGrpcClient( private val stub: ReactorBlockchainGrpc.ReactorBlockchainStub, - private val chain: Chain, - private val objectMapper: ObjectMapper + private val chain: Chain ) { companion object { @@ -39,14 +39,13 @@ class JsonRpcGrpcClient( } fun forSelector(matcher: Selector.Matcher): Reader { - return Executor(stub, chain, matcher, objectMapper) + return Executor(stub, chain, matcher) } class Executor( private val stub: ReactorBlockchainGrpc.ReactorBlockchainStub, private val chain: Chain, - private val matcher: Selector.Matcher, - private val objectMapper: ObjectMapper + private val matcher: Selector.Matcher ) : Reader { private val parser = JsonRpcParser() @@ -64,7 +63,7 @@ class JsonRpcGrpcClient( BlockchainOuterClass.NativeCallItem.newBuilder() .setId(1) .setMethod(key.method) - .setPayload(ByteString.copyFrom(objectMapper.writeValueAsBytes(key.params))) + .setPayload(ByteString.copyFrom(Global.objectMapper.writeValueAsBytes(key.params))) .build().let { req.addItems(it) } diff --git a/src/main/kotlin/io/emeraldpay/dshackle/upstream/rpcclient/JsonRpcHttpClient.kt b/src/main/kotlin/io/emeraldpay/dshackle/upstream/rpcclient/JsonRpcHttpClient.kt index 22e2f843..70ab0ae5 100644 --- a/src/main/kotlin/io/emeraldpay/dshackle/upstream/rpcclient/JsonRpcHttpClient.kt +++ b/src/main/kotlin/io/emeraldpay/dshackle/upstream/rpcclient/JsonRpcHttpClient.kt @@ -38,7 +38,6 @@ import java.util.function.Consumer */ class JsonRpcHttpClient( private val target: String, - private val objectMapper: ObjectMapper, basicAuth: AuthConfig.ClientBasicAuth? = null, tlsCAAuth: ByteArray? = null ) : Reader { @@ -94,7 +93,7 @@ class JsonRpcHttpClient( override fun read(key: JsonRpcRequest): Mono { return Mono.just(key) - .map { it.toJson(objectMapper) } + .map(JsonRpcRequest::toJson) .flatMap(this@JsonRpcHttpClient::execute) .map(parser::parse) } diff --git a/src/main/kotlin/io/emeraldpay/dshackle/upstream/rpcclient/JsonRpcRequest.kt b/src/main/kotlin/io/emeraldpay/dshackle/upstream/rpcclient/JsonRpcRequest.kt index d8fd2d83..d49e168d 100644 --- a/src/main/kotlin/io/emeraldpay/dshackle/upstream/rpcclient/JsonRpcRequest.kt +++ b/src/main/kotlin/io/emeraldpay/dshackle/upstream/rpcclient/JsonRpcRequest.kt @@ -15,21 +15,21 @@ */ package io.emeraldpay.dshackle.upstream.rpcclient -import com.fasterxml.jackson.databind.ObjectMapper +import io.emeraldpay.dshackle.Global class JsonRpcRequest( val method: String, val params: List ) { - fun toJson(objectMapper: ObjectMapper): ByteArray { + fun toJson(): ByteArray { val json = mapOf( "jsonrpc" to "2.0", "id" to 1, "method" to method, "params" to params ) - return objectMapper.writeValueAsBytes(json) + return Global.objectMapper.writeValueAsBytes(json) } override fun equals(other: Any?): Boolean { diff --git a/src/test/groovy/io/emeraldpay/dshackle/cache/BlockByHeightSpec.groovy b/src/test/groovy/io/emeraldpay/dshackle/cache/BlockByHeightSpec.groovy index db1ffc1c..6e53ffa4 100644 --- a/src/test/groovy/io/emeraldpay/dshackle/cache/BlockByHeightSpec.groovy +++ b/src/test/groovy/io/emeraldpay/dshackle/cache/BlockByHeightSpec.groovy @@ -16,6 +16,7 @@ package io.emeraldpay.dshackle.cache import com.fasterxml.jackson.databind.ObjectMapper +import io.emeraldpay.dshackle.Global import io.emeraldpay.dshackle.data.BlockContainer import io.emeraldpay.dshackle.test.TestingCommons import io.infinitape.etherjar.domain.BlockHash @@ -31,7 +32,7 @@ class BlockByHeightSpec extends Specification { String hash1 = "0xd3f34def3c56ba4e701540d15edaff9acd2a1c968a7ff83b3300ab5dfd5f6aab" String hash2 = "0x4aabdaff9acd2f30d15e00ab5dfd5f6c56ba4ea1c968a7ff8d3f34de70153b33" - ObjectMapper objectMapper = TestingCommons.objectMapper() + ObjectMapper objectMapper = Global.objectMapper def "Fetch with all data available"() { setup: @@ -46,7 +47,7 @@ class BlockByHeightSpec extends Specification { block.uncles = [] block.transactions = [] - BlockContainer.from(block, objectMapper).with { + BlockContainer.from(block).with { blocks.add(it) heights.add(it) } @@ -81,11 +82,11 @@ class BlockByHeightSpec extends Specification { block2.transactions = [] - BlockContainer.from(block1, objectMapper).with { + BlockContainer.from(block1).with { blocks.add(it) heights.add(it) } - BlockContainer.from(block2, objectMapper).with { + BlockContainer.from(block2).with { blocks.add(it) heights.add(it) } @@ -124,11 +125,11 @@ class BlockByHeightSpec extends Specification { block2.uncles = [] block2.transactions = [] - BlockContainer.from(block1, objectMapper).with { + BlockContainer.from(block1).with { blocks.add(it) heights.add(it) } - BlockContainer.from(block2, objectMapper).with { + BlockContainer.from(block2).with { blocks.add(it) heights.add(it) } @@ -153,7 +154,7 @@ class BlockByHeightSpec extends Specification { block.timestamp = Instant.now() // add only to heights - BlockContainer.from(block, objectMapper).with { + BlockContainer.from(block).with { heights.add(it) } @@ -177,7 +178,7 @@ class BlockByHeightSpec extends Specification { block.timestamp = Instant.now() // add only to blocks - BlockContainer.from(block, objectMapper).with { + BlockContainer.from(block).with { blocks.add(it) } diff --git a/src/test/groovy/io/emeraldpay/dshackle/cache/BlocksMemCacheSpec.groovy b/src/test/groovy/io/emeraldpay/dshackle/cache/BlocksMemCacheSpec.groovy index d9744e81..fadeeebb 100644 --- a/src/test/groovy/io/emeraldpay/dshackle/cache/BlocksMemCacheSpec.groovy +++ b/src/test/groovy/io/emeraldpay/dshackle/cache/BlocksMemCacheSpec.groovy @@ -17,6 +17,7 @@ package io.emeraldpay.dshackle.cache import com.fasterxml.jackson.databind.ObjectMapper +import io.emeraldpay.dshackle.Global import io.emeraldpay.dshackle.data.BlockContainer import io.emeraldpay.dshackle.data.BlockId import io.emeraldpay.dshackle.test.TestingCommons @@ -35,8 +36,6 @@ class BlocksMemCacheSpec extends Specification { String hash3 = "0x40d15edaff9acdabd2a1c96fd5f683b3300aad34e7015f34def3c56ba8a7ffb5" String hash4 = "0xa4e7a75dfd5f6a83b3304dc56bfa0abfd3fef01540d15edafc9683f9acd2a13b" - ObjectMapper objectMapper = TestingCommons.objectMapper() - def "Add and read"() { setup: def cache = new BlocksMemCache() @@ -49,10 +48,10 @@ class BlocksMemCacheSpec extends Specification { block.transactions = [] when: - cache.add(BlockContainer.from(block, objectMapper)) + cache.add(BlockContainer.from(block)) def act = cache.read(BlockId.from(hash1)).block() then: - objectMapper.readValue(act.json, BlockJson) == block + Global.objectMapper.readValue(act.json, BlockJson) == block } def "Keeps only configured amount"() { @@ -70,7 +69,7 @@ class BlocksMemCacheSpec extends Specification { block.uncles = [] block.transactions = [] - cache.add(BlockContainer.from(block, objectMapper)) + cache.add(BlockContainer.from(block)) } def act1 = cache.read(BlockId.from(hash1)).block() diff --git a/src/test/groovy/io/emeraldpay/dshackle/cache/BlocksRedisCacheSpec.groovy b/src/test/groovy/io/emeraldpay/dshackle/cache/BlocksRedisCacheSpec.groovy index cd01f207..531e6cf6 100644 --- a/src/test/groovy/io/emeraldpay/dshackle/cache/BlocksRedisCacheSpec.groovy +++ b/src/test/groovy/io/emeraldpay/dshackle/cache/BlocksRedisCacheSpec.groovy @@ -16,6 +16,7 @@ package io.emeraldpay.dshackle.cache import com.fasterxml.jackson.databind.ObjectMapper +import io.emeraldpay.dshackle.Global import io.emeraldpay.dshackle.data.BlockContainer import io.emeraldpay.dshackle.data.BlockId import io.emeraldpay.dshackle.data.TxId @@ -44,7 +45,7 @@ class BlocksRedisCacheSpec extends Specification { String hash3 = "0x40d15edaff9acdabd2a1c96fd5f683b3300aad34e7015f34def3c56ba8a7ffb5" String hash4 = "0xa4e7a75dfd5f6a83b3304dc56bfa0abfd3fef01540d15edafc9683f9acd2a13b" - ObjectMapper objectMapper = TestingCommons.objectMapper() + ObjectMapper objectMapper = Global.objectMapper def setup() { redis = IntegrationTestingCommons.redisConnection() @@ -94,7 +95,7 @@ class BlocksRedisCacheSpec extends Specification { block.uncles = [] when: - cache.add(BlockContainer.from(block, objectMapper)).subscribe() + cache.add(BlockContainer.from(block)).subscribe() def act = cache.read(BlockId.from(hash1)).block() then: act != null @@ -112,7 +113,7 @@ class BlocksRedisCacheSpec extends Specification { block.uncles = [] when: - cache.add(BlockContainer.from(block, objectMapper)).subscribe() + cache.add(BlockContainer.from(block)).subscribe() def act = cache.read(BlockId.from(hash2)).block() then: objectMapper.readValue(act.json, BlockJson) == block @@ -136,7 +137,7 @@ class BlocksRedisCacheSpec extends Specification { block.uncles = [] when: - cache.add(BlockContainer.from(block, objectMapper)).subscribe() + cache.add(BlockContainer.from(block)).subscribe() def act = cache.read(BlockId.from(hash2)).block() then: act != null diff --git a/src/test/groovy/io/emeraldpay/dshackle/cache/CachesSpec.groovy b/src/test/groovy/io/emeraldpay/dshackle/cache/CachesSpec.groovy index 1f80a6a3..510d0835 100644 --- a/src/test/groovy/io/emeraldpay/dshackle/cache/CachesSpec.groovy +++ b/src/test/groovy/io/emeraldpay/dshackle/cache/CachesSpec.groovy @@ -16,6 +16,7 @@ package io.emeraldpay.dshackle.cache import com.fasterxml.jackson.databind.ObjectMapper +import io.emeraldpay.dshackle.Global import io.emeraldpay.dshackle.data.BlockContainer import io.emeraldpay.dshackle.data.TxContainer import io.emeraldpay.dshackle.test.TestingCommons @@ -33,15 +34,12 @@ class CachesSpec extends Specification { String hash1 = "0xd3f34def3c56ba4e701540d15edaff9acd2a1c968a7ff83b3300ab5dfd5f6aab" String hash2 = "0x4aabdaff9acd2f30d15e00ab5dfd5f6c56ba4ea1c968a7ff8d3f34de70153b33" - ObjectMapper objectMapper = TestingCommons.objectMapper() - def "Evict txes if block updated"() { setup: TxMemCache txCache = Mock() HeightCache heightCache = Mock() BlocksMemCache blocksCache = Mock() def caches = Caches.newBuilder() - .setObjectMapper(objectMapper) .setTxByHash(txCache) .setBlockByHeight(heightCache) .setBlockByHash(blocksCache) @@ -53,7 +51,7 @@ class CachesSpec extends Specification { block1.totalDifficulty = BigInteger.ONE block1.timestamp = Instant.now() block1.transactions = [] - block1 = BlockContainer.from(block1, objectMapper) + block1 = BlockContainer.from(block1) def block2 = new BlockJson() block2.number = 100 @@ -61,7 +59,7 @@ class CachesSpec extends Specification { block2.totalDifficulty = BigInteger.ONE block2.timestamp = Instant.now() block2.transactions = [] - block2 = BlockContainer.from(block2, objectMapper) + block2 = BlockContainer.from(block2) when: caches.cache(Caches.Tag.LATEST, block1) @@ -84,7 +82,6 @@ class CachesSpec extends Specification { HeightCache heightCache = Mock() BlocksMemCache blocksCache = Mock() def caches = Caches.newBuilder() - .setObjectMapper(objectMapper) .setTxByHash(txCache) .setBlockByHeight(heightCache) .setBlockByHash(blocksCache) @@ -95,14 +92,14 @@ class CachesSpec extends Specification { block1.hash = BlockHash.from(hash1) block1.totalDifficulty = BigInteger.ONE block1.timestamp = Instant.now() - block1 = BlockContainer.from(block1, objectMapper) + block1 = BlockContainer.from(block1) def block2 = new BlockJson() block2.number = 100 block2.hash = BlockHash.from(hash2) block2.totalDifficulty = BigInteger.ONE block2.timestamp = Instant.now() - block2 = BlockContainer.from(block2, objectMapper) + block2 = BlockContainer.from(block2) when: caches.cache(Caches.Tag.LATEST, block1) @@ -125,7 +122,6 @@ class CachesSpec extends Specification { HeightCache heightCache = Mock() BlocksMemCache blocksCache = Mock() def caches = Caches.newBuilder() - .setObjectMapper(TestingCommons.objectMapper()) .setTxByHash(txCache) .setBlockByHeight(heightCache) .setBlockByHash(blocksCache) @@ -142,7 +138,7 @@ class CachesSpec extends Specification { ] when: - caches.cache(Caches.Tag.REQUESTED, BlockContainer.from(block, objectMapper)) + caches.cache(Caches.Tag.REQUESTED, BlockContainer.from(block)) then: 0 * txCache.add(_) } @@ -153,7 +149,6 @@ class CachesSpec extends Specification { HeightCache heightCache = Mock() BlocksMemCache blocksCache = Mock() def caches = Caches.newBuilder() - .setObjectMapper(TestingCommons.objectMapper()) .setTxByHash(txCache) .setBlockByHeight(heightCache) .setBlockByHash(blocksCache) @@ -179,12 +174,12 @@ class CachesSpec extends Specification { block.totalDifficulty = BigInteger.ONE block.transactions = [tx1, tx2] block.timestamp = Instant.now() - block = BlockContainer.from(block, objectMapper) + block = BlockContainer.from(block) when: caches.cache(Caches.Tag.REQUESTED, block) then: - 1 * txCache.add(TxContainer.from(tx1, objectMapper)) - 1 * txCache.add(TxContainer.from(tx2, objectMapper)) + 1 * txCache.add(TxContainer.from(tx1)) + 1 * txCache.add(TxContainer.from(tx2)) } } diff --git a/src/test/groovy/io/emeraldpay/dshackle/cache/HeightCacheSpec.groovy b/src/test/groovy/io/emeraldpay/dshackle/cache/HeightCacheSpec.groovy index ec927e9a..31bd45bf 100644 --- a/src/test/groovy/io/emeraldpay/dshackle/cache/HeightCacheSpec.groovy +++ b/src/test/groovy/io/emeraldpay/dshackle/cache/HeightCacheSpec.groovy @@ -16,6 +16,7 @@ package io.emeraldpay.dshackle.cache import com.fasterxml.jackson.databind.ObjectMapper +import io.emeraldpay.dshackle.Global import io.emeraldpay.dshackle.data.BlockContainer import io.emeraldpay.dshackle.test.TestingCommons import io.infinitape.etherjar.domain.BlockHash @@ -32,8 +33,6 @@ class HeightCacheSpec extends Specification { String hash3 = "0x40d15edaff9acdabd2a1c96fd5f683b3300aad34e7015f34def3c56ba8a7ffb5" String hash4 = "0xa4e7a75dfd5f6a83b3304dc56bfa0abfd3fef01540d15edafc9683f9acd2a13b" - ObjectMapper objectMapper = TestingCommons.objectMapper() - def "Add and read"() { setup: def cache = new HeightCache() @@ -45,7 +44,7 @@ class HeightCacheSpec extends Specification { block.hash = BlockHash.from(hash) block.totalDifficulty = BigInteger.ONE block.timestamp = Instant.now() - cache.add(BlockContainer.from(block, objectMapper)) + cache.add(BlockContainer.from(block)) } def act1 = cache.read(100).block() @@ -71,7 +70,7 @@ class HeightCacheSpec extends Specification { block.hash = BlockHash.from(hash) block.totalDifficulty = BigInteger.ONE block.timestamp = Instant.now() - cache.add(BlockContainer.from(block, objectMapper)) + cache.add(BlockContainer.from(block)) } def act1 = cache.read(100).block() diff --git a/src/test/groovy/io/emeraldpay/dshackle/cache/TxMemCacheSpec.groovy b/src/test/groovy/io/emeraldpay/dshackle/cache/TxMemCacheSpec.groovy index 96bf3db6..521ec9de 100644 --- a/src/test/groovy/io/emeraldpay/dshackle/cache/TxMemCacheSpec.groovy +++ b/src/test/groovy/io/emeraldpay/dshackle/cache/TxMemCacheSpec.groovy @@ -16,6 +16,7 @@ package io.emeraldpay.dshackle.cache import com.fasterxml.jackson.databind.ObjectMapper +import io.emeraldpay.dshackle.Global import io.emeraldpay.dshackle.data.BlockContainer import io.emeraldpay.dshackle.data.BlockId import io.emeraldpay.dshackle.data.TxContainer @@ -37,7 +38,7 @@ class TxMemCacheSpec extends Specification { String hash3 = "0x40d15edaff9acdabd2a1c96fd5f683b3300aad34e7015f34def3c56ba8a7ffb5" String hash4 = "0xa4e7a75dfd5f6a83b3304dc56bfa0abfd3fef01540d15edafc9683f9acd2a13b" - ObjectMapper objectMapper = TestingCommons.objectMapper() + ObjectMapper objectMapper = Global.objectMapper def "Add and read"() { setup: @@ -48,7 +49,7 @@ class TxMemCacheSpec extends Specification { tx.blockNumber = 100 when: - cache.add(TxContainer.from(tx, objectMapper)) + cache.add(TxContainer.from(tx)) def act = cache.read(TxId.from(hash1)).block() then: objectMapper.readValue(act.json, TransactionJson.class) == tx @@ -64,7 +65,7 @@ class TxMemCacheSpec extends Specification { tx.blockNumber = 100 + i tx.blockHash = BlockHash.from(hash) tx.hash = TransactionId.from(hash) - cache.add(TxContainer.from(tx, objectMapper)) + cache.add(TxContainer.from(tx)) } def act1 = cache.read(TxId.from(hash1)).block() @@ -88,14 +89,14 @@ class TxMemCacheSpec extends Specification { tx.blockNumber = 100 tx.blockHash = BlockHash.from(hash1) tx.hash = TransactionId.from(hash) - cache.add(TxContainer.from(tx, objectMapper)) + cache.add(TxContainer.from(tx)) } [hash3, hash4].eachWithIndex { String hash, int i -> def tx = new TransactionJson() tx.blockNumber = 101 tx.blockHash = BlockHash.from(hash2) tx.hash = TransactionId.from(hash) - cache.add(TxContainer.from(tx, objectMapper)) + cache.add(TxContainer.from(tx)) } cache.evict(BlockId.from(hash1)) @@ -122,14 +123,14 @@ class TxMemCacheSpec extends Specification { tx.blockNumber = 100 tx.blockHash = BlockHash.from(hash1) tx.hash = TransactionId.from(hash) - cache.add(TxContainer.from(tx, objectMapper)) + cache.add(TxContainer.from(tx)) } [hash3, hash4].eachWithIndex{ String hash, int i -> def tx = new TransactionJson() tx.blockNumber = 100 tx.blockHash = BlockHash.from(hash2) tx.hash = TransactionId.from(hash) - cache.add(TxContainer.from(tx, objectMapper)) + cache.add(TxContainer.from(tx)) } def block = new BlockJson() @@ -142,7 +143,7 @@ class TxMemCacheSpec extends Specification { new TransactionRefJson(TransactionId.from(hash2)), ] - cache.evict(BlockContainer.from(block, objectMapper)) + cache.evict(BlockContainer.from(block)) def act1 = cache.read(TxId.from(hash1)).block() def act2 = cache.read(TxId.from(hash2)).block() diff --git a/src/test/groovy/io/emeraldpay/dshackle/cache/TxRedisCacheSpec.groovy b/src/test/groovy/io/emeraldpay/dshackle/cache/TxRedisCacheSpec.groovy index bcc6375a..cdd5eb5c 100644 --- a/src/test/groovy/io/emeraldpay/dshackle/cache/TxRedisCacheSpec.groovy +++ b/src/test/groovy/io/emeraldpay/dshackle/cache/TxRedisCacheSpec.groovy @@ -15,7 +15,8 @@ */ package io.emeraldpay.dshackle.cache - +import com.fasterxml.jackson.databind.ObjectMapper +import io.emeraldpay.dshackle.Global import io.emeraldpay.dshackle.data.BlockContainer import io.emeraldpay.dshackle.data.BlockId import io.emeraldpay.dshackle.data.TxContainer @@ -49,7 +50,7 @@ class TxRedisCacheSpec extends Specification { String hash4 = "0xa4e7a75dfd5f6a83b3304dc56bfa0abfd3fef01540d15edafc9683f9acd2a13b" TxRedisCache cache - def objectMapper = TestingCommons.objectMapper() + ObjectMapper objectMapper = Global.objectMapper def setup() { StatefulRedisConnection redis = IntegrationTestingCommons.redisConnection() @@ -96,7 +97,7 @@ class TxRedisCacheSpec extends Specification { tx.nonce = 0 when: - cache.add(TxContainer.from(tx, objectMapper), BlockContainer.from(block, objectMapper)).subscribe() + cache.add(TxContainer.from(tx), BlockContainer.from(block)).subscribe() def act = cache.read(TxId.from(hash1)).block() then: act != null @@ -121,7 +122,7 @@ class TxRedisCacheSpec extends Specification { tx.nonce = 0 when: - cache.add(TxContainer.from(tx, objectMapper), BlockContainer.from(block, objectMapper)).subscribe() + cache.add(TxContainer.from(tx), BlockContainer.from(block)).subscribe() def act = cache.read(TxId.from(tx.hash)).block() then: act != null @@ -162,7 +163,7 @@ class TxRedisCacheSpec extends Specification { tx.hash = TransactionId.from(hash) tx.value = Wei.ofEthers(i) tx.nonce = 0 - cache.add(TxContainer.from(tx, objectMapper), BlockContainer.from(block1, objectMapper)).subscribe() + cache.add(TxContainer.from(tx), BlockContainer.from(block1)).subscribe() } [hash3, hash4].eachWithIndex{ String hash, int i -> def tx = new TransactionJson() @@ -171,11 +172,11 @@ class TxRedisCacheSpec extends Specification { tx.hash = TransactionId.from(hash) tx.value = Wei.ofEthers(i) tx.nonce = 0 - cache.add(TxContainer.from(tx, objectMapper), BlockContainer.from(block2, objectMapper)).subscribe() + cache.add(TxContainer.from(tx), BlockContainer.from(block2)).subscribe() } - cache.evict(BlockContainer.from(block1, objectMapper)).subscribe() + cache.evict(BlockContainer.from(block1)).subscribe() def act1 = cache.read(TxId.from(hash1)).block() def act2 = cache.read(TxId.from(hash2)).block() diff --git a/src/test/groovy/io/emeraldpay/dshackle/proxy/ProxyServerSpec.groovy b/src/test/groovy/io/emeraldpay/dshackle/proxy/ProxyServerSpec.groovy index c7baf366..0f84d750 100644 --- a/src/test/groovy/io/emeraldpay/dshackle/proxy/ProxyServerSpec.groovy +++ b/src/test/groovy/io/emeraldpay/dshackle/proxy/ProxyServerSpec.groovy @@ -42,7 +42,7 @@ class ProxyServerSpec extends Specification { ProxyServer server = new ProxyServer( new ProxyConfig(), - new ReadRpcJson(TestingCommons.objectMapper()), + new ReadRpcJson(), writeRpcJson, nativeCall, new TlsSetup(TestingCommons.fileResolver()) diff --git a/src/test/groovy/io/emeraldpay/dshackle/proxy/ReadRpcJsonSpec.groovy b/src/test/groovy/io/emeraldpay/dshackle/proxy/ReadRpcJsonSpec.groovy index c927c3d5..02a6771f 100644 --- a/src/test/groovy/io/emeraldpay/dshackle/proxy/ReadRpcJsonSpec.groovy +++ b/src/test/groovy/io/emeraldpay/dshackle/proxy/ReadRpcJsonSpec.groovy @@ -22,7 +22,7 @@ import spock.lang.Specification class ReadRpcJsonSpec extends Specification { - ReadRpcJson reader = new ReadRpcJson(TestingCommons.objectMapper()) + ReadRpcJson reader = new ReadRpcJson() def "Get first symbol"() { expect: diff --git a/src/test/groovy/io/emeraldpay/dshackle/proxy/WriteRpcJsonSpec.groovy b/src/test/groovy/io/emeraldpay/dshackle/proxy/WriteRpcJsonSpec.groovy index f7e926cc..1504f1df 100644 --- a/src/test/groovy/io/emeraldpay/dshackle/proxy/WriteRpcJsonSpec.groovy +++ b/src/test/groovy/io/emeraldpay/dshackle/proxy/WriteRpcJsonSpec.groovy @@ -26,7 +26,7 @@ import java.time.Duration class WriteRpcJsonSpec extends Specification { - WriteRpcJson writer = new WriteRpcJson(TestingCommons.objectMapper()) + WriteRpcJson writer = new WriteRpcJson() def "Write empty array"() { when: diff --git a/src/test/groovy/io/emeraldpay/dshackle/quorum/BroadcastQuorumSpec.groovy b/src/test/groovy/io/emeraldpay/dshackle/quorum/BroadcastQuorumSpec.groovy index 427ed8a4..9c6a1c39 100644 --- a/src/test/groovy/io/emeraldpay/dshackle/quorum/BroadcastQuorumSpec.groovy +++ b/src/test/groovy/io/emeraldpay/dshackle/quorum/BroadcastQuorumSpec.groovy @@ -16,6 +16,8 @@ */ package io.emeraldpay.dshackle.quorum +import com.fasterxml.jackson.databind.ObjectMapper +import io.emeraldpay.dshackle.Global import io.emeraldpay.dshackle.test.TestingCommons import io.emeraldpay.dshackle.upstream.Head import io.emeraldpay.dshackle.upstream.Upstream @@ -25,11 +27,11 @@ import spock.lang.Specification class BroadcastQuorumSpec extends Specification { - def objectMapper = TestingCommons.objectMapper() + ObjectMapper objectMapper = Global.objectMapper def "Resolved with first after 3 tries"() { setup: - def q = Spy(new BroadcastQuorum(objectMapper, 3)) + def q = Spy(new BroadcastQuorum(3)) def upstream1 = Stub(Upstream) def upstream2 = Stub(Upstream) def upstream3 = Stub(Upstream) @@ -61,7 +63,7 @@ class BroadcastQuorumSpec extends Specification { def "Remembers first response"() { setup: - def q = Spy(new BroadcastQuorum(objectMapper, 3)) + def q = Spy(new BroadcastQuorum(3)) def upstream1 = Stub(Upstream) def upstream2 = Stub(Upstream) def upstream3 = Stub(Upstream) diff --git a/src/test/groovy/io/emeraldpay/dshackle/quorum/NonEmptyQuorumSpec.groovy b/src/test/groovy/io/emeraldpay/dshackle/quorum/NonEmptyQuorumSpec.groovy index 3f38278e..a140408d 100644 --- a/src/test/groovy/io/emeraldpay/dshackle/quorum/NonEmptyQuorumSpec.groovy +++ b/src/test/groovy/io/emeraldpay/dshackle/quorum/NonEmptyQuorumSpec.groovy @@ -25,7 +25,7 @@ class NonEmptyQuorumSpec extends Specification { def "Fail if too many errors"() { setup: - def q = Spy(new NonEmptyQuorum(TestingCommons.objectMapper(), 3)) + def q = Spy(new NonEmptyQuorum(3)) def upstream1 = Stub(Upstream) def upstream2 = Stub(Upstream) def upstream3 = Stub(Upstream) @@ -57,7 +57,7 @@ class NonEmptyQuorumSpec extends Specification { def "Fail first if not error"() { setup: - def q = Spy(new NonEmptyQuorum(TestingCommons.objectMapper(), 3)) + def q = Spy(new NonEmptyQuorum(3)) def upstream1 = Stub(Upstream) def upstream2 = Stub(Upstream) def upstream3 = Stub(Upstream) @@ -77,7 +77,7 @@ class NonEmptyQuorumSpec extends Specification { def "Fail second if first is error"() { setup: - def q = Spy(new NonEmptyQuorum(TestingCommons.objectMapper(), 3)) + def q = Spy(new NonEmptyQuorum(3)) def upstream1 = Stub(Upstream) def upstream2 = Stub(Upstream) def upstream3 = Stub(Upstream) @@ -104,7 +104,7 @@ class NonEmptyQuorumSpec extends Specification { def "Fail second if first is null"() { setup: - def q = Spy(new NonEmptyQuorum(TestingCommons.objectMapper(), 3)) + def q = Spy(new NonEmptyQuorum(3)) def upstream1 = Stub(Upstream) def upstream2 = Stub(Upstream) def upstream3 = Stub(Upstream) diff --git a/src/test/groovy/io/emeraldpay/dshackle/quorum/NonceQuorumSpec.groovy b/src/test/groovy/io/emeraldpay/dshackle/quorum/NonceQuorumSpec.groovy index 83cededc..374389ce 100644 --- a/src/test/groovy/io/emeraldpay/dshackle/quorum/NonceQuorumSpec.groovy +++ b/src/test/groovy/io/emeraldpay/dshackle/quorum/NonceQuorumSpec.groovy @@ -16,6 +16,8 @@ */ package io.emeraldpay.dshackle.quorum +import com.fasterxml.jackson.databind.ObjectMapper +import io.emeraldpay.dshackle.Global import io.emeraldpay.dshackle.test.TestingCommons import io.emeraldpay.dshackle.upstream.Head import io.emeraldpay.dshackle.upstream.Upstream @@ -25,11 +27,11 @@ import spock.lang.Specification class NonceQuorumSpec extends Specification { - def objectMapper = TestingCommons.objectMapper() + ObjectMapper objectMapper = Global.objectMapper def "Gets max value"() { setup: - def q = Spy(new NonceQuorum(objectMapper, 3)) + def q = Spy(new NonceQuorum(3)) def upstream1 = Stub(Upstream) def upstream2 = Stub(Upstream) def upstream3 = Stub(Upstream) @@ -61,7 +63,7 @@ class NonceQuorumSpec extends Specification { def "Ignores errors"() { setup: - def q = Spy(new NonceQuorum(objectMapper, 3)) + def q = Spy(new NonceQuorum(3)) def upstream1 = Stub(Upstream) def upstream2 = Stub(Upstream) def upstream3 = Stub(Upstream) @@ -99,7 +101,7 @@ class NonceQuorumSpec extends Specification { def "Fail if too many errors"() { setup: - def q = Spy(new NonceQuorum(objectMapper, 3)) + def q = Spy(new NonceQuorum(3)) def upstream1 = Stub(Upstream) def upstream2 = Stub(Upstream) def upstream3 = Stub(Upstream) diff --git a/src/test/groovy/io/emeraldpay/dshackle/quorum/QuorumRpcReaderSpec.groovy b/src/test/groovy/io/emeraldpay/dshackle/quorum/QuorumRpcReaderSpec.groovy index 0af237c2..61149bca 100644 --- a/src/test/groovy/io/emeraldpay/dshackle/quorum/QuorumRpcReaderSpec.groovy +++ b/src/test/groovy/io/emeraldpay/dshackle/quorum/QuorumRpcReaderSpec.groovy @@ -99,7 +99,7 @@ class QuorumRpcReaderSpec extends Specification { def apis = new FilteredApis( [up], Selector.empty ) - def reader = new QuorumRpcReader(apis, new NonEmptyQuorum(TestingCommons.objectMapper(), 3)) + def reader = new QuorumRpcReader(apis, new NonEmptyQuorum(3)) when: def act = reader.read(new JsonRpcRequest("eth_test", [])) @@ -129,7 +129,7 @@ class QuorumRpcReaderSpec extends Specification { def apis = new FilteredApis( [up], Selector.empty ) - def reader = new QuorumRpcReader(apis, new NonEmptyQuorum(TestingCommons.objectMapper(), 3)) + def reader = new QuorumRpcReader(apis, new NonEmptyQuorum(3)) when: def act = reader.read(new JsonRpcRequest("eth_test", [])) @@ -159,7 +159,7 @@ class QuorumRpcReaderSpec extends Specification { def apis = new FilteredApis( [up], Selector.empty ) - def reader = new QuorumRpcReader(apis, new NonEmptyQuorum(TestingCommons.objectMapper(), 3)) + def reader = new QuorumRpcReader(apis, new NonEmptyQuorum(3)) when: def act = reader.read(new JsonRpcRequest("eth_test", [])) @@ -189,7 +189,7 @@ class QuorumRpcReaderSpec extends Specification { def apis = new FilteredApis( [up], Selector.empty ) - def reader = new QuorumRpcReader(apis, new NonEmptyQuorum(TestingCommons.objectMapper(), 3)) + def reader = new QuorumRpcReader(apis, new NonEmptyQuorum(3)) when: def act = reader.read(new JsonRpcRequest("eth_test", [])) diff --git a/src/test/groovy/io/emeraldpay/dshackle/quorum/ValueAwareQuorumSpec.groovy b/src/test/groovy/io/emeraldpay/dshackle/quorum/ValueAwareQuorumSpec.groovy index f985b0fc..04ae7c4e 100644 --- a/src/test/groovy/io/emeraldpay/dshackle/quorum/ValueAwareQuorumSpec.groovy +++ b/src/test/groovy/io/emeraldpay/dshackle/quorum/ValueAwareQuorumSpec.groovy @@ -62,7 +62,7 @@ class ValueAwareQuorumSpec extends Specification { class ValueAwareQuorumImpl extends ValueAwareQuorum { ValueAwareQuorumImpl() { - super(TestingCommons.objectMapper(), Object) + super(Object) } @Override diff --git a/src/test/groovy/io/emeraldpay/dshackle/rpc/NativeCallSpec.groovy b/src/test/groovy/io/emeraldpay/dshackle/rpc/NativeCallSpec.groovy index d43df755..4d0e4b13 100644 --- a/src/test/groovy/io/emeraldpay/dshackle/rpc/NativeCallSpec.groovy +++ b/src/test/groovy/io/emeraldpay/dshackle/rpc/NativeCallSpec.groovy @@ -16,8 +16,9 @@ */ package io.emeraldpay.dshackle.rpc - +import com.fasterxml.jackson.databind.ObjectMapper import io.emeraldpay.api.proto.BlockchainOuterClass +import io.emeraldpay.dshackle.Global import io.emeraldpay.dshackle.quorum.BroadcastQuorum import io.emeraldpay.dshackle.quorum.QuorumReaderFactory import io.emeraldpay.dshackle.quorum.QuorumRpcReader @@ -45,7 +46,7 @@ import java.util.concurrent.TimeoutException class NativeCallSpec extends Specification { - def objectMapper = TestingCommons.objectMapper() + ObjectMapper objectMapper = Global.objectMapper def "Tries router first"() { def routedApi = Mock(Reader) { @@ -56,7 +57,7 @@ class NativeCallSpec extends Specification { } def upstreams = Stub(MultistreamHolder) - def nativeCall = new NativeCall(upstreams, TestingCommons.objectMapper()) + def nativeCall = new NativeCall(upstreams) def ctx = new NativeCall.CallContext( 1, upstream, Selector.empty, new AlwaysQuorum(), new NativeCall.ParsedCallDetails("eth_test", []) @@ -77,7 +78,7 @@ class NativeCallSpec extends Specification { } def upstreams = Stub(MultistreamHolder) - def nativeCall = new NativeCall(upstreams, TestingCommons.objectMapper()) + def nativeCall = new NativeCall(upstreams) def ctx = new NativeCall.CallContext( 15, upstream, Selector.empty, new AlwaysQuorum(), new NativeCall.ParsedCallDetails("eth_test", []) @@ -100,7 +101,7 @@ class NativeCallSpec extends Specification { setup: def quorum = new AlwaysQuorum() - def nativeCall = new NativeCall(Stub(MultistreamHolder), TestingCommons.objectMapper()) + def nativeCall = new NativeCall(Stub(MultistreamHolder)) nativeCall.quorumReaderFactory = Mock(QuorumReaderFactory) { 1 * create(_, _) >> Mock(Reader) { 1 * read(_) >> Mono.just(new QuorumRpcReader.Result("\"foo\"".bytes, 1)) @@ -120,7 +121,7 @@ class NativeCallSpec extends Specification { setup: def quorum = new AlwaysQuorum() - def nativeCall = new NativeCall(Stub(MultistreamHolder), TestingCommons.objectMapper()) + def nativeCall = new NativeCall(Stub(MultistreamHolder)) nativeCall.quorumReaderFactory = Mock(QuorumReaderFactory) { 1 * create(_, _) >> Mock(Reader) { 1 * read(_) >> Mono.empty() @@ -140,7 +141,7 @@ class NativeCallSpec extends Specification { def "Packs call exception into response with id"() { setup: def upstreams = Stub(MultistreamHolder) - def nativeCall = new NativeCall(upstreams, TestingCommons.objectMapper()) + def nativeCall = new NativeCall(upstreams) when: def resp = nativeCall.processException(new NativeCall.CallFailure(5, new IllegalArgumentException("test test"))) then: @@ -157,7 +158,7 @@ class NativeCallSpec extends Specification { def "Packs unknown exception into response"() { setup: def upstreams = Stub(MultistreamHolder) - def nativeCall = new NativeCall(upstreams, TestingCommons.objectMapper()) + def nativeCall = new NativeCall(upstreams) when: def resp = nativeCall.processException(new IllegalArgumentException("test test")) then: @@ -173,7 +174,7 @@ class NativeCallSpec extends Specification { def "Builds normal response"() { setup: def upstreams = Stub(MultistreamHolder) - def nativeCall = new NativeCall(upstreams, TestingCommons.objectMapper()) + def nativeCall = new NativeCall(upstreams) def json = [jsonrpc:"2.0", id:1, result: "foo"] when: @@ -189,7 +190,7 @@ class NativeCallSpec extends Specification { def "Returns error for invalid chain"() { setup: def upstreams = Stub(MultistreamHolder) - def nativeCall = new NativeCall(upstreams, TestingCommons.objectMapper()) + def nativeCall = new NativeCall(upstreams) def req = BlockchainOuterClass.NativeCallRequest.newBuilder() .setChainValue(0) @@ -212,7 +213,7 @@ class NativeCallSpec extends Specification { def "Returns error for unsupported chain"() { setup: def upstreams = Mock(MultistreamHolder) - def nativeCall = new NativeCall(upstreams, TestingCommons.objectMapper()) + def nativeCall = new NativeCall(upstreams) def req = BlockchainOuterClass.NativeCallRequest.newBuilder() .setChainValue(Chain.TESTNET_MORDEN.id) @@ -238,7 +239,7 @@ class NativeCallSpec extends Specification { def "Calls cache before remote"() { setup: def upstreams = Stub(MultistreamHolder) - def nativeCall = new NativeCall(upstreams, TestingCommons.objectMapper()) + def nativeCall = new NativeCall(upstreams) def api = TestingCommons.api() def upstream = TestingCommons.aggregatedUpstream(api) @@ -257,7 +258,7 @@ class NativeCallSpec extends Specification { def "Uses cached value"() { setup: def upstreams = Stub(MultistreamHolder) - def nativeCall = new NativeCall(upstreams, TestingCommons.objectMapper()) + def nativeCall = new NativeCall(upstreams) def upstream = TestingCommons.aggregatedUpstream(TestingCommons.api()) def ctx = new NativeCall.CallContext(10, diff --git a/src/test/groovy/io/emeraldpay/dshackle/rpc/StreamHeadSpec.groovy b/src/test/groovy/io/emeraldpay/dshackle/rpc/StreamHeadSpec.groovy index 199629ce..0fdda60d 100644 --- a/src/test/groovy/io/emeraldpay/dshackle/rpc/StreamHeadSpec.groovy +++ b/src/test/groovy/io/emeraldpay/dshackle/rpc/StreamHeadSpec.groovy @@ -20,6 +20,7 @@ import com.fasterxml.jackson.databind.ObjectMapper import com.google.protobuf.ByteString import io.emeraldpay.api.proto.BlockchainOuterClass import io.emeraldpay.api.proto.Common +import io.emeraldpay.dshackle.Global import io.emeraldpay.dshackle.data.BlockContainer import io.emeraldpay.dshackle.test.EthereumUpstreamMock import io.emeraldpay.dshackle.test.TestingCommons @@ -38,7 +39,7 @@ import java.time.Instant class StreamHeadSpec extends Specification { - ObjectMapper objectMapper = TestingCommons.objectMapper() + ObjectMapper objectMapper = Global.objectMapper def "Errors on unavailable chain"() { setup: @@ -86,9 +87,9 @@ class StreamHeadSpec extends Specification { ) then: StepVerifier.create(flux.take(2)) - .then { upstream.nextBlock(BlockContainer.from(blocks[0], objectMapper)) } + .then { upstream.nextBlock(BlockContainer.from(blocks[0])) } .expectNext(heads[0]) - .then { upstream.nextBlock(BlockContainer.from(blocks[1], objectMapper)) } + .then { upstream.nextBlock(BlockContainer.from(blocks[1])) } .expectNext(heads[1]) .expectComplete() .verify(Duration.ofSeconds(1)) diff --git a/src/test/groovy/io/emeraldpay/dshackle/rpc/TrackBitcoinAddressSpec.groovy b/src/test/groovy/io/emeraldpay/dshackle/rpc/TrackBitcoinAddressSpec.groovy index 9f4a0488..842a9145 100644 --- a/src/test/groovy/io/emeraldpay/dshackle/rpc/TrackBitcoinAddressSpec.groovy +++ b/src/test/groovy/io/emeraldpay/dshackle/rpc/TrackBitcoinAddressSpec.groovy @@ -15,8 +15,10 @@ */ package io.emeraldpay.dshackle.rpc +import com.fasterxml.jackson.databind.ObjectMapper import io.emeraldpay.api.proto.BlockchainOuterClass import io.emeraldpay.api.proto.Common +import io.emeraldpay.dshackle.Global import io.emeraldpay.dshackle.data.BlockContainer import io.emeraldpay.dshackle.data.BlockId import io.emeraldpay.dshackle.test.TestingCommons @@ -38,11 +40,12 @@ import java.time.Instant class TrackBitcoinAddressSpec extends Specification { String hash1 = "0xa0e65cbc1b52a8ca60562112c6060552d882f16f34a9dba2ccdc05c0a6a27c22" + ObjectMapper objectMapper = Global.objectMapper def "Correct sum from multiple"() { setup: def json = this.class.getClassLoader().getResourceAsStream("bitcoin/unspent-one-addr.json") - def unspents = TestingCommons.objectMapper().readValue(json, List) + def unspents = objectMapper.readValue(json, List) TrackBitcoinAddress track = new TrackBitcoinAddress(Stub(MultistreamHolder)) when: def total = track.getTotal(Chain.BITCOIN, ["1K7xkspJg7DDKNwzXgoRSDCUxiFsRegsSK"], unspents) @@ -57,7 +60,7 @@ class TrackBitcoinAddressSpec extends Specification { def "Correct sum when other addresses"() { setup: def json = this.class.getClassLoader().getResourceAsStream("bitcoin/unspent-two-addr.json") - def unspents = TestingCommons.objectMapper().readValue(json, List) + def unspents = objectMapper.readValue(json, List) TrackBitcoinAddress track = new TrackBitcoinAddress(Stub(MultistreamHolder)) when: def total = track.getTotal(Chain.BITCOIN, ["1K7xkspJg7DDKNwzXgoRSDCUxiFsRegsSK"], unspents) @@ -72,7 +75,7 @@ class TrackBitcoinAddressSpec extends Specification { def "Sum for two addresses"() { setup: def json = this.class.getClassLoader().getResourceAsStream("bitcoin/unspent-two-addr.json") - def unspents = TestingCommons.objectMapper().readValue(json, List) + def unspents = objectMapper.readValue(json, List) TrackBitcoinAddress track = new TrackBitcoinAddress(Stub(MultistreamHolder)) when: def total = track.getTotal(Chain.BITCOIN, ["1K7xkspJg7DDKNwzXgoRSDCUxiFsRegsSK", "35hK24tcLEWcgNA4JxpvbkNkoAcDGqQPsP"], unspents).sort { it.address.address } @@ -107,7 +110,7 @@ class TrackBitcoinAddressSpec extends Specification { def "Zero for unknown address"() { setup: def json = this.class.getClassLoader().getResourceAsStream("bitcoin/unspent-two-addr.json") - def unspents = TestingCommons.objectMapper().readValue(json, List) + def unspents = objectMapper.readValue(json, List) TrackBitcoinAddress track = new TrackBitcoinAddress(Stub(MultistreamHolder)) when: def total = track.getTotal(Chain.BITCOIN, ["16rCmCmbuWDhPjWTrpQGaU3EPdZF7MTdUk", "1K7xkspJg7DDKNwzXgoRSDCUxiFsRegsSK"], unspents).sort { it.address.address } diff --git a/src/test/groovy/io/emeraldpay/dshackle/rpc/TrackEthereumAddressSpec.groovy b/src/test/groovy/io/emeraldpay/dshackle/rpc/TrackEthereumAddressSpec.groovy index 654a7133..d7876ff3 100644 --- a/src/test/groovy/io/emeraldpay/dshackle/rpc/TrackEthereumAddressSpec.groovy +++ b/src/test/groovy/io/emeraldpay/dshackle/rpc/TrackEthereumAddressSpec.groovy @@ -108,7 +108,7 @@ class TrackEthereumAddressSpec extends Specification { StepVerifier.create(flux) .expectNext(exp1).as("First block") .then { - upstreamMock.nextBlock(BlockContainer.from(block2, TestingCommons.objectMapper())) + upstreamMock.nextBlock(BlockContainer.from(block2)) } .expectNext(exp2).as("Second block") .thenCancel() diff --git a/src/test/groovy/io/emeraldpay/dshackle/rpc/TrackEthereumTxSpec.groovy b/src/test/groovy/io/emeraldpay/dshackle/rpc/TrackEthereumTxSpec.groovy index 72d4a60c..4e1925f9 100644 --- a/src/test/groovy/io/emeraldpay/dshackle/rpc/TrackEthereumTxSpec.groovy +++ b/src/test/groovy/io/emeraldpay/dshackle/rpc/TrackEthereumTxSpec.groovy @@ -103,7 +103,7 @@ class TrackEthereumTxSpec extends Specification { apiMock.answer("eth_getTransactionByHash", [txId], txJson) apiMock.answer("eth_getBlockByHash", [blockJson.hash.toHex(), false], blockJson) - upstreamMock.nextBlock(BlockContainer.from(blockHeadJson, TestingCommons.objectMapper())) + upstreamMock.nextBlock(BlockContainer.from(blockHeadJson)) when: def flux = trackTx.subscribe(req) @@ -301,7 +301,7 @@ class TrackEthereumTxSpec extends Specification { upstreamMock.blocks = Flux.fromIterable(blocks) .map { block -> - BlockContainer.from(block, TestingCommons.objectMapper()) + BlockContainer.from(block) } when: diff --git a/src/test/groovy/io/emeraldpay/dshackle/test/EthereumApiMock.groovy b/src/test/groovy/io/emeraldpay/dshackle/test/EthereumApiMock.groovy index 1abad589..11fc75d1 100644 --- a/src/test/groovy/io/emeraldpay/dshackle/test/EthereumApiMock.groovy +++ b/src/test/groovy/io/emeraldpay/dshackle/test/EthereumApiMock.groovy @@ -19,6 +19,7 @@ package io.emeraldpay.dshackle.test import com.fasterxml.jackson.databind.ObjectMapper import com.google.protobuf.ByteString import io.emeraldpay.api.proto.BlockchainOuterClass +import io.emeraldpay.dshackle.Global import io.emeraldpay.dshackle.reader.Reader import io.emeraldpay.dshackle.upstream.rpcclient.JsonRpcRequest import io.emeraldpay.dshackle.upstream.rpcclient.JsonRpcResponse @@ -37,12 +38,11 @@ class EthereumApiMock implements Reader { private static final Logger log = LoggerFactory.getLogger(this) List predefined = [] - private ObjectMapper objectMapper + private final ObjectMapper objectMapper = Global.objectMapper String id = "default" - EthereumApiMock(@NotNull ObjectMapper objectMapper) { - this.objectMapper = objectMapper + EthereumApiMock() { } EthereumApiMock answerOnce(@NotNull String method, List params, Object result) { diff --git a/src/test/groovy/io/emeraldpay/dshackle/test/EthereumUpstreamMock.groovy b/src/test/groovy/io/emeraldpay/dshackle/test/EthereumUpstreamMock.groovy index 9bfdbef0..c42e54e6 100644 --- a/src/test/groovy/io/emeraldpay/dshackle/test/EthereumUpstreamMock.groovy +++ b/src/test/groovy/io/emeraldpay/dshackle/test/EthereumUpstreamMock.groovy @@ -41,8 +41,8 @@ class EthereumUpstreamMock extends EthereumUpstream { static CallMethods allMethods() { new AggregatedCallMethods([ - new DefaultEthereumMethods(TestingCommons.objectMapper(), Chain.ETHEREUM), - new DefaultBitcoinMethods(TestingCommons.objectMapper()), + new DefaultEthereumMethods(Chain.ETHEREUM), + new DefaultBitcoinMethods(), new DirectCallMethods(["eth_test"]) ]) } @@ -62,7 +62,7 @@ class EthereumUpstreamMock extends EthereumUpstream { EthereumUpstreamMock(@NotNull String id, @NotNull Chain chain, @NotNull Reader api, CallMethods methods) { super(id, chain, api, null, UpstreamsConfig.Options.getDefaults(), new QuorumForLabels.QuorumItem(1, new UpstreamsConfig.Labels()), - methods, TestingCommons.objectMapper()) + methods) setLag(0) setStatus(UpstreamAvailability.OK) start() diff --git a/src/test/groovy/io/emeraldpay/dshackle/test/MultistreamHolderMock.groovy b/src/test/groovy/io/emeraldpay/dshackle/test/MultistreamHolderMock.groovy index 6c8de274..7790ae74 100644 --- a/src/test/groovy/io/emeraldpay/dshackle/test/MultistreamHolderMock.groovy +++ b/src/test/groovy/io/emeraldpay/dshackle/test/MultistreamHolderMock.groovy @@ -47,7 +47,7 @@ class MultistreamHolderMock implements MultistreamHolder { if (up instanceof EthereumMultistream) { upstreams[chain] = up } else if (up instanceof EthereumUpstream) { - upstreams[chain] = new EthereumMultistreamMock(chain, [up as EthereumUpstream], Caches.default(TestingCommons.objectMapper())) + upstreams[chain] = new EthereumMultistreamMock(chain, [up as EthereumUpstream], Caches.default()) } else { throw new IllegalArgumentException("Unsupported upstream type ${up.class}") } @@ -56,7 +56,7 @@ class MultistreamHolderMock implements MultistreamHolder { if (up instanceof BitcoinMultistream) { upstreams[chain] = up } else if (up instanceof BitcoinUpstream) { - upstreams[chain] = new BitcoinMultistream(chain, [up as BitcoinUpstream], Caches.default(TestingCommons.objectMapper()), TestingCommons.objectMapper()) + upstreams[chain] = new BitcoinMultistream(chain, [up as BitcoinUpstream], Caches.default()) } else { throw new IllegalArgumentException("Unsupported upstream type ${up.class}") } @@ -86,7 +86,7 @@ class MultistreamHolderMock implements MultistreamHolder { @Override DefaultEthereumMethods getDefaultMethods(@NotNull Chain chain) { if (target[chain] == null) { - DefaultEthereumMethods targets = new DefaultEthereumMethods(TestingCommons.objectMapper(), chain) + DefaultEthereumMethods targets = new DefaultEthereumMethods(chain) target[chain] = targets } return target[chain] @@ -102,7 +102,7 @@ class MultistreamHolderMock implements MultistreamHolder { EthereumReader customReader = null EthereumMultistreamMock(@NotNull Chain chain, @NotNull List upstreams, @NotNull Caches caches) { - super(chain, upstreams, caches, TestingCommons.objectMapper()) + super(chain, upstreams, caches) } @Override diff --git a/src/test/groovy/io/emeraldpay/dshackle/test/TestingCommons.groovy b/src/test/groovy/io/emeraldpay/dshackle/test/TestingCommons.groovy index df2fb400..dade8700 100644 --- a/src/test/groovy/io/emeraldpay/dshackle/test/TestingCommons.groovy +++ b/src/test/groovy/io/emeraldpay/dshackle/test/TestingCommons.groovy @@ -21,6 +21,7 @@ import com.fasterxml.jackson.databind.DeserializationFeature import com.fasterxml.jackson.databind.ObjectMapper import com.fasterxml.jackson.databind.module.SimpleModule import io.emeraldpay.dshackle.FileResolver +import io.emeraldpay.dshackle.Global import io.emeraldpay.dshackle.cache.Caches import io.emeraldpay.dshackle.cache.CachesFactory import io.emeraldpay.dshackle.config.CacheConfig @@ -38,27 +39,9 @@ import java.text.SimpleDateFormat class TestingCommons { - static ObjectMapper objectMapper() { - def module = new SimpleModule("EmeraldDShackle", new Version(1, 0, 0, null, null, null)) - - def objectMapper = new ObjectMapper() - objectMapper.registerModule(module) - objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false) - objectMapper - .setDateFormat(new SimpleDateFormat("yyyy-MM-dd\'T\'HH:mm:ss.SSS")) - .setTimeZone(TimeZone.getTimeZone("UTC")) - - return objectMapper - } - static EthereumApiMock api() { - return new EthereumApiMock(objectMapper()) + return new EthereumApiMock() } - - static JacksonRpcConverter rpcConverter() { - return new JacksonRpcConverter(objectMapper()) - } - static EthereumUpstreamMock upstream(Reader api) { return new EthereumUpstreamMock(Chain.ETHEREUM, api) } @@ -76,13 +59,13 @@ class TestingCommons { } static Multistream aggregatedUpstream(EthereumUpstream up) { - return new EthereumMultistream(Chain.ETHEREUM, [up], Caches.default(objectMapper()), objectMapper()).tap { + return new EthereumMultistream(Chain.ETHEREUM, [up], Caches.default()).tap { start() } } static CachesFactory emptyCaches() { - return new CachesFactory(objectMapper(), new CacheConfig()) + return new CachesFactory(new CacheConfig()) } static FileResolver fileResolver() { diff --git a/src/test/groovy/io/emeraldpay/dshackle/upstream/CurrentMultistreamHolderSpec.groovy b/src/test/groovy/io/emeraldpay/dshackle/upstream/CurrentMultistreamHolderSpec.groovy index 3d320d7b..3fa6ea32 100644 --- a/src/test/groovy/io/emeraldpay/dshackle/upstream/CurrentMultistreamHolderSpec.groovy +++ b/src/test/groovy/io/emeraldpay/dshackle/upstream/CurrentMultistreamHolderSpec.groovy @@ -25,7 +25,7 @@ class CurrentMultistreamHolderSpec extends Specification { def "add upstream"() { setup: - def current = new CurrentMultistreamHolder(TestingCommons.objectMapper(), TestingCommons.emptyCaches()) + def current = new CurrentMultistreamHolder(TestingCommons.emptyCaches()) def up = new EthereumUpstreamMock("test", Chain.ETHEREUM, TestingCommons.api()) when: current.update(new UpstreamChange(Chain.ETHEREUM, up, UpstreamChange.ChangeType.ADDED)) @@ -36,7 +36,7 @@ class CurrentMultistreamHolderSpec extends Specification { def "add multiple upstreams"() { setup: - def current = new CurrentMultistreamHolder(TestingCommons.objectMapper(), TestingCommons.emptyCaches()) + def current = new CurrentMultistreamHolder(TestingCommons.emptyCaches()) def up1 = new EthereumUpstreamMock("test1", Chain.ETHEREUM, TestingCommons.api()) def up2 = new EthereumUpstreamMock("test2", Chain.ETHEREUM_CLASSIC, TestingCommons.api()) def up3 = new EthereumUpstreamMock("test3", Chain.ETHEREUM, TestingCommons.api()) @@ -52,7 +52,7 @@ class CurrentMultistreamHolderSpec extends Specification { def "remove upstream"() { setup: - def current = new CurrentMultistreamHolder(TestingCommons.objectMapper(), TestingCommons.emptyCaches()) + def current = new CurrentMultistreamHolder(TestingCommons.emptyCaches()) def up1 = new EthereumUpstreamMock("test1", Chain.ETHEREUM, TestingCommons.api()) def up2 = new EthereumUpstreamMock("test2", Chain.ETHEREUM_CLASSIC, TestingCommons.api()) def up3 = new EthereumUpstreamMock("test3", Chain.ETHEREUM, TestingCommons.api()) @@ -70,7 +70,7 @@ class CurrentMultistreamHolderSpec extends Specification { def "available after adding"() { setup: - def current = new CurrentMultistreamHolder(TestingCommons.objectMapper(), TestingCommons.emptyCaches()) + def current = new CurrentMultistreamHolder(TestingCommons.emptyCaches()) def up1 = new EthereumUpstreamMock("test1", Chain.ETHEREUM, TestingCommons.api()) when: diff --git a/src/test/groovy/io/emeraldpay/dshackle/upstream/FilteredApisSpec.groovy b/src/test/groovy/io/emeraldpay/dshackle/upstream/FilteredApisSpec.groovy index 1eba282f..5f2d06e6 100644 --- a/src/test/groovy/io/emeraldpay/dshackle/upstream/FilteredApisSpec.groovy +++ b/src/test/groovy/io/emeraldpay/dshackle/upstream/FilteredApisSpec.groovy @@ -16,7 +16,6 @@ */ package io.emeraldpay.dshackle.upstream -import io.emeraldpay.dshackle.cache.Caches import io.emeraldpay.dshackle.config.UpstreamsConfig import io.emeraldpay.dshackle.startup.QuorumForLabels import io.emeraldpay.dshackle.test.EthereumApiStub @@ -25,7 +24,6 @@ import io.emeraldpay.dshackle.upstream.calls.DefaultEthereumMethods import io.emeraldpay.dshackle.upstream.ethereum.EthereumUpstream import io.emeraldpay.dshackle.upstream.ethereum.EthereumWsFactory import io.emeraldpay.grpc.Chain -import io.infinitape.etherjar.rpc.ReactorRpcClient import reactor.test.StepVerifier import spock.lang.Retry import spock.lang.Specification @@ -34,9 +32,7 @@ import java.time.Duration class FilteredApisSpec extends Specification { - def rpcClient = Stub(ReactorRpcClient) - def objectMapper = TestingCommons.objectMapper() - def ethereumTargets = new DefaultEthereumMethods(objectMapper, Chain.ETHEREUM) + def ethereumTargets = new DefaultEthereumMethods(Chain.ETHEREUM) def "Verifies labels"() { setup: @@ -55,7 +51,7 @@ class FilteredApisSpec extends Specification { (EthereumWsFactory) null, new UpstreamsConfig.Options(), new QuorumForLabels.QuorumItem(1, UpstreamsConfig.Labels.fromMap(it)), - ethereumTargets, TestingCommons.objectMapper() + ethereumTargets ) } def matcher = new Selector.LabelMatcher("test", ["foo"]) diff --git a/src/test/groovy/io/emeraldpay/dshackle/upstream/MultistreamSpec.groovy b/src/test/groovy/io/emeraldpay/dshackle/upstream/MultistreamSpec.groovy index aa243140..39298b0a 100644 --- a/src/test/groovy/io/emeraldpay/dshackle/upstream/MultistreamSpec.groovy +++ b/src/test/groovy/io/emeraldpay/dshackle/upstream/MultistreamSpec.groovy @@ -31,7 +31,7 @@ class MultistreamSpec extends Specification { setup: def up1 = new EthereumUpstreamMock("test1", Chain.ETHEREUM, TestingCommons.api(), new DirectCallMethods(["eth_test1", "eth_test2"])) def up2 = new EthereumUpstreamMock("test1", Chain.ETHEREUM, TestingCommons.api(), new DirectCallMethods(["eth_test2", "eth_test3"])) - def aggr = new EthereumMultistream(Chain.ETHEREUM, [up1, up2], Caches.default(TestingCommons.objectMapper()), TestingCommons.objectMapper()) + def aggr = new EthereumMultistream(Chain.ETHEREUM, [up1, up2], Caches.default()) when: aggr.onUpstreamsUpdated() def act = aggr.getMethods() diff --git a/src/test/groovy/io/emeraldpay/dshackle/upstream/bitcoin/BitcoinRpcHeadSpec.groovy b/src/test/groovy/io/emeraldpay/dshackle/upstream/bitcoin/BitcoinRpcHeadSpec.groovy index f33f7876..54eff538 100644 --- a/src/test/groovy/io/emeraldpay/dshackle/upstream/bitcoin/BitcoinRpcHeadSpec.groovy +++ b/src/test/groovy/io/emeraldpay/dshackle/upstream/bitcoin/BitcoinRpcHeadSpec.groovy @@ -86,7 +86,7 @@ class BitcoinRpcHeadSpec extends Specification { _ * read(new JsonRpcRequest("getblock", [hash1])) >> Mono.just(new JsonRpcResponse(block1.bytes, null)) _ * read(new JsonRpcRequest("getblock", [hash2])) >> Mono.just(new JsonRpcResponse(block2.bytes, null)) } - BitcoinRpcHead head = new BitcoinRpcHead(api, new ExtractBlock(TestingCommons.objectMapper()), Duration.ofMillis(200)) + BitcoinRpcHead head = new BitcoinRpcHead(api, new ExtractBlock(), Duration.ofMillis(200)) when: def act = head.flux.take(2) diff --git a/src/test/groovy/io/emeraldpay/dshackle/upstream/bitcoin/ExtractBlockSpec.groovy b/src/test/groovy/io/emeraldpay/dshackle/upstream/bitcoin/ExtractBlockSpec.groovy index 53ba10ed..367770a6 100644 --- a/src/test/groovy/io/emeraldpay/dshackle/upstream/bitcoin/ExtractBlockSpec.groovy +++ b/src/test/groovy/io/emeraldpay/dshackle/upstream/bitcoin/ExtractBlockSpec.groovy @@ -20,7 +20,7 @@ import spock.lang.Specification class ExtractBlockSpec extends Specification { - ExtractBlock extractBlock = new ExtractBlock(TestingCommons.objectMapper()) + ExtractBlock extractBlock = new ExtractBlock() def "Extract standard block"() { setup: diff --git a/src/test/groovy/io/emeraldpay/dshackle/upstream/ethereum/DefaultEthereumHeadSpec.groovy b/src/test/groovy/io/emeraldpay/dshackle/upstream/ethereum/DefaultEthereumHeadSpec.groovy index 013decd5..02360832 100644 --- a/src/test/groovy/io/emeraldpay/dshackle/upstream/ethereum/DefaultEthereumHeadSpec.groovy +++ b/src/test/groovy/io/emeraldpay/dshackle/upstream/ethereum/DefaultEthereumHeadSpec.groovy @@ -17,6 +17,7 @@ package io.emeraldpay.dshackle.upstream.ethereum import com.fasterxml.jackson.databind.ObjectMapper +import io.emeraldpay.dshackle.Global import io.emeraldpay.dshackle.data.BlockContainer import io.emeraldpay.dshackle.test.TestingCommons import io.infinitape.etherjar.domain.BlockHash @@ -30,17 +31,16 @@ import java.time.Instant class DefaultEthereumHeadSpec extends Specification { DefaultEthereumHead head = new DefaultEthereumHead() - ObjectMapper objectMapper = TestingCommons.objectMapper() + ObjectMapper objectMapper = Global.objectMapper def blocks = (10L..20L).collect { i -> BlockContainer.from( - new BlockJson().with { + new BlockJson().tap { it.number = 10000L + i it.hash = BlockHash.from("0x3ec2ebf5d0ec474d0ac6bc50d2770d8409ad76e119968e7919f85d5ec89152" + i) it.totalDifficulty = 11 * i it.timestamp = Instant.now() - return it - }, objectMapper) + }) } def "Starts to follow"() { @@ -90,13 +90,12 @@ class DefaultEthereumHeadSpec extends Specification { def "Ignores less difficult"() { when: def block3less = BlockContainer.from( - new BlockJson().with { + new BlockJson().tap { it.number = blocks[3].height it.hash = BlockHash.from(blocks[3].hash.value) it.totalDifficulty = blocks[3].difficulty - 1 it.timestamp = Instant.now() - return it - }, objectMapper) + }) head.follow(Flux.just(blocks[0], blocks[3], block3less)) def act = head.flux then: @@ -109,13 +108,12 @@ class DefaultEthereumHeadSpec extends Specification { def "Replaces with more difficult"() { when: def block3less = BlockContainer.from( - new BlockJson().with { + new BlockJson().tap { it.number = blocks[3].height it.hash = BlockHash.from(blocks[3].hash.value) it.totalDifficulty = blocks[3].difficulty + 1 it.timestamp = Instant.now() - return it - }, objectMapper) + }) head.follow(Flux.just(blocks[0], blocks[3], block3less)) def act = head.flux then: diff --git a/src/test/groovy/io/emeraldpay/dshackle/upstream/ethereum/EthereumFullBlocksReaderSpec.groovy b/src/test/groovy/io/emeraldpay/dshackle/upstream/ethereum/EthereumFullBlocksReaderSpec.groovy index ec504b15..326a966b 100644 --- a/src/test/groovy/io/emeraldpay/dshackle/upstream/ethereum/EthereumFullBlocksReaderSpec.groovy +++ b/src/test/groovy/io/emeraldpay/dshackle/upstream/ethereum/EthereumFullBlocksReaderSpec.groovy @@ -16,6 +16,7 @@ package io.emeraldpay.dshackle.upstream.ethereum import com.fasterxml.jackson.databind.ObjectMapper +import io.emeraldpay.dshackle.Global import io.emeraldpay.dshackle.cache.BlocksMemCache import io.emeraldpay.dshackle.cache.TxMemCache import io.emeraldpay.dshackle.data.BlockContainer @@ -39,7 +40,7 @@ class EthereumFullBlocksReaderSpec extends Specification { String hash3 = "0xa4e7a75dfd5f6a83b3304dc56bfa0abfd3fef01540d15edafc9683f9acd2a13b" String hash4 = "0xd3f34def3c56ba4e701540d15edaff9acd2a1c968a7ff83b3300ab5dfd5f6aab" - ObjectMapper objectMapper = TestingCommons.objectMapper() + ObjectMapper objectMapper = Global.objectMapper def tx1 = new TransactionJson().with { it.blockNumber = 100 @@ -113,15 +114,15 @@ class EthereumFullBlocksReaderSpec extends Specification { def txes = new TxMemCache() def blocks = new BlocksMemCache() - txes.add(TxContainer.from(tx1, objectMapper)) - txes.add(TxContainer.from(tx2, objectMapper)) - txes.add(TxContainer.from(tx3, objectMapper)) - txes.add(TxContainer.from(tx4, objectMapper)) - blocks.add(BlockContainer.from(block1, objectMapper)) - blocks.add(BlockContainer.from(block2, objectMapper)) - blocks.add(BlockContainer.from(block3, objectMapper)) + txes.add(TxContainer.from(tx1)) + txes.add(TxContainer.from(tx2)) + txes.add(TxContainer.from(tx3)) + txes.add(TxContainer.from(tx4)) + blocks.add(BlockContainer.from(block1)) + blocks.add(BlockContainer.from(block2)) + blocks.add(BlockContainer.from(block3)) - def full = new EthereumFullBlocksReader(objectMapper, blocks, txes) + def full = new EthereumFullBlocksReader(blocks, txes) when: def act = full.read(BlockId.from(block1.hash)).block() @@ -179,15 +180,15 @@ class EthereumFullBlocksReaderSpec extends Specification { def txes = new TxMemCache() def blocks = new BlocksMemCache() - txes.add(TxContainer.from(tx1, objectMapper)) - txes.add(TxContainer.from(tx2, objectMapper)) - txes.add(TxContainer.from(tx3, objectMapper)) - txes.add(TxContainer.from(tx4, objectMapper)) - blocks.add(BlockContainer.from(block1, objectMapper)) - blocks.add(BlockContainer.from(block2, objectMapper)) - blocks.add(BlockContainer.from(block3, objectMapper)) + txes.add(TxContainer.from(tx1)) + txes.add(TxContainer.from(tx2)) + txes.add(TxContainer.from(tx3)) + txes.add(TxContainer.from(tx4)) + blocks.add(BlockContainer.from(block1)) + blocks.add(BlockContainer.from(block2)) + blocks.add(BlockContainer.from(block3)) - def full = new EthereumFullBlocksReader(objectMapper, blocks, txes) + def full = new EthereumFullBlocksReader(blocks, txes) when: def act = full.read(BlockId.from(block3.hash)).block() @@ -204,10 +205,10 @@ class EthereumFullBlocksReaderSpec extends Specification { def txes = new TxMemCache() def blocks = new BlocksMemCache() - txes.add(TxContainer.from(tx1, objectMapper)) - blocks.add(BlockContainer.from(block1, objectMapper)) //missing tx2 in cache + txes.add(TxContainer.from(tx1)) + blocks.add(BlockContainer.from(block1)) //missing tx2 in cache - def full = new EthereumFullBlocksReader(objectMapper, blocks, txes) + def full = new EthereumFullBlocksReader(blocks, txes) when: def act = full.read(BlockId.from(block1.hash)).block() @@ -221,11 +222,11 @@ class EthereumFullBlocksReaderSpec extends Specification { def txes = new TxMemCache() def blocks = new BlocksMemCache() - txes.add(TxContainer.from(tx1, objectMapper)) - txes.add(TxContainer.from(tx2, objectMapper)) - txes.add(TxContainer.from(tx3, objectMapper)) + txes.add(TxContainer.from(tx1)) + txes.add(TxContainer.from(tx2)) + txes.add(TxContainer.from(tx3)) - def full = new EthereumFullBlocksReader(objectMapper, blocks, txes) + def full = new EthereumFullBlocksReader(blocks, txes) when: def act = full.read(BlockId.from(block1.hash)).block() diff --git a/src/test/groovy/io/emeraldpay/dshackle/upstream/ethereum/EthereumHeadLagObserverSpec.groovy b/src/test/groovy/io/emeraldpay/dshackle/upstream/ethereum/EthereumHeadLagObserverSpec.groovy index 7067843f..0d255b73 100644 --- a/src/test/groovy/io/emeraldpay/dshackle/upstream/ethereum/EthereumHeadLagObserverSpec.groovy +++ b/src/test/groovy/io/emeraldpay/dshackle/upstream/ethereum/EthereumHeadLagObserverSpec.groovy @@ -16,9 +16,7 @@ */ package io.emeraldpay.dshackle.upstream.ethereum -import com.fasterxml.jackson.databind.ObjectMapper import io.emeraldpay.dshackle.data.BlockContainer -import io.emeraldpay.dshackle.test.TestingCommons import io.emeraldpay.dshackle.upstream.Head import io.emeraldpay.dshackle.upstream.HeadLagObserver import io.emeraldpay.dshackle.upstream.Upstream @@ -35,8 +33,6 @@ import java.time.Instant class EthereumHeadLagObserverSpec extends Specification { - ObjectMapper objectMapper = TestingCommons.objectMapper() - def "Updates lag distance"() { setup: Head master = Mock() @@ -53,14 +49,12 @@ class EthereumHeadLagObserverSpec extends Specification { def blocks = [100, 101, 102].collect { i -> return BlockContainer.from( - new BlockJson().with { + new BlockJson().tap { it.number = i it.totalDifficulty = 2000 + i it.hash = BlockHash.from("0x3ec2ebf5d0ec474d0ac6bc50d2770d8409ad76e119968e7919f85d5ec8915" + i) it.timestamp = Instant.now() - return it - }, - objectMapper) + }) } def masterBus = TopicProcessor.create() @@ -97,14 +91,12 @@ class EthereumHeadLagObserverSpec extends Specification { def blocks = [100, 101, 102].collect { i -> return BlockContainer.from( - new BlockJson().with { + new BlockJson().tap { it.number = i it.totalDifficulty = 2000 + i it.hash = BlockHash.from("0x3ec2ebf5d0ec474d0ac6bc50d2770d8409ad76e119968e7919f85d5ec8915" + i) it.timestamp = Instant.now() - return it - }, - objectMapper) + }) } def upblocks = Flux.fromIterable(blocks) @@ -137,7 +129,7 @@ class EthereumHeadLagObserverSpec extends Specification { it.timestamp = Instant.now() return it } - delta as Long == observer.extractDistance(BlockContainer.from(top, objectMapper), BlockContainer.from(curr, objectMapper)) + delta as Long == observer.extractDistance(BlockContainer.from(top), BlockContainer.from(curr)) where: topHeight | topDiff | currHeight | currDiff | delta 100 | 1000 | 100 | 1000 | 0 diff --git a/src/test/groovy/io/emeraldpay/dshackle/upstream/ethereum/EthereumReaderSpec.groovy b/src/test/groovy/io/emeraldpay/dshackle/upstream/ethereum/EthereumReaderSpec.groovy index c8ee364c..6ce4dc3b 100644 --- a/src/test/groovy/io/emeraldpay/dshackle/upstream/ethereum/EthereumReaderSpec.groovy +++ b/src/test/groovy/io/emeraldpay/dshackle/upstream/ethereum/EthereumReaderSpec.groovy @@ -58,13 +58,12 @@ class EthereumReaderSpec extends Specification { def "Block by Id reads from cache"() { setup: def memCache = Mock(BlocksMemCache) { - 1 * read(blockId) >> Mono.just(BlockContainer.from(blockJson, TestingCommons.objectMapper())) + 1 * read(blockId) >> Mono.just(BlockContainer.from(blockJson)) } def caches = Caches.newBuilder() .setBlockByHash(memCache) - .setObjectMapper(TestingCommons.objectMapper()) .build() - def reader = new EthereumReader(Stub(Multistream), caches, TestingCommons.objectMapper()) + def reader = new EthereumReader(Stub(Multistream), caches) when: def act = reader.blocksById().read(blockId).block() @@ -80,13 +79,12 @@ class EthereumReaderSpec extends Specification { } def caches = Caches.newBuilder() .setBlockByHash(memCache) - .setObjectMapper(TestingCommons.objectMapper()) .build() def api = TestingCommons.api() api.answer("eth_getBlockByHash", ["0xf85b826fdf98ee0f48f7db001be00472e63ceb056846f4ecac5f0c32878b8ab2", false], blockJson) def upstream = TestingCommons.aggregatedUpstream(api) - def reader = new EthereumReader(upstream, caches, TestingCommons.objectMapper()) + def reader = new EthereumReader(upstream, caches) when: def act = reader.blocksById().read(blockId).block() @@ -102,13 +100,12 @@ class EthereumReaderSpec extends Specification { } def caches = Caches.newBuilder() .setBlockByHash(memCache) - .setObjectMapper(TestingCommons.objectMapper()) .build() def api = TestingCommons.api() api.answer("eth_getBlockByHash", ["0xf85b826fdf98ee0f48f7db001be00472e63ceb056846f4ecac5f0c32878b8ab2", false], blockJson) def upstream = TestingCommons.aggregatedUpstream(api) - def reader = new EthereumReader(upstream, caches, TestingCommons.objectMapper()) + def reader = new EthereumReader(upstream, caches) when: def act = reader.blocksById().read(blockId).block() @@ -120,13 +117,12 @@ class EthereumReaderSpec extends Specification { def "Block by Hash reads from cache"() { setup: def memCache = Mock(BlocksMemCache) { - 1 * read(blockId) >> Mono.just(BlockContainer.from(blockJson, TestingCommons.objectMapper())) + 1 * read(blockId) >> Mono.just(BlockContainer.from(blockJson)) } def caches = Caches.newBuilder() .setBlockByHash(memCache) - .setObjectMapper(TestingCommons.objectMapper()) .build() - def reader = new EthereumReader(Stub(Multistream), caches, TestingCommons.objectMapper()) + def reader = new EthereumReader(Stub(Multistream), caches) when: def act = reader.blocksByHash().read(blockJson.hash).block() @@ -142,12 +138,11 @@ class EthereumReaderSpec extends Specification { } def caches = Caches.newBuilder() .setBlockByHash(memCache) - .setObjectMapper(TestingCommons.objectMapper()) .build() def api = TestingCommons.api() api.answer("eth_getBlockByHash", ["0xf85b826fdf98ee0f48f7db001be00472e63ceb056846f4ecac5f0c32878b8ab2", false], blockJson) def upstream = TestingCommons.aggregatedUpstream(api) - def reader = new EthereumReader(upstream, caches, TestingCommons.objectMapper()) + def reader = new EthereumReader(upstream, caches) when: def act = reader.blocksByHash().read(blockJson.hash).block() @@ -159,13 +154,12 @@ class EthereumReaderSpec extends Specification { def "Tx by Hash reads from cache"() { setup: def memCache = Mock(TxMemCache) { - 1 * read(txId) >> Mono.just(TxContainer.from(txJson, TestingCommons.objectMapper())) + 1 * read(txId) >> Mono.just(TxContainer.from(txJson)) } def caches = Caches.newBuilder() .setTxByHash(memCache) - .setObjectMapper(TestingCommons.objectMapper()) .build() - def reader = new EthereumReader(Stub(Multistream), caches, TestingCommons.objectMapper()) + def reader = new EthereumReader(Stub(Multistream), caches) when: def act = reader.txByHash().read(txJson.hash).block() @@ -181,13 +175,12 @@ class EthereumReaderSpec extends Specification { } def caches = Caches.newBuilder() .setTxByHash(memCache) - .setObjectMapper(TestingCommons.objectMapper()) .build() def api = TestingCommons.api() api.answer("eth_getTransactionByHash", [txJson.hash.toHex()], txJson) def upstream = TestingCommons.aggregatedUpstream(api) - def reader = new EthereumReader(upstream, caches, TestingCommons.objectMapper()) + def reader = new EthereumReader(upstream, caches) when: def act = reader.txByHash().read(txJson.hash).block() @@ -203,7 +196,7 @@ class EthereumReaderSpec extends Specification { api.answerOnce("eth_getBalance", ["0x70b91ff87a902b53dc6e2f6bda8bb9b330ccd30c", "latest"], "0xff") EthereumUpstreamMock upstream = new EthereumUpstreamMock(Chain.ETHEREUM, api) def upstreams = TestingCommons.aggregatedUpstream(upstream) - def reader = new EthereumReader(upstreams, Caches.default(TestingCommons.objectMapper()), TestingCommons.objectMapper()) + def reader = new EthereumReader(upstreams, Caches.default()) reader.start() when: @@ -225,7 +218,7 @@ class EthereumReaderSpec extends Specification { it.number++ it.totalDifficulty = BigInteger.TWO } - upstream.nextBlock(BlockContainer.from(block2, TestingCommons.objectMapper())) + upstream.nextBlock(BlockContainer.from(block2)) Thread.sleep(50) act = reader.balance().read(Address.from("0x70b91ff87a902b53dc6e2f6bda8bb9b330ccd30c")).block() diff --git a/src/test/groovy/io/emeraldpay/dshackle/upstream/ethereum/EthereumWsFactorySpec.groovy b/src/test/groovy/io/emeraldpay/dshackle/upstream/ethereum/EthereumWsFactorySpec.groovy index 99b9e0d8..088945f5 100644 --- a/src/test/groovy/io/emeraldpay/dshackle/upstream/ethereum/EthereumWsFactorySpec.groovy +++ b/src/test/groovy/io/emeraldpay/dshackle/upstream/ethereum/EthereumWsFactorySpec.groovy @@ -15,17 +15,12 @@ */ package io.emeraldpay.dshackle.upstream.ethereum -import com.fasterxml.jackson.databind.ObjectMapper import io.emeraldpay.dshackle.cache.BlocksMemCache -import io.emeraldpay.dshackle.cache.Caches import io.emeraldpay.dshackle.data.BlockContainer -import io.emeraldpay.dshackle.data.BlockId import io.emeraldpay.dshackle.test.TestingCommons import io.infinitape.etherjar.domain.BlockHash -import io.infinitape.etherjar.rpc.ReactorRpcClient import io.infinitape.etherjar.rpc.json.BlockJson import io.infinitape.etherjar.rpc.json.TransactionRefJson -import reactor.core.publisher.Mono import reactor.test.StepVerifier import spock.lang.Specification @@ -35,11 +30,9 @@ import java.time.temporal.ChronoUnit class EthereumWsFactorySpec extends Specification { - ObjectMapper objectMapper = TestingCommons.objectMapper() - def "Fetch block"() { setup: - def wsf = new EthereumWsFactory(new URI("http://localhost"), new URI("http://localhost"), objectMapper) + def wsf = new EthereumWsFactory(new URI("http://localhost"), new URI("http://localhost")) def blocksCache = Mock(BlocksMemCache) def block = new BlockJson() @@ -61,7 +54,7 @@ class EthereumWsFactorySpec extends Specification { then: StepVerifier.create(ws.flux.take(1)) - .expectNext(BlockContainer.from(block, objectMapper)) + .expectNext(BlockContainer.from(block)) .expectComplete() .verify(Duration.ofSeconds(1)) } diff --git a/src/test/groovy/io/emeraldpay/dshackle/upstream/ethereum/NativeCallRouterSpec.groovy b/src/test/groovy/io/emeraldpay/dshackle/upstream/ethereum/NativeCallRouterSpec.groovy index f35c516f..3bdb147e 100644 --- a/src/test/groovy/io/emeraldpay/dshackle/upstream/ethereum/NativeCallRouterSpec.groovy +++ b/src/test/groovy/io/emeraldpay/dshackle/upstream/ethereum/NativeCallRouterSpec.groovy @@ -1,11 +1,9 @@ package io.emeraldpay.dshackle.upstream.ethereum import io.emeraldpay.dshackle.cache.Caches -import io.emeraldpay.dshackle.reader.EmptyReader import io.emeraldpay.dshackle.test.TestingCommons import io.emeraldpay.dshackle.upstream.calls.DefaultEthereumMethods import io.emeraldpay.dshackle.upstream.rpcclient.JsonRpcRequest -import io.emeraldpay.dshackle.upstream.rpcclient.JsonRpcResponse import io.emeraldpay.grpc.Chain import spock.lang.Specification @@ -15,13 +13,11 @@ class NativeCallRouterSpec extends Specification { def "Calls hardcoded"() { setup: - def methods = new DefaultEthereumMethods(TestingCommons.objectMapper(), Chain.ETHEREUM) + def methods = new DefaultEthereumMethods(Chain.ETHEREUM) def router = new NativeCallRouter( - TestingCommons.objectMapper(), new EthereumReader( TestingCommons.aggregatedUpstream(TestingCommons.api()), - Caches.default(TestingCommons.objectMapper()), - TestingCommons.objectMapper() + Caches.default() ), methods ) diff --git a/src/test/groovy/io/emeraldpay/dshackle/upstream/grpc/EthereumGrpcUpstreamSpec.groovy b/src/test/groovy/io/emeraldpay/dshackle/upstream/grpc/EthereumGrpcUpstreamSpec.groovy index 5b236dde..fe125a63 100644 --- a/src/test/groovy/io/emeraldpay/dshackle/upstream/grpc/EthereumGrpcUpstreamSpec.groovy +++ b/src/test/groovy/io/emeraldpay/dshackle/upstream/grpc/EthereumGrpcUpstreamSpec.groovy @@ -21,6 +21,7 @@ import com.google.protobuf.ByteString import io.emeraldpay.api.proto.BlockchainGrpc import io.emeraldpay.api.proto.BlockchainOuterClass import io.emeraldpay.api.proto.Common +import io.emeraldpay.dshackle.Global import io.emeraldpay.dshackle.data.BlockId import io.emeraldpay.dshackle.test.MockGrpcServer import io.emeraldpay.dshackle.test.TestingCommons @@ -39,7 +40,7 @@ import java.util.concurrent.CompletableFuture class EthereumGrpcUpstreamSpec extends Specification { MockGrpcServer mockServer = new MockGrpcServer() - ObjectMapper objectMapper = TestingCommons.objectMapper() + ObjectMapper objectMapper = Global.objectMapper def "Subscribe to head"() { setup: @@ -72,7 +73,7 @@ class EthereumGrpcUpstreamSpec extends Specification { ) } }) - def upstream = new EthereumGrpcUpstream("test", chain, client, objectMapper, new JsonRpcGrpcClient(client, chain, objectMapper)) + def upstream = new EthereumGrpcUpstream("test", chain, client, new JsonRpcGrpcClient(client, chain)) upstream.setLag(0) upstream.init(BlockchainOuterClass.DescribeChain.newBuilder() .addAllSupportedMethods(["eth_getBlockByHash"]) @@ -129,7 +130,7 @@ class EthereumGrpcUpstreamSpec extends Specification { ) } }) - def upstream = new EthereumGrpcUpstream("test", Chain.ETHEREUM, client, objectMapper, new JsonRpcGrpcClient(client, Chain.ETHEREUM, objectMapper)) + def upstream = new EthereumGrpcUpstream("test", Chain.ETHEREUM, client, new JsonRpcGrpcClient(client, Chain.ETHEREUM)) upstream.setLag(0) upstream.init(BlockchainOuterClass.DescribeChain.newBuilder() .addAllSupportedMethods(["eth_getBlockByHash"]) @@ -190,7 +191,7 @@ class EthereumGrpcUpstreamSpec extends Specification { finished.complete(true) } }) - def upstream = new EthereumGrpcUpstream("test", chain, client, objectMapper, new JsonRpcGrpcClient(client, chain, objectMapper)) + def upstream = new EthereumGrpcUpstream("test", chain, client, new JsonRpcGrpcClient(client, chain)) upstream.setLag(0) upstream.init(BlockchainOuterClass.DescribeChain.newBuilder() .addAllSupportedMethods(["eth_getBlockByHash"]) diff --git a/src/test/groovy/io/emeraldpay/dshackle/upstream/rpcclient/JsonRpcHttpClientSpec.groovy b/src/test/groovy/io/emeraldpay/dshackle/upstream/rpcclient/JsonRpcHttpClientSpec.groovy index 7ab3ddc6..9b019b5b 100644 --- a/src/test/groovy/io/emeraldpay/dshackle/upstream/rpcclient/JsonRpcHttpClientSpec.groovy +++ b/src/test/groovy/io/emeraldpay/dshackle/upstream/rpcclient/JsonRpcHttpClientSpec.groovy @@ -40,7 +40,7 @@ class JsonRpcHttpClientSpec extends Specification { def "Make a request"() { setup: - JsonRpcHttpClient client = new JsonRpcHttpClient("localhost:18332", TestingCommons.objectMapper(), null, null) + JsonRpcHttpClient client = new JsonRpcHttpClient("localhost:18332", null, null) def resp = '{' + ' "jsonrpc": "2.0",' + ' "result": "0x98de45",' + @@ -62,7 +62,7 @@ class JsonRpcHttpClientSpec extends Specification { def "Make request with basic auth"() { setup: def auth = new AuthConfig.ClientBasicAuth("user", "passwd") - def client = new JsonRpcHttpClient("localhost:18332", TestingCommons.objectMapper(), auth, null) + def client = new JsonRpcHttpClient("localhost:18332", auth, null) mockServer.when( HttpRequest.request() diff --git a/src/test/groovy/io/emeraldpay/dshackle/upstream/rpcclient/JsonRpcRequestSpec.groovy b/src/test/groovy/io/emeraldpay/dshackle/upstream/rpcclient/JsonRpcRequestSpec.groovy index 0d135a4a..44578ba8 100644 --- a/src/test/groovy/io/emeraldpay/dshackle/upstream/rpcclient/JsonRpcRequestSpec.groovy +++ b/src/test/groovy/io/emeraldpay/dshackle/upstream/rpcclient/JsonRpcRequestSpec.groovy @@ -24,7 +24,7 @@ class JsonRpcRequestSpec extends Specification { setup: def req = new JsonRpcRequest("test_foo", []) when: - def act = req.toJson(TestingCommons.objectMapper()) + def act = req.toJson() then: new String(act) == '{"jsonrpc":"2.0","id":1,"method":"test_foo","params":[]}' } @@ -33,7 +33,7 @@ class JsonRpcRequestSpec extends Specification { setup: def req = new JsonRpcRequest("test_foo", ["0x0000"]) when: - def act = req.toJson(TestingCommons.objectMapper()) + def act = req.toJson() then: new String(act) == '{"jsonrpc":"2.0","id":1,"method":"test_foo","params":["0x0000"]}' } @@ -42,7 +42,7 @@ class JsonRpcRequestSpec extends Specification { setup: def req = new JsonRpcRequest("test_foo", ["0x0000", false]) when: - def act = req.toJson(TestingCommons.objectMapper()) + def act = req.toJson() then: new String(act) == '{"jsonrpc":"2.0","id":1,"method":"test_foo","params":["0x0000",false]}' }