-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add determination of the lower block (#372)
- Loading branch information
1 parent
e79df25
commit eb774d7
Showing
26 changed files
with
607 additions
and
9 deletions.
There are no files selected for viewing
Submodule emerald-grpc
updated
from 52cd5c to f827ea
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
68 changes: 68 additions & 0 deletions
68
src/main/kotlin/io/emeraldpay/dshackle/upstream/LowerBoundBlockDetector.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,68 @@ | ||
package io.emeraldpay.dshackle.upstream | ||
|
||
import io.emeraldpay.dshackle.Chain | ||
import org.slf4j.LoggerFactory | ||
import reactor.core.publisher.Flux | ||
import reactor.core.publisher.Mono | ||
import java.time.Duration | ||
import java.util.concurrent.atomic.AtomicReference | ||
|
||
typealias LowerBoundBlockDetectorBuilder = (Chain, Upstream) -> LowerBoundBlockDetector | ||
|
||
fun Long.toHex() = "0x${this.toString(16)}" | ||
|
||
abstract class LowerBoundBlockDetector( | ||
private val chain: Chain, | ||
private val upstream: Upstream, | ||
) { | ||
private val currentLowerBlock = AtomicReference(LowerBlockData.default()) | ||
|
||
private val log = LoggerFactory.getLogger(this::class.java) | ||
|
||
fun lowerBlock(): Flux<LowerBlockData> { | ||
return Flux.interval( | ||
Duration.ofSeconds(15), | ||
Duration.ofSeconds(60), | ||
) | ||
.flatMap { lowerBlockDetect() } | ||
.filter { it.blockNumber > currentLowerBlock.get().blockNumber } | ||
.map { | ||
log.info("Lower block of ${upstream.getId()} $chain: block height - {}, slot - {}", it.blockNumber, it.slot ?: "NA") | ||
|
||
currentLowerBlock.set(it) | ||
it | ||
} | ||
} | ||
|
||
fun getCurrentLowerBlock(): LowerBlockData = currentLowerBlock.get() | ||
|
||
protected abstract fun lowerBlockDetect(): Mono<LowerBlockData> | ||
|
||
data class LowerBlockData( | ||
val blockNumber: Long, | ||
val slot: Long?, | ||
) : Comparable<LowerBlockData> { | ||
constructor(blockNumber: Long) : this(blockNumber, null) | ||
|
||
companion object { | ||
fun default() = LowerBlockData(0, 0) | ||
} | ||
|
||
override fun compareTo(other: LowerBlockData): Int { | ||
return this.blockNumber.compareTo(other.blockNumber) | ||
} | ||
} | ||
|
||
data class LowerBoundData( | ||
val left: Long, | ||
val right: Long, | ||
val current: Long, | ||
val found: Boolean, | ||
) { | ||
constructor(left: Long, right: Long) : this(left, right, 0, false) | ||
|
||
constructor(left: Long, right: Long, current: Long) : this(left, right, current, false) | ||
|
||
constructor(current: Long, found: Boolean) : this(0, 0, current, found) | ||
} | ||
} |
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
53 changes: 53 additions & 0 deletions
53
src/main/kotlin/io/emeraldpay/dshackle/upstream/RecursiveLowerBoundBlockDetector.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,53 @@ | ||
package io.emeraldpay.dshackle.upstream | ||
|
||
import io.emeraldpay.dshackle.Chain | ||
import reactor.core.publisher.Mono | ||
|
||
abstract class RecursiveLowerBoundBlockDetector( | ||
chain: Chain, | ||
private val upstream: Upstream, | ||
) : LowerBoundBlockDetector(chain, upstream) { | ||
|
||
override fun lowerBlockDetect(): Mono<LowerBlockData> { | ||
return Mono.just(upstream.getHead()) | ||
.flatMap { | ||
val currentHeight = it.getCurrentHeight() | ||
if (currentHeight == null) { | ||
Mono.empty() | ||
} else { | ||
Mono.just(LowerBoundData(0, currentHeight)) | ||
} | ||
} | ||
.expand { data -> | ||
if (data.found) { | ||
Mono.empty() | ||
} else { | ||
val middle = middleBlock(data) | ||
|
||
if (data.left > data.right) { | ||
val current = if (data.current == 0L) 1 else data.current | ||
Mono.just(LowerBoundData(current, true)) | ||
} else { | ||
hasState(middle) | ||
.map { | ||
if (it) { | ||
LowerBoundData(data.left, middle - 1, middle) | ||
} else { | ||
LowerBoundData(middle + 1, data.right, data.current) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
.filter { it.found } | ||
.next() | ||
.map { | ||
LowerBlockData(it.current) | ||
} | ||
} | ||
|
||
private fun middleBlock(lowerBoundData: LowerBoundData): Long = | ||
lowerBoundData.left + (lowerBoundData.right - lowerBoundData.left) / 2 | ||
|
||
protected abstract fun hasState(blockNumber: Long): Mono<Boolean> | ||
} |
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
27 changes: 27 additions & 0 deletions
27
src/main/kotlin/io/emeraldpay/dshackle/upstream/ethereum/EthereumLowerBoundBlockDetector.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,27 @@ | ||
package io.emeraldpay.dshackle.upstream.ethereum | ||
|
||
import io.emeraldpay.dshackle.Chain | ||
import io.emeraldpay.dshackle.upstream.RecursiveLowerBoundBlockDetector | ||
import io.emeraldpay.dshackle.upstream.Upstream | ||
import io.emeraldpay.dshackle.upstream.rpcclient.JsonRpcRequest | ||
import io.emeraldpay.dshackle.upstream.rpcclient.JsonRpcResponse | ||
import io.emeraldpay.dshackle.upstream.toHex | ||
import reactor.core.publisher.Mono | ||
|
||
class EthereumLowerBoundBlockDetector( | ||
chain: Chain, | ||
private val upstream: Upstream, | ||
) : RecursiveLowerBoundBlockDetector(chain, upstream) { | ||
|
||
override fun hasState(blockNumber: Long): Mono<Boolean> { | ||
return upstream.getIngressReader().read( | ||
JsonRpcRequest( | ||
"eth_getBalance", | ||
listOf("0x756F45E3FA69347A9A973A725E3C98bC4db0b5a0", blockNumber.toHex()), | ||
), | ||
) | ||
.flatMap(JsonRpcResponse::requireResult) | ||
.map { true } | ||
.onErrorReturn(false) | ||
} | ||
} |
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
Oops, something went wrong.