-
Notifications
You must be signed in to change notification settings - Fork 15
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
Sep 12, 2024
1 parent
5fe044b
commit ed1de1b
Showing
11 changed files
with
165 additions
and
22 deletions.
There are no files selected for viewing
Submodule emerald-grpc
updated
from 1934aa to 263a8c
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
63 changes: 63 additions & 0 deletions
63
.../kotlin/io/emeraldpay/dshackle/upstream/error/EthereumDebugTraceLowerBoundErrorHandler.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,63 @@ | ||
package io.emeraldpay.dshackle.upstream.error | ||
|
||
import io.emeraldpay.dshackle.upstream.ChainRequest | ||
import io.emeraldpay.dshackle.upstream.Upstream | ||
import io.emeraldpay.dshackle.upstream.ethereum.EthereumLowerBoundStateDetector.Companion.stateErrors | ||
import io.emeraldpay.dshackle.upstream.lowerbound.LowerBoundType | ||
|
||
object EthereumDebugTraceLowerBoundErrorHandler : EthereumLowerBoundErrorHandler() { | ||
private val zeroTagIndexMethods = setOf( | ||
"trace_block", | ||
"arbtrace_block", | ||
"debug_traceBlockByNumber", | ||
) | ||
private val firstTagIndexMethods = setOf( | ||
"trace_callMany", | ||
"arbtrace_callMany", | ||
"debug_traceCall", | ||
) | ||
private val secondTagIndexMethods = setOf( | ||
"trace_call", | ||
"arbtrace_call", | ||
) | ||
|
||
private val applicableMethods = zeroTagIndexMethods + firstTagIndexMethods + secondTagIndexMethods | ||
|
||
private val errors = stateErrors | ||
.plus( | ||
setOf( | ||
"historical state not available", | ||
), | ||
) | ||
private val errorRegexp = Regex("block .* not found") | ||
|
||
override fun handle(upstream: Upstream, request: ChainRequest, errorMessage: String?) { | ||
val type = if (request.method.startsWith("debug")) LowerBoundType.DEBUG else LowerBoundType.TRACE | ||
try { | ||
if (canHandle(request, errorMessage)) { | ||
parseTagParam(request, tagIndex(request.method))?.let { | ||
upstream.updateLowerBound(it, type) | ||
} | ||
} | ||
} catch (e: RuntimeException) { | ||
log.warn("Couldn't update the {} lower bound of {}, reason - {}", type, upstream.getId(), e.message) | ||
} | ||
} | ||
|
||
override fun canHandle(request: ChainRequest, errorMessage: String?): Boolean { | ||
return (errors.any { errorMessage?.contains(it) ?: false } || (errorMessage?.matches(errorRegexp) ?: false)) && | ||
applicableMethods.contains(request.method) | ||
} | ||
|
||
override fun tagIndex(method: String): Int { | ||
return if (firstTagIndexMethods.contains(method)) { | ||
1 | ||
} else if (secondTagIndexMethods.contains(method)) { | ||
2 | ||
} else if (zeroTagIndexMethods.contains(method)) { | ||
0 | ||
} else { | ||
-1 | ||
} | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
src/main/kotlin/io/emeraldpay/dshackle/upstream/error/EthereumLowerBoundErrorHandler.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,24 @@ | ||
package io.emeraldpay.dshackle.upstream.error | ||
|
||
import io.emeraldpay.dshackle.upstream.ChainRequest | ||
import io.emeraldpay.dshackle.upstream.rpcclient.ListParams | ||
import org.slf4j.LoggerFactory | ||
|
||
abstract class EthereumLowerBoundErrorHandler : ErrorHandler { | ||
protected val log = LoggerFactory.getLogger(this::class.java) | ||
|
||
protected fun parseTagParam(request: ChainRequest, tagIndex: Int): Long? { | ||
if (tagIndex != -1 && request.params is ListParams) { | ||
val params = request.params.list | ||
if (params.size >= tagIndex) { | ||
val tag = params[tagIndex] | ||
if (tag is String && tag.startsWith("0x")) { | ||
return tag.substring(2).toLong(16) | ||
} | ||
} | ||
} | ||
return null | ||
} | ||
|
||
protected abstract fun tagIndex(method: String): Int | ||
} |
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
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
52 changes: 52 additions & 0 deletions
52
...lin/io/emeraldpay/dshackle/upstream/error/EthereumDebugTraceLowerBoundErrorHandlerTest.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,52 @@ | ||
package io.emeraldpay.dshackle.upstream.error | ||
|
||
import io.emeraldpay.dshackle.upstream.ChainRequest | ||
import io.emeraldpay.dshackle.upstream.Upstream | ||
import io.emeraldpay.dshackle.upstream.lowerbound.LowerBoundType | ||
import io.emeraldpay.dshackle.upstream.rpcclient.ListParams | ||
import org.junit.jupiter.api.Test | ||
import org.junit.jupiter.params.ParameterizedTest | ||
import org.junit.jupiter.params.provider.Arguments | ||
import org.junit.jupiter.params.provider.Arguments.of | ||
import org.junit.jupiter.params.provider.MethodSource | ||
import org.mockito.Mockito.mock | ||
import org.mockito.kotlin.verify | ||
|
||
class EthereumDebugTraceLowerBoundErrorHandlerTest { | ||
|
||
@ParameterizedTest | ||
@MethodSource("requests") | ||
fun `update lower bound`(request: ChainRequest, type: LowerBoundType) { | ||
val upstream = mock<Upstream>() | ||
val handler = EthereumDebugTraceLowerBoundErrorHandler | ||
|
||
handler.handle(upstream, request, "missing trie node d5648cc9aef48154159d53800f2f") | ||
|
||
verify(upstream).updateLowerBound(213229736, type) | ||
} | ||
|
||
@Test | ||
fun `update lower bound base on regexp`() { | ||
val upstream = mock<Upstream>() | ||
val handler = EthereumDebugTraceLowerBoundErrorHandler | ||
|
||
handler.handle(upstream, ChainRequest("trace_block", ListParams("0xCB5A0A8")), "block #1 not found") | ||
|
||
verify(upstream).updateLowerBound(213229736, LowerBoundType.TRACE) | ||
} | ||
|
||
companion object { | ||
@JvmStatic | ||
fun requests(): List<Arguments> = | ||
listOf( | ||
of(ChainRequest("trace_block", ListParams("0xCB5A0A8")), LowerBoundType.TRACE), | ||
of(ChainRequest("arbtrace_block", ListParams("0xCB5A0A8")), LowerBoundType.TRACE), | ||
of(ChainRequest("debug_traceBlockByNumber", ListParams("0xCB5A0A8", mapOf("tracer" to "tracer"))), LowerBoundType.DEBUG), | ||
of(ChainRequest("trace_callMany", ListParams(arrayOf(mapOf("val" to 1)), "0xCB5A0A8")), LowerBoundType.TRACE), | ||
of(ChainRequest("arbtrace_callMany", ListParams(arrayOf(mapOf("val" to 1)), "0xCB5A0A8")), LowerBoundType.TRACE), | ||
of(ChainRequest("debug_traceCall", ListParams(mapOf("val" to 1), "0xCB5A0A8", mapOf("val" to 1))), LowerBoundType.DEBUG), | ||
of(ChainRequest("trace_call", ListParams(mapOf("val" to 1), arrayOf(mapOf("val" to 1)), "0xCB5A0A8")), LowerBoundType.TRACE), | ||
of(ChainRequest("arbtrace_call", ListParams(mapOf("val" to 1), arrayOf(mapOf("val" to 1)), "0xCB5A0A8")), LowerBoundType.TRACE), | ||
) | ||
} | ||
} |
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