problem: unnecessary method for Head

This commit is contained in:
Igor Artamonov
2019-08-16 22:03:32 -04:00
parent 303d7e1164
commit 03ce6f74a5
14 changed files with 36 additions and 64 deletions

View File

@@ -83,7 +83,7 @@ class StreamHead(
fun notify(chain: Chain, client: TopicProcessor<BlockchainOuterClass.ChainHead>) {
val upstream = upstreams.getUpstream(chain) ?: return
val head = upstream.getHead().getHead()
val head = upstream.getHead().getFlux().next()
head.subscribe { block ->
UpstreamServices.onceOk(upstream).subscribe { avail ->
if (avail) {

View File

@@ -210,7 +210,7 @@ class TrackTx(
mined = true,
confirmations = 1
)
upstream.getHead().getHead().map { head ->
upstream.getHead().getFlux().next().map { head ->
val height = updated.status.height
if (height == null || head.number < height) {
updated

View File

@@ -105,7 +105,7 @@ class ChainUpstreams (
fun printStatus() {
var height: Long? = null
try {
height = head!!.getHead().block(Duration.ofSeconds(1))?.number
height = head!!.getFlux().next().block(Duration.ofSeconds(1))?.number
} catch (e: IllegalStateException) {
//timout
} catch (e: Exception) {

View File

@@ -71,7 +71,7 @@ open class EthereumApi(
private fun callUpstream(method: String, params: List<Any>): Mono<out Any> {
if (method == "eth_blockNumber") {
val current = upstream?.getHead()?.getHead()?.let { head ->
val current = upstream?.getHead()?.getFlux()?.next()?.let { head ->
head.map { HexQuantity.from(it.number).toHex() }
}
if (current != null) {

View File

@@ -43,18 +43,11 @@ class EthereumHeadMerge(
}
}
override fun getHead(): Mono<BlockJson<TransactionId>> {
val curr = head.get()
if (curr != null) {
return Mono.just(curr)
}
return getFlux().next()
}
override fun getFlux(): Flux<BlockJson<TransactionId>> {
return Flux.from(this.flux)
.onBackpressureLatest()
return Flux.merge(
Mono.justOrEmpty(head.get()),
Flux.from(this.flux)
).onBackpressureLatest()
}
override fun stop() {

View File

@@ -60,17 +60,11 @@ class EthereumRpcHead(
refreshSubscription = null
}
override fun getHead(): Mono<BlockJson<TransactionId>> {
val current = head.get()
if (current != null) {
return Mono.just(current)
}
return Mono.from(stream)
}
override fun getFlux(): Flux<BlockJson<TransactionId>> {
return Flux.from(stream)
return Flux.merge(
Mono.justOrEmpty(head.get()),
Flux.from(stream)
).onBackpressureLatest()
}
}

View File

@@ -63,7 +63,7 @@ open class EthereumUpstream(
val rpc = EthereumRpcHead(api).apply {
this.start()
}
val currentHead = rpc.getHead().doFinally {
val currentHead = rpc.getFlux().next().doFinally {
rpc.stop()
}
EthereumHeadMerge(listOf(currentHead, ws.getFlux())).apply {

View File

@@ -20,16 +20,13 @@ class EthereumWsHead(
private val head = AtomicReference<BlockJson<TransactionId>>(null)
private var stream: Flux<BlockJson<TransactionId>>? = null
override fun getHead(): Mono<BlockJson<TransactionId>> {
val current = head.get()
if (current != null) {
return Mono.just(current)
}
return Mono.from(getFlux())
}
override fun getFlux(): Flux<BlockJson<TransactionId>> {
return stream?.let { Flux.from(it) } ?: Flux.error(Exception("Not started"))
return stream?.let {
Flux.merge(
Mono.justOrEmpty(head.get()),
Flux.from(this.stream)
).onBackpressureLatest()
} ?: Flux.error(Exception("Not started"))
}
override fun isRunning(): Boolean {

View File

@@ -168,16 +168,11 @@ open class GrpcUpstream(
val upstream: GrpcUpstream
): EthereumHead {
override fun getHead(): Mono<BlockJson<TransactionId>> {
val current = upstream.headBlock.get()
if (current != null) {
return Mono.just(current)
}
return Mono.from(upstream.streamBlocks)
}
override fun getFlux(): Flux<BlockJson<TransactionId>> {
return Flux.from(upstream.streamBlocks)
return Flux.merge(
Mono.justOrEmpty(upstream.headBlock.get()),
Flux.from(upstream.streamBlocks)
)
}
}

View File

@@ -4,8 +4,5 @@ import reactor.core.publisher.Flux
import reactor.core.publisher.Mono
interface Head<T> {
fun getHead(): Mono<T>
fun getFlux(): Flux<T>
}

View File

@@ -52,8 +52,7 @@ class HeadLagObserver (
fun getCurrentBlocks(up: Upstream): Flux<BlockJson<TransactionId>> {
val head = up.getHead()
return Flux.concat(head.getHead(), head.getFlux())
.take(Duration.ofSeconds(1))
return head.getFlux().take(Duration.ofSeconds(1))
}
fun mapLagging(top: BlockJson<TransactionId>, up: Upstream, blocks: Flux<BlockJson<TransactionId>>): Flux<Tuple2<Long, Upstream>> {