Skip to content

Commit

Permalink
make gas price dls
Browse files Browse the repository at this point in the history
  • Loading branch information
tonatoz committed May 31, 2024
1 parent 630f5da commit 5efa3d6
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 30 deletions.
46 changes: 24 additions & 22 deletions foundation/src/main/kotlin/org/drpc/chainsconfig/ChainsConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -31,14 +49,14 @@ data class ChainsConfig(private val chains: List<ChainConfig>) : Iterable<Chains
val id: String,
val blockchain: String,
val type: String,
val gasPrice: Long,
val gasPriceCondition: GasPriceCondition? = null,
) {
companion object {
@JvmStatic
fun default() = defaultWithContract(null)

@JvmStatic
fun defaultWithGasPrice(gasPrice: Long) = ChainConfig(
fun defaultWithContract(callLimitContract: String?) = ChainConfig(
Duration.ofSeconds(12),
6,
1,
Expand All @@ -48,34 +66,18 @@ data class ChainsConfig(private val chains: List<ChainConfig>) : Iterable<Chains
0,
"UNKNOWN",
emptyList(),
null,
callLimitContract,
"undefined",
"undefined",
"unknown",
gasPrice,
null,
)

@JvmStatic
fun defaultWithContract(callLimitContract: String?) = ChainConfig(
Duration.ofSeconds(12),
6,
1,
ChainOptions.PartialOptions(),
"0x0",
BigInteger.ZERO,
0,
"UNKNOWN",
emptyList(),
callLimitContract,
"undefined",
"undefined",
"unknown",
0,
fun defaultWithGasPriceCondition(gasPriceCondition: String) = defaultWithContract(null).copy(
gasPriceCondition = GasPriceCondition(gasPriceCondition),
)
}



}

fun resolve(chain: String): ChainConfig {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class ChainsConfigReader(
?: throw IllegalArgumentException("undefined shortnames for $blockchain")
val type = getValueAsString(node, "type")
?: throw IllegalArgumentException("undefined type for $blockchain")
val gasPrice = getValueAsLong(node, "gas-price") ?: 0
val gasPriceCondition = getValueAsString(node, "gas-price-condition")
return ChainsConfig.ChainConfig(
expectedBlockTime = expectedBlockTime,
syncingLagSize = lags.first,
Expand All @@ -101,7 +101,7 @@ class ChainsConfigReader(
id = id,
blockchain = blockchain,
type = type,
gasPrice = gasPrice,
gasPriceCondition = gasPriceCondition?.let { ChainsConfig.GasPriceCondition(gasPriceCondition) },
)
}

Expand Down
2 changes: 1 addition & 1 deletion foundation/src/main/resources/chains.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ chain-settings:
grpcId: 1006
chain-id: 0x38
short-names: [bsc, binance, bnb-smart-chain]
gas-price: 1000000000L
gas-price-condition: ne 1000000000L
- id: Testnet
priority: 1
code: BSC_TESTNET
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,18 +197,18 @@ open class EthereumUpstreamValidator @JvmOverloads constructor(
}

private fun validateGasPrice(): Mono<ValidateUpstreamSettingsResult> {
if (!options.validateGasPrice || config.gasPrice <= 0) {
if (!options.validateGasPrice || config.gasPriceCondition == null) {
return Mono.just(ValidateUpstreamSettingsResult.UPSTREAM_VALID)
}
return upstream.getIngressReader()
.read(ChainRequest("eth_gasPrice", ListParams()))
.flatMap(ChainResponse::requireStringResult)
.map { result ->
val actualGasPrice = result.substring(2).toLong(16)
if (actualGasPrice != config.gasPrice) {
if (!config.gasPriceCondition!!.check(actualGasPrice)) {
log.warn(
"Node ${upstream.getId()} has gasPrice $actualGasPrice, " +
"but it is not equal to the required ${config.gasPrice}",
"but it is not equal to the required ${config.gasPriceCondition!!.rules()}",
)
ValidateUpstreamSettingsResult.UPSTREAM_FATAL_SETTINGS_ERROR
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ class EthereumUpstreamValidatorSpec extends Specification {
it.validateChain = false
it.validateCallLimit = false
}.buildOptions()
def conf = ChainConfig.defaultWithGasPrice(1000000000)
def conf = ChainConfig.defaultWithGasPriceCondition("ne 3000000000")
def up = Mock(Upstream) {
3 * getIngressReader() >>
Mock(Reader) {
Expand All @@ -374,7 +374,7 @@ class EthereumUpstreamValidatorSpec extends Specification {
it.validateChain = false
it.validateCallLimit = false
}.buildOptions()
def conf = ChainConfig.defaultWithGasPrice(1000000000)
def conf = ChainConfig.defaultWithGasPriceCondition("eq 1000000000")
def up = Mock(Upstream) {
3 * getIngressReader() >>
Mock(Reader) {
Expand Down

0 comments on commit 5efa3d6

Please sign in to comment.