Check gas price (#494)

* add gas validation option

* gas price config for chains

* check gas price

* add tests

* fix txt

* make gas price dls

* fix config
This commit is contained in:
Anton
2024-05-31 15:46:55 +03:00
committed by GitHub
parent 4aff35994f
commit 772142e156
9 changed files with 133 additions and 19 deletions

View File

@@ -13,6 +13,7 @@ class ChainOptions {
val minPeers: Int,
val validateSyncing: Boolean,
val validateCallLimit: Boolean,
val validateGasPrice: Boolean,
val validateChain: Boolean,
val callLimitSize: Int,
)
@@ -30,6 +31,7 @@ class ChainOptions {
var providesBalance: Boolean? = null,
var validatePeers: Boolean? = null,
var validateCallLimit: Boolean? = null,
var validateGasPrice: Boolean? = null,
var minPeers: Int? = null,
var validateSyncing: Boolean? = null,
var validateChain: Boolean? = null,
@@ -56,6 +58,7 @@ class ChainOptions {
copy.providesBalance = overwrites.providesBalance ?: this.providesBalance
copy.validateSyncing = overwrites.validateSyncing ?: this.validateSyncing
copy.validateCallLimit = overwrites.validateCallLimit ?: this.validateCallLimit
copy.validateGasPrice = overwrites.validateGasPrice ?: this.validateGasPrice
copy.timeout = overwrites.timeout ?: this.timeout
copy.validateChain = overwrites.validateChain ?: this.validateChain
copy.disableUpstreamValidation =
@@ -75,6 +78,7 @@ class ChainOptions {
this.minPeers ?: 1,
this.validateSyncing ?: true,
this.validateCallLimit ?: true,
this.validateGasPrice ?: true,
this.validateChain ?: true,
this.callLimitSize ?: 1_000_000,
)

View File

@@ -25,6 +25,9 @@ class ChainOptionsReader : YamlConfigReader<ChainOptions.PartialOptions>() {
getValueAsBool(values, "validate-call-limit")?.let {
options.validateCallLimit = it
}
getValueAsBool(values, "validate-gas-price")?.let {
options.validateGasPrice = it
}
getValueAsBool(values, "validate-chain")?.let {
options.validateChain = it
}

View File

@@ -12,11 +12,29 @@ data class ChainsConfig(private val chains: List<ChainConfig>) : Iterable<Chains
override fun iterator(): Iterator<ChainConfig> {
return chains.iterator()
}
companion object {
@JvmStatic
fun default(): ChainsConfig = ChainsConfig(emptyList())
}
class GasPriceCondition(private val condition: String) {
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")
}
}
fun rules() = condition
}
data class ChainConfig(
val expectedBlockTime: Duration,
val syncingLagSize: Int,
@@ -30,7 +48,8 @@ data class ChainsConfig(private val chains: List<ChainConfig>) : Iterable<Chains
val callLimitContract: String?,
val id: String,
val blockchain: String,
val type: String
val type: String,
val gasPriceCondition: GasPriceCondition? = null,
) {
companion object {
@JvmStatic
@@ -50,12 +69,15 @@ data class ChainsConfig(private val chains: List<ChainConfig>) : Iterable<Chains
callLimitContract,
"undefined",
"undefined",
"unknown"
"unknown",
null,
)
@JvmStatic
fun defaultWithGasPriceCondition(gasPriceCondition: String) = defaultWithContract(null).copy(
gasPriceCondition = GasPriceCondition(gasPriceCondition),
)
}
}
fun resolve(chain: String): ChainConfig {

View File

@@ -86,6 +86,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")
return ChainsConfig.ChainConfig(
expectedBlockTime = expectedBlockTime,
syncingLagSize = lags.first,
@@ -99,7 +100,8 @@ class ChainsConfigReader(
shortNames = shortNames,
id = id,
blockchain = blockchain,
type = type
type = type,
gasPriceCondition = gasPriceCondition?.let { ChainsConfig.GasPriceCondition(gasPriceCondition) },
)
}