diff --git a/src/main/kotlin/io/emeraldpay/dshackle/cache/BlocksMemCache.kt b/src/main/kotlin/io/emeraldpay/dshackle/cache/BlocksMemCache.kt index dd036087..f44b7e36 100644 --- a/src/main/kotlin/io/emeraldpay/dshackle/cache/BlocksMemCache.kt +++ b/src/main/kotlin/io/emeraldpay/dshackle/cache/BlocksMemCache.kt @@ -18,6 +18,7 @@ package io.emeraldpay.dshackle.cache import io.infinitape.etherjar.domain.BlockHash import io.infinitape.etherjar.domain.TransactionId import io.infinitape.etherjar.rpc.json.BlockJson +import io.infinitape.etherjar.rpc.json.TransactionRefJson import reactor.core.publisher.Mono import java.util.concurrent.ConcurrentHashMap import java.util.concurrent.ConcurrentLinkedQueue @@ -26,14 +27,14 @@ class BlocksMemCache( val maxSize: Int = 64 ) { - private val mapping = ConcurrentHashMap>() + private val mapping = ConcurrentHashMap>() private val queue = ConcurrentLinkedQueue() - fun get(hash: BlockHash): Mono> { + fun get(hash: BlockHash): Mono> { return Mono.justOrEmpty(mapping[hash]) } - fun add(block: BlockJson) { + fun add(block: BlockJson) { mapping.put(block.hash, block) queue.add(block.hash) diff --git a/src/main/kotlin/io/emeraldpay/dshackle/quorum/AlwaysQuorum.kt b/src/main/kotlin/io/emeraldpay/dshackle/quorum/AlwaysQuorum.kt index 619a36ef..84ae4462 100644 --- a/src/main/kotlin/io/emeraldpay/dshackle/quorum/AlwaysQuorum.kt +++ b/src/main/kotlin/io/emeraldpay/dshackle/quorum/AlwaysQuorum.kt @@ -19,13 +19,14 @@ import io.emeraldpay.dshackle.upstream.Head import io.emeraldpay.dshackle.upstream.Upstream import io.infinitape.etherjar.domain.TransactionId import io.infinitape.etherjar.rpc.json.BlockJson +import io.infinitape.etherjar.rpc.json.TransactionRefJson open class AlwaysQuorum: CallQuorum { private var resolved = false private var result: ByteArray? = null - override fun init(head: Head>) { + override fun init(head: Head>) { } override fun isResolved(): Boolean { diff --git a/src/main/kotlin/io/emeraldpay/dshackle/quorum/BroadcastQuorum.kt b/src/main/kotlin/io/emeraldpay/dshackle/quorum/BroadcastQuorum.kt index abdc4e33..88e970fe 100644 --- a/src/main/kotlin/io/emeraldpay/dshackle/quorum/BroadcastQuorum.kt +++ b/src/main/kotlin/io/emeraldpay/dshackle/quorum/BroadcastQuorum.kt @@ -20,6 +20,7 @@ import io.emeraldpay.dshackle.upstream.Upstream import io.infinitape.etherjar.domain.TransactionId import io.infinitape.etherjar.rpc.JacksonRpcConverter import io.infinitape.etherjar.rpc.json.BlockJson +import io.infinitape.etherjar.rpc.json.TransactionRefJson open class BroadcastQuorum( jacksonRpcConverter: JacksonRpcConverter, @@ -30,7 +31,7 @@ open class BroadcastQuorum( private var txid: String? = null private var calls = 0 - override fun init(head: Head>) { + override fun init(head: Head>) { } override fun isResolved(): Boolean { diff --git a/src/main/kotlin/io/emeraldpay/dshackle/quorum/CallQuorum.kt b/src/main/kotlin/io/emeraldpay/dshackle/quorum/CallQuorum.kt index 920f7b3f..80e7ec3c 100644 --- a/src/main/kotlin/io/emeraldpay/dshackle/quorum/CallQuorum.kt +++ b/src/main/kotlin/io/emeraldpay/dshackle/quorum/CallQuorum.kt @@ -19,13 +19,14 @@ import io.emeraldpay.dshackle.upstream.Head import io.emeraldpay.dshackle.upstream.Upstream import io.infinitape.etherjar.domain.TransactionId import io.infinitape.etherjar.rpc.json.BlockJson +import io.infinitape.etherjar.rpc.json.TransactionRefJson import reactor.util.function.Tuple2 import java.util.function.BiFunction import java.util.function.Predicate interface CallQuorum { - fun init(head: Head>) + fun init(head: Head>) fun isResolved(): Boolean fun record(response: ByteArray, upstream: Upstream): Boolean diff --git a/src/main/kotlin/io/emeraldpay/dshackle/quorum/NonEmptyQuorum.kt b/src/main/kotlin/io/emeraldpay/dshackle/quorum/NonEmptyQuorum.kt index 0b3b582e..11f7174c 100644 --- a/src/main/kotlin/io/emeraldpay/dshackle/quorum/NonEmptyQuorum.kt +++ b/src/main/kotlin/io/emeraldpay/dshackle/quorum/NonEmptyQuorum.kt @@ -20,6 +20,7 @@ import io.emeraldpay.dshackle.upstream.Upstream import io.infinitape.etherjar.domain.TransactionId import io.infinitape.etherjar.rpc.JacksonRpcConverter import io.infinitape.etherjar.rpc.json.BlockJson +import io.infinitape.etherjar.rpc.json.TransactionRefJson open class NonEmptyQuorum( jacksonRpcConverter: JacksonRpcConverter, @@ -29,7 +30,7 @@ open class NonEmptyQuorum( private var result: ByteArray? = null private var tries: Int = 0 - override fun init(head: Head>) { + override fun init(head: Head>) { } override fun isResolved(): Boolean { diff --git a/src/main/kotlin/io/emeraldpay/dshackle/quorum/NonceQuorum.kt b/src/main/kotlin/io/emeraldpay/dshackle/quorum/NonceQuorum.kt index 902a1c27..401faee0 100644 --- a/src/main/kotlin/io/emeraldpay/dshackle/quorum/NonceQuorum.kt +++ b/src/main/kotlin/io/emeraldpay/dshackle/quorum/NonceQuorum.kt @@ -21,6 +21,7 @@ import io.infinitape.etherjar.domain.TransactionId import io.infinitape.etherjar.hex.HexQuantity import io.infinitape.etherjar.rpc.JacksonRpcConverter import io.infinitape.etherjar.rpc.json.BlockJson +import io.infinitape.etherjar.rpc.json.TransactionRefJson import java.util.concurrent.locks.ReentrantLock import kotlin.concurrent.withLock @@ -35,7 +36,7 @@ open class NonceQuorum( private var receivedTimes = 0 private var errors = 0 - override fun init(head: Head>) { + override fun init(head: Head>) { } override fun isResolved(): Boolean { diff --git a/src/main/kotlin/io/emeraldpay/dshackle/quorum/NotLaggingQuorum.kt b/src/main/kotlin/io/emeraldpay/dshackle/quorum/NotLaggingQuorum.kt index bcdd88d9..b016a1cf 100644 --- a/src/main/kotlin/io/emeraldpay/dshackle/quorum/NotLaggingQuorum.kt +++ b/src/main/kotlin/io/emeraldpay/dshackle/quorum/NotLaggingQuorum.kt @@ -19,13 +19,14 @@ import io.emeraldpay.dshackle.upstream.Head import io.emeraldpay.dshackle.upstream.Upstream import io.infinitape.etherjar.domain.TransactionId import io.infinitape.etherjar.rpc.json.BlockJson +import io.infinitape.etherjar.rpc.json.TransactionRefJson import java.util.concurrent.atomic.AtomicReference class NotLaggingQuorum(val maxLag: Long = 0): CallQuorum { private val result: AtomicReference = AtomicReference() - override fun init(head: Head>) { + override fun init(head: Head>) { } override fun isResolved(): Boolean { diff --git a/src/main/kotlin/io/emeraldpay/dshackle/reader/BlockApiReader.kt b/src/main/kotlin/io/emeraldpay/dshackle/reader/BlockApiReader.kt index a5bdcd90..ab7cd650 100644 --- a/src/main/kotlin/io/emeraldpay/dshackle/reader/BlockApiReader.kt +++ b/src/main/kotlin/io/emeraldpay/dshackle/reader/BlockApiReader.kt @@ -21,15 +21,16 @@ import io.infinitape.etherjar.domain.BlockHash import io.infinitape.etherjar.domain.TransactionId import io.infinitape.etherjar.rpc.Commands import io.infinitape.etherjar.rpc.json.BlockJson +import io.infinitape.etherjar.rpc.json.TransactionRefJson import reactor.core.publisher.Mono import reactor.retry.Repeat import java.time.Duration class BlockApiReader( val upstream: Upstream -): Reader> { +): Reader> { - override fun read(key: BlockHash): Mono> { + override fun read(key: BlockHash): Mono> { return Mono.just(key) .flatMap { upstream.getApi(Selector.empty) diff --git a/src/main/kotlin/io/emeraldpay/dshackle/reader/BlockCacheReader.kt b/src/main/kotlin/io/emeraldpay/dshackle/reader/BlockCacheReader.kt index 8083f808..7c1e4925 100644 --- a/src/main/kotlin/io/emeraldpay/dshackle/reader/BlockCacheReader.kt +++ b/src/main/kotlin/io/emeraldpay/dshackle/reader/BlockCacheReader.kt @@ -19,13 +19,14 @@ import io.emeraldpay.dshackle.cache.BlocksMemCache import io.infinitape.etherjar.domain.BlockHash import io.infinitape.etherjar.domain.TransactionId import io.infinitape.etherjar.rpc.json.BlockJson +import io.infinitape.etherjar.rpc.json.TransactionRefJson import reactor.core.publisher.Mono class BlockCacheReader( val cache: BlocksMemCache -): Reader> { +): Reader> { - override fun read(key: BlockHash): Mono> { + override fun read(key: BlockHash): Mono> { return cache.get(key) } } \ No newline at end of file diff --git a/src/main/kotlin/io/emeraldpay/dshackle/rpc/StreamHead.kt b/src/main/kotlin/io/emeraldpay/dshackle/rpc/StreamHead.kt index 7b82d5c5..a62dd066 100644 --- a/src/main/kotlin/io/emeraldpay/dshackle/rpc/StreamHead.kt +++ b/src/main/kotlin/io/emeraldpay/dshackle/rpc/StreamHead.kt @@ -22,6 +22,7 @@ import io.emeraldpay.dshackle.upstream.Upstreams import io.emeraldpay.grpc.Chain import io.infinitape.etherjar.domain.TransactionId import io.infinitape.etherjar.rpc.json.BlockJson +import io.infinitape.etherjar.rpc.json.TransactionRefJson import org.slf4j.LoggerFactory import org.springframework.beans.factory.annotation.Autowired import org.springframework.stereotype.Service @@ -47,7 +48,7 @@ class StreamHead( } } - fun asProto(chain: Chain, block: BlockJson): BlockchainOuterClass.ChainHead { + fun asProto(chain: Chain, block: BlockJson): BlockchainOuterClass.ChainHead { return BlockchainOuterClass.ChainHead.newBuilder() .setChainValue(chain.id) .setHeight(block.number) diff --git a/src/main/kotlin/io/emeraldpay/dshackle/rpc/TrackTx.kt b/src/main/kotlin/io/emeraldpay/dshackle/rpc/TrackTx.kt index 0e97fe27..ce24a5f7 100644 --- a/src/main/kotlin/io/emeraldpay/dshackle/rpc/TrackTx.kt +++ b/src/main/kotlin/io/emeraldpay/dshackle/rpc/TrackTx.kt @@ -27,6 +27,7 @@ import io.infinitape.etherjar.domain.TransactionId import io.infinitape.etherjar.rpc.Commands import io.infinitape.etherjar.rpc.json.BlockJson import io.infinitape.etherjar.rpc.json.TransactionJson +import io.infinitape.etherjar.rpc.json.TransactionRefJson import org.slf4j.LoggerFactory import org.springframework.beans.factory.annotation.Autowired import org.springframework.scheduling.annotation.Scheduled @@ -191,7 +192,7 @@ class TrackTx( } } - fun setBlockDetails(tx: TxDetails, block: BlockJson): TxDetails { + fun setBlockDetails(tx: TxDetails, block: BlockJson): TxDetails { return if (block.number != null && block.totalDifficulty != null) { tx.withStatus( blockTotalDifficulty = block.totalDifficulty, diff --git a/src/main/kotlin/io/emeraldpay/dshackle/upstream/AggregatedUpstream.kt b/src/main/kotlin/io/emeraldpay/dshackle/upstream/AggregatedUpstream.kt index 620f05a7..cf7e4bc6 100644 --- a/src/main/kotlin/io/emeraldpay/dshackle/upstream/AggregatedUpstream.kt +++ b/src/main/kotlin/io/emeraldpay/dshackle/upstream/AggregatedUpstream.kt @@ -26,6 +26,7 @@ import io.emeraldpay.dshackle.upstream.ethereum.EthereumHead import io.infinitape.etherjar.domain.BlockHash import io.infinitape.etherjar.domain.TransactionId import io.infinitape.etherjar.rpc.json.BlockJson +import io.infinitape.etherjar.rpc.json.TransactionRefJson import org.reactivestreams.Publisher import org.springframework.context.Lifecycle import reactor.core.Disposable @@ -43,7 +44,7 @@ abstract class AggregatedUpstream( private val blocksCache = BlocksMemCache() private var cacheSubscription: Disposable? = null - private val blockReader: Reader> = CompoundReader( + private val blockReader: Reader> = CompoundReader( listOf(BlockCacheReader(blocksCache)) ) var cache: CachingEthereumApi = CachingEthereumApi.empty() diff --git a/src/main/kotlin/io/emeraldpay/dshackle/upstream/CachingEthereumApi.kt b/src/main/kotlin/io/emeraldpay/dshackle/upstream/CachingEthereumApi.kt index 10343f77..0579a45c 100644 --- a/src/main/kotlin/io/emeraldpay/dshackle/upstream/CachingEthereumApi.kt +++ b/src/main/kotlin/io/emeraldpay/dshackle/upstream/CachingEthereumApi.kt @@ -26,12 +26,13 @@ import io.infinitape.etherjar.domain.TransactionId import io.infinitape.etherjar.hex.HexQuantity import io.infinitape.etherjar.rpc.json.BlockJson import io.infinitape.etherjar.rpc.json.ResponseJson +import io.infinitape.etherjar.rpc.json.TransactionRefJson import reactor.core.publisher.Mono import java.util.function.Function open class CachingEthereumApi( private val objectMapper: ObjectMapper, - private val cache: Reader>, + private val cache: Reader>, private val head: EthereumHead ): EthereumApi(objectMapper) { diff --git a/src/main/kotlin/io/emeraldpay/dshackle/upstream/HeadLagObserver.kt b/src/main/kotlin/io/emeraldpay/dshackle/upstream/HeadLagObserver.kt index 6e896b80..78fb3af0 100644 --- a/src/main/kotlin/io/emeraldpay/dshackle/upstream/HeadLagObserver.kt +++ b/src/main/kotlin/io/emeraldpay/dshackle/upstream/HeadLagObserver.kt @@ -18,6 +18,7 @@ package io.emeraldpay.dshackle.upstream import io.emeraldpay.dshackle.upstream.ethereum.EthereumHead import io.infinitape.etherjar.domain.TransactionId import io.infinitape.etherjar.rpc.json.BlockJson +import io.infinitape.etherjar.rpc.json.TransactionRefJson import org.slf4j.LoggerFactory import org.springframework.context.Lifecycle import reactor.core.Disposable @@ -57,7 +58,7 @@ class HeadLagObserver ( } } - fun probeFollowers(top: BlockJson): Flux> { + fun probeFollowers(top: BlockJson): Flux> { return followers.toFlux() .parallel(followers.size) .flatMap { mapLagging(top, it, getCurrentBlocks(it)) } @@ -65,12 +66,12 @@ class HeadLagObserver ( .onErrorContinue { t, _ -> log.warn("Failed to update lagging distance", t) } } - fun getCurrentBlocks(up: Upstream): Flux> { + fun getCurrentBlocks(up: Upstream): Flux> { val head = up.getHead() return head.getFlux().take(Duration.ofSeconds(1)) } - fun mapLagging(top: BlockJson, up: Upstream, blocks: Flux>): Flux> { + fun mapLagging(top: BlockJson, up: Upstream, blocks: Flux>): Flux> { return blocks .map { extractDistance(top, it) } .takeUntil{ lag -> lag <= 0L } @@ -80,7 +81,7 @@ class HeadLagObserver ( } } - fun extractDistance(top: BlockJson, curr: BlockJson): Long { + fun extractDistance(top: BlockJson, curr: BlockJson): Long { return when { curr.number > top.number -> if (curr.totalDifficulty >= top.totalDifficulty) 0 else forkDistance(top, curr) curr.number == top.number -> if (curr.totalDifficulty == top.totalDifficulty) 0 else forkDistance(top, curr) @@ -88,7 +89,7 @@ class HeadLagObserver ( } } - fun forkDistance(top: BlockJson, curr: BlockJson): Long { + fun forkDistance(top: BlockJson, curr: BlockJson): Long { //TODO look for common ancestor? though it may be a corruption return 6 } diff --git a/src/main/kotlin/io/emeraldpay/dshackle/upstream/ethereum/DefaultEthereumHead.kt b/src/main/kotlin/io/emeraldpay/dshackle/upstream/ethereum/DefaultEthereumHead.kt index aa30d10f..df9c9509 100644 --- a/src/main/kotlin/io/emeraldpay/dshackle/upstream/ethereum/DefaultEthereumHead.kt +++ b/src/main/kotlin/io/emeraldpay/dshackle/upstream/ethereum/DefaultEthereumHead.kt @@ -2,6 +2,7 @@ package io.emeraldpay.dshackle.upstream.ethereum import io.infinitape.etherjar.domain.TransactionId import io.infinitape.etherjar.rpc.json.BlockJson +import io.infinitape.etherjar.rpc.json.TransactionRefJson import org.slf4j.LoggerFactory import reactor.core.Disposable import reactor.core.publisher.Flux @@ -12,10 +13,10 @@ import java.util.concurrent.atomic.AtomicReference open class DefaultEthereumHead: EthereumHead { private val log = LoggerFactory.getLogger(DefaultEthereumHead::class.java) - private val head = AtomicReference>(null) - private val stream: TopicProcessor> = TopicProcessor.create() + private val head = AtomicReference>(null) + private val stream: TopicProcessor> = TopicProcessor.create() - fun follow(source: Flux>): Disposable { + fun follow(source: Flux>): Disposable { return source.distinctUntilChanged { it.hash }.filter { block -> @@ -37,14 +38,14 @@ open class DefaultEthereumHead: EthereumHead { } } - override fun getFlux(): Flux> { + override fun getFlux(): Flux> { return Flux.merge( Mono.justOrEmpty(head.get()), Flux.from(stream) ).onBackpressureLatest() } - fun getCurrent(): BlockJson? { + fun getCurrent(): BlockJson? { return head.get() } } \ No newline at end of file diff --git a/src/main/kotlin/io/emeraldpay/dshackle/upstream/ethereum/EmptyEthereumHead.kt b/src/main/kotlin/io/emeraldpay/dshackle/upstream/ethereum/EmptyEthereumHead.kt index 40e8e7eb..e276f38c 100644 --- a/src/main/kotlin/io/emeraldpay/dshackle/upstream/ethereum/EmptyEthereumHead.kt +++ b/src/main/kotlin/io/emeraldpay/dshackle/upstream/ethereum/EmptyEthereumHead.kt @@ -17,11 +17,12 @@ package io.emeraldpay.dshackle.upstream.ethereum import io.infinitape.etherjar.domain.TransactionId import io.infinitape.etherjar.rpc.json.BlockJson +import io.infinitape.etherjar.rpc.json.TransactionRefJson import reactor.core.publisher.Flux class EmptyEthereumHead : EthereumHead { - override fun getFlux(): Flux> { + override fun getFlux(): Flux> { return Flux.empty() } } \ No newline at end of file diff --git a/src/main/kotlin/io/emeraldpay/dshackle/upstream/ethereum/EthereumHead.kt b/src/main/kotlin/io/emeraldpay/dshackle/upstream/ethereum/EthereumHead.kt index 94f9a6d5..7841abe9 100644 --- a/src/main/kotlin/io/emeraldpay/dshackle/upstream/ethereum/EthereumHead.kt +++ b/src/main/kotlin/io/emeraldpay/dshackle/upstream/ethereum/EthereumHead.kt @@ -18,6 +18,7 @@ package io.emeraldpay.dshackle.upstream.ethereum import io.emeraldpay.dshackle.upstream.Head import io.infinitape.etherjar.domain.TransactionId import io.infinitape.etherjar.rpc.json.BlockJson +import io.infinitape.etherjar.rpc.json.TransactionRefJson -interface EthereumHead: Head> { +interface EthereumHead: Head> { } \ No newline at end of file diff --git a/src/main/kotlin/io/emeraldpay/dshackle/upstream/ethereum/EthereumHeadMerge.kt b/src/main/kotlin/io/emeraldpay/dshackle/upstream/ethereum/EthereumHeadMerge.kt index 9f3f6ab3..eeb84a84 100644 --- a/src/main/kotlin/io/emeraldpay/dshackle/upstream/ethereum/EthereumHeadMerge.kt +++ b/src/main/kotlin/io/emeraldpay/dshackle/upstream/ethereum/EthereumHeadMerge.kt @@ -17,6 +17,7 @@ package io.emeraldpay.dshackle.upstream.ethereum import io.infinitape.etherjar.domain.TransactionId import io.infinitape.etherjar.rpc.json.BlockJson +import io.infinitape.etherjar.rpc.json.TransactionRefJson import org.reactivestreams.Publisher import org.slf4j.LoggerFactory import org.springframework.context.Lifecycle @@ -26,7 +27,7 @@ import reactor.core.publisher.Mono import java.util.concurrent.atomic.AtomicReference class EthereumHeadMerge( - private val fluxes: Iterable>> + private val fluxes: Iterable>> ): DefaultEthereumHead(), Lifecycle { private var subscription: Disposable? = null diff --git a/src/main/kotlin/io/emeraldpay/dshackle/upstream/ethereum/EthereumWs.kt b/src/main/kotlin/io/emeraldpay/dshackle/upstream/ethereum/EthereumWs.kt index 9e3fe945..356ead74 100644 --- a/src/main/kotlin/io/emeraldpay/dshackle/upstream/ethereum/EthereumWs.kt +++ b/src/main/kotlin/io/emeraldpay/dshackle/upstream/ethereum/EthereumWs.kt @@ -20,6 +20,7 @@ import io.emeraldpay.dshackle.config.UpstreamsConfig import io.infinitape.etherjar.domain.TransactionId import io.infinitape.etherjar.rpc.Commands import io.infinitape.etherjar.rpc.json.BlockJson +import io.infinitape.etherjar.rpc.json.TransactionRefJson import io.infinitape.etherjar.rpc.ws.WebsocketClient import org.slf4j.LoggerFactory import reactor.core.publisher.Flux @@ -37,7 +38,7 @@ class EthereumWs( private val log = LoggerFactory.getLogger(EthereumWs::class.java) private val topic = TopicProcessor - .builder>() + .builder>() .name("new-blocks") .build() var basicAuth: UpstreamsConfig.BasicAuth? = null @@ -71,7 +72,7 @@ class EthereumWs( } } - fun getFlux(): Flux> { + fun getFlux(): Flux> { return Flux.from(this.topic) .onBackpressureLatest() } diff --git a/src/main/kotlin/io/emeraldpay/dshackle/upstream/grpc/GrpcUpstream.kt b/src/main/kotlin/io/emeraldpay/dshackle/upstream/grpc/GrpcUpstream.kt index 430c463e..da4b7a11 100644 --- a/src/main/kotlin/io/emeraldpay/dshackle/upstream/grpc/GrpcUpstream.kt +++ b/src/main/kotlin/io/emeraldpay/dshackle/upstream/grpc/GrpcUpstream.kt @@ -32,6 +32,7 @@ import io.infinitape.etherjar.domain.TransactionId import io.infinitape.etherjar.rpc.* import io.infinitape.etherjar.rpc.emerald.ReactorEmeraldClient import io.infinitape.etherjar.rpc.json.BlockJson +import io.infinitape.etherjar.rpc.json.TransactionRefJson import org.slf4j.LoggerFactory import org.springframework.context.Lifecycle import reactor.core.Disposable @@ -110,7 +111,7 @@ open class GrpcUpstream( internal fun observeHead(flux: Flux) { val base = flux.map { value -> - val block = BlockJson() + val block = BlockJson() block.number = value.height block.totalDifficulty = BigInteger(1, value.weight.toByteArray()) block.hash = BlockHash.from("0x"+value.blockId)