Check gas price multiply conditions (#496)

* multiply conditions

* update doc
This commit is contained in:
Anton
2024-06-03 12:51:50 +03:00
committed by GitHub
parent 724e9be98a
commit 753b7d8503
6 changed files with 87 additions and 33 deletions

View File

@@ -18,21 +18,30 @@ data class ChainsConfig(private val chains: List<ChainConfig>) : Iterable<Chains
fun default(): ChainsConfig = ChainsConfig(emptyList())
}
class GasPriceCondition(private val condition: String) {
class GasPriceCondition(rawConditions: List<String>) {
private val conditions: List<Pair<String, Long>> = rawConditions.map {
val parts = it.split(" ")
if (parts.size != 2 || listOf("ne", "eq", "gt", "lt", "ge", "le").none { op -> op == parts[0] }) {
throw IllegalArgumentException("Invalid condition: $it")
}
Pair(parts[0], parts[1].toLong())
}
fun check(value: Long): Boolean {
val (op, valueStr) = condition.split(" ")
return when (op) {
"ne" -> value != valueStr.toLong()
"eq" -> value == valueStr.toLong()
"gt" -> value > valueStr.toLong()
"lt" -> value < valueStr.toLong()
"ge" -> value >= valueStr.toLong()
"le" -> value <= valueStr.toLong()
else -> throw IllegalArgumentException("Unsupported condition: $condition")
return conditions.all { (op, limit) ->
when (op) {
"ne" -> value != limit
"eq" -> value == limit
"gt" -> value > limit
"lt" -> value < limit
"ge" -> value >= limit
"le" -> value <= limit
else -> false
}
}
}
fun rules() = condition
fun rules() = conditions.joinToString { (op, limit) -> "$op $limit" }
}
data class ChainConfig(
@@ -49,7 +58,7 @@ data class ChainsConfig(private val chains: List<ChainConfig>) : Iterable<Chains
val id: String,
val blockchain: String,
val type: String,
val gasPriceCondition: GasPriceCondition? = null,
val gasPriceCondition: GasPriceCondition,
) {
companion object {
@JvmStatic
@@ -70,12 +79,12 @@ data class ChainsConfig(private val chains: List<ChainConfig>) : Iterable<Chains
"undefined",
"undefined",
"unknown",
null,
GasPriceCondition(emptyList()),
)
@JvmStatic
fun defaultWithGasPriceCondition(gasPriceCondition: String) = defaultWithContract(null).copy(
gasPriceCondition = GasPriceCondition(gasPriceCondition),
fun defaultWithGasPriceCondition(gasPriceConditions: List<String>) = defaultWithContract(null).copy(
gasPriceCondition = GasPriceCondition(gasPriceConditions),
)
}
}

View File

@@ -35,14 +35,47 @@ class ChainsConfigReader(
MappingNode(
chain.tag,
listOf(
NodeTuple(ScalarNode(Tag.STR, "settings", null, null, DumperOptions.ScalarStyle.LITERAL), chainSettings),
NodeTuple(
ScalarNode(Tag.STR, "blockchain", null, null, DumperOptions.ScalarStyle.LITERAL),
ScalarNode(Tag.STR, blockchain, null, null, DumperOptions.ScalarStyle.LITERAL),
ScalarNode(
Tag.STR,
"settings",
null,
null,
DumperOptions.ScalarStyle.LITERAL,
),
chainSettings,
),
NodeTuple(
ScalarNode(Tag.STR, "type", null, null, DumperOptions.ScalarStyle.LITERAL),
ScalarNode(Tag.STR, type, null, null, DumperOptions.ScalarStyle.LITERAL),
ScalarNode(
Tag.STR,
"blockchain",
null,
null,
DumperOptions.ScalarStyle.LITERAL,
),
ScalarNode(
Tag.STR,
blockchain,
null,
null,
DumperOptions.ScalarStyle.LITERAL,
),
),
NodeTuple(
ScalarNode(
Tag.STR,
"type",
null,
null,
DumperOptions.ScalarStyle.LITERAL,
),
ScalarNode(
Tag.STR,
type,
null,
null,
DumperOptions.ScalarStyle.LITERAL,
),
),
),
chain.flowStyle,
@@ -62,7 +95,8 @@ class ChainsConfigReader(
private fun parseChain(blockchain: String, node: MappingNode): ChainsConfig.ChainConfig {
val id = getValueAsString(node, "id")
?: throw IllegalArgumentException("undefined id for $blockchain")
val settings = getMapping(node, "settings") ?: throw IllegalArgumentException("undefined settings for $blockchain")
val settings =
getMapping(node, "settings") ?: throw IllegalArgumentException("undefined settings for $blockchain")
val lags = getMapping(settings, "lags")?.let { lagConfig ->
Pair(
getValueAsInt(lagConfig, "syncing")
@@ -86,7 +120,7 @@ class ChainsConfigReader(
?: throw IllegalArgumentException("undefined shortnames for $blockchain")
val type = getValueAsString(node, "type")
?: throw IllegalArgumentException("undefined type for $blockchain")
val gasPriceCondition = getValueAsString(node, "gas-price-condition")
val gasPriceConditions = getListOfString(node, "gas-price-condition") ?: emptyList()
return ChainsConfig.ChainConfig(
expectedBlockTime = expectedBlockTime,
syncingLagSize = lags.first,
@@ -101,7 +135,7 @@ class ChainsConfigReader(
id = id,
blockchain = blockchain,
type = type,
gasPriceCondition = gasPriceCondition?.let { ChainsConfig.GasPriceCondition(gasPriceCondition) },
gasPriceCondition = ChainsConfig.GasPriceCondition(gasPriceConditions),
)
}
@@ -118,6 +152,7 @@ class ChainsConfigReader(
val merged = mergeMappingNode(defChain.second, curChain.second)
parseChain(defChain.first, merged!!)
}
else -> ChainsConfig.ChainConfig.default()
}
}

View File

@@ -191,7 +191,9 @@ chain-settings:
grpcId: 1006
chain-id: 0x38
short-names: [bsc, binance, bnb-smart-chain]
gas-price-condition: ne 3000000000
gas-price-condition:
- ne 3000000000
- ne 5000000000
- id: Testnet
priority: 1
code: BSC_TESTNET
@@ -610,7 +612,8 @@ chain-settings:
short-names: [kava]
chain-id: 0x8ae
grpcId: 1025
gas-price-condition: eq 1000000000
gas-price-condition:
- eq 1000000000
- id: Testnet
priority: 10
code: KAVA_TESTNET