-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Calculate lower bounds inside range + adaptive update (#485)
- Loading branch information
1 parent
100578d
commit 75d6fb3
Showing
21 changed files
with
301 additions
and
28 deletions.
There are no files selected for viewing
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
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
66 changes: 66 additions & 0 deletions
66
src/main/kotlin/io/emeraldpay/dshackle/upstream/error/EthereumStateLowerBoundErrorHandler.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,66 @@ | ||
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 | ||
import io.emeraldpay.dshackle.upstream.rpcclient.ListParams | ||
import org.slf4j.LoggerFactory | ||
|
||
object EthereumStateLowerBoundErrorHandler : ErrorHandler { | ||
private val log = LoggerFactory.getLogger(this::class.java) | ||
|
||
private val firstTagIndexMethods = setOf( | ||
"eth_call", | ||
"debug_traceCall", | ||
"eth_getBalance", | ||
"eth_estimateGas", | ||
"eth_getCode", | ||
"eth_getTransactionCount", | ||
) | ||
private val secondTagIndexMethods = setOf( | ||
"eth_getProof", | ||
"eth_getStorageAt", | ||
) | ||
|
||
private val applicableMethods = firstTagIndexMethods + secondTagIndexMethods | ||
|
||
override fun handle(upstream: Upstream, request: ChainRequest, errorMessage: String?) { | ||
try { | ||
if (canHandle(request, errorMessage)) { | ||
parseTagParam(request, tagIndex(request.method))?.let { | ||
upstream.updateLowerBound(it, LowerBoundType.STATE) | ||
} | ||
} | ||
} catch (e: RuntimeException) { | ||
log.warn("Couldn't update the {} lower bound of {}, reason - {}", LowerBoundType.STATE, upstream.getId(), e.message) | ||
} | ||
} | ||
|
||
override fun canHandle(request: ChainRequest, errorMessage: String?): Boolean { | ||
return stateErrors.any { errorMessage?.contains(it) ?: false } && applicableMethods.contains(request.method) | ||
} | ||
|
||
private 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 | ||
} | ||
|
||
private fun tagIndex(method: String): Int { | ||
return if (firstTagIndexMethods.contains(method)) { | ||
1 | ||
} else if (secondTagIndexMethods.contains(method)) { | ||
2 | ||
} else { | ||
-1 | ||
} | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
src/main/kotlin/io/emeraldpay/dshackle/upstream/error/UpstreamErrorHandler.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,35 @@ | ||
package io.emeraldpay.dshackle.upstream.error | ||
|
||
import io.emeraldpay.dshackle.config.context.SchedulersConfig | ||
import io.emeraldpay.dshackle.upstream.ChainRequest | ||
import io.emeraldpay.dshackle.upstream.Upstream | ||
import org.springframework.scheduling.concurrent.CustomizableThreadFactory | ||
import java.util.concurrent.CompletableFuture | ||
import java.util.concurrent.Executors | ||
|
||
interface ErrorHandler { | ||
fun handle(upstream: Upstream, request: ChainRequest, errorMessage: String?) | ||
|
||
fun canHandle(request: ChainRequest, errorMessage: String?): Boolean | ||
} | ||
|
||
object UpstreamErrorHandler { | ||
private val errorHandlers = listOf( | ||
EthereumStateLowerBoundErrorHandler, | ||
) | ||
private val errorHandlerExecutor = Executors.newFixedThreadPool( | ||
2 * SchedulersConfig.threadsMultiplier, | ||
CustomizableThreadFactory("error-handler-"), | ||
) | ||
|
||
fun handle(upstream: Upstream, request: ChainRequest, errorMessage: String?) { | ||
CompletableFuture.runAsync( | ||
{ | ||
errorHandlers | ||
.filter { it.canHandle(request, errorMessage) } | ||
.forEach { it.handle(upstream, request, errorMessage) } | ||
}, | ||
errorHandlerExecutor, | ||
) | ||
} | ||
} |
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
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.