Height matcher to proxy (#340)

This commit is contained in:
KirillPamPam
2023-11-27 14:54:28 +04:00
committed by GitHub
parent d0322a0dab
commit 4dc52091e3
4 changed files with 54 additions and 5 deletions

View File

@@ -285,9 +285,12 @@ open class NativeCall(
),
)
}
val requestMatcher = requestItem.selectorsList
.takeIf { it.isNotEmpty() }
?.run { Mono.just(Selector.convertToMatcher(this, upstream.getHead())) }
// for ethereum the actual block needed for the call may be specified in the call parameters
val callSpecificMatcher: Mono<Selector.Matcher> =
upstream.callSelector?.getMatcher(method, params, upstream.getHead(), passthrough) ?: Mono.empty()
requestMatcher ?: upstream.callSelector?.getMatcher(method, params, upstream.getHead(), passthrough) ?: Mono.empty()
return callSpecificMatcher.defaultIfEmpty(Selector.empty).map { csm ->
val matcher = Selector.Builder()
.withMatcher(csm)

View File

@@ -39,6 +39,26 @@ class Selector {
@JvmStatic
val anyLabel = AnyLabelMatcher()
@JvmStatic
fun convertToMatcher(selectors: List<BlockchainOuterClass.Selector>, head: Head): Matcher {
return selectors
.map {
when {
it.hasHeightSelector() -> {
val height = if (it.heightSelector.height == -1L) head.getCurrentHeight() else it.heightSelector.height
if (height == null) {
empty
} else {
HeightMatcher(height)
}
}
else -> empty
}
}.run {
MultiMatcher(this)
}
}
@JvmStatic
fun convertToMatcher(req: BlockchainOuterClass.Selector?): LabelSelectorMatcher {
return when {

View File

@@ -32,11 +32,37 @@ class SelectorSpec extends Specification {
private BlockchainOuterClass.Selector selectLabel2Selector = BlockchainOuterClass.Selector.newBuilder()
.setLabelSelector(selectLabel2).build()
def "Convert nothing"() {
def "Convert height selector"() {
setup:
def heightSelector = BlockchainOuterClass.Selector.newBuilder()
.setHeightSelector(
BlockchainOuterClass.HeightSelector.newBuilder()
.setHeight(10000)
.build()
)
.build()
when:
def act = Selector.convertToMatcher(null)
def act = Selector.convertToMatcher(List.of(heightSelector), Stub(Head))
then:
act.class == Selector.AnyLabelMatcher
act == new Selector.MultiMatcher(List.of(new Selector.HeightMatcher(10000)))
}
def "Convert height selector with latest"() {
setup:
def head = Mock(Head) {
1 * getCurrentHeight() >> 15000
}
def heightSelector = BlockchainOuterClass.Selector.newBuilder()
.setHeightSelector(
BlockchainOuterClass.HeightSelector.newBuilder()
.setHeight(-1)
.build()
)
.build()
when:
def act = Selector.convertToMatcher(List.of(heightSelector), head)
then:
act == new Selector.MultiMatcher(List.of(new Selector.HeightMatcher(15000)))
}
def "Convert LABEL match"() {