slot height matcher implementation (#353)

This commit is contained in:
Vyacheslav
2023-11-28 15:52:55 +02:00
committed by GitHub
parent bb3d56856c
commit 353a483cce
10 changed files with 81 additions and 4 deletions

View File

@@ -151,6 +151,10 @@ abstract class AbstractHead @JvmOverloads constructor(
return getCurrent()?.height
}
override fun getCurrentSlotHeight(): Long? {
return getCurrent()?.slot
}
override fun stop() {
stopping = true
future?.let {

View File

@@ -31,6 +31,10 @@ class EmptyHead : Head {
override fun getCurrentHeight(): Long? {
return null
}
override fun getCurrentSlotHeight(): Long? {
return null
}
override fun start() {
}

View File

@@ -38,6 +38,8 @@ interface Head {
fun getCurrentHeight(): Long?
fun getCurrentSlotHeight(): Long?
fun start()
fun stop()

View File

@@ -17,6 +17,7 @@ sealed class MatchesResponse {
is ExistsResponse -> "Label ${this.name} does not exist"
GrpcResponse -> "Upstream is not grpc"
is HeightResponse -> "Upstream height ${this.currentHeight} is less than ${this.height}"
is SlotHeightResponse -> "Upstream slot height ${this.currentSlotHeight} is less than ${this.slot}"
is MethodResponse -> "Method ${this.method} is not supported"
is MultiResponse ->
this.allResponses
@@ -75,6 +76,11 @@ sealed class MatchesResponse {
val currentHeight: Long,
) : MatchesResponse()
data class SlotHeightResponse(
val slot: Long,
val currentSlotHeight: Long,
) : MatchesResponse()
data class SameNodeResponse(
val upstreamHash: Byte,
) : MatchesResponse()

View File

@@ -25,6 +25,7 @@ import io.emeraldpay.dshackle.upstream.MatchesResponse.GrpcResponse
import io.emeraldpay.dshackle.upstream.MatchesResponse.HeightResponse
import io.emeraldpay.dshackle.upstream.MatchesResponse.NotMatchedResponse
import io.emeraldpay.dshackle.upstream.MatchesResponse.SameNodeResponse
import io.emeraldpay.dshackle.upstream.MatchesResponse.SlotHeightResponse
import io.emeraldpay.dshackle.upstream.MatchesResponse.Success
import org.apache.commons.lang3.StringUtils
import java.util.Collections
@@ -44,6 +45,9 @@ class Selector {
return selectors
.map {
when {
it.hasSlotHeightSelector() -> {
SlotMatcher(it.slotHeightSelector.slotHeight)
}
it.hasHeightSelector() -> {
val height = if (it.heightSelector.height == -1L) head.getCurrentHeight() else it.heightSelector.height
if (height == null) {
@@ -488,6 +492,39 @@ class Selector {
}
}
class SlotMatcher(val slotHeight: Long) : Matcher() {
override fun matchesWithCause(up: Upstream): MatchesResponse {
val currentHeight = up.getHead().getCurrentSlotHeight() ?: 0
return if (currentHeight >= slotHeight) {
Success
} else {
SlotHeightResponse(slotHeight, currentHeight)
}
}
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (other !is SlotMatcher) return false
if (slotHeight != other.slotHeight) return false
return true
}
override fun hashCode(): Int {
return slotHeight.hashCode()
}
override fun describeInternal(): String {
return "slot height $slotHeight"
}
override fun toString(): String {
return "Matcher: ${describeInternal()}"
}
}
class SameNodeMatcher(private val upstreamHash: Byte) : Matcher() {
override fun matchesWithCause(up: Upstream): MatchesResponse =

View File

@@ -63,6 +63,10 @@ class EnrichedMergedHead constructor(
return referenceHead.getCurrentHeight()
}
override fun getCurrentSlotHeight(): Long? {
return referenceHead.getCurrentSlotHeight()
}
override fun isRunning(): Boolean {
return cacheSub != null
}