-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Кирилл
committed
Apr 17, 2024
1 parent
c377083
commit a82af84
Showing
5 changed files
with
80 additions
and
60 deletions.
There are no files selected for viewing
57 changes: 6 additions & 51 deletions
57
src/main/kotlin/io/emeraldpay/dshackle/upstream/ethereum/HeadLivenessValidator.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,59 +1,14 @@ | ||
package io.emeraldpay.dshackle.upstream.ethereum | ||
|
||
import io.emeraldpay.dshackle.ThrottledLogger | ||
import io.emeraldpay.dshackle.upstream.Head | ||
import org.slf4j.LoggerFactory | ||
import reactor.core.publisher.Flux | ||
import reactor.core.scheduler.Scheduler | ||
import java.time.Duration | ||
|
||
class HeadLivenessValidator( | ||
private val head: Head, | ||
private val expectedBlockTime: Duration, | ||
private val scheduler: Scheduler, | ||
private val upstreamId: String, | ||
) { | ||
companion object { | ||
const val CHECKED_BLOCKS_UNTIL_LIVE = 3 | ||
private val log = LoggerFactory.getLogger(HeadLivenessValidator::class.java) | ||
} | ||
interface HeadLivenessValidator { | ||
fun getFlux(): Flux<Boolean> | ||
} | ||
|
||
fun getFlux(): Flux<Boolean> { | ||
val headLiveness = head.headLiveness() | ||
// first we have moving window of 2 blocks and check that they are consecutive ones | ||
val headFlux = head.getFlux().map { it.height }.buffer(2, 1).map { | ||
it.last() - it.first() == 1L | ||
}.scan(Pair(0, true)) { acc, value -> | ||
// then we accumulate consecutive true events, false resets counter | ||
if (value) { | ||
Pair(acc.first + 1, true) | ||
} else { | ||
if (log.isDebugEnabled) { | ||
log.debug("non consecutive blocks in head for $upstreamId") | ||
} else { | ||
ThrottledLogger.log(log, "non consecutive blocks in head for $upstreamId") | ||
} | ||
Pair(0, false) | ||
} | ||
}.flatMap { (count, value) -> | ||
// we emit when we have false or checked CHECKED_BLOCKS_UNTIL_LIVE blocks | ||
// CHECKED_BLOCKS_UNTIL_LIVE blocks == (CHECKED_BLOCKS_UNTIL_LIVE - 1) consecutive true | ||
when { | ||
count >= (CHECKED_BLOCKS_UNTIL_LIVE - 1) -> Flux.just(true) | ||
!value -> Flux.just(false) | ||
else -> Flux.empty() | ||
} | ||
}.timeout( | ||
expectedBlockTime.multipliedBy(CHECKED_BLOCKS_UNTIL_LIVE.toLong() * 2), | ||
Flux.just(false).doOnNext { | ||
if (log.isDebugEnabled) { | ||
log.debug("head liveness check broken with timeout in $upstreamId") | ||
} else { | ||
ThrottledLogger.log(log, "head liveness check broken with timeout in $upstreamId") | ||
} | ||
}, | ||
).repeat().subscribeOn(scheduler) | ||
class NoHeadLivenessValidator : HeadLivenessValidator { | ||
|
||
return Flux.merge(headFlux, headLiveness) | ||
override fun getFlux(): Flux<Boolean> { | ||
return Flux.just(false) | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
src/main/kotlin/io/emeraldpay/dshackle/upstream/ethereum/HeadLivenessValidatorImpl.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package io.emeraldpay.dshackle.upstream.ethereum | ||
|
||
import io.emeraldpay.dshackle.ThrottledLogger | ||
import io.emeraldpay.dshackle.upstream.Head | ||
import org.slf4j.LoggerFactory | ||
import reactor.core.publisher.Flux | ||
import reactor.core.scheduler.Scheduler | ||
import java.time.Duration | ||
|
||
class HeadLivenessValidatorImpl( | ||
private val head: Head, | ||
private val expectedBlockTime: Duration, | ||
private val scheduler: Scheduler, | ||
private val upstreamId: String, | ||
) : HeadLivenessValidator { | ||
companion object { | ||
const val CHECKED_BLOCKS_UNTIL_LIVE = 3 | ||
private val log = LoggerFactory.getLogger(HeadLivenessValidatorImpl::class.java) | ||
} | ||
|
||
override fun getFlux(): Flux<Boolean> { | ||
val headLiveness = head.headLiveness() | ||
// first we have moving window of 2 blocks and check that they are consecutive ones | ||
val headFlux = head.getFlux().map { it.height }.buffer(2, 1).map { | ||
it.last() - it.first() == 1L | ||
}.scan(Pair(0, true)) { acc, value -> | ||
// then we accumulate consecutive true events, false resets counter | ||
if (value) { | ||
Pair(acc.first + 1, true) | ||
} else { | ||
if (log.isDebugEnabled) { | ||
log.debug("non consecutive blocks in head for $upstreamId") | ||
} else { | ||
ThrottledLogger.log(log, "non consecutive blocks in head for $upstreamId") | ||
} | ||
Pair(0, false) | ||
} | ||
}.flatMap { (count, value) -> | ||
// we emit when we have false or checked CHECKED_BLOCKS_UNTIL_LIVE blocks | ||
// CHECKED_BLOCKS_UNTIL_LIVE blocks == (CHECKED_BLOCKS_UNTIL_LIVE - 1) consecutive true | ||
when { | ||
count >= (CHECKED_BLOCKS_UNTIL_LIVE - 1) -> Flux.just(true) | ||
!value -> Flux.just(false) | ||
else -> Flux.empty() | ||
} | ||
}.timeout( | ||
expectedBlockTime.multipliedBy(CHECKED_BLOCKS_UNTIL_LIVE.toLong() * 2), | ||
Flux.just(false).doOnNext { | ||
if (log.isDebugEnabled) { | ||
log.debug("head liveness check broken with timeout in $upstreamId") | ||
} else { | ||
ThrottledLogger.log(log, "head liveness check broken with timeout in $upstreamId") | ||
} | ||
}, | ||
).repeat().subscribeOn(scheduler) | ||
|
||
return Flux.merge(headFlux, headLiveness) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters